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..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 autodispose2.androidx.lifecycle.test.TestLifecycleOwner; import autodispose2.lifecycle.LifecycleEndedException; import autodispose2.test.RecordingObserver; import io.reactivex.rxjava3.disposables.Disposable; @@ -44,11 +44,9 @@ 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.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.emit(Lifecycle.Event.ON_PAUSE); - lifecycle.emit(Lifecycle.Event.ON_STOP); - lifecycle.emit(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 = TestLifecycleOwner.create(); - lifecycle.emit(Lifecycle.Event.ON_CREATE); + TestLifecycleOwner lifecycle = new TestLifecycleOwner(Lifecycle.State.CREATED); subject.to(autoDisposable(AndroidLifecycleScopeProvider.from(lifecycle))).subscribe(o); - lifecycle.emit(Lifecycle.Event.ON_START); - lifecycle.emit(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.emit(Lifecycle.Event.ON_PAUSE); - lifecycle.emit(Lifecycle.Event.ON_STOP); - lifecycle.emit(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 = TestLifecycleOwner.create(); - lifecycle.emit(Lifecycle.Event.ON_CREATE); - lifecycle.emit(Lifecycle.Event.ON_START); + TestLifecycleOwner lifecycle = new TestLifecycleOwner(Lifecycle.State.STARTED); subject.to(autoDisposable(AndroidLifecycleScopeProvider.from(lifecycle))).subscribe(o); - lifecycle.emit(Lifecycle.Event.ON_RESUME); + lifecycle.setCurrentState(Lifecycle.State.RESUMED); o.takeSubscribe(); o.assertNoMoreEvents(); // No initial value. @@ -119,14 +109,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.setCurrentState(Lifecycle.State.DESTROYED); } @Test @@ -136,10 +126,7 @@ 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.RESUMED); subject.to(autoDisposable(AndroidLifecycleScopeProvider.from(lifecycle))).subscribe(o); o.takeSubscribe(); @@ -152,12 +139,11 @@ 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.setCurrentState(Lifecycle.State.DESTROYED); } @Test @@ -167,13 +153,11 @@ 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.CREATED); 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.setCurrentState(Lifecycle.State.RESUMED); o.takeSubscribe(); o.assertNoMoreEvents(); // No initial value. @@ -185,12 +169,11 @@ 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.setCurrentState(Lifecycle.State.DESTROYED); } @Test @@ -200,10 +183,7 @@ 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.RESUMED); subject .to( autoDisposable( @@ -219,14 +199,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.setCurrentState(Lifecycle.State.DESTROYED); subject.onNext(3); o.assertNoMoreEvents(); } @@ -237,11 +217,8 @@ 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.RESUMED); + lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE); subject.to(autoDisposable(AndroidLifecycleScopeProvider.from(lifecycle))).subscribe(o); o.takeSubscribe(); @@ -250,12 +227,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 +243,9 @@ 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.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 // This simulates subscribing in fragments' onDestroyView, where we want the subscription to // still dispose properly in onDestroy. @@ -283,7 +257,7 @@ public void observable_offAfterOnDestroyView_shouldStopOnDestroy() { assertThat(o.takeNext()).isEqualTo(2); // We should stop here - lifecycle.emit(Lifecycle.Event.ON_DESTROY); + lifecycle.setCurrentState(Lifecycle.State.DESTROYED); subject.onNext(3); o.assertNoMoreEvents(); } @@ -294,14 +268,9 @@ 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); - }); + .runOnMainSync(() -> lifecycle.setCurrentState(Lifecycle.State.RESUMED)); subject.to(autoDisposable(AndroidLifecycleScopeProvider.from(lifecycle))).subscribe(o); o.takeSubscribe(); Throwable t = o.takeError(); @@ -316,13 +285,8 @@ 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.RESUMED); + lifecycle.setCurrentState(Lifecycle.State.DESTROYED); 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}" ] ]