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.admin.app", "android.admin.app.CtsDeviceAdminReceiver");
47     }
48 
getSecondReceiverComponent()49     static ComponentName getSecondReceiverComponent() {
50         return new ComponentName("android.admin.app", "android.admin.app.CtsDeviceAdminReceiver2");
51     }
52 
testDeviceAdminInfo()53     public void testDeviceAdminInfo() throws Exception {
54         if (!mDeviceAdmin) {
55             Log.w(TAG, "Skipping testDeviceAdminInfo");
56             return;
57         }
58         ResolveInfo resolveInfo = new ResolveInfo();
59         resolveInfo.activityInfo = mPackageManager.getReceiverInfo(mComponent,
60                 PackageManager.GET_META_DATA);
61 
62         DeviceAdminInfo info = new DeviceAdminInfo(mContext, resolveInfo);
63         assertEquals(mComponent, info.getComponent());
64         assertEquals(mComponent.getPackageName(), info.getPackageName());
65         assertEquals(mComponent.getClassName(), info.getReceiverName());
66 
67         assertTrue(info.usesPolicy(DeviceAdminInfo.USES_POLICY_FORCE_LOCK));
68         assertTrue(info.usesPolicy(DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD));
69         assertTrue(info.usesPolicy(DeviceAdminInfo.USES_POLICY_RESET_PASSWORD));
70         assertTrue(info.usesPolicy(DeviceAdminInfo.USES_POLICY_WATCH_LOGIN));
71         assertTrue(info.usesPolicy(DeviceAdminInfo.USES_POLICY_WIPE_DATA));
72 
73         assertEquals("force-lock",
74                 info.getTagForPolicy(DeviceAdminInfo.USES_POLICY_FORCE_LOCK));
75         assertEquals("limit-password",
76                 info.getTagForPolicy(DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD));
77         assertEquals("reset-password",
78                 info.getTagForPolicy(DeviceAdminInfo.USES_POLICY_RESET_PASSWORD));
79         assertEquals("watch-login",
80                 info.getTagForPolicy(DeviceAdminInfo.USES_POLICY_WATCH_LOGIN));
81         assertEquals("wipe-data",
82                 info.getTagForPolicy(DeviceAdminInfo.USES_POLICY_WIPE_DATA));
83     }
84 
testDeviceAdminInfo2()85     public void testDeviceAdminInfo2() throws Exception {
86         if (!mDeviceAdmin) {
87             Log.w(TAG, "Skipping testDeviceAdminInfo2");
88             return;
89         }
90         ResolveInfo resolveInfo = new ResolveInfo();
91         resolveInfo.activityInfo = mPackageManager.getReceiverInfo(mSecondComponent,
92                 PackageManager.GET_META_DATA);
93 
94         DeviceAdminInfo info = new DeviceAdminInfo(mContext, resolveInfo);
95         assertEquals(mSecondComponent, info.getComponent());
96         assertEquals(mSecondComponent.getPackageName(), info.getPackageName());
97         assertEquals(mSecondComponent.getClassName(), info.getReceiverName());
98 
99         assertFalse(info.usesPolicy(DeviceAdminInfo.USES_POLICY_FORCE_LOCK));
100         assertTrue(info.usesPolicy(DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD));
101         assertTrue(info.usesPolicy(DeviceAdminInfo.USES_POLICY_RESET_PASSWORD));
102         assertFalse(info.usesPolicy(DeviceAdminInfo.USES_POLICY_WATCH_LOGIN));
103         assertTrue(info.usesPolicy(DeviceAdminInfo.USES_POLICY_WIPE_DATA));
104 
105         assertEquals("force-lock",
106                 info.getTagForPolicy(DeviceAdminInfo.USES_POLICY_FORCE_LOCK));
107         assertEquals("limit-password",
108                 info.getTagForPolicy(DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD));
109         assertEquals("reset-password",
110                 info.getTagForPolicy(DeviceAdminInfo.USES_POLICY_RESET_PASSWORD));
111         assertEquals("watch-login",
112                 info.getTagForPolicy(DeviceAdminInfo.USES_POLICY_WATCH_LOGIN));
113         assertEquals("wipe-data",
114                 info.getTagForPolicy(DeviceAdminInfo.USES_POLICY_WIPE_DATA));
115     }
116 }
117