From b2c07aa82308863e9284da73346321a962a669da Mon Sep 17 00:00:00 2001 From: Riddle Hsu Date: Mon, 27 Apr 2020 18:42:14 +0800 Subject: [PATCH] Remove unnecessary cleanup procedures of WmTests Since WMS instance is created for each test methods, the cleanup for the window created by previous tests is not needed. Take ZOrderingTests as the example, it reduces about ~18% (20s+). If there are more windows to remove, the improvement will be more significant. Bug: 154655192 Test: presubmit Change-Id: If2cfdd15688fd72d37bab7caf6ab1f27766ba7f6 --- .../server/wm/SystemServicesTestRule.java | 27 +++-------- .../android/server/wm/WindowTestsBase.java | 45 ------------------- 2 files changed, 6 insertions(+), 66 deletions(-) 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 dea9294ff75be..bce1320f1d7ef 100644 --- a/services/tests/wmtests/src/com/android/server/wm/SystemServicesTestRule.java +++ b/services/tests/wmtests/src/com/android/server/wm/SystemServicesTestRule.java @@ -335,7 +335,11 @@ public class SystemServicesTestRule implements TestRule { mWmService.mConstants.dispose(); mWmService.mHighRefreshRateBlacklist.dispose(); + // 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(); @@ -399,8 +403,6 @@ public class SystemServicesTestRule implements TestRule { if (wm == null) { return; } - // Removing delayed FORCE_GC message decreases time for waiting idle. - wm.mH.removeMessages(WindowManagerService.H.FORCE_GC); waitHandlerIdle(wm.mH); waitHandlerIdle(wm.mAnimationHandler); // This is a different handler object than the wm.mAnimationHandler above. @@ -408,25 +410,8 @@ public class SystemServicesTestRule implements TestRule { waitHandlerIdle(SurfaceAnimationThread.getHandler()); } - private void waitHandlerIdle(Handler handler) { - synchronized (mCurrentMessagesProcessed) { - // Add a message to the handler queue and make sure it is fully processed before we move - // on. This makes sure all previous messages in the handler are fully processed vs. just - // popping them from the message queue. - mCurrentMessagesProcessed.set(false); - handler.post(() -> { - synchronized (mCurrentMessagesProcessed) { - mCurrentMessagesProcessed.set(true); - mCurrentMessagesProcessed.notifyAll(); - } - }); - while (!mCurrentMessagesProcessed.get()) { - try { - mCurrentMessagesProcessed.wait(); - } catch (InterruptedException e) { - } - } - } + static void waitHandlerIdle(Handler handler) { + handler.runWithScissors(() -> { }, 0 /* timeout */); } void waitUntilWindowAnimatorIdle() { diff --git a/services/tests/wmtests/src/com/android/server/wm/WindowTestsBase.java b/services/tests/wmtests/src/com/android/server/wm/WindowTestsBase.java index 6a64d1c976c46..94c204ab0fe05 100644 --- a/services/tests/wmtests/src/com/android/server/wm/WindowTestsBase.java +++ b/services/tests/wmtests/src/com/android/server/wm/WindowTestsBase.java @@ -52,13 +52,9 @@ import android.view.WindowManager; import com.android.server.AttributeCache; -import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; -import java.util.HashSet; -import java.util.LinkedList; - /** * Common base class for window manager unit test classes. * @@ -85,7 +81,6 @@ class WindowTestsBase extends SystemServiceTestsBase { WindowState mAppWindow; WindowState mChildAppWindowAbove; WindowState mChildAppWindowBelow; - HashSet mCommonWindows; /** * Spied {@link Transaction} class than can be used to verify calls. @@ -115,7 +110,6 @@ class WindowTestsBase extends SystemServiceTestsBase { mDisplayContent = createNewDisplay(true /* supportIme */); // Set-up some common windows. - mCommonWindows = new HashSet<>(); synchronized (mWm.mGlobalLock) { mWallpaperWindow = createCommonWindow(null, TYPE_WALLPAPER, "wallpaperWindow"); mImeWindow = createCommonWindow(null, TYPE_INPUT_METHOD, "mImeWindow"); @@ -151,48 +145,9 @@ class WindowTestsBase extends SystemServiceTestsBase { // Called before display is created. } - @After - public void tearDownBase() { - // If @After throws an exception, the error isn't logged. This will make sure any failures - // in the tear down are clear. This can be removed when b/37850063 is fixed. - try { - // Test may schedule to perform surface placement or other messages. Wait until a - // stable state to clean up for consistency. - waitUntilHandlersIdle(); - - final LinkedList nonCommonWindows = new LinkedList<>(); - - synchronized (mWm.mGlobalLock) { - mWm.mRoot.forAllWindows(w -> { - if (!mCommonWindows.contains(w)) { - nonCommonWindows.addLast(w); - } - }, true /* traverseTopToBottom */); - - while (!nonCommonWindows.isEmpty()) { - nonCommonWindows.pollLast().removeImmediately(); - } - - // Remove app transition & window freeze timeout callbacks to prevent unnecessary - // actions after test. - mWm.getDefaultDisplayContentLocked().mAppTransition - .removeAppTransitionTimeoutCallbacks(); - mWm.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT); - mDisplayContent.mInputMethodTarget = null; - } - - // Cleaned up everything in Handler. - cleanupWindowManagerHandlers(); - } catch (Exception e) { - Log.e(TAG, "Failed to tear down test", e); - throw e; - } - } - private WindowState createCommonWindow(WindowState parent, int type, String name) { synchronized (mWm.mGlobalLock) { final WindowState win = createWindow(parent, type, name); - mCommonWindows.add(win); // Prevent common windows from been IMe targets win.mAttrs.flags |= FLAG_NOT_FOCUSABLE; return win;