@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); googlePlayDriver = new GooglePlayDriver(mMockContext); driver = new TestJobDriver(googlePlayDriver); dispatcher = new FirebaseJobDispatcher(driver); when(mMockContext.getPackageName()).thenReturn("foo.bar.whatever"); when(constraintCheckerMock.areConstraintsSatisfied(any(JobInvocation.class))).thenReturn(true); }
@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) {} }; }
@Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); receiver = spy(Robolectric.buildService(GooglePlayReceiver.class).create().get()); when(contraintCheckerMock.areConstraintsSatisfied(any(JobInvocation.class))).thenReturn(true); when(receiver.getExecutionDelegator()).thenReturn(executionDelegatorMock); receiver.setGooglePlayDriver(driverMock); receiver.setValidationEnforcer(new ValidationEnforcer(new NoopJobValidator())); when(contextMock.getPackageName()).thenReturn("foo.bar.whatever"); }
if (!constraintChecker.areConstraintsSatisfied(jobInvocation)) { if (Log.isLoggable(TAG, Log.DEBUG)) { Log.d(TAG, "Not executing job because constraints still unmet. Job: " + jobInvocation);
@Test public void executeJob_constraintsUnsatisfied_schedulesJobRetry() throws RemoteException { // Simulate job constraints not being met. JobInvocation jobInvocation = new JobInvocation.Builder() .setTag("tag") .setService("service") .setTrigger(Trigger.NOW) .build(); when(constraintChecker.areConstraintsSatisfied(eq(jobInvocation))).thenReturn(false); executionDelegator.executeJob(jobInvocation); // Confirm that service not bound verify(mockContext, never()) .bindService(any(Intent.class), any(JobServiceConnection.class), anyInt()); assertThat(ExecutionDelegator.getJobServiceConnection("service")).isNull(); // Verify that job is set for a retry later. assertThat(receiver.lastResult).isEqualTo(JobService.RESULT_FAIL_RETRY); }
@Test public void testAreConstraintsSatisfied_nonNetworkConstraints() { JobInvocation job = jobBuilder .setConstraints( Constraint.uncompact(Constraint.DEVICE_IDLE | Constraint.DEVICE_CHARGING)) .build(); assertThat(constraintChecker.areConstraintsSatisfied(job)).isTrue(); }
@Test public void testAreConstraintsSatisfied_nonNetworkConstraint() { JobInvocation job = jobBuilder.setConstraints(Constraint.uncompact(Constraint.DEVICE_IDLE)).build(); assertThat(constraintChecker.areConstraintsSatisfied(job)).isTrue(); }
@Test public void testAreConstraintsSatisfied_anyNetworkRequired_unsatisfied_notConnected() { JobInvocation job = jobBuilder.setConstraints(Constraint.uncompact(Constraint.ON_ANY_NETWORK)).build(); shadowNetworkInfo.setConnectionStatus(/* isConnected= */ false); assertThat(constraintChecker.areConstraintsSatisfied(job)).isFalse(); }
@Test public void testAreConstraintsSatisfied_anyNetworkRequired_unsatisfied_nullNetworkInfo() { JobInvocation job = jobBuilder.setConstraints(Constraint.uncompact(Constraint.ON_ANY_NETWORK)).build(); shadowConnectivityManager.setActiveNetworkInfo(null); assertThat(constraintChecker.areConstraintsSatisfied(job)).isFalse(); }
@Test public void testAreConstraintsSatisfied_anyNetworkRequired_satisfied() { JobInvocation job = jobBuilder.setConstraints(Constraint.uncompact(Constraint.ON_ANY_NETWORK)).build(); shadowNetworkInfo.setConnectionStatus(/* isConnected= */ true); assertThat(constraintChecker.areConstraintsSatisfied(job)).isTrue(); }
@Test public void testAreConstraintsSatisfied_unmeteredNetworkRequired_unsatisfied_networkDisconnected() { JobInvocation job = jobBuilder.setConstraints(Constraint.uncompact(Constraint.ON_UNMETERED_NETWORK)).build(); shadowNetworkInfo.setConnectionStatus(/* isConnected= */ false); setNetworkMetered(false); assertThat(constraintChecker.areConstraintsSatisfied(job)).isFalse(); }
@Test public void testAreConstraintsSatisfied_unmeteredNetworkRequired_unsatisfied_networkMetered() { JobInvocation job = jobBuilder.setConstraints(Constraint.uncompact(Constraint.ON_UNMETERED_NETWORK)).build(); shadowNetworkInfo.setConnectionStatus(/* isConnected= */ true); setNetworkMetered(true); assertThat(constraintChecker.areConstraintsSatisfied(job)).isFalse(); }
@Test public void testAreConstraintsSatisfied_anyNetworkRequired_satisfied_includesNonNetworkConstraints() { JobInvocation job = jobBuilder .setConstraints( Constraint.uncompact( Constraint.DEVICE_IDLE | Constraint.DEVICE_CHARGING | Constraint.ON_ANY_NETWORK)) .build(); shadowNetworkInfo.setConnectionStatus(/* isConnected= */ true); assertThat(constraintChecker.areConstraintsSatisfied(job)).isTrue(); } }
@Test public void testAreConstraintsSatisfied_unmeteredNetworkRequired_satisfied() { JobInvocation job = jobBuilder.setConstraints(Constraint.uncompact(Constraint.ON_UNMETERED_NETWORK)).build(); shadowNetworkInfo.setConnectionStatus(/* isConnected= */ true); setNetworkMetered(false); assertThat(constraintChecker.areConstraintsSatisfied(job)).isTrue(); }