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.packageinstaller; 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.test.InstrumentationTestCase; 23 24 /** 25 * Base class for profile and device based tests. 26 * 27 * This class handles making sure that the test is the profile or device owner and that it has an 28 * active admin registered, so that all tests may assume these are done. 29 */ 30 public class ClearDeviceOwnerTest extends InstrumentationTestCase { 31 32 public static class BasicAdminReceiver extends DeviceAdminReceiver { 33 } 34 35 public static final String PACKAGE_NAME = BasicAdminReceiver.class.getPackage().getName(); 36 public static final ComponentName ADMIN_RECEIVER_COMPONENT = new ComponentName( 37 PACKAGE_NAME, BasicAdminReceiver.class.getName()); 38 39 private DevicePolicyManager mDevicePolicyManager; 40 41 @Override 42 protected void setUp() throws Exception { 43 super.setUp(); 44 45 mDevicePolicyManager = (DevicePolicyManager) 46 getInstrumentation().getContext().getSystemService(Context.DEVICE_POLICY_SERVICE); 47 assertNotNull(mDevicePolicyManager); 48 49 assertTrue(mDevicePolicyManager.isAdminActive(ADMIN_RECEIVER_COMPONENT)); 50 assertTrue("App is not device owner", mDevicePolicyManager.isDeviceOwnerApp(PACKAGE_NAME)); 51 } 52 53 @Override 54 protected void tearDown() throws Exception { 55 mDevicePolicyManager.clearDeviceOwnerApp(PACKAGE_NAME); 56 assertFalse(mDevicePolicyManager.isDeviceOwnerApp(PACKAGE_NAME)); 57 waitForActiveAdminRemoved(ADMIN_RECEIVER_COMPONENT); 58 59 super.tearDown(); 60 } 61 62 // This test clears the device owner and active admin on tearDown(). To be called from the host 63 // side test once a test case is finished. 64 public void testClearDeviceOwner() { 65 } 66 67 private void waitForActiveAdminRemoved(ComponentName cn) throws InterruptedException { 68 for (int i = 0; i < 1000 && mDevicePolicyManager.isAdminActive(cn); i++) { 69 Thread.sleep(100); 70 } 71 assertFalse(mDevicePolicyManager.isAdminActive(cn)); 72 } 73 } 74