1 /* 2 * Copyright (C) 2011 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.cts; 18 19 import android.app.admin.DeviceAdminInfo; 20 import android.content.ComponentName; 21 import android.content.pm.PackageManager; 22 import android.content.pm.ResolveInfo; 23 import android.test.AndroidTestCase; 24 import android.util.Log; 25 26 public class DeviceAdminInfoTest extends AndroidTestCase { 27 28 private static final String TAG = DeviceAdminInfoTest.class.getSimpleName(); 29 30 private PackageManager mPackageManager; 31 private ComponentName mComponent; 32 private ComponentName mSecondComponent; 33 private boolean mDeviceAdmin; 34 35 @Override setUp()36 protected void setUp() throws Exception { 37 super.setUp(); 38 mPackageManager = mContext.getPackageManager(); 39 mComponent = getReceiverComponent(); 40 mSecondComponent = getSecondReceiverComponent(); 41 mDeviceAdmin = 42 mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN); 43 } 44 getReceiverComponent()45 static ComponentName getReceiverComponent() { 46 return new ComponentName("android.deviceadmin.cts", 47 "android.deviceadmin.cts.CtsDeviceAdminReceiver"); 48 } 49 getSecondReceiverComponent()50 static ComponentName getSecondReceiverComponent() { 51 return new ComponentName("android.deviceadmin.cts", 52 "android.deviceadmin.cts.CtsDeviceAdminReceiver2"); 53 } 54 testDeviceAdminInfo()55 public void testDeviceAdminInfo() throws Exception { 56 if (!mDeviceAdmin) { 57 Log.w(TAG, "Skipping testDeviceAdminInfo"); 58 return; 59 } 60 ResolveInfo resolveInfo = new ResolveInfo(); 61 resolveInfo.activityInfo = mPackageManager.getReceiverInfo(mComponent, 62 PackageManager.GET_META_DATA); 63 64 DeviceAdminInfo info = new DeviceAdminInfo(mContext, resolveInfo); 65 assertEquals(mComponent, info.getComponent()); 66 assertEquals(mComponent.getPackageName(), info.getPackageName()); 67 assertEquals(mComponent.getClassName(), info.getReceiverName()); 68 69 assertTrue(info.usesPolicy(DeviceAdminInfo.USES_POLICY_FORCE_LOCK)); 70 assertTrue(info.usesPolicy(DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD)); 71 assertTrue(info.usesPolicy(DeviceAdminInfo.USES_POLICY_RESET_PASSWORD)); 72 assertTrue(info.usesPolicy(DeviceAdminInfo.USES_POLICY_WATCH_LOGIN)); 73 assertTrue(info.usesPolicy(DeviceAdminInfo.USES_POLICY_WIPE_DATA)); 74 75 assertEquals("force-lock", 76 info.getTagForPolicy(DeviceAdminInfo.USES_POLICY_FORCE_LOCK)); 77 assertEquals("limit-password", 78 info.getTagForPolicy(DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD)); 79 assertEquals("reset-password", 80 info.getTagForPolicy(DeviceAdminInfo.USES_POLICY_RESET_PASSWORD)); 81 assertEquals("watch-login", 82 info.getTagForPolicy(DeviceAdminInfo.USES_POLICY_WATCH_LOGIN)); 83 assertEquals("wipe-data", 84 info.getTagForPolicy(DeviceAdminInfo.USES_POLICY_WIPE_DATA)); 85 } 86 testDeviceAdminInfo2()87 public void testDeviceAdminInfo2() throws Exception { 88 if (!mDeviceAdmin) { 89 Log.w(TAG, "Skipping testDeviceAdminInfo2"); 90 return; 91 } 92 ResolveInfo resolveInfo = new ResolveInfo(); 93 resolveInfo.activityInfo = mPackageManager.getReceiverInfo(mSecondComponent, 94 PackageManager.GET_META_DATA); 95 96 DeviceAdminInfo info = new DeviceAdminInfo(mContext, resolveInfo); 97 assertEquals(mSecondComponent, info.getComponent()); 98 assertEquals(mSecondComponent.getPackageName(), info.getPackageName()); 99 assertEquals(mSecondComponent.getClassName(), info.getReceiverName()); 100 101 assertFalse(info.usesPolicy(DeviceAdminInfo.USES_POLICY_FORCE_LOCK)); 102 assertTrue(info.usesPolicy(DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD)); 103 assertTrue(info.usesPolicy(DeviceAdminInfo.USES_POLICY_RESET_PASSWORD)); 104 assertFalse(info.usesPolicy(DeviceAdminInfo.USES_POLICY_WATCH_LOGIN)); 105 assertTrue(info.usesPolicy(DeviceAdminInfo.USES_POLICY_WIPE_DATA)); 106 107 assertEquals("force-lock", 108 info.getTagForPolicy(DeviceAdminInfo.USES_POLICY_FORCE_LOCK)); 109 assertEquals("limit-password", 110 info.getTagForPolicy(DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD)); 111 assertEquals("reset-password", 112 info.getTagForPolicy(DeviceAdminInfo.USES_POLICY_RESET_PASSWORD)); 113 assertEquals("watch-login", 114 info.getTagForPolicy(DeviceAdminInfo.USES_POLICY_WATCH_LOGIN)); 115 assertEquals("wipe-data", 116 info.getTagForPolicy(DeviceAdminInfo.USES_POLICY_WIPE_DATA)); 117 } 118 } 119