Merge "Fix tests that request screen orientation change" into sc-mainline-prod
diff --git a/tests/src/com/android/providers/media/photopicker/espresso/OrientationUtils.java b/tests/src/com/android/providers/media/photopicker/espresso/OrientationUtils.java
new file mode 100644
index 0000000..3ca9163
--- /dev/null
+++ b/tests/src/com/android/providers/media/photopicker/espresso/OrientationUtils.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.providers.media.photopicker.espresso;
+
+import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
+import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
+import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
+import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
+
+import static androidx.test.espresso.Espresso.onIdle;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import androidx.test.ext.junit.rules.ActivityScenarioRule;
+
+class OrientationUtils {
+    public static void setLandscapeOrientation(ActivityScenarioRule<PhotoPickerTestActivity> rule) {
+        changeOrientation(rule, SCREEN_ORIENTATION_LANDSCAPE, ORIENTATION_LANDSCAPE);
+    }
+
+    public static void setPortraitOrientation(ActivityScenarioRule<PhotoPickerTestActivity> rule) {
+        changeOrientation(rule, SCREEN_ORIENTATION_PORTRAIT, ORIENTATION_PORTRAIT);
+    }
+
+    private static void changeOrientation(ActivityScenarioRule<PhotoPickerTestActivity> rule,
+            int screenOrientation, int configOrientation) {
+        rule.getScenario().onActivity(activity -> {
+            activity.setRequestedOrientation(screenOrientation);
+        });
+
+        onIdle();
+
+        rule.getScenario().onActivity(activity -> {
+            assertThat(activity.getResources().getConfiguration().orientation)
+                    .isEqualTo(configOrientation);
+        });
+    }
+}
diff --git a/tests/src/com/android/providers/media/photopicker/espresso/PreviewMultiSelectLongPressTest.java b/tests/src/com/android/providers/media/photopicker/espresso/PreviewMultiSelectLongPressTest.java
index eccdb26..ee5d381 100644
--- a/tests/src/com/android/providers/media/photopicker/espresso/PreviewMultiSelectLongPressTest.java
+++ b/tests/src/com/android/providers/media/photopicker/espresso/PreviewMultiSelectLongPressTest.java
@@ -16,11 +16,6 @@
 
 package com.android.providers.media.photopicker.espresso;
 
-import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
-import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
-import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
-import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
-
 import static androidx.test.espresso.Espresso.onView;
 import static androidx.test.espresso.action.ViewActions.click;
 import static androidx.test.espresso.assertion.ViewAssertions.doesNotExist;
@@ -32,6 +27,8 @@
 
 import static com.android.providers.media.photopicker.espresso.CustomSwipeAction.swipeLeftAndWait;
 import static com.android.providers.media.photopicker.espresso.CustomSwipeAction.swipeRightAndWait;
+import static com.android.providers.media.photopicker.espresso.OrientationUtils.setLandscapeOrientation;
+import static com.android.providers.media.photopicker.espresso.OrientationUtils.setPortraitOrientation;
 import static com.android.providers.media.photopicker.espresso.RecyclerViewTestUtils.*;
 import static com.android.providers.media.photopicker.espresso.RecyclerViewTestUtils.assertItemNotSelected;
 import static com.android.providers.media.photopicker.espresso.RecyclerViewTestUtils.assertItemSelected;
@@ -201,12 +198,8 @@
         onView(withId(PREVIEW_ADD_OR_SELECT_BUTTON_ID)).check(matches(isDisplayed()));
         onView(withId(PREVIEW_ADD_OR_SELECT_BUTTON_ID)).check(matches(withText(R.string.select)));
 
+        setPortraitOrientation(mRule);
         mRule.getScenario().onActivity(activity -> {
-            activity.setRequestedOrientation(SCREEN_ORIENTATION_PORTRAIT);
-        });
-        mRule.getScenario().onActivity(activity -> {
-            assertThat(activity.getResources().getConfiguration().orientation)
-                    .isEqualTo(ORIENTATION_PORTRAIT);
             final Button addOrSelectButton
                     = activity.findViewById(PREVIEW_ADD_OR_SELECT_BUTTON_ID);
             final int expectedAddOrSelectButtonWidth = activity.getResources()
@@ -215,12 +208,8 @@
             assertThat(addOrSelectButton.getWidth()).isEqualTo(expectedAddOrSelectButtonWidth);
         });
 
