diff options
author | Alexandru Croitor <alexandru.croitor@qt.io> | 2016-06-01 17:54:49 +0200 |
---|---|---|
committer | Alexandru Croitor <alexandru.croitor@qt.io> | 2016-08-09 06:29:08 +0000 |
commit | 6534b09073791398bad99863821740e986915bff (patch) | |
tree | 1d2476d387e75d691d112f71f74f9094b25cbd8a /tests/auto | |
parent | 521c3f6c47e3e7e284eaaca40c1820fbcb1b56e0 (diff) |
Add focusOnNavigationEnabled setting
The focusOnNavigationEnabled setting allows changing the behavior of
whether a WebEngine view (widget or quick) will automatically get
focus, whenever a navigation action happens (load, reload, previous
history entry, etc).
The default behavior before this patch was to always grab the focus.
[ChangeLog][QtWebEngine][General] Add focusOnNavigationEnabled setting
which allows controlling whether a web view will receive focus on a
navigation request. Previously the view always received the focus.
Task-number: QTBUG-52999
Change-Id: I6d30d973a41b53011131f21dcecbf6ec4d652759
Reviewed-by: Kai Koehne <kai.koehne@qt.io>
Diffstat (limited to 'tests/auto')
-rw-r--r-- | tests/auto/quick/qmltests/data/tst_focusOnNavigation.qml | 136 | ||||
-rw-r--r-- | tests/auto/quick/qmltests/qmltests.pro | 1 | ||||
-rw-r--r-- | tests/auto/widgets/qwebengineview/tst_qwebengineview.cpp | 86 |
3 files changed, 223 insertions, 0 deletions
diff --git a/tests/auto/quick/qmltests/data/tst_focusOnNavigation.qml b/tests/auto/quick/qmltests/data/tst_focusOnNavigation.qml new file mode 100644 index 000000000..ce0fa2e31 --- /dev/null +++ b/tests/auto/quick/qmltests/data/tst_focusOnNavigation.qml @@ -0,0 +1,136 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.5 +import QtTest 1.0 +import QtWebEngine 1.4 + +Item { + id: container + width: 500 + height: 300 + + Row { + id: row + spacing: 1 + + Rectangle { + anchors.top: row.top + anchors.topMargin: 7 + width: input.width + height: input.height + border.color: "black" + border.width: 1 + + TextInput { + id: input + width: 80 + height: 20 + verticalAlignment: TextInput.AlignVCenter + horizontalAlignment: TextInput.AlignHCenter + text: "Text" + } + } + + TestWebEngineView { + id: webView + width: 300 + height: 300 + } + } + + TestCase { + name: "WebEngineViewFocusOnNavigation" + when: windowShown + function init() { + } + + function test_focusOnNavigation_data() { + return [ + {tag: "focusOnNavigation true", focusOnNavigation: true, + viewReceivedFocus: true }, + {tag: "focusOnNavigation false", focusOnNavigation: false, + viewReceivedFocus: false }, + ] + } + + function triggerJavascriptFocus() { + var callbackCalled = false; + webView.runJavaScript("document.getElementById(\"input\").focus()", function(result) { + callbackCalled = true; + }); + wait(100); + verify(callbackCalled); + } + + function loadAndTriggerFocusAndCompare(data) { + verify(webView.waitForLoadSucceeded()); + triggerJavascriptFocus(); + compare(webView.activeFocus, data.viewReceivedFocus); + } + + function test_focusOnNavigation(data) { + // TextInput awlays has initial focus. + input.forceActiveFocus(); + + // Set focusOnNavigation property to current testing value. + webView.settings.focusOnNavigationEnabled = data.focusOnNavigation; + + // Load the content, invoke javascript focus on the view, and check which widget has + // focus. + webView.loadHtml("<html><head><title>Title</title></head><body>Hello" + + "<input id=\"input\" type=\"text\"></body></html>"); + loadAndTriggerFocusAndCompare(data); + + // Load a different page, and check focus. + webView.loadHtml("<html><head><title>Title</title></head><body>Hello 2" + + "<input id=\"input\" type=\"text\"></body></html>"); + loadAndTriggerFocusAndCompare(data); + + // Navigate to previous page in history, check focus. + webView.triggerWebAction(WebEngineView.Back) + loadAndTriggerFocusAndCompare(data); + + // Navigate to next page in history, check focus. + webView.triggerWebAction(WebEngineView.Forward) + loadAndTriggerFocusAndCompare(data); + + // Reload page, check focus. + webView.triggerWebAction(WebEngineView.Reload) + loadAndTriggerFocusAndCompare(data); + + // Reload page bypassing cache, check focus. + webView.triggerWebAction(WebEngineView.ReloadAndBypassCache) + loadAndTriggerFocusAndCompare(data); + + // Manually forcing focus on web view should work. + webView.forceActiveFocus() + compare(webView.activeFocus, true) + } + } +} diff --git a/tests/auto/quick/qmltests/qmltests.pro b/tests/auto/quick/qmltests/qmltests.pro index abb0a86fb..30c3aab30 100644 --- a/tests/auto/quick/qmltests/qmltests.pro +++ b/tests/auto/quick/qmltests/qmltests.pro @@ -45,6 +45,7 @@ OTHER_FILES += \ $$PWD/data/tst_favicon.qml \ $$PWD/data/tst_faviconDownload.qml \ $$PWD/data/tst_filePicker.qml \ + $$PWD/data/tst_focusOnNavigation.qml \ $$PWD/data/tst_formValidation.qml \ $$PWD/data/tst_geopermission.qml \ $$PWD/data/tst_javaScriptDialogs.qml \ diff --git a/tests/auto/widgets/qwebengineview/tst_qwebengineview.cpp b/tests/auto/widgets/qwebengineview/tst_qwebengineview.cpp index e3ca30ef5..ac142bb38 100644 --- a/tests/auto/widgets/qwebengineview/tst_qwebengineview.cpp +++ b/tests/auto/widgets/qwebengineview/tst_qwebengineview.cpp @@ -31,6 +31,8 @@ #include <qdiriterator.h> #include <qstackedlayout.h> #include <qtemporarydir.h> +#include <QLineEdit> +#include <QHBoxLayout> #define VERIFY_INPUTMETHOD_HINTS(actual, expect) \ QVERIFY(actual == expect); @@ -74,6 +76,8 @@ private Q_SLOTS: void doNotSendMouseKeyboardEventsWhenDisabled_data(); void stopSettingFocusWhenDisabled(); void stopSettingFocusWhenDisabled_data(); + void focusOnNavigation_data(); + void focusOnNavigation(); void changeLocale(); }; @@ -743,6 +747,88 @@ void tst_QWebEngineView::stopSettingFocusWhenDisabled_data() QTest::newRow("disabled view does not get focus") << false << false; } +void tst_QWebEngineView::focusOnNavigation_data() +{ + QTest::addColumn<bool>("focusOnNavigation"); + QTest::addColumn<bool>("viewReceivedFocus"); + QTest::newRow("focusOnNavigation true") << true << true; + QTest::newRow("focusOnNavigation false") << false << false; +} + +void tst_QWebEngineView::focusOnNavigation() +{ + QFETCH(bool, focusOnNavigation); + QFETCH(bool, viewReceivedFocus); + +#define triggerJavascriptFocus()\ + evaluateJavaScriptSync(webView->page(), "document.getElementById(\"input\").focus()"); +#define loadAndTriggerFocusAndCompare()\ + QTRY_COMPARE(loadSpy.count(), 1);\ + triggerJavascriptFocus();\ + QTRY_COMPARE(webView->hasFocus(), viewReceivedFocus); + + // Create a container widget, that will hold a line edit that has initial focus, and a web + // engine view. + QScopedPointer<QWidget> containerWidget(new QWidget); + QLineEdit *label = new QLineEdit; + label->setText(QString::fromLatin1("Text")); + label->setFocus(); + + // Create the web view, and set its focusOnNavigation property. + QWebEngineView *webView = new QWebEngineView; + QWebEngineSettings *settings = webView->page()->settings(); + settings->setAttribute(QWebEngineSettings::FocusOnNavigationEnabled, focusOnNavigation); + webView->resize(300, 300); + + QHBoxLayout *layout = new QHBoxLayout; + layout->addWidget(label); + layout->addWidget(webView); + + containerWidget->setLayout(layout); + containerWidget->show(); + QTest::qWaitForWindowExposed(containerWidget.data()); + + // Load the content, invoke javascript focus on the view, and check which widget has focus. + QSignalSpy loadSpy(webView, SIGNAL(loadFinished(bool))); + webView->setHtml("<html><head><title>Title</title></head><body>Hello" + "<input id=\"input\" type=\"text\"></body></html>"); + loadAndTriggerFocusAndCompare(); + + // Load a different page, and check focus. + loadSpy.clear(); + webView->setHtml("<html><head><title>Title</title></head><body>Hello 2" + "<input id=\"input\" type=\"text\"></body></html>"); + loadAndTriggerFocusAndCompare(); + + // Navigate to previous page in history, check focus. + loadSpy.clear(); + webView->triggerPageAction(QWebEnginePage::Back); + loadAndTriggerFocusAndCompare(); + + // Navigate to next page in history, check focus. + loadSpy.clear(); + webView->triggerPageAction(QWebEnginePage::Forward); + loadAndTriggerFocusAndCompare(); + + // Reload page, check focus. + loadSpy.clear(); + webView->triggerPageAction(QWebEnginePage::Reload); + loadAndTriggerFocusAndCompare(); + + // Reload page bypassing cache, check focus. + loadSpy.clear(); + webView->triggerPageAction(QWebEnginePage::ReloadAndBypassCache); + loadAndTriggerFocusAndCompare(); + + // Manually forcing focus on web view should work. + webView->setFocus(); + QTRY_COMPARE(webView->hasFocus(), true); + + // Clean up. +#undef loadAndTriggerFocusAndCompare +#undef triggerJavascriptFocus +} + void tst_QWebEngineView::changeLocale() { QUrl url("http://non.existent/"); |