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.test.AndroidTestCase; 29 import android.util.DisplayMetrics; 30 import android.util.Log; 31 32 import android.view.WindowManager; 33 import java.util.Arrays; 34 import java.util.Comparator; 35 36 public class FeatureTest extends AndroidTestCase { 37 38 private static final String TAG = "FeatureTest"; 39 40 private PackageManager mPackageManager; 41 private ActivityManager mActivityManager; 42 private WindowManager mWindowManager; 43 private boolean mSupportsDeviceAdmin; 44 private boolean mSupportsManagedProfiles; 45 46 @Override 47 protected void setUp() throws Exception { 48 super.setUp(); 49 mPackageManager = getContext().getPackageManager(); 50 mActivityManager = (ActivityManager)getContext().getSystemService(Context.ACTIVITY_SERVICE); 51 mWindowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); 52 mSupportsDeviceAdmin = 53 mPackageManager.hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN); 54 mSupportsManagedProfiles = 55 mPackageManager.hasSystemFeature(PackageManager.FEATURE_MANAGED_USERS); 56 } 57 58 public void testNoManagedUsersIfLowRamDevice() { 59 if (mPackageManager == null || mActivityManager == null) { 60 Log.w(TAG, "Skipping testNoManagedUsersIfLowRamDevice"); 61 return; 62 } 63 if (mActivityManager.isLowRamDevice()) { 64 assertFalse(mSupportsManagedProfiles); 65 } 66 } 67 68 /** 69 * Test whether device supports managed profiles as required by CDD 70 */ 71 public void testManagedProfileSupported() throws Exception { 72 // Managed profiles only required if device admin feature is supported 73 if (!mSupportsDeviceAdmin) { 74 Log.w(TAG, "Skipping testManagedProfileSupported"); 75 return; 76 } 77 78 if (mSupportsManagedProfiles) { 79 // Managed profiles supported nothing to check. 80 return; 81 } 82 83 // Managed profiles only required for handheld devices 84 if (!isHandheldDevice()) { 85 return; 86 } 87 88 // Skip the tests for non-emulated sdcard 89 if (!Environment.isExternalStorageEmulated()) { 90 return; 91 } 92 93 // Skip the tests for low-RAM devices 94 if (mActivityManager.isLowRamDevice()) { 95 return; 96 } 97 98 fail("Device should support managed profiles, but " 99 + PackageManager.FEATURE_MANAGED_USERS + " is not enabled"); 100 } 101 102 /** 103 * The CDD defines a handheld device as one that has a battery and a screen size between 104 * 2.5 and 8 inches. 105 */ 106 private boolean isHandheldDevice() throws Exception { 107 double screenInches = getScreenSizeInInches(); 108 return deviceHasBattery() && screenInches >= 2.5 && screenInches <= 8.0; 109 } 110 111 private boolean deviceHasBattery() { 112 final Intent batteryInfo = getContext().registerReceiver(null, 113 new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); 114 return batteryInfo.getBooleanExtra(BatteryManager.EXTRA_PRESENT, true); 115 } 116 117 private double getScreenSizeInInches() { 118 DisplayMetrics dm = new DisplayMetrics(); 119 mWindowManager.getDefaultDisplay().getMetrics(dm); 120 double widthInInchesSquared = Math.pow(dm.widthPixels/dm.xdpi,2); 121 double heightInInchesSquared = Math.pow(dm.heightPixels/dm.ydpi,2); 122 return Math.sqrt(widthInInchesSquared + heightInInchesSquared); 123 } 124 } 125