1 /*
2  * Copyright (C) 2017 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 package com.android.cts.devicepolicy.singleadmin;
17 
18 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE;
19 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME;
20 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_SKIP_ENCRYPTION;
21 
22 import static org.junit.Assert.assertTrue;
23 
24 import android.app.admin.DeviceAdminReceiver;
25 import android.content.ComponentName;
26 import android.content.Context;
27 import android.content.Intent;
28 
29 import androidx.test.InstrumentationRegistry;
30 import androidx.test.filters.SmallTest;
31 
32 import com.android.compatibility.common.util.devicepolicy.provisioning.SilentProvisioningTestManager;
33 
34 import org.junit.Before;
35 import org.junit.Test;
36 
37 @SmallTest
38 public class ProvisioningSingleAdminTest {
39 
40     private static final String ADMIN_RECEIVER_PACKAGE = AdminReceiver.class.getPackage().getName();
41     private static final String ADMIN_RECEIVER_NAME = AdminReceiver.class.getName();
42 
43     private static final ComponentName ADMIN_RECEIVER_COMPONENT = new ComponentName(
44             ADMIN_RECEIVER_PACKAGE, ADMIN_RECEIVER_NAME);
45 
46     private Context mContext;
47 
48     public static class AdminReceiver extends DeviceAdminReceiver {
49         @Override
onProfileProvisioningComplete(Context context, Intent intent)50         public void onProfileProvisioningComplete(Context context, Intent intent) {
51             super.onProfileProvisioningComplete(context, intent);
52             getManager(context).setProfileName(ADMIN_RECEIVER_COMPONENT, "Managed Profile");
53             getManager(context).setProfileEnabled(ADMIN_RECEIVER_COMPONENT);
54         }
55     }
56 
57     @Before
setUp()58     public void setUp() {
59         mContext = InstrumentationRegistry.getTargetContext();
60     }
61 
62     @Test
testManagedProfileProvisioning()63     public void testManagedProfileProvisioning() throws InterruptedException {
64         assertProvisioningSucceeds(createProvisioningIntent());
65     }
66 
createProvisioningIntent()67     private Intent createProvisioningIntent() {
68         return new Intent(ACTION_PROVISION_MANAGED_PROFILE)
69                 .putExtra(EXTRA_PROVISIONING_SKIP_ENCRYPTION, true)
70                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME, ADMIN_RECEIVER_PACKAGE);
71     }
72 
assertProvisioningSucceeds(Intent intent)73     private void assertProvisioningSucceeds(Intent intent) throws InterruptedException {
74         SilentProvisioningTestManager provisioningMgr = new SilentProvisioningTestManager(mContext);
75         assertTrue(provisioningMgr.startProvisioningAndWait(intent));
76     }
77 }
78