Add support for testing in portrait mode

Bug: 110103162
Test: TaplTests
Change-Id: I7f633405c04984ea8f04acdc6dd4ad21019d3409
diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java
index 55074f8..151c761 100644
--- a/src/com/android/launcher3/Launcher.java
+++ b/src/com/android/launcher3/Launcher.java
@@ -867,17 +867,6 @@
         }
     }
 
-    public boolean hasSettings() {
-        if (mLauncherCallbacks != null) {
-            return mLauncherCallbacks.hasSettings();
-        } else {
-            // On O and above we there is always some setting present settings (add icon to
-            // home screen or icon badging). On earlier APIs we will have the allow rotation
-            // setting, on devices with a locked orientation,
-            return Utilities.ATLEAST_OREO || !getResources().getBoolean(R.bool.allow_rotation);
-        }
-    }
-
     public boolean isInState(LauncherState state) {
         return mStateManager.getState() == state;
     }
diff --git a/src/com/android/launcher3/states/RotationHelper.java b/src/com/android/launcher3/states/RotationHelper.java
index e866445..9c4a4ea 100644
--- a/src/com/android/launcher3/states/RotationHelper.java
+++ b/src/com/android/launcher3/states/RotationHelper.java
@@ -56,7 +56,7 @@
     private final Activity mActivity;
     private final SharedPreferences mPrefs;
 
-    private final boolean mIgnoreAutoRotateSettings;
+    private boolean mIgnoreAutoRotateSettings;
     private boolean mAutoRotateEnabled;
 
     /**
@@ -110,6 +110,13 @@
         }
     }
 
+    // Used by tests only.
+    public void forceAllowRotationForTesting(boolean allowRotation) {
+        mIgnoreAutoRotateSettings =
+                allowRotation || mActivity.getResources().getBoolean(R.bool.allow_rotation);
+        notifyChange();
+    }
+
     public void initialize() {
         if (!mInitialized) {
             mInitialized = true;
diff --git a/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java b/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java
index 5a3d36d..ba7d9c5 100644
--- a/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java
+++ b/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java
@@ -52,7 +52,6 @@
 import com.android.launcher3.tapl.LauncherInstrumentation;
 import com.android.launcher3.testcomponent.AppWidgetNoConfig;
 import com.android.launcher3.testcomponent.AppWidgetWithConfig;
-import com.android.launcher3.util.Condition;
 import com.android.launcher3.util.Wait;
 import com.android.launcher3.util.rule.LauncherActivityRule;
 
@@ -79,20 +78,23 @@
     public static final long DEFAULT_WORKER_TIMEOUT_SECS = 5;
 
     protected MainThreadExecutor mMainThreadExecutor = new MainThreadExecutor();
-    protected UiDevice mDevice;
-    protected LauncherInstrumentation mLauncher;
+    protected final UiDevice mDevice;
+    protected final LauncherInstrumentation mLauncher;
     protected Context mTargetContext;
     protected String mTargetPackage;
 
     private static final String TAG = "AbstractLauncherUiTest";
 
+    protected AbstractLauncherUiTest() {
+        mDevice = UiDevice.getInstance(getInstrumentation());
+        mLauncher = new LauncherInstrumentation(getInstrumentation());
+    }
+
     @Rule
     public LauncherActivityRule mActivityMonitor = new LauncherActivityRule();
 
     @Before
     public void setUp() throws Exception {
-        mDevice = UiDevice.getInstance(getInstrumentation());
-        mLauncher = new LauncherInstrumentation(getInstrumentation());
         mTargetContext = InstrumentationRegistry.getTargetContext();
         mTargetPackage = mTargetContext.getPackageName();
         mDevice.executeShellCommand("settings put global heads_up_notifications_enabled 0");
@@ -285,12 +287,7 @@
     // flakiness.
     protected boolean waitForLauncherCondition(
             Function<Launcher, Boolean> condition, long timeout) {
-        return Wait.atMost(new Condition() {
-            @Override
-            public boolean isTrue() {
-                return getFromLauncher(condition);
-            }
-        }, timeout);
+        return Wait.atMost(() -> getFromLauncher(condition), timeout);
     }
 
     /**
diff --git a/tests/tapl/com/android/launcher3/tapl/LauncherInstrumentation.java b/tests/tapl/com/android/launcher3/tapl/LauncherInstrumentation.java
index 9e4a615..7885e3c 100644
--- a/tests/tapl/com/android/launcher3/tapl/LauncherInstrumentation.java
+++ b/tests/tapl/com/android/launcher3/tapl/LauncherInstrumentation.java
@@ -25,6 +25,7 @@
 import android.os.Parcelable;
 import android.provider.Settings;
 import android.util.Log;
+import android.view.Surface;
 import android.view.accessibility.AccessibilityEvent;
 
 import androidx.annotation.NonNull;
@@ -92,6 +93,7 @@
     private final boolean mSwipeUpEnabled;
     private Boolean mSwipeUpEnabledOverride = null;
     private final Instrumentation mInstrumentation;
+    private int mExpectedRotation = Surface.ROTATION_0;
 
     /**
      * Constructs the root of TAPL hierarchy. You get all other objects from it.
@@ -109,7 +111,7 @@
         assertTrue("Device must run in a test harness", ActivityManager.isRunningInTestHarness());
     }
 
-    // Used only by tests.
+    // Used only by TaplTests.
     public void overrideSwipeUpEnabled(Boolean swipeUpEnabledOverride) {
         mSwipeUpEnabledOverride = swipeUpEnabledOverride;
     }
@@ -144,14 +146,30 @@
         fail(message + ". " + "Actual: " + actual);
     }
 
+    static public void assertEquals(String message, int expected, int actual) {
+        if (expected != actual) {
+            fail(message + " expected: " + expected + " but was: " + actual);
+        }
+    }
+
     static void assertNotEquals(String message, int unexpected, int actual) {
         if (unexpected == actual) {
             failEquals(message, actual);
         }
     }
 
+    public void setExpectedRotation(int expectedRotation) {
+        mExpectedRotation = expectedRotation;
+    }
+
     private UiObject2 verifyContainerType(ContainerType containerType) {
+        assertEquals("Unexpected display rotation",
+                mExpectedRotation, mDevice.getDisplayRotation());
+        assertTrue("Presence of recents button doesn't match isSwipeUpEnabled()",
+                isSwipeUpEnabled() ==
+                        (mDevice.findObject(By.res(SYSTEMUI_PACKAGE, "recent_apps")) == null));
         log("verifyContainerType: " + containerType);
+
         switch (containerType) {
             case WORKSPACE: {
                 waitUntilGone(APPS_RES_ID);
@@ -172,7 +190,11 @@
                 return waitForLauncherObject(APPS_RES_ID);
             }
             case OVERVIEW: {
-                waitForLauncherObject(APPS_RES_ID);
+                if (mDevice.isNaturalOrientation()) {
+                    waitForLauncherObject(APPS_RES_ID);
+                } else {
+                    waitUntilGone(APPS_RES_ID);
+                }
                 waitUntilGone(WORKSPACE_RES_ID);
                 waitUntilGone(WIDGETS_RES_ID);
                 return waitForLauncherObject(OVERVIEW_RES_ID);