Java Code Examples for io.reactivex.observers.TestObserver#assertValueAt()

The following examples show how to use io.reactivex.observers.TestObserver#assertValueAt() . 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: Modern_Testing.java    From Reactive-Programming-With-Java-9 with MIT License 6 votes vote down vote up
@Test
public void test_just_new() {
	Observable<Integer> observable = Observable.just(12, 34, 6);
	TestObserver<Integer> testObserver = new TestObserver<>();

	observable.subscribe(testObserver);

	List<Integer> list = new ArrayList();
	testObserver.assertComplete();
	testObserver.assertResult(12, 34, 6);
	testObserver.assertValueCount(3);
	testObserver.assertNoErrors();
	testObserver.assertValueAt(2, (value) -> {
		// TODO Auto-generated method stub
		return value == 34;
	});

}
 
Example 2
Source File: GodEyeHelperTest.java    From AndroidGodEye with Apache License 2.0 5 votes vote down vote up
@Test
public void onPageLoadedSuccessForActivity() {
    try {
        GodEye.instance().uninstall();
        GodEye.instance().install(GodEyeConfig.noneConfigBuilder().withPageloadConfig(new PageloadConfig()).build());
        ActivityController<Test1Activity> activityController = Robolectric.buildActivity(Test1Activity.class).create().start().resume();
        Test1Activity activity = activityController.get();
        TestObserver testObserver = GodEye.instance().<Pageload, PageLifecycleEventInfo>moduleObservable(GodEye.ModuleName.PAGELOAD).test();
        GodEyeHelper.onPageLoaded(activity);
        activityController.pause().stop().destroy();
        Shadows.shadowOf(ThreadUtil.obtainHandler("godeye-pageload").getLooper()).getScheduler().advanceToNextPostedRunnable();

        List<TestPageEvent> testPageEvents = new ArrayList<>();
        testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_CREATE, 1));
        testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_START, 2));
        testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_DRAW, 3));
        testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_RESUME, 4));
        testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_LOAD, 5));
        testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_PAUSE, 6));
        testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_STOP, 7));
        testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_DESTROY, 8));
        testObserver.assertValueCount(8);
        for (int i = 0; i < 8; i++) {
            int finalI = i;
            testObserver.assertValueAt(i, new Predicate<PageLifecycleEventInfo>() {
                @Override
                public boolean test(PageLifecycleEventInfo o) throws Exception {
                    return testPageEvents.get(finalI).pageHashCode == o.pageInfo.pageHashCode
                            && testPageEvents.get(finalI).allEventSize == o.allEvents.size()
                            && testPageEvents.get(finalI).lifecycleEvent.equals(o.currentEvent.lifecycleEvent);
                }
            });
        }
    } catch (UninstallException e) {
        fail();
    }
}
 
Example 3
Source File: BehaviorSubjectCombineLatestTest.java    From akarnokd-misc with Apache License 2.0 5 votes vote down vote up
@Test
public void test() {
    Subject<Integer> intSource = BehaviorSubject.createDefault(1);

    Subject<List<Observable<Integer>>> mainSubject = 
            BehaviorSubject.createDefault(singletonList(intSource));

    TestObserver<List<Integer>> testObserver = 
            mainSubject.flatMap(observables ->
            Observable.combineLatest(observables, this::castObjectsToInts)
                    )
            .test();

    List<Observable<Integer>> newValue = new ArrayList<>();
    newValue.add(intSource); // same value as before
    newValue.add(Observable.just(2)); // add another value to this list.

    mainSubject.onNext(newValue);

    // intSource was already '1', but this is just to 'update' it.
    intSource.onNext(1); // COMMENT OUT THIS LINE

    testObserver.assertValueAt(0, singletonList(1));
    testObserver.assertValueAt(1, Arrays.asList(1, 2));
    testObserver.assertValueAt(2, Arrays.asList(1)); // COMMENT OUT THIS LINE
    testObserver.assertValueAt(3, Arrays.asList(1, 2)); // COMMENT OUT THIS LINE
    testObserver.assertValueCount(4); // REPLACE 3 WITH 2
}
 
Example 4
Source File: AggregatedDataCallMockIntegrationShould.java    From dhis2-android-sdk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void emit_progress() {

    TestObserver<D2Progress> testObserver = d2.aggregatedModule().data().download().test();
    testObserver.assertValueCount(4);

    testObserver.assertValueAt(0, v -> v.lastCall().equals("SystemInfo"));
    testObserver.assertValueAt(1, v -> v.lastCall().equals("DataValue"));
    testObserver.assertValueAt(2, v -> v.lastCall().equals("DataSetCompleteRegistration"));
    testObserver.assertValueAt(3, v -> v.lastCall().equals("DataApproval"));


    testObserver.dispose();
}
 
