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.deviceowner; 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.Intent; 23 import android.net.Uri; 24 import android.os.Process; 25 import android.test.AndroidTestCase; 26 27 /** 28 * Base class for device-owner based tests. 29 * 30 * This class handles making sure that the test is the device owner 31 * and that it has an active admin registered, so that all tests may 32 * assume these are done. The admin component can be accessed through 33 * {@link #getWho()}. 34 */ 35 public abstract class BaseDeviceOwnerTest extends AndroidTestCase { 36 37 public static class BasicAdminReceiver extends DeviceAdminReceiver { 38 @Override onChoosePrivateKeyAlias(Context context, Intent intent, int uid, Uri uri, String suggestedAlias)39 public String onChoosePrivateKeyAlias(Context context, Intent intent, int uid, Uri uri, 40 String suggestedAlias) { 41 if (uid != Process.myUid() || uri == null) { 42 return null; 43 } 44 return uri.getQueryParameter("alias"); 45 } 46 } 47 48 public static final String PACKAGE_NAME = BaseDeviceOwnerTest.class.getPackage().getName(); 49 50 protected DevicePolicyManager mDevicePolicyManager; 51 52 @Override setUp()53 protected void setUp() throws Exception { 54 super.setUp(); 55 56 mDevicePolicyManager = (DevicePolicyManager) 57 mContext.getSystemService(Context.DEVICE_POLICY_SERVICE); 58 assertDeviceOwner(mDevicePolicyManager); 59 } 60 assertDeviceOwner(DevicePolicyManager dpm)61 static void assertDeviceOwner(DevicePolicyManager dpm) { 62 assertNotNull(dpm); 63 assertTrue(dpm.isAdminActive(getWho())); 64 assertTrue(dpm.isDeviceOwnerApp(PACKAGE_NAME)); 65 } 66 getWho()67 protected static ComponentName getWho() { 68 return new ComponentName(PACKAGE_NAME, BasicAdminReceiver.class.getName()); 69 } 70 } 71