ActorService extends Service { @Override public IBinder onBind(Intent intent) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O || ACTION_DIRECT_START.equals(intent.getAction())) { return getLegacyBinder(); } return getBinder(); } @SuppressWarnings({"unused", "WeakerAccess"}) protected  ActorService(@NonNull String name); @SuppressWarnings({"unused", "WeakerAccess"}) protected  ActorService(@NonNull String name, long stopDelay); @SuppressWarnings({"unused", "WeakerAccess"}) protected  ActorService(@NonNull String name, long stopDelay, boolean useWakeLocks); @Override IBinder onBind(Intent intent); @Override void onCreate(); @Override int onStartCommand(Intent intent, int flags, int startId); @Override void onDestroy();  }
@Test public void onBind_hasValidLocalInterface_always() throws Exception { IBinder binder = createService().onBind(new Intent()); Assert.assertEquals(ActorService.LOCAL_SENDER_INTERFACE, binder.getInterfaceDescriptor()); Assert.assertNotNull(binder.queryLocalInterface(ActorService.LOCAL_SENDER_INTERFACE)); }
@SuppressWarnings("unchecked") @Test public void deliver_deliverMessage_always() throws Exception { ActorService service = createService(); IBinder binder = service.onBind(new Intent()); ActorService.RemoteMessageSender sender = (ActorService.RemoteMessageSender) binder.queryLocalInterface(ActorService.LOCAL_SENDER_INTERFACE); sender.deliver(Transaction.<Object>obtain(mImpl, mMessage, mFailureHandler)); ShadowLooper shadowLooper = Shadows.shadowOf(service.mThread.getLooper()); shadowLooper.runOneTask(); Mockito.verify(mMessage).invoke(mImpl); Assert.assertEquals(false, Shadows.shadowOf(service).isStoppedBySelf()); }
@SuppressWarnings("unchecked") @Test public void deliver_useWakeLocks_always() throws Exception { ActorService service = createService(true); IBinder binder = service.onBind(new Intent()); ActorService.RemoteMessageSender sender = (ActorService.RemoteMessageSender) binder.queryLocalInterface(ActorService.LOCAL_SENDER_INTERFACE); sender.deliver(Transaction.<Object>obtain(mImpl, mMessage, mFailureHandler)); ShadowLooper shadowLooper = Shadows.shadowOf(service.mThread.getLooper()); shadowLooper.runOneTask(); PowerManager.WakeLock wakeLock = ShadowPowerManager.getLatestWakeLock(); Assert.assertNotNull(wakeLock); Assert.assertEquals(false, wakeLock.isHeld()); }
@SuppressWarnings("unchecked") @Test public void deliver_stopService_always() throws Exception { ActorService service = createService(); IBinder binder = service.onBind(new Intent()); ActorService.RemoteMessageSender sender = (ActorService.RemoteMessageSender) binder.queryLocalInterface(ActorService.LOCAL_SENDER_INTERFACE); sender.deliver(Transaction.<Object>obtain(mImpl, mMessage, mFailureHandler)); ShadowLooper shadowLooper = Shadows.shadowOf(service.mThread.getLooper()); shadowLooper.runOneTask(); Assert.assertEquals(false, Shadows.shadowOf(service).isStoppedBySelf()); shadowLooper.runOneTask(); Assert.assertEquals(true, Shadows.shadowOf(service).isStoppedBySelf()); }