1 /*
2  * Copyright 2016, 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.finalization;
18 
19 import static android.app.admin.DeviceAdminReceiver.ACTION_PROFILE_PROVISIONING_COMPLETE;
20 
21 import static org.mockito.Mockito.verify;
22 import static org.mockito.Mockito.when;
23 
24 import android.app.admin.DevicePolicyManager;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.os.UserHandle;
28 import android.test.AndroidTestCase;
29 
30 import androidx.test.filters.SmallTest;
31 
32 import com.android.managedprovisioning.common.Utils;
33 
34 import org.mockito.ArgumentCaptor;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 
38 /**
39  * Unit tests for {@link DpcReceivedSuccessReceiver}.
40  */
41 public class DpcReceivedSuccessReceiverTest extends AndroidTestCase {
42     private static final String TEST_MDM_PACKAGE_NAME = "mdm.package.name";
43     private static final Intent TEST_INTENT = new Intent(ACTION_PROFILE_PROVISIONING_COMPLETE);
44     private static final UserHandle MANAGED_PROFILE_USER_HANDLE = UserHandle.of(123);
45 
46     @Mock private Context mContext;
47     @Mock private Utils mUtils;
48     @Mock private DevicePolicyManager mDevicePolicyManager;
49 
50     @Override
setUp()51     public void setUp() {
52         // this is necessary for mockito to work
53         System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString());
54         MockitoAnnotations.initMocks(this);
55 
56         when(mContext.getSystemServiceName(DevicePolicyManager.class))
57                 .thenReturn(Context.DEVICE_POLICY_SERVICE);
58         when(mContext.getSystemService(Context.DEVICE_POLICY_SERVICE))
59                 .thenReturn(mDevicePolicyManager);
60     }
61 
62     @SmallTest
testReceiver()63     public void testReceiver() {
64         // GIVEN that no account migration occurred during provisioning
65         final DpcReceivedSuccessReceiver receiver = new DpcReceivedSuccessReceiver(
66                 /* migratedAccount= */ null, MANAGED_PROFILE_USER_HANDLE,
67                 TEST_MDM_PACKAGE_NAME, /* callback */ null);
68 
69         // WHEN the profile provisioning complete intent was received by the DPC
70         receiver.onReceive(mContext, TEST_INTENT);
71 
72         // THEN the system should be told to finalize the provisioning
73         ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
74         verify(mDevicePolicyManager).finalizeWorkProfileProvisioning(
75                 MANAGED_PROFILE_USER_HANDLE, /* migratedAccount= */ null);
76     }
77 }
78