Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 968cc7f3 authored by Yunfan Chen's avatar Yunfan Chen
Browse files

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
parent 2f040bef
Loading
Loading
Loading
Loading
+25 −10
Original line number Original line Diff line number Diff line
@@ -100,7 +100,7 @@ public class TaskStackChangedListenerTest {
    }
    }


    @Test
    @Test
    @FlakyTest(bugId = 119893767)
    @FlakyTest(detail = "Promote to presubmit when shown to be stable.")
    public void testTaskDescriptionChanged() throws Exception {
    public void testTaskDescriptionChanged() throws Exception {
        final Object[] params = new Object[2];
        final Object[] params = new Object[2];
        final CountDownLatch latch = new CountDownLatch(1);
        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);
        waitForCallback(latch);
        assertEquals(activity.getTaskId(), params[0]);
        assertEquals(taskId, params[0]);
        assertEquals("Test Label", ((TaskDescription) params[1]).getLabel());
        assertEquals("Test Label", ((TaskDescription) params[1]).getLabel());
    }
    }


    @Test
    @Test
    @FlakyTest(bugId = 119893767)
    @FlakyTest(detail = "Promote to presubmit when shown to be stable.")
    public void testActivityRequestedOrientationChanged() throws Exception {
    public void testActivityRequestedOrientationChanged() throws Exception {
        final int[] params = new int[2];
        final int[] params = new int[2];
        final CountDownLatch latch = new CountDownLatch(1);
        final CountDownLatch latch = new CountDownLatch(1);
@@ -142,9 +146,12 @@ public class TaskStackChangedListenerTest {
                latch.countDown();
                latch.countDown();
            }
            }
        });
        });
        final Activity activity = startTestActivity(ActivityRequestedOrientationChange.class);
        int taskId;
        synchronized (sLock) {
            taskId = startTestActivity(ActivityRequestedOrientationChange.class).getTaskId();
        }
        waitForCallback(latch);
        waitForCallback(latch);
        assertEquals(activity.getTaskId(), params[0]);
        assertEquals(taskId, params[0]);
        assertEquals(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT, params[1]);
        assertEquals(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT, params[1]);
    }
    }


@@ -152,7 +159,7 @@ public class TaskStackChangedListenerTest {
     * Tests for onTaskCreated, onTaskMovedToFront, onTaskRemoved and onTaskRemovalStarted.
     * Tests for onTaskCreated, onTaskMovedToFront, onTaskRemoved and onTaskRemovalStarted.
     */
     */
    @Test
    @Test
    @FlakyTest(bugId = 119893767)
    @FlakyTest(detail = "Promote to presubmit when shown to be stable.")
    public void testTaskChangeCallBacks() throws Exception {
    public void testTaskChangeCallBacks() throws Exception {
        final Object[] params = new Object[2];
        final Object[] params = new Object[2];
        final CountDownLatch taskCreatedLaunchLatch = new CountDownLatch(1);
        final CountDownLatch taskCreatedLaunchLatch = new CountDownLatch(1);
@@ -245,7 +252,7 @@ public class TaskStackChangedListenerTest {


    private void waitForCallback(CountDownLatch latch) {
    private void waitForCallback(CountDownLatch latch) {
        try {
        try {
            final boolean result = latch.await(2, TimeUnit.SECONDS);
            final boolean result = latch.await(4, TimeUnit.SECONDS);
            if (!result) {
            if (!result) {
                throw new RuntimeException("Timed out waiting for task stack change notification");
                throw new RuntimeException("Timed out waiting for task stack change notification");
            }
            }
@@ -324,18 +331,26 @@ public class TaskStackChangedListenerTest {
        protected void onPostResume() {
        protected void onPostResume() {
            super.onPostResume();
            super.onPostResume();
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            synchronized (sLock) {
                // Hold the lock to ensure no one is trying to access fields of this Activity in
                // this test.
                finish();
                finish();
            }
            }
        }
        }
    }


    public static class ActivityTaskDescriptionChange extends TestActivity {
    public static class ActivityTaskDescriptionChange extends TestActivity {
        @Override
        @Override
        protected void onPostResume() {
        protected void onPostResume() {
            super.onPostResume();
            super.onPostResume();
            setTaskDescription(new TaskDescription("Test Label"));
            setTaskDescription(new TaskDescription("Test Label"));
            synchronized (sLock) {
                // Hold the lock to ensure no one is trying to access fields of this Activity in
                // this test.
                finish();
                finish();
            }
            }
        }
        }
    }


    public static class ActivityTaskChangeCallbacks extends TestActivity {
    public static class ActivityTaskChangeCallbacks extends TestActivity {
        public boolean mOnDetachedFromWindowCalled = false;
        public boolean mOnDetachedFromWindowCalled = false;