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