From 0d2325fc561dec9d269af6d8d868c9974fd998b7 Mon Sep 17 00:00:00 2001 From: Matthias Emde Date: Sun, 12 Jan 2025 12:22:47 +0100 Subject: [PATCH] revert 'assertWithLease' --- app/src/androidTest/java/app/ComposeRule.kt | 39 ---------------- .../presentation/ActiveSessionScreenTest.kt | 13 +++--- .../core/presentation/MusikusBottomBarTest.kt | 33 +++++--------- .../core/presentation/MusikusNavHostTest.kt | 33 +++++++------- .../LibraryFolderDetailsScreenTest.kt | 45 ++++++++----------- .../presentation/LibraryIntegrationTest.kt | 5 +-- .../library/presentation/LibraryScreenTest.kt | 17 +++---- 7 files changed, 59 insertions(+), 126 deletions(-) diff --git a/app/src/androidTest/java/app/ComposeRule.kt b/app/src/androidTest/java/app/ComposeRule.kt index e85664fa..ebe36832 100644 --- a/app/src/androidTest/java/app/ComposeRule.kt +++ b/app/src/androidTest/java/app/ComposeRule.kt @@ -10,45 +10,6 @@ package app import androidx.compose.ui.test.SemanticsNodeInteraction import androidx.compose.ui.test.getBoundsInRoot -import androidx.compose.ui.test.junit4.AndroidComposeTestRule -import kotlin.time.Duration.Companion.milliseconds - -val LeaseSleepDuration = 500.milliseconds -const val LeaseDefaultAttempts = 10 - -fun AndroidComposeTestRule<*, *>.assertWithLease( - attempts: Int = LeaseDefaultAttempts, - assertion: () -> Unit -) { - try { - assertion() - } catch (e: Throwable) { - if (attempts > 0) { - Thread.sleep(LeaseSleepDuration.inWholeMilliseconds) - assertWithLease(attempts - 1, assertion) - } else { - throw e - } - } -} - -fun SemanticsNodeInteraction.assertWithLease( - attempts: Int = LeaseDefaultAttempts, - assertion: SemanticsNodeInteraction.() -> Unit -): SemanticsNodeInteraction { - try { - assertion() - } catch (e: Throwable) { - if (attempts > 0) { - Thread.sleep(LeaseSleepDuration.inWholeMilliseconds) - assertWithLease(attempts - 1, assertion) - } else { - throw e - } - } - - return this -} /** * Asserts that the given SemanticsNodeInteractions are vertically ordered on the screen. diff --git a/app/src/androidTest/java/app/musikus/activesession/presentation/ActiveSessionScreenTest.kt b/app/src/androidTest/java/app/musikus/activesession/presentation/ActiveSessionScreenTest.kt index e5ef2408..e631b12b 100644 --- a/app/src/androidTest/java/app/musikus/activesession/presentation/ActiveSessionScreenTest.kt +++ b/app/src/androidTest/java/app/musikus/activesession/presentation/ActiveSessionScreenTest.kt @@ -23,7 +23,6 @@ import androidx.compose.ui.test.performTextInput import androidx.navigation.NavHostController import androidx.test.filters.SdkSuppress import app.ScreenshotRule -import app.assertWithLease import app.musikus.core.data.Nullable import app.musikus.core.data.SectionWithLibraryItem import app.musikus.core.data.SessionWithSectionsWithLibraryItems @@ -129,7 +128,7 @@ class ActiveSessionScreenTest { composeRule.onNodeWithContentDescription("Start practicing").performClick() composeRule.onNodeWithText("TestItem1").performClick() - composeRule.onNodeWithContentDescription("Next item").assertWithLease { assertIsDisplayed() } + composeRule.onNodeWithContentDescription("Next item").assertIsDisplayed() } @Test @@ -142,13 +141,13 @@ class ActiveSessionScreenTest { composeRule.onNodeWithContentDescription("Pause").performClick() // Pause timer is displayed - composeRule.onNodeWithText("Paused 00:00").assertWithLease { assertIsDisplayed() } + composeRule.onNodeWithText("Paused 00:00").assertIsDisplayed() fakeTimeProvider.advanceTimeBy(90.seconds) // Pause timer shows correct time composeRule.onNodeWithText("Paused 01:30") - .assertWithLease { assertIsDisplayed() } + .assertIsDisplayed() .performClick() // Resume session // Pause timer is hidden @@ -171,7 +170,7 @@ class ActiveSessionScreenTest { matcher = hasText("TestItem3") and hasAnySibling(hasText("00:00")) - ).assertWithLease { assertIsDisplayed() } + ).assertIsDisplayed() } @Test @@ -183,7 +182,7 @@ class ActiveSessionScreenTest { composeRule.onNodeWithText("TestItem1").performClick() // Item is selected - composeRule.onNodeWithText("TestItem1").assertWithLease { assertIsDisplayed() } + composeRule.onNodeWithText("TestItem1").assertIsDisplayed() // Open item selector again composeRule.onNodeWithContentDescription("Next item").performClick() @@ -192,7 +191,7 @@ class ActiveSessionScreenTest { composeRule.onNodeWithText("TestItem2").performClick() // Item is selected - composeRule.onNodeWithText("TestItem2").assertWithLease { assertIsDisplayed() } + composeRule.onNodeWithText("TestItem2").assertIsDisplayed() } @Test diff --git a/app/src/androidTest/java/app/musikus/core/presentation/MusikusBottomBarTest.kt b/app/src/androidTest/java/app/musikus/core/presentation/MusikusBottomBarTest.kt index 97e7fabe..94628e78 100644 --- a/app/src/androidTest/java/app/musikus/core/presentation/MusikusBottomBarTest.kt +++ b/app/src/androidTest/java/app/musikus/core/presentation/MusikusBottomBarTest.kt @@ -10,19 +10,15 @@ package app.musikus.core.presentation import androidx.activity.compose.setContent import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember -import androidx.compose.runtime.setValue -import androidx.compose.ui.test.assertIsSelected import androidx.compose.ui.test.junit4.createAndroidComposeRule import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick import androidx.hilt.navigation.compose.hiltViewModel import androidx.lifecycle.compose.collectAsStateWithLifecycle +import androidx.navigation.NavHostController import app.musikus.core.domain.FakeTimeProvider import dagger.hilt.android.testing.HiltAndroidRule import dagger.hilt.android.testing.HiltAndroidTest -import io.mockk.every import io.mockk.mockk import io.mockk.verify import kotlinx.coroutines.test.runTest @@ -42,7 +38,7 @@ class MusikusBottomBarTest { @get:Rule(order = 1) val composeRule = createAndroidComposeRule() - lateinit var onTabSelectedMock: (HomeTab) -> Unit + lateinit var navController: NavHostController @Before fun setUp() { @@ -53,16 +49,15 @@ class MusikusBottomBarTest { val mainUiState by mainViewModel.uiState.collectAsStateWithLifecycle() val eventHandler = mainViewModel::onUiEvent - var currentTab: HomeTab by remember { mutableStateOf(HomeTab.Sessions) } - - onTabSelectedMock = mockk() - every { onTabSelectedMock(any()) } answers { currentTab = firstArg() } + navController = mockk(relaxed = true) MusikusBottomBar( mainUiState = mainUiState, mainEventHandler = eventHandler, - currentTab = currentTab, - onTabSelected = onTabSelectedMock, + currentTab = HomeTab.Sessions, + onTabSelected = { selectedTab -> + navController.navigate(Screen.Home(selectedTab)) + }, ) } } @@ -73,7 +68,7 @@ class MusikusBottomBarTest { // Since we are already on the sessions tab, we should not navigate verify(exactly = 0) { - onTabSelectedMock(any()) + navController.navigate(any()) } } @@ -82,10 +77,8 @@ class MusikusBottomBarTest { composeRule.onNodeWithText("Goals").performClick() verify(exactly = 1) { - onTabSelectedMock(HomeTab.Goals) + navController.navigate(Screen.Home(HomeTab.Goals)) } - - composeRule.onNodeWithText("Goals").assertIsSelected() } @Test @@ -93,10 +86,8 @@ class MusikusBottomBarTest { composeRule.onNodeWithText("Statistics").performClick() verify(exactly = 1) { - onTabSelectedMock(HomeTab.Statistics) + navController.navigate(Screen.Home(HomeTab.Statistics)) } - - composeRule.onNodeWithText("Statistics").assertIsSelected() } @Test @@ -104,9 +95,7 @@ class MusikusBottomBarTest { composeRule.onNodeWithText("Library").performClick() verify(exactly = 1) { - onTabSelectedMock(HomeTab.Library) + navController.navigate(Screen.Home(HomeTab.Library)) } - - composeRule.onNodeWithText("Library").assertIsSelected() } } diff --git a/app/src/androidTest/java/app/musikus/core/presentation/MusikusNavHostTest.kt b/app/src/androidTest/java/app/musikus/core/presentation/MusikusNavHostTest.kt index 9ecd4abd..b3dfdcb8 100644 --- a/app/src/androidTest/java/app/musikus/core/presentation/MusikusNavHostTest.kt +++ b/app/src/androidTest/java/app/musikus/core/presentation/MusikusNavHostTest.kt @@ -19,7 +19,6 @@ import androidx.hilt.navigation.compose.hiltViewModel import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.navigation.compose.ComposeNavigator import androidx.navigation.testing.TestNavHostController -import app.assertWithLease import app.musikus.core.data.UUIDConverter import app.musikus.core.domain.FakeTimeProvider import app.musikus.core.presentation.theme.MusikusTheme @@ -86,7 +85,7 @@ class MusikusNavHostTest { require(screen is Screen.Home) assertThat(screen.tab).isEqualTo(HomeTab.Sessions) - composeRule.onNodeWithText("Sessions").assertWithLease { assertIsDisplayed() } + composeRule.onNodeWithText("Sessions").assertIsDisplayed() } @Test @@ -103,7 +102,7 @@ class MusikusNavHostTest { require(screen is Screen.Home) assertThat(screen.tab).isEqualTo(HomeTab.Goals) - composeRule.onNodeWithText("Goals").assertWithLease { assertIsDisplayed() } + composeRule.onNodeWithText("Goals").assertIsDisplayed() } @Test @@ -119,7 +118,7 @@ class MusikusNavHostTest { assertThat(screen).isInstanceOf(Screen.Home::class.java) require(screen is Screen.Home) assertThat(screen.tab).isEqualTo(HomeTab.Statistics) - composeRule.onNodeWithText("Statistics").assertWithLease { assertIsDisplayed() } + composeRule.onNodeWithText("Statistics").assertIsDisplayed() } @Test @@ -136,7 +135,7 @@ class MusikusNavHostTest { require(screen is Screen.Home) assertThat(screen.tab).isEqualTo(HomeTab.Library) - composeRule.onNodeWithText("Library").assertWithLease { assertIsDisplayed() } + composeRule.onNodeWithText("Library").assertIsDisplayed() } @Test @@ -150,7 +149,7 @@ class MusikusNavHostTest { val screen = navController.currentBackStackEntry?.toScreen() assertThat(screen).isInstanceOf(Screen.LibraryFolderDetails::class.java) - composeRule.onNodeWithText("Folder not found").assertWithLease { assertIsDisplayed() } + composeRule.onNodeWithText("Folder not found").assertIsDisplayed() } @Test @@ -164,7 +163,7 @@ class MusikusNavHostTest { val screen = navController.currentBackStackEntry?.toScreen() assertThat(screen).isInstanceOf(Screen.ActiveSession::class.java) - composeRule.onNodeWithText("Practice Time").assertWithLease { assertIsDisplayed() } + composeRule.onNodeWithText("Practice Time").assertIsDisplayed() } @Test @@ -178,7 +177,7 @@ class MusikusNavHostTest { val screen = navController.currentBackStackEntry?.toScreen() assertThat(screen).isInstanceOf(Screen.SessionStatistics::class.java) - composeRule.onNodeWithText("Session History").assertWithLease { assertIsDisplayed() } + composeRule.onNodeWithText("Session History").assertIsDisplayed() } @Test @@ -192,7 +191,7 @@ class MusikusNavHostTest { val screen = navController.currentBackStackEntry?.toScreen() assertThat(screen).isInstanceOf(Screen.GoalStatistics::class.java) - composeRule.onNodeWithText("Goal History").assertWithLease { assertIsDisplayed() } + composeRule.onNodeWithText("Goal History").assertIsDisplayed() } @Test @@ -206,7 +205,7 @@ class MusikusNavHostTest { val screen = navController.currentBackStackEntry?.toScreen() assertThat(screen).isInstanceOf(Screen.MainMenuEntry.Settings::class.java) - composeRule.onNodeWithText("Settings").assertWithLease { assertIsDisplayed() } + composeRule.onNodeWithText("Settings").assertIsDisplayed() } @Test @@ -220,7 +219,7 @@ class MusikusNavHostTest { val screen = navController.currentBackStackEntry?.toScreen() assertThat(screen).isInstanceOf(Screen.MainMenuEntry.Donate::class.java) - composeRule.onNodeWithText("Support us!").assertWithLease { assertIsDisplayed() } + composeRule.onNodeWithText("Support us!").assertIsDisplayed() } @Test @@ -234,7 +233,7 @@ class MusikusNavHostTest { val screen = navController.currentBackStackEntry?.toScreen() assertThat(screen).isInstanceOf(Screen.MainMenuEntry.About::class.java) - composeRule.onNodeWithText("About").assertWithLease { assertIsDisplayed() } + composeRule.onNodeWithText("About").assertIsDisplayed() } @Test @@ -248,7 +247,7 @@ class MusikusNavHostTest { val screen = navController.currentBackStackEntry?.toScreen() assertThat(screen).isInstanceOf(Screen.MainMenuEntry.Help::class.java) - composeRule.onNodeWithText("Help").assertWithLease { assertIsDisplayed() } + composeRule.onNodeWithText("Help").assertIsDisplayed() } @Test @@ -262,7 +261,7 @@ class MusikusNavHostTest { val screen = navController.currentBackStackEntry?.toScreen() assertThat(screen).isInstanceOf(Screen.SettingsOption.Backup::class.java) - composeRule.onNodeWithText("Backup and restore").assertWithLease { assertIsDisplayed() } + composeRule.onNodeWithText("Backup and restore").assertIsDisplayed() } @Test @@ -276,7 +275,7 @@ class MusikusNavHostTest { val screen = navController.currentBackStackEntry?.toScreen() assertThat(screen).isInstanceOf(Screen.SettingsOption.Export::class.java) - composeRule.onNodeWithText("Export session data").assertWithLease { assertIsDisplayed() } + composeRule.onNodeWithText("Export session data").assertIsDisplayed() } @Test @@ -290,7 +289,7 @@ class MusikusNavHostTest { val screen = navController.currentBackStackEntry?.toScreen() assertThat(screen).isInstanceOf(Screen.SettingsOption.Appearance::class.java) - composeRule.onNodeWithText("Appearance").assertWithLease { assertIsDisplayed() } + composeRule.onNodeWithText("Appearance").assertIsDisplayed() } @Test @@ -304,6 +303,6 @@ class MusikusNavHostTest { val screen = navController.currentBackStackEntry?.toScreen() assertThat(screen).isInstanceOf(Screen.License::class.java) - composeRule.onNodeWithText("Licenses").assertWithLease { assertIsDisplayed() } + composeRule.onNodeWithText("Licenses").assertIsDisplayed() } } diff --git a/app/src/androidTest/java/app/musikus/library/presentation/LibraryFolderDetailsScreenTest.kt b/app/src/androidTest/java/app/musikus/library/presentation/LibraryFolderDetailsScreenTest.kt index b10b81c1..db116a96 100644 --- a/app/src/androidTest/java/app/musikus/library/presentation/LibraryFolderDetailsScreenTest.kt +++ b/app/src/androidTest/java/app/musikus/library/presentation/LibraryFolderDetailsScreenTest.kt @@ -27,7 +27,6 @@ import androidx.hilt.navigation.compose.hiltViewModel import androidx.test.filters.SdkSuppress import app.ScreenshotRule import app.assertNodesInVerticalOrder -import app.assertWithLease import app.musikus.core.data.UUIDConverter import app.musikus.core.domain.FakeTimeProvider import app.musikus.core.presentation.MainActivity @@ -91,7 +90,7 @@ class LibraryFolderDetailsScreenTest { composeRule.onNodeWithContentDescription("Create").performClick() // Check if item is displayed - composeRule.onNodeWithText("TestItem2").assertWithLease { assertIsDisplayed() } + composeRule.onNodeWithText("TestItem2").assertIsDisplayed() } private fun clickSortMode( @@ -128,37 +127,31 @@ class LibraryFolderDetailsScreenTest { } // Check if items are displayed in correct order - composeRule.assertWithLease { - assertNodesInVerticalOrder( - composeRule.onNodeWithText("TestItem2"), - composeRule.onNodeWithText("TestItem1"), - composeRule.onNodeWithText("TestItem3") - ) - } + assertNodesInVerticalOrder( + composeRule.onNodeWithText("TestItem2"), + composeRule.onNodeWithText("TestItem1"), + composeRule.onNodeWithText("TestItem3") + ) // Change sorting mode to name descending clickSortMode("items", "Name") // Check if items are displayed in correct order - composeRule.assertWithLease { - assertNodesInVerticalOrder( - composeRule.onNodeWithText("TestItem3"), - composeRule.onNodeWithText("TestItem2"), - composeRule.onNodeWithText("TestItem1") - ) - } + assertNodesInVerticalOrder( + composeRule.onNodeWithText("TestItem3"), + composeRule.onNodeWithText("TestItem2"), + composeRule.onNodeWithText("TestItem1") + ) // Change sorting mode to name ascending clickSortMode("items", "Name") // Check if items are displayed in correct order - composeRule.assertWithLease { - assertNodesInVerticalOrder( - composeRule.onNodeWithText("TestItem1"), - composeRule.onNodeWithText("TestItem2"), - composeRule.onNodeWithText("TestItem3") - ) - } + assertNodesInVerticalOrder( + composeRule.onNodeWithText("TestItem1"), + composeRule.onNodeWithText("TestItem2"), + composeRule.onNodeWithText("TestItem3") + ) } @Test @@ -172,7 +165,7 @@ class LibraryFolderDetailsScreenTest { composeRule.onNodeWithText("Edit").performClick() // Check if folder name is displayed - composeRule.onNodeWithText("TestFolder2").assertWithLease { assertIsDisplayed() } + composeRule.onNodeWithText("TestFolder2").assertIsDisplayed() } @Test @@ -192,7 +185,7 @@ class LibraryFolderDetailsScreenTest { composeRule.onNodeWithText("Edit").performClick() // Check if edited item name is displayed - composeRule.onNodeWithText("TestItem2").assertWithLease { assertIsDisplayed() } + composeRule.onNodeWithText("TestItem2").assertIsDisplayed() // Edit item using action mode composeRule.onNodeWithText("TestItem2").performTouchInput { longClick() } @@ -207,7 +200,7 @@ class LibraryFolderDetailsScreenTest { composeRule.onNodeWithText("Edit").performClick() // Check if edited item name is displayed - composeRule.onNodeWithText("TestItem3").assertWithLease { assertIsDisplayed() } + composeRule.onNodeWithText("TestItem3").assertIsDisplayed() } @Test diff --git a/app/src/androidTest/java/app/musikus/library/presentation/LibraryIntegrationTest.kt b/app/src/androidTest/java/app/musikus/library/presentation/LibraryIntegrationTest.kt index e902a7a1..522d86f6 100644 --- a/app/src/androidTest/java/app/musikus/library/presentation/LibraryIntegrationTest.kt +++ b/app/src/androidTest/java/app/musikus/library/presentation/LibraryIntegrationTest.kt @@ -30,7 +30,6 @@ import androidx.navigation.compose.composable import androidx.navigation.testing.TestNavHostController import androidx.navigation.toRoute import app.ScreenshotRule -import app.assertWithLease import app.musikus.core.domain.FakeTimeProvider import app.musikus.core.presentation.HomeTab import app.musikus.core.presentation.HomeTabNavType @@ -127,7 +126,7 @@ class LibraryIntegrationTest { composeRule.onNodeWithText("TestFolder").performClick() // Check if item is displayed - composeRule.onNodeWithText("TestItem1").assertWithLease { assertIsDisplayed() } + composeRule.onNodeWithText("TestItem1").assertIsDisplayed() // Add an item from inside the folder (folder should be pre-selected) composeRule.onNodeWithContentDescription("Add item").performClick() @@ -135,6 +134,6 @@ class LibraryIntegrationTest { composeRule.onNodeWithContentDescription("Create").performClick() // Check if item is displayed - composeRule.onNodeWithText("TestItem2").assertWithLease { assertIsDisplayed() } + composeRule.onNodeWithText("TestItem2").assertIsDisplayed() } } diff --git a/app/src/androidTest/java/app/musikus/library/presentation/LibraryScreenTest.kt b/app/src/androidTest/java/app/musikus/library/presentation/LibraryScreenTest.kt index 929235d8..064612ec 100644 --- a/app/src/androidTest/java/app/musikus/library/presentation/LibraryScreenTest.kt +++ b/app/src/androidTest/java/app/musikus/library/presentation/LibraryScreenTest.kt @@ -30,7 +30,6 @@ import androidx.hilt.navigation.compose.hiltViewModel import androidx.lifecycle.compose.collectAsStateWithLifecycle import androidx.test.core.app.ApplicationProvider import app.ScreenshotRule -import app.assertWithLease import app.musikus.R import app.musikus.core.domain.FakeTimeProvider import app.musikus.core.presentation.MainActivity @@ -86,8 +85,8 @@ class LibraryScreenTest { composeRule.onNodeWithContentDescription("Add folder or item").performClick() - composeRule.onNodeWithContentDescription("Add folder").assertWithLease { assertIsDisplayed() } - composeRule.onNodeWithContentDescription("Add item").assertWithLease { assertIsDisplayed() } + composeRule.onNodeWithContentDescription("Add folder").assertIsDisplayed() + composeRule.onNodeWithContentDescription("Add item").assertIsDisplayed() } @Test @@ -95,9 +94,7 @@ class LibraryScreenTest { val context = ApplicationProvider.getApplicationContext() // Check if hint is displayed initially - composeRule.onNodeWithText( - context.getString(R.string.library_screen_hint) - ).assertWithLease { assertIsDisplayed() } + composeRule.onNodeWithText(context.getString(R.string.library_screen_hint)).assertIsDisplayed() // Add a folder composeRule.onNodeWithContentDescription("Add folder or item").performClick() @@ -114,9 +111,7 @@ class LibraryScreenTest { composeRule.onNodeWithContentDescription("Delete forever (1)").performClick() // Check if hint is displayed again - composeRule.onNodeWithText( - context.getString(R.string.library_screen_hint) - ).assertWithLease { assertIsDisplayed() } + composeRule.onNodeWithText(context.getString(R.string.library_screen_hint)).assertIsDisplayed() // Add an item composeRule.onNodeWithContentDescription("Add folder or item").performClick() @@ -133,9 +128,7 @@ class LibraryScreenTest { composeRule.onNodeWithContentDescription("Delete forever (1)").performClick() // Check if hint is displayed again - composeRule.onNodeWithText( - context.getString(R.string.library_screen_hint) - ).assertWithLease { assertIsDisplayed() } + composeRule.onNodeWithText(context.getString(R.string.library_screen_hint)).assertIsDisplayed() } private fun clickSortMode(