Improved setOrientation() by waiting until it effectively changes.

Test: cts-tradefed run commandAndExit cts-dev -m CtsAutoFillServiceTestCases

Fixes: 63756784

Change-Id: Icf91451cef32cb92c37533495b74e05de82f78f6
diff --git a/tests/autofillservice/src/android/autofillservice/cts/DuplicateIdActivityTest.java b/tests/autofillservice/src/android/autofillservice/cts/DuplicateIdActivityTest.java
index a112b88..14ca6a0 100644
--- a/tests/autofillservice/src/android/autofillservice/cts/DuplicateIdActivityTest.java
+++ b/tests/autofillservice/src/android/autofillservice/cts/DuplicateIdActivityTest.java
@@ -47,8 +47,8 @@
 
     @Before
     public void setup() {
-        Helper.disableAutoRotation();
-        Helper.setOrientation(0);
+        Helper.disableAutoRotation(sUiBot);
+        Helper.setOrientation(sUiBot, 0);
 
         mActivity = mActivityRule.getActivity();
     }
@@ -112,7 +112,7 @@
         sReplier.addResponse(NO_RESPONSE);
 
         // Force rotation to force onDestroy->onCreate cycle
-        Helper.setOrientation(1);
+        Helper.setOrientation(sUiBot, 1);
 
         // Select other field to trigger new partition
         runShellCommand("input keyevent KEYCODE_TAB");
diff --git a/tests/autofillservice/src/android/autofillservice/cts/Helper.java b/tests/autofillservice/src/android/autofillservice/cts/Helper.java
index c490913..98adf3d 100644
--- a/tests/autofillservice/src/android/autofillservice/cts/Helper.java
+++ b/tests/autofillservice/src/android/autofillservice/cts/Helper.java
@@ -63,10 +63,10 @@
     static final String ID_LOGIN = "login";
     static final String ID_OUTPUT = "output";
 
-    /** Pass to {@link #setOrientation(int)} to change the display to portrait mode */
+    /** Pass to {@link #setOrientation(UiBot, int)} to change the display to portrait mode */
     public static int PORTRAIT = 0;
 
-    /** Pass to {@link #setOrientation(int)} to change the display to landscape mode */
+    /** Pass to {@link #setOrientation(UiBot, int)} to change the display to landscape mode */
     public static int LANDSCAPE = 1;
 
     /**
@@ -100,6 +100,11 @@
     static final int UI_TIMEOUT_MS = 2000;
 
     /**
+     * Timeout (in milliseconds) for changing the screen orientation.
+     */
+    static final int UI_SCREEN_ORIENTATION_TIMEOUT_MS = 10000;
+
+    /**
      * Time to wait in between retries
      */
     static final int RETRY_MS = 100;
@@ -613,9 +618,9 @@
     /**
      * Prevents the screen to rotate by itself
      */
-    public static void disableAutoRotation() {
+    public static void disableAutoRotation(UiBot uiBot) {
         runShellCommand(ACCELLEROMETER_CHANGE, 0);
-        setOrientation(PORTRAIT);
+        setOrientation(uiBot, PORTRAIT);
     }
 
     /**
@@ -631,8 +636,24 @@
      *
      * @param value {@link #PORTRAIT} or {@link #LANDSCAPE};
      */
-    public static void setOrientation(int value) {
-        runShellCommand(ORIENTATION_CHANGE, value);
+    public static void setOrientation(UiBot uiBot, int value) {
+        long startTime = System.currentTimeMillis();
+
+        while (System.currentTimeMillis() - startTime < UI_SCREEN_ORIENTATION_TIMEOUT_MS) {
+            runShellCommand(ORIENTATION_CHANGE, value);
+            final int actualValue = uiBot.getOrientation();
+            if (actualValue == value) {
+                return;
+            }
+            Log.d(TAG, "setOrientation(): sleeping until " + actualValue + " == " + value);
+            try {
+                Thread.sleep(RETRY_MS);
+            } catch (InterruptedException e) {
+                Thread.currentThread().interrupt();
+            }
+        }
+        throw new RetryableException("Screen orientation didn't change to %d in %d ms", value,
+                UI_SCREEN_ORIENTATION_TIMEOUT_MS);
     }
 
     /**
@@ -640,7 +661,7 @@
      *
      * @return The pid of the process
      */
-    public static int getOutOfProcessPid(@NonNull String processName) throws InterruptedException {
+    public static int getOutOfProcessPid(@NonNull String processName) {
         long startTime = System.currentTimeMillis();
 
         while (System.currentTimeMillis() - startTime < UI_TIMEOUT_MS) {
@@ -654,7 +675,11 @@
                 }
             }
 
-            Thread.sleep(RETRY_MS);
+            try {
+                Thread.sleep(RETRY_MS);
+            } catch (InterruptedException e) {
+                Thread.currentThread().interrupt();
+            }
         }
 
         throw new IllegalStateException("process not found");
diff --git a/tests/autofillservice/src/android/autofillservice/cts/SessionLifecycleTest.java b/tests/autofillservice/src/android/autofillservice/cts/SessionLifecycleTest.java
index b0f5756..15dd4b5 100644
--- a/tests/autofillservice/src/android/autofillservice/cts/SessionLifecycleTest.java
+++ b/tests/autofillservice/src/android/autofillservice/cts/SessionLifecycleTest.java
@@ -67,7 +67,7 @@
      */
     @Before
     public void disableAutoRotation() {
-        Helper.disableAutoRotation();
+        Helper.disableAutoRotation(sUiBot);
     }
 
     /**
@@ -134,7 +134,7 @@
 
         // Change orientation which triggers a destroy -> create in the app as the activity
         // cannot deal with such situations
-        setOrientation(LANDSCAPE);
+        setOrientation(sUiBot, LANDSCAPE);
 
         // Delete stopped marker
         getStoppedMarker(getContext()).delete();
@@ -147,7 +147,7 @@
 
         // Change orientation which triggers a destroy -> create in the app as the activity
         // cannot deal with such situations
-        setOrientation(PORTRAIT);
+        setOrientation(sUiBot, PORTRAIT);
 
         // Approve authentication
         sUiBot.selectById(BUTTON_FULL_ID);
@@ -157,7 +157,7 @@
 
         // Change orientation which triggers a destroy -> create in the app as the activity
         // cannot deal with such situations
-        setOrientation(LANDSCAPE);
+        setOrientation(sUiBot, LANDSCAPE);
 
         // Select dataset
         sUiBot.selectDataset("dataset");
@@ -176,7 +176,7 @@
         sUiBot.assertShownById("android:id/autofill_save_yes");
 
         // Change orientation to make sure save UI can handle this
-        setOrientation(PORTRAIT);
+        setOrientation(sUiBot, PORTRAIT);
 
         // Tap "Save".
         sUiBot.selectById("android:id/autofill_save_yes");
diff --git a/tests/autofillservice/src/android/autofillservice/cts/UiBot.java b/tests/autofillservice/src/android/autofillservice/cts/UiBot.java
index 281071e..67a036d 100644
--- a/tests/autofillservice/src/android/autofillservice/cts/UiBot.java
+++ b/tests/autofillservice/src/android/autofillservice/cts/UiBot.java
@@ -533,4 +533,13 @@
         }
         throw new RetryableException("Title '%s' not found for %s", expectedTitle, object);
     }
+
+    /**
+     * Gets the value of the screen orientation.
+     *
+     * @return typically {@link Helper#LANDSCAPE} or {@link Helper#PORTRAIT}.
+     */
+    public int getOrientation() {
+        return mDevice.getDisplayRotation();
+    }
 }