Home | History | Annotate | Download | only in com.android.cts.deviceadmin
      1 /*
      2  * Copyright (C) 2016 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.deviceadmin;
     17 
     18 import android.app.admin.DeviceAdminReceiver;
     19 import android.app.admin.DevicePolicyManager;
     20 import android.content.ComponentName;
     21 import android.os.Build;
     22 import android.test.AndroidTestCase;
     23 
     24 public class BaseDeviceAdminTest extends AndroidTestCase {
     25 
     26     public static class AdminReceiver extends DeviceAdminReceiver {
     27     }
     28 
     29     protected String mPackageName;
     30     protected ComponentName mAdminComponent;
     31 
     32     public DevicePolicyManager dpm;
     33 
     34     @Override
     35     protected void setUp() throws Exception {
     36         super.setUp();
     37 
     38         dpm = mContext.getSystemService(DevicePolicyManager.class);
     39         mPackageName = mContext.getPackageName();
     40         mAdminComponent = new ComponentName(mContext, AdminReceiver.class);
     41     }
     42 
     43     /**
     44      * @return the target API level.  Note we don't get it from the package manager information
     45      * but we just parse the last two digits of the package name.  This is to catch a potential
     46      * issue where we forget to change the target API level in the manifest.  (Conversely,
     47      * if we forget to change the package name, we'll catch that in the caller side.)
     48      */
     49     protected int getTargetApiLevel() {
     50         final String packageName = mContext.getPackageName();
     51         return Integer.parseInt(packageName.substring(packageName.length() - 2));
     52     }
     53 
     54     protected boolean isDeviceOwner() {
     55         return dpm.isDeviceOwnerApp(mAdminComponent.getPackageName());
     56     }
     57 
     58     protected void assertDeviceOwner() {
     59         assertTrue("Not device owner", isDeviceOwner());
     60     }
     61 
     62     protected void assertNotDeviceOwner() {
     63         assertFalse("Must not be device owner", isDeviceOwner());
     64     }
     65 
     66     protected void assertNotActiveAdmin() throws Exception {
     67         for (int i = 0; i < 1000 && dpm.isAdminActive(mAdminComponent); i++) {
     68             Thread.sleep(10);
     69         }
     70         assertFalse("Still active admin", dpm.isAdminActive(mAdminComponent));
     71     }
     72 
     73     protected boolean shouldResetPasswordThrow() {
     74         return getTargetApiLevel() > Build.VERSION_CODES.M;
     75     }
     76 
     77     protected void resetComplexPasswordRestrictions() {
     78         dpm.setPasswordMinimumLength(mAdminComponent, 0);
     79         dpm.setPasswordMinimumUpperCase(mAdminComponent, 0);
     80         dpm.setPasswordMinimumLowerCase(mAdminComponent, 0);
     81         dpm.setPasswordMinimumLetters(mAdminComponent, 0);
     82         dpm.setPasswordMinimumNumeric(mAdminComponent, 0);
     83         dpm.setPasswordMinimumSymbols(mAdminComponent, 0);
     84         dpm.setPasswordMinimumNonLetter(mAdminComponent, 0);
     85     }
     86 
     87     protected void clearPassword() {
     88         assertDeviceOwner();
     89 
     90         resetComplexPasswordRestrictions();
     91 
     92         dpm.setPasswordQuality(mAdminComponent, DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED);
     93         assertTrue(dpm.resetPassword("", /* flags =*/ 0));
     94     }
     95 }
     96