Home | History | Annotate | Download | only in deviceowner
      1 /*
      2  * Copyright (C) 2014 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.deviceowner;
     17 
     18 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_DEVICE;
     19 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_LEAVE_ALL_SYSTEM_APPS_ENABLED;
     20 import static android.content.pm.PackageManager.MATCH_SYSTEM_ONLY;
     21 import static java.util.stream.Collectors.toList;
     22 
     23 import android.app.admin.DevicePolicyManager;
     24 import android.content.Intent;
     25 import android.content.pm.ApplicationInfo;
     26 import android.content.pm.PackageManager;
     27 import android.util.Log;
     28 
     29 import com.android.compatibility.common.util.devicepolicy.provisioning.SilentProvisioningTestManager;
     30 import java.util.ArrayList;
     31 import java.util.List;
     32 
     33 
     34 public class DeviceOwnerProvisioningTest extends BaseDeviceOwnerTest {
     35     private static final String TAG = "DeviceOwnerProvisioningTest";
     36 
     37     private List<String> mEnabledAppsBeforeTest;
     38     private PackageManager mPackageManager;
     39     private DevicePolicyManager mDpm;
     40 
     41     @Override
     42     protected void setUp() throws Exception {
     43         super.setUp();
     44 
     45         mPackageManager = getContext().getPackageManager();
     46         mDpm = getContext().getSystemService(DevicePolicyManager.class);
     47         mEnabledAppsBeforeTest = getSystemPackageNameList();
     48     }
     49 
     50     @Override
     51     protected void tearDown() throws Exception {
     52         enableUninstalledApp();
     53         super.tearDown();
     54     }
     55 
     56     public void testProvisionDeviceOwner() throws Exception {
     57         deviceOwnerProvision(getBaseProvisioningIntent());
     58     }
     59 
     60     public void testProvisionDeviceOwner_withAllSystemAppsEnabled() throws Exception {
     61         List<String> systemAppsBefore = getSystemPackageNameList();
     62 
     63         Intent intent = getBaseProvisioningIntent()
     64                 .putExtra(EXTRA_PROVISIONING_LEAVE_ALL_SYSTEM_APPS_ENABLED, true);
     65         deviceOwnerProvision(intent);
     66 
     67         List<String> systemAppsAfter = getSystemPackageNameList();
     68         assertTrue(systemAppsBefore.equals(systemAppsAfter));
     69     }
     70 
     71     private void enableUninstalledApp() {
     72         final List<String> currentEnabledApps = getSystemPackageNameList();
     73 
     74         final List<String> disabledApps = new ArrayList<String>(mEnabledAppsBeforeTest);
     75         disabledApps.removeAll(currentEnabledApps);
     76 
     77         for (String disabledSystemApp : disabledApps) {
     78             Log.i(TAG, "enable app : " + disabledSystemApp);
     79             mDevicePolicyManager.enableSystemApp(BasicAdminReceiver.getComponentName(getContext()),
     80                     disabledSystemApp);
     81         }
     82     }
     83 
     84     private Intent getBaseProvisioningIntent() {
     85         return new Intent(ACTION_PROVISION_MANAGED_DEVICE)
     86                 .putExtra(DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME,
     87                         BasicAdminReceiver.getComponentName(getContext()))
     88                 .putExtra(DevicePolicyManager.EXTRA_PROVISIONING_SKIP_ENCRYPTION, true);
     89     }
     90 
     91     private void deviceOwnerProvision(Intent intent) throws Exception {
     92         SilentProvisioningTestManager provisioningManager =
     93                 new SilentProvisioningTestManager(getContext());
     94         assertTrue(provisioningManager.startProvisioningAndWait(intent));
     95         Log.i(TAG, "device owner provisioning successful");
     96         assertTrue(mDpm.isDeviceOwnerApp(getContext().getPackageName()));
     97         Log.i(TAG, "device owner app: " + getContext().getPackageName());
     98     }
     99 
    100     private List<String> getPackageNameList() {
    101         return getPackageNameList(0 /* Default flags */);
    102     }
    103 
    104     private List<String> getSystemPackageNameList() {
    105         return getPackageNameList(MATCH_SYSTEM_ONLY);
    106     }
    107 
    108     private List<String> getPackageNameList(int flags) {
    109         return mPackageManager.getInstalledApplications(flags)
    110                 .stream()
    111                 .map((ApplicationInfo appInfo) -> appInfo.packageName)
    112                 .sorted()
    113                 .collect(toList());
    114     }
    115 }
    116