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