private synchronized Messenger getServiceMessenger() { if (serviceMessenger == null) { serviceMessenger = new Messenger(new GooglePlayMessageHandler(Looper.getMainLooper(), this)); } return serviceMessenger; }
@Override public void handleMessage(Message message) { if (message == null) { return; } AppOpsManager appOpsManager = (AppOpsManager) googlePlayReceiver.getApplicationContext().getSystemService(Context.APP_OPS_SERVICE); try { appOpsManager.checkPackage(message.sendingUid, GooglePlayDriver.BACKEND_PACKAGE); } catch (SecurityException e) { Log.e(TAG, "Message was not sent from GCM."); return; } switch (message.what) { case MSG_START_EXEC: handleStartMessage(message); break; case MSG_STOP_EXEC: handleStopMessage(message); break; case MSG_INIT: // Not implemented. break; default: Log.e(TAG, "Unrecognized message received: " + message); break; } }
@Test public void handleMessage_stopExecution_invalidNoCrash() throws Exception { Message message = Message.obtain(); message.what = GooglePlayMessageHandler.MSG_STOP_EXEC; message.replyTo = messengerMock; handler.handleMessage(message); } }
@Test public void handleMessage_nullNoException() throws Exception { handler.handleMessage(null); }
@Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); handler = new GooglePlayMessageHandler(looper, receiverMock); when(constraintCheckerMock.areConstraintsSatisfied(any(JobInvocation.class))).thenReturn(true); executionDelegator = new ExecutionDelegator(contextMock, jobFinishedCallbackMock, constraintCheckerMock); when(receiverMock.getExecutionDelegator()).thenReturn(executionDelegator); when(receiverMock.getApplicationContext()).thenReturn(contextMock); when(contextMock.getSystemService(Context.APP_OPS_SERVICE)).thenReturn(appOpsManager); }
@Test public void handleMessage_startExecution_noData() throws Exception { Message message = Message.obtain(); message.what = GooglePlayMessageHandler.MSG_START_EXEC; message.replyTo = messengerMock; handler.handleMessage(message); verify(receiverMock, never()) .prepareJob(any(GooglePlayMessengerCallback.class), any(Bundle.class)); }
@Test public void handleMessage_ignoreIfSenderIsNotGcm() throws Exception { Message message = Message.obtain(); message.what = GooglePlayMessageHandler.MSG_START_EXEC; Bundle data = new Bundle(); data.putString(REQUEST_PARAM_TAG, "TAG"); message.setData(data); message.replyTo = messengerMock; doThrow(new SecurityException()) .when(appOpsManager) .checkPackage(message.sendingUid, GooglePlayDriver.BACKEND_PACKAGE); handler.handleMessage(message); verify(receiverMock, never()).prepareJob(any(GooglePlayMessengerCallback.class), eq(data)); }
@Test public void handleMessage_startExecution() throws Exception { Message startExecutionMsg = Message.obtain(); startExecutionMsg.what = GooglePlayMessageHandler.MSG_START_EXEC; Bundle data = new Bundle(); data.putString(REQUEST_PARAM_TAG, "TAG"); startExecutionMsg.setData(data); startExecutionMsg.replyTo = messengerMock; JobInvocation jobInvocation = new Builder() .setTag("tag") .setService(TestJobService.class.getName()) .setTrigger(Trigger.NOW) .build(); when(receiverMock.prepareJob(any(GooglePlayMessengerCallback.class), eq(data))) .thenReturn(jobInvocation); when(contextMock.bindService( any(Intent.class), any(JobServiceConnection.class), eq(BIND_AUTO_CREATE))) .thenReturn(true); handler.handleMessage(startExecutionMsg); verify(contextMock) .bindService(any(Intent.class), jobServiceConnectionCaptor.capture(), eq(BIND_AUTO_CREATE)); assertTrue(jobServiceConnectionCaptor.getValue().hasJobInvocation(jobInvocation)); }
handler.handleMessage(message);