1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 package com.android.storagemanager.automatic; 18 19 import android.app.job.JobInfo; 20 import android.app.job.JobScheduler; 21 import android.content.Context; 22 import android.content.Intent; 23 import com.android.storagemanager.testing.TestingConstants; 24 import org.junit.Before; 25 import org.junit.Test; 26 import org.junit.runner.RunWith; 27 import org.mockito.ArgumentCaptor; 28 import org.mockito.Mock; 29 import org.mockito.MockitoAnnotations; 30 import org.robolectric.RobolectricTestRunner; 31 import org.robolectric.annotation.Config; 32 33 import java.util.List; 34 35 import static com.google.common.truth.Truth.assertThat; 36 import static org.mockito.Mockito.times; 37 import static org.mockito.Mockito.verify; 38 import static org.mockito.Mockito.verifyNoMoreInteractions; 39 import static org.mockito.Mockito.when; 40 41 @RunWith(RobolectricTestRunner.class) 42 @Config(manifest=TestingConstants.MANIFEST, sdk=TestingConstants.SDK_VERSION) 43 public class AutomaticStorageBroadcastReceiverTest { 44 @Mock private Context mMockContext; 45 @Mock private JobScheduler mJobScheduler; 46 @Mock private Intent mMockIntent; 47 48 @Before 49 public void setUp() { 50 MockitoAnnotations.initMocks(this); 51 } 52 53 @Test 54 public void testSetupJobServicesOnBoot() { 55 when(mMockContext.getSystemService(Context.JOB_SCHEDULER_SERVICE)).thenReturn( 56 mJobScheduler); 57 58 AutomaticStorageBroadcastReceiver br = new AutomaticStorageBroadcastReceiver(); 59 br.onReceive(mMockContext, mMockIntent); 60 61 // Verify that the JobScheduler scheduled two jobs. 62 ArgumentCaptor<JobInfo> jobCaptor = ArgumentCaptor.forClass(JobInfo.class); 63 verify(mJobScheduler, times(1)).schedule(jobCaptor.capture()); 64 verifyNoMoreInteractions(mJobScheduler); 65 66 // Ensure that the jobs are the ones we expect. 67 List<JobInfo> capturedJobs = jobCaptor.getAllValues(); 68 JobInfo asmJob = capturedJobs.get(0); 69 assertThat(asmJob.getService().getClassName()) 70 .isEqualTo(AutomaticStorageManagementJobService.class.getName()); 71 assertThat(asmJob.isRequireCharging()).isTrue(); 72 assertThat(asmJob.isRequireDeviceIdle()).isTrue(); 73 } 74 } 75