### Question:
RemoteCall { public void cancel() { mFuture.complete(RemoteResult.FAILED_CANCELLED); } RemoteCall(RemoteCallable<IBackupCallback> callable, long timeoutMs);  RemoteCall(boolean cancelled, RemoteCallable<IBackupCallback> callable, long timeoutMs); static RemoteResult execute(RemoteCallable<IBackupCallback> callable, long timeoutMs); @WorkerThread RemoteResult call(); void cancel();  }### Answer:
@Test public void testCall_whenImmediateCallableAndTimeOut0AndCancelIsCalledBeforeCall_returnsCancel() throws Exception { RemoteCall remoteCall = new RemoteCall(IMMEDIATE_CALLABLE, 0); remoteCall.cancel(); RemoteResult result = runInWorkerThread(remoteCall::call); assertThat(result).isEqualTo(RemoteResult.FAILED_CANCELLED); }
@Test public void testCall_whenTimesOutBeforeCancelIsCalled_returnsTimeOut() throws Exception { ConditionVariable scheduled = new ConditionVariable(false); RemoteCall remoteCall = new RemoteCall(callback -> scheduled.open(), 500); Future<RemoteResult> result = runInWorkerThreadAsync(remoteCall::call); scheduled.block(); runToEndOfTasks(Looper.getMainLooper()); remoteCall.cancel(); assertThat(result.get()).isEqualTo(RemoteResult.FAILED_TIMED_OUT); }
@Test public void testCall_whenCallbackIsCalledBeforeCancel_returnsResult() throws Exception { CompletableFuture<IBackupCallback> callbackFuture = new CompletableFuture<>(); RemoteCall remoteCall = new RemoteCall(callbackFuture::complete, 1000); Future<RemoteResult> result = runInWorkerThreadAsync(remoteCall::call); IBackupCallback callback = callbackFuture.get(); callback.operationComplete(3); remoteCall.cancel(); assertThat(result.get()).isEqualTo(RemoteResult.of(3)); }
@Test public void testCall_whenCancelIsCalledBeforeCallbackButAfterCall_returnsCancel() throws Exception { CompletableFuture<IBackupCallback> callbackFuture = new CompletableFuture<>(); RemoteCall remoteCall = new RemoteCall(callbackFuture::complete, 1000); Future<RemoteResult> result = runInWorkerThreadAsync(remoteCall::call); IBackupCallback callback = callbackFuture.get(); remoteCall.cancel(); callback.operationComplete(3); assertThat(result.get()).isEqualTo(RemoteResult.FAILED_CANCELLED); }
@Test public void testCall_whenCancelIsCalledBeforeTimeOutButAfterCall_returnsCancel() throws Exception { ConditionVariable scheduled = new ConditionVariable(false); RemoteCall remoteCall = new RemoteCall(callback -> scheduled.open(), 1000); Future<RemoteResult> result = runInWorkerThreadAsync(remoteCall::call); scheduled.block(); remoteCall.cancel(); runToEndOfTasks(Looper.getMainLooper()); assertThat(result.get()).isEqualTo(RemoteResult.FAILED_CANCELLED); }