-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5d7ce9b
commit 3a1f0a1
Showing
3 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<application> | ||
<activity android:name="com.slack.circuit.foundation.NavigableCircuitRetainedStateTestActivity"/> | ||
<activity android:name="com.slack.circuit.foundation.NavigableCircuitSaveableStateTestActivity"/> | ||
<activity android:name="com.slack.circuit.foundation.NavigableCircuitViewModelStateTestActivity"/> | ||
</application> | ||
</manifest> |
109 changes: 109 additions & 0 deletions
109
...dUnitTest/kotlin/com/slack/circuit/foundation/NavigableCircuitSaveableStateAndroidTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// Copyright (C) 2023 Slack Technologies, LLC | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package com.slack.circuit.foundation | ||
|
||
import androidx.compose.ui.test.assertTextEquals | ||
import androidx.compose.ui.test.junit4.createAndroidComposeRule | ||
import androidx.compose.ui.test.onNodeWithTag | ||
import androidx.compose.ui.test.performClick | ||
import androidx.test.core.app.ActivityScenario | ||
import com.slack.circuit.internal.test.TestContentTags.TAG_COUNT | ||
import com.slack.circuit.internal.test.TestContentTags.TAG_GO_NEXT | ||
import com.slack.circuit.internal.test.TestContentTags.TAG_INCREASE_COUNT | ||
import com.slack.circuit.internal.test.TestContentTags.TAG_LABEL | ||
import com.slack.circuit.internal.test.TestContentTags.TAG_POP | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
@RunWith(ComposeUiTestRunner::class) | ||
class NavigableCircuitSaveableStateAndroidTest { | ||
|
||
@get:Rule | ||
val composeTestRule = createAndroidComposeRule<NavigableCircuitSaveableStateTestActivity>() | ||
|
||
private val scenario: ActivityScenario<NavigableCircuitSaveableStateTestActivity> | ||
get() = composeTestRule.activityRule.scenario | ||
|
||
@Test | ||
fun saveableStateScopedToBackstackWithRecreations() { | ||
composeTestRule.run { | ||
// Current: Screen A. Increase count to 1 | ||
onNodeWithTag(TAG_LABEL).assertTextEquals("A") | ||
onNodeWithTag(TAG_COUNT).assertTextEquals("0") | ||
onNodeWithTag(TAG_INCREASE_COUNT).performClick() | ||
onNodeWithTag(TAG_COUNT).assertTextEquals("1") | ||
|
||
// Now recreate the Activity and assert that the values were retained | ||
scenario.recreate() | ||
onNodeWithTag(TAG_LABEL).assertTextEquals("A") | ||
onNodeWithTag(TAG_COUNT).assertTextEquals("1") | ||
|
||
// Navigate to Screen B. Increase count to 1 | ||
onNodeWithTag(TAG_GO_NEXT).performClick() | ||
onNodeWithTag(TAG_LABEL).assertTextEquals("B") | ||
onNodeWithTag(TAG_COUNT).assertTextEquals("0") | ||
onNodeWithTag(TAG_INCREASE_COUNT).performClick() | ||
onNodeWithTag(TAG_COUNT).assertTextEquals("1") | ||
|
||
// Now recreate the Activity and assert that the values were retained | ||
scenario.recreate() | ||
onNodeWithTag(TAG_LABEL).assertTextEquals("B") | ||
onNodeWithTag(TAG_COUNT).assertTextEquals("1") | ||
|
||
// Navigate to Screen C. Increase count to 1 | ||
onNodeWithTag(TAG_GO_NEXT).performClick() | ||
onNodeWithTag(TAG_LABEL).assertTextEquals("C") | ||
onNodeWithTag(TAG_COUNT).assertTextEquals("0") | ||
onNodeWithTag(TAG_INCREASE_COUNT).performClick() | ||
onNodeWithTag(TAG_COUNT).assertTextEquals("1") | ||
|
||
// Now recreate the Activity and assert that the values were retained | ||
scenario.recreate() | ||
onNodeWithTag(TAG_LABEL).assertTextEquals("C") | ||
onNodeWithTag(TAG_COUNT).assertTextEquals("1") | ||
|
||
// Pop to Screen B. Increase count from 1 to 2. | ||
onNodeWithTag(TAG_POP).performClick() | ||
onNodeWithTag(TAG_LABEL).assertTextEquals("B") | ||
onNodeWithTag(TAG_COUNT).assertTextEquals("1") | ||
onNodeWithTag(TAG_INCREASE_COUNT).performClick() | ||
onNodeWithTag(TAG_COUNT).assertTextEquals("2") | ||
|
||
// Navigate to Screen C. Assert that it's state was not retained | ||
onNodeWithTag(TAG_GO_NEXT).performClick() | ||
onNodeWithTag(TAG_LABEL).assertTextEquals("C") | ||
onNodeWithTag(TAG_COUNT).assertTextEquals("0") | ||
|
||
// Now recreate the Activity and assert that the values were retained | ||
scenario.recreate() | ||
onNodeWithTag(TAG_LABEL).assertTextEquals("C") | ||
onNodeWithTag(TAG_COUNT).assertTextEquals("0") | ||
|
||
// Pop to Screen B. Assert that it's state was retained | ||
onNodeWithTag(TAG_POP).performClick() | ||
onNodeWithTag(TAG_LABEL).assertTextEquals("B") | ||
onNodeWithTag(TAG_COUNT).assertTextEquals("2") | ||
|
||
// Now recreate the Activity and assert that the values were retained | ||
scenario.recreate() | ||
onNodeWithTag(TAG_LABEL).assertTextEquals("B") | ||
onNodeWithTag(TAG_COUNT).assertTextEquals("2") | ||
|
||
// Pop to Screen A. Assert that it's state was retained | ||
onNodeWithTag(TAG_POP).performClick() | ||
onNodeWithTag(TAG_LABEL).assertTextEquals("A") | ||
onNodeWithTag(TAG_COUNT).assertTextEquals("1") | ||
|
||
// Now recreate the Activity and assert that the values were retained | ||
scenario.recreate() | ||
onNodeWithTag(TAG_LABEL).assertTextEquals("A") | ||
onNodeWithTag(TAG_COUNT).assertTextEquals("1") | ||
|
||
// Navigate to Screen B. Assert that it's state was not retained | ||
onNodeWithTag(TAG_GO_NEXT).performClick() | ||
onNodeWithTag(TAG_LABEL).assertTextEquals("B") | ||
onNodeWithTag(TAG_COUNT).assertTextEquals("0") | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...UnitTest/kotlin/com/slack/circuit/foundation/NavigableCircuitSaveableStateTestActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (C) 2023 Slack Technologies, LLC | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package com.slack.circuit.foundation | ||
|
||
import android.os.Bundle | ||
import androidx.activity.ComponentActivity | ||
import androidx.activity.compose.setContent | ||
import com.slack.circuit.backstack.rememberSaveableBackStack | ||
import com.slack.circuit.internal.test.TestCountPresenter | ||
import com.slack.circuit.internal.test.TestScreen | ||
import com.slack.circuit.internal.test.createTestCircuit | ||
|
||
class NavigableCircuitSaveableStateTestActivity : ComponentActivity() { | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
val circuit = createTestCircuit(rememberType = TestCountPresenter.RememberType.Saveable) | ||
|
||
setContent { | ||
CircuitCompositionLocals(circuit) { | ||
val backStack = rememberSaveableBackStack(TestScreen.ScreenA) | ||
val navigator = | ||
rememberCircuitNavigator( | ||
backStack = backStack, | ||
onRootPop = {}, // no-op for tests | ||
) | ||
NavigableCircuitContent(navigator = navigator, backStack = backStack) | ||
} | ||
} | ||
} | ||
} |