Brian Muramatsu | 2ab311c | 2010-12-23 16:01:00 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package android.app.cts; |
| 17 | |
| 18 | import android.app.Activity; |
| 19 | import android.app.ActivityManager; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
Zach Johnson | 3568bf4 | 2015-09-25 01:50:44 -0700 | [diff] [blame] | 22 | import android.content.pm.PackageManager; |
Brian Muramatsu | 2ab311c | 2010-12-23 16:01:00 -0800 | [diff] [blame] | 23 | import android.content.res.Configuration; |
| 24 | import android.test.ActivityInstrumentationTestCase2; |
| 25 | import android.util.DisplayMetrics; |
| 26 | import android.view.Display; |
| 27 | import android.view.WindowManager; |
Daniel Xie | 6ac85e0 | 2015-04-27 11:24:23 -0700 | [diff] [blame] | 28 | import java.util.HashMap; |
| 29 | import java.util.Map; |
Brian Muramatsu | 2ab311c | 2010-12-23 16:01:00 -0800 | [diff] [blame] | 30 | |
| 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 | */ |
| 36 | public class ActivityManagerMemoryClassTest |
| 37 | extends ActivityInstrumentationTestCase2<ActivityManagerMemoryClassLaunchActivity> { |
| 38 | |
| 39 | public ActivityManagerMemoryClassTest() { |
| 40 | super(ActivityManagerMemoryClassLaunchActivity.class); |
| 41 | } |
| 42 | |
Daniel Xie | 6ac85e0 | 2015-04-27 11:24:23 -0700 | [diff] [blame] | 43 | public static class ExpectedMemorySizesClass { |
Zach Johnson | 3568bf4 | 2015-09-25 01:50:44 -0700 | [diff] [blame] | 44 | private static final Map<Integer, Integer> expectedMemorySizeForWatch |
| 45 | = new HashMap<Integer, Integer>(); |
Daniel Xie | 6ac85e0 | 2015-04-27 11:24:23 -0700 | [diff] [blame] | 46 | 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 Johnson | 3568bf4 | 2015-09-25 01:50:44 -0700 | [diff] [blame] | 54 | 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 Xie | 6ac85e0 | 2015-04-27 11:24:23 -0700 | [diff] [blame] | 69 | 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 Xie | 15f3d2d | 2015-04-28 10:17:42 -0700 | [diff] [blame] | 73 | expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_280, 48); |
Adam Powell | eb1e0ea | 2015-06-03 15:51:50 -0700 | [diff] [blame] | 74 | expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_XHIGH, 48); |
| 75 | expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_360, 64); |
Daniel Xie | 6ac85e0 | 2015-04-27 11:24:23 -0700 | [diff] [blame] | 76 | expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_400, 96); |
Robert Carr | 108b498 | 2015-08-13 14:57:40 -0700 | [diff] [blame] | 77 | expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_420, 112); |
Daniel Xie | 6ac85e0 | 2015-04-27 11:24:23 -0700 | [diff] [blame] | 78 | 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 Xie | 15f3d2d | 2015-04-28 10:17:42 -0700 | [diff] [blame] | 88 | expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_280, 96); |
Adam Powell | eb1e0ea | 2015-06-03 15:51:50 -0700 | [diff] [blame] | 89 | expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_XHIGH, 128); |
| 90 | expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_360, 160); |
Daniel Xie | 6ac85e0 | 2015-04-27 11:24:23 -0700 | [diff] [blame] | 91 | expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_400, 192); |
Robert Carr | 108b498 | 2015-08-13 14:57:40 -0700 | [diff] [blame] | 92 | expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_420, 228); |
Daniel Xie | 6ac85e0 | 2015-04-27 11:24:23 -0700 | [diff] [blame] | 93 | 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 Xie | 15f3d2d | 2015-04-28 10:17:42 -0700 | [diff] [blame] | 103 | expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_280, 144); |
Adam Powell | eb1e0ea | 2015-06-03 15:51:50 -0700 | [diff] [blame] | 104 | expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_XHIGH, 192); |
| 105 | expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_360, 240); |
Daniel Xie | 6ac85e0 | 2015-04-27 11:24:23 -0700 | [diff] [blame] | 106 | expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_400, 288); |
Robert Carr | 108b498 | 2015-08-13 14:57:40 -0700 | [diff] [blame] | 107 | expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_420, 336); |
Daniel Xie | 6ac85e0 | 2015-04-27 11:24:23 -0700 | [diff] [blame] | 108 | expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_XXHIGH, 384); |
| 109 | expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_560, 576); |
| 110 | expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_XXXHIGH, 768); |
| 111 | } |
| 112 | |
Zach Johnson | 3568bf4 | 2015-09-25 01:50:44 -0700 | [diff] [blame] | 113 | 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 Xie | 6ac85e0 | 2015-04-27 11:24:23 -0700 | [diff] [blame] | 122 | 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 Muramatsu | 2ab311c | 2010-12-23 16:01:00 -0800 | [diff] [blame] | 137 | 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 Johnson | 3568bf4 | 2015-09-25 01:50:44 -0700 | [diff] [blame] | 170 | Context context = getInstrumentation().getTargetContext(); |
| 171 | boolean isWatch = |
| 172 | context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WATCH); |
| 173 | int expectedMinimumMemory = |
| 174 | ExpectedMemorySizesClass.getExpectedMemorySize(screenSize, screenDensity, isWatch); |
Brian Muramatsu | 2ab311c | 2010-12-23 16:01:00 -0800 | [diff] [blame] | 175 | |
| 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 | } |