Fix DockConfigChangeTests

The tests assumed that devices would be rotated to 270 degrees upon
docking but this isn't true as emulators rotate to 0 degrees.

This fix reads the config_deskDockRotation value to determine the
docked rotation. This is the same value used to actually rotate the
device on dock.

Bug: 295876429
Bug: 294255156
Test: atest DockConfigChangeTests
      manually verified on UDC build and main build
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:4e75b9b88c914b81df878509e3334292d85f6bf3)
Merged-In: I961f6692d67edf559d95f25981f765be17d58786
Change-Id: I961f6692d67edf559d95f25981f765be17d58786
diff --git a/tests/framework/base/windowmanager/src/android/server/wm/DockConfigChangeTests.java b/tests/framework/base/windowmanager/src/android/server/wm/DockConfigChangeTests.java
index 3e2a292..f10fe84 100644
--- a/tests/framework/base/windowmanager/src/android/server/wm/DockConfigChangeTests.java
+++ b/tests/framework/base/windowmanager/src/android/server/wm/DockConfigChangeTests.java
@@ -21,7 +21,6 @@
 import static android.content.res.Configuration.UI_MODE_TYPE_NORMAL;
 import static android.server.wm.app.Components.TEST_ACTIVITY;
 import static android.server.wm.deskresources.Components.DESK_RESOURCES_ACTIVITY;
-import static android.view.Surface.ROTATION_270;
 
 import static com.android.compatibility.common.util.SystemUtil.runShellCommand;
 
@@ -31,6 +30,7 @@
 import android.content.Intent;
 import android.content.res.Resources;
 import android.platform.test.annotations.Presubmit;
+import android.view.Surface;
 
 import org.junit.Test;
 
@@ -47,10 +47,10 @@
         // enabled.
         assumeTrue(getConfigSkipRelaunchOnDock());
 
-        // Set rotation to 270 first, since docking forces rotation to 270, causing an extra config
-        // change.
+        // Set rotation to the same rotation as the device would be rotated to after docking. This
+        // prevents an extraneous config change from the device rotating when docked/undocked.
         RotationSession rotationSession = createManagedRotationSession();
-        rotationSession.set(ROTATION_270, /*waitDeviceRotation=*/ true);
+        rotateToDockRotation(rotationSession);
 
         launchActivity(TEST_ACTIVITY);
         waitAndAssertResumedActivity(TEST_ACTIVITY, "Activity must be resumed");
@@ -80,10 +80,10 @@
         // enabled.
         assumeTrue(getConfigSkipRelaunchOnDock());
 
-        // Set rotation to 270 first, since docking forces rotation to 270, causing an extra config
-        // change.
+        // Set rotation to the same rotation as the device would be rotated to after docking. This
+        // prevents an extraneous config change from the device rotating when docked/undocked.
         RotationSession rotationSession = createManagedRotationSession();
-        rotationSession.set(ROTATION_270, /*waitDeviceRotation=*/ true);
+        rotateToDockRotation(rotationSession);
 
         launchActivity(DESK_RESOURCES_ACTIVITY);
         waitAndAssertResumedActivity(DESK_RESOURCES_ACTIVITY, "Activity must be resumed");
@@ -115,6 +115,43 @@
                         "bool", "android"));
     }
 
+    /**
+     * Rotates the device to the same rotation as it would rotate to when docked.
+     *
+     * Dock rotation is read from config_deskDockRotation.
+     */
+    void rotateToDockRotation(RotationSession rotationSession) {
+        int rotation = rotationDegreesToConst(mContext.getResources().getInteger(
+                Resources.getSystem().getIdentifier("config_deskDockRotation",
+                        "integer", "android")));
+        if (rotation == -1) {
+            // -1 could come from the const itself, which means no rotation on dock, or from
+            // rotationDegreesToConst, which means we got an unexpected value from the resource.
+            return;
+        }
+        rotationSession.set(rotation);
+    }
+
+    /**
+     * Converts from a rotation in degrees to a {@link Surface.Rotation} constant.
+     *
+     * Returns -1 if a value that doesn't match a {@link Surface.Rotation} constant is provided.
+     */
+    private int rotationDegreesToConst(int rotationDegrees) {
+        switch (rotationDegrees) {
+            case 0:
+                return Surface.ROTATION_0;
+            case 90:
+                return Surface.ROTATION_90;
+            case 180:
+                return Surface.ROTATION_180;
+            case 270:
+                return Surface.ROTATION_270;
+        }
+        return -1;
+    }
+
+
     private class DockTestSession implements AutoCloseable {
         private void dock() {
             runShellCommand("dumpsys DockObserver set state "