1 /*
2  * Copyright (C) 2015 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.deviceandprofileowner;
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.PackageInfo;
23 import android.content.pm.PackageManager;
24 import android.os.UserManager;
25 import android.test.InstrumentationTestCase;
26 
27 /**
28  * Base class for profile and device based tests.
29  *
30  * This class handles making sure that the test is the profile or device owner and that it has an
31  * active admin registered, so that all tests may assume these are done.
32  */
33 public class BaseDeviceAdminTest extends InstrumentationTestCase {
34 
35     public static class BasicAdminReceiver extends DeviceAdminReceiver {
36     }
37 
38     public static final String PACKAGE_NAME = BasicAdminReceiver.class.getPackage().getName();
39     public static final ComponentName ADMIN_RECEIVER_COMPONENT = new ComponentName(
40             PACKAGE_NAME, BasicAdminReceiver.class.getName());
41 
42     protected DevicePolicyManager mDevicePolicyManager;
43     protected UserManager mUserManager;
44     protected Context mContext;
45 
46     @Override
setUp()47     protected void setUp() throws Exception {
48         super.setUp();
49         mContext = getInstrumentation().getContext();
50 
51         mDevicePolicyManager = mContext.getSystemService(DevicePolicyManager.class);
52         assertNotNull(mDevicePolicyManager);
53 
54         mUserManager = mContext.getSystemService(UserManager.class);
55         assertNotNull(mUserManager);
56 
57         assertTrue(mDevicePolicyManager.isAdminActive(ADMIN_RECEIVER_COMPONENT));
58         assertTrue("App is neither device nor profile owner",
59                 mDevicePolicyManager.isProfileOwnerApp(PACKAGE_NAME) ||
60                 mDevicePolicyManager.isDeviceOwnerApp(PACKAGE_NAME));
61     }
62 
getTargetApiLevel()63     protected int getTargetApiLevel() throws Exception {
64         final PackageManager pm = mContext.getPackageManager();
65         final PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(), /* flags =*/ 0);
66         return pi.applicationInfo.targetSdkVersion;
67     }
68 }
69