blob: 68efef01f252c41b5bed51a009b8b8c8ba576e76 [file] [log] [blame]
Adam Lesinski9e7b5db2013-09-24 17:54:29 -07001/*
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
17package android.hardware.cts;
18
Nicholas Sauer4b7f7c02015-04-17 16:21:09 -070019import static android.content.res.Configuration.SCREENLAYOUT_SIZE_LARGE;
20import static android.content.res.Configuration.SCREENLAYOUT_SIZE_NORMAL;
21import static android.content.res.Configuration.SCREENLAYOUT_SIZE_SMALL;
22import static android.content.res.Configuration.SCREENLAYOUT_SIZE_XLARGE;
23
24import static android.util.DisplayMetrics.DENSITY_400;
25import static android.util.DisplayMetrics.DENSITY_560;
26import static android.util.DisplayMetrics.DENSITY_HIGH;
27import static android.util.DisplayMetrics.DENSITY_LOW;
28import static android.util.DisplayMetrics.DENSITY_MEDIUM;
29import static android.util.DisplayMetrics.DENSITY_TV;
30import static android.util.DisplayMetrics.DENSITY_XHIGH;
31
Adam Lesinski9e7b5db2013-09-24 17:54:29 -070032import android.app.ActivityManager;
33import android.content.Context;
Nicholas Sauer4b7f7c02015-04-17 16:21:09 -070034import android.content.pm.PackageManager;
35import android.content.res.Configuration;
36import android.os.Build;
Adam Lesinski9e7b5db2013-09-24 17:54:29 -070037import android.test.AndroidTestCase;
Nicholas Sauer4b7f7c02015-04-17 16:21:09 -070038import android.util.DisplayMetrics;
39import android.view.WindowManager;
40import android.util.Log;
Adam Lesinski9e7b5db2013-09-24 17:54:29 -070041
42import java.io.FileInputStream;
43import java.io.IOException;
44import java.io.InputStream;
45import java.util.Scanner;
46import java.util.StringTokenizer;
47
48/**
49 * Tests that devices with low RAM specify themselves as Low RAM devices
50 */
51public class LowRamDeviceTest extends AndroidTestCase {
52
Nicholas Sauer4b7f7c02015-04-17 16:21:09 -070053 private static final long ONE_MEGABYTE = 1048576L;
54 private static final String TAG = "LowRamDeviceTest";
Adam Lesinski9e7b5db2013-09-24 17:54:29 -070055
Nicholas Sauer4b7f7c02015-04-17 16:21:09 -070056 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 Lesinski9e7b5db2013-09-24 17:54:29 -070065 (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE);
66
Nicholas Sauer4b7f7c02015-04-17 16:21:09 -070067 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 Huacdf0522015-06-18 16:42:23 +080095 if (supports64Bit) {
96 assertMinMemoryMb(704);
97 } else {
98 assertMinMemoryMb(424);
99 }
zhongjiecc790132015-06-08 10:43:26 +0800100 } else if (greaterThanDpi(density, DENSITY_560, screenSize,
Nicholas Sauer4b7f7c02015-04-17 16:21:09 -0700101 SCREENLAYOUT_SIZE_NORMAL, SCREENLAYOUT_SIZE_SMALL) ||
zhongjiecc790132015-06-08 10:43:26 +0800102 greaterThanDpi(density, DENSITY_400, screenSize, SCREENLAYOUT_SIZE_LARGE) ||
103 greaterThanDpi(density, DENSITY_XHIGH, screenSize, SCREENLAYOUT_SIZE_XLARGE)) {
Nicholas Sauer4b7f7c02015-04-17 16:21:09 -0700104
105 if (supports64Bit) {
zhongjiecc790132015-06-08 10:43:26 +0800106 assertMinMemoryMb(1824);
Nicholas Sauer4b7f7c02015-04-17 16:21:09 -0700107 } else {
zhongjiecc790132015-06-08 10:43:26 +0800108 assertMinMemoryMb(1344);
Nicholas Sauer4b7f7c02015-04-17 16:21:09 -0700109 }
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 }
zhongjiecc790132015-06-08 10:43:26 +0800120 } else if (greaterThanDpi(density, DENSITY_XHIGH, screenSize,
Nicholas Sauer4b7f7c02015-04-17 16:21:09 -0700121 SCREENLAYOUT_SIZE_NORMAL, SCREENLAYOUT_SIZE_SMALL) ||
zhongjiecc790132015-06-08 10:43:26 +0800122 greaterThanDpi(density, DENSITY_TV, screenSize, SCREENLAYOUT_SIZE_LARGE) ||
123 greaterThanDpi(density, DENSITY_MEDIUM, screenSize, SCREENLAYOUT_SIZE_XLARGE)) {
Nicholas Sauer4b7f7c02015-04-17 16:21:09 -0700124
125 if (supports64Bit) {
zhongjiecc790132015-06-08 10:43:26 +0800126 assertMinMemoryMb(832);
Nicholas Sauer4b7f7c02015-04-17 16:21:09 -0700127 } else {
zhongjiecc790132015-06-08 10:43:26 +0800128 assertMinMemoryMb(512);
Nicholas Sauer4b7f7c02015-04-17 16:21:09 -0700129 }
Adam Lesinski9e7b5db2013-09-24 17:54:29 -0700130 }
131 }
132
133 /**
Nicholas Sauer4b7f7c02015-04-17 16:21:09 -0700134 * @return the total memory accessible by the kernel as defined by
135 * {@code ActivityManager.MemoryInfo}.
Adam Lesinski9e7b5db2013-09-24 17:54:29 -0700136 */
Nicholas Sauer4b7f7c02015-04-17 16:21:09 -0700137 private long getTotalMemory() {
138 ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
139 mActivityManager.getMemoryInfo(memoryInfo);
140 return memoryInfo.totalMem;
Adam Lesinski9e7b5db2013-09-24 17:54:29 -0700141 }
142
Nicholas Sauer4b7f7c02015-04-17 16:21:09 -0700143 /** @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 Hu79d048b2015-05-28 16:45:01 +0800158 boolean lowRam = totalMemoryMb <= 512;
Nicholas Sauer4b7f7c02015-04-17 16:21:09 -0700159 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 Lesinski9e7b5db2013-09-24 17:54:29 -0700194}