Skip to content

Commit 5e7119b

Browse files
Riddle HsuGerrit Code Review
authored andcommitted
Merge "Improve compatibility of testGetMemoryClass" into android14-tests-dev
2 parents e475701 + c4c40a7 commit 5e7119b

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)