+        setLandscapeOrientation(mRule);
         mRule.getScenario().onActivity(activity -> {
-            activity.setRequestedOrientation(SCREEN_ORIENTATION_LANDSCAPE);
-        });
-        mRule.getScenario().onActivity(activity -> {
-            assertThat(activity.getResources().getConfiguration().orientation)
-                    .isEqualTo(ORIENTATION_LANDSCAPE);
             final Button addOrSelectButton
                     = activity.findViewById(PREVIEW_ADD_OR_SELECT_BUTTON_ID);
             final int expectedAddOrSelectButtonWidth = activity.getResources()
diff --git a/tests/src/com/android/providers/media/photopicker/espresso/PreviewSingleSelectTest.java b/tests/src/com/android/providers/media/photopicker/espresso/PreviewSingleSelectTest.java
index 8d7ae52..9892778 100644
--- a/tests/src/com/android/providers/media/photopicker/espresso/PreviewSingleSelectTest.java
+++ b/tests/src/com/android/providers/media/photopicker/espresso/PreviewSingleSelectTest.java
@@ -16,12 +16,8 @@
 
 package com.android.providers.media.photopicker.espresso;
 
-import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
-import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
-import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
-import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
-
 import static androidx.test.espresso.Espresso.onView;
+import static androidx.test.espresso.Espresso.onIdle;
 import static androidx.test.espresso.action.ViewActions.click;
 import static androidx.test.espresso.assertion.ViewAssertions.doesNotExist;
 import static androidx.test.espresso.assertion.ViewAssertions.matches;
@@ -33,6 +29,8 @@
 import static androidx.test.espresso.matcher.ViewMatchers.withText;
 
 import static com.android.providers.media.photopicker.espresso.BottomSheetTestUtils.assertBottomSheetState;
+import static com.android.providers.media.photopicker.espresso.OrientationUtils.setLandscapeOrientation;
+import static com.android.providers.media.photopicker.espresso.OrientationUtils.setPortraitOrientation;
 import static com.android.providers.media.photopicker.espresso.RecyclerViewTestUtils.longClickItem;
 
 import static com.google.android.material.bottomsheet.BottomSheetBehavior.STATE_COLLAPSED;
@@ -171,14 +169,7 @@
 
     @Test
     public void testPreview_noScrimLayerAndHasSolidColorInPortrait() {
-        mRule.getScenario().onActivity(activity -> {
-            activity.setRequestedOrientation(SCREEN_ORIENTATION_PORTRAIT);
-        });
-
-        mRule.getScenario().onActivity(activity -> {
-            assertThat(activity.getResources().getConfiguration().orientation)
-                    .isEqualTo(ORIENTATION_PORTRAIT);
-        });
+        setPortraitOrientation(mRule);
 
         onView(withId(PICKER_TAB_RECYCLERVIEW_ID)).check(matches(isDisplayed()));
         // Navigate to preview
@@ -196,14 +187,7 @@
 
     @Test
     public void testPreview_showScrimLayerInLandscape() {
-        mRule.getScenario().onActivity(activity -> {
-            activity.setRequestedOrientation(SCREEN_ORIENTATION_LANDSCAPE);
-        });
-
-        mRule.getScenario().onActivity(activity -> {
-            assertThat(activity.getResources().getConfiguration().orientation)
-                    .isEqualTo(ORIENTATION_LANDSCAPE);
-        });
+        setLandscapeOrientation(mRule);
 
         onView(withId(PICKER_TAB_RECYCLERVIEW_ID)).check(matches(isDisplayed()));
 
@@ -231,12 +215,8 @@
         onView(withId(PREVIEW_ADD_OR_SELECT_BUTTON_ID)).check(matches(isDisplayed()));
         onView(withId(PREVIEW_ADD_OR_SELECT_BUTTON_ID)).check(matches(withText(R.string.add)));
 
+        setPortraitOrientation(mRule);
         mRule.getScenario().onActivity(activity -> {
-            activity.setRequestedOrientation(ORIENTATION_PORTRAIT);
-        });
-        mRule.getScenario().onActivity(activity -> {
-            assertThat(activity.getResources().getConfiguration().orientation)
-                    .isEqualTo(ORIENTATION_PORTRAIT);
             final Button addOrSelectButton
                     = activity.findViewById(PREVIEW_ADD_OR_SELECT_BUTTON_ID);
             final int expectedAddOrSelectButtonWidth = activity.getResources()
@@ -245,12 +225,8 @@
             assertThat(addOrSelectButton.getWidth()).isEqualTo(expectedAddOrSelectButtonWidth);
         });
 
+        setLandscapeOrientation(mRule);
         mRule.getScenario().onActivity(activity -> {
-            activity.setRequestedOrientation(SCREEN_ORIENTATION_LANDSCAPE);
-        });
-        mRule.getScenario().onActivity(activity -> {
-            assertThat(activity.getResources().getConfiguration().orientation)
-                    .isEqualTo(ORIENTATION_LANDSCAPE);
             final Button addOrSelectButton
                     = activity.findViewById(PREVIEW_ADD_OR_SELECT_BUTTON_ID);
             final int expectedAddOrSelectButtonWidth = activity.getResources()
@@ -263,7 +239,7 @@
     private void registerIdlingResourceAndWaitForIdle() {
         mRule.getScenario().onActivity((activity -> IdlingRegistry.getInstance().register(
                 new ViewPager2IdlingResource(activity.findViewById(R.id.preview_viewPager)))));
-        Espresso.onIdle();
+        onIdle();
     }
 
     private void assertBackgroundColorOnToolbarAndBottomBar(Activity activity, int colorResId) {