@Test @SmallTest public void testDependencies_passesMergedOutputs() { String key = "key"; String value1 = "value1"; String value2 = "value2"; OneTimeWorkRequest prerequisiteWork1 = new OneTimeWorkRequest.Builder(EchoingWorker.class) .setInputData(new Data.Builder().putString(key, value1).build()) .build(); OneTimeWorkRequest prerequisiteWork2 = new OneTimeWorkRequest.Builder(EchoingWorker.class) .setInputData(new Data.Builder().putString(key, value2).build()) .build(); OneTimeWorkRequest work = new OneTimeWorkRequest.Builder(TestWorker.class) .setInputMerger(ArrayCreatingInputMerger.class) .build(); Dependency dependency1 = new Dependency(work.getStringId(), prerequisiteWork1.getStringId()); Dependency dependency2 = new Dependency(work.getStringId(), prerequisiteWork2.getStringId()); mDatabase.beginTransaction(); try { insertWork(prerequisiteWork1); insertWork(prerequisiteWork2); insertWork(work); mDependencyDao.insertDependency(dependency1); mDependencyDao.insertDependency(dependency2); mDatabase.setTransactionSuccessful(); } finally { mDatabase.endTransaction(); } new WorkerWrapper.Builder(mContext, mConfiguration, mDatabase, prerequisiteWork1.getStringId()) .withSchedulers(Collections.singletonList(mMockScheduler)) .build().run(); new WorkerWrapper.Builder(mContext, mConfiguration, mDatabase, prerequisiteWork2.getStringId()) .withSchedulers(Collections.singletonList(mMockScheduler)) .build().run(); WorkerWrapper workerWrapper = new WorkerWrapper.Builder(mContext, mConfiguration, mDatabase, work.getStringId()) .withSchedulers(Collections.singletonList(mMockScheduler)) .build(); workerWrapper.run(); Data input = workerWrapper.mWorker.getInputData(); assertThat(input.size(), is(1)); assertThat(Arrays.asList(input.getStringArray(key)), containsInAnyOrder(value1, value2)); }
@WorkerThread @Override public void run() { if (tryCheckForInterruptionAndNotify()) { return; } mWorkSpec = mWorkSpecDao.getWorkSpec(mWorkSpecId); if (mWorkSpec == null) { Log.e(TAG, String.format("Didn't find WorkSpec for id %s", mWorkSpecId)); notifyListener(false, false); return; } if (mWorkSpec.state != ENQUEUED) { notifyIncorrectStatus(); return; } Data input; if (mWorkSpec.isPeriodic()) { input = mWorkSpec.input; } else { InputMerger inputMerger = InputMerger.fromClassName(mWorkSpec.inputMergerClassName); if (inputMerger == null) { Log.e(TAG, String.format("Could not create Input Merger %s", mWorkSpec.inputMergerClassName)); setFailedAndNotify(); return; } List<Data> inputs = new ArrayList<>(); inputs.add(mWorkSpec.input); inputs.addAll(mWorkSpecDao.getInputsFromPrerequisites(mWorkSpecId)); input = inputMerger.merge(inputs); } Extras extras = new Extras( input, mWorkTagDao.getTagsForWorkSpecId(mWorkSpecId), mRuntimeExtras, mWorkSpec.runAttemptCount); if (mWorker == null) { mWorker = workerFromWorkSpec(mAppContext, mWorkSpec, extras); } if (mWorker == null) { Log.e(TAG, String.format("Could for create Worker %s", mWorkSpec.workerClassName)); setFailedAndNotify(); return; } if (trySetRunning()) { if (tryCheckForInterruptionAndNotify()) { return; } Worker.Result result; try { result = mWorker.doWork(); } catch (Exception | Error e) { result = Worker.Result.FAILURE; } try { mWorkDatabase.beginTransaction(); if (!tryCheckForInterruptionAndNotify()) { State state = mWorkSpecDao.getState(mWorkSpecId); if (state == null) { notifyListener(false, false); } else if (state == RUNNING) { handleResult(result); } else if (!state.isFinished()) { rescheduleAndNotify(); } mWorkDatabase.setTransactionSuccessful(); } } finally { mWorkDatabase.endTransaction(); } } else { notifyIncorrectStatus(); } }
WorkerWrapper implements Runnable { @WorkerThread @Override public void run() { if (tryCheckForInterruptionAndNotify()) { return; } mWorkSpec = mWorkSpecDao.getWorkSpec(mWorkSpecId); if (mWorkSpec == null) { Log.e(TAG, String.format("Didn't find WorkSpec for id %s", mWorkSpecId)); notifyListener(false, false); return; } if (mWorkSpec.state != ENQUEUED) { notifyIncorrectStatus(); return; } Data input; if (mWorkSpec.isPeriodic()) { input = mWorkSpec.input; } else { InputMerger inputMerger = InputMerger.fromClassName(mWorkSpec.inputMergerClassName); if (inputMerger == null) { Log.e(TAG, String.format("Could not create Input Merger %s", mWorkSpec.inputMergerClassName)); setFailedAndNotify(); return; } List<Data> inputs = new ArrayList<>(); inputs.add(mWorkSpec.input); inputs.addAll(mWorkSpecDao.getInputsFromPrerequisites(mWorkSpecId)); input = inputMerger.merge(inputs); } Extras extras = new Extras( input, mWorkTagDao.getTagsForWorkSpecId(mWorkSpecId), mRuntimeExtras, mWorkSpec.runAttemptCount); if (mWorker == null) { mWorker = workerFromWorkSpec(mAppContext, mWorkSpec, extras); } if (mWorker == null) { Log.e(TAG, String.format("Could for create Worker %s", mWorkSpec.workerClassName)); setFailedAndNotify(); return; } if (trySetRunning()) { if (tryCheckForInterruptionAndNotify()) { return; } Worker.Result result; try { result = mWorker.doWork(); } catch (Exception | Error e) { result = Worker.Result.FAILURE; } try { mWorkDatabase.beginTransaction(); if (!tryCheckForInterruptionAndNotify()) { State state = mWorkSpecDao.getState(mWorkSpecId); if (state == null) { notifyListener(false, false); } else if (state == RUNNING) { handleResult(result); } else if (!state.isFinished()) { rescheduleAndNotify(); } mWorkDatabase.setTransactionSuccessful(); } } finally { mWorkDatabase.endTransaction(); } } else { notifyIncorrectStatus(); } } }
WorkerWrapper implements Runnable { @WorkerThread @Override public void run() { if (tryCheckForInterruptionAndNotify()) { return; } mWorkSpec = mWorkSpecDao.getWorkSpec(mWorkSpecId); if (mWorkSpec == null) { Log.e(TAG, String.format("Didn't find WorkSpec for id %s", mWorkSpecId)); notifyListener(false, false); return; } if (mWorkSpec.state != ENQUEUED) { notifyIncorrectStatus(); return; } Data input; if (mWorkSpec.isPeriodic()) { input = mWorkSpec.input; } else { InputMerger inputMerger = InputMerger.fromClassName(mWorkSpec.inputMergerClassName); if (inputMerger == null) { Log.e(TAG, String.format("Could not create Input Merger %s", mWorkSpec.inputMergerClassName)); setFailedAndNotify(); return; } List<Data> inputs = new ArrayList<>(); inputs.add(mWorkSpec.input); inputs.addAll(mWorkSpecDao.getInputsFromPrerequisites(mWorkSpecId)); input = inputMerger.merge(inputs); } Extras extras = new Extras( input, mWorkTagDao.getTagsForWorkSpecId(mWorkSpecId), mRuntimeExtras, mWorkSpec.runAttemptCount); if (mWorker == null) { mWorker = workerFromWorkSpec(mAppContext, mWorkSpec, extras); } if (mWorker == null) { Log.e(TAG, String.format("Could for create Worker %s", mWorkSpec.workerClassName)); setFailedAndNotify(); return; } if (trySetRunning()) { if (tryCheckForInterruptionAndNotify()) { return; } Worker.Result result; try { result = mWorker.doWork(); } catch (Exception | Error e) { result = Worker.Result.FAILURE; } try { mWorkDatabase.beginTransaction(); if (!tryCheckForInterruptionAndNotify()) { State state = mWorkSpecDao.getState(mWorkSpecId); if (state == null) { notifyListener(false, false); } else if (state == RUNNING) { handleResult(result); } else if (!state.isFinished()) { rescheduleAndNotify(); } mWorkDatabase.setTransactionSuccessful(); } } finally { mWorkDatabase.endTransaction(); } } else { notifyIncorrectStatus(); } } private  WorkerWrapper(Builder builder); }
WorkerWrapper implements Runnable { @WorkerThread @Override public void run() { if (tryCheckForInterruptionAndNotify()) { return; } mWorkSpec = mWorkSpecDao.getWorkSpec(mWorkSpecId); if (mWorkSpec == null) { Log.e(TAG, String.format("Didn't find WorkSpec for id %s", mWorkSpecId)); notifyListener(false, false); return; } if (mWorkSpec.state != ENQUEUED) { notifyIncorrectStatus(); return; } Data input; if (mWorkSpec.isPeriodic()) { input = mWorkSpec.input; } else { InputMerger inputMerger = InputMerger.fromClassName(mWorkSpec.inputMergerClassName); if (inputMerger == null) { Log.e(TAG, String.format("Could not create Input Merger %s", mWorkSpec.inputMergerClassName)); setFailedAndNotify(); return; } List<Data> inputs = new ArrayList<>(); inputs.add(mWorkSpec.input); inputs.addAll(mWorkSpecDao.getInputsFromPrerequisites(mWorkSpecId)); input = inputMerger.merge(inputs); } Extras extras = new Extras( input, mWorkTagDao.getTagsForWorkSpecId(mWorkSpecId), mRuntimeExtras, mWorkSpec.runAttemptCount); if (mWorker == null) { mWorker = workerFromWorkSpec(mAppContext, mWorkSpec, extras); } if (mWorker == null) { Log.e(TAG, String.format("Could for create Worker %s", mWorkSpec.workerClassName)); setFailedAndNotify(); return; } if (trySetRunning()) { if (tryCheckForInterruptionAndNotify()) { return; } Worker.Result result; try { result = mWorker.doWork(); } catch (Exception | Error e) { result = Worker.Result.FAILURE; } try { mWorkDatabase.beginTransaction(); if (!tryCheckForInterruptionAndNotify()) { State state = mWorkSpecDao.getState(mWorkSpecId); if (state == null) { notifyListener(false, false); } else if (state == RUNNING) { handleResult(result); } else if (!state.isFinished()) { rescheduleAndNotify(); } mWorkDatabase.setTransactionSuccessful(); } } finally { mWorkDatabase.endTransaction(); } } else { notifyIncorrectStatus(); } } private  WorkerWrapper(Builder builder); @WorkerThread @Override void run(); @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) void interrupt(boolean cancelled); @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) @SuppressWarnings("ClassNewInstance") static Worker workerFromClassName(
            @NonNull Context context,
            @NonNull String workerClassName,
            @NonNull UUID workSpecId,
            @NonNull Extras extras); }
