Home | History | Annotate | Download | only in test
      1 /*
      2  * Copyright 2018 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package androidx.work.test;
     18 
     19 import android.content.Context;
     20 import android.support.annotation.NonNull;
     21 
     22 import androidx.work.Configuration;
     23 import androidx.work.impl.Scheduler;
     24 import androidx.work.impl.WorkManagerImpl;
     25 import androidx.work.impl.utils.taskexecutor.TaskExecutor;
     26 import androidx.work.impl.utils.taskexecutor.WorkManagerTaskExecutor;
     27 
     28 import java.util.Collections;
     29 import java.util.List;
     30 import java.util.UUID;
     31 
     32 /**
     33  * Helps initialize {@link androidx.work.WorkManager} for testing.
     34  */
     35 public final class WorkManagerTestInitHelper {
     36     /**
     37      * Initializes a test {@link androidx.work.WorkManager} with a {@link SynchronousExecutor}.
     38      *
     39      * @param context The application {@link Context}
     40      */
     41     public static void initializeTestWorkManager(@NonNull Context context) {
     42         SynchronousExecutor synchronousExecutor = new SynchronousExecutor();
     43         Configuration configuration = new Configuration.Builder()
     44                 .setExecutor(synchronousExecutor)
     45                 .build();
     46         initializeTestWorkManager(context, configuration);
     47     }
     48 
     49     /**
     50      * Initializes a test {@link androidx.work.WorkManager} with a user-specified
     51      * {@link androidx.work.Configuration}.
     52      *
     53      * @param context The application {@link Context}
     54      * @param configuration The {@link androidx.work.Configuration}
     55      */
     56     public static void initializeTestWorkManager(
     57             @NonNull Context context,
     58             @NonNull Configuration configuration) {
     59 
     60         setupSynchronousTaskExecutor();
     61         final TestScheduler scheduler = new TestScheduler();
     62         WorkManagerImpl workManager = new TestWorkManagerImpl(context, configuration) {
     63             @NonNull
     64             @Override
     65             public List<Scheduler> getSchedulers() {
     66                 return Collections.singletonList((Scheduler) scheduler);
     67             }
     68 
     69             @Override
     70             public void setAllConstraintsMet(@NonNull UUID workSpecId) {
     71                 scheduler.setAllConstraintsMet(workSpecId);
     72             }
     73         };
     74         workManager.getProcessor().addExecutionListener(scheduler);
     75         WorkManagerImpl.setDelegate(workManager);
     76     }
     77 
     78     /**
     79      * @return An instance of {@link TestDriver}. This exposes additional functionality that is
     80      * useful in the context of testing when using WorkManager.
     81      */
     82     public static TestDriver getTestDriver() {
     83         WorkManagerImpl workManager = WorkManagerImpl.getInstance();
     84         if (workManager == null) {
     85             return null;
     86         } else {
     87             return ((TestWorkManagerImpl) WorkManagerImpl.getInstance());
     88         }
     89     }
     90 
     91     private WorkManagerTestInitHelper() {
     92     }
     93 
     94     private static void setupSynchronousTaskExecutor() {
     95         WorkManagerTaskExecutor.getInstance()
     96                 .setTaskExecutor(new TaskExecutor() {
     97                     @Override
     98                     public void postToMainThread(Runnable runnable) {
     99                         runnable.run();
    100                     }
    101 
    102                     @Override
    103                     public void executeOnBackgroundThread(Runnable runnable) {
    104                         runnable.run();
    105                     }
    106                 });
    107     }
    108 }
    109