From 1e83a2d1b61b13323163dfe8cac64dad397cb202 Mon Sep 17 00:00:00 2001 From: Joerg Bornemann Date: Wed, 25 May 2016 16:14:41 +0200 Subject: Propagate the view's screen coordinates on global position change Suppose having a QWebEngineView as one of many child widgets in a layout. Resize some child widget such that the position of the QWebEngineView changes (without changing the position of the top-level window). Chromium must be informed of the changed global position, otherwise popups will be opened at the old position. Also see commit 7f941a34, which originally introduced the coordinate propagation for top-level window moves. Task-number: QTBUG-51244 Change-Id: Ieb372e8d7554700d5e8d1e2148ab778094ea3878 Reviewed-by: Allan Sandfeld Jensen --- .../render_widget_host_view_qt_delegate_widget.cpp | 8 ++ .../render_widget_host_view_qt_delegate_widget.h | 1 + tests/auto/widgets/qwebenginepage/BLACKLIST | 3 + .../widgets/qwebenginepage/tst_qwebenginepage.cpp | 113 +++++++++++++++++++++ 4 files changed, 125 insertions(+) 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 2937c94b7..a8300aa05 100644 --- a/src/webenginewidgets/render_widget_host_view_qt_delegate_widget.cpp +++ b/src/webenginewidgets/render_widget_host_view_qt_delegate_widget.cpp @@ -266,6 +266,13 @@ QVariant RenderWidgetHostViewQtDelegateWidget::inputMethodQuery(Qt::InputMethodQ void RenderWidgetHostViewQtDelegateWidget::resizeEvent(QResizeEvent *resizeEvent) { QOpenGLWidget::resizeEvent(resizeEvent); + + const QPoint globalPos = mapToGlobal(pos()); + if (globalPos != m_lastGlobalPos) { + m_lastGlobalPos = globalPos; + m_client->windowBoundsChanged(); + } + m_client->notifyResize(); } @@ -384,6 +391,7 @@ void RenderWidgetHostViewQtDelegateWidget::paintGL() void RenderWidgetHostViewQtDelegateWidget::onWindowPosChanged() { + m_lastGlobalPos = mapToGlobal(pos()); m_client->windowBoundsChanged(); } diff --git a/src/webenginewidgets/render_widget_host_view_qt_delegate_widget.h b/src/webenginewidgets/render_widget_host_view_qt_delegate_widget.h index bccfeb0f4..ecf2d2d33 100644 --- a/src/webenginewidgets/render_widget_host_view_qt_delegate_widget.h +++ b/src/webenginewidgets/render_widget_host_view_qt_delegate_widget.h @@ -101,6 +101,7 @@ private: QScopedPointer m_sgRenderer; bool m_isPopup; QColor m_clearColor; + QPoint m_lastGlobalPos; QList m_windowConnections; }; diff --git a/tests/auto/widgets/qwebenginepage/BLACKLIST b/tests/auto/widgets/qwebenginepage/BLACKLIST index 91858f299..045783c94 100644 --- a/tests/auto/widgets/qwebenginepage/BLACKLIST +++ b/tests/auto/widgets/qwebenginepage/BLACKLIST @@ -1,3 +1,6 @@ +[comboBoxPopupPositionAfterMove] +linux + [geolocationRequestJS] * diff --git a/tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp b/tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp index fb3f8ff4e..ede50e63f 100644 --- a/tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp +++ b/tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp @@ -23,10 +23,12 @@ #include #include #include +#include #include #include #include #include +#include #include #include #include @@ -113,6 +115,8 @@ private Q_SLOTS: void initTestCase(); void cleanupTestCase(); void thirdPartyCookiePolicy(); + void comboBoxPopupPositionAfterMove(); + void comboBoxPopupPositionAfterChildMove(); void contextMenuCopy(); void contextMenuPopulatedOnce(); void acceptNavigationRequest(); @@ -238,6 +242,8 @@ private Q_SLOTS: void mouseButtonTranslation(); private: + static QPoint elementCenter(QWebEnginePage *page, const QString &id); + QWebEngineView* m_view; QWebEnginePage* m_page; QWebEngineView* m_inputFieldsTestView; @@ -3113,6 +3119,96 @@ void tst_QWebEnginePage::thirdPartyCookiePolicy() #endif } +static QWindow *findNewTopLevelWindow(const QWindowList &oldTopLevelWindows) +{ + const auto tlws = QGuiApplication::topLevelWindows(); + for (auto w : tlws) { + if (!oldTopLevelWindows.contains(w)) { + return w; + } + } + return nullptr; +} + +void tst_QWebEnginePage::comboBoxPopupPositionAfterMove() +{ + QScreen *screen = QGuiApplication::primaryScreen(); + QWebEngineView view; + view.move(screen->availableGeometry().topLeft()); + view.resize(640, 480); + view.show(); + + QSignalSpy loadSpy(&view, SIGNAL(loadFinished(bool))); + view.setHtml(QLatin1String("")); + QTRY_COMPARE(loadSpy.count(), 1); + const auto oldTlws = QGuiApplication::topLevelWindows(); + QWindow *window = view.windowHandle(); + QTest::mouseClick(window, Qt::LeftButton, Qt::KeyboardModifiers(), + elementCenter(view.page(), "foo")); + + QWindow *popup = nullptr; + QTRY_VERIFY(popup = findNewTopLevelWindow(oldTlws)); + QPoint popupPos = popup->position(); + + // Close the popup by clicking somewhere into the page. + QTest::mouseClick(window, Qt::LeftButton, Qt::KeyboardModifiers(), QPoint(1, 1)); + QTRY_VERIFY(!QGuiApplication::topLevelWindows().contains(popup)); + + // Move the top-level QWebEngineView a little and check the popup's position. + const QPoint offset(12, 13); + view.move(screen->availableGeometry().topLeft() + offset); + QTest::mouseClick(window, Qt::LeftButton, Qt::KeyboardModifiers(), + elementCenter(view.page(), "foo")); + QTRY_VERIFY(popup = findNewTopLevelWindow(oldTlws)); + QCOMPARE(popupPos + offset, popup->position()); +} + +void tst_QWebEnginePage::comboBoxPopupPositionAfterChildMove() +{ + QWidget mainWidget; + mainWidget.setLayout(new QHBoxLayout); + + QWidget spacer; + spacer.setMinimumWidth(50); + mainWidget.layout()->addWidget(&spacer); + + QWebEngineView view; + mainWidget.layout()->addWidget(&view); + + QScreen *screen = QGuiApplication::primaryScreen(); + mainWidget.move(screen->availableGeometry().topLeft()); + mainWidget.resize(640, 480); + mainWidget.show(); + + QSignalSpy loadSpy(&view, SIGNAL(loadFinished(bool))); + view.setHtml(QLatin1String("")); + QTRY_COMPARE(loadSpy.count(), 1); + const auto oldTlws = QGuiApplication::topLevelWindows(); + QWindow *window = view.window()->windowHandle(); + QTest::mouseClick(window, Qt::LeftButton, Qt::KeyboardModifiers(), + view.mapTo(view.window(), elementCenter(view.page(), "foo"))); + + QWindow *popup = nullptr; + QTRY_VERIFY(popup = findNewTopLevelWindow(oldTlws)); + QPoint popupPos = popup->position(); + + // Close the popup by clicking somewhere into the page. + QTest::mouseClick(window, Qt::LeftButton, Qt::KeyboardModifiers(), + view.mapTo(view.window(), QPoint(1, 1))); + QTRY_VERIFY(!QGuiApplication::topLevelWindows().contains(popup)); + + // Resize the "spacer" widget, and implicitly change the global position of the QWebEngineView. + spacer.setMinimumWidth(100); + QTest::mouseClick(window, Qt::LeftButton, Qt::KeyboardModifiers(), + view.mapTo(view.window(), elementCenter(view.page(), "foo"))); + QTRY_VERIFY(popup = findNewTopLevelWindow(oldTlws)); + QCOMPARE(popupPos + QPoint(50, 0), popup->position()); +} + #ifdef Q_OS_MAC void tst_QWebEnginePage::macCopyUnicodeToClipboard() { @@ -4992,5 +5088,22 @@ void tst_QWebEnginePage::mouseButtonTranslation() delete view; } +QPoint tst_QWebEnginePage::elementCenter(QWebEnginePage *page, const QString &id) +{ + QVariantList rectList = evaluateJavaScriptSync(page, + "(function(){" + "var elem = document.getElementById('" + id + "');" + "var rect = elem.getBoundingClientRect();" + "return [(rect.left + rect.right) / 2, (rect.top + rect.bottom) / 2];" + "})()").toList(); + + if (rectList.count() != 2) { + qWarning("elementCenter failed."); + return QPoint(); + } + + return QPoint(rectList.at(0).toInt(), rectList.at(1).toInt()); +} + QTEST_MAIN(tst_QWebEnginePage) #include "tst_qwebenginepage.moc" -- cgit v1.2.3