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.Instrumentation; 19 import android.app.admin.DevicePolicyManager; 20 import android.content.ComponentName; 21 import android.content.pm.PackageManager; 22 import android.support.test.uiautomator.UiDevice; 23 import android.test.AndroidTestCase; 24 25 import androidx.test.InstrumentationRegistry; 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 protected DevicePolicyManager mDevicePolicyManager; 38 protected Instrumentation mInstrumentation; 39 protected UiDevice mDevice; 40 protected boolean mHasSecureLockScreen; 41 42 @Override setUp()43 protected void setUp() throws Exception { 44 super.setUp(); 45 46 mInstrumentation = InstrumentationRegistry.getInstrumentation(); 47 mDevice = UiDevice.getInstance(mInstrumentation); 48 mDevicePolicyManager = mContext.getSystemService(DevicePolicyManager.class); 49 mHasSecureLockScreen = mContext.getPackageManager().hasSystemFeature( 50 PackageManager.FEATURE_SECURE_LOCK_SCREEN); 51 assertDeviceOwner(); 52 } 53 assertDeviceOwner()54 private void assertDeviceOwner() { 55 assertNotNull(mDevicePolicyManager); 56 assertTrue(mDevicePolicyManager.isAdminActive(getWho())); 57 assertTrue(mDevicePolicyManager.isDeviceOwnerApp(mContext.getPackageName())); 58 assertFalse(mDevicePolicyManager.isManagedProfile(getWho())); 59 } 60 getWho()61 protected ComponentName getWho() { 62 return BasicAdminReceiver.getComponentName(mContext); 63 } 64 executeShellCommand(String... command)65 protected String executeShellCommand(String... command) throws Exception { 66 return mDevice.executeShellCommand(String.join(" ", command)); 67 } 68 } 69