From 968cc7f333e33d6488c6d6c0859c7109c101b1e9 Mon Sep 17 00:00:00 2001 From: Yunfan Chen Date: Fri, 15 Feb 2019 20:14:14 +0900 Subject: [PATCH] Fix flaky TaskStackChangedListenerTest The test was flaky is because the activity can be killed before the test finished running. The test activity would be killed in onPostResumed and the latch count down is happening in onResumed. After that, the test was trying to access task ID via the activity. It is possible that when accessing task ID, the activity is no longer there. To fix that, keep the task ID when creating the activity in the test. This will significantly reduce, if not eliminate, the flakiness inside. Mark as pre-submit candidate to be monitored. Test: atest TaskStackChangedListenerTest Bug: 119893767 Change-Id: I3826282b395169097cb3496e47557d622a8fc856 --- .../wm/TaskStackChangedListenerTest.java | 35 +++++++++++++------ 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/services/tests/wmtests/src/com/android/server/wm/TaskStackChangedListenerTest.java b/services/tests/wmtests/src/com/android/server/wm/TaskStackChangedListenerTest.java index dcca3167c752e..42a205a2fb3df 100644 --- a/services/tests/wmtests/src/com/android/server/wm/TaskStackChangedListenerTest.java +++ b/services/tests/wmtests/src/com/android/server/wm/TaskStackChangedListenerTest.java @@ -100,7 +100,7 @@ public class TaskStackChangedListenerTest { } @Test - @FlakyTest(bugId = 119893767) + @FlakyTest(detail = "Promote to presubmit when shown to be stable.") public void testTaskDescriptionChanged() throws Exception { final Object[] params = new Object[2]; final CountDownLatch latch = new CountDownLatch(1); @@ -122,14 +122,18 @@ public class TaskStackChangedListenerTest { } } }); - final Activity activity = startTestActivity(ActivityTaskDescriptionChange.class); + + int taskId; + synchronized (sLock) { + taskId = startTestActivity(ActivityTaskDescriptionChange.class).getTaskId(); + } waitForCallback(latch); - assertEquals(activity.getTaskId(), params[0]); + assertEquals(taskId, params[0]); assertEquals("Test Label", ((TaskDescription) params[1]).getLabel()); } @Test - @FlakyTest(bugId = 119893767) + @FlakyTest(detail = "Promote to presubmit when shown to be stable.") public void testActivityRequestedOrientationChanged() throws Exception { final int[] params = new int[2]; final CountDownLatch latch = new CountDownLatch(1); @@ -142,9 +146,12 @@ public class TaskStackChangedListenerTest { latch.countDown(); } }); - final Activity activity = startTestActivity(ActivityRequestedOrientationChange.class); + int taskId; + synchronized (sLock) { + taskId = startTestActivity(ActivityRequestedOrientationChange.class).getTaskId(); + } waitForCallback(latch); - assertEquals(activity.getTaskId(), params[0]); + assertEquals(taskId, params[0]); assertEquals(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT, params[1]); } @@ -152,7 +159,7 @@ public class TaskStackChangedListenerTest { * Tests for onTaskCreated, onTaskMovedToFront, onTaskRemoved and onTaskRemovalStarted. */ @Test - @FlakyTest(bugId = 119893767) + @FlakyTest(detail = "Promote to presubmit when shown to be stable.") public void testTaskChangeCallBacks() throws Exception { final Object[] params = new Object[2]; final CountDownLatch taskCreatedLaunchLatch = new CountDownLatch(1); @@ -245,7 +252,7 @@ public class TaskStackChangedListenerTest { private void waitForCallback(CountDownLatch latch) { try { - final boolean result = latch.await(2, TimeUnit.SECONDS); + final boolean result = latch.await(4, TimeUnit.SECONDS); if (!result) { throw new RuntimeException("Timed out waiting for task stack change notification"); } @@ -324,7 +331,11 @@ public class TaskStackChangedListenerTest { protected void onPostResume() { super.onPostResume(); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); - finish(); + synchronized (sLock) { + // Hold the lock to ensure no one is trying to access fields of this Activity in + // this test. + finish(); + } } } @@ -333,7 +344,11 @@ public class TaskStackChangedListenerTest { protected void onPostResume() { super.onPostResume(); setTaskDescription(new TaskDescription("Test Label")); - finish(); + synchronized (sLock) { + // Hold the lock to ensure no one is trying to access fields of this Activity in + // this test. + finish(); + } } }