1 /*
2  * Copyright (C) 2023 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 com.android.server.healthconnect;
18 
19 import static org.mockito.ArgumentMatchers.any;
20 import static org.mockito.Mockito.times;
21 import static org.mockito.Mockito.verify;
22 import static org.mockito.Mockito.when;
23 
24 import android.app.job.JobScheduler;
25 import android.content.Context;
26 
27 
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.mockito.Mock;
31 import org.mockito.MockitoAnnotations;
32 
33 public class HealthConnectDailyJobsTest {
34     private static final String HEALTH_CONNECT_DAILY_JOB_NAMESPACE = "HEALTH_CONNECT_DAILY_JOB";
35     private static final String ANDROID_SERVER_PACKAGE_NAME = "com.android.server";
36     @Mock Context mContext;
37     @Mock private JobScheduler mJobScheduler;
38 
39     @Before
setUp()40     public void setUp() {
41         MockitoAnnotations.initMocks(this);
42 
43         when(mJobScheduler.forNamespace(HEALTH_CONNECT_DAILY_JOB_NAMESPACE))
44                 .thenReturn(mJobScheduler);
45         when(mContext.getSystemService(JobScheduler.class)).thenReturn(mJobScheduler);
46         when(mContext.getPackageName()).thenReturn(ANDROID_SERVER_PACKAGE_NAME);
47     }
48 
49     @Test
testJobSchedule()50     public void testJobSchedule() {
51         HealthConnectDailyJobs.schedule(mContext, 0);
52         verify(mJobScheduler, times(1)).schedule(any());
53         HealthConnectDailyJobs.schedule(mContext, 1);
54         verify(mJobScheduler, times(2)).schedule(any());
55         HealthConnectDailyJobs.cancelAllJobs(mContext);
56         verify(mJobScheduler, times(1)).cancelAll();
57     }
58 }
59