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 static com.google.common.truth.Truth.assertThat;
20 import static org.mockito.Mockito.times;
21 import static org.mockito.Mockito.verify;
22 import static org.mockito.Mockito.verifyNoMoreInteractions;
23 import static org.mockito.Mockito.when;
24 
25 import android.app.job.JobInfo;
26 import android.app.job.JobScheduler;
27 import android.content.Context;
28 import android.content.Intent;
29 
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.ArgumentCaptor;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 
37 import java.util.List;
38 import org.robolectric.RobolectricTestRunner;
39 
40 @RunWith(RobolectricTestRunner.class)
41 public class AutomaticStorageBroadcastReceiverTest {
42     @Mock private Context mMockContext;
43     @Mock private JobScheduler mJobScheduler;
44     @Mock private Intent mMockIntent;
45 
46     @Before
setUp()47     public void setUp() {
48         MockitoAnnotations.initMocks(this);
49     }
50 
51     @Test
testSetupJobServicesOnBoot()52     public void testSetupJobServicesOnBoot() {
53         when(mMockContext.getSystemService(Context.JOB_SCHEDULER_SERVICE)).thenReturn(
54                 mJobScheduler);
55 
56         AutomaticStorageBroadcastReceiver br = new AutomaticStorageBroadcastReceiver();
57         br.onReceive(mMockContext, mMockIntent);
58 
59         // Verify that the JobScheduler scheduled two jobs.
60         ArgumentCaptor<JobInfo> jobCaptor = ArgumentCaptor.forClass(JobInfo.class);
61         verify(mJobScheduler, times(1)).schedule(jobCaptor.capture());
62         verifyNoMoreInteractions(mJobScheduler);
63 
64         // Ensure that the jobs are the ones we expect.
65         List<JobInfo> capturedJobs = jobCaptor.getAllValues();
66         JobInfo asmJob = capturedJobs.get(0);
67         assertThat(asmJob.getService().getClassName())
68                 .isEqualTo(AutomaticStorageManagementJobService.class.getName());
69         assertThat(asmJob.isRequireCharging()).isTrue();
70         assertThat(asmJob.isRequireDeviceIdle()).isTrue();
71     }
72 }
73