From e2a90aa21e85dd646b2379919c628724134e7fc9 Mon Sep 17 00:00:00 2001 From: Riddle Hsu Date: Thu, 16 Jun 2022 22:58:22 +0800 Subject: [PATCH] Disallow unchecked message removal on shared handler The dispose() of each shared handler threads in SystemServicesTestRule#tearDown should be enough for test cleanup. So remove the usages of removeCallbacksAndMessages to align the policy of shared handler. Bug: 235594693 Test: atest WmTests Change-Id: I4662f06d1d45c62bba08f286643df17c39eec43d --- .../java/com/android/server/AnimationThread.java | 2 +- .../android/server/wm/SurfaceAnimationThread.java | 2 +- .../android/server/wm/SystemServiceTestsBase.java | 4 ---- .../android/server/wm/SystemServicesTestRule.java | 14 -------------- 4 files changed, 2 insertions(+), 20 deletions(-) diff --git a/services/core/java/com/android/server/AnimationThread.java b/services/core/java/com/android/server/AnimationThread.java index fad743eafdaac..826e7b52a9df6 100644 --- a/services/core/java/com/android/server/AnimationThread.java +++ b/services/core/java/com/android/server/AnimationThread.java @@ -40,7 +40,7 @@ public final class AnimationThread extends ServiceThread { sInstance = new AnimationThread(); sInstance.start(); sInstance.getLooper().setTraceTag(Trace.TRACE_TAG_WINDOW_MANAGER); - sHandler = new Handler(sInstance.getLooper()); + sHandler = makeSharedHandler(sInstance.getLooper()); } } diff --git a/services/core/java/com/android/server/wm/SurfaceAnimationThread.java b/services/core/java/com/android/server/wm/SurfaceAnimationThread.java index 1259ee901e4cf..8ea715c4084e0 100644 --- a/services/core/java/com/android/server/wm/SurfaceAnimationThread.java +++ b/services/core/java/com/android/server/wm/SurfaceAnimationThread.java @@ -40,7 +40,7 @@ public final class SurfaceAnimationThread extends ServiceThread { sInstance = new SurfaceAnimationThread(); sInstance.start(); sInstance.getLooper().setTraceTag(Trace.TRACE_TAG_WINDOW_MANAGER); - sHandler = new Handler(sInstance.getLooper()); + sHandler = makeSharedHandler(sInstance.getLooper()); } } diff --git a/services/tests/wmtests/src/com/android/server/wm/SystemServiceTestsBase.java b/services/tests/wmtests/src/com/android/server/wm/SystemServiceTestsBase.java index 53c2a5b8967d1..486486869d055 100644 --- a/services/tests/wmtests/src/com/android/server/wm/SystemServiceTestsBase.java +++ b/services/tests/wmtests/src/com/android/server/wm/SystemServiceTestsBase.java @@ -47,10 +47,6 @@ class SystemServiceTestsBase { mLockRule.waitForLocked(mSystemServicesTestRule::waitUntilWindowAnimatorIdle); } - void cleanupWindowManagerHandlers() { - mLockRule.waitForLocked(mSystemServicesTestRule::cleanupWindowManagerHandlers); - } - boolean waitHandlerIdle(Handler handler) { return waitHandlerIdle(handler, 0 /* timeout */); } diff --git a/services/tests/wmtests/src/com/android/server/wm/SystemServicesTestRule.java b/services/tests/wmtests/src/com/android/server/wm/SystemServicesTestRule.java index 859eba600af59..ab7e8eab28c3c 100644 --- a/services/tests/wmtests/src/com/android/server/wm/SystemServicesTestRule.java +++ b/services/tests/wmtests/src/com/android/server/wm/SystemServicesTestRule.java @@ -370,8 +370,6 @@ public class SystemServicesTestRule implements TestRule { // This makes sure the posted messages without delay are processed, e.g. // DisplayPolicy#release, WindowManagerService#setAnimationScale. waitUntilWindowManagerHandlersIdle(); - // Clear all posted messages with delay, so they don't be executed at unexpected times. - cleanupWindowManagerHandlers(); // Needs to explicitly dispose current static threads because there could be messages // scheduled at a later time, and all mocks are invalid when it's executed. DisplayThread.dispose(); @@ -460,18 +458,6 @@ public class SystemServicesTestRule implements TestRule { return proc; } - void cleanupWindowManagerHandlers() { - final WindowManagerService wm = getWindowManagerService(); - if (wm == null) { - return; - } - wm.mH.removeCallbacksAndMessages(null); - wm.mAnimationHandler.removeCallbacksAndMessages(null); - // This is a different handler object than the wm.mAnimationHandler above. - AnimationThread.getHandler().removeCallbacksAndMessages(null); - SurfaceAnimationThread.getHandler().removeCallbacksAndMessages(null); - } - void waitUntilWindowManagerHandlersIdle() { final WindowManagerService wm = getWindowManagerService(); if (wm == null) {