Reduce dependency of real device for wm unit test

By default the test copy the display info from the environment.
Such as the display size can be close to square or
mDefaultFixedToUserRotation is true for FEATURE_AUTOMOTIVE,
which may not match the assumption of some tests.

Though ideally the unit test should mock those attributes to
have consistent precondition, so far there are not too many
cases hence make the affected tests more independent.

Bug: 272527315
Bug: 267795360
Test: atest ActivityRecordTests#testIsSnapshotCompatible
      WallpaperControllerTests.testWallpaperSizeWithFixedTransform

Change-Id: I3373a3f7093e9ace1edf185a9d124e262f27f1f0
This commit is contained in:
Riddle Hsu
2023-06-02 17:21:22 +08:00
parent 25aa2d3c87
commit f0b39f5c93
4 changed files with 15 additions and 9 deletions

View File

@@ -1996,7 +1996,8 @@ public class ActivityRecordTests extends WindowTestsBase {
assertTrue(activity.isSnapshotCompatible(snapshot));
setRotatedScreenOrientationSilently(activity);
doReturn(task.getWindowConfiguration().getRotation() + 1).when(mDisplayContent)
.rotationForActivityInDifferentOrientation(activity);
assertFalse(activity.isSnapshotCompatible(snapshot));
}

View File

@@ -4654,14 +4654,6 @@ public class SizeCompatTests extends WindowTestsBase {
return c;
}
private static void resizeDisplay(DisplayContent displayContent, int width, int height) {
displayContent.updateBaseDisplayMetrics(width, height, displayContent.mBaseDisplayDensity,
displayContent.mBaseDisplayPhysicalXDpi, displayContent.mBaseDisplayPhysicalYDpi);
final Configuration c = new Configuration();
displayContent.computeScreenConfiguration(c);
displayContent.onRequestedOverrideConfigurationChanged(c);
}
private static void setNeverConstrainDisplayApisFlag(@Nullable String value,
boolean makeDefault) {
DeviceConfig.setProperty(NAMESPACE_CONSTRAIN_DISPLAY_APIS,

View File

@@ -127,6 +127,10 @@ public class WallpaperControllerTests extends WindowTestsBase {
public void testWallpaperSizeWithFixedTransform() {
// No wallpaper
final DisplayContent dc = mDisplayContent;
if (dc.mBaseDisplayHeight == dc.mBaseDisplayWidth) {
// Make sure the size is different when changing orientation.
resizeDisplay(dc, 500, 1000);
}
// No wallpaper WSA Surface
final WindowState wallpaperWindow = createWallpaperWindow(dc);

View File

@@ -71,6 +71,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.res.Configuration;
import android.graphics.Insets;
import android.graphics.Rect;
import android.hardware.HardwareBuffer;
@@ -945,6 +946,14 @@ class WindowTestsBase extends SystemServiceTestsBase {
dc.setRotationAnimation(null);
}
static void resizeDisplay(DisplayContent displayContent, int width, int height) {
displayContent.updateBaseDisplayMetrics(width, height, displayContent.mBaseDisplayDensity,
displayContent.mBaseDisplayPhysicalXDpi, displayContent.mBaseDisplayPhysicalYDpi);
final Configuration c = new Configuration();
displayContent.computeScreenConfiguration(c);
displayContent.onRequestedOverrideConfigurationChanged(c);
}
// The window definition for UseTestDisplay#addWindows. The test can declare to add only
// necessary windows, that avoids adding unnecessary overhead of unused windows.
static final int W_NOTIFICATION_SHADE = TYPE_NOTIFICATION_SHADE;