Home | History | Annotate | Download | only in utils
      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.impl.utils;
     18 
     19 import static org.hamcrest.CoreMatchers.is;
     20 import static org.hamcrest.MatcherAssert.assertThat;
     21 import static org.mockito.Mockito.doNothing;
     22 import static org.mockito.Mockito.mock;
     23 import static org.mockito.Mockito.spy;
     24 import static org.mockito.Mockito.times;
     25 import static org.mockito.Mockito.verify;
     26 import static org.mockito.Mockito.when;
     27 
     28 import android.content.ComponentName;
     29 import android.content.Context;
     30 import android.content.Intent;
     31 import android.support.test.InstrumentationRegistry;
     32 import android.support.test.filters.SmallTest;
     33 import android.support.test.runner.AndroidJUnit4;
     34 
     35 import androidx.work.impl.WorkDatabase;
     36 import androidx.work.impl.WorkManagerImpl;
     37 import androidx.work.impl.model.WorkSpecDao;
     38 
     39 import org.junit.Before;
     40 import org.junit.Test;
     41 import org.junit.runner.RunWith;
     42 
     43 @SmallTest
     44 @RunWith(AndroidJUnit4.class)
     45 public class ForceStopRunnableTest {
     46 
     47     private Context mContext;
     48     private WorkManagerImpl mWorkManager;
     49     private WorkDatabase mWorkDatabase;
     50     private WorkSpecDao mWorkSpecDao;
     51     private Preferences mPreferences;
     52     private ForceStopRunnable mRunnable;
     53 
     54     @Before
     55     public void setUp() {
     56         mContext = InstrumentationRegistry.getTargetContext().getApplicationContext();
     57         mWorkManager = mock(WorkManagerImpl.class);
     58         mWorkDatabase = mock(WorkDatabase.class);
     59         mWorkSpecDao = mock(WorkSpecDao.class);
     60         mPreferences = mock(Preferences.class);
     61         when(mWorkManager.getWorkDatabase()).thenReturn(mWorkDatabase);
     62         when(mWorkDatabase.workSpecDao()).thenReturn(mWorkSpecDao);
     63         when(mWorkManager.getPreferences()).thenReturn(mPreferences);
     64         mRunnable = new ForceStopRunnable(mContext, mWorkManager);
     65     }
     66 
     67     @Test
     68     public void testIntent() {
     69         Intent intent = mRunnable.getIntent();
     70         ComponentName componentName = intent.getComponent();
     71         assertThat(componentName.getClassName(),
     72                 is(ForceStopRunnable.BroadcastReceiver.class.getName()));
     73         assertThat(intent.getAction(), is(ForceStopRunnable.ACTION_FORCE_STOP_RESCHEDULE));
     74     }
     75 
     76     @Test
     77     public void testReschedulesOnForceStop() {
     78         ForceStopRunnable runnable = spy(mRunnable);
     79         when(runnable.shouldCancelPersistedJobs()).thenReturn(false);
     80         when(runnable.isForceStopped()).thenReturn(true);
     81         runnable.run();
     82         verify(mWorkManager, times(1)).rescheduleEligibleWork();
     83     }
     84 
     85     @Test
     86     public void test_doNothingWhenNotForceStopped() {
     87         ForceStopRunnable runnable = spy(mRunnable);
     88         when(runnable.shouldCancelPersistedJobs()).thenReturn(false);
     89         when(runnable.isForceStopped()).thenReturn(false);
     90         runnable.run();
     91         verify(mWorkManager, times(0)).rescheduleEligibleWork();
     92     }
     93 
     94     @Test
     95     public void test_cancelAllJobSchedulerJobs() {
     96         ForceStopRunnable runnable = spy(mRunnable);
     97         doNothing().when(runnable).cancelAllInJobScheduler();
     98         when(runnable.shouldCancelPersistedJobs()).thenReturn(true);
     99         runnable.run();
    100         verify(runnable, times(1)).cancelAllInJobScheduler();
    101         verify(mPreferences, times(1)).setMigratedPersistedJobs();
    102     }
    103 
    104     @Test
    105     public void test_doNothingWhenThereIsNothingToCancel() {
    106         ForceStopRunnable runnable = spy(mRunnable);
    107         doNothing().when(runnable).cancelAllInJobScheduler();
    108         when(runnable.shouldCancelPersistedJobs()).thenReturn(false);
    109         runnable.run();
    110         verify(runnable, times(0)).cancelAllInJobScheduler();
    111     }
    112 }
    113