summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlexandru Croitor <alexandru.croitor@qt.io>2016-06-01 17:54:49 +0200
committerAlexandru Croitor <alexandru.croitor@qt.io>2016-08-09 06:29:08 +0000
commit6534b09073791398bad99863821740e986915bff (patch)
tree1d2476d387e75d691d112f71f74f9094b25cbd8a
parent521c3f6c47e3e7e284eaaca40c1820fbcb1b56e0 (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>
-rw-r--r--src/core/web_contents_adapter.cpp23
-rw-r--r--src/core/web_contents_adapter.h2
-rw-r--r--src/core/web_engine_settings.cpp1
-rw-r--r--src/core/web_engine_settings.h3
-rw-r--r--src/webengine/api/qquickwebenginesettings.cpp22
-rw-r--r--src/webengine/api/qquickwebenginesettings_p.h4
-rw-r--r--src/webengine/api/qquickwebengineview_p_p.h1
-rw-r--r--src/webengine/doc/src/webengineview.qdoc8
-rw-r--r--src/webengine/plugin/plugin.cpp1
-rw-r--r--src/webenginewidgets/api/qwebenginepage_p.h1
-rw-r--r--src/webenginewidgets/api/qwebenginesettings.cpp2
-rw-r--r--src/webenginewidgets/api/qwebenginesettings.h3
-rw-r--r--src/webenginewidgets/doc/src/qwebenginepage_lgpl.qdoc4
-rw-r--r--src/webenginewidgets/doc/src/qwebenginesettings_lgpl.qdoc5
-rw-r--r--tests/auto/quick/qmltests/data/tst_focusOnNavigation.qml136
-rw-r--r--tests/auto/quick/qmltests/qmltests.pro1
-rw-r--r--tests/auto/widgets/qwebengineview/tst_qwebengineview.cpp86
17 files changed, 292 insertions, 11 deletions
diff --git a/src/core/web_contents_adapter.cpp b/src/core/web_contents_adapter.cpp
index b0aae6ca9..a7799544b 100644
--- a/src/core/web_contents_adapter.cpp
+++ b/src/core/web_contents_adapter.cpp
@@ -470,21 +470,21 @@ void WebContentsAdapter::stop()
controller.RemoveEntryAtIndex(index);
d->webContents->Stop();
- d->webContents->Focus();
+ focusIfNecessary();
}
void WebContentsAdapter::reload()
{
Q_D(WebContentsAdapter);
d->webContents->GetController().Reload(/*checkRepost = */false);
- d->webContents->Focus();
+ focusIfNecessary();
}
void WebContentsAdapter::reloadAndBypassCache()
{
Q_D(WebContentsAdapter);
d->webContents->GetController().ReloadBypassingCache(/*checkRepost = */false);
- d->webContents->Focus();
+ focusIfNecessary();
}
void WebContentsAdapter::load(const QUrl &url)
@@ -507,7 +507,7 @@ void WebContentsAdapter::load(const QUrl &url)
params.transition_type = ui::PageTransitionFromInt(ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_ADDRESS_BAR);
params.override_user_agent = content::NavigationController::UA_OVERRIDE_TRUE;
d->webContents->GetController().LoadURLWithParams(params);
- d->webContents->Focus();
+ focusIfNecessary();
}
void WebContentsAdapter::setContent(const QByteArray &data, const QString &mimeType, const QUrl &baseUrl)
@@ -532,7 +532,7 @@ void WebContentsAdapter::setContent(const QByteArray &data, const QString &mimeT
params.transition_type = ui::PageTransitionFromInt(ui::PAGE_TRANSITION_TYPED | ui::PAGE_TRANSITION_FROM_API);
params.override_user_agent = content::NavigationController::UA_OVERRIDE_TRUE;
d->webContents->GetController().LoadURLWithParams(params);
- d->webContents->Focus();
+ focusIfNecessary();
d->webContents->Unselect();
}
@@ -646,14 +646,14 @@ void WebContentsAdapter::navigateToIndex(int offset)
{
Q_D(WebContentsAdapter);
d->webContents->GetController().GoToIndex(offset);
- d->webContents->Focus();
+ focusIfNecessary();
}
void WebContentsAdapter::navigateToOffset(int offset)
{
Q_D(WebContentsAdapter);
d->webContents->GetController().GoToOffset(offset);
- d->webContents->Focus();
+ focusIfNecessary();
}
int WebContentsAdapter::navigationEntryCount()
@@ -1246,6 +1246,15 @@ void WebContentsAdapter::replaceMisspelling(const QString &word)
#endif
}
+void WebContentsAdapter::focusIfNecessary()
+{
+ Q_D(WebContentsAdapter);
+ const WebEngineSettings *settings = d->adapterClient->webEngineSettings();
+ bool focusOnNavigation = settings->testAttribute(WebEngineSettings::FocusOnNavigationEnabled);
+ if (focusOnNavigation)
+ d->webContents->Focus();
+}
+
WebContentsAdapterClient::RenderProcessTerminationStatus
WebContentsAdapterClient::renderProcessExitStatus(int terminationStatus) {
auto status = WebContentsAdapterClient::RenderProcessTerminationStatus(-1);
diff --git a/src/core/web_contents_adapter.h b/src/core/web_contents_adapter.h
index a29778a7e..08f68e76b 100644
--- a/src/core/web_contents_adapter.h
+++ b/src/core/web_contents_adapter.h
@@ -180,6 +180,8 @@ public:
void viewSource();
bool canViewSource();
+ void focusIfNecessary();
+
private:
Q_DISABLE_COPY(WebContentsAdapter)
diff --git a/src/core/web_engine_settings.cpp b/src/core/web_engine_settings.cpp
index 6c17c3ce9..a706b860e 100644
--- a/src/core/web_engine_settings.cpp
+++ b/src/core/web_engine_settings.cpp
@@ -237,6 +237,7 @@ void WebEngineSettings::initDefaults(bool offTheRecord)
s_defaultAttributes.insert(Accelerated2dCanvasEnabled, accelerated2dCanvas);
s_defaultAttributes.insert(AutoLoadIconsForPage, true);
s_defaultAttributes.insert(TouchIconsEnabled, false);
+ s_defaultAttributes.insert(FocusOnNavigationEnabled, true);
}
if (offTheRecord)
m_attributes.insert(LocalStorageEnabled, false);
diff --git a/src/core/web_engine_settings.h b/src/core/web_engine_settings.h
index e21eee8a9..55e659b53 100644
--- a/src/core/web_engine_settings.h
+++ b/src/core/web_engine_settings.h
@@ -79,7 +79,8 @@ public:
WebGLEnabled,
Accelerated2dCanvasEnabled,
AutoLoadIconsForPage,
- TouchIconsEnabled
+ TouchIconsEnabled,
+ FocusOnNavigationEnabled
};
// Must match the values from the public API in qwebenginesettings.h.
diff --git a/src/webengine/api/qquickwebenginesettings.cpp b/src/webengine/api/qquickwebenginesettings.cpp
index 3fea83166..a69db0554 100644
--- a/src/webengine/api/qquickwebenginesettings.cpp
+++ b/src/webengine/api/qquickwebenginesettings.cpp
@@ -304,6 +304,20 @@ bool QQuickWebEngineSettings::touchIconsEnabled() const
}
/*!
+ \qmlproperty bool WebEngineSettings::focusOnNavigationEnabled
+ \since QtWebEngine 1.4
+
+ Focus is given to the view whenever a navigation operation occurs
+ (load, stop, reload, reload and bypass cache, forward, backward, set content, and so on).
+
+ Enabled by default.
+*/
+bool QQuickWebEngineSettings::focusOnNavigationEnabled() const
+{
+ return d_ptr->testAttribute(WebEngineSettings::FocusOnNavigationEnabled);
+}
+
+/*!
\qmlproperty QString WebEngineSettings::defaultTextEncoding
\since QtWebEngine 1.2
@@ -472,6 +486,14 @@ void QQuickWebEngineSettings::setDefaultTextEncoding(QString encoding)
Q_EMIT defaultTextEncodingChanged();
}
+void QQuickWebEngineSettings::setFocusOnNavigationEnabled(bool on)
+{
+ bool wasOn = d_ptr->testAttribute(WebEngineSettings::FocusOnNavigationEnabled);
+ d_ptr->setAttribute(WebEngineSettings::FocusOnNavigationEnabled, on);
+ if (wasOn != on)
+ Q_EMIT focusOnNavigationEnabledChanged();
+}
+
void QQuickWebEngineSettings::setParentSettings(QQuickWebEngineSettings *parentSettings)
{
d_ptr->setParentSettings(parentSettings->d_ptr.data());
diff --git a/src/webengine/api/qquickwebenginesettings_p.h b/src/webengine/api/qquickwebenginesettings_p.h
index 584027260..2675630f8 100644
--- a/src/webengine/api/qquickwebenginesettings_p.h
+++ b/src/webengine/api/qquickwebenginesettings_p.h
@@ -82,6 +82,7 @@ class Q_WEBENGINE_PRIVATE_EXPORT QQuickWebEngineSettings : public QObject {
Q_PROPERTY(bool accelerated2dCanvasEnabled READ accelerated2dCanvasEnabled WRITE setAccelerated2dCanvasEnabled NOTIFY accelerated2dCanvasEnabledChanged REVISION 2)
Q_PROPERTY(bool autoLoadIconsForPage READ autoLoadIconsForPage WRITE setAutoLoadIconsForPage NOTIFY autoLoadIconsForPageChanged REVISION 2)
Q_PROPERTY(bool touchIconsEnabled READ touchIconsEnabled WRITE setTouchIconsEnabled NOTIFY touchIconsEnabledChanged REVISION 2)
+ Q_PROPERTY(bool focusOnNavigationEnabled READ focusOnNavigationEnabled WRITE setFocusOnNavigationEnabled NOTIFY focusOnNavigationEnabledChanged REVISION 3)
public:
~QQuickWebEngineSettings();
@@ -105,6 +106,7 @@ public:
bool accelerated2dCanvasEnabled() const;
bool autoLoadIconsForPage() const;
bool touchIconsEnabled() const;
+ bool focusOnNavigationEnabled() const;
void setAutoLoadImages(bool on);
void setJavascriptEnabled(bool on);
@@ -125,6 +127,7 @@ public:
void setAccelerated2dCanvasEnabled(bool on);
void setAutoLoadIconsForPage(bool on);
void setTouchIconsEnabled(bool on);
+ void setFocusOnNavigationEnabled(bool on);
signals:
void autoLoadImagesChanged();
@@ -146,6 +149,7 @@ signals:
Q_REVISION(2) void accelerated2dCanvasEnabledChanged();
Q_REVISION(2) void autoLoadIconsForPageChanged();
Q_REVISION(2) void touchIconsEnabledChanged();
+ Q_REVISION(3) void focusOnNavigationEnabledChanged();
private:
explicit QQuickWebEngineSettings(QQuickWebEngineSettings *parentSettings = 0);
diff --git a/src/webengine/api/qquickwebengineview_p_p.h b/src/webengine/api/qquickwebengineview_p_p.h
index 66952ca6e..98efb822c 100644
--- a/src/webengine/api/qquickwebengineview_p_p.h
+++ b/src/webengine/api/qquickwebengineview_p_p.h
@@ -226,6 +226,7 @@ public:
bool m_fullscreenMode;
bool isLoading;
bool m_activeFocusOnPress;
+ bool m_navigationActionTriggered;
qreal devicePixelRatio;
QMap<quint64, QJSValue> m_callbacks;
QList<QSharedPointer<CertificateErrorController> > m_certificateErrorControllers;
diff --git a/src/webengine/doc/src/webengineview.qdoc b/src/webengine/doc/src/webengineview.qdoc
index 6fd9ba45b..938545d79 100644
--- a/src/webengine/doc/src/webengineview.qdoc
+++ b/src/webengine/doc/src/webengineview.qdoc
@@ -82,7 +82,11 @@
described by \l FindFlags.
The \l setActiveFocusOnPress() method can be used to create a UI element that should not get
- focus. This can be useful in a hybrid UI.
+ focus on press. This can be useful in a hybrid UI.
+
+ The \l {WebEngineSettings::focusOnNavigationEnabled} {focusOnNavigationEnabled} setting can be
+ used to stop the view from automatically receiving focus when a navigation operation occurs
+ (like loading or reloading a page or navigating through history).
The \l linkHovered() signal is emitted when a mouse pointer passes over a link and thus
corresponds to the \c{mouseover} DOM event.
@@ -481,7 +485,7 @@
a web engine view can be used to create a UI element that should not get focus. This can be
useful in a hybrid UI.
- \sa activeFocusOnPressChanged()
+ \sa activeFocusOnPressChanged, WebEngineSettings::focusOnNavigationEnabled
*/
/*!
diff --git a/src/webengine/plugin/plugin.cpp b/src/webengine/plugin/plugin.cpp
index 2f7d2c2c4..5c33086c0 100644
--- a/src/webengine/plugin/plugin.cpp
+++ b/src/webengine/plugin/plugin.cpp
@@ -100,6 +100,7 @@ public:
qmlRegisterUncreatableType<QQuickWebEngineSettings>(uri, 1, 1, "WebEngineSettings", tr("Cannot create a separate instance of WebEngineSettings"));
qmlRegisterUncreatableType<QQuickWebEngineSettings, 1>(uri, 1, 2, "WebEngineSettings", tr("Cannot create a separate instance of WebEngineSettings"));
qmlRegisterUncreatableType<QQuickWebEngineSettings, 2>(uri, 1, 3, "WebEngineSettings", tr("Cannot create a separate instance of WebEngineSettings"));
+ qmlRegisterUncreatableType<QQuickWebEngineSettings, 3>(uri, 1, 4, "WebEngineSettings", tr("Cannot create a separate instance of WebEngineSettings"));
qmlRegisterSingletonType<QQuickWebEngineSingleton>(uri, 1, 1, "WebEngine", webEngineSingletonProvider);
qmlRegisterUncreatableType<QQuickWebEngineHistory>(uri, 1, 1, "NavigationHistory",
tr("Cannot create a separate instance of NavigationHistory"));
diff --git a/src/webenginewidgets/api/qwebenginepage_p.h b/src/webenginewidgets/api/qwebenginepage_p.h
index 03883bb44..8d2a86350 100644
--- a/src/webenginewidgets/api/qwebenginepage_p.h
+++ b/src/webenginewidgets/api/qwebenginepage_p.h
@@ -174,6 +174,7 @@ public:
QWebChannel *webChannel;
unsigned int webChannelWorldId;
QUrl iconUrl;
+ bool m_navigationActionTriggered;
mutable QtWebEngineCore::CallbackDirectory m_callbacks;
mutable QAction *actions[QWebEnginePage::WebActionCount];
diff --git a/src/webenginewidgets/api/qwebenginesettings.cpp b/src/webenginewidgets/api/qwebenginesettings.cpp
index dfca16287..dbc6a0e9b 100644
--- a/src/webenginewidgets/api/qwebenginesettings.cpp
+++ b/src/webenginewidgets/api/qwebenginesettings.cpp
@@ -89,6 +89,8 @@ static WebEngineSettings::Attribute toWebEngineAttribute(QWebEngineSettings::Web
return WebEngineSettings::AutoLoadIconsForPage;
case QWebEngineSettings::TouchIconsEnabled:
return WebEngineSettings::TouchIconsEnabled;
+ case QWebEngineSettings::FocusOnNavigationEnabled:
+ return WebEngineSettings::FocusOnNavigationEnabled;
default:
return WebEngineSettings::UnsupportedInCoreSettings;
}
diff --git a/src/webenginewidgets/api/qwebenginesettings.h b/src/webenginewidgets/api/qwebenginesettings.h
index 8eda50ee2..fb7fcfee5 100644
--- a/src/webenginewidgets/api/qwebenginesettings.h
+++ b/src/webenginewidgets/api/qwebenginesettings.h
@@ -85,7 +85,8 @@ public:
WebGLEnabled,
Accelerated2dCanvasEnabled,
AutoLoadIconsForPage,
- TouchIconsEnabled
+ TouchIconsEnabled,
+ FocusOnNavigationEnabled
};
enum FontSize {
diff --git a/src/webenginewidgets/doc/src/qwebenginepage_lgpl.qdoc b/src/webenginewidgets/doc/src/qwebenginepage_lgpl.qdoc
index a0dc49d0a..560d4a2e4 100644
--- a/src/webenginewidgets/doc/src/qwebenginepage_lgpl.qdoc
+++ b/src/webenginewidgets/doc/src/qwebenginepage_lgpl.qdoc
@@ -87,6 +87,10 @@
QWebEngineScript::ScriptWorldId provides some predefined IDs for this purpose. Using the
\c runJavaScript() version without the world ID is the same as running the script in the
\c MainWorld.
+
+ The \l {QWebEngineSettings::FocusOnNavigationEnabled} {FocusOnNavigationEnabled} setting can be
+ used to stop the view associated with the page from automatically receiving focus when a
+ navigation operation occurs (like loading or reloading a page or navigating through history).
*/
/*!
diff --git a/src/webenginewidgets/doc/src/qwebenginesettings_lgpl.qdoc b/src/webenginewidgets/doc/src/qwebenginesettings_lgpl.qdoc
index 558aa1f50..5bea28f55 100644
--- a/src/webenginewidgets/doc/src/qwebenginesettings_lgpl.qdoc
+++ b/src/webenginewidgets/doc/src/qwebenginesettings_lgpl.qdoc
@@ -146,6 +146,11 @@
\value TouchIconsEnabled
Enables support for touch icons and precomposed touch icons
Disabled by default. (Added in Qt 5.7)
+ \value FocusOnNavigationEnabled
+ Gives focus to the view associated with the page, whenever a navigation operation occurs
+ (load, stop, reload, reload and bypass cache, forward, backward, set content, and so
+ on).
+ Enabled by default. (Added in Qt 5.8)
*/
/*!
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/");