@Test @SdkSuppress(minSdkVersion = 23, maxSdkVersion = 25) public void testConstraintTrackingWorker_onConstraintsMet() throws InterruptedException { when(mBatteryNotLowTracker.getInitialState()).thenReturn(true); Constraints constraints = new Constraints.Builder() .setRequiresBatteryNotLow(true) .build(); String delegateName = EchoingWorker.class.getName(); Data input = new Data.Builder() .putString(ConstraintTrackingWorker.ARGUMENT_CLASS_NAME, delegateName) .putBoolean(TEST_ARGUMENT_NAME, true) .build(); final OneTimeWorkRequest work = new OneTimeWorkRequest.Builder(ConstraintTrackingWorker.class) .setInputData(input) .setConstraints(constraints) .build(); insertWork(work); String workSpecId = work.getStringId(); ConstraintTrackingWorker worker = (ConstraintTrackingWorker) WorkerWrapper.workerFromClassName( mContext, ConstraintTrackingWorker.class.getName(), work.getId(), new Extras(input, Collections.<String>emptyList(), null, 1)); ConstraintTrackingWorker spyWorker = spy(worker); when(spyWorker.getWorkDatabase()).thenReturn(mDatabase); WorkerWrapper.Builder builder = new WorkerWrapper.Builder(mContext, mConfiguration, mDatabase, workSpecId); builder.withWorker(spyWorker) .withListener(this) .withSchedulers(Collections.singletonList(mScheduler)); mExecutorService.submit(builder.build()); mLatch.await(TEST_TIMEOUT_IN_SECONDS, TimeUnit.SECONDS); WorkSpec workSpec = mDatabase.workSpecDao().getWorkSpec(workSpecId); assertThat(mLatch.getCount(), is(0L)); assertThat(workSpec.state, is(State.SUCCEEDED)); Data output = workSpec.output; assertThat(output.getBoolean(TEST_ARGUMENT_NAME, false), is(true)); }
@VisibleForTesting public WorkDatabase getWorkDatabase() { return WorkManagerImpl.getInstance().getWorkDatabase(); }
ConstraintTrackingWorker extends Worker implements WorkConstraintsCallback { @VisibleForTesting public WorkDatabase getWorkDatabase() { return WorkManagerImpl.getInstance().getWorkDatabase(); } }
ConstraintTrackingWorker extends Worker implements WorkConstraintsCallback { @VisibleForTesting public WorkDatabase getWorkDatabase() { return WorkManagerImpl.getInstance().getWorkDatabase(); } ConstraintTrackingWorker(); }
ConstraintTrackingWorker extends Worker implements WorkConstraintsCallback { @VisibleForTesting public WorkDatabase getWorkDatabase() { return WorkManagerImpl.getInstance().getWorkDatabase(); } ConstraintTrackingWorker(); @Override @NonNull Result doWork(); @VisibleForTesting WorkDatabase getWorkDatabase(); @Override void onAllConstraintsMet(@NonNull List<String> workSpecIds); @Override void onAllConstraintsNotMet(@NonNull List<String> workSpecIds); }
ConstraintTrackingWorker extends Worker implements WorkConstraintsCallback { @VisibleForTesting public WorkDatabase getWorkDatabase() { return WorkManagerImpl.getInstance().getWorkDatabase(); } ConstraintTrackingWorker(); @Override @NonNull Result doWork(); @VisibleForTesting WorkDatabase getWorkDatabase(); @Override void onAllConstraintsMet(@NonNull List<String> workSpecIds); @Override void onAllConstraintsNotMet(@NonNull List<String> workSpecIds); static final String ARGUMENT_CLASS_NAME; }