WorkerWrapper implements Runnable { @WorkerThread @Override public void run() { if (tryCheckForInterruptionAndNotify()) { return; } mWorkSpec = mWorkSpecDao.getWorkSpec(mWorkSpecId); if (mWorkSpec == null) { Log.e(TAG, String.format("Didn't find WorkSpec for id %s", mWorkSpecId)); notifyListener(false, false); return; } if (mWorkSpec.state != ENQUEUED) { notifyIncorrectStatus(); return; } Data input; if (mWorkSpec.isPeriodic()) { input = mWorkSpec.input; } else { InputMerger inputMerger = InputMerger.fromClassName(mWorkSpec.inputMergerClassName); if (inputMerger == null) { Log.e(TAG, String.format("Could not create Input Merger %s", mWorkSpec.inputMergerClassName)); setFailedAndNotify(); return; } List<Data> inputs = new ArrayList<>(); inputs.add(mWorkSpec.input); inputs.addAll(mWorkSpecDao.getInputsFromPrerequisites(mWorkSpecId)); input = inputMerger.merge(inputs); } Extras extras = new Extras( input, mWorkTagDao.getTagsForWorkSpecId(mWorkSpecId), mRuntimeExtras, mWorkSpec.runAttemptCount); if (mWorker == null) { mWorker = workerFromWorkSpec(mAppContext, mWorkSpec, extras); } if (mWorker == null) { Log.e(TAG, String.format("Could for create Worker %s", mWorkSpec.workerClassName)); setFailedAndNotify(); return; } if (trySetRunning()) { if (tryCheckForInterruptionAndNotify()) { return; } Worker.Result result; try { result = mWorker.doWork(); } catch (Exception | Error e) { result = Worker.Result.FAILURE; } try { mWorkDatabase.beginTransaction(); if (!tryCheckForInterruptionAndNotify()) { State state = mWorkSpecDao.getState(mWorkSpecId); if (state == null) { notifyListener(false, false); } else if (state == RUNNING) { handleResult(result); } else if (!state.isFinished()) { rescheduleAndNotify(); } mWorkDatabase.setTransactionSuccessful(); } } finally { mWorkDatabase.endTransaction(); } } else { notifyIncorrectStatus(); } } private  WorkerWrapper(Builder builder); @WorkerThread @Override void run(); @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) void interrupt(boolean cancelled); @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) @SuppressWarnings("ClassNewInstance") static Worker workerFromClassName(
            @NonNull Context context,
            @NonNull String workerClassName,
            @NonNull UUID workSpecId,
            @NonNull Extras extras);  }