ServiceActorThread implements ActorThread { @NonNull @Override public <T> ActorRef<T> bind(@NonNull Class<T> cls, @NonNull T impl) { ServiceMessageSenderProxy<T> postman = new ServiceMessageSenderProxy<>(mContext, mFailureHandler, mService, mJobId, impl); T instance = mProxyFactory.newProxy(cls, postman); return new ActorRefImpl<>(instance); } ServiceActorThread(@NonNull Context context, @NonNull ProxyFactory proxyFactory,
                                     @NonNull FailureHandler failureHandler, @NonNull Class<? extends ActorService> service,
                                     int jobId); @NonNull @Override ActorRef<T> bind(@NonNull Class<T> cls, @NonNull T impl);  }
@Test public void deliverMessageTest() { ShadowApplication application = ShadowApplication.getInstance(); ServiceActorThread thread = new ServiceActorThread(application.getApplicationContext(), mProxyFactory, mFailureHandler, ActorService.class, 1); ActorRef<Runnable> ref = thread.bind(Runnable.class, mActorImpl); Assert.assertSame(mProxy, ref.tell()); ArgumentCaptor<MessageSender> postmanCaptor = ArgumentCaptor.forClass(MessageSender.class); Mockito.verify(mProxyFactory).newProxy(Mockito.<Class>any(), postmanCaptor.capture()); MessageSender postman = postmanCaptor.getValue(); Assert.assertNotNull(postman); Mockito.doReturn(true).when(mMessageSender).deliver(Mockito.any(Transaction.class)); Message message = Mockito.mock(Message.class); postman.deliver(message); Intent intent = application.getNextStartedService(); Assert.assertNotNull(intent); Assert.assertEquals(new ComponentName(application.getApplicationContext(), ActorService.class), intent.getComponent()); ArgumentCaptor<Transaction> captor = ArgumentCaptor.forClass(Transaction.class); Mockito.verify(mMessageSender).deliver(captor.capture()); Transaction transaction = captor.getValue(); Assert.assertNotNull(transaction); Assert.assertSame(mActorImpl, transaction.impl); Assert.assertSame(mFailureHandler, transaction.failureHandler); Assert.assertSame(message, transaction.message); Mockito.reset(mMessageSender); Mockito.doReturn(mBinder).when(mMessageSender).asBinder(); Mockito.doReturn(false).when(mMessageSender).deliver(Mockito.any(Transaction.class)); application.clearStartedServices(); message = Mockito.mock(Message.class); postman.deliver(message); intent = application.getNextStartedService(); Assert.assertNotNull(intent); Assert.assertEquals(new ComponentName(application.getApplicationContext(), ActorService.class), intent.getComponent()); captor = ArgumentCaptor.forClass(Transaction.class); Mockito.verify(mMessageSender, Mockito.times(2)).deliver(captor.capture()); transaction = captor.getValue(); Assert.assertNotNull(transaction); Assert.assertSame(mActorImpl, transaction.impl); Assert.assertSame(mFailureHandler, transaction.failureHandler); Assert.assertSame(message, transaction.message); Mockito.reset(mMessageSender); Mockito.doReturn(mBinder).when(mMessageSender).asBinder(); Mockito.doReturn(true).when(mMessageSender).deliver(Mockito.<Transaction>any()); application.clearStartedServices(); message = Mockito.mock(Message.class); postman.deliver(message); Assert.assertNull(application.getNextStartedService()); Mockito.verify(mMessageSender).deliver(captor.capture()); transaction = captor.getValue(); Assert.assertNotNull(transaction); Assert.assertSame(mActorImpl, transaction.impl); Assert.assertSame(mFailureHandler, transaction.failureHandler); Assert.assertSame(message, transaction.message); ((ServiceConnection) postman).onServiceDisconnected(null); Mockito.reset(mMessageSender); Mockito.doReturn(mBinder).when(mMessageSender).asBinder(); application.clearStartedServices(); message = Mockito.mock(Message.class); postman.deliver(message); intent = application.getNextStartedService(); Assert.assertNotNull(intent); Assert.assertEquals(new ComponentName(application.getApplicationContext(), ActorService.class), intent.getComponent()); captor = ArgumentCaptor.forClass(Transaction.class); Mockito.verify(mMessageSender).deliver(captor.capture()); transaction = captor.getValue(); Assert.assertNotNull(transaction); Assert.assertSame(mActorImpl, transaction.impl); Assert.assertSame(mFailureHandler, transaction.failureHandler); Assert.assertSame(message, transaction.message); }
@Test public void emptyInterfaceTest() { ShadowApplication application = ShadowApplication.getInstance(); ComponentName componentName = new ComponentName(application.getApplicationContext(), ActorService.class); Binder binder = new Binder(); ServiceActorThread thread = new ServiceActorThread(application.getApplicationContext(), mProxyFactory, mFailureHandler, ActorService.class, 1); ActorRef<Runnable> ref = thread.bind(Runnable.class, mActorImpl); Assert.assertSame(mProxy, ref.tell()); ArgumentCaptor<MessageSender> postmanCaptor = ArgumentCaptor.forClass(MessageSender.class); Mockito.verify(mProxyFactory).newProxy(Mockito.<Class>any(), postmanCaptor.capture()); MessageSender postman = postmanCaptor.getValue(); Assert.assertNotNull(postman); ((ServiceConnection) postman).onServiceConnected(componentName, binder); Intent intent = application.getNextStoppedService(); Assert.assertNotNull(intent); Assert.assertEquals(new ComponentName(application.getApplicationContext(), ActorService.class), intent.getComponent()); intent = application.getNextStartedService(); Assert.assertNotNull(intent); Assert.assertEquals(new ComponentName(application.getApplicationContext(), ActorService.class), intent.getComponent()); application.clearStartedServices(); ((ServiceConnection) postman).onServiceConnected(componentName, binder); intent = application.getNextStoppedService(); Assert.assertNotNull(intent); Assert.assertEquals(new ComponentName(application.getApplicationContext(), ActorService.class), intent.getComponent()); intent = application.getNextStartedService(); Assert.assertNull(intent); }
@Test public void emptyRemoteExceptionTest() throws Exception { ShadowApplication application = ShadowApplication.getInstance(); ComponentName componentName = new ComponentName(application.getApplicationContext(), ActorService.class); IBinder binder = Mockito.mock(IBinder.class); Mockito.doThrow(new RemoteException()).when(binder).getInterfaceDescriptor(); ServiceActorThread thread = new ServiceActorThread(application.getApplicationContext(), mProxyFactory, mFailureHandler, ActorService.class, 1); ActorRef<Runnable> ref = thread.bind(Runnable.class, mActorImpl); Assert.assertSame(mProxy, ref.tell()); ArgumentCaptor<MessageSender> postmanCaptor = ArgumentCaptor.forClass(MessageSender.class); Mockito.verify(mProxyFactory).newProxy(Mockito.<Class>any(), postmanCaptor.capture()); MessageSender postman = postmanCaptor.getValue(); Assert.assertNotNull(postman); ((ServiceConnection) postman).onServiceConnected(componentName, binder); Intent intent = application.getNextStoppedService(); Assert.assertNotNull(intent); Assert.assertEquals(new ComponentName(application.getApplicationContext(), ActorService.class), intent.getComponent()); intent = application.getNextStartedService(); Assert.assertNotNull(intent); Assert.assertEquals(new ComponentName(application.getApplicationContext(), ActorService.class), intent.getComponent()); application.clearStartedServices(); ((ServiceConnection) postman).onServiceConnected(componentName, binder); intent = application.getNextStoppedService(); Assert.assertNotNull(intent); Assert.assertEquals(new ComponentName(application.getApplicationContext(), ActorService.class), intent.getComponent()); intent = application.getNextStartedService(); Assert.assertNull(intent); }