AppObservable { public static <T> Observable<T> bindFragment(Fragment fragment, Observable<T> source) { Assertions.assertUiThread(); return source.observeOn(mainThread()) .lift(new OperatorConditionalBinding<T, Fragment>(fragment, FRAGMENT_VALIDATOR)); } private  AppObservable(); static Observable<T> bindActivity(Activity activity, Observable<T> source); static Observable<T> bindFragment(Fragment fragment, Observable<T> source); static Observable<T> bindSupportFragment(android.support.v4.app.Fragment fragment, Observable<T> source);  }
@Test public void itSupportsNativeFragments() { AppObservable.bindFragment(fragment, Observable.just("success")).subscribe(new TestObserver<String>(observer)); verify(observer).onNext("success"); verify(observer).onCompleted(); }
@Test(expected = AssertionError.class) public void itThrowsIfObserverCallsFromFragmentFromBackgroundThread() throws Throwable { final Future<Object> future = Executors.newSingleThreadExecutor().submit(new Callable<Object>() { @Override public Object call() throws Error { AppObservable.bindFragment(fragment, Observable.empty()); return null; } }); try { future.get(1, TimeUnit.SECONDS); } catch (ExecutionException e) { throw e.getCause(); } }
@Test public void bindFragmentToSourceFromDifferentThread() throws InterruptedException { CountDownLatch done = new CountDownLatch(1); AppObservable.bindFragment(fragment, TestUtil.atBackgroundThread(done)).subscribe(new TestObserver<String>(observer)); done.await(); ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); verify(observer).onNext(TestUtil.STRING_EXPECTATION); verify(observer).onCompleted(); }