Fixed test cases that rotated the device after the Save UI was shown.

These tests rotated the device back to portrait, which triggers a new session.

They used to pass because AutoFillServiceTestCase defines an @After
ignoreFurtherRequests() method that ignore them, but now somehow the timing has
changed so the new request is made before the @After rule is called

This CL properly fix the problem by explicitly waiting for that request.

Test: atest CtsAutoFillServiceTestCases:SimpleSaveActivityTest
Test: atest CtsAutoFillServiceTestCases

Bug: 70348203

Change-Id: I11aca60bab80824f80aebf408ca243f6c2635dbf
Merged-In: I11aca60bab80824f80aebf408ca243f6c2635dbf
(cherry picked from commit db044c2f5356685569499ba99d854865c4b0fdb6)
diff --git a/tests/autofillservice/src/android/autofillservice/cts/AutoFillServiceTestCase.java b/tests/autofillservice/src/android/autofillservice/cts/AutoFillServiceTestCase.java
index 74e168b..e72d85d 100644
--- a/tests/autofillservice/src/android/autofillservice/cts/AutoFillServiceTestCase.java
+++ b/tests/autofillservice/src/android/autofillservice/cts/AutoFillServiceTestCase.java
@@ -64,6 +64,7 @@
 
     protected final Context mContext;
     protected final String mPackageName;
+
     /**
      * Stores the previous logging level so it's restored after the test.
      */
diff --git a/tests/autofillservice/src/android/autofillservice/cts/CustomDescriptionWithLinkTestCase.java b/tests/autofillservice/src/android/autofillservice/cts/CustomDescriptionWithLinkTestCase.java
index 210fab8..e8b183c 100644
--- a/tests/autofillservice/src/android/autofillservice/cts/CustomDescriptionWithLinkTestCase.java
+++ b/tests/autofillservice/src/android/autofillservice/cts/CustomDescriptionWithLinkTestCase.java
@@ -68,6 +68,7 @@
                     PostSaveLinkTappedAction.ROTATE_THEN_TAP_BACK_BUTTON);
         } finally {
             sUiBot.setScreenOrientation(UiBot.PORTRAIT);
+            cleanUpAfterScreenOrientationIsBackToPortrait();
         }
     }
 
@@ -84,6 +85,9 @@
     protected abstract void saveUiRestoredAfterTappingLinkTest(PostSaveLinkTappedAction type)
             throws Exception;
 
+    protected void cleanUpAfterScreenOrientationIsBackToPortrait() throws Exception {
+    }
+
     /**
      * Tests scenarios when user taps a link in the custom description, taps back to return to the
      * activity with the Save UI, and touch outside the Save UI to dismiss it.
diff --git a/tests/autofillservice/src/android/autofillservice/cts/SimpleSaveActivityTest.java b/tests/autofillservice/src/android/autofillservice/cts/SimpleSaveActivityTest.java
index 8e4ec03..24b5e4c 100644
--- a/tests/autofillservice/src/android/autofillservice/cts/SimpleSaveActivityTest.java
+++ b/tests/autofillservice/src/android/autofillservice/cts/SimpleSaveActivityTest.java
@@ -173,6 +173,7 @@
             saveTest(true);
         } finally {
             sUiBot.setScreenOrientation(UiBot.PORTRAIT);
+            cleanUpAfterScreenOrientationIsBackToPortrait();
         }
     }
 
@@ -200,6 +201,9 @@
         UiObject2 saveUi = sUiBot.assertSaveShowing(SAVE_DATA_TYPE_GENERIC);
 
         if (rotate) {
+            // After the device rotates, the input field get focus and generate a new session.
+            sReplier.addResponse(CannedFillResponse.NO_RESPONSE);
+
             sUiBot.setScreenOrientation(UiBot.LANDSCAPE);
             saveUi = sUiBot.assertSaveShowing(SAVE_DATA_TYPE_GENERIC);
         }
@@ -493,6 +497,9 @@
         // .. then do something to return to previous activity...
         switch (type) {
             case ROTATE_THEN_TAP_BACK_BUTTON:
+                // After the device rotates, the input field get focus and generate a new session.
+                sReplier.addResponse(CannedFillResponse.NO_RESPONSE);
+
                 sUiBot.setScreenOrientation(UiBot.LANDSCAPE);
                 // not breaking on purpose
             case TAP_BACK_BUTTON:
@@ -515,6 +522,12 @@
 
         final SaveRequest saveRequest = sReplier.getNextSaveRequest();
         assertTextAndValue(findNodeByResourceId(saveRequest.structure, ID_INPUT), "108");
+
+    }
+
+    @Override
+    protected void cleanUpAfterScreenOrientationIsBackToPortrait() throws Exception {
+        sReplier.getNextFillRequest();
     }
 
     @Override