Skip to content

Commit bc24557

Browse files
(Re)introduce ViewTreeLifecycleOwner support to containers
Introduces something called `WorkflowLifecycleOwner` that implements `LifecycleOwner` and is set as the `ViewTreeLifecycleOwner` on the child views of the navigation containers: `WorkflowViewStub`, `BackstackContainer`, and `ModalContainer`. These containers all move their lifecycle to the `DESTROYED` state when they are about to discard the current child view and replace it with a new one because the rendering type changed. Until then, their lifecycle follows that of their parent lifecycle. This PR was created in March, then left for a few months, and finally picked back up in August. That said, very little had to be changed. The updates to AndroidX and Kotlin were merged in separate PRs (#459, #460). It was started by reverting commit 8500b06 (which reverted 74e14e9 and cdca31c).
1 parent 70af222 commit bc24557

39 files changed

Lines changed: 2684 additions & 418 deletions

File tree

.buildscript/configure-android-defaults.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@ android {
2121
exclude 'META-INF/common.kotlin_module'
2222
exclude 'META-INF/android_debug.kotlin_module'
2323
exclude 'META-INF/android_release.kotlin_module'
24+
exclude 'META-INF/AL2.0'
25+
exclude 'META-INF/LGPL2.1'
2426
}
2527
}

