Skip to content

Commit

Permalink
revert 'assertWithLease'
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasemde committed Jan 12, 2025
1 parent 0c0d714 commit 0d2325f
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 126 deletions.
39 changes: 0 additions & 39 deletions app/src/androidTest/java/app/ComposeRule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -171,7 +170,7 @@ class ActiveSessionScreenTest {
matcher = hasText("TestItem3")
and
hasAnySibling(hasText("00:00"))
).assertWithLease { assertIsDisplayed() }
).assertIsDisplayed()
}

@Test
Expand All @@ -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()
Expand All @@ -192,7 +191,7 @@ class ActiveSessionScreenTest {
composeRule.onNodeWithText("TestItem2").performClick()

// Item is selected
composeRule.onNodeWithText("TestItem2").assertWithLease { assertIsDisplayed() }
composeRule.onNodeWithText("TestItem2").assertIsDisplayed()
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -42,7 +38,7 @@ class MusikusBottomBarTest {
@get:Rule(order = 1)
val composeRule = createAndroidComposeRule<MainActivity>()

lateinit var onTabSelectedMock: (HomeTab) -> Unit
lateinit var navController: NavHostController

@Before
fun setUp() {
Expand All @@ -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<HomeTab>() }
navController = mockk<NavHostController>(relaxed = true)

MusikusBottomBar(
mainUiState = mainUiState,
mainEventHandler = eventHandler,
currentTab = currentTab,
onTabSelected = onTabSelectedMock,
currentTab = HomeTab.Sessions,
onTabSelected = { selectedTab ->
navController.navigate(Screen.Home(selectedTab))
},
)
}
}
Expand All @@ -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>(any())
}
}

Expand All @@ -82,31 +77,25 @@ class MusikusBottomBarTest {
composeRule.onNodeWithText("Goals").performClick()

verify(exactly = 1) {
onTabSelectedMock(HomeTab.Goals)
navController.navigate(Screen.Home(HomeTab.Goals))
}

composeRule.onNodeWithText("Goals").assertIsSelected()
}

@Test
fun navigateToStatistics() = runTest {
composeRule.onNodeWithText("Statistics").performClick()

verify(exactly = 1) {
onTabSelectedMock(HomeTab.Statistics)
navController.navigate(Screen.Home(HomeTab.Statistics))
}

composeRule.onNodeWithText("Statistics").assertIsSelected()
}

@Test
fun navigateToLibrary() = runTest {
composeRule.onNodeWithText("Library").performClick()

verify(exactly = 1) {
onTabSelectedMock(HomeTab.Library)
navController.navigate(Screen.Home(HomeTab.Library))
}

composeRule.onNodeWithText("Library").assertIsSelected()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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()
}
}
Loading

0 comments on commit 0d2325f

Please sign in to comment.