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 import android.os.UserHandle; 27 28 import androidx.test.ext.junit.runners.AndroidJUnit4; 29 30 import org.junit.Before; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 import org.mockito.Mock; 34 import org.mockito.MockitoAnnotations; 35 36 @RunWith(AndroidJUnit4.class) 37 public class HealthConnectDailyJobsTest { 38 private static final String HEALTH_CONNECT_DAILY_JOB_NAMESPACE = "HEALTH_CONNECT_DAILY_JOB"; 39 private static final String ANDROID_SERVER_PACKAGE_NAME = "com.android.server"; 40 @Mock Context mContext; 41 @Mock private JobScheduler mJobScheduler; 42 43 @Before setUp()44 public void setUp() { 45 MockitoAnnotations.initMocks(this); 46 47 when(mJobScheduler.forNamespace(HEALTH_CONNECT_DAILY_JOB_NAMESPACE)) 48 .thenReturn(mJobScheduler); 49 when(mContext.getSystemService(JobScheduler.class)).thenReturn(mJobScheduler); 50 when(mContext.getPackageName()).thenReturn(ANDROID_SERVER_PACKAGE_NAME); 51 } 52 53 @Test testJobSchedule()54 public void testJobSchedule() { 55 HealthConnectDailyJobs.schedule(mContext, UserHandle.getUserHandleForUid(0)); 56 verify(mJobScheduler, times(1)).schedule(any()); 57 HealthConnectDailyJobs.schedule(mContext, UserHandle.getUserHandleForUid(1)); 58 verify(mJobScheduler, times(2)).schedule(any()); 59 HealthConnectDailyJobs.cancelAllJobs(mContext); 60 verify(mJobScheduler, times(1)).cancelAll(); 61 } 62 } 63