• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.job;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertTrue;
22 import static org.mockito.Mockito.doReturn;
23 import static org.mockito.Mockito.mock;
24 
25 import android.app.ActivityManagerInternal;
26 import android.content.Context;
27 import android.os.RemoteException;
28 import android.os.SystemClock;
29 
30 import androidx.test.core.app.ApplicationProvider;
31 import androidx.test.filters.SmallTest;
32 import androidx.test.runner.AndroidJUnit4;
33 
34 import com.android.server.LocalServices;
35 import com.android.server.job.JobConcurrencyManager.GracePeriodObserver;
36 import com.android.server.pm.UserManagerInternal;
37 
38 import org.junit.AfterClass;
39 import org.junit.Before;
40 import org.junit.BeforeClass;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 
44 import java.time.Clock;
45 import java.time.Duration;
46 import java.time.ZoneOffset;
47 
48 @RunWith(AndroidJUnit4.class)
49 @SmallTest
50 public final class GracePeriodObserverTest {
51     private GracePeriodObserver mGracePeriodObserver;
52     private UserManagerInternal mUserManagerInternal;
53     private static final int FIRST_USER = 0;
54 
55     @BeforeClass
setUpOnce()56     public static void setUpOnce() {
57         UserManagerInternal userManagerInternal = mock(UserManagerInternal.class);
58         LocalServices.addService(UserManagerInternal.class, userManagerInternal);
59         ActivityManagerInternal activityManagerInternal = mock(ActivityManagerInternal.class);
60         LocalServices.addService(ActivityManagerInternal.class, activityManagerInternal);
61     }
62 
63     @AfterClass
tearDownOnce()64     public static void tearDownOnce() {
65         LocalServices.removeServiceForTest(UserManagerInternal.class);
66         LocalServices.removeServiceForTest(ActivityManagerInternal.class);
67     }
68 
69     @Before
setUp()70     public void setUp() {
71         final Context context = ApplicationProvider.getApplicationContext();
72         JobSchedulerService.sElapsedRealtimeClock =
73                 Clock.fixed(SystemClock.elapsedRealtimeClock().instant(), ZoneOffset.UTC);
74         doReturn(FIRST_USER)
75                 .when(LocalServices.getService(ActivityManagerInternal.class)).getCurrentUserId();
76         mUserManagerInternal = LocalServices.getService(UserManagerInternal.class);
77         doReturn(true).when(mUserManagerInternal).exists(FIRST_USER);
78         mGracePeriodObserver = new GracePeriodObserver(context);
79     }
80 
81     @Test
testGracePeriod()82     public void testGracePeriod() throws RemoteException {
83         final int oldUser = FIRST_USER;
84         final int newUser = 10;
85         doReturn(true).when(mUserManagerInternal).exists(newUser);
86         mGracePeriodObserver.onUserSwitchComplete(newUser);
87         assertTrue(mGracePeriodObserver.isWithinGracePeriodForUser(oldUser));
88         JobSchedulerService.sElapsedRealtimeClock =
89                 Clock.offset(JobSchedulerService.sElapsedRealtimeClock,
90                         Duration.ofMillis(mGracePeriodObserver.mGracePeriod));
91         assertFalse(mGracePeriodObserver.isWithinGracePeriodForUser(oldUser));
92     }
93 
94     @Test
testCleanUp()95     public void testCleanUp() throws RemoteException {
96         final int removedUser = FIRST_USER;
97         final int newUser = 10;
98         mGracePeriodObserver.onUserSwitchComplete(newUser);
99 
100         final int sizeBefore = mGracePeriodObserver.mGracePeriodExpiration.size();
101         doReturn(false).when(mUserManagerInternal).exists(removedUser);
102 
103         mGracePeriodObserver.onUserRemoved(removedUser);
104         assertEquals(sizeBefore - 1, mGracePeriodObserver.mGracePeriodExpiration.size());
105     }
106 }
107