Polling on hasFocus() for requestFocus() success

When request focus before View measure done to have
a valid size,it cannot get focus in fact.
That will cause some cases fail if it test the process
of key event handling which depending on view focus.
Polling on hasFocus() and try to call requestFocus()
until get focus or timeout.

Bug: 111615345

Change-Id: I3cbcdbbd1ef3391ff2536369a3351af4e2e4c5e5
Test: WebViewClientTest#testOnUnhandledkeyEvent
diff --git a/libs/deviceutillegacy/src/android/webkit/cts/WebViewOnUiThread.java b/libs/deviceutillegacy/src/android/webkit/cts/WebViewOnUiThread.java
index 7db9a76..d20e1a4 100644
--- a/libs/deviceutillegacy/src/android/webkit/cts/WebViewOnUiThread.java
+++ b/libs/deviceutillegacy/src/android/webkit/cts/WebViewOnUiThread.java
@@ -277,6 +277,16 @@
     }
 
     public void requestFocus() {
+        new PollingCheck(LOAD_TIMEOUT) {
+            @Override
+            protected boolean check() {
+                requestFocusOnUiThread();
+                return hasFocus();
+            }
+        }.run();
+    }
+
+    private void requestFocusOnUiThread() {
         runOnUiThread(new Runnable() {
             @Override
             public void run() {
@@ -285,6 +295,15 @@
         });
     }
 
+    private boolean hasFocus() {
+        return getValue(new ValueGetter<Boolean>() {
+            @Override
+            public Boolean capture() {
+                return mWebView.hasFocus();
+            }
+        });
+    }
+
     public boolean canZoomIn() {
         return getValue(new ValueGetter<Boolean>() {
             @Override