summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPeter Varga <pvarga@inf.u-szeged.hu>2016-11-08 10:34:31 +0100
committerPeter Varga <pvarga@inf.u-szeged.hu>2016-11-29 15:02:02 +0000
commit01c029fd90162bdbbcf9534537f26138a333c570 (patch)
tree4edc0a8e4fd557f68baf31cf16e1e1fd5a7c5771
parenta79fd91391c489ea1cd7baf717778b75e4847b92 (diff)
Fix keyboard focus of RWHVQDW after popup
Task-number: QTBUG-56652 Change-Id: I1a6655587a9104dd817332e2eb5f886c057d8f64 Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
-rw-r--r--src/webenginewidgets/render_widget_host_view_qt_delegate_widget.cpp7
-rw-r--r--tests/auto/widgets/qwebengineview/tst_qwebengineview.cpp59
-rw-r--r--tests/auto/widgets/tests.pri2
3 files changed, 67 insertions, 1 deletions
diff --git a/src/webenginewidgets/render_widget_host_view_qt_delegate_widget.cpp b/src/webenginewidgets/render_widget_host_view_qt_delegate_widget.cpp
index 8c8e9fb71..27268d26b 100644
--- a/src/webenginewidgets/render_widget_host_view_qt_delegate_widget.cpp
+++ b/src/webenginewidgets/render_widget_host_view_qt_delegate_widget.cpp
@@ -202,6 +202,13 @@ QRectF RenderWidgetHostViewQtDelegateWidget::contentsRect() const
void RenderWidgetHostViewQtDelegateWidget::setKeyboardFocus()
{
+ // If the corresponding window is inactive (for example, because of a popup),
+ // the active focus cannot be set. Sync up with the Window System to try to
+ // reactivate the window in time if the other window (possibly popup) which took
+ // the focus is already closed.
+ if (window() && !window()->isActive())
+ QGuiApplication::sync();
+
m_rootItem->forceActiveFocus();
}
diff --git a/tests/auto/widgets/qwebengineview/tst_qwebengineview.cpp b/tests/auto/widgets/qwebengineview/tst_qwebengineview.cpp
index 9966ad9ee..829f11586 100644
--- a/tests/auto/widgets/qwebengineview/tst_qwebengineview.cpp
+++ b/tests/auto/widgets/qwebengineview/tst_qwebengineview.cpp
@@ -31,8 +31,11 @@
#include <qdiriterator.h>
#include <qstackedlayout.h>
#include <qtemporarydir.h>
+#include <QCompleter>
#include <QLineEdit>
#include <QHBoxLayout>
+#include <QQuickItem>
+#include <QQuickWidget>
#define VERIFY_INPUTMETHOD_HINTS(actual, expect) \
QVERIFY(actual == expect);
@@ -83,6 +86,7 @@ private Q_SLOTS:
void inputMethodsTextFormat_data();
void inputMethodsTextFormat();
void keyboardEvents();
+ void keyboardFocusAfterPopup();
};
// This will be called before the first test function is executed.
@@ -1014,5 +1018,60 @@ void tst_QWebEngineView::keyboardEvents()
QVERIFY(loadFinishedSpy.wait());
}
+void tst_QWebEngineView::keyboardFocusAfterPopup()
+{
+ QScopedPointer<QWidget> containerWidget(new QWidget);
+
+ QLineEdit *urlLine = new QLineEdit(containerWidget.data());
+ QStringList urlList;
+ urlList << "test";
+ QCompleter *completer = new QCompleter(urlList, urlLine);
+ completer->setCompletionMode(QCompleter::PopupCompletion);
+ urlLine->setCompleter(completer);
+ urlLine->setFocus();
+
+ QWebEngineView *webView = new QWebEngineView(containerWidget.data());
+ QSignalSpy loadFinishedSpy(webView, SIGNAL(loadFinished(bool)));
+
+ connect(urlLine, &QLineEdit::editingFinished, [=] {
+ webView->setHtml("<html><body onload=\"document.getElementById('input1').focus()\">"
+ " <input type='text' id='input1' />"
+ "</body></html>");
+
+ // Check whether the RenderWidgetHostView has the keyboard focus
+ QQuickWidget *rwhv = qobject_cast<QQuickWidget *>(webView->focusProxy());
+ QVERIFY(rwhv);
+ QVERIFY(rwhv->hasFocus());
+ QVERIFY(rwhv->rootObject()->hasFocus());
+ QVERIFY(rwhv->window()->windowHandle()->isActive());
+ QVERIFY(rwhv->rootObject()->hasActiveFocus());
+ });
+
+ QVBoxLayout *layout = new QVBoxLayout;
+ layout->addWidget(urlLine);
+ layout->addWidget(webView);
+
+ containerWidget->setLayout(layout);
+ containerWidget->show();
+ QTest::qWaitForWindowExposed(containerWidget.data());
+
+ // Trigger completer's popup and select the first suggestion
+ QTest::keyClick(urlLine, Qt::Key_T);
+ qApp->processEvents();
+ QTRY_VERIFY(qApp->activePopupWidget());
+ QTest::keyClick(qApp->activePopupWidget(), Qt::Key_Down);
+ qApp->processEvents();
+ QTest::keyClick(qApp->activePopupWidget(), Qt::Key_Enter);
+ qApp->processEvents();
+
+ // After the load the focused window should forward the keyboard events to the webView
+ QVERIFY(loadFinishedSpy.wait());
+ // Wait for active focus on the input field
+ QTRY_COMPARE(evaluateJavaScriptSync(webView->page(), "document.activeElement.id").toString(), QStringLiteral("input1"));
+ QTest::keyClick(qApp->focusWindow(), Qt::Key_X);
+ qApp->processEvents();
+ QTRY_COMPARE(evaluateJavaScriptSync(webView->page(), "document.getElementById('input1').value").toString(), QStringLiteral("x"));
+}
+
QTEST_MAIN(tst_QWebEngineView)
#include "tst_qwebengineview.moc"
diff --git a/tests/auto/widgets/tests.pri b/tests/auto/widgets/tests.pri
index ca19a9496..14074cd08 100644
--- a/tests/auto/widgets/tests.pri
+++ b/tests/auto/widgets/tests.pri
@@ -12,7 +12,7 @@ INCLUDEPATH += $$PWD
RESOURCES += ../resources/tests.qrc
exists($$_PRO_FILE_PWD_/$${TARGET}.qrc): RESOURCES += $${TARGET}.qrc
-QT += testlib network webenginewidgets widgets
+QT += testlib network webenginewidgets widgets quick quickwidgets
macx: CONFIG -= app_bundle
# This define is used by some tests to look up resources in the source tree