1 /*
2  * Copyright 2017, 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 com.android.cts.deviceandprofileowner;
18 
19 import android.os.AsyncTask;
20 
21 import java.util.concurrent.Semaphore;
22 import java.util.concurrent.TimeUnit;
23 
24 /**
25  * Test class that calls DPM.clearApplicationUserData and verifies that it doesn't time out.
26  */
27 public class ClearApplicationDataTest extends BaseDeviceAdminTest {
28     private static final String TEST_PKG = "com.android.cts.intent.receiver";
29     private static final String DEVICE_ADMIN_PKG = "com.android.cts.deviceandprofileowner";
30     private static final Semaphore mSemaphore = new Semaphore(0);
31     private static final long CLEAR_APPLICATION_DATA_TIMEOUT_S = 10;
32 
testClearApplicationData_testPkg()33     public void testClearApplicationData_testPkg() throws Exception {
34         clearApplicationDataTest(TEST_PKG, /* shouldSucceed */ true);
35     }
36 
testClearApplicationData_deviceProvisioning()37     public void testClearApplicationData_deviceProvisioning() throws Exception {
38         String deviceProvisioningPackageName = getDeviceProvisioningPackageName();
39         if (deviceProvisioningPackageName == null) {
40             return;
41         }
42         clearApplicationDataTest(deviceProvisioningPackageName, /* shouldSucceed */ false);
43     }
44 
testClearApplicationData_activeAdmin()45     public void testClearApplicationData_activeAdmin() throws Exception {
46         clearApplicationDataTest(DEVICE_ADMIN_PKG, /* shouldSucceed */ false);
47     }
48 
clearApplicationDataTest(String packageName, boolean shouldSucceed)49     private void clearApplicationDataTest(String packageName, boolean shouldSucceed)
50             throws Exception {
51         mDevicePolicyManager.clearApplicationUserData(ADMIN_RECEIVER_COMPONENT,
52                 packageName, AsyncTask.THREAD_POOL_EXECUTOR,
53                 (String pkg, boolean succeeded) -> {
54                     assertEquals(packageName, pkg);
55                     assertEquals(shouldSucceed, succeeded);
56                     mSemaphore.release();
57                 });
58         assertTrue("Clearing application data took too long",
59                 mSemaphore.tryAcquire(CLEAR_APPLICATION_DATA_TIMEOUT_S, TimeUnit.SECONDS));
60     }
61 
getDeviceProvisioningPackageName()62     private String getDeviceProvisioningPackageName() {
63         final int provisioning_app_id = mContext.getResources().getIdentifier(
64                 "config_deviceProvisioningPackage", "string", "android");
65         if (provisioning_app_id > 0) {
66             return mContext.getResources().getString(provisioning_app_id);
67         } else {
68             return null;
69         }
70     }
71 }
72