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 import com.android.bedstead.dpmwrapper.TestAppSystemServiceFactory; 23 24 // TODO (b/174859111): evaluate and refactor dependency on this class to have only 25 // affiliated profile-owner based tests depends on this class. Tests for Device Owner only, 26 // e.g. PackageInstallTest should not depend on this class. Otherwise, tests can easily break 27 // in multi-user setup. 28 /** 29 * Base class for affiliated profile-owner based tests. 30 * 31 * This class handles making sure that the test is the affiliated profile owner and that it has an 32 * active admin registered, so that all tests may assume these are done. The admin component can be 33 * accessed through {@link #getWho()}. 34 */ 35 public abstract class BaseAffiliatedProfileOwnerTest extends AndroidTestCase { 36 37 protected DevicePolicyManager mDevicePolicyManager; 38 39 @Override setUp()40 protected void setUp() throws Exception { 41 super.setUp(); 42 // In headless system user mode, affiliated PO is set on seceondary when DO is setup on 43 // user 0. Therefore, this test will run on user 0. 44 mDevicePolicyManager = TestAppSystemServiceFactory.getDevicePolicyManager(mContext, 45 BasicAdminReceiver.class, /* forDeviceOwner= */ true); 46 assertDeviceOrAffiliatedProfileOwner(); 47 } 48 assertDeviceOrAffiliatedProfileOwner()49 private void assertDeviceOrAffiliatedProfileOwner() { 50 assertNotNull(mDevicePolicyManager); 51 assertTrue(mDevicePolicyManager.isAdminActive(getWho())); 52 boolean isDeviceOwner = mDevicePolicyManager.isDeviceOwnerApp(mContext.getPackageName()); 53 boolean isAffiliatedProfileOwner = mDevicePolicyManager.isProfileOwnerApp( 54 mContext.getPackageName()) 55 && mDevicePolicyManager.isAffiliatedUser(); 56 assertTrue(isDeviceOwner || isAffiliatedProfileOwner); 57 } 58 getWho()59 protected ComponentName getWho() { 60 return BasicAdminReceiver.getComponentName(mContext); 61 } 62 } 63