Java Code Examples for rx.observers.TestSubscriber#assertValues()

The following examples show how to use rx.observers.TestSubscriber#assertValues() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RxJavaTimeFilteringOperatorsIntegrationTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenTimedObservable_whenSampling_thenOnlySampleItemsAreEmitted() {

    TestScheduler testScheduler = new TestScheduler();

    Observable<Integer> timedObservable =
            Observable.just(1, 2, 3, 4, 5, 6)
                    .zipWith(
                            Observable.interval(0, 1, TimeUnit.SECONDS, testScheduler),
                            (item, time) -> item
                    );

    TestSubscriber<Integer> subscriber = new TestSubscriber();

    Observable<Integer> sampledObservable =
            timedObservable.sample(2500L, TimeUnit.MILLISECONDS, testScheduler);

    sampledObservable.subscribe(subscriber);

    testScheduler.advanceTimeBy(7, TimeUnit.SECONDS);

    subscriber.assertCompleted();
    subscriber.assertNoErrors();
    subscriber.assertValues(3, 5, 6);
}
 
Example 2
Source File: TransformersTest.java    From rxjava-extras with Apache License 2.0 6 votes vote down vote up
@Test
public void testDelayWithPlayRate() {
    TestSubscriber<Object> ts = TestSubscriber.create();
    TestScheduler scheduler = new TestScheduler();
    Func1<Integer, Long> time = new Func1<Integer, Long>() {

        @Override
        public Long call(Integer t) {
            return (long) t;
        }
    };
    Observable //
            .just(1, 2, 3) //
            .compose(Transformers.delay(time, Functions.constant0(0.001), 0, scheduler)) //
            .subscribe(ts);
    ts.assertNoValues();
    scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
    ts.assertValue(1);
    scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
    ts.assertValues(1, 2);
    scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
    ts.assertValues(1, 2, 3);
    ts.assertCompleted();
}
 
Example 3
Source File: RxWifiManagerTest.java    From rx-receivers with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("ResourceType") @Test //
public void networkStateChanges() throws IllegalAccessException, InstantiationException {
  Application application = RuntimeEnvironment.application;

  TestSubscriber<NetworkStateChangedEvent> o = new TestSubscriber<>();
  RxWifiManager.networkStateChanges(application).subscribe(o);
  o.assertValues();

  NetworkInfo networkInfo1 = NetworkInfo.class.newInstance();
  WifiInfo wifiInfo1 = WifiInfo.class.newInstance();
  Intent intent1 = new Intent(NETWORK_STATE_CHANGED_ACTION) //
      .putExtra(WifiManager.EXTRA_NETWORK_INFO, networkInfo1)
      .putExtra(WifiManager.EXTRA_BSSID, "foo")
      .putExtra(WifiManager.EXTRA_WIFI_INFO, wifiInfo1);
  application.sendBroadcast(intent1);
  NetworkStateChangedEvent event1 =
      NetworkStateChangedEvent.create(networkInfo1, "foo", wifiInfo1);
  o.assertValues(event1);

  NetworkInfo networkInfo2 = NetworkInfo.class.newInstance();
  Intent intent2 = new Intent(NETWORK_STATE_CHANGED_ACTION) //
      .putExtra(WifiManager.EXTRA_NETWORK_INFO, networkInfo2);
  application.sendBroadcast(intent2);
  NetworkStateChangedEvent event2 = NetworkStateChangedEvent.create(networkInfo2, null, null);
  o.assertValues(event1, event2);
}
 
Example 4
Source File: OperatorBufferPredicateBoundaryTest.java    From rxjava-extras with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void bufferUntilWith1() {
    TestSubscriber<List<Integer>> ts = new TestSubscriber<List<Integer>>();
    
    Observable.range(1, 10)
    .compose(Transformers.bufferUntil(UtilityFunctions.<Integer>alwaysTrue()))
    .subscribe(ts);
    
    ts.assertValues(
            Arrays.asList(1),
            Arrays.asList(2),
            Arrays.asList(3),
            Arrays.asList(4),
            Arrays.asList(5),
            Arrays.asList(6),
            Arrays.asList(7),
            Arrays.asList(8),
            Arrays.asList(9),
            Arrays.asList(10)
        );
    ts.assertNoErrors();
    ts.assertCompleted();
}
 
