/* package */ synchronized ExecutionDelegator getExecutionDelegator() { if (executionDelegator == null) { executionDelegator = new ExecutionDelegator(this, this, new ConstraintChecker(getApplicationContext())); } return executionDelegator; }
@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); }
@Before public void setUp() { MockitoAnnotations.initMocks(this); when(mockContext.getPackageName()).thenReturn("com.example.foo"); when(constraintChecker.areConstraintsSatisfied(any(JobInvocation.class))).thenReturn(true); receiver = new TestJobReceiver(); executionDelegator = new ExecutionDelegator(mockContext, receiver, constraintChecker); ExecutionDelegator.cleanServiceConnections(); noopBinder = new IRemoteJobService.Stub() { @Override public void start(Bundle invocationData, IJobCallback callback) {} @Override public void stop(Bundle invocationData, boolean needToSendResult) {} }; }
@Test public void onReschedule_stopJob() { Bundle bundle = TestUtil.getBundleForContentJobExecution(); JobCoder prefixedCoder = new JobCoder(BundleProtocol.PACKED_PARAM_BUNDLE_PREFIX); JobInvocation invocation = prefixedCoder.decodeIntentBundle(bundle); Job job = TestUtil.getBuilderWithNoopValidator() .setService(TestJobService.class) .setTrigger(invocation.getTrigger()) .setTag(invocation.getTag()) .build(); receiver.prepareJob(jobCallbackMock, bundle); when(contextMock.bindService( any(Intent.class), any(JobServiceConnection.class), eq(BIND_AUTO_CREATE))) .thenReturn(true); new ExecutionDelegator(contextMock, mock(JobFinishedCallback.class), contraintCheckerMock) .executeJob(invocation); verify(contextMock) .bindService(any(Intent.class), jobServiceConnectionCaptor.capture(), eq(BIND_AUTO_CREATE)); assertTrue(jobServiceConnectionCaptor.getValue().hasJobInvocation(invocation)); GooglePlayReceiver.onSchedule(job); assertFalse(jobServiceConnectionCaptor.getValue().hasJobInvocation(invocation)); assertNull( "JobServiceConnection should be removed.", ExecutionDelegator.getJobServiceConnection(invocation.getService())); }
@Test public void schedule_whenRunning_onStopIsCalled() { // simulate running job Bundle bundle = TestUtil.getBundleForContentJobExecution(); JobCoder prefixedCoder = new JobCoder(BundleProtocol.PACKED_PARAM_BUNDLE_PREFIX); JobInvocation invocation = prefixedCoder.decodeIntentBundle(bundle); googlePlayReceiver.prepareJob(jobCallbackMock, bundle); when(mMockContext.bindService( any(Intent.class), serviceConnectionCaptor.capture(), eq(BIND_AUTO_CREATE))) .thenReturn(true); new ExecutionDelegator(mMockContext, callbackMock, constraintCheckerMock) .executeJob(invocation); Job job = TestUtil.getBuilderWithNoopValidator() .setService(TestJobService.class) .setTrigger(invocation.getTrigger()) .setTag(invocation.getTag()) .build(); googlePlayDriver.schedule(job); // reschedule request during the execution verify(mMockContext).sendBroadcast(any(Intent.class)); assertTrue(serviceConnectionCaptor.getValue().wasUnbound()); assertNull( "JobServiceConnection should be removed.", ExecutionDelegator.getJobServiceConnection(invocation.getService())); }