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.app.stubs.ActivityManagerMemoryClassLaunchActivity; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.pm.PackageManager; 24 import android.content.res.Configuration; 25 import android.test.ActivityInstrumentationTestCase2; 26 import android.util.DisplayMetrics; 27 import android.view.Display; 28 import android.view.WindowManager; 29 30 import com.android.compatibility.common.util.CddTest; 31 32 import java.util.HashMap; 33 import java.util.Map; 34 35 /** 36 * {@link ActivityInstrumentationTestCase2} that tests {@link ActivityManager#getMemoryClass()} 37 * by checking that the memory class matches the proper screen density and by launching an 38 * application that attempts to allocate memory on the heap. 39 */ 40 public class ActivityManagerMemoryClassTest 41 extends ActivityInstrumentationTestCase2<ActivityManagerMemoryClassLaunchActivity> { 42 43 public ActivityManagerMemoryClassTest() { 44 super(ActivityManagerMemoryClassLaunchActivity.class); 45 } 46 47 public static class ExpectedMemorySizesClass { 48 private static final Map<Integer, Integer> expectedMemorySizeForWatch 49 = new HashMap<Integer, Integer>(); 50 private static final Map<Integer, Integer> expectedMemorySizeForSmallNormalScreen 51 = new HashMap<Integer, Integer>(); 52 private static final Map<Integer, Integer> expectedMemorySizeForLargeScreen 53 = new HashMap<Integer, Integer>(); 54 private static final Map<Integer, Integer> expectedMemorySizeForXLargeScreen 55 = new HashMap<Integer, Integer>(); 56 57 static { 58 expectedMemorySizeForWatch.put(DisplayMetrics.DENSITY_LOW, 32); 59 expectedMemorySizeForWatch.put(DisplayMetrics.DENSITY_MEDIUM, 32); 60 expectedMemorySizeForWatch.put(DisplayMetrics.DENSITY_TV, 32); 61 expectedMemorySizeForWatch.put(DisplayMetrics.DENSITY_HIGH, 36); 62 expectedMemorySizeForWatch.put(DisplayMetrics.DENSITY_260, 36); 63 expectedMemorySizeForWatch.put(DisplayMetrics.DENSITY_280, 36); 64 expectedMemorySizeForWatch.put(DisplayMetrics.DENSITY_300, 36); 65 expectedMemorySizeForWatch.put(DisplayMetrics.DENSITY_XHIGH, 48); 66 expectedMemorySizeForWatch.put(DisplayMetrics.DENSITY_340, 48); 67 expectedMemorySizeForWatch.put(DisplayMetrics.DENSITY_360, 48); 68 expectedMemorySizeForWatch.put(DisplayMetrics.DENSITY_400, 56); 69 expectedMemorySizeForWatch.put(DisplayMetrics.DENSITY_420, 64); 70 expectedMemorySizeForWatch.put(DisplayMetrics.DENSITY_440, 88); 71 expectedMemorySizeForWatch.put(DisplayMetrics.DENSITY_XXHIGH, 88); 72 expectedMemorySizeForWatch.put(DisplayMetrics.DENSITY_560, 112); 73 expectedMemorySizeForWatch.put(DisplayMetrics.DENSITY_XXXHIGH, 154); 74 } 75 76 static { 77 expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_LOW, 32); 78 expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_MEDIUM, 32); 79 expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_TV, 48); 80 expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_HIGH, 48); 81 expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_260, 48); 82 expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_280, 48); 83 expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_300, 48); 84 expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_XHIGH, 80); 85 expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_340, 80); 86 expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_360, 80); 87 expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_400, 96); 88 expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_420, 112); 89 expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_440, 128); 90 expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_XXHIGH, 128); 91 expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_560, 192); 92 expectedMemorySizeForSmallNormalScreen.put(DisplayMetrics.DENSITY_XXXHIGH, 256); 93 } 94 95 static { 96 expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_LOW, 32); 97 expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_MEDIUM, 64); 98 expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_TV, 80); 99 expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_HIGH, 80); 100 expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_260, 96); 101 expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_280, 96); 102 expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_300, 96); 103 expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_XHIGH, 128); 104 expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_340, 160); 105 expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_360, 160); 106 expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_400, 192); 107 expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_420, 228); 108 expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_440, 256); 109 expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_XXHIGH, 256); 110 expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_560, 384); 111 expectedMemorySizeForLargeScreen.put(DisplayMetrics.DENSITY_XXXHIGH, 512); 112 } 113 114 static { 115 expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_LOW, 48); 116 expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_MEDIUM, 80); 117 expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_TV, 96); 118 expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_HIGH, 96); 119 expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_260, 144); 120 expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_280, 144); 121 expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_300, 144); 122 expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_XHIGH, 192); 123 expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_340, 192); 124 expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_360, 240); 125 expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_400, 288); 126 expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_420, 336); 127 expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_440, 384); 128 expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_XXHIGH, 384); 129 expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_560, 576); 130 expectedMemorySizeForXLargeScreen.put(DisplayMetrics.DENSITY_XXXHIGH, 768); 131 } 132 133 public static Integer getExpectedMemorySize( 134 int screenSize, 135 int screenDensity, 136 boolean isWatch) { 137 138 if (isWatch) { 139 return expectedMemorySizeForWatch.get(screenDensity); 140 } 141 142 switch (screenSize) { 143 case Configuration.SCREENLAYOUT_SIZE_SMALL: 144 case Configuration.SCREENLAYOUT_SIZE_NORMAL: 145 return expectedMemorySizeForSmallNormalScreen.get(screenDensity); 146 case Configuration.SCREENLAYOUT_SIZE_LARGE: 147 return expectedMemorySizeForLargeScreen.get(screenDensity); 148 case Configuration.SCREENLAYOUT_SIZE_XLARGE: 149 return expectedMemorySizeForXLargeScreen.get(screenDensity); 150 default: 151 throw new IllegalArgumentException("No memory requirement specified " 152 + " for screen layout size " + screenSize); 153 } 154 } 155 } 156 157 @CddTest(requirement="3.7") 158 public void testGetMemoryClass() throws Exception { 159 int memoryClass = getMemoryClass(); 160 int screenDensity = getScreenDensity(); 161 int screenSize = getScreenSize(); 162 assertMemoryForScreenDensity(memoryClass, screenDensity, screenSize); 163 164 runHeapTestApp(memoryClass); 165 } 166 167 private int getMemoryClass() { 168 Context context = getInstrumentation().getTargetContext(); 169 ActivityManager activityManager = 170 (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); 171 return activityManager.getMemoryClass(); 172 } 173 174 private int getScreenDensity() { 175 Context context = getInstrumentation().getTargetContext(); 176 WindowManager windowManager = 177 (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 178 Display display = windowManager.getDefaultDisplay(); 179 DisplayMetrics metrics = new DisplayMetrics(); 180 display.getMetrics(metrics); 181 return metrics.densityDpi; 182 } 183 184 private int getScreenSize() { 185 Context context = getInstrumentation().getTargetContext(); 186 Configuration config = context.getResources().getConfiguration(); 187 return config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK; 188 } 189 190 private void assertMemoryForScreenDensity(int memoryClass, int screenDensity, int screenSize) { 191 Context context = getInstrumentation().getTargetContext(); 192 boolean isWatch = 193 context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WATCH); 194 int expectedMinimumMemory = 195 ExpectedMemorySizesClass.getExpectedMemorySize(screenSize, screenDensity, isWatch); 196 197 assertTrue("Expected to have at least " + expectedMinimumMemory 198 + "mb of memory for screen density " + screenDensity, 199 memoryClass >= expectedMinimumMemory); 200 } 201 202 private void runHeapTestApp(int memoryClass) throws InterruptedException { 203 Intent intent = new Intent(); 204 intent.putExtra(ActivityManagerMemoryClassLaunchActivity.MEMORY_CLASS_EXTRA, 205 memoryClass); 206 setActivityIntent(intent); 207 ActivityManagerMemoryClassLaunchActivity activity = getActivity(); 208 assertEquals("The test application couldn't allocate memory close to the amount " 209 + " specified by the memory class.", Activity.RESULT_OK, activity.getResult()); 210 } 211 } 212