Example 5
Source File: GodEyeHelperTest.java    From AndroidGodEye with Apache License 2.0 4 votes vote down vote up
/**
 * ACTIVITYTest2Activity, pageHashCode=638030090,ON_CREATE
 * FRAGMENTTest1Fragment, pageHashCode=436993154,ON_ATTACH
 * FRAGMENTTest1Fragment, pageHashCode=436993154,ON_CREATE
 * ACTIVITYTest2Activity, pageHashCode=638030090,ON_START
 * FRAGMENTTest1Fragment, pageHashCode=436993154,ON_VIEW_CREATE
 * FRAGMENTTest1Fragment, pageHashCode=436993154,ON_START
 * ACTIVITYTest2Activity, pageHashCode=638030090,ON_DRAW
 * FRAGMENTTest1Fragment, pageHashCode=436993154,ON_DRAW
 * ACTIVITYTest2Activity, pageHashCode=638030090,ON_RESUME
 * FRAGMENTTest1Fragment, pageHashCode=436993154,ON_RESUME
 * ACTIVITYTest2Activity, pageHashCode=638030090,ON_LOAD
 * FRAGMENTTest1Fragment, pageHashCode=436993154,ON_LOAD
 * ACTIVITYTest2Activity, pageHashCode=638030090,ON_PAUSE
 * FRAGMENTTest1Fragment, pageHashCode=436993154,ON_PAUSE
 * ACTIVITYTest2Activity, pageHashCode=638030090,ON_STOP
 * FRAGMENTTest1Fragment, pageHashCode=436993154,ON_STOP
 * ACTIVITYTest2Activity, pageHashCode=638030090,ON_DESTROY
 * FRAGMENTTest1Fragment, pageHashCode=436993154,ON_VIEW_DESTROY
 * FRAGMENTTest1Fragment, pageHashCode=436993154,ON_DESTROY
 * FRAGMENTTest1Fragment, pageHashCode=436993154,ON_DETACH
 */
@Test
public void onPageLoadedSuccessWithFragment() {
    try {
        GodEye.instance().uninstall();
        GodEye.instance().install(GodEyeConfig.noneConfigBuilder().withPageloadConfig(new PageloadConfig()).build());
        ActivityController<Test2Activity> activityController = Robolectric.buildActivity(Test2Activity.class).create().start().resume();
        Test2Activity activity = activityController.get();
        GodEyeHelper.onPageLoaded(activity);
        TestObserver testObserver = GodEye.instance().<Pageload, PageLifecycleEventInfo>moduleObservable(GodEye.ModuleName.PAGELOAD).test();
        Test1Fragment fragment = activity.getTest1Fragment();
        GodEyeHelper.onPageLoaded(fragment);
        activityController.pause().stop().destroy();
        Shadows.shadowOf(ThreadUtil.obtainHandler("godeye-pageload").getLooper()).getScheduler().advanceToNextPostedRunnable();
        List<TestPageEvent> testPageEvents = new ArrayList<>();
        testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_CREATE, 1));
        testPageEvents.add(new TestPageEvent(fragment.hashCode(), FragmentLifecycleEvent.ON_ATTACH, 1));
        testPageEvents.add(new TestPageEvent(fragment.hashCode(), FragmentLifecycleEvent.ON_CREATE, 2));
        testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_START, 2));
        testPageEvents.add(new TestPageEvent(fragment.hashCode(), FragmentLifecycleEvent.ON_VIEW_CREATE, 3));
        testPageEvents.add(new TestPageEvent(fragment.hashCode(), FragmentLifecycleEvent.ON_START, 4));
        testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_DRAW, 3));
        testPageEvents.add(new TestPageEvent(fragment.hashCode(), FragmentLifecycleEvent.ON_DRAW, 5));
        testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_RESUME, 4));
        testPageEvents.add(new TestPageEvent(fragment.hashCode(), FragmentLifecycleEvent.ON_RESUME, 6));
        testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_LOAD, 5));
        testPageEvents.add(new TestPageEvent(fragment.hashCode(), FragmentLifecycleEvent.ON_LOAD, 7));
        testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_PAUSE, 6));
        testPageEvents.add(new TestPageEvent(fragment.hashCode(), FragmentLifecycleEvent.ON_PAUSE, 8));
        testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_STOP, 7));
        testPageEvents.add(new TestPageEvent(fragment.hashCode(), FragmentLifecycleEvent.ON_STOP, 9));
        testPageEvents.add(new TestPageEvent(activity.hashCode(), ActivityLifecycleEvent.ON_DESTROY, 8));
        testPageEvents.add(new TestPageEvent(fragment.hashCode(), FragmentLifecycleEvent.ON_VIEW_DESTROY, 10));
        testPageEvents.add(new TestPageEvent(fragment.hashCode(), FragmentLifecycleEvent.ON_DESTROY, 11));
        testPageEvents.add(new TestPageEvent(fragment.hashCode(), FragmentLifecycleEvent.ON_DETACH, 12));
        testObserver.assertValueCount(20);
        for (int i = 0; i < 20; i++) {
            int finalI = i;
            testObserver.assertValueAt(i, new Predicate<PageLifecycleEventInfo>() {
                @Override
                public boolean test(PageLifecycleEventInfo o) throws Exception {
                    return testPageEvents.get(finalI).pageHashCode == o.pageInfo.pageHashCode
                            && testPageEvents.get(finalI).allEventSize == o.allEvents.size()
                            && testPageEvents.get(finalI).lifecycleEvent.equals(o.currentEvent.lifecycleEvent);
                }
            });
        }
    } catch (UninstallException e) {
        fail();
    }
}