Fix navbar position assertion

Fixes: 284199070
Test: atest FlickerTestsRotation
Change-Id: Ie1b9c81af501e46944ff352b3094d12864351c51
diff --git a/libraries/flicker/src/android/tools/common/Position.kt b/libraries/flicker/src/android/tools/common/Position.kt
new file mode 100644
index 0000000..e06dec1
--- /dev/null
+++ b/libraries/flicker/src/android/tools/common/Position.kt
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2023 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.
+ */
+
+/*
+ * Copyright (C) 2023 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 android.tools.common
+
+enum class Position {
+    TOP,
+    BOTTOM,
+    LEFT,
+    RIGHT,
+    INVALID
+}
diff --git a/libraries/flicker/src/android/tools/common/flicker/subject/region/IRegionSubject.kt b/libraries/flicker/src/android/tools/common/flicker/subject/region/IRegionSubject.kt
index f28053a..7915434 100644
--- a/libraries/flicker/src/android/tools/common/flicker/subject/region/IRegionSubject.kt
+++ b/libraries/flicker/src/android/tools/common/flicker/subject/region/IRegionSubject.kt
@@ -215,4 +215,36 @@
      * @throws AssertionError
      */
     fun isSameAspectRatio(other: Region, threshold: Double): IRegionSubject
+
+    /**
+     * Asserts that region has the same top coordinates as [displayRect]
+     *
+     * @param displayRect Display area
+     * @throws AssertionError
+     */
+    fun hasSameTopPosition(displayRect: Rect): IRegionSubject
+
+    /**
+     * Asserts that region has the same bottom coordinates as [displayRect]
+     *
+     * @param displayRect Display area
+     * @throws AssertionError
+     */
+    fun hasSameBottomPosition(displayRect: Rect): IRegionSubject
+
+    /**
+     * Asserts that region has the same right coordinates as [displayRect]
+     *
+     * @param displayRect Display area
+     * @throws AssertionError
+     */
+    fun hasSameRightPosition(displayRect: Rect): IRegionSubject
+
+    /**
+     * Asserts that region has the same left coordinates as [displayRect]
+     *
+     * @param displayRect Display area
+     * @throws AssertionError
+     */
+    fun hasSameLeftPosition(displayRect: Rect): IRegionSubject
 }
diff --git a/libraries/flicker/src/android/tools/common/flicker/subject/region/RegionSubject.kt b/libraries/flicker/src/android/tools/common/flicker/subject/region/RegionSubject.kt
index 3121762..ae3721c 100644
--- a/libraries/flicker/src/android/tools/common/flicker/subject/region/RegionSubject.kt
+++ b/libraries/flicker/src/android/tools/common/flicker/subject/region/RegionSubject.kt
@@ -397,6 +397,24 @@
         }
     }
 
+    /** {@inheritDoc} */
+    override fun hasSameBottomPosition(displayRect: Rect): RegionSubject = apply {
+        assertEquals("bottom", Region(arrayOf(displayRect))) { it.bottom }
+    }
+
+    /** {@inheritDoc} */
+    override fun hasSameTopPosition(displayRect: Rect): RegionSubject = apply {
+        assertEquals("top", Region(arrayOf(displayRect))) { it.top }
+    }
+
+    override fun hasSameLeftPosition(displayRect: Rect): RegionSubject = apply {
+        assertEquals("left", Region(arrayOf(displayRect))) { it.left }
+    }
+
+    override fun hasSameRightPosition(displayRect: Rect): RegionSubject = apply {
+        assertEquals("right", Region(arrayOf(displayRect))) { it.right }
+    }
+
     fun isSameAspectRatio(other: RegionSubject, threshold: Double = 0.1): IRegionSubject =
         isSameAspectRatio(other.region, threshold)
 
diff --git a/libraries/flicker/src/android/tools/common/flicker/subject/region/RegionTraceSubject.kt b/libraries/flicker/src/android/tools/common/flicker/subject/region/RegionTraceSubject.kt
index 40229c4..c5cd402 100644
--- a/libraries/flicker/src/android/tools/common/flicker/subject/region/RegionTraceSubject.kt
+++ b/libraries/flicker/src/android/tools/common/flicker/subject/region/RegionTraceSubject.kt
@@ -142,4 +142,20 @@
             it.isSameAspectRatio(other, threshold)
         }
     }
+
+    override fun hasSameLeftPosition(displayRect: Rect): RegionTraceSubject = apply {
+        addAssertion("hasSameLeftPosition($displayRect") { it.hasSameLeftPosition(displayRect) }
+    }
+
+    override fun hasSameBottomPosition(displayRect: Rect): RegionTraceSubject = apply {
+        addAssertion("hasSameBottomPosition($displayRect") { it.hasSameBottomPosition(displayRect) }
+    }
+
+    override fun hasSameRightPosition(displayRect: Rect): RegionTraceSubject = apply {
+        addAssertion("hasSameRightPosition($displayRect") { it.hasSameRightPosition(displayRect) }
+    }
+
+    override fun hasSameTopPosition(displayRect: Rect): RegionTraceSubject = apply {
+        addAssertion("hasSameTopPosition($displayRect") { it.hasSameTopPosition(displayRect) }
+    }
 }
diff --git a/libraries/flicker/src/android/tools/common/traces/surfaceflinger/Display.kt b/libraries/flicker/src/android/tools/common/traces/surfaceflinger/Display.kt
index eb721ed..b66cc2c 100644
--- a/libraries/flicker/src/android/tools/common/traces/surfaceflinger/Display.kt
+++ b/libraries/flicker/src/android/tools/common/traces/surfaceflinger/Display.kt
@@ -16,6 +16,8 @@
 
 package android.tools.common.traces.surfaceflinger
 
+import android.tools.common.Position
+import android.tools.common.Rotation
 import android.tools.common.datatypes.Rect
 import android.tools.common.datatypes.Size
 import android.tools.common.withCache
@@ -40,6 +42,22 @@
     // Alias for layerStackSpace, since bounds is what is used for layers
     val bounds = layerStackSpace
 
+    fun navBarPosition(isGesturalNavigation: Boolean): Position {
+        val requestedRotation = transform.getRotation()
+
+        return when {
+            // display off
+            isOff -> Position.INVALID
+            // nav bar is at the bottom of the screen
+            !requestedRotation.isRotated() || isGesturalNavigation -> Position.BOTTOM
+            // nav bar is on the right side
+            requestedRotation == Rotation.ROTATION_90 -> Position.RIGHT
+            // nav bar is on the left side
+            requestedRotation == Rotation.ROTATION_270 -> Position.LEFT
+            else -> error("Unknown rotation $requestedRotation")
+        }
+    }
+
     override fun equals(other: Any?): Boolean {
         if (this === other) return true
         if (other !is Display) return false