From 809cbc53e01689872ade84262b581224c9fcbf08 Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Wed, 19 Dec 2018 14:11:30 +0900 Subject: Fix restoring animator_duration_scale back to default WindowManagerService.setAnimationScale() sends an asynchronous message to Handler to persist a setting value, but WindowManagerServiceRule cleans up all remaining messages from Handler when a test finishes. Thus there may be a chance that global animator_duration_scale setting is left unchanged. This CL eliminates removing pending messages and makes them finish instead. Fixes: 121229219 Test: Pass all presubmit tests for Window manager. $ atest --test-mapping frameworks/base/services/core/java/com/android/server/wm $ adb shell settings get global animator_duration_scale 1.0 Change-Id: I97a920cd221d9e0ed3f5cbf2172d9188b39d0ca6 --- .../server/wm/RemoteAnimationControllerTest.java | 2 +- .../server/wm/WindowManagerServiceRule.java | 31 +++++++++++++++++----- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/services/tests/wmtests/src/com/android/server/wm/RemoteAnimationControllerTest.java b/services/tests/wmtests/src/com/android/server/wm/RemoteAnimationControllerTest.java index ad2a708b88d9..413b6f4b3905 100644 --- a/services/tests/wmtests/src/com/android/server/wm/RemoteAnimationControllerTest.java +++ b/services/tests/wmtests/src/com/android/server/wm/RemoteAnimationControllerTest.java @@ -143,8 +143,8 @@ public class RemoteAnimationControllerTest extends WindowTestsBase { @Test public void testTimeout_scaled() throws Exception { - mWm.setAnimationScale(2, 5.0f); try { + mWm.setAnimationScale(2, 5.0f); final WindowState win = createWindow(null /* parent */, TYPE_BASE_APPLICATION, "testWin"); final AnimationAdapter adapter = mController.createAnimationAdapter(win.mAppToken, diff --git a/services/tests/wmtests/src/com/android/server/wm/WindowManagerServiceRule.java b/services/tests/wmtests/src/com/android/server/wm/WindowManagerServiceRule.java index 04e433e98678..cba958682401 100644 --- a/services/tests/wmtests/src/com/android/server/wm/WindowManagerServiceRule.java +++ b/services/tests/wmtests/src/com/android/server/wm/WindowManagerServiceRule.java @@ -35,6 +35,7 @@ import static org.mockito.Mockito.when; import android.app.ActivityManagerInternal; import android.content.Context; import android.hardware.display.DisplayManagerInternal; +import android.os.Handler; import android.os.PowerManagerInternal; import android.os.PowerSaveState; import android.view.Display; @@ -54,6 +55,7 @@ import org.mockito.invocation.InvocationOnMock; import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.CountDownLatch; /** * A test rule that sets up a fresh WindowManagerService instance before each test and makes sure @@ -193,12 +195,29 @@ public class WindowManagerServiceRule implements TestRule { if (wm == null) { return; } - wm.mH.removeCallbacksAndMessages(null); - wm.mAnimationHandler.removeCallbacksAndMessages(null); - SurfaceAnimationThread.getHandler().removeCallbacksAndMessages(null); - wm.mH.runWithScissors(() -> { }, 0); - wm.mAnimationHandler.runWithScissors(() -> { }, 0); - SurfaceAnimationThread.getHandler().runWithScissors(() -> { }, 0); + // Removing delayed FORCE_GC message decreases time for waiting idle. + wm.mH.removeMessages(WindowManagerService.H.FORCE_GC); + waitHandlerIdle(wm.mH); + waitHandlerIdle(wm.mAnimationHandler); + waitHandlerIdle(SurfaceAnimationThread.getHandler()); + } + + private static void waitHandlerIdle(Handler handler) { + if (handler.hasMessagesOrCallbacks()) { + final CountDownLatch latch = new CountDownLatch(1); + // Wait for delayed messages are processed. + handler.getLooper().getQueue().addIdleHandler(() -> { + if (handler.hasMessagesOrCallbacks()) { + return true; // keep idle handler. + } + latch.countDown(); + return false; // remove idle handler. + }); + try { + latch.await(); + } catch (InterruptedException e) { + } + } } private void destroyAllSurfaceTransactions() { -- cgit v1.2.3-59-g8ed1b