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; |
| 22 | import android.content.res.Configuration; |
| 23 | import android.test.ActivityInstrumentationTestCase2; |
| 24 | import android.util.DisplayMetrics; |
| 25 | import android.view.Display; |
| 26 | import android.view.WindowManager; |
Daniel Xie | 6ac85e0 | 2015-04-27 11:24:23 -0700 | [diff] [blame] | 27 | import java.util.HashMap; |
| 28 | import java.util.Map; |
Brian Muramatsu | 2ab311c | 2010-12-23 16:01:00 -0800 | [diff] [blame] | 29 | |
| 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 | */ |
| 35 | public class ActivityManagerMemoryClassTest |
| 36 | extends ActivityInstrumentationTestCase2<ActivityManagerMemoryClassLaunchActivity> { |
| 37 | |
| 38 | public ActivityManagerMemoryClassTest() { |
| 39 | super(ActivityManagerMemoryClassLaunchActivity.class); |
| 40 | } |
| 41 | |
Daniel Xie | 6ac85e0 | 2015-04-27 11:24:23 -0700 | [diff] [blame] | 42 | 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 Xie | 15f3d2d | 2015-04-28 10:17:42 -0700 | [diff] [blame] | 55 | expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_280, 48); |
Adam Powell | eb1e0ea | 2015-06-03 15:51:50 -0700 | [diff] [blame] | 56 | expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_XHIGH, 48); |
| 57 | expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_360, 64); |
Daniel Xie | 6ac85e0 | 2015-04-27 11:24:23 -0700 | [diff] [blame] | 58 | expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_400, 96); |
Robert Carr | 108b498 | 2015-08-13 14:57:40 -0700 | [diff] [blame] | 59 | expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_420, 112); |
Daniel Xie | 6ac85e0 | 2015-04-27 11:24:23 -0700 | [diff] [blame] | 60 | 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 Xie | 15f3d2d | 2015-04-28 10:17:42 -0700 | [diff] [blame] | 70 | expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_280, 96); |
Adam Powell | eb1e0ea | 2015-06-03 15:51:50 -0700 | [diff] [blame] | 71 | expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_XHIGH, 128); |
| 72 | expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_360, 160); |
Daniel Xie | 6ac85e0 | 2015-04-27 11:24:23 -0700 | [diff] [blame] | 73 | expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_400, 192); |
Robert Carr | 108b498 | 2015-08-13 14:57:40 -0700 | [diff] [blame] | 74 | expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_420, 228); |
Daniel Xie | 6ac85e0 | 2015-04-27 11:24:23 -0700 | [diff] [blame] | 75 | 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 Xie | 15f3d2d | 2015-04-28 10:17:42 -0700 | [diff] [blame] | 85 | expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_280, 144); |
Adam Powell | eb1e0ea | 2015-06-03 15:51:50 -0700 | [diff] [blame] | 86 | expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_XHIGH, 192); |
| 87 | expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_360, 240); |
Daniel Xie | 6ac85e0 | 2015-04-27 11:24:23 -0700 | [diff] [blame] | 88 | expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_400, 288); |
Robert Carr | 108b498 | 2015-08-13 14:57:40 -0700 | [diff] [blame] | 89 | expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_420, 336); |
Daniel Xie | 6ac85e0 | 2015-04-27 11:24:23 -0700 | [diff] [blame] | 90 | 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 Muramatsu | 2ab311c | 2010-12-23 16:01:00 -0800 | [diff] [blame] | 111 | 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 Xie | b2f7f8a | 2015-04-29 14:11:40 -0700 | [diff] [blame] | 144 | int expectedMinimumMemory = ExpectedMemorySizesClass.getExpectedMemorySize(screenSize, |
| 145 | screenDensity); |
Brian Muramatsu | 2ab311c | 2010-12-23 16:01:00 -0800 | [diff] [blame] | 146 | |
| 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 | } |