-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
...c/androidTest/java/com/uber/autodispose/android/archcomponents/ViewScopeProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Integer> o = new RecordingObserver<>(LOGGER); | ||
final PublishSubject<Integer> 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<Integer>(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<Integer> o = new RecordingObserver<>(LOGGER); | ||
PublishSubject<Integer> 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<Integer>(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<Integer> o = new RecordingObserver<>(LOGGER); | ||
final PublishSubject<Integer> subject = PublishSubject.create(); | ||
|
||
UninitializedLifecycleOwner owner = new UninitializedLifecycleOwner(); | ||
subject.to(new ObservableScoper<Integer>(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<Integer> o = new RecordingObserver<>(LOGGER); | ||
final PublishSubject<Integer> 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<Integer>(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; | ||
} | ||
} | ||
} |