Example 5
Source File: RxTelephonyManagerTest.java    From rx-receivers with Apache License 2.0 6 votes vote down vote up
@Test public void phoneStateChanges() {
  Application application = RuntimeEnvironment.application;

  TestSubscriber<PhoneStateChangedEvent> o = new TestSubscriber<>();
  RxTelephonyManager.phoneStateChanges(application).subscribe(o);
  o.assertValues();

  Intent intent1 = new Intent(ACTION_PHONE_STATE_CHANGED) //
      .putExtra(EXTRA_STATE, EXTRA_INCOMING_NUMBER)
      .putExtra(EXTRA_INCOMING_NUMBER, "123-456-7890");
  application.sendBroadcast(intent1);
  PhoneStateChangedEvent event1 =
      PhoneStateChangedEvent.create(EXTRA_INCOMING_NUMBER, "123-456-7890");
  o.assertValues(event1);

  Intent intent2 = new Intent(ACTION_PHONE_STATE_CHANGED).putExtra(EXTRA_STATE, EXTRA_STATE_IDLE);
  application.sendBroadcast(intent2);
  PhoneStateChangedEvent event2 = PhoneStateChangedEvent.create(EXTRA_STATE_IDLE, null);
  o.assertValues(event1, event2);
}
 
Example 6
Source File: TransformerOnTerminateResumeTest.java    From rxjava-extras with Apache License 2.0 6 votes vote down vote up
@Test
public void mainErrors() {
    
    Transformer<Integer, Integer> op = new TransformerOnTerminateResume<Integer>(new Func1<Throwable, Observable<Integer>>() {
        @Override
        public Observable<Integer> call(Throwable e) {
            return Observable.just(11);
        }
    }, Observable.just(12));
    
    TestSubscriber<Integer> ts = TestSubscriber.create();
    
    Observable.range(1, 10).concatWith(Observable.<Integer>error(new IOException()))
    .compose(op)
    .subscribe(ts);
    
    ts.assertValues(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
    ts.assertNoErrors();
    ts.assertCompleted();
}
 
Example 7
Source File: RxJavaTimeFilteringOperatorsIntegrationTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenTimedObservable_whenThrottlingLast_thenThrottleLastItemsAreEmitted() {

    TestScheduler testScheduler = new TestScheduler();

    Observable<Integer> timedObservable =
            Observable.just(1, 2, 3, 4, 5, 6)
                    .zipWith(
                            Observable.interval(0, 1, TimeUnit.SECONDS, testScheduler),
                            (item, time) -> item
                    );

    TestSubscriber<Integer> subscriber = new TestSubscriber();

    Observable<Integer> filteredObservable = timedObservable.throttleLast(3100L, TimeUnit.MILLISECONDS, testScheduler);

    filteredObservable.subscribe(subscriber);

    testScheduler.advanceTimeBy(7, TimeUnit.SECONDS);

    subscriber.assertCompleted();
    subscriber.assertNoErrors();
    subscriber.assertValues(4, 6);
}
 
Example 8
Source File: SortedMergeTest.java    From hawkular-metrics with Apache License 2.0 5 votes vote down vote up
@Test
public void testErrorInMiddle() {
    Observable<Integer> o1 = Observable.just(1, 3, 5, 7);
    Observable<Integer> o2 = Observable.just(2).concatWith(Observable.<Integer>error(new RuntimeException()));

    TestSubscriber<Integer> ts = new TestSubscriber<>();
    SortedMerge.create(Arrays.asList(o1, o2)).subscribe(ts);

    ts.assertError(RuntimeException.class);
    ts.assertNotCompleted();
    ts.assertValues(1, 2);
}
 
Example 9
Source File: RxJavaSkipOperatorsIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenObservable_whenFilteringDistinctUntilChanged_thenOnlyDistinctConsecutiveItemsAreEmitted() {

    Observable<Integer> sourceObservable = Observable.just(1, 1, 2, 2, 1, 3, 3, 1);
    TestSubscriber<Integer> subscriber = new TestSubscriber();

    Observable<Integer> distinctObservable = sourceObservable.distinctUntilChanged();

    distinctObservable.subscribe(subscriber);

    subscriber.assertCompleted();
    subscriber.assertNoErrors();
    subscriber.assertValueCount(5);
    subscriber.assertValues(1, 2, 1, 3, 1);
}
 
Example 10
Source File: RxPermissionsTest.java    From rx-android-permissions with Apache License 2.0 5 votes vote down vote up
public void testObserveAndRequestPermissions() {

        TestSubscriber<Boolean> observeSubscriber = new TestSubscriber<>();
        TestSubscriber<Boolean> requestSubscriber = new TestSubscriber<>();

        RxPermissionsUnderTest permissions = new RxPermissionsUnderTest(getContext(), false);
        MockPermissionsRequester requester = new MockPermissionsRequester(false);

        permissions.observe(
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(observeSubscriber);

        permissions.request(requester,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(requestSubscriber);

        requester.granted = true;
        requester.request();

        observeSubscriber.assertValues(false, false, false, false, true);
        observeSubscriber.assertNoErrors();
        observeSubscriber.assertNotCompleted();

        requestSubscriber.assertValues(false, false, true);
        requestSubscriber.assertNoErrors();
        requestSubscriber.assertCompleted();
    }
 
Example 11
Source File: RxJavaSkipOperatorsIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenObservable_whenSkippingWhile_thenFirstItemsAreSkipped() {

    Observable<Integer> sourceObservable = Observable.just(1, 2, 3, 4, 5, 4, 3, 2, 1);
    TestSubscriber<Integer> subscriber = new TestSubscriber();

    Observable<Integer> filteredObservable = sourceObservable.skipWhile(i -> i < 4);

    filteredObservable.subscribe(subscriber);

    subscriber.assertCompleted();
    subscriber.assertNoErrors();
    subscriber.assertValueCount(6);
    subscriber.assertValues(4, 5, 4, 3, 2, 1);
}
 
Example 12
Source File: ObservableQueueTest.java    From Jockey with Apache License 2.0 5 votes vote down vote up
@Test
public void testConsistencyOfEnqueueBeforeSubscribe() {
    TestSubscriber<String> subscriber = new TestSubscriber<>();

    mSubject.enqueue("Hello world!");
    mSubject.enqueue("My hands are typing words!");

    Observable<String> observable = mSubject.toObservable();

    mSubject.enqueue("HAAAAAANDS!");
    observable.subscribe(subscriber);

    subscriber.assertNoTerminalEvent();
    subscriber.assertValues("Hello world!", "My hands are typing words!", "HAAAAAANDS!");
}
 
Example 13
Source File: OrderedMergeTest.java    From rxjava-extras with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testAsymmetricMerge() {
	Observable<Integer> o1 = Observable.just(1, 3, 5, 7);
	Observable<Integer> o2 = Observable.just(2, 4);

	TestSubscriber<Integer> ts = TestSubscriber.create();
	OrderedMerge.create(Arrays.asList(o1, o2)).subscribe(ts);

	ts.assertNoErrors();
	ts.assertCompleted();
	ts.assertValues(1, 2, 3, 4, 5, 7);
}
 
Example 14
Source File: TransformersTest.java    From rxjava-extras with Apache License 2.0 5 votes vote down vote up
@Test
// @org.junit.Ignore
public void testDelayFinalUnsubscribeForRefCount() {
    TestScheduler s = new TestScheduler();
    final AtomicInteger count = new AtomicInteger();
    Observable<Long> o = Observable //
            .interval(1, TimeUnit.SECONDS, s) //
            .doOnSubscribe(Actions.increment0(count)) //
            .publish() //
            .refCount() //
            .compose(Transformers.<Long> delayFinalUnsubscribe(2500, TimeUnit.MILLISECONDS, s));
    {
        TestSubscriber<Long> ts1 = TestSubscriber.create();
        o.subscribe(ts1);
        assertEquals(1, count.get());
        s.advanceTimeBy(1, TimeUnit.SECONDS);
        ts1.assertValues(0L);
        ts1.unsubscribe();
    }
    s.advanceTimeBy(1, TimeUnit.SECONDS);
    {
        TestSubscriber<Long> ts2 = TestSubscriber.create();
        o.subscribe(ts2);
        ts2.assertNoValues();
        s.advanceTimeBy(1, TimeUnit.SECONDS);
        ts2.assertValues(2L);
        ts2.unsubscribe();
    }
    assertEquals(1, count.get());
    s.advanceTimeBy(2500, TimeUnit.MILLISECONDS);
    {
        TestSubscriber<Long> ts3 = TestSubscriber.create();
        s.advanceTimeBy(1, TimeUnit.SECONDS);
        o.subscribe(ts3);
        assertEquals(2, count.get());
        s.advanceTimeBy(1, TimeUnit.SECONDS);
        ts3.assertValues(0L);
    }

}
 
Example 15
Source File: ObservableCombineUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenTwoObservables_whenMerged_shouldEmitCombinedResults() {
    TestSubscriber<String> testSubscriber = new TestSubscriber<>();

    Observable.merge(
      Observable.from(asList("Hello", "World")),
      Observable.from(asList("I love", "RxJava"))
    ).subscribe(testSubscriber);

    testSubscriber.assertValues("Hello", "World", "I love", "RxJava");
}
 
Example 16
Source File: IntegratorTest.java    From mantis with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldRespectMinimumValue() {

    Observable<Double> result = data.lift(new Integrator(0, 0.0, 10.0));
    TestSubscriber<Double> testSubscriber = new TestSubscriber<>();
    result.subscribe(testSubscriber);
    testSubscriber.assertCompleted();
    testSubscriber.assertValues(1.0, 0.0, 0.0, 0.0);
}
 
Example 17
Source File: RxJavaSkipOperatorsIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenRangeObservable_whenSkipping_thenFirstFourItemsAreSkipped() {

    Observable<Integer> sourceObservable = Observable.range(1, 10);
    TestSubscriber<Integer> subscriber = new TestSubscriber();

    Observable<Integer> filteredObservable = sourceObservable.skip(4);

    filteredObservable.subscribe(subscriber);

    subscriber.assertCompleted();
    subscriber.assertNoErrors();
    subscriber.assertValueCount(6);
    subscriber.assertValues(5, 6, 7, 8, 9, 10);
}
 
Example 18
Source File: SortedMergeTest.java    From hawkular-metrics with Apache License 2.0 5 votes vote down vote up
@Test
public void testAsymmetricMerge() {
    Observable<Integer> o1 = Observable.just(1, 3, 5, 7);
    Observable<Integer> o2 = Observable.just(2, 4);

    TestSubscriber<Integer> ts = new TestSubscriber<>();
    SortedMerge.create(Arrays.asList(o1, o2)).subscribe(ts);

    ts.assertNoErrors();
    ts.assertCompleted();
    ts.assertValues(1, 2, 3, 4, 5, 7);
}
 
Example 19
Source File: OperatorBufferPredicateBoundaryTest.java    From rxjava-extras with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void bufferWhileBackpressure() {
    TestSubscriber<List<Integer>> ts = TestSubscriber.create(0);
    
    Observable.range(1, RxRingBuffer.SIZE * 4)
    .compose(Transformers.bufferWhile(new Func1<Integer, Boolean>() {
        @Override
        public Boolean call(Integer v) {
            return (v % 3) == 0;
        }
    })).subscribe(ts);
    
    ts.assertNoValues();
    ts.assertNoErrors();
    ts.assertNotCompleted();
    
    ts.requestMore(1);
    
    ts.assertValue(Arrays.asList(1, 2));

    ts.requestMore(1);
    ts.assertValues(
            Arrays.asList(1, 2),
            Arrays.asList(3, 4, 5)
    );
    ts.assertNoErrors();
    ts.assertNotCompleted();
    
    ts.requestMore(1);
    ts.assertValues(
            Arrays.asList(1, 2),
            Arrays.asList(3, 4, 5),
            Arrays.asList(6, 7, 8)
    );
    ts.assertNoErrors();
    ts.assertNotCompleted();
    
    ts.requestMore(Long.MAX_VALUE);
    
    ts.assertValueCount((RxRingBuffer.SIZE * 4) / 3 + 1);
    ts.assertNoErrors();
    ts.assertCompleted();
}
 
Example 20
Source File: RxPresenterTest.java    From FlowGeek with GNU General Public License v2.0 4 votes vote down vote up
private void assertValues(List<Integer> values, TestSubscriber<Integer> subscriber) {
    subscriber.assertValues(values.toArray(new Integer[values.size()]));
}