From b0474a665195abd94b5b5319a2eca04baff2b73b Mon Sep 17 00:00:00 2001
From: Peter Varga <pvarga@inf.u-szeged.hu>
Date: Fri, 7 Oct 2016 11:37:34 +0200
Subject: [PATCH] Speculative stabilization of test_focusOnNavigation quick
 auto test

Reimplement setting focus on a HTML element in JavaScript.

Change-Id: I1db53bcb526fd52df1fa23527089f8186dfd0ea3
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
---
 tests/auto/quick/qmltests/BLACKLIST           |  3 --
 .../qmltests/data/tst_focusOnNavigation.qml   | 29 ++++++++++++-------
 2 files changed, 19 insertions(+), 13 deletions(-)

diff --git a/tests/auto/quick/qmltests/BLACKLIST b/tests/auto/quick/qmltests/BLACKLIST
index f7eda413c..63d31c875 100644
--- a/tests/auto/quick/qmltests/BLACKLIST
+++ b/tests/auto/quick/qmltests/BLACKLIST
@@ -10,9 +10,6 @@ osx
 [WebViewGeopermission::test_geoPermissionRequest]
 osx
 
-[WebEngineViewFocusOnNavigation::test_focusOnNavigation]
-*
-
 [WebEngineViewLoadUrl::test_urlProperty]
 windows
 
diff --git a/tests/auto/quick/qmltests/data/tst_focusOnNavigation.qml b/tests/auto/quick/qmltests/data/tst_focusOnNavigation.qml
index ce0fa2e31..15ec320a4 100644
--- a/tests/auto/quick/qmltests/data/tst_focusOnNavigation.qml
+++ b/tests/auto/quick/qmltests/data/tst_focusOnNavigation.qml
@@ -67,8 +67,6 @@ Item {
     TestCase {
         name: "WebEngineViewFocusOnNavigation"
         when: windowShown
-        function init() {
-        }
 
         function test_focusOnNavigation_data() {
             return [
@@ -79,18 +77,29 @@ Item {
             ]
         }
 
-        function triggerJavascriptFocus() {
-            var callbackCalled = false;
-            webView.runJavaScript("document.getElementById(\"input\").focus()", function(result) {
-                    callbackCalled = true;
-                });
-            wait(100);
-            verify(callbackCalled);
+        function getActiveElementId() {
+            var activeElementId;
+            webView.runJavaScript("document.activeElement.id", function(result) {
+                activeElementId = result;
+            });
+            tryVerify(function() { return activeElementId != undefined });
+            return activeElementId;
+        }
+
+        function verifyElementHasFocus(element) {
+            tryVerify(function() { return getActiveElementId() == element; }, 5000,
+                "Element \"" + element + "\" has focus");
+
+        }
+
+        function setFocusToElement(element) {
+            webView.runJavaScript("document.getElementById('" + element + "').focus()");
+            verifyElementHasFocus(element);
         }
 
         function loadAndTriggerFocusAndCompare(data) {
             verify(webView.waitForLoadSucceeded());
-            triggerJavascriptFocus();
+            setFocusToElement("input");
             compare(webView.activeFocus, data.viewReceivedFocus);
         }
 
-- 
GitLab