ActorHandlerBase extends Handler { boolean sendTransaction(@NonNull Transaction transaction) { final int id; synchronized (this) { if (mLastId == -1) { return false; } id = ++mLastId; } return sendMessage(obtainMessage(MSG_TRANSACTION, id, 0, transaction)); } ActorHandlerBase(Looper looper, long stopDelay, @Nullable PowerManager.WakeLock wakeLock); @Override void handleMessage(@NonNull Message msg);  }
@Test public void sendTransaction_sendMessage_validHandler() { ActorHandlerBase handler = createHandler(ActorHandlerBase.NO_DELAY); Transaction transaction = Transaction.<Object>obtain(mImpl, mMessage, mFailureHandler); boolean result = handler.sendTransaction(transaction); Assert.assertTrue(result); Mockito.verify(handler).sendMessage(mMessageCaptor.capture()); android.os.Message message = mMessageCaptor.getValue(); Assert.assertNotNull(message); Assert.assertEquals(ActorHandlerBase.MSG_TRANSACTION, message.what); Assert.assertSame(transaction, message.obj); }
@Test public void sendTransaction_increaseLastId_validHandler() { ActorHandlerBase handler = createHandler(ActorHandlerBase.NO_DELAY); Assert.assertTrue(handler.sendTransaction(Transaction.<Object>obtain(mImpl, mMessage, mFailureHandler))); Assert.assertTrue(handler.sendTransaction(Transaction.<Object>obtain(mImpl, mMessage, mFailureHandler))); Mockito.verify(handler, Mockito.times(2)).sendMessage(mMessageCaptor.capture()); List<android.os.Message> messages = mMessageCaptor.getAllValues(); Assert.assertEquals(2, messages.size()); Assert.assertNotEquals(messages.get(0).arg1, messages.get(1).arg1); }