Fix DisplayTest.testModeSwitchOnPrimaryDisplay

The test creates a shuffled list of modes to test. It's important
that the first mode is differnet from the currently active mode.
Currently the code aknowledges that, but it doesn't take into account
that the active mode can change after creating the list. This way
it's possible that the first mode will be the same as the active
mode and the test will fail because no onDisplayChanged event
will be generated.

This CL moves the creation of the list after turning off refresh
rate switching.

Test: test no longer fails
Bug: 201616188
Change-Id: I914401c4878eea4cf69aa653f72d7b9eaf7c4cf2
diff --git a/tests/tests/display/src/android/display/cts/DisplayTest.java b/tests/tests/display/src/android/display/cts/DisplayTest.java
index 993ec85..8c192e7 100644
--- a/tests/tests/display/src/android/display/cts/DisplayTest.java
+++ b/tests/tests/display/src/android/display/cts/DisplayTest.java
@@ -590,27 +590,29 @@
         Display.Mode[] modes = mDefaultDisplay.getSupportedModes();
         assumeTrue("Need two or more display modes to exercise switching.", modes.length > 1);
 
-        // Create a deterministically shuffled list of display modes, which ends with the
-        // current active mode. We'll switch to the modes in this order. The active mode is last
-        // so we don't need an extra mode switch in case the test completes successfully.
-        Display.Mode activeMode = mDefaultDisplay.getMode();
-        List<Display.Mode> modesList = new ArrayList<>(modes.length);
-        for (Display.Mode mode : modes) {
-            if (mode.getModeId() != activeMode.getModeId()) {
-                modesList.add(mode);
-            }
-        }
-        Random random = new Random(42);
-        Collections.shuffle(modesList, random);
-        modesList.add(activeMode);
-
         try {
             mDisplayManager.setShouldAlwaysRespectAppRequestedMode(true);
             assertTrue(mDisplayManager.shouldAlwaysRespectAppRequestedMode());
             mInitialRefreshRateSwitchingType =
                     DisplayUtil.getRefreshRateSwitchingType(mDisplayManager);
             mDisplayManager.setRefreshRateSwitchingType(DisplayManager.SWITCHING_TYPE_NONE);
+
             final DisplayTestActivity activity = launchActivity(mRetainedDisplayTestActivity);
+
+            // Create a deterministically shuffled list of display modes, which ends with the
+            // current active mode. We'll switch to the modes in this order. The active mode is last
+            // so we don't need an extra mode switch in case the test completes successfully.
+            Display.Mode activeMode = mDefaultDisplay.getMode();
+            List<Display.Mode> modesList = new ArrayList<>(modes.length);
+            for (Display.Mode mode : modes) {
+                if (mode.getModeId() != activeMode.getModeId()) {
+                    modesList.add(mode);
+                }
+            }
+            Random random = new Random(42);
+            Collections.shuffle(modesList, random);
+            modesList.add(activeMode);
+
             for (Display.Mode mode : modesList) {
                 testSwitchToModeId(activity, mode);
             }