CancelableOperation implements Cancelable, Runnable { @NonNull public CancelableOperation addOnCancel(@NonNull Cancelable cancelable) { synchronized (this) { if (isCancelled()) { cancelable.cancel(); } if (!isDone()) { cancelables.add(cancelable); } } return this; } CancelableOperation();  CancelableOperation(@Nullable Looper looper); @Override final boolean cancel(); @Override final boolean cancel(boolean mayInterruptIfRunning); @Override final void run(); @Override final boolean isDone(); @Override final boolean isCancelled(); @NonNull CancelableOperation addOnRun(@NonNull Runnable runnable); @NonNull CancelableOperation addOnCancel(@NonNull Cancelable cancelable); @NonNull Handler getHandler();  }
@Test public void testAddOnCancel() { TestOperation onCancel = new TestOperation(Looper.myLooper()); operation.addOnCancel(onCancel); operation.cancel(); looper.runToEndOfTasks(); assertTrue(onCancel.onCancelCalled); }