From 42af4e546894463a622ba88269e3a19696053712 Mon Sep 17 00:00:00 2001 From: Bill Lin Date: Thu, 8 Oct 2020 19:07:03 +0800 Subject: [PATCH] Presubmit com.android.wm.shell.onehanded test flaky Root cause: Obtain handler from OneHandedDisplayAreaOrganizer and assert if there's message in the message queue will encounter timing problem. Solution: Set a spied handler object into OneHandedDisplayAreaOrganizer, and verify if sendMessage() was called or not. Test: for i in {1..1000};do atest WMShellUnitTests;done Bug: 170376119 Change-Id: Ie9f5561d61ac3c917a7dfced6c3281adb81fd12e --- .../OneHandedDisplayAreaOrganizer.java | 4 +- .../OneHandedDisplayAreaOrganizerTest.java | 94 ++++++++----------- 2 files changed, 41 insertions(+), 57 deletions(-) diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/onehanded/OneHandedDisplayAreaOrganizer.java b/libs/WindowManager/Shell/src/com/android/wm/shell/onehanded/OneHandedDisplayAreaOrganizer.java index b9c57c980ec96..17418f9346912 100644 --- a/libs/WindowManager/Shell/src/com/android/wm/shell/onehanded/OneHandedDisplayAreaOrganizer.java +++ b/libs/WindowManager/Shell/src/com/android/wm/shell/onehanded/OneHandedDisplayAreaOrganizer.java @@ -339,8 +339,8 @@ public class OneHandedDisplayAreaOrganizer extends DisplayAreaOrganizer { } @VisibleForTesting - Handler getUpdateHandler() { - return mUpdateHandler; + void setUpdateHandler(Handler updateHandler) { + mUpdateHandler = updateHandler; } /** diff --git a/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/onehanded/OneHandedDisplayAreaOrganizerTest.java b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/onehanded/OneHandedDisplayAreaOrganizerTest.java index 67a1b78036f8e..6d1a3c4722450 100644 --- a/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/onehanded/OneHandedDisplayAreaOrganizerTest.java +++ b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/onehanded/OneHandedDisplayAreaOrganizerTest.java @@ -25,6 +25,7 @@ import static org.mockito.ArgumentMatchers.anyFloat; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.Mockito.any; import static org.mockito.Mockito.never; +import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -45,7 +46,6 @@ import androidx.test.filters.SmallTest; import com.android.wm.shell.common.DisplayController; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; @@ -60,7 +60,6 @@ public class OneHandedDisplayAreaOrganizerTest extends OneHandedTestCase { DisplayAreaInfo mDisplayAreaInfo; Display mDisplay; - Handler mUpdateHandler; OneHandedDisplayAreaOrganizer mDisplayAreaOrganizer; OneHandedTutorialHandler mTutorialHandler; OneHandedAnimationController.OneHandedTransitionAnimator mFakeAnimator; @@ -82,6 +81,9 @@ public class OneHandedDisplayAreaOrganizerTest extends OneHandedTestCase { @Mock WindowContainerTransaction mMockWindowContainerTransaction; + Handler mSpyUpdateHandler; + Handler.Callback mUpdateCallback = (msg) -> false; + @Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); @@ -110,7 +112,8 @@ public class OneHandedDisplayAreaOrganizerTest extends OneHandedTestCase { mMockDisplayController, mMockAnimationController, mTutorialHandler); - mUpdateHandler = mDisplayAreaOrganizer.getUpdateHandler(); + mSpyUpdateHandler = spy(new Handler(OneHandedThread.get().getLooper(), mUpdateCallback)); + mDisplayAreaOrganizer.setUpdateHandler(mSpyUpdateHandler); } @Test @@ -130,7 +133,6 @@ public class OneHandedDisplayAreaOrganizerTest extends OneHandedTestCase { assertThat(mDisplayAreaOrganizer.mDisplayAreaMap).isEmpty(); } - @Ignore("b/160848002") @Test public void testScheduleOffset() { final int xOffSet = 0; @@ -138,200 +140,182 @@ public class OneHandedDisplayAreaOrganizerTest extends OneHandedTestCase { mDisplayAreaOrganizer.scheduleOffset(xOffSet, yOffSet); mTestableLooper.processAllMessages(); - assertThat(mUpdateHandler.hasMessages( - OneHandedDisplayAreaOrganizer.MSG_OFFSET_ANIMATE)).isEqualTo(true); + verify(mSpyUpdateHandler).sendMessage(any()); } @Test public void testRotation_portrait_0_to_landscape_90() { when(mMockLeash.isValid()).thenReturn(false); // Rotate 0 -> 90 - mTestableLooper.processAllMessages(); mDisplayAreaOrganizer.onRotateDisplay(Surface.ROTATION_0, Surface.ROTATION_90, mMockWindowContainerTransaction); + mTestableLooper.processAllMessages(); - assertThat(mUpdateHandler.hasMessages( - OneHandedDisplayAreaOrganizer.MSG_RESET_IMMEDIATE)).isEqualTo(true); + verify(mSpyUpdateHandler).sendMessage(any()); } @Test public void testRotation_portrait_0_to_seascape_270() { when(mMockLeash.isValid()).thenReturn(false); // Rotate 0 -> 270 - mTestableLooper.processAllMessages(); mDisplayAreaOrganizer.onRotateDisplay(Surface.ROTATION_0, Surface.ROTATION_270, mMockWindowContainerTransaction); + mTestableLooper.processAllMessages(); - assertThat(mUpdateHandler.hasMessages( - OneHandedDisplayAreaOrganizer.MSG_RESET_IMMEDIATE)).isEqualTo(true); - + verify(mSpyUpdateHandler).sendMessage(any()); } @Test public void testRotation_portrait_180_to_landscape_90() { when(mMockLeash.isValid()).thenReturn(false); // Rotate 180 -> 90 - mTestableLooper.processAllMessages(); mDisplayAreaOrganizer.onRotateDisplay(Surface.ROTATION_180, Surface.ROTATION_90, mMockWindowContainerTransaction); + mTestableLooper.processAllMessages(); - assertThat(mUpdateHandler.hasMessages( - OneHandedDisplayAreaOrganizer.MSG_RESET_IMMEDIATE)).isEqualTo(true); + verify(mSpyUpdateHandler).sendMessage(any()); } @Test public void testRotation_portrait_180_to_seascape_270() { when(mMockLeash.isValid()).thenReturn(false); // Rotate 180 -> 270 - mTestableLooper.processAllMessages(); mDisplayAreaOrganizer.onRotateDisplay(Surface.ROTATION_180, Surface.ROTATION_270, mMockWindowContainerTransaction); + mTestableLooper.processAllMessages(); - assertThat(mUpdateHandler.hasMessages( - OneHandedDisplayAreaOrganizer.MSG_RESET_IMMEDIATE)).isEqualTo(true); + verify(mSpyUpdateHandler).sendMessage(any()); } @Test public void testRotation_landscape_90_to_portrait_0() { when(mMockLeash.isValid()).thenReturn(false); // Rotate 90 -> 0 - mTestableLooper.processAllMessages(); mDisplayAreaOrganizer.onRotateDisplay(Surface.ROTATION_90, Surface.ROTATION_0, mMockWindowContainerTransaction); + mTestableLooper.processAllMessages(); - assertThat(mUpdateHandler.hasMessages( - OneHandedDisplayAreaOrganizer.MSG_RESET_IMMEDIATE)).isEqualTo(true); + verify(mSpyUpdateHandler).sendMessage(any()); } @Test public void testRotation_landscape_90_to_portrait_180() { when(mMockLeash.isValid()).thenReturn(false); // Rotate 90 -> 180 - mTestableLooper.processAllMessages(); mDisplayAreaOrganizer.onRotateDisplay(Surface.ROTATION_90, Surface.ROTATION_180, mMockWindowContainerTransaction); + mTestableLooper.processAllMessages(); - assertThat(mUpdateHandler.hasMessages( - OneHandedDisplayAreaOrganizer.MSG_RESET_IMMEDIATE)).isEqualTo(true); + verify(mSpyUpdateHandler).sendMessage(any()); } @Test public void testRotation_Seascape_270_to_portrait_0() { when(mMockLeash.isValid()).thenReturn(false); // Rotate 270 -> 0 - mTestableLooper.processAllMessages(); mDisplayAreaOrganizer.onRotateDisplay(Surface.ROTATION_270, Surface.ROTATION_0, mMockWindowContainerTransaction); + mTestableLooper.processAllMessages(); - assertThat(mUpdateHandler.hasMessages( - OneHandedDisplayAreaOrganizer.MSG_RESET_IMMEDIATE)).isEqualTo(true); + verify(mSpyUpdateHandler).sendMessage(any()); } @Test public void testRotation_seascape_90_to_portrait_180() { when(mMockLeash.isValid()).thenReturn(false); // Rotate 270 -> 180 - mTestableLooper.processAllMessages(); mDisplayAreaOrganizer.onRotateDisplay(Surface.ROTATION_270, Surface.ROTATION_180, mMockWindowContainerTransaction); + mTestableLooper.processAllMessages(); - assertThat(mUpdateHandler.hasMessages( - OneHandedDisplayAreaOrganizer.MSG_RESET_IMMEDIATE)).isEqualTo(true); + verify(mSpyUpdateHandler).sendMessage(any()); } @Test public void testRotation_portrait_0_to_portrait_0() { when(mMockLeash.isValid()).thenReturn(false); // Rotate 0 -> 0 - mTestableLooper.processAllMessages(); mDisplayAreaOrganizer.onRotateDisplay(Surface.ROTATION_0, Surface.ROTATION_0, mMockWindowContainerTransaction); + mTestableLooper.processAllMessages(); - assertThat(mUpdateHandler.hasMessages( - OneHandedDisplayAreaOrganizer.MSG_RESET_IMMEDIATE)).isEqualTo(false); + verify(mSpyUpdateHandler, never()).sendMessage(any()); } @Test public void testRotation_portrait_0_to_portrait_180() { when(mMockLeash.isValid()).thenReturn(false); // Rotate 0 -> 180 - mTestableLooper.processAllMessages(); mDisplayAreaOrganizer.onRotateDisplay(Surface.ROTATION_0, Surface.ROTATION_180, mMockWindowContainerTransaction); + mTestableLooper.processAllMessages(); - assertThat(mUpdateHandler.hasMessages( - OneHandedDisplayAreaOrganizer.MSG_RESET_IMMEDIATE)).isEqualTo(false); + verify(mSpyUpdateHandler, never()).sendMessage(any()); } @Test public void testRotation_portrait_180_to_portrait_180() { when(mMockLeash.isValid()).thenReturn(false); // Rotate 180 -> 180 - mTestableLooper.processAllMessages(); mDisplayAreaOrganizer.onRotateDisplay(Surface.ROTATION_180, Surface.ROTATION_180, mMockWindowContainerTransaction); + mTestableLooper.processAllMessages(); - assertThat(mUpdateHandler.hasMessages( - OneHandedDisplayAreaOrganizer.MSG_RESET_IMMEDIATE)).isEqualTo(false); + verify(mSpyUpdateHandler, never()).sendMessage(any()); } @Test public void testRotation_portrait_180_to_portrait_0() { when(mMockLeash.isValid()).thenReturn(false); // Rotate 180 -> 0 - mTestableLooper.processAllMessages(); mDisplayAreaOrganizer.onRotateDisplay(Surface.ROTATION_180, Surface.ROTATION_0, mMockWindowContainerTransaction); + mTestableLooper.processAllMessages(); - assertThat(mUpdateHandler.hasMessages( - OneHandedDisplayAreaOrganizer.MSG_RESET_IMMEDIATE)).isEqualTo(false); + verify(mSpyUpdateHandler, never()).sendMessage(any()); } @Test public void testRotation_landscape_90_to_landscape_90() { when(mMockLeash.isValid()).thenReturn(false); // Rotate 90 -> 90 - mTestableLooper.processAllMessages(); mDisplayAreaOrganizer.onRotateDisplay(Surface.ROTATION_90, Surface.ROTATION_90, mMockWindowContainerTransaction); + mTestableLooper.processAllMessages(); - assertThat(mUpdateHandler.hasMessages( - OneHandedDisplayAreaOrganizer.MSG_RESET_IMMEDIATE)).isEqualTo(false); + verify(mSpyUpdateHandler, never()).sendMessage(any()); } @Test public void testRotation_landscape_90_to_seascape_270() { when(mMockLeash.isValid()).thenReturn(false); // Rotate 90 -> 270 - mTestableLooper.processAllMessages(); mDisplayAreaOrganizer.onRotateDisplay(Surface.ROTATION_90, Surface.ROTATION_270, mMockWindowContainerTransaction); + mTestableLooper.processAllMessages(); - assertThat(mUpdateHandler.hasMessages( - OneHandedDisplayAreaOrganizer.MSG_RESET_IMMEDIATE)).isEqualTo(false); + verify(mSpyUpdateHandler, never()).sendMessage(any()); } @Test public void testRotation_seascape_270_to_seascape_270() { when(mMockLeash.isValid()).thenReturn(false); // Rotate 270 -> 270 - mTestableLooper.processAllMessages(); mDisplayAreaOrganizer.onRotateDisplay(Surface.ROTATION_270, Surface.ROTATION_270, mMockWindowContainerTransaction); + mTestableLooper.processAllMessages(); - assertThat(mUpdateHandler.hasMessages( - OneHandedDisplayAreaOrganizer.MSG_RESET_IMMEDIATE)).isEqualTo(false); + verify(mSpyUpdateHandler, never()).sendMessage(any()); } @Test public void testRotation_seascape_90_to_landscape_90() { when(mMockLeash.isValid()).thenReturn(false); // Rotate 270 -> 90 - mTestableLooper.processAllMessages(); mDisplayAreaOrganizer.onRotateDisplay(Surface.ROTATION_270, Surface.ROTATION_90, mMockWindowContainerTransaction); + mTestableLooper.processAllMessages(); - assertThat(mUpdateHandler.hasMessages( - OneHandedDisplayAreaOrganizer.MSG_RESET_IMMEDIATE)).isEqualTo(false); + verify(mSpyUpdateHandler, never()).sendMessage(any()); } }