@Test @SmallTest public void testRunning_onlyWhenEnqueued() { OneTimeWorkRequest work = new OneTimeWorkRequest.Builder(TestWorker.class) .setInitialState(RUNNING) .build(); insertWork(work); new WorkerWrapper.Builder(mContext, mConfiguration, mDatabase, work.getStringId()) .withListener(mMockListener) .build() .run(); verify(mMockListener).onExecuted(work.getStringId(), false, true); }
@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);  }