Adam Lesinski | 9e7b5db | 2013-09-24 17:54:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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.hardware.cts; |
| 18 | |
Nicholas Sauer | 4b7f7c0 | 2015-04-17 16:21:09 -0700 | [diff] [blame] | 19 | import static android.content.res.Configuration.SCREENLAYOUT_SIZE_LARGE; |
| 20 | import static android.content.res.Configuration.SCREENLAYOUT_SIZE_NORMAL; |
| 21 | import static android.content.res.Configuration.SCREENLAYOUT_SIZE_SMALL; |
| 22 | import static android.content.res.Configuration.SCREENLAYOUT_SIZE_XLARGE; |
| 23 | |
| 24 | import static android.util.DisplayMetrics.DENSITY_400; |
| 25 | import static android.util.DisplayMetrics.DENSITY_560; |
| 26 | import static android.util.DisplayMetrics.DENSITY_HIGH; |
| 27 | import static android.util.DisplayMetrics.DENSITY_LOW; |
| 28 | import static android.util.DisplayMetrics.DENSITY_MEDIUM; |
| 29 | import static android.util.DisplayMetrics.DENSITY_TV; |
| 30 | import static android.util.DisplayMetrics.DENSITY_XHIGH; |
| 31 | |
Adam Lesinski | 9e7b5db | 2013-09-24 17:54:29 -0700 | [diff] [blame] | 32 | import android.app.ActivityManager; |
| 33 | import android.content.Context; |
Nicholas Sauer | 4b7f7c0 | 2015-04-17 16:21:09 -0700 | [diff] [blame] | 34 | import android.content.pm.PackageManager; |
| 35 | import android.content.res.Configuration; |
| 36 | import android.os.Build; |
Adam Lesinski | 9e7b5db | 2013-09-24 17:54:29 -0700 | [diff] [blame] | 37 | import android.test.AndroidTestCase; |
Nicholas Sauer | 4b7f7c0 | 2015-04-17 16:21:09 -0700 | [diff] [blame] | 38 | import android.util.DisplayMetrics; |
| 39 | import android.view.WindowManager; |
| 40 | import android.util.Log; |
Adam Lesinski | 9e7b5db | 2013-09-24 17:54:29 -0700 | [diff] [blame] | 41 | |
| 42 | import java.io.FileInputStream; |
| 43 | import java.io.IOException; |
| 44 | import java.io.InputStream; |
| 45 | import java.util.Scanner; |
| 46 | import java.util.StringTokenizer; |
| 47 | |
| 48 | /** |
| 49 | * Tests that devices with low RAM specify themselves as Low RAM devices |
| 50 | */ |
| 51 | public class LowRamDeviceTest extends AndroidTestCase { |
| 52 | |
Nicholas Sauer | 4b7f7c0 | 2015-04-17 16:21:09 -0700 | [diff] [blame] | 53 | private static final long ONE_MEGABYTE = 1048576L; |
| 54 | private static final String TAG = "LowRamDeviceTest"; |
Adam Lesinski | 9e7b5db | 2013-09-24 17:54:29 -0700 | [diff] [blame] | 55 | |
Nicholas Sauer | 4b7f7c0 | 2015-04-17 16:21:09 -0700 | [diff] [blame] | 56 | private PackageManager mPackageManager; |
| 57 | private ActivityManager mActivityManager; |
| 58 | private DisplayMetrics mDisplayMetrics; |
| 59 | |
| 60 | @Override |
| 61 | protected void setUp() throws Exception { |
| 62 | super.setUp(); |
| 63 | mPackageManager = getContext().getPackageManager(); |
| 64 | mActivityManager = |
Adam Lesinski | 9e7b5db | 2013-09-24 17:54:29 -0700 | [diff] [blame] | 65 | (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE); |
| 66 | |
Nicholas Sauer | 4b7f7c0 | 2015-04-17 16:21:09 -0700 | [diff] [blame] | 67 | mDisplayMetrics = new DisplayMetrics(); |
| 68 | WindowManager windowManager = |
| 69 | (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); |
| 70 | windowManager.getDefaultDisplay().getMetrics(mDisplayMetrics); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Test the devices reported memory to ensure it meets the minimum values described |
| 75 | * in CDD 7.6.1. |
| 76 | */ |
| 77 | public void testMinimumMemory() { |
| 78 | int density = mDisplayMetrics.densityDpi; |
| 79 | Boolean supports64Bit = supportsSixtyFourBit(); |
| 80 | int screenSize = getScreenSize(); |
| 81 | Boolean lowRamDevice = mActivityManager.isLowRamDevice(); |
| 82 | Boolean watch = mPackageManager.hasSystemFeature(PackageManager.FEATURE_WATCH); |
| 83 | |
| 84 | Log.i(TAG, String.format("density=%d, supports64Bit=%s, screenSize=%d, watch=%s", |
| 85 | density, supports64Bit, screenSize, watch)); |
| 86 | |
| 87 | if (watch) { |
| 88 | assertFalse("Device is not expected to be 64-bit", supports64Bit); |
| 89 | assertMinMemoryMb(416); |
| 90 | } else if (lessThanDpi(density, DENSITY_HIGH, screenSize, |
| 91 | SCREENLAYOUT_SIZE_NORMAL, SCREENLAYOUT_SIZE_SMALL) || |
| 92 | lessThanDpi(density, DENSITY_MEDIUM, screenSize, SCREENLAYOUT_SIZE_LARGE) || |
| 93 | lessThanDpi(density, DENSITY_LOW, screenSize, SCREENLAYOUT_SIZE_XLARGE)) { |
| 94 | |
Junjie Hu | acdf052 | 2015-06-18 16:42:23 +0800 | [diff] [blame] | 95 | if (supports64Bit) { |
| 96 | assertMinMemoryMb(704); |
| 97 | } else { |
| 98 | assertMinMemoryMb(424); |
| 99 | } |
zhongjie | cc79013 | 2015-06-08 10:43:26 +0800 | [diff] [blame] | 100 | } else if (greaterThanDpi(density, DENSITY_560, screenSize, |
Nicholas Sauer | 4b7f7c0 | 2015-04-17 16:21:09 -0700 | [diff] [blame] | 101 | SCREENLAYOUT_SIZE_NORMAL, SCREENLAYOUT_SIZE_SMALL) || |
zhongjie | cc79013 | 2015-06-08 10:43:26 +0800 | [diff] [blame] | 102 | greaterThanDpi(density, DENSITY_400, screenSize, SCREENLAYOUT_SIZE_LARGE) || |
| 103 | greaterThanDpi(density, DENSITY_XHIGH, screenSize, SCREENLAYOUT_SIZE_XLARGE)) { |
Nicholas Sauer | 4b7f7c0 | 2015-04-17 16:21:09 -0700 | [diff] [blame] | 104 | |
| 105 | if (supports64Bit) { |
zhongjie | cc79013 | 2015-06-08 10:43:26 +0800 | [diff] [blame] | 106 | assertMinMemoryMb(1824); |
Nicholas Sauer | 4b7f7c0 | 2015-04-17 16:21:09 -0700 | [diff] [blame] | 107 | } else { |
zhongjie | cc79013 | 2015-06-08 10:43:26 +0800 | [diff] [blame] | 108 | assertMinMemoryMb(1344); |
Nicholas Sauer | 4b7f7c0 | 2015-04-17 16:21:09 -0700 | [diff] [blame] | 109 | } |
| 110 | } else if (greaterThanDpi(density, DENSITY_400, screenSize, |
| 111 | SCREENLAYOUT_SIZE_NORMAL, SCREENLAYOUT_SIZE_SMALL) || |
| 112 | greaterThanDpi(density, DENSITY_XHIGH, screenSize, SCREENLAYOUT_SIZE_LARGE) || |
| 113 | greaterThanDpi(density, DENSITY_TV, screenSize, SCREENLAYOUT_SIZE_XLARGE)) { |
| 114 | |
| 115 | if (supports64Bit) { |
| 116 | assertMinMemoryMb(1280); |
| 117 | } else { |
| 118 | assertMinMemoryMb(896); |
| 119 | } |
zhongjie | cc79013 | 2015-06-08 10:43:26 +0800 | [diff] [blame] | 120 | } else if (greaterThanDpi(density, DENSITY_XHIGH, screenSize, |
Nicholas Sauer | 4b7f7c0 | 2015-04-17 16:21:09 -0700 | [diff] [blame] | 121 | SCREENLAYOUT_SIZE_NORMAL, SCREENLAYOUT_SIZE_SMALL) || |
zhongjie | cc79013 | 2015-06-08 10:43:26 +0800 | [diff] [blame] | 122 | greaterThanDpi(density, DENSITY_TV, screenSize, SCREENLAYOUT_SIZE_LARGE) || |
| 123 | greaterThanDpi(density, DENSITY_MEDIUM, screenSize, SCREENLAYOUT_SIZE_XLARGE)) { |
Nicholas Sauer | 4b7f7c0 | 2015-04-17 16:21:09 -0700 | [diff] [blame] | 124 | |
| 125 | if (supports64Bit) { |
zhongjie | cc79013 | 2015-06-08 10:43:26 +0800 | [diff] [blame] | 126 | assertMinMemoryMb(832); |
Nicholas Sauer | 4b7f7c0 | 2015-04-17 16:21:09 -0700 | [diff] [blame] | 127 | } else { |
zhongjie | cc79013 | 2015-06-08 10:43:26 +0800 | [diff] [blame] | 128 | assertMinMemoryMb(512); |
Nicholas Sauer | 4b7f7c0 | 2015-04-17 16:21:09 -0700 | [diff] [blame] | 129 | } |
Adam Lesinski | 9e7b5db | 2013-09-24 17:54:29 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
| 133 | /** |
Nicholas Sauer | 4b7f7c0 | 2015-04-17 16:21:09 -0700 | [diff] [blame] | 134 | * @return the total memory accessible by the kernel as defined by |
| 135 | * {@code ActivityManager.MemoryInfo}. |
Adam Lesinski | 9e7b5db | 2013-09-24 17:54:29 -0700 | [diff] [blame] | 136 | */ |
Nicholas Sauer | 4b7f7c0 | 2015-04-17 16:21:09 -0700 | [diff] [blame] | 137 | private long getTotalMemory() { |
| 138 | ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo(); |
| 139 | mActivityManager.getMemoryInfo(memoryInfo); |
| 140 | return memoryInfo.totalMem; |
Adam Lesinski | 9e7b5db | 2013-09-24 17:54:29 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Nicholas Sauer | 4b7f7c0 | 2015-04-17 16:21:09 -0700 | [diff] [blame] | 143 | /** @return the screen size as defined in {@Configuration}. */ |
| 144 | private int getScreenSize() { |
| 145 | Configuration config = getContext().getResources().getConfiguration(); |
| 146 | return config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK; |
| 147 | } |
| 148 | |
| 149 | /** @return true iff this device supports 64 bit ABIs */ |
| 150 | private static boolean supportsSixtyFourBit() { |
| 151 | return Build.SUPPORTED_64_BIT_ABIS.length > 0; |
| 152 | } |
| 153 | |
| 154 | /** Asserts that the given values conform to the specs in CDD 7.6.1 */ |
| 155 | private void assertMinMemoryMb(long minMb) { |
| 156 | |
| 157 | long totalMemoryMb = getTotalMemory() / ONE_MEGABYTE; |
Junjie Hu | 79d048b | 2015-05-28 16:45:01 +0800 | [diff] [blame] | 158 | boolean lowRam = totalMemoryMb <= 512; |
Nicholas Sauer | 4b7f7c0 | 2015-04-17 16:21:09 -0700 | [diff] [blame] | 159 | boolean lowRamDevice = mActivityManager.isLowRamDevice(); |
| 160 | |
| 161 | Log.i(TAG, String.format("minMb=%,d", minMb)); |
| 162 | Log.i(TAG, String.format("totalMemoryMb=%,d", totalMemoryMb)); |
| 163 | Log.i(TAG, "lowRam=" + lowRam); |
| 164 | Log.i(TAG, "lowRamDevice=" + lowRamDevice); |
| 165 | |
| 166 | assertTrue(String.format("Does not meet minimum memory requirements (CDD 7.6.1)." |
| 167 | + "Found = %d, Minimum = %d", totalMemoryMb, minMb), totalMemoryMb >= minMb); |
| 168 | |
| 169 | assertTrue("Device must specify low RAM property: ro.config.low_ram=true", |
| 170 | !lowRam || (lowRam && lowRamDevice)); |
| 171 | } |
| 172 | |
| 173 | private static boolean lessThanDpi(int actualDensityDpi, int expectedDensityDpi, |
| 174 | int actualScreenSize, int... expectedScreenSizes) { |
| 175 | return actualDensityDpi <= expectedDensityDpi && |
| 176 | contains(expectedScreenSizes, actualScreenSize); |
| 177 | } |
| 178 | |
| 179 | private static boolean greaterThanDpi(int actualDensityDpi, int expectedDensityDpi, |
| 180 | int actualScreenSize, int... expectedScreenSizes) { |
| 181 | return actualDensityDpi >= expectedDensityDpi && |
| 182 | contains(expectedScreenSizes, actualScreenSize); |
| 183 | } |
| 184 | |
| 185 | /** @return true iff the {@code array} contains the {@code target} */ |
| 186 | private static boolean contains(int [] array, int target) { |
| 187 | for(int a : array) { |
| 188 | if (a == target) { |
| 189 | return true; |
| 190 | } |
| 191 | } |
| 192 | return false; |
| 193 | } |
Adam Lesinski | 9e7b5db | 2013-09-24 17:54:29 -0700 | [diff] [blame] | 194 | } |