From 03ab545244ccb8424cf6572bb456b134bb386c41 Mon Sep 17 00:00:00 2001 From: Ray Ryan Date: Tue, 18 Jan 2022 15:57:19 -0800 Subject: [PATCH] Upgrades leak canary, testing, androidx lifecycle. Note that we hold off on upgrading androidx activity, appcompat, etc. Those push androidx core past 1.6.0, which breaks https://github.com/cashapp/paparazzi. We should hold off until they catch up, which should happen soon after AS Bumblebee freezes. --- .buildscript/android-ui-tests.gradle | 1 - buildSrc/src/main/java/Dependencies.kt | 20 +++++++++---------- .../compose/hellocompose/HelloComposeTest.kt | 8 ++++++-- .../hellocomposebinding/HelloBindingTest.kt | 8 ++++++-- .../HelloComposeWorkflowTest.kt | 8 ++++++-- .../inlinerendering/InlineRenderingTest.kt | 8 ++++++-- .../compose/launcher/SampleLauncherTest.kt | 8 ++++++-- .../nestedrenderings/NestedRenderingsTest.kt | 8 ++++++-- .../sample/compose/preview/PreviewTest.kt | 8 ++++++-- .../sample/compose/textinput/TextInputTest.kt | 8 ++++++-- .../sample/poetryapp/PoetryAppTest.kt | 7 +++++-- .../squareup/sample/ravenapp/RavenAppTest.kt | 7 +++++-- .../HelloBackButtonEspressoTest.kt | 5 ++++- samples/dungeon/app/build.gradle.kts | 1 - .../squareup/sample/dungeon/DungeonAppTest.kt | 5 ++++- .../HelloWorkflowFragmentAppTest.kt | 5 ++++- .../helloworkflow/HelloWorkflowAppTest.kt | 5 ++++- .../stubvisibility/StubVisibilityAppTest.kt | 5 ++++- .../squareup/sample/TicTacToeEspressoTest.kt | 5 ++++- .../sample/mainactivity/TodoAppTest.kt | 5 ++++- .../compose/tooling/PreviewViewFactoryTest.kt | 8 ++++++-- .../ui/compose/ComposeViewFactoryTest.kt | 9 +++++++-- .../compose/ComposeViewTreeIntegrationTest.kt | 10 +++++++--- .../ui/compose/CompositionRootTest.kt | 8 ++++++-- .../workflow1/ui/compose/RenderAsStateTest.kt | 8 ++++++-- .../ui/compose/WorkflowRenderingTest.kt | 11 +++++++--- .../backstack/test/BackstackContainerTest.kt | 5 ++++- .../test/ModalViewContainerLifecycleTest.kt | 5 ++++- .../workflow1/ui/modal/ModalContainer.kt | 20 ++++++++----------- workflow-ui/core-android/build.gradle.kts | 3 ++- .../ui/WorkflowViewStubLifecycleTest.kt | 5 ++++- 31 files changed, 158 insertions(+), 69 deletions(-) diff --git a/.buildscript/android-ui-tests.gradle b/.buildscript/android-ui-tests.gradle index 189b4ae44..7acbf7239 100644 --- a/.buildscript/android-ui-tests.gradle +++ b/.buildscript/android-ui-tests.gradle @@ -1,7 +1,6 @@ android { defaultConfig { testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" - testInstrumentationRunnerArgument "listener", "leakcanary.FailTestOnLeakRunListener" } testOptions { diff --git a/buildSrc/src/main/java/Dependencies.kt b/buildSrc/src/main/java/Dependencies.kt index c00605e2a..3c4be7ed9 100644 --- a/buildSrc/src/main/java/Dependencies.kt +++ b/buildSrc/src/main/java/Dependencies.kt @@ -25,15 +25,15 @@ object Dependencies { const val ui = "androidx.compose.ui:ui:1.1.0-rc01" } - const val constraint_layout = "androidx.constraintlayout:constraintlayout:2.1.0" + const val constraint_layout = "androidx.constraintlayout:constraintlayout:2.1.2" const val fragment = "androidx.fragment:fragment:1.3.6" const val fragmentKtx = "androidx.fragment:fragment-ktx:1.3.6" const val gridlayout = "androidx.gridlayout:gridlayout:1.0.0" object Lifecycle { - const val ktx = "androidx.lifecycle:lifecycle-runtime-ktx:2.3.1" - const val viewModel = "androidx.lifecycle:lifecycle-viewmodel:2.3.1" - const val viewModelKtx = "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1" + const val ktx = "androidx.lifecycle:lifecycle-runtime-ktx:2.4.0" + const val viewModel = "androidx.lifecycle:lifecycle-viewmodel:2.4.0" + const val viewModelKtx = "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0" const val viewModelSavedState = "androidx.lifecycle:lifecycle-viewmodel-savedstate:1.1.0" } @@ -53,7 +53,7 @@ object Dependencies { // Required for Dungeon Crawler sample. const val desugar_jdk_libs = "com.android.tools:desugar_jdk_libs:1.1.5" - const val leakcanary = "com.squareup.leakcanary:leakcanary-android:2.7" + const val leakcanary = "com.squareup.leakcanary:leakcanary-android:2.8.1" const val radiography = "com.squareup.radiography:radiography:2.4.0" const val rxandroid2 = "io.reactivex.rxjava2:rxandroid:2.1.1" const val seismic = "com.squareup:seismic:1.0.2" @@ -126,7 +126,7 @@ object Dependencies { object AndroidX { const val compose = "androidx.compose.ui:ui-test-junit4:1.0.1" const val core = "androidx.test:core:1.3.0" - const val lifecycle = "androidx.lifecycle:lifecycle-runtime-testing:2.3.1" + const val lifecycle = "androidx.lifecycle:lifecycle-runtime-testing:2.4.0" object Espresso { const val core = "androidx.test.espresso:espresso-core:3.3.0" @@ -134,15 +134,15 @@ object Dependencies { const val intents = "androidx.test.espresso:espresso-intents:3.3.0" } - const val junitExt = "androidx.test.ext:junit:1.1.2" - const val runner = "androidx.test:runner:1.3.0" - const val truthExt = "androidx.test.ext:truth:1.3.0" + const val junitExt = "androidx.test.ext:junit:1.1.3" + const val runner = "androidx.test:runner:1.4.0" + const val truthExt = "androidx.test.ext:truth:1.4.0" const val uiautomator = "androidx.test.uiautomator:uiautomator:2.2.0" } const val hamcrestCore = "org.hamcrest:hamcrest-core:2.2" const val junit = "junit:junit:4.13.2" - const val leakcanaryInstrumentation = "com.squareup.leakcanary:leakcanary-android-instrumentation:2.7" + const val leakcanaryInstrumentation = "com.squareup.leakcanary:leakcanary-android-instrumentation:2.8.1" const val mockito = "org.mockito:mockito-core:3.3.3" const val robolectric = "org.robolectric:robolectric:4.5.1" const val truth = "com.google.truth:truth:1.1.3" diff --git a/samples/compose-samples/src/androidTest/java/com/squareup/sample/compose/hellocompose/HelloComposeTest.kt b/samples/compose-samples/src/androidTest/java/com/squareup/sample/compose/hellocompose/HelloComposeTest.kt index 8982189ad..58b859f89 100644 --- a/samples/compose-samples/src/androidTest/java/com/squareup/sample/compose/hellocompose/HelloComposeTest.kt +++ b/samples/compose-samples/src/androidTest/java/com/squareup/sample/compose/hellocompose/HelloComposeTest.kt @@ -7,16 +7,20 @@ import androidx.compose.ui.test.performClick import androidx.test.ext.junit.runners.AndroidJUnit4 import com.squareup.workflow1.ui.WorkflowUiExperimentalApi import com.squareup.workflow1.ui.internal.test.IdleAfterTestRule +import leakcanary.DetectLeaksAfterTestSuccess import org.junit.Rule import org.junit.Test +import org.junit.rules.RuleChain import org.junit.runner.RunWith @RunWith(AndroidJUnit4::class) @OptIn(WorkflowUiExperimentalApi::class) class HelloComposeTest { - @get:Rule val composeRule = createAndroidComposeRule() - @get:Rule val idleAfterTest = IdleAfterTestRule + private val composeRule = createAndroidComposeRule() + @get:Rule val rules: RuleChain = RuleChain.outerRule(DetectLeaksAfterTestSuccess()) + .around(IdleAfterTestRule) + .around(composeRule) @Test fun togglesBetweenStates() { composeRule.onNodeWithText("Hello") diff --git a/samples/compose-samples/src/androidTest/java/com/squareup/sample/compose/hellocomposebinding/HelloBindingTest.kt b/samples/compose-samples/src/androidTest/java/com/squareup/sample/compose/hellocomposebinding/HelloBindingTest.kt index 808a895c8..ad1bd7ba8 100644 --- a/samples/compose-samples/src/androidTest/java/com/squareup/sample/compose/hellocomposebinding/HelloBindingTest.kt +++ b/samples/compose-samples/src/androidTest/java/com/squareup/sample/compose/hellocomposebinding/HelloBindingTest.kt @@ -7,16 +7,20 @@ import androidx.compose.ui.test.performClick import androidx.test.ext.junit.runners.AndroidJUnit4 import com.squareup.workflow1.ui.WorkflowUiExperimentalApi import com.squareup.workflow1.ui.internal.test.IdleAfterTestRule +import leakcanary.DetectLeaksAfterTestSuccess import org.junit.Rule import org.junit.Test +import org.junit.rules.RuleChain import org.junit.runner.RunWith @RunWith(AndroidJUnit4::class) @OptIn(WorkflowUiExperimentalApi::class) class HelloBindingTest { - @get:Rule val composeRule = createAndroidComposeRule() - @get:Rule val idleAfterTest = IdleAfterTestRule + private val composeRule = createAndroidComposeRule() + @get:Rule val rules: RuleChain = RuleChain.outerRule(DetectLeaksAfterTestSuccess()) + .around(IdleAfterTestRule) + .around(composeRule) @Test fun togglesBetweenStates() { composeRule.onNodeWithText("Hello") diff --git a/samples/compose-samples/src/androidTest/java/com/squareup/sample/compose/hellocomposeworkflow/HelloComposeWorkflowTest.kt b/samples/compose-samples/src/androidTest/java/com/squareup/sample/compose/hellocomposeworkflow/HelloComposeWorkflowTest.kt index 0faecbe43..6e29f7091 100644 --- a/samples/compose-samples/src/androidTest/java/com/squareup/sample/compose/hellocomposeworkflow/HelloComposeWorkflowTest.kt +++ b/samples/compose-samples/src/androidTest/java/com/squareup/sample/compose/hellocomposeworkflow/HelloComposeWorkflowTest.kt @@ -7,16 +7,20 @@ import androidx.compose.ui.test.performClick import androidx.test.ext.junit.runners.AndroidJUnit4 import com.squareup.workflow1.ui.WorkflowUiExperimentalApi import com.squareup.workflow1.ui.internal.test.IdleAfterTestRule +import leakcanary.DetectLeaksAfterTestSuccess import org.junit.Rule import org.junit.Test +import org.junit.rules.RuleChain import org.junit.runner.RunWith @RunWith(AndroidJUnit4::class) @OptIn(WorkflowUiExperimentalApi::class) class HelloComposeWorkflowTest { - @get:Rule val composeRule = createAndroidComposeRule() - @get:Rule val idleAfterTest = IdleAfterTestRule + private val composeRule = createAndroidComposeRule() + @get:Rule val rules: RuleChain = RuleChain.outerRule(DetectLeaksAfterTestSuccess()) + .around(IdleAfterTestRule) + .around(composeRule) @Test fun togglesBetweenStates() { composeRule.onNodeWithText("Hello") diff --git a/samples/compose-samples/src/androidTest/java/com/squareup/sample/compose/inlinerendering/InlineRenderingTest.kt b/samples/compose-samples/src/androidTest/java/com/squareup/sample/compose/inlinerendering/InlineRenderingTest.kt index e1544a3ff..ed8bc57f8 100644 --- a/samples/compose-samples/src/androidTest/java/com/squareup/sample/compose/inlinerendering/InlineRenderingTest.kt +++ b/samples/compose-samples/src/androidTest/java/com/squareup/sample/compose/inlinerendering/InlineRenderingTest.kt @@ -8,16 +8,20 @@ import androidx.compose.ui.test.performClick import androidx.test.ext.junit.runners.AndroidJUnit4 import com.squareup.workflow1.ui.WorkflowUiExperimentalApi import com.squareup.workflow1.ui.internal.test.IdleAfterTestRule +import leakcanary.DetectLeaksAfterTestSuccess import org.junit.Rule import org.junit.Test +import org.junit.rules.RuleChain import org.junit.runner.RunWith @RunWith(AndroidJUnit4::class) @OptIn(WorkflowUiExperimentalApi::class) class InlineRenderingTest { - @get:Rule val composeRule = createAndroidComposeRule() - @get:Rule val idleAfterTest = IdleAfterTestRule + private val composeRule = createAndroidComposeRule() + @get:Rule val rules: RuleChain = RuleChain.outerRule(DetectLeaksAfterTestSuccess()) + .around(IdleAfterTestRule) + .around(composeRule) @Test fun counterIncrements() { composeRule.onNode(hasClickAction()) diff --git a/samples/compose-samples/src/androidTest/java/com/squareup/sample/compose/launcher/SampleLauncherTest.kt b/samples/compose-samples/src/androidTest/java/com/squareup/sample/compose/launcher/SampleLauncherTest.kt index f1d68ba84..2e1fe6f1d 100644 --- a/samples/compose-samples/src/androidTest/java/com/squareup/sample/compose/launcher/SampleLauncherTest.kt +++ b/samples/compose-samples/src/androidTest/java/com/squareup/sample/compose/launcher/SampleLauncherTest.kt @@ -13,16 +13,20 @@ import androidx.test.platform.app.InstrumentationRegistry import com.squareup.sample.compose.R import com.squareup.workflow1.ui.WorkflowUiExperimentalApi import com.squareup.workflow1.ui.internal.test.IdleAfterTestRule +import leakcanary.DetectLeaksAfterTestSuccess import org.junit.Rule import org.junit.Test +import org.junit.rules.RuleChain import org.junit.runner.RunWith @RunWith(AndroidJUnit4::class) @OptIn(WorkflowUiExperimentalApi::class) class SampleLauncherTest { - @get:Rule val composeRule = createAndroidComposeRule() - @get:Rule val idleAfterTest = IdleAfterTestRule + private val composeRule = createAndroidComposeRule() + @get:Rule val rules: RuleChain = RuleChain.outerRule(DetectLeaksAfterTestSuccess()) + .around(IdleAfterTestRule) + .around(composeRule) @OptIn(ExperimentalTestApi::class) @Test fun allSamplesLaunch() { diff --git a/samples/compose-samples/src/androidTest/java/com/squareup/sample/compose/nestedrenderings/NestedRenderingsTest.kt b/samples/compose-samples/src/androidTest/java/com/squareup/sample/compose/nestedrenderings/NestedRenderingsTest.kt index 6ca35dd5d..692c8b009 100644 --- a/samples/compose-samples/src/androidTest/java/com/squareup/sample/compose/nestedrenderings/NestedRenderingsTest.kt +++ b/samples/compose-samples/src/androidTest/java/com/squareup/sample/compose/nestedrenderings/NestedRenderingsTest.kt @@ -11,8 +11,10 @@ import androidx.compose.ui.test.performClick import androidx.test.ext.junit.runners.AndroidJUnit4 import com.squareup.workflow1.ui.WorkflowUiExperimentalApi import com.squareup.workflow1.ui.internal.test.IdleAfterTestRule +import leakcanary.DetectLeaksAfterTestSuccess import org.junit.Rule import org.junit.Test +import org.junit.rules.RuleChain import org.junit.runner.RunWith private const val ADD_BUTTON_TEXT = "Add Child" @@ -21,8 +23,10 @@ private const val ADD_BUTTON_TEXT = "Add Child" @OptIn(WorkflowUiExperimentalApi::class) class NestedRenderingsTest { - @get:Rule val composeRule = createAndroidComposeRule() - @get:Rule val idleAfterTest = IdleAfterTestRule + private val composeRule = createAndroidComposeRule() + @get:Rule val rules: RuleChain = RuleChain.outerRule(DetectLeaksAfterTestSuccess()) + .around(IdleAfterTestRule) + .around(composeRule) @Test fun childrenAreAddedAndRemoved() { composeRule.onNodeWithText(ADD_BUTTON_TEXT) diff --git a/samples/compose-samples/src/androidTest/java/com/squareup/sample/compose/preview/PreviewTest.kt b/samples/compose-samples/src/androidTest/java/com/squareup/sample/compose/preview/PreviewTest.kt index c20607921..40bdaa243 100644 --- a/samples/compose-samples/src/androidTest/java/com/squareup/sample/compose/preview/PreviewTest.kt +++ b/samples/compose-samples/src/androidTest/java/com/squareup/sample/compose/preview/PreviewTest.kt @@ -7,16 +7,20 @@ import androidx.compose.ui.test.onNodeWithText import androidx.test.ext.junit.runners.AndroidJUnit4 import com.squareup.workflow1.ui.WorkflowUiExperimentalApi import com.squareup.workflow1.ui.internal.test.IdleAfterTestRule +import leakcanary.DetectLeaksAfterTestSuccess import org.junit.Rule import org.junit.Test +import org.junit.rules.RuleChain import org.junit.runner.RunWith @RunWith(AndroidJUnit4::class) @OptIn(WorkflowUiExperimentalApi::class) class PreviewTest { - @get:Rule val composeRule = createAndroidComposeRule() - @get:Rule val idleAfterTest = IdleAfterTestRule + private val composeRule = createAndroidComposeRule() + @get:Rule val rules: RuleChain = RuleChain.outerRule(DetectLeaksAfterTestSuccess()) + .around(IdleAfterTestRule) + .around(composeRule) @Test fun showsPreviewRendering() { composeRule.onNodeWithText(ContactDetailsRendering::class.java.simpleName, substring = true) diff --git a/samples/compose-samples/src/androidTest/java/com/squareup/sample/compose/textinput/TextInputTest.kt b/samples/compose-samples/src/androidTest/java/com/squareup/sample/compose/textinput/TextInputTest.kt index 34fbc0ac8..a4dd7bd3c 100644 --- a/samples/compose-samples/src/androidTest/java/com/squareup/sample/compose/textinput/TextInputTest.kt +++ b/samples/compose-samples/src/androidTest/java/com/squareup/sample/compose/textinput/TextInputTest.kt @@ -15,16 +15,20 @@ import androidx.compose.ui.text.AnnotatedString import androidx.test.ext.junit.runners.AndroidJUnit4 import com.squareup.workflow1.ui.WorkflowUiExperimentalApi import com.squareup.workflow1.ui.internal.test.IdleAfterTestRule +import leakcanary.DetectLeaksAfterTestSuccess import org.junit.Rule import org.junit.Test +import org.junit.rules.RuleChain import org.junit.runner.RunWith @RunWith(AndroidJUnit4::class) @OptIn(WorkflowUiExperimentalApi::class) class TextInputTest { - @get:Rule val composeRule = createAndroidComposeRule() - @get:Rule val idleAfterTest = IdleAfterTestRule + private val composeRule = createAndroidComposeRule() + @get:Rule val rules: RuleChain = RuleChain.outerRule(DetectLeaksAfterTestSuccess()) + .around(IdleAfterTestRule) + .around(composeRule) @OptIn(ExperimentalTestApi::class) @Test fun allowsTextEditing() { diff --git a/samples/containers/app-poetry/src/androidTest/java/com/squareup/sample/poetryapp/PoetryAppTest.kt b/samples/containers/app-poetry/src/androidTest/java/com/squareup/sample/poetryapp/PoetryAppTest.kt index 611e513f0..e1762839e 100644 --- a/samples/containers/app-poetry/src/androidTest/java/com/squareup/sample/poetryapp/PoetryAppTest.kt +++ b/samples/containers/app-poetry/src/androidTest/java/com/squareup/sample/poetryapp/PoetryAppTest.kt @@ -8,18 +8,21 @@ import androidx.test.ext.junit.runners.AndroidJUnit4 import com.squareup.sample.container.poetryapp.R import com.squareup.workflow1.ui.WorkflowUiExperimentalApi import com.squareup.workflow1.ui.internal.test.inAnyView +import leakcanary.DetectLeaksAfterTestSuccess import org.junit.Rule import org.junit.Test +import org.junit.rules.RuleChain import org.junit.runner.RunWith @RunWith(AndroidJUnit4::class) @OptIn(WorkflowUiExperimentalApi::class) class PoetryAppTest { - @get:Rule val scenarioRule = ActivityScenarioRule(PoetryActivity::class.java) + private val scenarioRule = ActivityScenarioRule(PoetryActivity::class.java) + @get:Rule val rules = RuleChain.outerRule(DetectLeaksAfterTestSuccess()).around(scenarioRule)!! @Test fun launches() { inAnyView(withText(R.string.poems)) - .check(matches(isDisplayed())) + .check(matches(isDisplayed())) } } diff --git a/samples/containers/app-raven/src/androidTest/java/com/squareup/sample/ravenapp/RavenAppTest.kt b/samples/containers/app-raven/src/androidTest/java/com/squareup/sample/ravenapp/RavenAppTest.kt index ea53034af..ab6c4d7d7 100644 --- a/samples/containers/app-raven/src/androidTest/java/com/squareup/sample/ravenapp/RavenAppTest.kt +++ b/samples/containers/app-raven/src/androidTest/java/com/squareup/sample/ravenapp/RavenAppTest.kt @@ -7,18 +7,21 @@ import androidx.test.ext.junit.rules.ActivityScenarioRule import androidx.test.ext.junit.runners.AndroidJUnit4 import com.squareup.workflow1.ui.WorkflowUiExperimentalApi import com.squareup.workflow1.ui.internal.test.inAnyView +import leakcanary.DetectLeaksAfterTestSuccess import org.junit.Rule import org.junit.Test +import org.junit.rules.RuleChain import org.junit.runner.RunWith @RunWith(AndroidJUnit4::class) @OptIn(WorkflowUiExperimentalApi::class) class RavenAppTest { - @get:Rule val scenarioRule = ActivityScenarioRule(RavenActivity::class.java) + private val scenarioRule = ActivityScenarioRule(RavenActivity::class.java) + @get:Rule val rules = RuleChain.outerRule(DetectLeaksAfterTestSuccess()).around(scenarioRule)!! @Test fun launches() { inAnyView(withText("The Raven")) - .check(matches(isDisplayed())) + .check(matches(isDisplayed())) } } diff --git a/samples/containers/hello-back-button/src/androidTest/java/com/squareup/sample/hellobackbutton/HelloBackButtonEspressoTest.kt b/samples/containers/hello-back-button/src/androidTest/java/com/squareup/sample/hellobackbutton/HelloBackButtonEspressoTest.kt index 7a945c141..994fddd05 100644 --- a/samples/containers/hello-back-button/src/androidTest/java/com/squareup/sample/hellobackbutton/HelloBackButtonEspressoTest.kt +++ b/samples/containers/hello-back-button/src/androidTest/java/com/squareup/sample/hellobackbutton/HelloBackButtonEspressoTest.kt @@ -10,15 +10,18 @@ import androidx.test.ext.junit.runners.AndroidJUnit4 import com.squareup.workflow1.ui.WorkflowUiExperimentalApi import com.squareup.workflow1.ui.internal.test.inAnyView import com.squareup.workflow1.ui.internal.test.actuallyPressBack +import leakcanary.DetectLeaksAfterTestSuccess import org.junit.Rule import org.junit.Test +import org.junit.rules.RuleChain import org.junit.runner.RunWith @RunWith(AndroidJUnit4::class) @OptIn(WorkflowUiExperimentalApi::class) class HelloBackButtonEspressoTest { - @get:Rule val scenarioRule = ActivityScenarioRule(HelloBackButtonActivity::class.java) + private val scenarioRule = ActivityScenarioRule(HelloBackButtonActivity::class.java) + @get:Rule val rules = RuleChain.outerRule(DetectLeaksAfterTestSuccess()).around(scenarioRule)!! @Test fun wrappedTakesPrecedence() { inAnyView(withId(R.id.hello_message)).apply { diff --git a/samples/dungeon/app/build.gradle.kts b/samples/dungeon/app/build.gradle.kts index 27157d624..407ab65a2 100644 --- a/samples/dungeon/app/build.gradle.kts +++ b/samples/dungeon/app/build.gradle.kts @@ -12,7 +12,6 @@ android { multiDexEnabled = true testInstrumentationRunner = "com.squareup.sample.dungeon.DungeonTestRunner" - testInstrumentationRunnerArguments["listener"] = "leakcanary.FailTestOnLeakRunListener" } compileOptions { diff --git a/samples/dungeon/app/src/androidTest/java/com/squareup/sample/dungeon/DungeonAppTest.kt b/samples/dungeon/app/src/androidTest/java/com/squareup/sample/dungeon/DungeonAppTest.kt index e93bc8766..e5ac90078 100644 --- a/samples/dungeon/app/src/androidTest/java/com/squareup/sample/dungeon/DungeonAppTest.kt +++ b/samples/dungeon/app/src/androidTest/java/com/squareup/sample/dungeon/DungeonAppTest.kt @@ -7,15 +7,18 @@ import androidx.test.ext.junit.rules.ActivityScenarioRule import androidx.test.ext.junit.runners.AndroidJUnit4 import com.squareup.workflow1.ui.WorkflowUiExperimentalApi import com.squareup.workflow1.ui.internal.test.inAnyView +import leakcanary.DetectLeaksAfterTestSuccess import org.junit.Rule import org.junit.Test +import org.junit.rules.RuleChain import org.junit.runner.RunWith @RunWith(AndroidJUnit4::class) @OptIn(WorkflowUiExperimentalApi::class) class DungeonAppTest { - @get:Rule val scenarioRule = ActivityScenarioRule(DungeonActivity::class.java) + private val scenarioRule = ActivityScenarioRule(DungeonActivity::class.java) + @get:Rule val rules = RuleChain.outerRule(DetectLeaksAfterTestSuccess()).around(scenarioRule)!! @Test fun loadsBoardsList() { inAnyView(withText(R.string.boards_list_label)) diff --git a/samples/hello-workflow-fragment/src/androidTest/java/com/squareup/sample/helloworkflowfragment/HelloWorkflowFragmentAppTest.kt b/samples/hello-workflow-fragment/src/androidTest/java/com/squareup/sample/helloworkflowfragment/HelloWorkflowFragmentAppTest.kt index dd6ba0834..26bfeadb9 100644 --- a/samples/hello-workflow-fragment/src/androidTest/java/com/squareup/sample/helloworkflowfragment/HelloWorkflowFragmentAppTest.kt +++ b/samples/hello-workflow-fragment/src/androidTest/java/com/squareup/sample/helloworkflowfragment/HelloWorkflowFragmentAppTest.kt @@ -10,9 +10,11 @@ import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.filters.SdkSuppress import com.squareup.workflow1.ui.WorkflowUiExperimentalApi import com.squareup.workflow1.ui.internal.test.inAnyView +import leakcanary.DetectLeaksAfterTestSuccess import org.hamcrest.Matchers.containsString import org.junit.Rule import org.junit.Test +import org.junit.rules.RuleChain import org.junit.runner.RunWith // Life is too short to debug why LeakCanary breaks this on API 21 @@ -22,7 +24,8 @@ import org.junit.runner.RunWith @OptIn(WorkflowUiExperimentalApi::class) class HelloWorkflowFragmentAppTest { - @get:Rule val scenarioRule = ActivityScenarioRule(HelloWorkflowFragmentActivity::class.java) + private val scenarioRule = ActivityScenarioRule(HelloWorkflowFragmentActivity::class.java) + @get:Rule val rules = RuleChain.outerRule(DetectLeaksAfterTestSuccess()).around(scenarioRule)!! @Test fun togglesHelloAndGoodbye() { inAnyView(withText(containsString("Hello"))) diff --git a/samples/hello-workflow/src/androidTest/java/com/squareup/sample/helloworkflow/HelloWorkflowAppTest.kt b/samples/hello-workflow/src/androidTest/java/com/squareup/sample/helloworkflow/HelloWorkflowAppTest.kt index 998dcffb9..678841db6 100644 --- a/samples/hello-workflow/src/androidTest/java/com/squareup/sample/helloworkflow/HelloWorkflowAppTest.kt +++ b/samples/hello-workflow/src/androidTest/java/com/squareup/sample/helloworkflow/HelloWorkflowAppTest.kt @@ -8,15 +8,18 @@ import androidx.test.ext.junit.rules.ActivityScenarioRule import androidx.test.ext.junit.runners.AndroidJUnit4 import com.squareup.workflow1.ui.WorkflowUiExperimentalApi import com.squareup.workflow1.ui.internal.test.inAnyView +import leakcanary.DetectLeaksAfterTestSuccess import org.junit.Rule import org.junit.Test +import org.junit.rules.RuleChain import org.junit.runner.RunWith @RunWith(AndroidJUnit4::class) @OptIn(WorkflowUiExperimentalApi::class) class HelloWorkflowAppTest { - @get:Rule val scenarioRule = ActivityScenarioRule(HelloWorkflowActivity::class.java) + private val scenarioRule = ActivityScenarioRule(HelloWorkflowActivity::class.java) + @get:Rule val rules = RuleChain.outerRule(DetectLeaksAfterTestSuccess()).around(scenarioRule)!! @Test fun togglesHelloAndGoodbye() { inAnyView(withText("Hello")) diff --git a/samples/stub-visibility/src/androidTest/java/com/squareup/sample/stubvisibility/StubVisibilityAppTest.kt b/samples/stub-visibility/src/androidTest/java/com/squareup/sample/stubvisibility/StubVisibilityAppTest.kt index 4eea50c6e..df9f4da4b 100644 --- a/samples/stub-visibility/src/androidTest/java/com/squareup/sample/stubvisibility/StubVisibilityAppTest.kt +++ b/samples/stub-visibility/src/androidTest/java/com/squareup/sample/stubvisibility/StubVisibilityAppTest.kt @@ -9,16 +9,19 @@ import androidx.test.ext.junit.rules.ActivityScenarioRule import androidx.test.ext.junit.runners.AndroidJUnit4 import com.squareup.workflow1.ui.WorkflowUiExperimentalApi import com.squareup.workflow1.ui.internal.test.inAnyView +import leakcanary.DetectLeaksAfterTestSuccess import org.hamcrest.CoreMatchers.not import org.junit.Rule import org.junit.Test +import org.junit.rules.RuleChain import org.junit.runner.RunWith @RunWith(AndroidJUnit4::class) @OptIn(WorkflowUiExperimentalApi::class) internal class StubVisibilityAppTest { - @get:Rule val scenarioRule = ActivityScenarioRule(StubVisibilityActivity::class.java) + private val scenarioRule = ActivityScenarioRule(StubVisibilityActivity::class.java) + @get:Rule val rules = RuleChain.outerRule(DetectLeaksAfterTestSuccess()).around(scenarioRule)!! @Test fun togglesFooter() { inAnyView(withId(R.id.should_be_wrapped)) diff --git a/samples/tictactoe/app/src/androidTest/java/com/squareup/sample/TicTacToeEspressoTest.kt b/samples/tictactoe/app/src/androidTest/java/com/squareup/sample/TicTacToeEspressoTest.kt index e66c26ab4..e970e7469 100644 --- a/samples/tictactoe/app/src/androidTest/java/com/squareup/sample/TicTacToeEspressoTest.kt +++ b/samples/tictactoe/app/src/androidTest/java/com/squareup/sample/TicTacToeEspressoTest.kt @@ -27,10 +27,12 @@ import com.squareup.workflow1.ui.environment import com.squareup.workflow1.ui.getRendering import com.squareup.workflow1.ui.internal.test.inAnyView import com.squareup.workflow1.ui.internal.test.actuallyPressBack +import leakcanary.DetectLeaksAfterTestSuccess import org.junit.After import org.junit.Before import org.junit.Rule import org.junit.Test +import org.junit.rules.RuleChain import org.junit.runner.RunWith import java.util.concurrent.atomic.AtomicReference @@ -38,7 +40,8 @@ import java.util.concurrent.atomic.AtomicReference @RunWith(AndroidJUnit4::class) class TicTacToeEspressoTest { - @get:Rule var scenarioRule = ActivityScenarioRule(TicTacToeActivity::class.java) + private val scenarioRule = ActivityScenarioRule(TicTacToeActivity::class.java) + @get:Rule val rules = RuleChain.outerRule(DetectLeaksAfterTestSuccess()).around(scenarioRule)!! private val scenario get() = scenarioRule.scenario @Before diff --git a/samples/todo-android/app/src/androidTest/java/com/squareup/sample/mainactivity/TodoAppTest.kt b/samples/todo-android/app/src/androidTest/java/com/squareup/sample/mainactivity/TodoAppTest.kt index 253f1eead..dd87c622d 100644 --- a/samples/todo-android/app/src/androidTest/java/com/squareup/sample/mainactivity/TodoAppTest.kt +++ b/samples/todo-android/app/src/androidTest/java/com/squareup/sample/mainactivity/TodoAppTest.kt @@ -14,18 +14,21 @@ import com.squareup.sample.todo.ToDoActivity import com.squareup.workflow1.ui.WorkflowUiExperimentalApi import com.squareup.workflow1.ui.internal.test.inAnyView import com.squareup.workflow1.ui.internal.test.actuallyPressBack +import leakcanary.DetectLeaksAfterTestSuccess import org.hamcrest.Matchers.allOf import org.junit.After import org.junit.Before import org.junit.Rule import org.junit.Test +import org.junit.rules.RuleChain import org.junit.runner.RunWith @RunWith(AndroidJUnit4::class) @OptIn(WorkflowUiExperimentalApi::class) class TodoAppTest { - @get:Rule val scenarioRule = ActivityScenarioRule(ToDoActivity::class.java) + private val scenarioRule = ActivityScenarioRule(ToDoActivity::class.java) + @get:Rule val rules = RuleChain.outerRule(DetectLeaksAfterTestSuccess()).around(scenarioRule)!! private val uiDevice by lazy { UiDevice.getInstance(getInstrumentation()) } @Before diff --git a/workflow-ui/compose-tooling/src/androidTest/java/com/squareup/workflow1/ui/compose/tooling/PreviewViewFactoryTest.kt b/workflow-ui/compose-tooling/src/androidTest/java/com/squareup/workflow1/ui/compose/tooling/PreviewViewFactoryTest.kt index 2e15eb6de..ba1ba788c 100644 --- a/workflow-ui/compose-tooling/src/androidTest/java/com/squareup/workflow1/ui/compose/tooling/PreviewViewFactoryTest.kt +++ b/workflow-ui/compose-tooling/src/androidTest/java/com/squareup/workflow1/ui/compose/tooling/PreviewViewFactoryTest.kt @@ -19,16 +19,20 @@ import com.squareup.workflow1.ui.WorkflowUiExperimentalApi import com.squareup.workflow1.ui.compose.WorkflowRendering import com.squareup.workflow1.ui.compose.composeViewFactory import com.squareup.workflow1.ui.internal.test.IdleAfterTestRule +import leakcanary.DetectLeaksAfterTestSuccess import org.junit.Rule import org.junit.Test +import org.junit.rules.RuleChain import org.junit.runner.RunWith @OptIn(WorkflowUiExperimentalApi::class) @RunWith(AndroidJUnit4::class) internal class PreviewViewFactoryTest { - @get:Rule val composeRule = createComposeRule() - @get:Rule val idleAfterTest = IdleAfterTestRule + private val composeRule = createComposeRule() + @get:Rule val rules: RuleChain = RuleChain.outerRule(DetectLeaksAfterTestSuccess()) + .around(IdleAfterTestRule) + .around(composeRule) @Test fun singleChild() { composeRule.setContent { diff --git a/workflow-ui/compose/src/androidTest/java/com/squareup/workflow1/ui/compose/ComposeViewFactoryTest.kt b/workflow-ui/compose/src/androidTest/java/com/squareup/workflow1/ui/compose/ComposeViewFactoryTest.kt index b334a0e3d..dd57141a9 100644 --- a/workflow-ui/compose/src/androidTest/java/com/squareup/workflow1/ui/compose/ComposeViewFactoryTest.kt +++ b/workflow-ui/compose/src/androidTest/java/com/squareup/workflow1/ui/compose/ComposeViewFactoryTest.kt @@ -22,16 +22,21 @@ import com.squareup.workflow1.ui.ViewRegistry import com.squareup.workflow1.ui.WorkflowUiExperimentalApi import com.squareup.workflow1.ui.WorkflowViewStub import com.squareup.workflow1.ui.internal.test.IdleAfterTestRule +import leakcanary.DetectLeaksAfterTestSuccess import org.junit.Rule import org.junit.Test +import org.junit.rules.RuleChain import org.junit.runner.RunWith @OptIn(WorkflowUiExperimentalApi::class) @RunWith(AndroidJUnit4::class) internal class ComposeViewFactoryTest { - @get:Rule val composeRule = createComposeRule() - @get:Rule val idleAfterTest = IdleAfterTestRule + private val composeRule = createComposeRule() + @get:Rule val rules: RuleChain = + RuleChain.outerRule(DetectLeaksAfterTestSuccess()) + .around(IdleAfterTestRule) + .around(composeRule) @Test fun showsComposeContent() { val viewFactory = composeViewFactory { _, _ -> diff --git a/workflow-ui/compose/src/androidTest/java/com/squareup/workflow1/ui/compose/ComposeViewTreeIntegrationTest.kt b/workflow-ui/compose/src/androidTest/java/com/squareup/workflow1/ui/compose/ComposeViewTreeIntegrationTest.kt index a2338173a..ffc0022b4 100644 --- a/workflow-ui/compose/src/androidTest/java/com/squareup/workflow1/ui/compose/ComposeViewTreeIntegrationTest.kt +++ b/workflow-ui/compose/src/androidTest/java/com/squareup/workflow1/ui/compose/ComposeViewTreeIntegrationTest.kt @@ -37,18 +37,22 @@ import com.squareup.workflow1.ui.internal.test.IdleAfterTestRule import com.squareup.workflow1.ui.internal.test.WorkflowUiTestActivity import com.squareup.workflow1.ui.modal.HasModals import com.squareup.workflow1.ui.modal.ModalViewContainer +import leakcanary.DetectLeaksAfterTestSuccess import org.junit.Before import org.junit.Rule import org.junit.Test +import org.junit.rules.RuleChain import kotlin.reflect.KClass @OptIn(WorkflowUiExperimentalApi::class) internal class ComposeViewTreeIntegrationTest { - @get:Rule val composeRule = createAndroidComposeRule() - private val scenario get() = composeRule.activityRule.scenario + private val composeRule = createAndroidComposeRule() + @get:Rule val rules: RuleChain = RuleChain.outerRule(DetectLeaksAfterTestSuccess()) + .around(IdleAfterTestRule) + .around(composeRule) - @get:Rule val idleAfterTest = IdleAfterTestRule + private val scenario get() = composeRule.activityRule.scenario @Before fun setUp() { scenario.onActivity { diff --git a/workflow-ui/compose/src/androidTest/java/com/squareup/workflow1/ui/compose/CompositionRootTest.kt b/workflow-ui/compose/src/androidTest/java/com/squareup/workflow1/ui/compose/CompositionRootTest.kt index 5558b7181..5c49202ea 100644 --- a/workflow-ui/compose/src/androidTest/java/com/squareup/workflow1/ui/compose/CompositionRootTest.kt +++ b/workflow-ui/compose/src/androidTest/java/com/squareup/workflow1/ui/compose/CompositionRootTest.kt @@ -9,16 +9,20 @@ import androidx.compose.ui.test.onNodeWithText import androidx.test.ext.junit.runners.AndroidJUnit4 import com.squareup.workflow1.ui.WorkflowUiExperimentalApi import com.squareup.workflow1.ui.internal.test.IdleAfterTestRule +import leakcanary.DetectLeaksAfterTestSuccess import org.junit.Rule import org.junit.Test +import org.junit.rules.RuleChain import org.junit.runner.RunWith @RunWith(AndroidJUnit4::class) @OptIn(WorkflowUiExperimentalApi::class) internal class CompositionRootTest { - @get:Rule val composeRule = createComposeRule() - @get:Rule val idleAfterTest = IdleAfterTestRule + private val composeRule = createComposeRule() + @get:Rule val rules: RuleChain = RuleChain.outerRule(DetectLeaksAfterTestSuccess()) + .around(IdleAfterTestRule) + .around(composeRule) @Test fun wrappedWithRootIfNecessary_wrapsWhenNecessary() { val root: CompositionRoot = { content -> diff --git a/workflow-ui/compose/src/androidTest/java/com/squareup/workflow1/ui/compose/RenderAsStateTest.kt b/workflow-ui/compose/src/androidTest/java/com/squareup/workflow1/ui/compose/RenderAsStateTest.kt index 788247c5f..2937a44b2 100644 --- a/workflow-ui/compose/src/androidTest/java/com/squareup/workflow1/ui/compose/RenderAsStateTest.kt +++ b/workflow-ui/compose/src/androidTest/java/com/squareup/workflow1/ui/compose/RenderAsStateTest.kt @@ -32,11 +32,13 @@ import kotlinx.coroutines.awaitCancellation import kotlinx.coroutines.isActive import kotlinx.coroutines.job import kotlinx.coroutines.test.TestCoroutineScope +import leakcanary.DetectLeaksAfterTestSuccess import okio.ByteString import okio.ByteString.Companion.decodeBase64 import org.junit.Ignore import org.junit.Rule import org.junit.Test +import org.junit.rules.RuleChain import org.junit.runner.RunWith import kotlin.test.assertFailsWith @@ -44,8 +46,10 @@ import kotlin.test.assertFailsWith @OptIn(WorkflowUiExperimentalApi::class) internal class RenderAsStateTest { - @get:Rule val composeRule = createComposeRule() - @get:Rule val idleAfterTest = IdleAfterTestRule + private val composeRule = createComposeRule() + @get:Rule val rules: RuleChain = RuleChain.outerRule(DetectLeaksAfterTestSuccess()) + .around(IdleAfterTestRule) + .around(composeRule) @Test fun passesPropsThrough() { val workflow = Workflow.stateless { it } diff --git a/workflow-ui/compose/src/androidTest/java/com/squareup/workflow1/ui/compose/WorkflowRenderingTest.kt b/workflow-ui/compose/src/androidTest/java/com/squareup/workflow1/ui/compose/WorkflowRenderingTest.kt index a09e87c32..e8d6a24df 100644 --- a/workflow-ui/compose/src/androidTest/java/com/squareup/workflow1/ui/compose/WorkflowRenderingTest.kt +++ b/workflow-ui/compose/src/androidTest/java/com/squareup/workflow1/ui/compose/WorkflowRenderingTest.kt @@ -68,10 +68,12 @@ import com.squareup.workflow1.ui.ViewRegistry import com.squareup.workflow1.ui.WorkflowUiExperimentalApi import com.squareup.workflow1.ui.bindShowRendering import com.squareup.workflow1.ui.internal.test.IdleAfterTestRule +import leakcanary.DetectLeaksAfterTestSuccess import org.hamcrest.Description import org.hamcrest.TypeSafeMatcher import org.junit.Rule import org.junit.Test +import org.junit.rules.RuleChain import org.junit.runner.RunWith import kotlin.reflect.KClass @@ -79,8 +81,10 @@ import kotlin.reflect.KClass @RunWith(AndroidJUnit4::class) internal class WorkflowRenderingTest { - @get:Rule val composeRule = createComposeRule() - @get:Rule val idleAfterTest = IdleAfterTestRule + private val composeRule = createComposeRule() + @get:Rule val rules: RuleChain = RuleChain.outerRule(DetectLeaksAfterTestSuccess()) + .around(IdleAfterTestRule) + .around(composeRule) @Test fun doesNotRecompose_whenFactoryChanged() { val registry1 = ViewRegistry(composeViewFactory { rendering, _ -> @@ -393,6 +397,7 @@ internal class WorkflowRenderingTest { @Test fun skipsPreviousContentWhenIncompatible() { var disposeCount = 0 + class Rendering( override val compatibilityKey: String ) : ComposableRendering, Compatible { @@ -542,7 +547,7 @@ internal class WorkflowRenderingTest { private data class LegacyViewRendering( val text: String - ) : AndroidViewRendering { + ) : AndroidViewRendering { override val viewFactory: ViewFactory = object : ViewFactory { override val type = LegacyViewRendering::class diff --git a/workflow-ui/container-android/src/androidTest/java/com/squareup/workflow1/ui/backstack/test/BackstackContainerTest.kt b/workflow-ui/container-android/src/androidTest/java/com/squareup/workflow1/ui/backstack/test/BackstackContainerTest.kt index e62aa0ec6..fd696c496 100644 --- a/workflow-ui/container-android/src/androidTest/java/com/squareup/workflow1/ui/backstack/test/BackstackContainerTest.kt +++ b/workflow-ui/container-android/src/androidTest/java/com/squareup/workflow1/ui/backstack/test/BackstackContainerTest.kt @@ -16,14 +16,17 @@ import com.squareup.workflow1.ui.backstack.test.fixtures.BackStackContainerLifec import com.squareup.workflow1.ui.backstack.test.fixtures.ViewStateTestView import com.squareup.workflow1.ui.backstack.test.fixtures.viewForScreen import com.squareup.workflow1.ui.backstack.test.fixtures.waitForScreen +import leakcanary.DetectLeaksAfterTestSuccess import org.junit.Rule import org.junit.Test +import org.junit.rules.RuleChain @OptIn(WorkflowUiExperimentalApi::class) internal class BackstackContainerTest { - @get:Rule internal val scenarioRule = + private val scenarioRule = ActivityScenarioRule(BackStackContainerLifecycleActivity::class.java) + @get:Rule val rules = RuleChain.outerRule(DetectLeaksAfterTestSuccess()).around(scenarioRule)!! private val scenario get() = scenarioRule.scenario // region Basic instance state save/restore tests diff --git a/workflow-ui/container-android/src/androidTest/java/com/squareup/workflow1/ui/modal/test/ModalViewContainerLifecycleTest.kt b/workflow-ui/container-android/src/androidTest/java/com/squareup/workflow1/ui/modal/test/ModalViewContainerLifecycleTest.kt index 48a2a7aec..0e78948bd 100644 --- a/workflow-ui/container-android/src/androidTest/java/com/squareup/workflow1/ui/modal/test/ModalViewContainerLifecycleTest.kt +++ b/workflow-ui/container-android/src/androidTest/java/com/squareup/workflow1/ui/modal/test/ModalViewContainerLifecycleTest.kt @@ -8,16 +8,19 @@ import com.google.common.truth.Truth.assertThat import com.squareup.workflow1.ui.modal.ModalViewContainer import com.squareup.workflow1.ui.modal.test.ModalViewContainerLifecycleActivity.TestRendering.LeafRendering import com.squareup.workflow1.ui.modal.test.ModalViewContainerLifecycleActivity.TestRendering.RecurseRendering +import leakcanary.DetectLeaksAfterTestSuccess import org.junit.Rule import org.junit.Test +import org.junit.rules.RuleChain /** * Tests for [ModalViewContainer]'s [LifecycleOwner] integration. */ internal class ModalViewContainerLifecycleTest { - @get:Rule internal val scenarioRule = + private val scenarioRule = ActivityScenarioRule(ModalViewContainerLifecycleActivity::class.java) + @get:Rule val rules = RuleChain.outerRule(DetectLeaksAfterTestSuccess()).around(scenarioRule)!! private val scenario get() = scenarioRule.scenario /** diff --git a/workflow-ui/container-android/src/main/java/com/squareup/workflow1/ui/modal/ModalContainer.kt b/workflow-ui/container-android/src/main/java/com/squareup/workflow1/ui/modal/ModalContainer.kt index 13ce2fadf..3bc50e7e6 100644 --- a/workflow-ui/container-android/src/main/java/com/squareup/workflow1/ui/modal/ModalContainer.kt +++ b/workflow-ui/container-android/src/main/java/com/squareup/workflow1/ui/modal/ModalContainer.kt @@ -11,15 +11,14 @@ import android.view.View import android.view.ViewGroup import android.view.ViewGroup.LayoutParams.MATCH_PARENT import android.widget.FrameLayout +import androidx.lifecycle.DefaultLifecycleObserver import androidx.lifecycle.Lifecycle -import androidx.lifecycle.Lifecycle.Event.ON_DESTROY -import androidx.lifecycle.LifecycleObserver -import androidx.lifecycle.OnLifecycleEvent +import androidx.lifecycle.LifecycleOwner import com.squareup.workflow1.ui.Compatible import com.squareup.workflow1.ui.ViewEnvironment -import com.squareup.workflow1.ui.androidx.WorkflowLifecycleOwner import com.squareup.workflow1.ui.WorkflowUiExperimentalApi import com.squareup.workflow1.ui.WorkflowViewStub +import com.squareup.workflow1.ui.androidx.WorkflowLifecycleOwner import com.squareup.workflow1.ui.backstack.withBackStackStateKeyPrefix import com.squareup.workflow1.ui.compatible @@ -79,7 +78,9 @@ public abstract class ModalContainer @JvmOverloads constr dialogView.addOnAttachStateChangeListener( object : OnAttachStateChangeListener { - val onDestroy = OnDestroy { ref.dismiss() } + val dismissOnDestroy = object : DefaultLifecycleObserver { + override fun onDestroy(owner: LifecycleOwner) = ref.dismiss() + } var lifecycle: Lifecycle? = null override fun onViewAttachedToWindow(v: View) { // Note this is a different lifecycle than the WorkflowLifecycleOwner – it will @@ -87,11 +88,11 @@ public abstract class ModalContainer @JvmOverloads constr lifecycle = parentLifecycleOwner?.lifecycle // Android makes a lot of logcat noise if it has to close the window for us. :/ // https://github.com/square/workflow/issues/51 - lifecycle?.addObserver(onDestroy) + lifecycle?.addObserver(dismissOnDestroy) } override fun onViewDetachedFromWindow(v: View) { - lifecycle?.removeObserver(onDestroy) + lifecycle?.removeObserver(dismissOnDestroy) lifecycle = null } } @@ -246,10 +247,5 @@ public abstract class ModalContainer @JvmOverloads constr } } -private class OnDestroy(private val block: () -> Unit) : LifecycleObserver { - @OnLifecycleEvent(ON_DESTROY) - fun onDestroy() = block() -} - private val Dialog.decorView: View? get() = window?.decorView diff --git a/workflow-ui/core-android/build.gradle.kts b/workflow-ui/core-android/build.gradle.kts index 63fed5087..0289f88e2 100644 --- a/workflow-ui/core-android/build.gradle.kts +++ b/workflow-ui/core-android/build.gradle.kts @@ -27,6 +27,8 @@ dependencies { implementation(Dependencies.AndroidX.activity) implementation(Dependencies.AndroidX.fragment) implementation(Dependencies.AndroidX.Lifecycle.ktx) + implementation(Dependencies.AndroidX.Lifecycle.viewModel) + implementation(Dependencies.AndroidX.Lifecycle.viewModelKtx) implementation(Dependencies.AndroidX.savedstate) implementation(Dependencies.Kotlin.Coroutines.android) implementation(Dependencies.Kotlin.Coroutines.core) @@ -41,6 +43,5 @@ dependencies { testImplementation(Dependencies.Test.robolectric) androidTestImplementation(Dependencies.AndroidX.appcompat) - androidTestImplementation(Dependencies.AndroidX.Lifecycle.viewModel) androidTestImplementation(Dependencies.Test.truth) } diff --git a/workflow-ui/core-android/src/androidTest/java/com/squareup/workflow1/ui/WorkflowViewStubLifecycleTest.kt b/workflow-ui/core-android/src/androidTest/java/com/squareup/workflow1/ui/WorkflowViewStubLifecycleTest.kt index af76b8ab9..b3b02c7eb 100644 --- a/workflow-ui/core-android/src/androidTest/java/com/squareup/workflow1/ui/WorkflowViewStubLifecycleTest.kt +++ b/workflow-ui/core-android/src/androidTest/java/com/squareup/workflow1/ui/WorkflowViewStubLifecycleTest.kt @@ -29,9 +29,11 @@ import com.squareup.workflow1.ui.WorkflowViewStubLifecycleActivity.TestRendering import com.squareup.workflow1.ui.WorkflowViewStubLifecycleActivity.TestRendering.LeafRendering import com.squareup.workflow1.ui.WorkflowViewStubLifecycleActivity.TestRendering.RecurseRendering import com.squareup.workflow1.ui.WorkflowViewStubLifecycleActivity.TestRendering.ViewRendering +import leakcanary.DetectLeaksAfterTestSuccess import org.hamcrest.Matchers.equalTo import org.junit.Rule import org.junit.Test +import org.junit.rules.RuleChain /** * Tests for [WorkflowViewStub]'s [LifecycleOwner] integration. @@ -39,8 +41,9 @@ import org.junit.Test @OptIn(WorkflowUiExperimentalApi::class) internal class WorkflowViewStubLifecycleTest { - @get:Rule internal val scenarioRule = + private val scenarioRule = ActivityScenarioRule(WorkflowViewStubLifecycleActivity::class.java) + @get:Rule val rules = RuleChain.outerRule(DetectLeaksAfterTestSuccess()).around(scenarioRule)!! private val scenario get() = scenarioRule.scenario /**