1 /* 2 * Copyright (C) 2022 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.notification; 18 19 import static junit.framework.Assert.assertEquals; 20 import static junit.framework.Assert.assertFalse; 21 import static junit.framework.Assert.assertTrue; 22 23 import static org.mockito.Mockito.times; 24 import static org.mockito.Mockito.verify; 25 26 import android.app.job.JobInfo; 27 import android.app.job.JobParameters; 28 import android.app.job.JobScheduler; 29 import android.testing.AndroidTestingRunner; 30 31 import androidx.test.rule.ServiceTestRule; 32 33 import com.android.server.LocalServices; 34 import com.android.server.UiServiceTestCase; 35 36 import org.junit.Before; 37 import org.junit.Rule; 38 import org.junit.Test; 39 import org.junit.runner.RunWith; 40 import org.mockito.ArgumentCaptor; 41 import org.mockito.Captor; 42 import org.mockito.Mock; 43 44 @RunWith(AndroidTestingRunner.class) 45 public class ReviewNotificationPermissionsJobServiceTest extends UiServiceTestCase { 46 private ReviewNotificationPermissionsJobService mJobService; 47 48 @Mock 49 private JobParameters mJobParams; 50 51 @Captor 52 ArgumentCaptor<JobInfo> mJobInfoCaptor; 53 54 @Mock 55 private JobScheduler mMockJobScheduler; 56 57 @Mock 58 private NotificationManagerInternal mMockNotificationManagerInternal; 59 60 @Rule 61 public final ServiceTestRule mServiceRule = new ServiceTestRule(); 62 63 @Before setUp()64 public void setUp() throws Exception { 65 mJobService = new ReviewNotificationPermissionsJobService(); 66 mContext.addMockSystemService(JobScheduler.class, mMockJobScheduler); 67 68 // add NotificationManagerInternal to LocalServices 69 LocalServices.removeServiceForTest(NotificationManagerInternal.class); 70 LocalServices.addService(NotificationManagerInternal.class, 71 mMockNotificationManagerInternal); 72 } 73 74 @Test testScheduleJob()75 public void testScheduleJob() { 76 final int rescheduleTimeMillis = 350; // arbitrary number 77 78 // attempt to schedule the job 79 ReviewNotificationPermissionsJobService.scheduleJob(mContext, rescheduleTimeMillis); 80 verify(mMockJobScheduler, times(1)).schedule(mJobInfoCaptor.capture()); 81 82 // verify various properties of the job that is passed in to the job scheduler 83 JobInfo jobInfo = mJobInfoCaptor.getValue(); 84 assertEquals(ReviewNotificationPermissionsJobService.JOB_ID, jobInfo.getId()); 85 assertEquals(rescheduleTimeMillis, jobInfo.getMinLatencyMillis()); 86 assertTrue(jobInfo.isPersisted()); // should continue after reboot 87 assertFalse(jobInfo.isPeriodic()); // one time 88 } 89 90 @Test testOnStartJob()91 public void testOnStartJob() { 92 // the job need not be persisted after it does its work, so it'll return 93 // false 94 assertFalse(mJobService.onStartJob(mJobParams)); 95 96 // verify that starting the job causes the notification to be sent 97 verify(mMockNotificationManagerInternal).sendReviewPermissionsNotification(); 98 } 99 } 100