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