Home | History | Annotate | Download | only in managedprofile
      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.managedprofile;
     17 
     18 import android.app.admin.DevicePolicyManager;
     19 import android.content.ComponentName;
     20 import android.content.Context;
     21 import android.test.AndroidTestCase;
     22 
     23 /**
     24  * This test executes helper tasks as active device admin in the primary user.
     25  */
     26 public class PrimaryUserAdminHelper extends AndroidTestCase {
     27 
     28     private DevicePolicyManager mDpm;
     29 
     30     @Override
     31     protected void setUp() throws Exception {
     32         super.setUp();
     33         mDpm = (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
     34     }
     35 
     36     /**
     37      * Device admin can only be deactivated by itself and this test should be executed before the
     38      * device admin package can be uninstalled.
     39      */
     40     public void testClearDeviceAdmin() throws Exception {
     41         ComponentName cn = PrimaryUserDeviceAdmin.ADMIN_RECEIVER_COMPONENT;
     42         if (mDpm.isAdminActive(cn)) {
     43             mDpm.removeActiveAdmin(cn);
     44             // Wait until device admin is not active (with 2 minutes timeout).
     45             for (int i = 0; i < 2 * 60 && mDpm.isAdminActive(cn); i++) {
     46                 Thread.sleep(1000);  // 1 second.
     47             }
     48         }
     49         assertFalse("Clear device admin failed", mDpm.isAdminActive(cn));
     50     }
     51 }
     52