blob: c8ef253e2db062328bfe1e4810af8868946bfca9 [file] [log] [blame]
Brian Muramatsu2ab311c2010-12-23 16:01:00 -08001/*
2 * Copyright (C) 2011 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 */
16package android.app.cts;
17
18import android.app.Activity;
19import android.app.ActivityManager;
20import android.content.Context;
21import android.content.Intent;
Zach Johnson3568bf42015-09-25 01:50:44 -070022import android.content.pm.PackageManager;
Brian Muramatsu2ab311c2010-12-23 16:01:00 -080023import android.content.res.Configuration;
24import android.test.ActivityInstrumentationTestCase2;
25import android.util.DisplayMetrics;
26import android.view.Display;
27import android.view.WindowManager;
Daniel Xie6ac85e02015-04-27 11:24:23 -070028import java.util.HashMap;
29import java.util.Map;
Brian Muramatsu2ab311c2010-12-23 16:01:00 -080030
31/**
32 * {@link ActivityInstrumentationTestCase2} that tests {@link ActivityManager#getMemoryClass()}
33 * by checking that the memory class matches the proper screen density and by launching an
34 * application that attempts to allocate memory on the heap.
35 */
36public class ActivityManagerMemoryClassTest
37 extends ActivityInstrumentationTestCase2<ActivityManagerMemoryClassLaunchActivity> {
38
39 public ActivityManagerMemoryClassTest() {
40 super(ActivityManagerMemoryClassLaunchActivity.class);
41 }
42
Daniel Xie6ac85e02015-04-27 11:24:23 -070043 public static class ExpectedMemorySizesClass {
Zach Johnson3568bf42015-09-25 01:50:44 -070044 private static final Map<Integer, Integer> expectedMemorySizeForWatch
45 = new HashMap<Integer, Integer>();
Daniel Xie6ac85e02015-04-27 11:24:23 -070046 private static final Map<Integer, Integer> expectedMemorySizeForSmallNormalScreen
47 = new HashMap<Integer, Integer>();
48 private static final Map<Integer, Integer> expectedMemorySizeForLargeScreen
49 = new HashMap<Integer, Integer>();
50 private static final Map<Integer, Integer> expectedMemorySizeForXLargeScreen
51 = new HashMap<Integer, Integer>();
52
53 static {
Zach Johnson3568bf42015-09-25 01:50:44 -070054 expectedMemorySizeForWatch.put(DisplayMetrics.DENSITY_LOW, 32);
55 expectedMemorySizeForWatch.put(DisplayMetrics.DENSITY_MEDIUM, 32);
56 expectedMemorySizeForWatch.put(DisplayMetrics.DENSITY_TV, 32);
57 expectedMemorySizeForWatch.put(DisplayMetrics.DENSITY_HIGH, 36);
58 expectedMemorySizeForWatch.put(DisplayMetrics.DENSITY_280, 36);
59 expectedMemorySizeForWatch.put(DisplayMetrics.DENSITY_XHIGH, 48);
60 expectedMemorySizeForWatch.put(DisplayMetrics.DENSITY_360, 48);
61 expectedMemorySizeForWatch.put(DisplayMetrics.DENSITY_400, 56);
62 expectedMemorySizeForWatch.put(DisplayMetrics.DENSITY_420, 64);
63 expectedMemorySizeForWatch.put(DisplayMetrics.DENSITY_XXHIGH, 88);
64 expectedMemorySizeForWatch.put(DisplayMetrics.DENSITY_560, 112);
65 expectedMemorySizeForWatch.put(DisplayMetrics.DENSITY_XXXHIGH, 154);
66 }
67
68 static {
Daniel Xie6ac85e02015-04-27 11:24:23 -070069 expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_LOW, 32);
70 expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_MEDIUM, 32);
71 expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_TV, 48);
72 expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_HIGH, 48);
Daniel Xie15f3d2d2015-04-28 10:17:42 -070073 expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_280, 48);
Adam Powelleb1e0ea2015-06-03 15:51:50 -070074 expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_XHIGH, 48);
75 expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_360, 64);
Daniel Xie6ac85e02015-04-27 11:24:23 -070076 expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_400, 96);
Robert Carr108b4982015-08-13 14:57:40 -070077 expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_420, 112);
Daniel Xie6ac85e02015-04-27 11:24:23 -070078 expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_XXHIGH, 128);
79 expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_560, 192);
80 expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_XXXHIGH, 256);
81 }
82
83 static {
84 expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_LOW, 32);
85 expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_MEDIUM, 64);
86 expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_TV, 80);
87 expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_HIGH, 80);
Daniel Xie15f3d2d2015-04-28 10:17:42 -070088 expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_280, 96);
Adam Powelleb1e0ea2015-06-03 15:51:50 -070089 expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_XHIGH, 128);
90 expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_360, 160);
Daniel Xie6ac85e02015-04-27 11:24:23 -070091 expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_400, 192);
Robert Carr108b4982015-08-13 14:57:40 -070092 expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_420, 228);
Daniel Xie6ac85e02015-04-27 11:24:23 -070093 expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_XXHIGH, 256);
94 expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_560, 384);
95 expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_XXXHIGH, 512);
96 }
97
98 static {
99 expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_LOW, 48);
100 expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_MEDIUM, 80);
101 expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_TV, 96);
102 expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_HIGH, 96);
Daniel Xie15f3d2d2015-04-28 10:17:42 -0700103 expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_280, 144);
Adam Powelleb1e0ea2015-06-03 15:51:50 -0700104 expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_XHIGH, 192);
105 expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_360, 240);
Daniel Xie6ac85e02015-04-27 11:24:23 -0700106 expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_400, 288);
Robert Carr108b4982015-08-13 14:57:40 -0700107 expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_420, 336);
Daniel Xie6ac85e02015-04-27 11:24:23 -0700108 expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_XXHIGH, 384);
109 expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_560, 576);
110 expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_XXXHIGH, 768);
111 }
112
Zach Johnson3568bf42015-09-25 01:50:44 -0700113 public static Integer getExpectedMemorySize(
114 int screenSize,
115 int screenDensity,
116 boolean isWatch) {
117
118 if (isWatch) {
119 return expectedMemorySizeForWatch.get(screenDensity);
120 }
121
Daniel Xie6ac85e02015-04-27 11:24:23 -0700122 switch (screenSize) {
123 case Configuration.SCREENLAYOUT_SIZE_SMALL:
124 case Configuration.SCREENLAYOUT_SIZE_NORMAL:
125 return expectedMemorySizeForSmallNormalScreen.get(screenDensity);
126 case Configuration.SCREENLAYOUT_SIZE_LARGE:
127 return expectedMemorySizeForLargeScreen.get(screenDensity);
128 case Configuration.SCREENLAYOUT_SIZE_XLARGE:
129 return expectedMemorySizeForXLargeScreen.get(screenDensity);
130 default:
131 throw new IllegalArgumentException("No memory requirement specified "
132 + " for screen layout size " + screenSize);
133 }
134 }
135 }
136
Brian Muramatsu2ab311c2010-12-23 16:01:00 -0800137 public void testGetMemoryClass() throws Exception {
138 int memoryClass = getMemoryClass();
139 int screenDensity = getScreenDensity();
140 int screenSize = getScreenSize();
141 assertMemoryForScreenDensity(memoryClass, screenDensity, screenSize);
142
143 runHeapTestApp(memoryClass);
144 }
145
146 private int getMemoryClass() {
147 Context context = getInstrumentation().getTargetContext();
148 ActivityManager activityManager =
149 (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
150 return activityManager.getMemoryClass();
151 }
152
153 private int getScreenDensity() {
154 Context context = getInstrumentation().getTargetContext();
155 WindowManager windowManager =
156 (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
157 Display display = windowManager.getDefaultDisplay();
158 DisplayMetrics metrics = new DisplayMetrics();
159 display.getMetrics(metrics);
160 return metrics.densityDpi;
161 }
162
163 private int getScreenSize() {
164 Context context = getInstrumentation().getTargetContext();
165 Configuration config = context.getResources().getConfiguration();
166 return config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;
167 }
168
169 private void assertMemoryForScreenDensity(int memoryClass, int screenDensity, int screenSize) {
Zach Johnson3568bf42015-09-25 01:50:44 -0700170 Context context = getInstrumentation().getTargetContext();
171 boolean isWatch =
172 context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WATCH);
173 int expectedMinimumMemory =
174 ExpectedMemorySizesClass.getExpectedMemorySize(screenSize, screenDensity, isWatch);
Brian Muramatsu2ab311c2010-12-23 16:01:00 -0800175
176 assertTrue("Expected to have at least " + expectedMinimumMemory
177 + "mb of memory for screen density " + screenDensity,
178 memoryClass >= expectedMinimumMemory);
179 }
180
181 private void runHeapTestApp(int memoryClass) throws InterruptedException {
182 Intent intent = new Intent();
183 intent.putExtra(ActivityManagerMemoryClassLaunchActivity.MEMORY_CLASS_EXTRA,
184 memoryClass);
185 setActivityIntent(intent);
186 ActivityManagerMemoryClassLaunchActivity activity = getActivity();
187 assertEquals("The test application couldn't allocate memory close to the amount "
188 + " specified by the memory class.", Activity.RESULT_OK, activity.getResult());
189 }
190}