Build/Install/Run: + * atest FrameworksCoreTests:WindowMetricsTest + * + *
This test class is a part of Window Manager Service tests and specified in + * {@link com.android.server.wm.test.filters.FrameworksTestsFilter}. + */ +@FlakyTest(bugId = 148789183, detail = "Remove after confirmed it's stable.") +@RunWith(AndroidJUnit4.class) +@SmallTest +@Presubmit +public class WindowMetricsTest { + private Context mWindowContext; + private WindowManager mWm; + + @Before + public void setUp() { + final Context insetContext = InstrumentationRegistry.getInstrumentation() + .getTargetContext(); + final Display display = insetContext.getSystemService(DisplayManager.class) + .getDisplay(DEFAULT_DISPLAY); + mWindowContext = insetContext.createDisplayContext(display) + .createWindowContext(TYPE_APPLICATION_OVERLAY, null /* options */); + mWm = mWindowContext.getSystemService(WindowManager.class); + } + + @Test + public void testAddViewANdRemoveView_GetMetrics_DoNotCrash() { + final View view = new View(mWindowContext); + final WindowManager.LayoutParams params = + new WindowManager.LayoutParams(TYPE_APPLICATION_OVERLAY); + Handler.getMain().runWithScissors(() -> { + mWm.addView(view, params); + // Check get metrics do not crash. + WindowMetrics currentMetrics = mWm.getCurrentWindowMetrics(); + WindowMetrics maxMetrics = mWm.getMaximumWindowMetrics(); + verifyMetricsSanity(currentMetrics, maxMetrics); + + mWm.removeViewImmediate(view); + // Check get metrics do not crash. + currentMetrics = mWm.getCurrentWindowMetrics(); + maxMetrics = mWm.getMaximumWindowMetrics(); + verifyMetricsSanity(currentMetrics, maxMetrics); + }, 0); + } + + private static void verifyMetricsSanity(WindowMetrics currentMetrics, + WindowMetrics maxMetrics) { + Size currentSize = currentMetrics.getSize(); + Size maxSize = maxMetrics.getSize(); + + assertTrue(maxSize.getWidth() >= currentSize.getWidth()); + assertTrue(maxSize.getHeight() >= currentSize.getHeight()); + } +} diff --git a/services/core/java/com/android/server/wm/WindowManagerService.java b/services/core/java/com/android/server/wm/WindowManagerService.java index 9cb5ba7a74a4c..e0f8f0e9aee92 100644 --- a/services/core/java/com/android/server/wm/WindowManagerService.java +++ b/services/core/java/com/android/server/wm/WindowManagerService.java @@ -8075,32 +8075,39 @@ public class WindowManagerService extends IWindowManager.Stub public void getWindowInsets(WindowManager.LayoutParams attrs, int displayId, Rect outContentInsets, Rect outStableInsets, DisplayCutout.ParcelableWrapper displayCutout) { - synchronized (mGlobalLock) { - final DisplayContent dc = mRoot.getDisplayContentOrCreate(displayId); - if (dc == null) { - throw new WindowManager.InvalidDisplayException("Display#" + displayId - + "could not be found!"); + final long origId = Binder.clearCallingIdentity(); + try { + synchronized (mGlobalLock) { + final DisplayContent dc = getDisplayContentOrCreate(displayId, attrs.token); + if (dc == null) { + throw new WindowManager.InvalidDisplayException("Display#" + displayId + + "could not be found!"); + } + final WindowToken windowToken = dc.getWindowToken(attrs.token); + final ActivityRecord activity; + if (windowToken != null && windowToken.asActivityRecord() != null) { + activity = windowToken.asActivityRecord(); + } else { + activity = null; + } + final Rect taskBounds; + final boolean floatingStack; + if (activity != null && activity.getTask() != null) { + final Task task = activity.getTask(); + taskBounds = new Rect(); + task.getBounds(taskBounds); + floatingStack = task.isFloating(); + } else { + taskBounds = null; + floatingStack = false; + } + final DisplayFrames displayFrames = dc.mDisplayFrames; + final DisplayPolicy policy = dc.getDisplayPolicy(); + policy.getLayoutHintLw(attrs, taskBounds, displayFrames, floatingStack, + new Rect(), outContentInsets, outStableInsets, displayCutout); } - final WindowToken windowToken = dc.getWindowToken(attrs.token); - final ActivityRecord activity; - if (windowToken != null && windowToken.asActivityRecord() != null) { - activity = windowToken.asActivityRecord(); - } else { - activity = null; - } - final Rect taskBounds = new Rect(); - final boolean floatingStack; - if (activity != null && activity.getTask() != null) { - final Task task = activity.getTask(); - task.getBounds(taskBounds); - floatingStack = task.isFloating(); - } else { - floatingStack = false; - } - final DisplayFrames displayFrames = dc.mDisplayFrames; - final DisplayPolicy policy = dc.getDisplayPolicy(); - policy.getLayoutHintLw(attrs, taskBounds, displayFrames, floatingStack, - new Rect(), outContentInsets, outStableInsets, displayCutout); + } finally { + Binder.restoreCallingIdentity(origId); } } } diff --git a/tests/utils/testutils/java/com/android/server/wm/test/filters/FrameworksTestsFilter.java b/tests/utils/testutils/java/com/android/server/wm/test/filters/FrameworksTestsFilter.java index 26916bcffcfb3..aed62d0468961 100644 --- a/tests/utils/testutils/java/com/android/server/wm/test/filters/FrameworksTestsFilter.java +++ b/tests/utils/testutils/java/com/android/server/wm/test/filters/FrameworksTestsFilter.java @@ -46,6 +46,7 @@ public final class FrameworksTestsFilter extends SelectTest { "android.view.InsetsSourceTest", "android.view.InsetsSourceConsumerTest", "android.view.InsetsStateTest", + "android.view.WindowMetricsTest" }; public FrameworksTestsFilter(Bundle testArgs) {