Improved setOrientation() by waiting until it effectively changes.
Test: cts-tradefed run commandAndExit cts-dev -m CtsAutoFillServiceTestCases
Fixes: 63756784
Merged-In: Icf91451cef32cb92c37533495b74e05de82f78f6
Change-Id: Icf91451cef32cb92c37533495b74e05de82f78f6
diff --git a/tests/autofillservice/src/android/autofillservice/cts/Helper.java b/tests/autofillservice/src/android/autofillservice/cts/Helper.java
index 7c91578..52abf95 100644
--- a/tests/autofillservice/src/android/autofillservice/cts/Helper.java
+++ b/tests/autofillservice/src/android/autofillservice/cts/Helper.java
@@ -64,10 +64,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;
/**
@@ -101,6 +101,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;
@@ -614,9 +619,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);
}
/**
@@ -632,8 +637,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);
}
/**
@@ -641,7 +662,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) {
@@ -655,7 +676,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 1601512..4109d31 100644
--- a/tests/autofillservice/src/android/autofillservice/cts/SessionLifecycleTest.java
+++ b/tests/autofillservice/src/android/autofillservice/cts/SessionLifecycleTest.java
@@ -66,7 +66,7 @@
*/
@Before
public void disableAutoRotation() {
- Helper.disableAutoRotation();
+ Helper.disableAutoRotation(sUiBot);
}
/**
@@ -133,7 +133,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();
@@ -146,7 +146,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);
@@ -156,7 +156,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");
@@ -175,7 +175,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();
+ }
}