From b0e8ebbf718d4c277c820bff879c9b285b82bc4a Mon Sep 17 00:00:00 2001 From: William Brawner Date: Thu, 1 Aug 2024 18:47:12 -0600 Subject: [PATCH] Fix failing UI tests --- .../wbrawner/simplemarkdown/MarkdownTests.kt | 44 +++++++++++++------ .../simplemarkdown/MarkdownViewModel.kt | 2 +- .../wbrawner/simplemarkdown/ui/MainScreen.kt | 2 +- app/src/main/res/values/strings.xml | 2 +- 4 files changed, 34 insertions(+), 16 deletions(-) diff --git a/app/src/androidTest/kotlin/com/wbrawner/simplemarkdown/MarkdownTests.kt b/app/src/androidTest/kotlin/com/wbrawner/simplemarkdown/MarkdownTests.kt index 261f968..6aba4f9 100644 --- a/app/src/androidTest/kotlin/com/wbrawner/simplemarkdown/MarkdownTests.kt +++ b/app/src/androidTest/kotlin/com/wbrawner/simplemarkdown/MarkdownTests.kt @@ -6,13 +6,10 @@ import android.content.Context import android.content.Intent import android.net.Uri import android.webkit.WebView -import androidx.compose.ui.input.key.Key -import androidx.compose.ui.input.key.KeyEvent -import androidx.compose.ui.input.key.NativeKeyEvent import androidx.compose.ui.semantics.SemanticsProperties import androidx.compose.ui.semantics.getOrNull -import androidx.compose.ui.test.ExperimentalTestApi import androidx.compose.ui.test.SemanticsMatcher +import androidx.compose.ui.test.SemanticsNodeInteraction import androidx.compose.ui.test.assert import androidx.compose.ui.test.assertIsDisplayed import androidx.compose.ui.test.assertIsNotDisplayed @@ -27,13 +24,8 @@ import androidx.compose.ui.test.junit4.createEmptyComposeRule import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick -import androidx.compose.ui.test.performKeyInput -import androidx.compose.ui.test.performKeyPress -import androidx.compose.ui.test.performTextInput -import androidx.compose.ui.test.performTextInputSelection import androidx.compose.ui.test.performTextReplacement import androidx.compose.ui.test.printToLog -import androidx.compose.ui.text.TextRange import androidx.core.content.FileProvider import androidx.test.core.app.ActivityScenario import androidx.test.core.app.ApplicationProvider.getApplicationContext @@ -233,7 +225,7 @@ class MarkdownTests { private fun ComposeTestRule.checkTitleEquals(title: String) = onNode(hasAnySibling(hasContentDescription("Main Menu")).and(hasText(title))) - .assertIsDisplayed() + .waitUntilIsDisplayed() private fun ComposeTestRule.typeMarkdown(markdown: String) = onNode(hasSetTextAction()).performTextReplacement(markdown) @@ -258,12 +250,38 @@ class MarkdownTests { private fun ComposeTestRule.clickSaveMenuItem() = onNodeWithText("Save").performClick() private fun ComposeTestRule.verifyDialogIsShown(text: String) = - onNode(isDialog().and(hasAnyDescendant(hasText(text)))).assertIsDisplayed() + onNode(isDialog().and(hasAnyDescendant(hasText(text)))).waitUntilIsDisplayed() - private fun ComposeTestRule.verifyDialogIsNotShown() = onNode(isDialog()).assertIsNotDisplayed() + private fun ComposeTestRule.verifyDialogIsNotShown() = + onNode(isDialog()).waitUntilIsNotDisplayed() private fun ComposeTestRule.discardChanges() = onNodeWithText("No").performClick() private fun ComposeTestRule.verifyTextIsShown(text: String) = - onNodeWithText(text).assertIsDisplayed() + onNodeWithText(text).waitUntilIsDisplayed() + + private val ASSERTION_TIMEOUT = 5_000L + + private fun SemanticsNodeInteraction.waitUntil(assertion: SemanticsNodeInteraction.() -> Unit) { + val start = System.currentTimeMillis() + lateinit var assertionError: AssertionError + while (System.currentTimeMillis() - start < ASSERTION_TIMEOUT) { + try { + assertion() + return + } catch (e: AssertionError) { + assertionError = e + Thread.sleep(10) + } + } + throw assertionError + } + + private fun SemanticsNodeInteraction.waitUntilIsDisplayed() = waitUntil { + assertIsDisplayed() + } + + private fun SemanticsNodeInteraction.waitUntilIsNotDisplayed() = waitUntil { + assertIsNotDisplayed() + } } diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/MarkdownViewModel.kt b/app/src/main/java/com/wbrawner/simplemarkdown/MarkdownViewModel.kt index 835bb0d..ee6fb09 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/MarkdownViewModel.kt +++ b/app/src/main/java/com/wbrawner/simplemarkdown/MarkdownViewModel.kt @@ -96,7 +96,7 @@ class MarkdownViewModel( markdown = content, initialMarkdown = content, reloadToggle = currentState.reloadToggle.inv(), - toast = ParameterizedText(R.string.file_loaded) + toast = ParameterizedText(R.string.file_loaded, arrayOf(name)) ) preferenceHelper[Preference.AUTOSAVE_URI] = actualLoadPath } ?: throw IllegalStateException("Opened file was null") diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/ui/MainScreen.kt b/app/src/main/java/com/wbrawner/simplemarkdown/ui/MainScreen.kt index a1cb013..62a39d0 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/ui/MainScreen.kt +++ b/app/src/main/java/com/wbrawner/simplemarkdown/ui/MainScreen.kt @@ -380,4 +380,4 @@ fun

MarkdownViewModel.collectAsState(prop: KProperty1, initi remember(prop) { state.map { prop.get(it) }.distinctUntilChanged() }.collectAsState(initial) @Composable -fun ParameterizedText.stringRes() = stringResource(text, params) \ No newline at end of file +fun ParameterizedText.stringRes() = stringResource(text, *params) \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3ab3f56..b8fd010 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -16,7 +16,7 @@ Share file to… Successfully saved %1$s An error occurred while saving the file - File successfully loaded + Successfully loaded %1$s An error occurred while opening the file Back Main Menu