Add some unit tests for ViewUtils

Bug: 168844996
Test: atest com.android.car.ui.utils.ViewUtilsTest
Change-Id: Iff674078aba8856fabadb7acc05ca2b73acd0fc4
diff --git a/car-ui-lib/car-ui-lib/src/androidTest/java/com/android/car/ui/utils/ViewUtilsTest.java b/car-ui-lib/car-ui-lib/src/androidTest/java/com/android/car/ui/utils/ViewUtilsTest.java
index 76797e3..ccb9d08 100644
--- a/car-ui-lib/car-ui-lib/src/androidTest/java/com/android/car/ui/utils/ViewUtilsTest.java
+++ b/car-ui-lib/car-ui-lib/src/androidTest/java/com/android/car/ui/utils/ViewUtilsTest.java
@@ -20,6 +20,12 @@
 import static android.view.View.INVISIBLE;
 import static android.view.View.VISIBLE;
 
+import static com.android.car.ui.utils.ViewUtils.DEFAULT_FOCUS;
+import static com.android.car.ui.utils.ViewUtils.FOCUSED_BY_DEFAULT;
+import static com.android.car.ui.utils.ViewUtils.IMPLICIT_DEFAULT_FOCUS;
+import static com.android.car.ui.utils.ViewUtils.NO_FOCUS;
+import static com.android.car.ui.utils.ViewUtils.REGULAR_FOCUS;
+
 import static com.google.common.truth.Truth.assertThat;
 
 import android.view.LayoutInflater;
@@ -93,6 +99,34 @@
     }
 
     @Test
+    public void testGetAncestorFocusArea() {
+        mRoot.post(() -> assertThat(ViewUtils.getAncestorFocusArea(mView2)).isEqualTo(mFocusArea2));
+    }
+
+    @Test
+    public void testGetAncestorFocusArea_doesNotReturnItself() {
+        mRoot.post(() -> assertThat(ViewUtils.getAncestorFocusArea(mFocusArea2)).isNull());
+    }
+
+    @Test
+    public void testGetAncestorFocusArea_outsideFocusArea() {
+        mRoot.post(() -> assertThat(ViewUtils.getAncestorFocusArea(mFpv)).isNull());
+    }
+
+    @Test
+    public void testGetAncestorScrollableContainer() {
+        mRoot.post(() -> {
+            View firstItem = mList5.getLayoutManager().findViewByPosition(0);
+            assertThat(ViewUtils.getAncestorScrollableContainer(firstItem)).isEqualTo(mList5);
+        });
+    }
+
+    @Test
+    public void testGetAncestorScrollableContainer_returnNull() {
+        mRoot.post(() -> assertThat(ViewUtils.getAncestorScrollableContainer(mView2)).isNull());
+    }
+
+    @Test
     public void testFindFocusedByDefaultView() {
         mRoot.post(() -> {
             View focusedByDefault = ViewUtils.findFocusedByDefaultView(mRoot);
@@ -348,6 +382,42 @@
         });
     }
 
+    @Test
+    public void testAdjustFocus_differentFocusLevels() {
+        mRoot.post(() -> {
+            assertThat(ViewUtils.adjustFocus(mFocusArea2, NO_FOCUS)).isTrue();
+            assertThat(ViewUtils.adjustFocus(mFocusArea2, REGULAR_FOCUS)).isFalse();
+
+            assertThat(ViewUtils.adjustFocus(mFocusArea5, REGULAR_FOCUS)).isTrue();
+            assertThat(ViewUtils.adjustFocus(mFocusArea5, IMPLICIT_DEFAULT_FOCUS)).isFalse();
+
+            assertThat(ViewUtils.adjustFocus(mFocusArea4, IMPLICIT_DEFAULT_FOCUS)).isTrue();
+            assertThat(ViewUtils.adjustFocus(mFocusArea4, DEFAULT_FOCUS)).isFalse();
+
+            assertThat(ViewUtils.adjustFocus(mFocusArea3, DEFAULT_FOCUS)).isTrue();
+            assertThat(ViewUtils.adjustFocus(mFocusArea3, FOCUSED_BY_DEFAULT)).isFalse();
+        });
+    }
+
+    @Test
+    public void testGetFocusLevel() {
+        mRoot.post(() -> {
+            assertThat(ViewUtils.getFocusLevel(null)).isEqualTo(NO_FOCUS);
+            assertThat(ViewUtils.getFocusLevel(mFpv)).isEqualTo(NO_FOCUS);
+            mFocusArea2.setVisibility(INVISIBLE);
+            assertThat(ViewUtils.getFocusLevel(mView2)).isEqualTo(NO_FOCUS);
+
+            assertThat(ViewUtils.getFocusLevel(mView4)).isEqualTo(REGULAR_FOCUS);
+
+            View firstItem = mList5.getLayoutManager().findViewByPosition(0);
+            assertThat(ViewUtils.getFocusLevel(firstItem)).isEqualTo(IMPLICIT_DEFAULT_FOCUS);
+
+            assertThat(ViewUtils.getFocusLevel(mDefaultFocus4)).isEqualTo(DEFAULT_FOCUS);
+
+            assertThat(ViewUtils.getFocusLevel(mFocusedByDefault3)).isEqualTo(FOCUSED_BY_DEFAULT);
+        });
+    }
+
     private static void assertRequestFocus(@Nullable View view, boolean focused) {
         boolean result = ViewUtils.requestFocus(view);
         assertThat(result).isEqualTo(focused);
diff --git a/car-ui-lib/car-ui-lib/src/main/java/com/android/car/ui/utils/ViewUtils.java b/car-ui-lib/car-ui-lib/src/main/java/com/android/car/ui/utils/ViewUtils.java
index 7c95577..853649b 100644
--- a/car-ui-lib/car-ui-lib/src/main/java/com/android/car/ui/utils/ViewUtils.java
+++ b/car-ui-lib/car-ui-lib/src/main/java/com/android/car/ui/utils/ViewUtils.java
@@ -176,8 +176,9 @@
         return false;
     }
 
+    @VisibleForTesting
     @FocusLevel
-    private static int getFocusLevel(@Nullable View view) {
+    static int getFocusLevel(@Nullable View view) {
         if (view == null || view instanceof FocusParkingView || !view.isShown()) {
             return NO_FOCUS;
         }