From 4303e1b89463881928d57600ed5c2a11fae5f483 Mon Sep 17 00:00:00 2001 From: Zac Sweers Date: Sun, 8 Aug 2021 21:36:47 -0400 Subject: [PATCH 1/5] Rebase and update --- .../Module.md | 2 +- .../lifecycle/test/KotlinExtensions.kt | 1 + .../lifecycle/test/TestLifecycleOwner.java | 3 + .../build.gradle | 1 + .../AndroidLifecycleScopeProviderTest.java | 136 +++++++++--------- docs/index.md | 3 +- gradle/dependencies.gradle | 1 + settings.gradle | 8 +- 8 files changed, 80 insertions(+), 75 deletions(-) diff --git a/android/autodispose-androidx-lifecycle-test/Module.md b/android/autodispose-androidx-lifecycle-test/Module.md index 5021b801b..8591b1172 100644 --- a/android/autodispose-androidx-lifecycle-test/Module.md +++ b/android/autodispose-androidx-lifecycle-test/Module.md @@ -1,3 +1,3 @@ # Module autodispose-androidx-lifecycle-test -Test helpers for `autodispose-androidx-lifecycle` support. +DEPRECATED diff --git a/android/autodispose-androidx-lifecycle-test/src/main/java/autodispose2/androidx/lifecycle/test/KotlinExtensions.kt b/android/autodispose-androidx-lifecycle-test/src/main/java/autodispose2/androidx/lifecycle/test/KotlinExtensions.kt index 58b4ce868..d0cd44b9e 100644 --- a/android/autodispose-androidx-lifecycle-test/src/main/java/autodispose2/androidx/lifecycle/test/KotlinExtensions.kt +++ b/android/autodispose-androidx-lifecycle-test/src/main/java/autodispose2/androidx/lifecycle/test/KotlinExtensions.kt @@ -26,4 +26,5 @@ import io.reactivex.rxjava3.annotations.CheckReturnValue */ @SuppressLint("RestrictedApi") @CheckReturnValue +@Deprecated("Switch to androidx.lifecycle.testing.TestLifecycleOwner") inline fun LifecycleRegistry.test(): TestLifecycleOwner = TestLifecycleOwner.create(this) diff --git a/android/autodispose-androidx-lifecycle-test/src/main/java/autodispose2/androidx/lifecycle/test/TestLifecycleOwner.java b/android/autodispose-androidx-lifecycle-test/src/main/java/autodispose2/androidx/lifecycle/test/TestLifecycleOwner.java index 12e059b58..2b902f2e9 100644 --- a/android/autodispose-androidx-lifecycle-test/src/main/java/autodispose2/androidx/lifecycle/test/TestLifecycleOwner.java +++ b/android/autodispose-androidx-lifecycle-test/src/main/java/autodispose2/androidx/lifecycle/test/TestLifecycleOwner.java @@ -26,7 +26,10 @@ /** * A test {@link LifecycleOwner} implementation for testing. You can either back it with your own * instance or just stub it in place and use its public emit() API. + * + * @deprecated Switch to androidx.lifecycle.testing.TestLifecycleOwner */ +@Deprecated @RestrictTo(TESTS) public final class TestLifecycleOwner implements LifecycleOwner { diff --git a/android/autodispose-androidx-lifecycle/build.gradle b/android/autodispose-androidx-lifecycle/build.gradle index e34f1d8ca..31791ff40 100644 --- a/android/autodispose-androidx-lifecycle/build.gradle +++ b/android/autodispose-androidx-lifecycle/build.gradle @@ -33,6 +33,7 @@ dependencies { androidTestImplementation project(':android:autodispose-androidx-lifecycle-test') androidTestImplementation project(':test-utils') + androidTestImplementation deps.androidx.lifecycle.runtimeTest androidTestImplementation deps.test.androidExtJunit androidTestImplementation deps.test.androidRunner androidTestImplementation deps.test.androidRules diff --git a/android/autodispose-androidx-lifecycle/src/androidTest/java/autodispose2/androidx/lifecycle/AndroidLifecycleScopeProviderTest.java b/android/autodispose-androidx-lifecycle/src/androidTest/java/autodispose2/androidx/lifecycle/AndroidLifecycleScopeProviderTest.java index e7964560e..e685f8698 100644 --- a/android/autodispose-androidx-lifecycle/src/androidTest/java/autodispose2/androidx/lifecycle/AndroidLifecycleScopeProviderTest.java +++ b/android/autodispose-androidx-lifecycle/src/androidTest/java/autodispose2/androidx/lifecycle/AndroidLifecycleScopeProviderTest.java @@ -23,7 +23,7 @@ import androidx.test.annotation.UiThreadTest; import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.platform.app.InstrumentationRegistry; -import autodispose2.androidx.lifecycle.test.TestLifecycleOwner; +import androidx.lifecycle.testing.TestLifecycleOwner; import autodispose2.lifecycle.LifecycleEndedException; import autodispose2.test.RecordingObserver; import io.reactivex.rxjava3.disposables.Disposable; @@ -44,11 +44,11 @@ public void observable_beforeCreate() { final PublishSubject subject = PublishSubject.create(); // Spin it up - TestLifecycleOwner lifecycle = TestLifecycleOwner.create(); + TestLifecycleOwner lifecycle = new TestLifecycleOwner(Lifecycle.State.INITIALIZED); subject.to(autoDisposable(AndroidLifecycleScopeProvider.from(lifecycle))).subscribe(o); - lifecycle.emit(Lifecycle.Event.ON_CREATE); - lifecycle.emit(Lifecycle.Event.ON_START); - lifecycle.emit(Lifecycle.Event.ON_RESUME); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_RESUME); o.takeSubscribe(); o.assertNoMoreEvents(); // No initial value. @@ -59,9 +59,9 @@ public void observable_beforeCreate() { subject.onNext(1); assertThat(o.takeNext()).isEqualTo(1); - lifecycle.emit(Lifecycle.Event.ON_PAUSE); - lifecycle.emit(Lifecycle.Event.ON_STOP); - lifecycle.emit(Lifecycle.Event.ON_DESTROY); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_STOP); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY); subject.onNext(2); o.assertNoMoreEvents(); @@ -74,11 +74,11 @@ public void observable_createDestroy() { final PublishSubject subject = PublishSubject.create(); // Spin it up - TestLifecycleOwner lifecycle = TestLifecycleOwner.create(); - lifecycle.emit(Lifecycle.Event.ON_CREATE); + TestLifecycleOwner lifecycle = new TestLifecycleOwner(Lifecycle.State.INITIALIZED); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE); subject.to(autoDisposable(AndroidLifecycleScopeProvider.from(lifecycle))).subscribe(o); - lifecycle.emit(Lifecycle.Event.ON_START); - lifecycle.emit(Lifecycle.Event.ON_RESUME); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_RESUME); o.takeSubscribe(); o.assertNoMoreEvents(); // No initial value. @@ -89,9 +89,9 @@ public void observable_createDestroy() { subject.onNext(1); assertThat(o.takeNext()).isEqualTo(1); - lifecycle.emit(Lifecycle.Event.ON_PAUSE); - lifecycle.emit(Lifecycle.Event.ON_STOP); - lifecycle.emit(Lifecycle.Event.ON_DESTROY); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_STOP); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY); subject.onNext(2); o.assertNoMoreEvents(); @@ -104,11 +104,11 @@ public void observable_startStop() { final PublishSubject subject = PublishSubject.create(); // Spin it up - TestLifecycleOwner lifecycle = TestLifecycleOwner.create(); - lifecycle.emit(Lifecycle.Event.ON_CREATE); - lifecycle.emit(Lifecycle.Event.ON_START); + TestLifecycleOwner lifecycle = new TestLifecycleOwner(Lifecycle.State.INITIALIZED); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START); subject.to(autoDisposable(AndroidLifecycleScopeProvider.from(lifecycle))).subscribe(o); - lifecycle.emit(Lifecycle.Event.ON_RESUME); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_RESUME); o.takeSubscribe(); o.assertNoMoreEvents(); // No initial value. @@ -119,14 +119,14 @@ public void observable_startStop() { subject.onNext(1); assertThat(o.takeNext()).isEqualTo(1); - lifecycle.emit(Lifecycle.Event.ON_PAUSE); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE); // We should stop here - lifecycle.emit(Lifecycle.Event.ON_STOP); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_STOP); subject.onNext(2); o.assertNoMoreEvents(); - lifecycle.emit(Lifecycle.Event.ON_DESTROY); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY); } @Test @@ -136,10 +136,10 @@ public void observable_resumePause() { final PublishSubject subject = PublishSubject.create(); // Spin it up - TestLifecycleOwner lifecycle = TestLifecycleOwner.create(); - lifecycle.emit(Lifecycle.Event.ON_CREATE); - lifecycle.emit(Lifecycle.Event.ON_START); - lifecycle.emit(Lifecycle.Event.ON_RESUME); + TestLifecycleOwner lifecycle = new TestLifecycleOwner(Lifecycle.State.INITIALIZED); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_RESUME); subject.to(autoDisposable(AndroidLifecycleScopeProvider.from(lifecycle))).subscribe(o); o.takeSubscribe(); @@ -152,12 +152,12 @@ public void observable_resumePause() { assertThat(o.takeNext()).isEqualTo(1); // We should stop here - lifecycle.emit(Lifecycle.Event.ON_PAUSE); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE); subject.onNext(2); o.assertNoMoreEvents(); - lifecycle.emit(Lifecycle.Event.ON_STOP); - lifecycle.emit(Lifecycle.Event.ON_DESTROY); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_STOP); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY); } @Test @@ -167,13 +167,13 @@ public void observable_createPause() { final PublishSubject subject = PublishSubject.create(); // Spin it up - TestLifecycleOwner lifecycle = TestLifecycleOwner.create(); - lifecycle.emit(Lifecycle.Event.ON_CREATE); + TestLifecycleOwner lifecycle = new TestLifecycleOwner(Lifecycle.State.INITIALIZED); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE); subject .to(autoDisposable(AndroidLifecycleScopeProvider.from(lifecycle, Lifecycle.Event.ON_PAUSE))) .subscribe(o); - lifecycle.emit(Lifecycle.Event.ON_START); - lifecycle.emit(Lifecycle.Event.ON_RESUME); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_RESUME); o.takeSubscribe(); o.assertNoMoreEvents(); // No initial value. @@ -185,12 +185,12 @@ public void observable_createPause() { assertThat(o.takeNext()).isEqualTo(1); // We should stop here - lifecycle.emit(Lifecycle.Event.ON_PAUSE); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE); subject.onNext(2); o.assertNoMoreEvents(); - lifecycle.emit(Lifecycle.Event.ON_STOP); - lifecycle.emit(Lifecycle.Event.ON_DESTROY); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_STOP); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY); } @Test @@ -200,10 +200,10 @@ public void observable_resumeDestroy() { final PublishSubject subject = PublishSubject.create(); // Spin it up - TestLifecycleOwner lifecycle = TestLifecycleOwner.create(); - lifecycle.emit(Lifecycle.Event.ON_CREATE); - lifecycle.emit(Lifecycle.Event.ON_START); - lifecycle.emit(Lifecycle.Event.ON_RESUME); + TestLifecycleOwner lifecycle = new TestLifecycleOwner(Lifecycle.State.INITIALIZED); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_RESUME); subject .to( autoDisposable( @@ -219,14 +219,14 @@ public void observable_resumeDestroy() { subject.onNext(1); assertThat(o.takeNext()).isEqualTo(1); - lifecycle.emit(Lifecycle.Event.ON_PAUSE); - lifecycle.emit(Lifecycle.Event.ON_STOP); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_STOP); subject.onNext(2); assertThat(o.takeNext()).isEqualTo(2); // We should stop here - lifecycle.emit(Lifecycle.Event.ON_DESTROY); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY); subject.onNext(3); o.assertNoMoreEvents(); } @@ -237,11 +237,11 @@ public void observable_offAfterPause_shouldStopOnStop() { final RecordingObserver o = new RecordingObserver<>(LOGGER); final PublishSubject subject = PublishSubject.create(); - TestLifecycleOwner lifecycle = TestLifecycleOwner.create(); - lifecycle.emit(Lifecycle.Event.ON_CREATE); - lifecycle.emit(Lifecycle.Event.ON_START); - lifecycle.emit(Lifecycle.Event.ON_RESUME); - lifecycle.emit(Lifecycle.Event.ON_PAUSE); + TestLifecycleOwner lifecycle = new TestLifecycleOwner(Lifecycle.State.INITIALIZED); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_RESUME); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE); subject.to(autoDisposable(AndroidLifecycleScopeProvider.from(lifecycle))).subscribe(o); o.takeSubscribe(); @@ -250,12 +250,12 @@ public void observable_offAfterPause_shouldStopOnStop() { assertThat(o.takeNext()).isEqualTo(2); // We could resume again - lifecycle.emit(Lifecycle.Event.ON_RESUME); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_RESUME); subject.onNext(3); assertThat(o.takeNext()).isEqualTo(3); // We should stop here - lifecycle.emit(Lifecycle.Event.ON_STOP); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_STOP); subject.onNext(3); o.assertNoMoreEvents(); } @@ -266,12 +266,12 @@ public void observable_offAfterOnDestroyView_shouldStopOnDestroy() { final RecordingObserver o = new RecordingObserver<>(LOGGER); final PublishSubject subject = PublishSubject.create(); - TestLifecycleOwner lifecycle = TestLifecycleOwner.create(); - lifecycle.emit(Lifecycle.Event.ON_CREATE); - lifecycle.emit(Lifecycle.Event.ON_START); - lifecycle.emit(Lifecycle.Event.ON_RESUME); - lifecycle.emit(Lifecycle.Event.ON_PAUSE); - lifecycle.emit(Lifecycle.Event.ON_STOP); + TestLifecycleOwner lifecycle = new TestLifecycleOwner(Lifecycle.State.INITIALIZED); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_RESUME); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_STOP); // In a CREATED state now but the next event will be destroy // This simulates subscribing in fragments' onDestroyView, where we want the subscription to // still dispose properly in onDestroy. @@ -283,7 +283,7 @@ public void observable_offAfterOnDestroyView_shouldStopOnDestroy() { assertThat(o.takeNext()).isEqualTo(2); // We should stop here - lifecycle.emit(Lifecycle.Event.ON_DESTROY); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY); subject.onNext(3); o.assertNoMoreEvents(); } @@ -294,13 +294,13 @@ public void observable_offMainThread_shouldFail() { PublishSubject subject = PublishSubject.create(); // Spin it up - TestLifecycleOwner lifecycle = TestLifecycleOwner.create(); + TestLifecycleOwner lifecycle = new TestLifecycleOwner(Lifecycle.State.INITIALIZED); InstrumentationRegistry.getInstrumentation() .runOnMainSync( () -> { - lifecycle.emit(Lifecycle.Event.ON_CREATE); - lifecycle.emit(Lifecycle.Event.ON_START); - lifecycle.emit(Lifecycle.Event.ON_RESUME); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_RESUME); }); subject.to(autoDisposable(AndroidLifecycleScopeProvider.from(lifecycle))).subscribe(o); o.takeSubscribe(); @@ -316,13 +316,13 @@ public void observable_offAfterDestroy_shouldFail() { final RecordingObserver o = new RecordingObserver<>(LOGGER); final PublishSubject subject = PublishSubject.create(); - TestLifecycleOwner lifecycle = TestLifecycleOwner.create(); - lifecycle.emit(Lifecycle.Event.ON_CREATE); - lifecycle.emit(Lifecycle.Event.ON_START); - lifecycle.emit(Lifecycle.Event.ON_RESUME); - lifecycle.emit(Lifecycle.Event.ON_PAUSE); - lifecycle.emit(Lifecycle.Event.ON_STOP); - lifecycle.emit(Lifecycle.Event.ON_DESTROY); + TestLifecycleOwner lifecycle = new TestLifecycleOwner(Lifecycle.State.INITIALIZED); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_RESUME); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_STOP); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY); subject.to(autoDisposable(AndroidLifecycleScopeProvider.from(lifecycle))).subscribe(o); Disposable d = o.takeSubscribe(); diff --git a/docs/index.md b/docs/index.md index 292f60afd..222dae26e 100644 --- a/docs/index.md +++ b/docs/index.md @@ -178,8 +178,7 @@ for generating `CompletableSource` representations from `LifecycleScopeProvider` There are three artifacts with extra support for Android: * `autodispose-android` has a `ViewScopeProvider` for use with Android `View` classes. -* `autodispose-android-archcomponents` has a `AndroidLifecycleScopeProvider` for use with `LifecycleOwner` and `Lifecycle` implementations. -* `autodispose-android-archcomponents-test` has a `TestLifecycleOwner` for use in testing. +* `autodispose-androidx-lifecycle` has a `AndroidLifecycleScopeProvider` for use with `LifecycleOwner` and `Lifecycle` implementations. Note that the project is compiled against Java 8. If you need support for lower Java versions, you should use D8 (Android Gradle Plugin 3.2+) or desugar as needed (depending on the build system). diff --git a/gradle/dependencies.gradle b/gradle/dependencies.gradle index b51a1f026..549e9f708 100644 --- a/gradle/dependencies.gradle +++ b/gradle/dependencies.gradle @@ -84,6 +84,7 @@ def androidx = [ common: "androidx.lifecycle:lifecycle-common:${versions.androidxLifecycle}", runtime: "androidx.lifecycle:lifecycle-runtime:${versions.androidxLifecycle}", runtimeKtx: "androidx.lifecycle:lifecycle-runtime-ktx:${versions.androidxLifecycle}", + runtimeTest: "androidx.lifecycle:lifecycle-runtime-testing:${versions.androidxLifecycle}", vmKtx: "androidx.lifecycle:lifecycle-viewmodel-ktx:${versions.androidxLifecycle}" ] ] diff --git a/settings.gradle b/settings.gradle index 019ff256c..39151891d 100644 --- a/settings.gradle +++ b/settings.gradle @@ -47,11 +47,11 @@ pluginManagement { rootProject.name = 'autodispose-root' if (System.getenv("ANDROID_HOME") != null) { - include ':android:autodispose-android' - include ':android:autodispose-androidx-lifecycle' - include ':android:autodispose-androidx-lifecycle-test' - include ':sample' } +include ':android:autodispose-android' +include ':android:autodispose-androidx-lifecycle' +include ':android:autodispose-androidx-lifecycle-test' +include ':sample' include ':autodispose' include ':autodispose-interop:coroutines' include ':autodispose-lifecycle' From 1dedf3ae406d8412554b8a615c100fe4a234fae4 Mon Sep 17 00:00:00 2001 From: Zac Sweers Date: Sun, 8 Aug 2021 21:45:34 -0400 Subject: [PATCH 2/5] Simplify creation states --- .../AndroidLifecycleScopeProviderTest.java | 65 +++++-------------- 1 file changed, 16 insertions(+), 49 deletions(-) diff --git a/android/autodispose-androidx-lifecycle/src/androidTest/java/autodispose2/androidx/lifecycle/AndroidLifecycleScopeProviderTest.java b/android/autodispose-androidx-lifecycle/src/androidTest/java/autodispose2/androidx/lifecycle/AndroidLifecycleScopeProviderTest.java index e685f8698..7d8d782ed 100644 --- a/android/autodispose-androidx-lifecycle/src/androidTest/java/autodispose2/androidx/lifecycle/AndroidLifecycleScopeProviderTest.java +++ b/android/autodispose-androidx-lifecycle/src/androidTest/java/autodispose2/androidx/lifecycle/AndroidLifecycleScopeProviderTest.java @@ -46,9 +46,7 @@ public void observable_beforeCreate() { // Spin it up TestLifecycleOwner lifecycle = new TestLifecycleOwner(Lifecycle.State.INITIALIZED); subject.to(autoDisposable(AndroidLifecycleScopeProvider.from(lifecycle))).subscribe(o); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_RESUME); + lifecycle.setCurrentState(Lifecycle.State.RESUMED); o.takeSubscribe(); o.assertNoMoreEvents(); // No initial value. @@ -59,9 +57,7 @@ public void observable_beforeCreate() { subject.onNext(1); assertThat(o.takeNext()).isEqualTo(1); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_STOP); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY); + lifecycle.setCurrentState(Lifecycle.State.DESTROYED); subject.onNext(2); o.assertNoMoreEvents(); @@ -74,11 +70,9 @@ public void observable_createDestroy() { final PublishSubject subject = PublishSubject.create(); // Spin it up - TestLifecycleOwner lifecycle = new TestLifecycleOwner(Lifecycle.State.INITIALIZED); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE); + TestLifecycleOwner lifecycle = new TestLifecycleOwner(Lifecycle.State.CREATED); subject.to(autoDisposable(AndroidLifecycleScopeProvider.from(lifecycle))).subscribe(o); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_RESUME); + lifecycle.setCurrentState(Lifecycle.State.RESUMED); o.takeSubscribe(); o.assertNoMoreEvents(); // No initial value. @@ -89,9 +83,7 @@ public void observable_createDestroy() { subject.onNext(1); assertThat(o.takeNext()).isEqualTo(1); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_STOP); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY); + lifecycle.setCurrentState(Lifecycle.State.DESTROYED); subject.onNext(2); o.assertNoMoreEvents(); @@ -104,11 +96,9 @@ public void observable_startStop() { final PublishSubject subject = PublishSubject.create(); // Spin it up - TestLifecycleOwner lifecycle = new TestLifecycleOwner(Lifecycle.State.INITIALIZED); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START); + TestLifecycleOwner lifecycle = new TestLifecycleOwner(Lifecycle.State.STARTED); subject.to(autoDisposable(AndroidLifecycleScopeProvider.from(lifecycle))).subscribe(o); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_RESUME); + lifecycle.setCurrentState(Lifecycle.State.RESUMED); o.takeSubscribe(); o.assertNoMoreEvents(); // No initial value. @@ -136,10 +126,7 @@ public void observable_resumePause() { final PublishSubject subject = PublishSubject.create(); // Spin it up - TestLifecycleOwner lifecycle = new TestLifecycleOwner(Lifecycle.State.INITIALIZED); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_RESUME); + TestLifecycleOwner lifecycle = new TestLifecycleOwner(Lifecycle.State.RESUMED); subject.to(autoDisposable(AndroidLifecycleScopeProvider.from(lifecycle))).subscribe(o); o.takeSubscribe(); @@ -167,13 +154,11 @@ public void observable_createPause() { final PublishSubject subject = PublishSubject.create(); // Spin it up - TestLifecycleOwner lifecycle = new TestLifecycleOwner(Lifecycle.State.INITIALIZED); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE); + TestLifecycleOwner lifecycle = new TestLifecycleOwner(Lifecycle.State.CREATED); subject .to(autoDisposable(AndroidLifecycleScopeProvider.from(lifecycle, Lifecycle.Event.ON_PAUSE))) .subscribe(o); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_RESUME); + lifecycle.setCurrentState(Lifecycle.State.RESUMED); o.takeSubscribe(); o.assertNoMoreEvents(); // No initial value. @@ -200,10 +185,7 @@ public void observable_resumeDestroy() { final PublishSubject subject = PublishSubject.create(); // Spin it up - TestLifecycleOwner lifecycle = new TestLifecycleOwner(Lifecycle.State.INITIALIZED); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_RESUME); + TestLifecycleOwner lifecycle = new TestLifecycleOwner(Lifecycle.State.RESUMED); subject .to( autoDisposable( @@ -237,10 +219,7 @@ public void observable_offAfterPause_shouldStopOnStop() { final RecordingObserver o = new RecordingObserver<>(LOGGER); final PublishSubject subject = PublishSubject.create(); - TestLifecycleOwner lifecycle = new TestLifecycleOwner(Lifecycle.State.INITIALIZED); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_RESUME); + TestLifecycleOwner lifecycle = new TestLifecycleOwner(Lifecycle.State.RESUMED); lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE); subject.to(autoDisposable(AndroidLifecycleScopeProvider.from(lifecycle))).subscribe(o); @@ -266,10 +245,7 @@ public void observable_offAfterOnDestroyView_shouldStopOnDestroy() { final RecordingObserver o = new RecordingObserver<>(LOGGER); final PublishSubject subject = PublishSubject.create(); - TestLifecycleOwner lifecycle = new TestLifecycleOwner(Lifecycle.State.INITIALIZED); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_RESUME); + TestLifecycleOwner lifecycle = new TestLifecycleOwner(Lifecycle.State.RESUMED); lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE); lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_STOP); // In a CREATED state now but the next event will be destroy @@ -297,11 +273,7 @@ public void observable_offMainThread_shouldFail() { TestLifecycleOwner lifecycle = new TestLifecycleOwner(Lifecycle.State.INITIALIZED); InstrumentationRegistry.getInstrumentation() .runOnMainSync( - () -> { - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_RESUME); - }); + () -> lifecycle.setCurrentState(Lifecycle.State.RESUMED)); subject.to(autoDisposable(AndroidLifecycleScopeProvider.from(lifecycle))).subscribe(o); o.takeSubscribe(); Throwable t = o.takeError(); @@ -316,13 +288,8 @@ public void observable_offAfterDestroy_shouldFail() { final RecordingObserver o = new RecordingObserver<>(LOGGER); final PublishSubject subject = PublishSubject.create(); - TestLifecycleOwner lifecycle = new TestLifecycleOwner(Lifecycle.State.INITIALIZED); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_RESUME); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_STOP); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY); + TestLifecycleOwner lifecycle = new TestLifecycleOwner(Lifecycle.State.RESUMED); + lifecycle.setCurrentState(Lifecycle.State.DESTROYED); subject.to(autoDisposable(AndroidLifecycleScopeProvider.from(lifecycle))).subscribe(o); Disposable d = o.takeSubscribe(); From c889059842b612d735ab6b2106dd18a923cc14c7 Mon Sep 17 00:00:00 2001 From: Zac Sweers Date: Sun, 8 Aug 2021 21:48:43 -0400 Subject: [PATCH 3/5] Clean up destroy states --- .../lifecycle/AndroidLifecycleScopeProviderTest.java | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/android/autodispose-androidx-lifecycle/src/androidTest/java/autodispose2/androidx/lifecycle/AndroidLifecycleScopeProviderTest.java b/android/autodispose-androidx-lifecycle/src/androidTest/java/autodispose2/androidx/lifecycle/AndroidLifecycleScopeProviderTest.java index 7d8d782ed..cea738894 100644 --- a/android/autodispose-androidx-lifecycle/src/androidTest/java/autodispose2/androidx/lifecycle/AndroidLifecycleScopeProviderTest.java +++ b/android/autodispose-androidx-lifecycle/src/androidTest/java/autodispose2/androidx/lifecycle/AndroidLifecycleScopeProviderTest.java @@ -116,7 +116,7 @@ public void observable_startStop() { subject.onNext(2); o.assertNoMoreEvents(); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY); + lifecycle.setCurrentState(Lifecycle.State.DESTROYED); } @Test @@ -143,8 +143,7 @@ public void observable_resumePause() { subject.onNext(2); o.assertNoMoreEvents(); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_STOP); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY); + lifecycle.setCurrentState(Lifecycle.State.DESTROYED); } @Test @@ -174,8 +173,7 @@ public void observable_createPause() { subject.onNext(2); o.assertNoMoreEvents(); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_STOP); - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY); + lifecycle.setCurrentState(Lifecycle.State.DESTROYED); } @Test @@ -208,7 +206,7 @@ public void observable_resumeDestroy() { assertThat(o.takeNext()).isEqualTo(2); // We should stop here - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY); + lifecycle.setCurrentState(Lifecycle.State.DESTROYED); subject.onNext(3); o.assertNoMoreEvents(); } @@ -259,7 +257,7 @@ public void observable_offAfterOnDestroyView_shouldStopOnDestroy() { assertThat(o.takeNext()).isEqualTo(2); // We should stop here - lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY); + lifecycle.setCurrentState(Lifecycle.State.DESTROYED); subject.onNext(3); o.assertNoMoreEvents(); } From e3551ab5ea7c592c7f6bff9c5a9c8f90eb203f4d Mon Sep 17 00:00:00 2001 From: Zac Sweers Date: Sun, 8 Aug 2021 21:49:10 -0400 Subject: [PATCH 4/5] Restore env check --- settings.gradle | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/settings.gradle b/settings.gradle index 39151891d..019ff256c 100644 --- a/settings.gradle +++ b/settings.gradle @@ -47,11 +47,11 @@ pluginManagement { rootProject.name = 'autodispose-root' if (System.getenv("ANDROID_HOME") != null) { + include ':android:autodispose-android' + include ':android:autodispose-androidx-lifecycle' + include ':android:autodispose-androidx-lifecycle-test' + include ':sample' } -include ':android:autodispose-android' -include ':android:autodispose-androidx-lifecycle' -include ':android:autodispose-androidx-lifecycle-test' -include ':sample' include ':autodispose' include ':autodispose-interop:coroutines' include ':autodispose-lifecycle' From 3d3604e952d16e901628776b09b7dfdc43d802d1 Mon Sep 17 00:00:00 2001 From: Zac Sweers Date: Sun, 8 Aug 2021 21:49:16 -0400 Subject: [PATCH 5/5] spotless --- .../lifecycle/AndroidLifecycleScopeProviderTest.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/android/autodispose-androidx-lifecycle/src/androidTest/java/autodispose2/androidx/lifecycle/AndroidLifecycleScopeProviderTest.java b/android/autodispose-androidx-lifecycle/src/androidTest/java/autodispose2/androidx/lifecycle/AndroidLifecycleScopeProviderTest.java index cea738894..af5eab1e1 100644 --- a/android/autodispose-androidx-lifecycle/src/androidTest/java/autodispose2/androidx/lifecycle/AndroidLifecycleScopeProviderTest.java +++ b/android/autodispose-androidx-lifecycle/src/androidTest/java/autodispose2/androidx/lifecycle/AndroidLifecycleScopeProviderTest.java @@ -20,10 +20,10 @@ import android.util.Log; import androidx.lifecycle.Lifecycle; +import androidx.lifecycle.testing.TestLifecycleOwner; import androidx.test.annotation.UiThreadTest; import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.platform.app.InstrumentationRegistry; -import androidx.lifecycle.testing.TestLifecycleOwner; import autodispose2.lifecycle.LifecycleEndedException; import autodispose2.test.RecordingObserver; import io.reactivex.rxjava3.disposables.Disposable; @@ -270,8 +270,7 @@ public void observable_offMainThread_shouldFail() { // Spin it up TestLifecycleOwner lifecycle = new TestLifecycleOwner(Lifecycle.State.INITIALIZED); InstrumentationRegistry.getInstrumentation() - .runOnMainSync( - () -> lifecycle.setCurrentState(Lifecycle.State.RESUMED)); + .runOnMainSync(() -> lifecycle.setCurrentState(Lifecycle.State.RESUMED)); subject.to(autoDisposable(AndroidLifecycleScopeProvider.from(lifecycle))).subscribe(o); o.takeSubscribe(); Throwable t = o.takeError();