Home | History | Annotate | Download | only in cts
      1 /**
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
      5  * use this file except in compliance with the License. You may obtain a copy
      6  * 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, WITHOUT
     12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
     13  * License for the specific language governing permissions and limitations
     14  * under the License.
     15  */
     16 package android.content.pm.cts;
     17 
     18 import android.app.ActivityManager;
     19 import android.content.Context;
     20 import android.content.Intent;
     21 import android.content.IntentFilter;
     22 import android.content.pm.FeatureGroupInfo;
     23 import android.content.pm.FeatureInfo;
     24 import android.content.pm.PackageInfo;
     25 import android.content.pm.PackageManager;
     26 import android.os.BatteryManager;
     27 import android.os.Environment;
     28 import android.platform.test.annotations.AppModeFull;
     29 import android.test.AndroidTestCase;
     30 import android.util.DisplayMetrics;
     31 import android.util.Log;
     32 
     33 import android.view.WindowManager;
     34 import java.util.Arrays;
     35 import java.util.Comparator;
     36 
     37 @AppModeFull // TODO(Instant) Figure out which APIs should work.
     38 public class FeatureTest extends AndroidTestCase {
     39 
     40     private static final String TAG = "FeatureTest";
     41 
     42     private PackageManager mPackageManager;
     43     private ActivityManager mActivityManager;
     44     private WindowManager mWindowManager;
     45     private boolean mSupportsDeviceAdmin;
     46     private boolean mSupportsManagedProfiles;
     47 
     48     @Override
     49     protected void setUp() throws Exception {
     50         super.setUp();
     51         mPackageManager = getContext().getPackageManager();
     52         mActivityManager = (ActivityManager)getContext().getSystemService(Context.ACTIVITY_SERVICE);
     53         mWindowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
     54         mSupportsDeviceAdmin =
     55                 mPackageManager.hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN);
     56         mSupportsManagedProfiles =
     57                 mPackageManager.hasSystemFeature(PackageManager.FEATURE_MANAGED_USERS);
     58     }
     59 
     60     public void testNoManagedUsersIfLowRamDevice() {
     61         if (mPackageManager == null || mActivityManager == null) {
     62             Log.w(TAG, "Skipping testNoManagedUsersIfLowRamDevice");
     63             return;
     64         }
     65         if (mActivityManager.isLowRamDevice()) {
     66             assertFalse(mSupportsManagedProfiles);
     67         }
     68     }
     69 
     70     /**
     71      * Test whether device supports managed profiles as required by CDD
     72      */
     73     public void testManagedProfileSupported() throws Exception {
     74         // Managed profiles only required if device admin feature is supported
     75         if (!mSupportsDeviceAdmin) {
     76             Log.w(TAG, "Skipping testManagedProfileSupported");
     77             return;
     78         }
     79 
     80         if (mSupportsManagedProfiles) {
     81             // Managed profiles supported nothing to check.
     82             return;
     83         }
     84 
     85         // Managed profiles only required for handheld devices
     86         if (!isHandheldDevice()) {
     87             return;
     88         }
     89 
     90         // Skip the tests for non-emulated sdcard
     91         if (!Environment.isExternalStorageEmulated()) {
     92             return;
     93         }
     94 
     95         // Skip the tests for low-RAM devices
     96         if (mActivityManager.isLowRamDevice()) {
     97             return;
     98         }
     99 
    100         fail("Device should support managed profiles, but "
    101                 + PackageManager.FEATURE_MANAGED_USERS + " is not enabled");
    102     }
    103 
    104     /**
    105      * The CDD defines a handheld device as one that has a battery and a screen size between
    106      * 2.5 and 8 inches.
    107      */
    108     private boolean isHandheldDevice() throws Exception {
    109         double screenInches = getScreenSizeInInches();
    110         return deviceHasBattery() && screenInches >= 2.5 && screenInches <= 8.0;
    111     }
    112 
    113     private boolean deviceHasBattery() {
    114         final Intent batteryInfo = getContext().registerReceiver(null,
    115                 new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    116         return batteryInfo.getBooleanExtra(BatteryManager.EXTRA_PRESENT, true);
    117     }
    118 
    119     private double getScreenSizeInInches() {
    120         DisplayMetrics dm = new DisplayMetrics();
    121         mWindowManager.getDefaultDisplay().getMetrics(dm);
    122         double widthInInchesSquared = Math.pow(dm.widthPixels/dm.xdpi,2);
    123         double heightInInchesSquared = Math.pow(dm.heightPixels/dm.ydpi,2);
    124         return Math.sqrt(widthInInchesSquared + heightInInchesSquared);
    125     }
    126 }
    127