1 /*
2  * Copyright (C) 2014 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.managedprofile;
17 
18 import android.app.admin.DeviceAdminReceiver;
19 import android.app.admin.DevicePolicyManager;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.pm.PackageManager;
23 import android.test.InstrumentationTestCase;
24 
25 /**
26  * Base class for profile-owner based tests.
27  *
28  * This class handles making sure that the test is the profile owner and that it has an active admin
29  * registered, so that all tests may assume these are done.
30  */
31 public class BaseManagedProfileTest extends InstrumentationTestCase {
32 
33     public static class BasicAdminReceiver extends DeviceAdminReceiver {
34     }
35 
36     public static final ComponentName ADMIN_RECEIVER_COMPONENT = new ComponentName(
37             BasicAdminReceiver.class.getPackage().getName(), BasicAdminReceiver.class.getName());
38 
39     protected DevicePolicyManager mDevicePolicyManager;
40     protected DevicePolicyManager mParentDevicePolicyManager;
41     protected Context mContext;
42     protected boolean mHasSecureLockScreen;
43 
44     @Override
setUp()45     protected void setUp() throws Exception {
46         super.setUp();
47         mContext = getInstrumentation().getContext();
48 
49         mDevicePolicyManager = (DevicePolicyManager)
50                 mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
51         mParentDevicePolicyManager =
52                 mDevicePolicyManager.getParentProfileInstance(ADMIN_RECEIVER_COMPONENT);
53         assertNotNull(mDevicePolicyManager);
54 
55         assertTrue(mDevicePolicyManager.isAdminActive(ADMIN_RECEIVER_COMPONENT));
56         assertTrue(mDevicePolicyManager.isProfileOwnerApp(
57                 ADMIN_RECEIVER_COMPONENT.getPackageName()));
58         assertTrue(mDevicePolicyManager.isManagedProfile(ADMIN_RECEIVER_COMPONENT));
59 
60         mHasSecureLockScreen = mContext.getPackageManager().hasSystemFeature(
61                 PackageManager.FEATURE_SECURE_LOCK_SCREEN);
62     }
63 
getDevicePolicyManager(boolean isParent)64     protected DevicePolicyManager getDevicePolicyManager(boolean isParent) {
65         return isParent ? mParentDevicePolicyManager : mDevicePolicyManager;
66     }
67 }
68