@Test public void onUnsubscribePostsOffMainThread() throws InterruptedException { ShadowLooper.pauseMainLooper(); final CountDownLatch latch = new CountDownLatch(1); final AtomicBoolean called = new AtomicBoolean(); new Thread(new Runnable() { @Override public void run() { new AbstractDisposeOnMainThread() { @Override protected void onDispose() { called.set(true); } }.dispose(); latch.countDown(); } }).start(); assertTrue(latch.await(1, SECONDS)); assertFalse(called.get()); ShadowLooper.runMainLooperOneTask(); assertTrue(called.get()); }
@Override public final void dispose() { if (unsubscribed.compareAndSet(false, true)) { if (Looper.myLooper() == Looper.getMainLooper()) { onDispose(); } else { AndroidInteropSchedulers.mainThread().scheduleDirect(new Runnable() { @Override public void run() { onDispose(); } }); } } }
AbstractDisposeOnMainThread implements Disposable { @Override public final void dispose() { if (unsubscribed.compareAndSet(false, true)) { if (Looper.myLooper() == Looper.getMainLooper()) { onDispose(); } else { AndroidInteropSchedulers.mainThread().scheduleDirect(new Runnable() { @Override public void run() { onDispose(); } }); } } } }
AbstractDisposeOnMainThread implements Disposable { @Override public final void dispose() { if (unsubscribed.compareAndSet(false, true)) { if (Looper.myLooper() == Looper.getMainLooper()) { onDispose(); } else { AndroidInteropSchedulers.mainThread().scheduleDirect(new Runnable() { @Override public void run() { onDispose(); } }); } } }  }
AbstractDisposeOnMainThread implements Disposable { @Override public final void dispose() { if (unsubscribed.compareAndSet(false, true)) { if (Looper.myLooper() == Looper.getMainLooper()) { onDispose(); } else { AndroidInteropSchedulers.mainThread().scheduleDirect(new Runnable() { @Override public void run() { onDispose(); } }); } } }  static void verifyMainThread(); @Override final boolean isDisposed(); @Override final void dispose(); }
AbstractDisposeOnMainThread implements Disposable { @Override public final void dispose() { if (unsubscribed.compareAndSet(false, true)) { if (Looper.myLooper() == Looper.getMainLooper()) { onDispose(); } else { AndroidInteropSchedulers.mainThread().scheduleDirect(new Runnable() { @Override public void run() { onDispose(); } }); } } }  static void verifyMainThread(); @Override final boolean isDisposed(); @Override final void dispose();  }