@Test public void autoDispose_outsideScope_withProviderAndPlugin_shouldFailWithWrappedExp() { AutoDisposePlugins.setOutsideScopeHandler(e -> { // Wrap in an IllegalStateException so we can verify this is the exception we see on the // other side throw new IllegalStateException(e); }); ScopeProvider provider = outsideScopeProvider(); TestObserver<Integer> o = PublishSubject.<Integer>create().as(autoDisposable(provider)) .test(); o.assertNoValues(); o.assertError(throwable -> throwable instanceof IllegalStateException && throwable.getCause() instanceof OutsideScopeException); } }
@Test public void autoDispose_withProviderAndPlugin_withoutStarting_shouldFailWithExp() { AutoDisposePlugins.setOutsideScopeHandler(e -> { // Wrap in an IllegalStateException so we can verify this is the exception we see on the // other side throw new IllegalStateException(e); }); BehaviorSubject<Integer> lifecycle = BehaviorSubject.create(); LifecycleScopeProvider<Integer> provider = makeLifecycleProvider(lifecycle); PublishSubject<Integer> source = PublishSubject.create(); TestObserver<Integer> o = source.as(autoDisposable(provider)) .test(); o.assertNoValues(); o.assertError(throwable -> throwable instanceof IllegalStateException && throwable.getCause() instanceof OutsideScopeException); } }
@Test public void unbound_shouldStillPassValues() { PublishSubject<Integer> s = PublishSubject.create(); TestObserver<Integer> o = s.as(autoDisposable(ScopeProvider.UNBOUND)) .test(); s.onNext(1); o.assertValue(1); o.dispose(); }
@Test public void autoDispose_outsideScope_withProviderAndNoOpPlugin_shouldFailSilently() { AutoDisposePlugins.setOutsideScopeHandler(e -> { }); ScopeProvider provider = outsideScopeProvider(); PublishSubject<Integer> source = PublishSubject.create(); TestObserver<Integer> o = source.as(autoDisposable(provider)) .test(); assertThat(source.hasObservers()).isFalse(); o.assertNoValues(); o.assertNoErrors(); }
@Test public void autoDispose_withScopeProviderCompleted_shouldNotReportDoubleSubscriptions() { TestObserver<Object> o = PublishSubject.create() .as(autoDisposable(ScopeProvider.UNBOUND)) .test(); o.assertNoValues(); o.assertNoErrors(); rule.assertNoErrors(); }
@Test public void autoDispose_withProviderAndNoOpPlugin_withoutStarting_shouldFailSilently() { AutoDisposePlugins.setOutsideScopeHandler(e -> { }); BehaviorSubject<Integer> lifecycle = BehaviorSubject.create(); LifecycleScopeProvider<Integer> provider = makeLifecycleProvider(lifecycle); PublishSubject<Integer> source = PublishSubject.create(); TestObserver<Integer> o = source.as(autoDisposable(provider)) .test(); assertThat(source.hasObservers()).isFalse(); assertThat(lifecycle.hasObservers()).isFalse(); o.assertNoValues(); o.assertNoErrors(); }
@Test public void autoDispose_withMaybe_interrupted() { RecordingObserver<Integer> o = new RecordingObserver<>(LOGGER); PublishSubject<Integer> source = PublishSubject.create(); CompletableSubject scope = CompletableSubject.create(); source.as(autoDisposable(scope)) .subscribe(o); o.takeSubscribe(); assertThat(source.hasObservers()).isTrue(); assertThat(scope.hasObservers()).isTrue(); source.onNext(1); assertThat(o.takeNext()).isEqualTo(1); scope.onComplete(); source.onNext(2); o.assertNoMoreEvents(); assertThat(source.hasObservers()).isFalse(); assertThat(scope.hasObservers()).isFalse(); }
@Test public void bindLifecycle_normalTermination_unsubscribe() { lifecycleProvider.emitCreate(); RecordingObserver<Integer> o = new RecordingObserver<>(LOGGER); PublishSubject<Integer> source = PublishSubject.create(); source.as(autoDisposable(RxLifecycleInterop.from(lifecycleProvider))) .subscribe(o); o.takeSubscribe(); assertThat(source.hasObservers()).isTrue(); source.onNext(1); assertThat(o.takeNext()).isEqualTo(1); lifecycleProvider.emitDestroy(); source.onNext(2); o.assertNoMoreEvents(); assertThat(source.hasObservers()).isFalse(); }
@Test public void bindLifecycle_normalTermination_unsubscribe() { lifecycleProvider.emitCreate(); RecordingObserver<Integer> o = new RecordingObserver<>(LOGGER); PublishSubject<Integer> source = PublishSubject.create(); source.as(autoDisposable(RxLifecycleInterop.from(lifecycleProvider))) .subscribe(o); o.takeSubscribe(); assertThat(source.hasObservers()).isTrue(); source.onNext(1); assertThat(o.takeNext()).isEqualTo(1); lifecycleProvider.emitDestroy(); source.onNext(2); o.assertNoMoreEvents(); assertThat(source.hasObservers()).isFalse(); }
@Test public void autoDispose_withProvider() { RecordingObserver<Integer> o = new RecordingObserver<>(LOGGER); PublishSubject<Integer> source = PublishSubject.create(); CompletableSubject scope = CompletableSubject.create(); ScopeProvider provider = TestUtil.makeProvider(scope); source.as(autoDisposable(provider)) .subscribe(o); o.takeSubscribe(); assertThat(source.hasObservers()).isTrue(); assertThat(scope.hasObservers()).isTrue(); source.onNext(1); assertThat(o.takeNext()).isEqualTo(1); source.onNext(2); assertThat(source.hasObservers()).isTrue(); assertThat(scope.hasObservers()).isTrue(); assertThat(o.takeNext()).isEqualTo(2); scope.onComplete(); source.onNext(3); o.assertNoMoreEvents(); assertThat(source.hasObservers()).isFalse(); assertThat(scope.hasObservers()).isFalse(); }
@Test public void bindUntilEvent_interruptedTermination_unsubscribe() { lifecycleProvider.emitCreate(); RecordingObserver<Integer> o = new RecordingObserver<>(LOGGER); PublishSubject<Integer> source = PublishSubject.create(); source.as(autoDisposable( RxLifecycleInterop.from(lifecycleProvider, TestLifecycleProvider.Event.DESTROY))) .subscribe(o); o.takeSubscribe(); assertThat(source.hasObservers()).isTrue(); source.onNext(1); assertThat(o.takeNext()).isEqualTo(1); lifecycleProvider.emitDestroy(); source.onNext(2); o.assertNoMoreEvents(); assertThat(source.hasObservers()).isFalse(); } }
@Test public void bindLifecycle_outsideLifecycleBound_unsubscribe() { lifecycleProvider.emitCreate(); RecordingObserver<Integer> o = new RecordingObserver<>(LOGGER); PublishSubject<Integer> source = PublishSubject.create(); lifecycleProvider.emitDestroy(); source.as(autoDisposable(RxLifecycleInterop.from(lifecycleProvider))) .subscribe(o); o.takeSubscribe(); source.onNext(2); o.assertNoMoreEvents(); assertThat(source.hasObservers()).isFalse(); // Because RxLifecycle // treats OutsideLifecycleException as terminal event. }
@Test public void bindLifecycle_outsideLifecycleBound_unsubscribe() { lifecycleProvider.emitCreate(); RecordingObserver<Integer> o = new RecordingObserver<>(LOGGER); PublishSubject<Integer> source = PublishSubject.create(); lifecycleProvider.emitDestroy(); source.as(autoDisposable(RxLifecycleInterop.from(lifecycleProvider))) .subscribe(o); o.takeSubscribe(); source.onNext(2); o.assertNoMoreEvents(); assertThat(source.hasObservers()).isFalse(); // Because RxLifecycle // treats OutsideLifecycleException as terminal event. }
@Test public void autoDispose_withMaybe_normal() { TestObserver<Integer> o = new TestObserver<>(); PublishSubject<Integer> source = PublishSubject.create(); CompletableSubject scope = CompletableSubject.create(); Disposable d = source.as(autoDisposable(scope)) .subscribeWith(o); o.assertSubscribed(); assertThat(source.hasObservers()).isTrue(); assertThat(scope.hasObservers()).isTrue(); source.onNext(1); o.assertValue(1); source.onNext(2); source.onComplete(); o.assertValues(1, 2); o.assertComplete(); assertThat(d.isDisposed()).isFalse(); // Because it completed normally, was not disposed. assertThat(source.hasObservers()).isFalse(); assertThat(scope.hasObservers()).isFalse(); }
@Test public void bindUntilEvent_interruptedTermination_unsubscribe() { lifecycleProvider.emitCreate(); RecordingObserver<Integer> o = new RecordingObserver<>(LOGGER); PublishSubject<Integer> source = PublishSubject.create(); source.as(autoDisposable( RxLifecycleInterop.from(lifecycleProvider, TestLifecycleProvider.Event.DESTROY))) .subscribe(o); o.takeSubscribe(); assertThat(source.hasObservers()).isTrue(); source.onNext(1); assertThat(o.takeNext()).isEqualTo(1); lifecycleProvider.emitDestroy(); source.onNext(2); o.assertNoMoreEvents(); assertThat(source.hasObservers()).isFalse(); } }
@Test public void bindLifecycle_normalTermination_completeTheStream() { lifecycleProvider.emitCreate(); TestObserver<Integer> o = new TestObserver<>(); PublishSubject<Integer> source = PublishSubject.create(); Disposable d = source.as(autoDisposable(RxLifecycleInterop.from(lifecycleProvider))) .subscribeWith(o); o.assertSubscribed(); assertThat(source.hasObservers()).isTrue(); source.onNext(1); o.assertValue(1); source.onNext(2); source.onComplete(); o.assertValues(1, 2); o.assertComplete(); assertThat(d.isDisposed()).isFalse(); // Because it completed normally, was not disposed. assertThat(source.hasObservers()).isFalse(); }
@Test public void bindLifecycle_normalTermination_completeTheStream() { lifecycleProvider.emitCreate(); TestObserver<Integer> o = new TestObserver<>(); PublishSubject<Integer> source = PublishSubject.create(); Disposable d = source.as(autoDisposable(RxLifecycleInterop.from(lifecycleProvider))) .subscribeWith(o); o.assertSubscribed(); assertThat(source.hasObservers()).isTrue(); source.onNext(1); o.assertValue(1); source.onNext(2); source.onComplete(); o.assertValues(1, 2); o.assertComplete(); assertThat(d.isDisposed()).isFalse(); // Because it completed normally, was not disposed. assertThat(source.hasObservers()).isFalse(); }
@Test public void bindUntilEvent_normalTermination_completeTheStream() { lifecycleProvider.emitCreate(); TestObserver<Integer> o = new TestObserver<>(); PublishSubject<Integer> source = PublishSubject.create(); Disposable d = source.as(autoDisposable( RxLifecycleInterop.from(lifecycleProvider, TestLifecycleProvider.Event.DESTROY))) .subscribeWith(o); o.assertSubscribed(); assertThat(source.hasObservers()).isTrue(); source.onNext(1); o.assertValue(1); source.onNext(2); source.onComplete(); o.assertValues(1, 2); o.assertComplete(); assertThat(d.isDisposed()).isFalse(); // Because it completed normally, was not disposed. assertThat(source.hasObservers()).isFalse(); }
@Test public void bindUntilEvent_normalTermination_completeTheStream() { lifecycleProvider.emitCreate(); TestObserver<Integer> o = new TestObserver<>(); PublishSubject<Integer> source = PublishSubject.create(); Disposable d = source.as(autoDisposable( RxLifecycleInterop.from(lifecycleProvider, TestLifecycleProvider.Event.DESTROY))) .subscribeWith(o); o.assertSubscribed(); assertThat(source.hasObservers()).isTrue(); source.onNext(1); o.assertValue(1); source.onNext(2); source.onComplete(); o.assertValues(1, 2); o.assertComplete(); assertThat(d.isDisposed()).isFalse(); // Because it completed normally, was not disposed. assertThat(source.hasObservers()).isFalse(); }
@Test public void autoDispose_withProviderAndNoOpPlugin_afterEnding_shouldFailSilently() { AutoDisposePlugins.setOutsideScopeHandler(e -> { // Noop }); BehaviorSubject<Integer> lifecycle = BehaviorSubject.createDefault(0); lifecycle.onNext(1); lifecycle.onNext(2); lifecycle.onNext(3); LifecycleScopeProvider<Integer> provider = makeLifecycleProvider(lifecycle); PublishSubject<Integer> source = PublishSubject.create(); TestObserver<Integer> o = source.as(autoDisposable(provider)) .test(); assertThat(source.hasObservers()).isFalse(); assertThat(lifecycle.hasObservers()).isFalse(); o.assertNoValues(); o.assertNoErrors(); }