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.deviceowner; 17 18 import android.app.admin.DevicePolicyManager; 19 import android.content.ComponentName; 20 import android.test.AndroidTestCase; 21 22 /** 23 * Base class for affiliated profile-owner based tests. 24 * 25 * This class handles making sure that the test is the affiliated profile owner and that it has an 26 * active admin registered, so that all tests may assume these are done. The admin component can be 27 * accessed through {@link #getWho()}. 28 */ 29 public abstract class BaseAffiliatedProfileOwnerTest extends AndroidTestCase { 30 31 protected DevicePolicyManager mDevicePolicyManager; 32 33 @Override setUp()34 protected void setUp() throws Exception { 35 super.setUp(); 36 37 mDevicePolicyManager = mContext.getSystemService(DevicePolicyManager.class); 38 assertDeviceOrAffiliatedProfileOwner(); 39 } 40 assertDeviceOrAffiliatedProfileOwner()41 private void assertDeviceOrAffiliatedProfileOwner() { 42 assertNotNull(mDevicePolicyManager); 43 assertTrue(mDevicePolicyManager.isAdminActive(getWho())); 44 boolean isDeviceOwner = mDevicePolicyManager.isDeviceOwnerApp(mContext.getPackageName()); 45 boolean isAffiliatedProfileOwner = mDevicePolicyManager.isProfileOwnerApp( 46 mContext.getPackageName()) 47 && mDevicePolicyManager.isAffiliatedUser(); 48 assertTrue(isDeviceOwner || isAffiliatedProfileOwner); 49 } 50 getWho()51 protected ComponentName getWho() { 52 return BasicAdminReceiver.getComponentName(mContext); 53 } 54 } 55