• 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.managedprovisioning;
18 
19 import static android.app.admin.DevicePolicyManager.STATE_USER_PROFILE_FINALIZED;
20 
21 import static org.mockito.Mockito.never;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.when;
24 
25 import android.app.admin.DevicePolicyManager;
26 import android.content.Context;
27 
28 import androidx.test.filters.SmallTest;
29 
30 import com.android.managedprovisioning.finalization.UserProvisioningStateHelper;
31 import com.android.managedprovisioning.manageduser.ManagedUserRemovalUtils;
32 import com.android.managedprovisioning.ota.CrossProfileAppsPregrantController;
33 import com.android.managedprovisioning.preprovisioning.EncryptionController;
34 
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 
40 // TODO(b/185377693): Update tests to use the new testing library so that we don't have to rely
41 //  on mocks
42 @SmallTest
43 public class BootReminderTest {
44 
45     @Mock
46     private Context mContext;
47 
48     @Mock
49     private DevicePolicyManager mDevicePolicyManager;
50 
51     @Mock
52     private UserProvisioningStateHelper mUserProvisioningStateHelper;
53 
54     @Mock
55     private EncryptionController mEncryptionController;
56 
57     @Mock
58     private CrossProfileAppsPregrantController mCrossProfileAppsPregrantController;
59 
60     private final ManagedUserRemovalUtils mManagedUserRemovalUtils = new ManagedUserRemovalUtils();
61 
62     @Before
setUp()63     public void setUp() {
64         MockitoAnnotations.initMocks(this);
65         when(mContext.getSystemServiceName(DevicePolicyManager.class))
66                 .thenReturn(Context.DEVICE_POLICY_SERVICE);
67         when(mContext.getSystemService(DevicePolicyManager.class))
68                 .thenReturn(mDevicePolicyManager);
69     }
70 
71     @Test
onReceive_hasWorkProfile_userProvisioningStateIntact()72     public void onReceive_hasWorkProfile_userProvisioningStateIntact() {
73         BootReminder bootReminder = createBootReminderWithWorkProfile();
74 
75         bootReminder.onReceive(mContext, /* intent */ null);
76 
77         verify(mUserProvisioningStateHelper, never()).resetPrimaryUserProvisioningState();
78     }
79 
80     @Test
onReceive_userProvisioningStateUnmanaged_userProvisioningStateIntact()81     public void onReceive_userProvisioningStateUnmanaged_userProvisioningStateIntact() {
82         BootReminder bootReminder = createBootReminder();
83 
84         bootReminder.onReceive(mContext, /* intent */ null);
85 
86         verify(mUserProvisioningStateHelper, never()).resetPrimaryUserProvisioningState();
87     }
88 
89     @Test
onReceive_userProvisioningStateProfileFinalized_userProvisioningStateReset()90     public void onReceive_userProvisioningStateProfileFinalized_userProvisioningStateReset() {
91         BootReminder bootReminder = createBootReminder();
92         setUserProvisioningStateFinalized();
93 
94         bootReminder.onReceive(mContext, /* intent */ null);
95 
96         verify(mUserProvisioningStateHelper).resetPrimaryUserProvisioningState();
97     }
98 
setUserProvisioningStateFinalized()99     private void setUserProvisioningStateFinalized() {
100         when(mDevicePolicyManager.getUserProvisioningState())
101             .thenReturn(STATE_USER_PROFILE_FINALIZED);
102     }
103 
createBootReminder()104     private BootReminder createBootReminder() {
105         return createBootReminderInternal(/* hasManagedProfile */ false);
106     }
107 
createBootReminderWithWorkProfile()108     private BootReminder createBootReminderWithWorkProfile() {
109         return createBootReminderInternal(/* hasManagedProfile */ true);
110     }
111 
createBootReminderInternal(boolean hasManagedProfile)112     private BootReminder createBootReminderInternal(boolean hasManagedProfile) {
113         return new BootReminder(
114                 context -> hasManagedProfile,
115                 context -> mUserProvisioningStateHelper,
116                 context -> mEncryptionController,
117                 context -> mCrossProfileAppsPregrantController,
118                 mManagedUserRemovalUtils);
119     }
120 }
121