Skip to content

Commit

Permalink
Introduce idling functions for testing
Browse files Browse the repository at this point in the history
Implemented `isIdleNow` function, which can be used with `IdlingResource` or `waitUntil`,and `awaitIdle` suspend
function for Compose UI testing, utilizing the `SwrCacheView` introduced in #135.

Previously, it was not possible to determine whether there were ongoing operations within the cache. With these new
functions, it is now possible to synchronize with Compose test functions for proper coordination during testing.
  • Loading branch information
ogaclejapan committed Nov 30, 2024
1 parent 6a2c122 commit cabd60f
Show file tree
Hide file tree
Showing 12 changed files with 217 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.runComposeUiTest
import androidx.compose.ui.test.waitUntilAtLeastOneExists
import androidx.compose.ui.test.waitUntilExactlyOneExists
import kotlinx.coroutines.launch
import soil.query.InfiniteQueryId
import soil.query.InfiniteQueryKey
Expand All @@ -37,6 +36,7 @@ import soil.query.compose.tooling.SwrPreviewClient
import soil.query.core.Marker
import soil.query.core.Reply
import soil.query.core.orNone
import soil.query.test.test
import soil.testing.UnitTest
import kotlin.test.Test

Expand All @@ -46,7 +46,7 @@ class InfiniteQueryComposableTest : UnitTest() {
@Test
fun testRememberInfiniteQuery() = runComposeUiTest {
val key = TestInfiniteQueryKey()
val client = SwrCache(coroutineScope = SwrCacheScope())
val client = SwrCache(coroutineScope = SwrCacheScope()).test()
setContent {
SwrClientProvider(client) {
val query = rememberInfiniteQuery(key, config = InfiniteQueryConfig {
Expand All @@ -72,16 +72,15 @@ class InfiniteQueryComposableTest : UnitTest() {
}
}
}

waitUntilAtLeastOneExists(hasTestTag("query"))
waitUntil { client.isIdleNow() }
onNodeWithTag("query").assertTextEquals("Size: 10 - Page: 0")
onNodeWithTag("loadMore").assertTextEquals("HasNext: true")
}

@Test
fun testRememberInfiniteQuery_select() = runComposeUiTest {
val key = TestInfiniteQueryKey()
val client = SwrCache(coroutineScope = SwrCacheScope())
val client = SwrCache(coroutineScope = SwrCacheScope()).test()
setContent {
SwrClientProvider(client) {
val query = rememberInfiniteQuery(key, select = { it.chunkedData })
Expand All @@ -99,14 +98,14 @@ class InfiniteQueryComposableTest : UnitTest() {
}
}

waitUntilAtLeastOneExists(hasTestTag("query"))
waitUntil { client.isIdleNow() }
onAllNodes(hasTestTag("query")).assertCountEquals(10)
}

@Test
fun testRememberInfiniteQuery_loadMore() = runComposeUiTest {
val key = TestInfiniteQueryKey()
val client = SwrCache(coroutineScope = SwrCacheScope())
val client = SwrCache(coroutineScope = SwrCacheScope()).test()
setContent {
SwrClientProvider(client) {
val query = rememberInfiniteQuery(key)
Expand Down Expand Up @@ -138,10 +137,12 @@ class InfiniteQueryComposableTest : UnitTest() {
}
}

waitUntilExactlyOneExists(hasTestTag("query0"))
waitUntil { client.isIdleNow() }
onNodeWithTag("query0").assertTextEquals("Size: 10 - Page: 0")
onNodeWithTag("loadMore").performClick()
waitUntilExactlyOneExists(hasTestTag("query1"))

waitForIdle()
waitUntil { client.isIdleNow() }
onNodeWithTag("query1").assertTextEquals("Size: 10 - Page: 1")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import soil.query.SwrCacheScope
import soil.query.buildInfiniteQueryKey
import soil.query.core.Reply
import soil.query.emptyChunks
import soil.query.test.test
import soil.testing.UnitTest
import kotlin.test.Test
import kotlin.test.assertEquals
Expand All @@ -36,7 +37,7 @@ class InfiniteQueryRecompositionOptimizerTest : UnitTest() {
@Test
fun testRecompositionCount_default() = runComposeUiTest {
val key = TestInfiniteQueryKey()
val client = SwrCache(coroutineScope = SwrCacheScope())
val client = SwrCache(coroutineScope = SwrCacheScope()).test()
var recompositionCount = 0
setContent {
SwrClientProvider(client) {
Expand Down Expand Up @@ -68,7 +69,7 @@ class InfiniteQueryRecompositionOptimizerTest : UnitTest() {
@Test
fun testRecompositionCount_disabled() = runComposeUiTest {
val key = TestInfiniteQueryKey()
val client = SwrCache(coroutineScope = SwrCacheScope())
val client = SwrCache(coroutineScope = SwrCacheScope()).test()
var recompositionCount = 0
setContent {
SwrClientProvider(client) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.assertTextEquals
import androidx.compose.ui.test.hasTestTag
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.runComposeUiTest
import androidx.compose.ui.test.waitUntilExactlyOneExists
import kotlinx.coroutines.launch
import soil.query.MutationKey
import soil.query.MutationState
Expand All @@ -41,7 +39,7 @@ class MutationComposableTest : UnitTest() {
@Test
fun testRememberMutation() = runComposeUiTest {
val key = TestMutationKey()
val client = SwrCache(coroutineScope = SwrCacheScope())
val client = SwrCache(coroutineScope = SwrCacheScope()).test()
setContent {
SwrClientProvider(client) {
val mutation = rememberMutation(key, config = MutationConfig {
Expand Down Expand Up @@ -72,7 +70,8 @@ class MutationComposableTest : UnitTest() {
onNodeWithTag("result").assertDoesNotExist()
onNodeWithTag("mutation").performClick()

waitUntilExactlyOneExists(hasTestTag("result"))
waitForIdle()
waitUntil { client.isIdleNow() }
onNodeWithTag("result").assertTextEquals("Soil - 1")
}

Expand Down Expand Up @@ -111,7 +110,8 @@ class MutationComposableTest : UnitTest() {
}

onNodeWithTag("mutation").performClick()
waitUntilExactlyOneExists(hasTestTag("result"))

waitUntil { client.isIdleNow() }
onNodeWithTag("result").assertTextEquals("error")
}

Expand Down Expand Up @@ -146,7 +146,9 @@ class MutationComposableTest : UnitTest() {
}

onNodeWithTag("mutation").performClick()
waitUntilExactlyOneExists(hasTestTag("result"))

waitForIdle()
waitUntil { client.isIdleNow() }
onNodeWithTag("result").assertTextEquals("error")
}

Expand Down Expand Up @@ -237,7 +239,7 @@ class MutationComposableTest : UnitTest() {
@Test
fun testRememberMutationIf() = runComposeUiTest {
val key = TestMutationKey()
val client = SwrCache(coroutineScope = SwrCacheScope())
val client = SwrCache(coroutineScope = SwrCacheScope()).test()
setContent {
SwrClientProvider(client) {
var enabled by remember { mutableStateOf(false) }
Expand Down Expand Up @@ -275,7 +277,8 @@ class MutationComposableTest : UnitTest() {
onNodeWithTag("result").assertDoesNotExist()
onNodeWithTag("mutation").performClick()

waitUntilExactlyOneExists(hasTestTag("result"))
waitForIdle()
waitUntil { client.isIdleNow() }
onNodeWithTag("result").assertTextEquals("Soil - 1")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.assertTextEquals
import androidx.compose.ui.test.hasTestTag
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.runComposeUiTest
import androidx.compose.ui.test.waitUntilExactlyOneExists
import soil.query.QueryId
import soil.query.QueryKey
import soil.query.QueryState
Expand All @@ -40,7 +38,7 @@ class QueryComposableTest : UnitTest() {
@Test
fun testRememberQuery() = runComposeUiTest {
val key = TestQueryKey()
val client = SwrCache(coroutineScope = SwrCacheScope())
val client = SwrCache(coroutineScope = SwrCacheScope()).test()
setContent {
SwrClientProvider(client) {
val query = rememberQuery(key, config = QueryConfig {
Expand All @@ -56,7 +54,7 @@ class QueryComposableTest : UnitTest() {
}
}

waitUntilExactlyOneExists(hasTestTag("query"))
waitUntil { client.isIdleNow() }
onNodeWithTag("query").assertTextEquals("Hello, Soil!")
}

Expand All @@ -76,7 +74,7 @@ class QueryComposableTest : UnitTest() {
}
}

waitUntilExactlyOneExists(hasTestTag("query"))
waitUntil { client.isIdleNow() }
onNodeWithTag("query").assertTextEquals("HELLO, COMPOSE!")
}

Expand All @@ -99,7 +97,7 @@ class QueryComposableTest : UnitTest() {
}
}

waitUntilExactlyOneExists(hasTestTag("query"))
waitUntil { client.isIdleNow() }
onNodeWithTag("query").assertTextEquals("error")
}

Expand All @@ -121,7 +119,7 @@ class QueryComposableTest : UnitTest() {
}
}

waitUntilExactlyOneExists(hasTestTag("query"))
waitUntil { client.isIdleNow() }
onNodeWithTag("query").assertTextEquals("Hello, Compose!Hello, Soil!")
}

Expand All @@ -145,7 +143,7 @@ class QueryComposableTest : UnitTest() {
}
}

waitUntilExactlyOneExists(hasTestTag("query"))
waitUntil { client.isIdleNow() }
onNodeWithTag("query").assertTextEquals("Hello, Compose!Hello, Soil!Hello, Kotlin!")
}

Expand All @@ -169,7 +167,7 @@ class QueryComposableTest : UnitTest() {
}
}

waitUntilExactlyOneExists(hasTestTag("query"))
waitUntil { client.isIdleNow() }
onNodeWithTag("query").assertTextEquals("Hello, Compose!|Hello, Soil!|Hello, Kotlin!")
}

Expand Down Expand Up @@ -260,7 +258,7 @@ class QueryComposableTest : UnitTest() {
@Test
fun testRememberQueryIf() = runComposeUiTest {
val key = TestQueryKey()
val client = SwrCache(coroutineScope = SwrCacheScope())
val client = SwrCache(coroutineScope = SwrCacheScope()).test()
setContent {
SwrClientProvider(client) {
var enabled by remember { mutableStateOf(false) }
Expand All @@ -281,7 +279,8 @@ class QueryComposableTest : UnitTest() {
onNodeWithTag("query").assertDoesNotExist()
onNodeWithTag("toggle").performClick()

waitUntilExactlyOneExists(hasTestTag("query"))
waitForIdle()
waitUntil { client.isIdleNow() }
onNodeWithTag("query").assertTextEquals("Hello, Soil!")
}

Expand Down Expand Up @@ -315,7 +314,8 @@ class QueryComposableTest : UnitTest() {
onNodeWithTag("query").assertDoesNotExist()
onNodeWithTag("toggle").performClick()

waitUntilExactlyOneExists(hasTestTag("query"))
waitForIdle()
waitUntil { client.isIdleNow() }
onNodeWithTag("query").assertTextEquals("HELLO, COMPOSE!")
}

Expand Down Expand Up @@ -350,7 +350,8 @@ class QueryComposableTest : UnitTest() {
onNodeWithTag("query").assertDoesNotExist()
onNodeWithTag("toggle").performClick()

waitUntilExactlyOneExists(hasTestTag("query"))
waitForIdle()
waitUntil { client.isIdleNow() }
onNodeWithTag("query").assertTextEquals("Hello, Compose!Hello, Soil!")
}

Expand Down Expand Up @@ -387,7 +388,8 @@ class QueryComposableTest : UnitTest() {
onNodeWithTag("query").assertDoesNotExist()
onNodeWithTag("toggle").performClick()

waitUntilExactlyOneExists(hasTestTag("query"))
waitForIdle()
waitUntil { client.isIdleNow() }
onNodeWithTag("query").assertTextEquals("Hello, Compose!Hello, Soil!Hello, Kotlin!")
}

Expand Down Expand Up @@ -424,7 +426,8 @@ class QueryComposableTest : UnitTest() {
onNodeWithTag("query").assertDoesNotExist()
onNodeWithTag("toggle").performClick()

waitUntilExactlyOneExists(hasTestTag("query"))
waitForIdle()
waitUntil { client.isIdleNow() }
onNodeWithTag("query").assertTextEquals("Hello, Compose!|Hello, Soil!|Hello, Kotlin!")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import soil.query.SwrCache
import soil.query.SwrCacheScope
import soil.query.buildQueryKey
import soil.query.core.Reply
import soil.query.test.test
import soil.testing.UnitTest
import kotlin.test.Test
import kotlin.test.assertEquals
Expand All @@ -33,7 +34,7 @@ class QueryRecompositionOptimizerTest : UnitTest() {
@Test
fun testRecompositionCount_default() = runComposeUiTest {
val key = TestQueryKey()
val client = SwrCache(coroutineScope = SwrCacheScope())
val client = SwrCache(coroutineScope = SwrCacheScope()).test()
var recompositionCount = 0
setContent {
SwrClientProvider(client) {
Expand All @@ -55,7 +56,7 @@ class QueryRecompositionOptimizerTest : UnitTest() {
@Test
fun testRecompositionCount_disabled() = runComposeUiTest {
val key = TestQueryKey()
val client = SwrCache(coroutineScope = SwrCacheScope())
val client = SwrCache(coroutineScope = SwrCacheScope()).test()
var recompositionCount = 0
setContent {
SwrClientProvider(client) {
Expand Down
Loading

0 comments on commit cabd60f

Please sign in to comment.