Test that user-specified focus loops don't hang

Bug: 62960867
Test: FocusFinderTest#testChainVisibility
Change-Id: Id368f16e02ae02ef63a0b1fbd410f635f6f82680
diff --git a/tests/tests/view/src/android/view/cts/FocusFinderTest.java b/tests/tests/view/src/android/view/cts/FocusFinderTest.java
index a8015ab..5ec5e7f 100644
--- a/tests/tests/view/src/android/view/cts/FocusFinderTest.java
+++ b/tests/tests/view/src/android/view/cts/FocusFinderTest.java
@@ -245,7 +245,7 @@
         assertTrue(nextFocus == mBottomRight || nextFocus == mBottomLeft);
     }
 
-    @Test
+    @Test(timeout = 500)
     public void testChainVisibility() {
         mBottomRight.setNextFocusForwardId(mBottomLeft.getId());
         mBottomLeft.setNextFocusForwardId(mTopRight.getId());
@@ -256,6 +256,20 @@
         mBottomLeft.setNextFocusForwardId(View.NO_ID);
         next = mFocusFinder.findNextFocus(mLayout, mBottomRight, View.FOCUS_FORWARD);
         assertSame(mTopLeft, next);
+
+        // This shouldn't go into an infinite loop
+        mBottomRight.setNextFocusForwardId(mTopRight.getId());
+        mTopLeft.setNextFocusForwardId(mTopRight.getId());
+        mTopRight.setNextFocusForwardId(mBottomLeft.getId());
+        mBottomLeft.setNextFocusForwardId(mTopLeft.getId());
+        mActivityRule.getActivity().runOnUiThread(() -> {
+            mTopLeft.setVisibility(View.INVISIBLE);
+            mTopRight.setVisibility(View.INVISIBLE);
+            mBottomLeft.setVisibility(View.INVISIBLE);
+            mBottomRight.setVisibility(View.INVISIBLE);
+        });
+        InstrumentationRegistry.getInstrumentation().waitForIdleSync();
+        mFocusFinder.findNextFocus(mLayout, mBottomRight, View.FOCUS_FORWARD);
     }
 
     private void verifyNextCluster(View currentCluster, int direction, View expectedNextCluster) {