Java Code Examples for io.reactivex.Observable.test()

The following are Jave code examples for showing how to use test() of the io.reactivex.Observable class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: lacomida   File: CartManagerTest.java   Source Code and License Vote up 6 votes
@Test
public void testCartEmitter() {
	CartManager cartManager = Injector.get(CartManager.class);

	Observable<Integer> observable = cartManager.cartChangedObserver();
	TestObserver<Integer> test = observable.test();

	Plate pizza = new Plate().setId(1L);

	cartManager.addPlate(pizza);
	cartManager.addPlate(new Plate());
	cartManager.removePlate(pizza);

	test.assertSubscribed()
			.assertValueAt(0, integer -> integer.equals(1))
			.assertValueAt(1, integer -> integer.equals(2))
			.assertValueAt(2, integer -> integer.equals(1));
}
 
Example 2
Project: RxJava2-weather-example   File: MockApiTest.java   Source Code and License Vote up 5 votes
@Test
public void testForecast() throws Exception {
    server.enqueue(new MockResponse().setBody(responseExample));

    DarkSkyApi darkSkyApi = Utils.buildDarkSkyInstance(server.url("/"));

    Observable<JsonObject> forecast = darkSkyApi.forecast("", -1, -1);
    TestObserver<JsonObject> test = forecast.test();
    test.assertNoErrors();
    JsonObject expected = new Gson().fromJson(responseExample, JsonObject.class);
    test.assertResult(expected);
}
 
Example 3
Project: RxBroadcastReceiver   File: RxBroadcastReceiverTest.java   Source Code and License Vote up 5 votes
@Test
public void shouldNotReceiveBroadcastAfterDisposed() {
    //GIVEN
    final Observable<Intent> observable = RxBroadcastReceivers.fromIntentFilter(application, testIntentFilter);

    //WHEN
    final TestObserver<Intent> observer = observable.test(true);
    application.sendBroadcast(testIntent1);
    application.sendBroadcast(testIntent2);
    application.sendBroadcast(testIntent3);
    //THEN
    observer.assertValueCount(0);
    observer.assertEmpty();
}
 
Example 4
Project: RxBroadcastReceiver   File: RxBroadcastReceiverTest.java   Source Code and License Vote up 5 votes
@Test
public void shouldReturnErrorWhenSubscribeOnNonLooperThread() {
    //GIVEN
    final Observable<Intent> observable = RxBroadcastReceivers.fromIntentFilter(application, testIntentFilter)
            .subscribeOn(Schedulers.newThread());

    //WHEN
    final TestObserver<Intent> observer = observable.test();

    //THEN
    observer.awaitTerminalEvent();
    observer.assertTerminated();
}
 
Example 5
Project: RxBroadcastReceiver   File: RxBroadcastReceiverTest.java   Source Code and License Vote up 4 votes
@Test
public void shouldReceiveBroadcastOnLooperThread() throws InterruptedException {
    //GIVEN
    final Semaphore beforeLooperPrepare = new Semaphore(0);
    final Semaphore afterLooperPrepare = new Semaphore(0);
    //due to robolectic dirty hack to subscription to be run really on TestHandlerThread due to robolectric
    final HandlerThread handlerThread = new HandlerThread("TestHandlerThread") {
        @Override
        protected void onLooperPrepared() {
            try {
                beforeLooperPrepare.acquire();
            } catch (final InterruptedException e) {
                e.printStackTrace();
            }
            shadowOf(Looper.myLooper()).idle();
            afterLooperPrepare.release();
        }
    };

    handlerThread.start();
    final Observable<Intent> observable = RxBroadcastReceivers.fromIntentFilter(application, testIntentFilter)
            .subscribeOn(AndroidSchedulers.from(handlerThread.getLooper()));

    //WHEN
    final TestObserver<Intent> observer = observable.test();

    beforeLooperPrepare.release();
    afterLooperPrepare.acquire();

    assertEquals(0, shadowOf(handlerThread.getLooper()).getScheduler().size());
    application.sendBroadcast(testIntent1);
    assertEquals(1, shadowOf(handlerThread.getLooper()).getScheduler().size());
    application.sendBroadcast(testIntent2);
    assertEquals(2, shadowOf(handlerThread.getLooper()).getScheduler().size());
    application.sendBroadcast(testIntent3);
    assertEquals(3, shadowOf(handlerThread.getLooper()).getScheduler().size());

    shadowOf(handlerThread.getLooper()).idle();
    //THEN

    observer.assertValueCount(3);
    observer.assertValues(testIntent1, testIntent2, testIntent3);
}