Skip to content

Commit

Permalink
Deprecate TestLifecycleOwner (#433)
Browse files Browse the repository at this point in the history
* Rebase and update

* Simplify creation states

* Clean up destroy states

* Restore env check

* spotless
  • Loading branch information
ZacSweers authored Aug 9, 2021
1 parent fb8ba7b commit e5b4c32
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 74 deletions.
2 changes: 1 addition & 1 deletion android/autodispose-androidx-lifecycle-test/Module.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Module autodispose-androidx-lifecycle-test

Test helpers for `autodispose-androidx-lifecycle` support.
DEPRECATED
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
1 change: 1 addition & 0 deletions android/autodispose-androidx-lifecycle/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -44,11 +44,9 @@ public void observable_beforeCreate() {
final PublishSubject<Integer> 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.
Expand All @@ -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();
Expand All @@ -74,11 +70,9 @@ public void observable_createDestroy() {
final PublishSubject<Integer> 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.
Expand All @@ -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();
Expand All @@ -104,11 +96,9 @@ public void observable_startStop() {
final PublishSubject<Integer> 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.
Expand All @@ -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
Expand All @@ -136,10 +126,7 @@ public void observable_resumePause() {
final PublishSubject<Integer> 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();
Expand All @@ -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
Expand All @@ -167,13 +153,11 @@ public void observable_createPause() {
final PublishSubject<Integer> 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.
Expand All @@ -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
Expand All @@ -200,10 +183,7 @@ public void observable_resumeDestroy() {
final PublishSubject<Integer> 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(
Expand All @@ -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();
}
Expand All @@ -237,11 +217,8 @@ public void observable_offAfterPause_shouldStopOnStop() {
final RecordingObserver<Integer> o = new RecordingObserver<>(LOGGER);
final PublishSubject<Integer> 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();
Expand All @@ -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();
}
Expand All @@ -266,12 +243,9 @@ public void observable_offAfterOnDestroyView_shouldStopOnDestroy() {
final RecordingObserver<Integer> o = new RecordingObserver<>(LOGGER);
final PublishSubject<Integer> 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.
Expand All @@ -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();
}
Expand All @@ -294,14 +268,9 @@ public void observable_offMainThread_shouldFail() {
PublishSubject<Integer> 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();
Expand All @@ -316,13 +285,8 @@ public void observable_offAfterDestroy_shouldFail() {
final RecordingObserver<Integer> o = new RecordingObserver<>(LOGGER);
final PublishSubject<Integer> 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();
Expand Down
3 changes: 1 addition & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
1 change: 1 addition & 0 deletions gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
]
]
Expand Down

0 comments on commit e5b4c32

Please sign in to comment.