ActorHandlerBase extends Handler { @Override public void handleMessage(@NonNull Message msg) { switch (msg.what) { case MSG_TRANSACTION: handleTransaction((Transaction) msg.obj, msg.arg1); break; case MSG_POISON_PILL: handlePoisonPill(msg.arg1); break; } } ActorHandlerBase(Looper looper, long stopDelay, @Nullable PowerManager.WakeLock wakeLock); @Override void handleMessage(@NonNull Message msg);  }
@Test public void handleMessage_callMethod_methodTransaction() { ActorHandlerBase handler = createHandler(ActorHandlerBase.NO_DELAY); Transaction transaction = Transaction.<Object>obtain(mImpl, mMessage, mFailureHandler); android.os.Message message = android.os.Message.obtain(handler, ActorHandlerBase.MSG_TRANSACTION, 10, 0, transaction); handler.handleMessage(message); Mockito.verify(mMessage).invoke(mImpl); Assert.assertNull(transaction.message); }
@Test public void handleMessage_noPoisonPill_methodTransaction() { ActorHandlerBase handler = createHandler(ActorHandlerBase.NO_DELAY); Transaction transaction = Transaction.<Object>obtain(mImpl, mMessage, mFailureHandler); android.os.Message message = android.os.Message.obtain(handler, ActorHandlerBase.MSG_TRANSACTION, 10, 0, transaction); handler.handleMessage(message); Mockito.verify(handler, Mockito.never()).removeMessages(ActorHandlerBase.MSG_POISON_PILL); Mockito.verify(handler, Mockito.never()).sendMessage(Mockito.<android.os.Message>any()); }
@SuppressWarnings("ThrowableNotThrown") @Test public void handleMessage_callFailureHandle_methodTransactionThrows() { ActorHandlerBase handler = createHandler(ActorHandlerBase.NO_DELAY); Transaction transaction = Transaction.<Object>obtain(mImpl, mMessage, mFailureHandler); android.os.Message message = android.os.Message.obtain(handler, ActorHandlerBase.MSG_TRANSACTION, 10, 0, transaction); RuntimeException exception = new RuntimeException(); Mockito.doThrow(exception).when(mMessage).invoke(Mockito.any()); Mockito.doReturn(mException).when(mMessage).exception(); handler.handleMessage(message); Mockito.verify(mFailureHandler).onUncaughtException(mImpl, mMessage, mException); Mockito.verify(mException).initCause(exception); Assert.assertNull(transaction.message); }
@Test public void handleMessage_takePoisonPill_methodTransaction() { ActorHandlerBase handler = createHandler(60000); Transaction transaction = Transaction.<Object>obtain(mImpl, mMessage, mFailureHandler); android.os.Message message = android.os.Message.obtain(handler, ActorHandlerBase.MSG_TRANSACTION, 10, 0, transaction); handler.handleMessage(message); Mockito.verify(handler).removeMessages(ActorHandlerBase.MSG_POISON_PILL); Mockito.verify(handler).sendMessageDelayed(mMessageCaptor.capture(), mTimeoutCaptor.capture()); Assert.assertEquals(60000L, (long) mTimeoutCaptor.getValue()); android.os.Message poisonPill = mMessageCaptor.getValue(); Assert.assertNotNull(poisonPill); Assert.assertEquals(ActorHandlerBase.MSG_POISON_PILL, poisonPill.what); Assert.assertEquals(10, poisonPill.arg1); }
@Test public void handleMessage_takePoisonPill_methodTransactionThrows() { ActorHandlerBase handler = createHandler(60000); Transaction transaction = Transaction.<Object>obtain(mImpl, mMessage, mFailureHandler); android.os.Message message = android.os.Message.obtain(handler, ActorHandlerBase.MSG_TRANSACTION, 10, 0, transaction); RuntimeException exception = new RuntimeException(); Mockito.doThrow(exception).when(mMessage).invoke(Mockito.any()); Mockito.doReturn(mException).when(mMessage).exception(); handler.handleMessage(message); Mockito.verify(handler).removeMessages(ActorHandlerBase.MSG_POISON_PILL); Mockito.verify(handler).sendMessageDelayed(mMessageCaptor.capture(), mTimeoutCaptor.capture()); Assert.assertEquals(60000L, (long) mTimeoutCaptor.getValue()); android.os.Message poisonPill = mMessageCaptor.getValue(); Assert.assertNotNull(poisonPill); Assert.assertEquals(ActorHandlerBase.MSG_POISON_PILL, poisonPill.what); Assert.assertEquals(10, poisonPill.arg1); }
@Test public void handleMessage_wakeLockAcquireRelease_methodTransaction() { PowerManager pm = (PowerManager) ShadowApplication.getInstance().getApplicationContext().getSystemService(Context.POWER_SERVICE); final PowerManager.WakeLock wakeLock = Shadows.shadowOf(pm).newWakeLock(0, "test-wakelock"); ActorHandlerBase handler = createHandler(ActorHandlerBase.NO_DELAY, wakeLock); Transaction transaction = Transaction.<Object>obtain(mImpl, mMessage, mFailureHandler); android.os.Message message = android.os.Message.obtain(handler, ActorHandlerBase.MSG_TRANSACTION, 10, 0, transaction); handler.handleMessage(message); Assert.assertSame(wakeLock, ShadowPowerManager.getLatestWakeLock()); Assert.assertEquals(false, wakeLock.isHeld()); }