Skip to content

Commit c4c40a7

Browse files
author
Riddle Hsu
committed
Improve compatibility of testGetMemoryClass
The configuration screen size is the available space for apps, so it is not the same as physical display size. As the current definition of screenHeightDp: The height of the available screen space in dp units excluding the area occupied by window insets,such as the status bar, navigation bar, and cutouts. Since screenLayout is calculated from screenHeightDp and screenWidthDp, the result can be different depending on the orientation and how the insets occupied the space. So this change adds a tolerance from the insets size to be able to use a smaller screen size definition. Then a lower memory size can be chosen as the assertion target. E.g. when density is 400 XLARGE is 288m, LARGE is 192m. The device usually sets dalvik.vm.heapgrowthlimit=256m Then the case can be passed by 256>192 Bug: 310820325 Test: atest CtsAppTestCases:ActivityManagerMemoryClassTest If is is on a device with density 400, then adb shell wm size 1800x2800 If is is on a device with density 420, then adb shell wm size 1890x2800 (i.e. short side is ~720dp) The test should pass when putting the device in portrait. Change-Id: Ie8e38340dc3e08e541c7aa8f80161fcb8de63ee3
1 parent 8407d97 commit c4c40a7

1 file changed

Lines changed: 27 additions & 1 deletion

File tree

tests/app/src/android/app/cts/ActivityManagerMemoryClassTest.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@
2222
import android.content.Intent;
2323
import android.content.pm.PackageManager;
2424
import android.content.res.Configuration;
25+
import android.graphics.Insets;
2526
import android.test.ActivityInstrumentationTestCase2;
2627
import android.util.DisplayMetrics;
28+
import android.util.Log;
2729
import android.view.Display;
30+
import android.view.WindowInsets;
2831
import android.view.WindowManager;
2932

3033
import androidx.test.uiautomator.UiDevice;
@@ -247,7 +250,30 @@ private int getScreenDensity() {
247250
private int getScreenSize() {
248251
Context context = getInstrumentation().getTargetContext();
249252
Configuration config = context.getResources().getConfiguration();
250-
return config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;
253+
final int configScreenSize = config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;
254+
final int minScreenSizeDp = Math.min(config.screenWidthDp, config.screenHeightDp);
255+
// The insets size may affect screenSizeDp in different orientations. E.g., the short side
256+
// is 720dp as the width in portrait orientation, but when the short side is the height in
257+
// landscape orientation, the value will be smaller than 720dp because the insets of
258+
// system bars may occupy a little space. Then the screen size from Configuration will be
259+
// LARGE in landscape and XLARGE in portrait. So below calculation allows to return a
260+
// smaller size definition if the size excluding insets is lower than the size threshold.
261+
final Insets insets = getActivity().getWindowManager().getCurrentWindowMetrics()
262+
.getWindowInsets().getInsetsIgnoringVisibility(WindowInsets.Type.systemBars());
263+
final int insetsSize = Math.max(insets.top + insets.bottom, insets.left + insets.right);
264+
final int toleranceSizeDp = (int) (insetsSize /
265+
((float) config.densityDpi / DisplayMetrics.DENSITY_DEFAULT) + 0.5f);
266+
Log.i("ActivityManagerMemoryClassTest", "getScreenSize: config=" + config
267+
+ " insets=" + insets + " toleranceSizeDp=" + toleranceSizeDp);
268+
if (configScreenSize == Configuration.SCREENLAYOUT_SIZE_XLARGE
269+
&& (minScreenSizeDp - toleranceSizeDp < 720)) {
270+
return Configuration.SCREENLAYOUT_SIZE_LARGE;
271+
}
272+
if (configScreenSize == Configuration.SCREENLAYOUT_SIZE_LARGE
273+
&& (minScreenSizeDp - toleranceSizeDp < 480)) {
274+
return Configuration.SCREENLAYOUT_SIZE_NORMAL;
275+
}
276+
return configScreenSize;
251277
}
252278

253279
private void assertMemoryForScreenDensity(int memoryClass, int screenDensity, int screenSize) {

0 commit comments

Comments
 (0)