GooglePlayMessageHandler extends Handler { @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: break; default: Log.e(TAG, "Unrecognized message received: " + message); break; } } GooglePlayMessageHandler(Looper mainLooper, GooglePlayReceiver googlePlayReceiver); @Override void handleMessage(Message message);  }
@Test public void handleMessage_nullNoException() throws Exception { handler.handleMessage(null); }
@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_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_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)); }
@Test public void handleMessage_stopExecution_noUnbindWaitForTheResult() throws Exception { Message message = Message.obtain(); message.what = GooglePlayMessageHandler.MSG_STOP_EXEC; JobCoder jobCoder = GooglePlayReceiver.getJobCoder(); Bundle data = TestUtil.encodeContentUriJob(TestUtil.getContentUriTrigger(), jobCoder); JobInvocation jobInvocation = jobCoder.decode(data).build(); message.setData(data); message.replyTo = messengerMock; when(contextMock.bindService( any(Intent.class), any(JobServiceConnection.class), eq(BIND_AUTO_CREATE))) .thenReturn(true); executionDelegator.executeJob(jobInvocation); verify(contextMock) .bindService(any(Intent.class), jobServiceConnectionCaptor.capture(), eq(BIND_AUTO_CREATE)); JobServiceConnection serviceConnection = jobServiceConnectionCaptor.getValue(); assertTrue(serviceConnection.hasJobInvocation(jobInvocation)); when(iBinderMock.queryLocalInterface(anyString())).thenReturn(jobServiceMock); serviceConnection.onServiceConnected(null, iBinderMock); verify(jobServiceMock).start(bundleCaptor.capture(), jobCallbackCaptor.capture()); handler.handleMessage(message); assertEquals( serviceConnection, ExecutionDelegator.getJobServiceConnection(jobInvocation.getService())); assertFalse(serviceConnection.wasUnbound()); verify(contextMock, never()).unbindService(serviceConnection); jobCallbackCaptor .getValue() .jobFinished(bundleCaptor.getValue(), JobService.RESULT_FAIL_NORETRY); verify(contextMock).unbindService(serviceConnection); assertTrue(serviceConnection.wasUnbound()); }
@Test public void handleMessage_stopExecution_invalidNoCrash() throws Exception { Message message = Message.obtain(); message.what = GooglePlayMessageHandler.MSG_STOP_EXEC; message.replyTo = messengerMock; handler.handleMessage(message); }