From 293397cc577eb8e0805e0ef68f960dbb4574cf24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCri=20Valdmann?= Date: Wed, 22 Jul 2020 11:18:26 +0200 Subject: Fix incorrect doc for acceptNavigationRequest and add test The function is called for all navigation requests. The original bug report was most likely due to the use relative links on a page without a base url, which indeed results in no call to acceptNavigationRequest being made. But this has nothing to do with local vs remote URLs, it's rather that relative links, which are relative to nothing, are not valid links and there thus cannot be any navigation request either. Task-number: QTBUG-48435 Change-Id: I08bd0c86d67bf1dd1d7662468321777254a7db0b Reviewed-by: Allan Sandfeld Jensen --- .../doc/src/qwebenginepage_lgpl.qdoc | 9 +++---- .../widgets/qwebenginepage/tst_qwebenginepage.cpp | 29 ++++++++++++++++++++++ 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/webenginewidgets/doc/src/qwebenginepage_lgpl.qdoc b/src/webenginewidgets/doc/src/qwebenginepage_lgpl.qdoc index 9d5c41713..789d9794e 100644 --- a/src/webenginewidgets/doc/src/qwebenginepage_lgpl.qdoc +++ b/src/webenginewidgets/doc/src/qwebenginepage_lgpl.qdoc @@ -376,11 +376,6 @@ to the main frame or a child frame. If the function returns \c true, the navigation request is accepted and \c url is loaded. The default implementation accepts all navigation requests. - This function is called for absolute URLs that are prefixed with \c {http://} or \c {https://} - and for unrecognized schemes, such as \c {mailto:}, which will be handled by QDesktopServices - if accepted. To have this function called also upon receiving navigation requests to local URLs, - prefix the URLs with \c {http://}. - Navigation requests can be delegated to the Qt application instead of having the HTML handler engine process them by overloading this function. This is necessary when an HTML document is used as part of the user interface, and not to display external data, for example, when @@ -391,6 +386,10 @@ signal that returns \c false is to be expected even after delegating the request. + \note When using setHtml or setContent with relative links, make + sure to specify a base url, otherwise the links will be considered + invalid and no navigation requests will be emitted. + The \l{QWebEngineUrlRequestInterceptor} class offers further options for intercepting and manipulating requests. */ diff --git a/tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp b/tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp index 621c2dbd7..a3c45918b 100644 --- a/tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp +++ b/tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp @@ -93,6 +93,7 @@ private Q_SLOTS: void comboBoxPopupPositionAfterChildMove(); void acceptNavigationRequest(); void acceptNavigationRequestNavigationType(); + void acceptNavigationRequestRelativeToNothing(); void geolocationRequestJS_data(); void geolocationRequestJS(); void loadFinished(); @@ -602,6 +603,34 @@ void tst_QWebEnginePage::acceptNavigationRequestNavigationType() } } +// Relative url without base url. +// +// See also: QTBUG-48435 +void tst_QWebEnginePage::acceptNavigationRequestRelativeToNothing() +{ + TestPage page; + QSignalSpy loadSpy(&page, SIGNAL(loadFinished(bool))); + + page.setHtml(QString("limited time offer"), + /* baseUrl: */ QUrl()); + QTRY_COMPARE_WITH_TIMEOUT(loadSpy.count(), 1, 20000); + page.runJavaScript(QStringLiteral("document.getElementById(\"link\").click()")); + QTRY_COMPARE_WITH_TIMEOUT(loadSpy.count(), 2, 20000); + page.setHtml(QString("limited time offer"), + /* baseUrl: */ QString("qrc:/")); + QTRY_COMPARE_WITH_TIMEOUT(loadSpy.count(), 3, 20000); + page.runJavaScript(QStringLiteral("document.getElementById(\"link\").click()")); + QTRY_COMPARE_WITH_TIMEOUT(loadSpy.count(), 4, 20000); + + // The two setHtml and the second click are counted, while the + // first click is ignored due to the empty base url. + QCOMPARE(page.navigations.count(), 3); + QCOMPARE(page.navigations[0].type, QWebEnginePage::NavigationTypeTyped); + QCOMPARE(page.navigations[1].type, QWebEnginePage::NavigationTypeTyped); + QCOMPARE(page.navigations[2].type, QWebEnginePage::NavigationTypeLinkClicked); + QCOMPARE(page.navigations[2].url, QUrl(QString("qrc:/S0"))); +} + void tst_QWebEnginePage::popupFormSubmission() { TestPage page; -- cgit v1.2.3