From 46bef4b2c2b79310af86668114c8e69766c444eb Mon Sep 17 00:00:00 2001 From: Zac Sweers Date: Wed, 30 Aug 2017 19:42:05 -0700 Subject: [PATCH] Add tests! --- .../build.gradle | 4 + .../archcomponents/ViewScopeProviderTest.java | 143 ++++++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 autodispose-android-archcomponents/src/androidTest/java/com/uber/autodispose/android/archcomponents/ViewScopeProviderTest.java diff --git a/autodispose-android-archcomponents/build.gradle b/autodispose-android-archcomponents/build.gradle index b8e010678..e61bb2e32 100644 --- a/autodispose-android-archcomponents/build.gradle +++ b/autodispose-android-archcomponents/build.gradle @@ -54,6 +54,10 @@ dependencies { provided deps.misc.errorProneAnnotations provided project(':autodispose-provided') errorprone deps.build.errorProne + + androidTestCompile project(':test-utils') + androidTestCompile deps.test.androidRunner + androidTestCompile deps.test.androidRules } // Disable for now until we're ready to release this diff --git a/autodispose-android-archcomponents/src/androidTest/java/com/uber/autodispose/android/archcomponents/ViewScopeProviderTest.java b/autodispose-android-archcomponents/src/androidTest/java/com/uber/autodispose/android/archcomponents/ViewScopeProviderTest.java new file mode 100644 index 000000000..af1a54b1e --- /dev/null +++ b/autodispose-android-archcomponents/src/androidTest/java/com/uber/autodispose/android/archcomponents/ViewScopeProviderTest.java @@ -0,0 +1,143 @@ +/* + * Copyright (c) 2017. Uber Technologies + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.uber.autodispose.android.archcomponents; + +import android.arch.lifecycle.Lifecycle; +import android.arch.lifecycle.LifecycleRegistry; +import android.arch.lifecycle.LifecycleRegistryOwner; +import android.support.test.annotation.UiThreadTest; +import android.support.test.rule.UiThreadTestRule; +import android.support.test.runner.AndroidJUnit4; +import android.util.Log; +import com.uber.autodispose.LifecycleEndedException; +import com.uber.autodispose.LifecycleNotStartedException; +import com.uber.autodispose.ObservableScoper; +import com.uber.autodispose.test.RecordingObserver; +import io.reactivex.disposables.Disposable; +import io.reactivex.subjects.PublishSubject; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +import static com.google.common.truth.Truth.assertThat; + +@RunWith(AndroidJUnit4.class) public final class ViewScopeProviderTest { + + private static final RecordingObserver.Logger LOGGER = new RecordingObserver.Logger() { + @Override public void log(String message) { + Log.d(ViewScopeProviderTest.class.getSimpleName(), message); + } + }; + + @Rule public UiThreadTestRule uiThreadTestRule = new UiThreadTestRule(); + + @Test @UiThreadTest public void observable_normal() { + final RecordingObserver o = new RecordingObserver<>(LOGGER); + final PublishSubject subject = PublishSubject.create(); + + // Spin it up + TestLifecycleOwnerScopeProvider lifecycle = new TestLifecycleOwnerScopeProvider(); + lifecycle.emit(Lifecycle.Event.ON_CREATE); + lifecycle.emit(Lifecycle.Event.ON_START); + lifecycle.emit(Lifecycle.Event.ON_RESUME); + subject.to(new ObservableScoper(LifecycleOwnerScopeProvider.from(lifecycle))) + .subscribe(o); + + Disposable d = o.takeSubscribe(); + o.assertNoMoreEvents(); // No initial value. + + subject.onNext(0); + assertThat(o.takeNext()).isEqualTo(0); + + 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); + + subject.onNext(2); + o.assertNoMoreEvents(); + + d.dispose(); + } + + @Test public void observable_offMainThread_shouldFail() { + RecordingObserver o = new RecordingObserver<>(LOGGER); + PublishSubject subject = PublishSubject.create(); + + // Spin it up + TestLifecycleOwnerScopeProvider lifecycle = new TestLifecycleOwnerScopeProvider(); + lifecycle.emit(Lifecycle.Event.ON_CREATE); + lifecycle.emit(Lifecycle.Event.ON_START); + lifecycle.emit(Lifecycle.Event.ON_RESUME); + subject.to(new ObservableScoper(LifecycleOwnerScopeProvider.from(lifecycle))) + .subscribe(o); + + Disposable d = o.takeSubscribe(); + Throwable t = o.takeError(); + assertThat(t).isInstanceOf(IllegalStateException.class); + assertThat(t.getMessage()).contains("main thread"); + o.assertNoMoreEvents(); + assertThat(d.isDisposed()).isTrue(); + } + + @Test @UiThreadTest public void observable_offBeforeAttach_shouldFail() { + final RecordingObserver o = new RecordingObserver<>(LOGGER); + final PublishSubject subject = PublishSubject.create(); + + UninitializedLifecycleOwner owner = new UninitializedLifecycleOwner(); + subject.to(new ObservableScoper(LifecycleOwnerScopeProvider.from(owner))) + .subscribe(o); + + Disposable d = o.takeSubscribe(); + Throwable t = o.takeError(); + assertThat(t).isInstanceOf(LifecycleNotStartedException.class); + o.assertNoMoreEvents(); + assertThat(d.isDisposed()).isTrue(); + } + + @Test @UiThreadTest public void observable_offAfterDetach_shouldFail() { + final RecordingObserver o = new RecordingObserver<>(LOGGER); + final PublishSubject subject = PublishSubject.create(); + + TestLifecycleOwnerScopeProvider lifecycle = new TestLifecycleOwnerScopeProvider(); + 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); + subject.to(new ObservableScoper(LifecycleOwnerScopeProvider.from(lifecycle))) + .subscribe(o); + + Disposable d = o.takeSubscribe(); + Throwable t = o.takeError(); + assertThat(t).isInstanceOf(LifecycleEndedException.class); + o.assertNoMoreEvents(); + assertThat(d.isDisposed()).isTrue(); + } + + private static class UninitializedLifecycleOwner implements LifecycleRegistryOwner { + + LifecycleRegistry registry = new LifecycleRegistry(this); + + @Override public LifecycleRegistry getLifecycle() { + return registry; + } + } +}