/* package */ synchronized ExecutionDelegator getExecutionDelegator() { if (executionDelegator == null) { executionDelegator = new ExecutionDelegator(this, this, new ConstraintChecker(getApplicationContext())); } return executionDelegator; }
/** * Returns true iff all the specified job constraints are satisfied. Note: At the moment this * method only checks for network constraints and nothing more. * * @param job the job whose constraints are to be checked. */ public boolean areConstraintsSatisfied(JobInvocation job) { int jobConstraints = Constraint.compact(job.getConstraints()); return areNetworkConstraintsSatisfied(jobConstraints); }
/** * Returns true if the specified jobConstraints are satisfied. We only check whether network * constraints are satisfied. All other constraints are assumed to be satisfied. */ private boolean areNetworkConstraintsSatisfied(@JobConstraint int jobConstraints) { // Network constraints are always satisfied for jobs that don't need a network if (!wantsNetwork(jobConstraints)) { return true; } // Ensure basic network connectivity is available. ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (!isNetworkConnected(connectivityManager)) { return false; } // Note: Constraint.ON_ANY_NETWORK and Constraint.ON_UNMETERED_NETWORK are mutually exclusive. // Constraints satisfied if we don't need an unmetered network (as that implies any network is // OK) or current network is unmetered. return !wantsUnmeteredNetwork(jobConstraints) || isNetworkUnmetered(connectivityManager); }
/** Returns true if any of the given {@code jobConstraints} require a network. */ private static boolean wantsNetwork(@JobConstraint int jobConstraints) { return wantsAnyNetwork(jobConstraints) || wantsUnmeteredNetwork(jobConstraints); }
@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() { MockitoAnnotations.initMocks(this); context = RuntimeEnvironment.application; constraintChecker = new ConstraintChecker(context); jobBuilder = new JobInvocation.Builder().setTag(JOB_TAG).setService(JOB_SERVICE).setTrigger(Trigger.NOW); ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); shadowConnectivityManager = shadowOf(connectivityManager); shadowNetworkInfo = shadowOf(connectivityManager.getActiveNetworkInfo()); }
@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(); }