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 
17 package android.admin.app;
18 
19 import android.app.admin.DevicePolicyManager;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.test.AndroidTestCase;
23 
24 /**
25  * Helper to deactivate Device Admins.
26  */
27 public class DeactivationTest extends AndroidTestCase {
28     private static final String PACKAGE = CtsDeviceAdminReceiver.class.getPackage().getName();
29     private static final ComponentName RECEIVER1 = new ComponentName(PACKAGE,
30             CtsDeviceAdminReceiver.class.getName());
31     private static final ComponentName RECEIVER2 = new ComponentName(PACKAGE,
32             CtsDeviceAdminReceiver2.class.getName());
33 
testDeactivateAdmins()34     public void testDeactivateAdmins() throws Exception {
35         DevicePolicyManager manager = (DevicePolicyManager)
36                 getContext().getSystemService(Context.DEVICE_POLICY_SERVICE);
37         assertNotNull(manager);
38 
39         manager.removeActiveAdmin(RECEIVER1);
40         manager.removeActiveAdmin(RECEIVER2);
41 
42         for (int i = 0; i < 1000 && isActive(manager); i++) {
43             try {
44                 Thread.sleep(100);
45             } catch (InterruptedException e) {
46                 e.printStackTrace();
47             }
48         }
49         assertFalse(isActive(manager));
50     }
51 
isActive(DevicePolicyManager manager)52     private boolean isActive(DevicePolicyManager manager) {
53         return manager.isAdminActive(RECEIVER1) ||
54                 manager.isAdminActive(RECEIVER2);
55     }
56 }