From a6c6665d79e6d4097c0a1155ff5f963b9a9eab19 Mon Sep 17 00:00:00 2001 From: Alexandru Croitor Date: Tue, 31 Jan 2017 15:01:43 +0100 Subject: Fix QWebEngineView::setFocus to properly set internal QQuickItem focus The widgets object hierarchy related to focus goes like this: QWebEngineView's focus proxy is -> RenderWidgetHostViewQtDelegateWidget, which has an internal QQuickRootItem defined by QQuickWidget, and the child of the item is -> RenderWidgetHostViewQuickItem. Previously when QWebEngineView::setFocus was called, the focus was set on the RenderWidgetHostViewQtDelegateWidget and the QQuickRootItem, but not on the RenderWidgetHostViewQuickItem. This caused for e.g. an active HTML text input not receiving focus. Make sure the RenderWidgetHostViewQuickItem is marked to have focus within its root item, so that if the root item receives active focus, so will RenderWidgetHostViewQuickItem receive it. Task-number: QTBUG-58515 Change-Id: I175610e3dfebc03733aefe26c16f47096df8ff5b Reviewed-by: Allan Sandfeld Jensen --- .../widgets/qwebengineview/tst_qwebengineview.cpp | 50 ++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'tests') diff --git a/tests/auto/widgets/qwebengineview/tst_qwebengineview.cpp b/tests/auto/widgets/qwebengineview/tst_qwebengineview.cpp index 151b82b61..02e1d417e 100644 --- a/tests/auto/widgets/qwebengineview/tst_qwebengineview.cpp +++ b/tests/auto/widgets/qwebengineview/tst_qwebengineview.cpp @@ -84,6 +84,7 @@ private Q_SLOTS: void stopSettingFocusWhenDisabled_data(); void focusOnNavigation_data(); void focusOnNavigation(); + void focusInternalRenderWidgetHostViewQuickItem(); void changeLocale(); void inputMethodsTextFormat_data(); @@ -843,11 +844,60 @@ void tst_QWebEngineView::focusOnNavigation() webView->setFocus(); QTRY_COMPARE(webView->hasFocus(), true); + // Clean up. #undef loadAndTriggerFocusAndCompare #undef triggerJavascriptFocus } +void tst_QWebEngineView::focusInternalRenderWidgetHostViewQuickItem() +{ + // Create a container widget, that will hold a line edit that has initial focus, and a web + // engine view. + QScopedPointer containerWidget(new QWidget); + QLineEdit *label = new QLineEdit; + label->setText(QString::fromLatin1("Text")); + label->setFocus(); + + // Create the web view, and set its focusOnNavigation property to false, so it doesn't + // get initial focus. + QWebEngineView *webView = new QWebEngineView; + QWebEngineSettings *settings = webView->page()->settings(); + settings->setAttribute(QWebEngineSettings::FocusOnNavigationEnabled, false); + 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, and check that focus is not set. + QSignalSpy loadSpy(webView, SIGNAL(loadFinished(bool))); + webView->setHtml("TitleHello" + ""); + QTRY_COMPARE(loadSpy.count(), 1); + QTRY_COMPARE(webView->hasFocus(), false); + + // Manually trigger focus. + webView->setFocus(); + + // Check that focus is set in QWebEngineView and all internal classes. + QTRY_COMPARE(webView->hasFocus(), true); + + QQuickWidget *renderWidgetHostViewQtDelegateWidget = + qobject_cast(webView->focusProxy()); + QVERIFY(renderWidgetHostViewQtDelegateWidget); + QTRY_COMPARE(renderWidgetHostViewQtDelegateWidget->hasFocus(), true); + + QQuickItem *renderWidgetHostViewQuickItem = + renderWidgetHostViewQtDelegateWidget->rootObject(); + QVERIFY(renderWidgetHostViewQuickItem); + QTRY_COMPARE(renderWidgetHostViewQuickItem->hasFocus(), true); +} + void tst_QWebEngineView::changeLocale() { QUrl url("http://non.existent/"); -- cgit v1.2.3