buildSrc/src/main/java/Dependencies.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,19 @@ object Dependencies {
1616
const val activityKtx = "androidx.activity:activity-ktx:1.3.0"
1717
const val appcompat = "androidx.appcompat:appcompat:1.3.1"
1818

19+
object Compose {
20+
const val foundation = "androidx.compose.foundation:foundation:1.0.1"
21+
const val ui = "androidx.compose.ui:ui:1.0.1"
22+
}
23+
1924
const val constraint_layout = "androidx.constraintlayout:constraintlayout:2.1.0"
2025
const val fragment = "androidx.fragment:fragment:1.3.6"
2126
const val fragmentKtx = "androidx.fragment:fragment-ktx:1.3.6"
2227
const val gridlayout = "androidx.gridlayout:gridlayout:1.0.0"
2328

2429
object Lifecycle {
2530
const val ktx = "androidx.lifecycle:lifecycle-runtime-ktx:2.3.1"
31+
const val viewModel = "androidx.lifecycle:lifecycle-viewmodel:2.3.1"
2632
const val viewModelKtx = "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1"
2733
const val viewModelSavedState = "androidx.lifecycle:lifecycle-viewmodel-savedstate:1.1.0"
2834
}
@@ -113,6 +119,7 @@ object Dependencies {
113119

114120
object Test {
115121
object AndroidX {
122+
const val compose = "androidx.compose.ui:ui-test-junit4:1.0.1"
116123
const val core = "androidx.test:core:1.3.0"
117124

118125
object Espresso {

samples/tictactoe/app/src/androidTest/java/com/squareup/sample/TicTacToeEspressoTest.kt

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package com.squareup.sample
33
import android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
44
import android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
55
import android.view.View
6-
import androidx.test.core.app.ActivityScenario
7-
import androidx.test.core.app.ActivityScenario.launch
86
import androidx.test.espresso.IdlingRegistry
97
import androidx.test.espresso.ViewInteraction
108
import androidx.test.espresso.action.ViewActions.click
@@ -15,6 +13,7 @@ import androidx.test.espresso.matcher.ViewMatchers.hasDescendant
1513
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
1614
import androidx.test.espresso.matcher.ViewMatchers.withId
1715
import androidx.test.espresso.matcher.ViewMatchers.withText
16+
import androidx.test.ext.junit.rules.ActivityScenarioRule
1817
import androidx.test.ext.junit.runners.AndroidJUnit4
1918
import com.google.common.truth.Truth.assertThat
2019
import com.squareup.sample.gameworkflow.GamePlayScreen
@@ -26,10 +25,11 @@ import com.squareup.workflow1.ui.ViewEnvironment
2625
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
2726
import com.squareup.workflow1.ui.environment
2827
import com.squareup.workflow1.ui.getRendering
29-
import com.squareup.workflow1.ui.internal.test.actuallyPressBack
3028
import com.squareup.workflow1.ui.internal.test.inAnyView
29+
import com.squareup.workflow1.ui.internal.test.actuallyPressBack
3130
import org.junit.After
3231
import org.junit.Before
32+
import org.junit.Rule
3333
import org.junit.Test
3434
import org.junit.runner.RunWith
3535
import java.util.concurrent.atomic.AtomicReference
@@ -38,25 +38,23 @@ import java.util.concurrent.atomic.AtomicReference
3838
@RunWith(AndroidJUnit4::class)
3939
class TicTacToeEspressoTest {
4040

41-
private lateinit var scenario: ActivityScenario<TicTacToeActivity>
41+
@Rule @JvmField var scenarioRule = ActivityScenarioRule(TicTacToeActivity::class.java)
42+
private val scenario get() = scenarioRule.scenario
4243

4344
@Before
4445
fun setUp() {
45-
scenario = launch(TicTacToeActivity::class.java)
46-
.apply {
47-
onActivity { activity ->
48-
IdlingRegistry.getInstance()
49-
.register(activity.idlingResource)
50-
activity.requestedOrientation = SCREEN_ORIENTATION_PORTRAIT
51-
}
52-
}
46+
scenario.onActivity { activity ->
47+
IdlingRegistry.getInstance()
48+
.register(activity.idlingResource)
49+
activity.requestedOrientation = SCREEN_ORIENTATION_PORTRAIT
50+
}
5351
}
5452

5553
@After
5654
fun tearDown() {
5755
scenario.onActivity { activity ->
5856
IdlingRegistry.getInstance()
59-
.unregister(activity.idlingResource)
57+
.unregister(activity.idlingResource)
6058
}
6159
}
6260

@@ -93,7 +91,7 @@ class TicTacToeEspressoTest {
9391
// lambda above and it all worked just fine. But that seems like a land mine.)
9492

9593
inAnyView(withId(R.id.game_play_toolbar))
96-
.check(matches(hasDescendant(withText("O, place your ${Player.O.symbol}"))))
94+
.check(matches(hasDescendant(withText("O, place your ${Player.O.symbol}"))))
9795

9896
// Now that we're confident the views have updated, back to the activity
9997
// to mess with what should be the updated rendering.
@@ -134,7 +132,7 @@ class TicTacToeEspressoTest {
134132
// email should have been restored from view state.
135133
inAnyView(withId(R.id.login_email)).check(matches(withText("foo@bar")))
136134
inAnyView(withId(R.id.login_error_message))
137-
.check(matches(withText("Unknown email or invalid password")))
135+
.check(matches(withText("Unknown email or invalid password")))
138136
}
139137

140138
@Test fun dialogSurvivesConfigChange() {

settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ include(
3030
":workflow-tracing",
3131
":workflow-ui:backstack-common",
3232
":workflow-ui:backstack-android",
33+
":workflow-ui:compose",
3334
":workflow-ui:core-common",
3435
":workflow-ui:core-android",
3536
":workflow-ui:internal-testing-android",

workflow-ui/backstack-android/src/androidTest/AndroidManifest.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
xmlns:android="http://schemas.android.com/apk/res/android">
33

44
<application>
5-
<activity android:name=".fixtures.BackStackTestActivity" />
5+
<activity
6+
android:name=".fixtures.BackStackContainerLifecycleActivity"
7+
android:theme="@style/Theme.AppCompat.NoActionBar"/>
68
</application>
79
</manifest>

workflow-ui/backstack-android/src/androidTest/java/com/squareup/workflow1/ui/backstack/test/BackStackContainerTest.kt

Lines changed: 0 additions & 210 deletions
This file was deleted.

0 commit comments

Comments
 (0)