blob: dfa278a34b17fdd959120609b377826a291e97d8 [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;
22import android.content.res.Configuration;
23import android.test.ActivityInstrumentationTestCase2;
24import android.util.DisplayMetrics;
25import android.view.Display;
26import android.view.WindowManager;
Daniel Xie6ac85e02015-04-27 11:24:23 -070027import java.util.HashMap;
28import java.util.Map;
Brian Muramatsu2ab311c2010-12-23 16:01:00 -080029
30/**
31 * {@link ActivityInstrumentationTestCase2} that tests {@link ActivityManager#getMemoryClass()}
32 * by checking that the memory class matches the proper screen density and by launching an
33 * application that attempts to allocate memory on the heap.
34 */
35public class ActivityManagerMemoryClassTest
36 extends ActivityInstrumentationTestCase2<ActivityManagerMemoryClassLaunchActivity> {
37
38 public ActivityManagerMemoryClassTest() {
39 super(ActivityManagerMemoryClassLaunchActivity.class);
40 }
41
Daniel Xie6ac85e02015-04-27 11:24:23 -070042 public static class ExpectedMemorySizesClass {
43 private static final Map<Integer, Integer> expectedMemorySizeForSmallNormalScreen
44 = new HashMap<Integer, Integer>();
45 private static final Map<Integer, Integer> expectedMemorySizeForLargeScreen
46 = new HashMap<Integer, Integer>();
47 private static final Map<Integer, Integer> expectedMemorySizeForXLargeScreen
48 = new HashMap<Integer, Integer>();
49
50 static {
51 expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_LOW, 32);
52 expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_MEDIUM, 32);
53 expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_TV, 48);
54 expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_HIGH, 48);
Daniel Xie15f3d2d2015-04-28 10:17:42 -070055 expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_280, 48);
Adam Powelleb1e0ea2015-06-03 15:51:50 -070056 expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_XHIGH, 48);
57 expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_360, 64);
Daniel Xie6ac85e02015-04-27 11:24:23 -070058 expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_400, 96);
Robert Carr108b4982015-08-13 14:57:40 -070059 expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_420, 112);
Daniel Xie6ac85e02015-04-27 11:24:23 -070060 expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_XXHIGH, 128);
61 expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_560, 192);
62 expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_XXXHIGH, 256);
63 }
64
65 static {
66 expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_LOW, 32);
67 expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_MEDIUM, 64);
68 expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_TV, 80);
69 expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_HIGH, 80);
Daniel Xie15f3d2d2015-04-28 10:17:42 -070070 expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_280, 96);
Adam Powelleb1e0ea2015-06-03 15:51:50 -070071 expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_XHIGH, 128);
72 expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_360, 160);
Daniel Xie6ac85e02015-04-27 11:24:23 -070073 expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_400, 192);
Robert Carr108b4982015-08-13 14:57:40 -070074 expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_420, 228);
Daniel Xie6ac85e02015-04-27 11:24:23 -070075 expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_XXHIGH, 256);
76 expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_560, 384);
77 expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_XXXHIGH, 512);
78 }
79
80 static {
81 expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_LOW, 48);
82 expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_MEDIUM, 80);
83 expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_TV, 96);
84 expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_HIGH, 96);
Daniel Xie15f3d2d2015-04-28 10:17:42 -070085 expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_280, 144);
Adam Powelleb1e0ea2015-06-03 15:51:50 -070086 expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_XHIGH, 192);
87 expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_360, 240);
Daniel Xie6ac85e02015-04-27 11:24:23 -070088 expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_400, 288);
Robert Carr108b4982015-08-13 14:57:40 -070089 expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_420, 336);
Daniel Xie6ac85e02015-04-27 11:24:23 -070090 expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_XXHIGH, 384);
91 expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_560, 576);
92 expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_XXXHIGH, 768);
93 }
94
95 public static Integer getExpectedMemorySize(int screenSize, int screenDensity) {
96 switch (screenSize) {
97 case Configuration.SCREENLAYOUT_SIZE_SMALL:
98 case Configuration.SCREENLAYOUT_SIZE_NORMAL:
99 return expectedMemorySizeForSmallNormalScreen.get(screenDensity);
100 case Configuration.SCREENLAYOUT_SIZE_LARGE:
101 return expectedMemorySizeForLargeScreen.get(screenDensity);
102 case Configuration.SCREENLAYOUT_SIZE_XLARGE:
103 return expectedMemorySizeForXLargeScreen.get(screenDensity);
104 default:
105 throw new IllegalArgumentException("No memory requirement specified "
106 + " for screen layout size " + screenSize);
107 }
108 }
109 }
110
Brian Muramatsu2ab311c2010-12-23 16:01:00 -0800111 public void testGetMemoryClass() throws Exception {
112 int memoryClass = getMemoryClass();
113 int screenDensity = getScreenDensity();
114 int screenSize = getScreenSize();
115 assertMemoryForScreenDensity(memoryClass, screenDensity, screenSize);
116
117 runHeapTestApp(memoryClass);
118 }
119
120 private int getMemoryClass() {
121 Context context = getInstrumentation().getTargetContext();
122 ActivityManager activityManager =
123 (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
124 return activityManager.getMemoryClass();
125 }
126
127 private int getScreenDensity() {
128 Context context = getInstrumentation().getTargetContext();
129 WindowManager windowManager =
130 (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
131 Display display = windowManager.getDefaultDisplay();
132 DisplayMetrics metrics = new DisplayMetrics();
133 display.getMetrics(metrics);
134 return metrics.densityDpi;
135 }
136
137 private int getScreenSize() {
138 Context context = getInstrumentation().getTargetContext();
139 Configuration config = context.getResources().getConfiguration();
140 return config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;
141 }
142
143 private void assertMemoryForScreenDensity(int memoryClass, int screenDensity, int screenSize) {
Daniel Xieb2f7f8a2015-04-29 14:11:40 -0700144 int expectedMinimumMemory = ExpectedMemorySizesClass.getExpectedMemorySize(screenSize,
145 screenDensity);
Brian Muramatsu2ab311c2010-12-23 16:01:00 -0800146
147 assertTrue("Expected to have at least " + expectedMinimumMemory
148 + "mb of memory for screen density " + screenDensity,
149 memoryClass >= expectedMinimumMemory);
150 }
151
152 private void runHeapTestApp(int memoryClass) throws InterruptedException {
153 Intent intent = new Intent();
154 intent.putExtra(ActivityManagerMemoryClassLaunchActivity.MEMORY_CLASS_EXTRA,
155 memoryClass);
156 setActivityIntent(intent);
157 ActivityManagerMemoryClassLaunchActivity activity = getActivity();
158 assertEquals("The test application couldn't allocate memory close to the amount "
159 + " specified by the memory class.", Activity.RESULT_OK, activity.getResult());
160 }
161}