diff options
Diffstat (limited to 'tests/auto')
21 files changed, 520 insertions, 1359 deletions
diff --git a/tests/auto/core/qwebengineurlrequestinterceptor/tst_qwebengineurlrequestinterceptor.cpp b/tests/auto/core/qwebengineurlrequestinterceptor/tst_qwebengineurlrequestinterceptor.cpp index 23bf88417..7b7fec6f4 100644 --- a/tests/auto/core/qwebengineurlrequestinterceptor/tst_qwebengineurlrequestinterceptor.cpp +++ b/tests/auto/core/qwebengineurlrequestinterceptor/tst_qwebengineurlrequestinterceptor.cpp @@ -51,7 +51,9 @@ private Q_SLOTS: void cleanupTestCase(); void interceptRequest(); void ipv6HostEncoding(); + void requestedUrl_data(); void requestedUrl(); + void setUrlSameUrl_data(); void setUrlSameUrl(); void firstPartyUrl(); void firstPartyUrlNestedIframes_data(); @@ -123,10 +125,7 @@ public: // Skip import documents and sandboxed documents. // See Document::SiteForCookies() in chromium/third_party/blink/renderer/core/dom/document.cc. - // - // TODO: Change this to empty URL during the next chromium update: - // https://chromium-review.googlesource.com/c/chromium/src/+/1213082/ - return requestInfo.firstPartyUrl == QUrl("data:,"); + return requestInfo.firstPartyUrl == QUrl(""); } QList<RequestInfo> getUrlRequestForType(QWebEngineUrlRequestInfo::ResourceType type) @@ -249,14 +248,27 @@ void tst_QWebEngineUrlRequestInterceptor::ipv6HostEncoding() QCOMPARE(contentProvider.requestedUrls.at(0), QUrl::fromEncoded("http://[::1]/test.xml")); } +void tst_QWebEngineUrlRequestInterceptor::requestedUrl_data() +{ + QTest::addColumn<bool>("interceptInPage"); + + QTest::newRow("Profile intercept") << false; + QTest::newRow("Page intercept") << true; +} + void tst_QWebEngineUrlRequestInterceptor::requestedUrl() { + QFETCH(bool, interceptInPage); + QWebEngineProfile profile; profile.settings()->setAttribute(QWebEngineSettings::ErrorPageEnabled, false); TestRequestInterceptor interceptor(/* intercept */ true); - profile.setRequestInterceptor(&interceptor); + if (!interceptInPage) + profile.setRequestInterceptor(&interceptor); QWebEnginePage page(&profile); + if (interceptInPage) + page.setRequestInterceptor(&interceptor); QSignalSpy spy(&page, SIGNAL(loadFinished(bool))); page.setUrl(QUrl("qrc:///resources/__placeholder__")); @@ -273,19 +285,29 @@ void tst_QWebEngineUrlRequestInterceptor::requestedUrl() QCOMPARE(page.url(), QUrl("qrc:///resources/content.html")); page.setUrl(QUrl("http://abcdef.abcdef")); - QTRY_COMPARE_WITH_TIMEOUT(spy.count(), 3, 12000); + QTRY_COMPARE_WITH_TIMEOUT(spy.count(), 3, 15000); QCOMPARE(interceptor.requestInfos.at(3).requestUrl, QUrl("http://abcdef.abcdef/")); QCOMPARE(page.requestedUrl(), QUrl("qrc:///resources/__placeholder__")); QCOMPARE(page.url(), QUrl("qrc:///resources/content.html")); } +void tst_QWebEngineUrlRequestInterceptor::setUrlSameUrl_data() +{ + requestedUrl_data(); +} + void tst_QWebEngineUrlRequestInterceptor::setUrlSameUrl() { + QFETCH(bool, interceptInPage); + QWebEngineProfile profile; TestRequestInterceptor interceptor(/* intercept */ true); - profile.setRequestInterceptor(&interceptor); + if (!interceptInPage) + profile.setRequestInterceptor(&interceptor); QWebEnginePage page(&profile); + if (interceptInPage) + page.setRequestInterceptor(&interceptor); QSignalSpy spy(&page, SIGNAL(loadFinished(bool))); page.setUrl(QUrl("qrc:///resources/__placeholder__")); diff --git a/tests/auto/quick/dialogs/dialogs.pro b/tests/auto/quick/dialogs/dialogs.pro index e262c3814..29d509b20 100644 --- a/tests/auto/quick/dialogs/dialogs.pro +++ b/tests/auto/quick/dialogs/dialogs.pro @@ -1,5 +1,5 @@ include(../tests.pri) -QT += webengine webengine-private +QT += core-private webengine webengine-private HEADERS += \ server.h \ diff --git a/tests/auto/quick/dialogs/tst_dialogs.cpp b/tests/auto/quick/dialogs/tst_dialogs.cpp index ecc2764fd..cecea1831 100644 --- a/tests/auto/quick/dialogs/tst_dialogs.cpp +++ b/tests/auto/quick/dialogs/tst_dialogs.cpp @@ -26,7 +26,6 @@ ** ****************************************************************************/ -#include "qtwebengineglobal.h" #include "testhandler.h" #include "server.h" #include <QtWebEngine/private/qquickwebenginedialogrequests_p.h> diff --git a/tests/auto/quick/publicapi/tst_publicapi.cpp b/tests/auto/quick/publicapi/tst_publicapi.cpp index 0e48e280d..4cbadfdaa 100644 --- a/tests/auto/quick/publicapi/tst_publicapi.cpp +++ b/tests/auto/quick/publicapi/tst_publicapi.cpp @@ -40,6 +40,7 @@ #include <private/qquickwebengineview_p.h> #include <private/qquickwebengineaction_p.h> #include <private/qquickwebenginecertificateerror_p.h> +#include <private/qquickwebengineclientcertificateselection_p.h> #include <private/qquickwebenginedialogrequests_p.h> #include <private/qquickwebenginedownloaditem_p.h> #include <private/qquickwebenginehistory_p.h> @@ -60,6 +61,8 @@ static const QList<const QMetaObject *> typesToCheck = QList<const QMetaObject * << &QQuickWebEngineView::staticMetaObject << &QQuickWebEngineAction::staticMetaObject << &QQuickWebEngineCertificateError::staticMetaObject + << &QQuickWebEngineClientCertificateOption::staticMetaObject + << &QQuickWebEngineClientCertificateSelection::staticMetaObject << &QQuickWebEngineDownloadItem::staticMetaObject << &QQuickWebEngineHistory::staticMetaObject << &QQuickWebEngineHistoryListModel::staticMetaObject @@ -86,6 +89,8 @@ static QList<const char *> knownEnumNames = QList<const char *>(); static const QStringList hardcodedTypes = QStringList() << "QJSValue" << "QQmlListProperty<QQuickWebEngineScript>" + << "QQmlListProperty<QQuickWebEngineClientCertificateOption>" + << "const QQuickWebEngineClientCertificateOption*" << "QQmlWebChannel*" // Ignore the testSupport types without making a fuss. << "QQuickWebEngineTestSupport*" diff --git a/tests/auto/quick/qmltests/data/tst_download.qml b/tests/auto/quick/qmltests/data/tst_download.qml index 019ebd9dc..5eb704cce 100644 --- a/tests/auto/quick/qmltests/data/tst_download.qml +++ b/tests/auto/quick/qmltests/data/tst_download.qml @@ -28,7 +28,8 @@ import QtQuick 2.0 import QtTest 1.0 -import QtWebEngine 1.5 +import QtWebEngine 1.9 +import Qt.labs.platform 1.0 TestWebEngineView { id: webEngineView @@ -42,6 +43,12 @@ TestWebEngineView { property var downloadState: [] property var downloadInterruptReason: null + function urlToPath(url) { + var path = url.toString() + path = path.replace(/^(file:\/{2})/,"") + return path + } + SignalSpy { id: downLoadRequestedSpy target: testDownloadProfile @@ -135,5 +142,16 @@ TestWebEngineView { tryCompare(downloadState, "1", WebEngineDownloadItem.DownloadCancelled) tryCompare(webEngineView, "downloadInterruptReason", WebEngineDownloadItem.UserCanceled) } + + function test_downloadLocation() { + var tmpPath = urlToPath(StandardPaths.writableLocation(StandardPaths.TempLocation)); + var downloadPath = urlToPath(StandardPaths.writableLocation(StandardPaths.DownloadLocation)); + + testDownloadProfile.downloadPath = tmpPath; + compare(testDownloadProfile.downloadPath, tmpPath); + + testDownloadProfile.downloadPath = downloadPath; + compare(testDownloadProfile.downloadPath, downloadPath); + } } } diff --git a/tests/auto/quick/qmltests/data/tst_findText.qml b/tests/auto/quick/qmltests/data/tst_findText.qml index dfcfd586f..1ec574fae 100644 --- a/tests/auto/quick/qmltests/data/tst_findText.qml +++ b/tests/auto/quick/qmltests/data/tst_findText.qml @@ -116,7 +116,7 @@ TestWebEngineView { webEngineView.clear() webEngineView.findText("bla", findFlags, webEngineView.findTextCallback) - tryCompare(webEngineView, "matchCount", 100) + tryCompare(webEngineView, "matchCount", 100, 20000) verify(!findFailed) } @@ -172,7 +172,7 @@ TestWebEngineView { webEngineView.clear() webEngineView.findText("hello", findFlags, webEngineView.findTextCallback) - tryCompare(webEngineView, "matchCount", 0) + tryCompare(webEngineView, "matchCount", 0, 20000) verify(findFailed) runJavaScript("document.body.innerHTML = 'blahellobla'"); diff --git a/tests/auto/quick/qmltests/data/tst_profile.qml b/tests/auto/quick/qmltests/data/tst_profile.qml new file mode 100644 index 000000000..ee7fa4e99 --- /dev/null +++ b/tests/auto/quick/qmltests/data/tst_profile.qml @@ -0,0 +1,66 @@ +/**************************************************************************** +** +** Copyright (C) 2018 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.0 +import QtTest 1.0 +import QtWebEngine 1.9 + +TestWebEngineView { + id: webEngineView + width: 400 + height: 300 + + + WebEngineProfile { + id: profile1 + } + WebEngineProfile { + id: profile2 + } + property bool profile1UsedForGlobalCertificateVerification: profile1.useForGlobalCertificateVerification + + TestCase { + name: "WebEngineProfile" + + function test_useForGlobalCertificateVerification() { + verify(!profile1.useForGlobalCertificateVerification); + verify(!profile2.useForGlobalCertificateVerification); + verify(!webEngineView.profile1UsedForGlobalCertificateVerification); + + profile1.useForGlobalCertificateVerification = true; + verify(profile1.useForGlobalCertificateVerification); + verify(!profile2.useForGlobalCertificateVerification); + verify(webEngineView.profile1UsedForGlobalCertificateVerification); + + profile2.useForGlobalCertificateVerification = true; + verify(!webEngineView.profile1UsedForGlobalCertificateVerification); + verify(!profile1.useForGlobalCertificateVerification); + verify(profile2.useForGlobalCertificateVerification); + } + } +} diff --git a/tests/auto/quick/qmltests/qmltests.pro b/tests/auto/quick/qmltests/qmltests.pro index a2b05e091..ad479cd31 100644 --- a/tests/auto/quick/qmltests/qmltests.pro +++ b/tests/auto/quick/qmltests/qmltests.pro @@ -67,6 +67,7 @@ OTHER_FILES += \ $$PWD/data/tst_navigationHistory.qml \ $$PWD/data/tst_navigationRequested.qml \ $$PWD/data/tst_newViewRequest.qml \ + $$PWD/data/tst_profile.qml \ $$PWD/data/tst_properties.qml \ $$PWD/data/tst_runJavaScript.qml \ $$PWD/data/tst_scrollPosition.qml \ diff --git a/tests/auto/quick/qquickwebengineview/BLACKLIST b/tests/auto/quick/qquickwebengineview/BLACKLIST index 76cb18c1e..5bb38576a 100644 --- a/tests/auto/quick/qquickwebengineview/BLACKLIST +++ b/tests/auto/quick/qquickwebengineview/BLACKLIST @@ -9,8 +9,13 @@ windows [basicRenderingSanity] * + [javascriptClipboard:default] opensuse-leap + [javascriptClipboard:canPaste] opensuse-leap +[changeLocale] +* + diff --git a/tests/auto/quick/qquickwebengineview/qquickwebengineview.pro b/tests/auto/quick/qquickwebengineview/qquickwebengineview.pro index c253bc2a6..38c130aa3 100644 --- a/tests/auto/quick/qquickwebengineview/qquickwebengineview.pro +++ b/tests/auto/quick/qquickwebengineview/qquickwebengineview.pro @@ -1,6 +1,6 @@ include(../tests.pri) exists($${TARGET}.qrc):RESOURCES += $${TARGET}.qrc -QT_PRIVATE += webengine-private gui-private webenginecore-private +QT_PRIVATE += core_private gui-private webengine-private webenginecore-private HEADERS += ../shared/util.h diff --git a/tests/auto/quick/qquickwebengineview/tst_qquickwebengineview.cpp b/tests/auto/quick/qquickwebengineview/tst_qquickwebengineview.cpp index 007acb8b0..8947bba9d 100644 --- a/tests/auto/quick/qquickwebengineview/tst_qquickwebengineview.cpp +++ b/tests/auto/quick/qquickwebengineview/tst_qquickwebengineview.cpp @@ -31,6 +31,7 @@ #include <QScopedPointer> #include <QtCore/qelapsedtimer.h> +#include <QtCore/qregularexpression.h> #include <QtGui/qclipboard.h> #include <QtGui/qguiapplication.h> #include <QtGui/qpa/qwindowsysteminterface.h> @@ -40,6 +41,7 @@ #include <QtGui/private/qinputmethod_p.h> #include <QtWebEngine/private/qquickwebengineview_p.h> #include <QtWebEngine/private/qquickwebenginesettings_p.h> +#include <QtWebEngineCore/private/qtwebenginecore-config_p.h> #include <qpa/qplatforminputcontext.h> #include <functional> @@ -1000,8 +1002,8 @@ void tst_QQuickWebEngineView::changeLocale() QVERIFY(waitForLoadFailed(viewDE.data())); QTRY_VERIFY(!evaluateJavaScriptSync(viewDE.data(), "document.body.innerText").isNull()); - errorLines = evaluateJavaScriptSync(viewDE.data(), "document.body.innerText").toString().split(QRegExp("[\r\n]"), QString::SkipEmptyParts); - QCOMPARE(errorLines.first().toUtf8(), QByteArrayLiteral("Die Website ist nicht erreichbar")); + errorLines = evaluateJavaScriptSync(viewDE.data(), "document.body.innerText").toString().split(QRegularExpression("[\r\n]"), QString::SkipEmptyParts); + QCOMPARE(errorLines.first().toUtf8(), QByteArrayLiteral("Die Website ist nicht erreichbar Die Server-IP-Adresse von non.existent wurde nicht gefunden.")); QLocale::setDefault(QLocale("en")); QScopedPointer<QQuickWebEngineView> viewEN(newWebEngineView()); @@ -1009,7 +1011,7 @@ void tst_QQuickWebEngineView::changeLocale() QVERIFY(waitForLoadFailed(viewEN.data())); QTRY_VERIFY(!evaluateJavaScriptSync(viewEN.data(), "document.body.innerText").isNull()); - errorLines = evaluateJavaScriptSync(viewEN.data(), "document.body.innerText").toString().split(QRegExp("[\r\n]"), QString::SkipEmptyParts); + errorLines = evaluateJavaScriptSync(viewEN.data(), "document.body.innerText").toString().split(QRegularExpression("[\r\n]"), QString::SkipEmptyParts); QCOMPARE(errorLines.first().toUtf8(), QByteArrayLiteral("This site can\xE2\x80\x99t be reached")); // Reset error page @@ -1021,7 +1023,7 @@ void tst_QQuickWebEngineView::changeLocale() QVERIFY(waitForLoadFailed(viewDE.data())); QTRY_VERIFY(!evaluateJavaScriptSync(viewDE.data(), "document.body.innerText").isNull()); - errorLines = evaluateJavaScriptSync(viewDE.data(), "document.body.innerText").toString().split(QRegExp("[\r\n]"), QString::SkipEmptyParts); + errorLines = evaluateJavaScriptSync(viewDE.data(), "document.body.innerText").toString().split(QRegularExpression("[\r\n]"), QString::SkipEmptyParts); QCOMPARE(errorLines.first().toUtf8(), QByteArrayLiteral("Die Website ist nicht erreichbar")); } diff --git a/tests/auto/quick/quick.pro b/tests/auto/quick/quick.pro index 50a6a8587..46dd57f76 100644 --- a/tests/auto/quick/quick.pro +++ b/tests/auto/quick/quick.pro @@ -1,5 +1,5 @@ -include($$QTWEBENGINE_OUT_ROOT/src/core/qtwebenginecore-config.pri) # workaround for QTBUG-68093 -QT_FOR_CONFIG += webenginecore-private +include($$QTWEBENGINE_OUT_ROOT/src/webengine/qtwebengine-config.pri) # workaround for QTBUG-68093 +QT_FOR_CONFIG += webengine-private TEMPLATE = subdirs diff --git a/tests/auto/quick/tests.pri b/tests/auto/quick/tests.pri index f7104ad9c..1bf69da43 100644 --- a/tests/auto/quick/tests.pri +++ b/tests/auto/quick/tests.pri @@ -1,5 +1,5 @@ -include($$QTWEBENGINE_OUT_ROOT/src/core/qtwebenginecore-config.pri) # workaround for QTBUG-68093 -QT_FOR_CONFIG += webenginecore-private +include($$QTWEBENGINE_OUT_ROOT/src/webengine/qtwebengine-config.pri) # workaround for QTBUG-68093 +QT_FOR_CONFIG += webengine-private TEMPLATE = app diff --git a/tests/auto/widgets/origins/tst_origins.cpp b/tests/auto/widgets/origins/tst_origins.cpp index 4e415af90..61c7232b0 100644 --- a/tests/auto/widgets/origins/tst_origins.cpp +++ b/tests/auto/widgets/origins/tst_origins.cpp @@ -301,7 +301,7 @@ void tst_Origins::jsUrlOrigin() QCOMPARE(eval(QSL("new URL(\"qrc:/crysis.css\").origin")), QVariant(QSL("qrc://"))); QCOMPARE(eval(QSL("new URL(\"qrc://foo.com/crysis.css\").origin")), QVariant(QSL("qrc://"))); - // Same with unregistered schemes. + // Unregistered schemes behaves like opaque origins. QCOMPARE(eval(QSL("new URL(\"tst:/banana\").origin")), QVariant(QSL("tst://"))); QCOMPARE(eval(QSL("new URL(\"tst://foo.com/banana\").origin")), QVariant(QSL("tst://"))); @@ -564,8 +564,6 @@ private: // Try opening a WebSocket from pages loaded over various URL schemes. void tst_Origins::webSocket() { - const int kAbnormalClosure = 1006; - EchoServer echoServer; QWebChannel channel; channel.registerObject(QSL("echoServer"), &echoServer); @@ -578,9 +576,9 @@ void tst_Origins::webSocket() QVERIFY(load(QSL("qrc:/resources/websocket.html"))); QTRY_COMPARE(eval(QSL("result")), QVariant(QSL("ok"))); - // Only registered schemes can open WebSockets. + // Unregistered schemes can also open WebSockets (since Chromium 71) QVERIFY(load(QSL("tst:/resources/websocket.html"))); - QTRY_COMPARE(eval(QSL("result")), QVariant(kAbnormalClosure)); + QTRY_COMPARE(eval(QSL("result")), QVariant(QSL("ok"))); // Even an insecure registered scheme can open WebSockets. QVERIFY(load(QSL("PathSyntax:/resources/websocket.html"))); @@ -599,11 +597,10 @@ void tst_Origins::dedicatedWorker() QTRY_VERIFY(eval(QSL("done")).toBool()); QCOMPARE(eval(QSL("result")), QVariant(42)); - // Unregistered schemes cannot create Workers. + // Unregistered schemes can also create Workers (since Chromium 71) QVERIFY(load(QSL("tst:/resources/dedicatedWorker.html"))); QTRY_VERIFY(eval(QSL("done")).toBool()); - QVERIFY(eval(QSL("error")).toString() - .contains(QSL("Access to dedicated workers is denied to origin 'tst://'"))); + QCOMPARE(eval(QSL("result")), QVariant(42)); // Even an insecure registered scheme can create Workers. QVERIFY(load(QSL("PathSyntax:/resources/dedicatedWorker.html"))); @@ -727,15 +724,9 @@ void tst_Origins::createObjectURL() QVERIFY(load(QSL("qrc:/resources/createObjectURL.html"))); QVERIFY(eval(QSL("result")).toString().startsWith(QSL("blob:qrc:"))); - // Illegal for unregistered schemes (renderer gets terminated). - qRegisterMetaType<QWebEnginePage::RenderProcessTerminationStatus>("RenderProcessTerminationStatus"); - QSignalSpy loadFinishedSpy(m_page, &QWebEnginePage::loadFinished); - QSignalSpy renderProcessTerminatedSpy(m_page, &QWebEnginePage::renderProcessTerminated); - m_page->load(QSL("tst:/resources/createObjectURL.html")); - QVERIFY(!renderProcessTerminatedSpy.empty() || renderProcessTerminatedSpy.wait(20000)); - QVERIFY(renderProcessTerminatedSpy.front().value(0).value<QWebEnginePage::RenderProcessTerminationStatus>() - != QWebEnginePage::NormalTerminationStatus); - QVERIFY(loadFinishedSpy.empty()); + // Also legal for unregistered schemes (since Chromium 71) + QVERIFY(load(QSL("tst:/resources/createObjectURL.html"))); + QVERIFY(eval(QSL("result")).toString().startsWith(QSL("blob:tst:"))); } QTEST_MAIN(tst_Origins) diff --git a/tests/auto/widgets/qwebenginedownloaditem/tst_qwebenginedownloaditem.cpp b/tests/auto/widgets/qwebenginedownloaditem/tst_qwebenginedownloaditem.cpp index 2af818928..b50036aa8 100644 --- a/tests/auto/widgets/qwebenginedownloaditem/tst_qwebenginedownloaditem.cpp +++ b/tests/auto/widgets/qwebenginedownloaditem/tst_qwebenginedownloaditem.cpp @@ -31,6 +31,7 @@ #include <QStandardPaths> #include <QTemporaryDir> #include <QTest> +#include <QRegularExpression> #include <QWebEngineDownloadItem> #include <QWebEnginePage> #include <QWebEngineProfile> @@ -69,6 +70,12 @@ private Q_SLOTS: void downloadFileNot1(); void downloadFileNot2(); void downloadDeleted(); + void downloadUniqueFilename_data(); + void downloadUniqueFilename(); + void downloadUniqueFileNameWithTimeStamp(); + void downloadToDefaultLocation(); + void downloadToNonExistentDir(); + void downloadToReadOnlyDir(); private: void saveLink(QPoint linkPos); @@ -134,6 +141,8 @@ void tst_QWebEngineDownloadItem::cleanup() QTRY_COMPARE(m_requestedDownloads.count(), 0); QCOMPARE(m_finishedDownloads.count(), 0); QVERIFY(m_server->stop()); + // Set download path to default. + m_profile->setDownloadPath(""); } void tst_QWebEngineDownloadItem::cleanupTestCase() @@ -611,6 +620,7 @@ void tst_QWebEngineDownloadItem::downloadPage_data() void tst_QWebEngineDownloadItem::downloadPage() { + QSKIP("Unstable since Chromium 71"); QFETCH(QWebEngineDownloadItem::SavePageFormat, savePageFormat); // Set up HTTP server @@ -812,5 +822,264 @@ void tst_QWebEngineDownloadItem::downloadDeleted() QTRY_COMPARE(finishedCount, 1); } +void tst_QWebEngineDownloadItem::downloadUniqueFilename_data() +{ + QTest::addColumn<QString>("baseName"); + QTest::addColumn<QString>("extension"); + + QTest::newRow("txt") << QString("test(1.test)") << QString("txt"); + QTest::newRow("tar.gz") << QString("test(1.test)") << QString("tar.gz"); +} + +void tst_QWebEngineDownloadItem::downloadUniqueFilename() +{ + QFETCH(QString, baseName); + QFETCH(QString, extension); + QString fileName = QString("%1.%2").arg(baseName).arg(extension); + QString downloadedFilePath; + bool downloadFinished = false; + + QTemporaryDir tmpDir; + QVERIFY(tmpDir.isValid()); + m_profile->setDownloadPath(tmpDir.path()); + + // Set up HTTP server + ScopedConnection sc1 = connect(m_server, &HttpServer::newRequest, [&](HttpReqRep *rr) { + if (rr->requestMethod() == "GET" && rr->requestPath() == ("/" + fileName)) { + rr->setResponseHeader(QByteArrayLiteral("content-type"), QByteArrayLiteral("application/octet-stream")); + rr->setResponseHeader(QByteArrayLiteral("content-disposition"), QByteArrayLiteral("attachment")); + rr->setResponseBody(QByteArrayLiteral("a")); + rr->sendResponse(); + } else { + rr->setResponseStatus(404); + rr->sendResponse(); + } + }); + + // Set up profile and download handler + ScopedConnection sc2 = connect(m_profile, &QWebEngineProfile::downloadRequested, [&](QWebEngineDownloadItem *item) { + item->accept(); + connect(item, &QWebEngineDownloadItem::finished, [&, item]() { + QCOMPARE(item->state(), QWebEngineDownloadItem::DownloadCompleted); + QCOMPARE(item->isFinished(), true); + QCOMPARE(item->totalBytes(), item->receivedBytes()); + QVERIFY(item->receivedBytes() > 0); + QCOMPARE(item->interruptReason(), QWebEngineDownloadItem::NoReason); + QCOMPARE(item->type(), QWebEngineDownloadItem::Attachment); + QCOMPARE(item->isSavePageDownload(), false); + downloadedFilePath = item->path(); + downloadFinished = true; + }); + }); + + m_page->setUrl(m_server->url("/" + fileName)); + QTRY_VERIFY(downloadFinished); + QVERIFY(QFile(downloadedFilePath).exists()); + QCOMPARE(downloadedFilePath, m_profile->downloadPath() + "/" + baseName + "." + extension); + + for (int i = 1; i <= 2; ++i) { + downloadFinished = false; + m_page->setUrl(m_server->url("/" + fileName)); + QTRY_VERIFY(downloadFinished); + QVERIFY(QFile(downloadedFilePath).exists()); + QCOMPARE(downloadedFilePath, m_profile->downloadPath() + "/" + baseName + " (" + QString::number(i) + ")." + extension); + } +} + +void tst_QWebEngineDownloadItem::downloadUniqueFileNameWithTimeStamp() +{ + // Set up HTTP server + QString baseName("test(1.test)"); + QString extension("txt"); + QString fileName = QString("%1.%2").arg(baseName).arg(extension); + QString downloadedFilePath; + bool downloadFinished = false; + + QTemporaryDir tmpDir; + QVERIFY(tmpDir.isValid()); + m_profile->setDownloadPath(tmpDir.path()); + + ScopedConnection sc1 = connect(m_server, &HttpServer::newRequest, [&](HttpReqRep *rr) { + if (rr->requestMethod() == "GET" && rr->requestPath() == ("/" + fileName)) { + rr->setResponseHeader(QByteArrayLiteral("content-type"), QByteArrayLiteral("application/octet-stream")); + rr->setResponseHeader(QByteArrayLiteral("content-disposition"), QByteArrayLiteral("attachment")); + rr->setResponseBody(QByteArrayLiteral("a")); + rr->sendResponse(); + } else { + rr->setResponseStatus(404); + rr->sendResponse(); + } + }); + + // Set up profile and download handler + ScopedConnection sc2 = connect(m_profile, &QWebEngineProfile::downloadRequested, [&](QWebEngineDownloadItem *item) { + item->accept(); + connect(item, &QWebEngineDownloadItem::finished, [&, item]() { + QCOMPARE(item->state(), QWebEngineDownloadItem::DownloadCompleted); + QCOMPARE(item->isFinished(), true); + QCOMPARE(item->totalBytes(), item->receivedBytes()); + QVERIFY(item->receivedBytes() > 0); + QCOMPARE(item->interruptReason(), QWebEngineDownloadItem::NoReason); + QCOMPARE(item->page(), m_page); + downloadFinished = true; + downloadedFilePath = item->path(); + }); + }); + + // Create the first empty file without uniquifier. + { + QFile file(m_profile->downloadPath() + "/" + fileName); + file.open(QIODevice::ReadWrite); + } + + // Create 99 empty files with uniquifier. + for (int i = 1; i < 100; i++) { + QFile file(m_profile->downloadPath() + "/" + baseName + " (" + QString::number(i) + ")." + extension); + file.open(QIODevice::ReadWrite); + } + + // Create 100th (kMaxUniqueFiles) empty file with uniquifier. + m_page->setUrl(m_server->url("/" + fileName)); + QTRY_VERIFY(downloadFinished); + QVERIFY(QFile(downloadedFilePath).exists()); + QCOMPARE(downloadedFilePath, m_profile->downloadPath() + "/" + baseName + " (100)." + extension); + + // Check if the downloaded files are suffixed with timestamp after the 100th download. + for (int i = 101; i < 103; i++) { + downloadFinished = false; + m_page->setUrl(m_server->url("/" + fileName)); + QTRY_VERIFY(downloadFinished); + QVERIFY(QFile(downloadedFilePath).exists()); + QRegularExpression fileNameCheck("^.*" + QRegularExpression::escape(baseName) + " - (.*)[.]" + QRegularExpression::escape(extension) + "$"); + QRegularExpressionMatch match = fileNameCheck.match(downloadedFilePath); + QVERIFY(match.hasMatch()); + // ISO 8601 Date and time in UTC + QRegExp timeStamp("^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[01]|0[1-9]|[12][0-9])T(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])([.][0-9]+)?(Z|[+-](?:2[0-3]|[01][0-9]):[0-5][0-9])?$"); + QVERIFY(timeStamp.exactMatch(match.captured(1))); + } +} + +void tst_QWebEngineDownloadItem::downloadToDefaultLocation() +{ + QTemporaryDir tmpDir; + QVERIFY(tmpDir.isValid()); + + QCOMPARE(m_profile->downloadPath(), QStandardPaths::writableLocation(QStandardPaths::DownloadLocation)); + + m_profile->setDownloadPath(""); + QCOMPARE(m_profile->downloadPath(), QStandardPaths::writableLocation(QStandardPaths::DownloadLocation)); + + m_profile->setDownloadPath(tmpDir.path()); + QCOMPARE(m_profile->downloadPath(), tmpDir.path()); + + m_profile->setDownloadPath(QStandardPaths::writableLocation(QStandardPaths::DownloadLocation)); + QCOMPARE(m_profile->downloadPath(), QStandardPaths::writableLocation(QStandardPaths::DownloadLocation)); +} + +void tst_QWebEngineDownloadItem::downloadToNonExistentDir() +{ + QString baseName("test(1.test)"); + QString extension("txt"); + QString fileName = QString("%1.%2").arg(baseName).arg(extension); + QString downloadedFilePath; + bool downloadFinished = false; + + QTemporaryDir tmpDir; + QVERIFY(tmpDir.isValid()); + m_profile->setDownloadPath(tmpDir.path()); + + // Set up HTTP server + ScopedConnection sc1 = connect(m_server, &HttpServer::newRequest, [&](HttpReqRep *rr) { + if (rr->requestMethod() == "GET" && rr->requestPath() == ("/" + fileName)) { + rr->setResponseHeader(QByteArrayLiteral("content-type"), QByteArrayLiteral("application/octet-stream")); + rr->setResponseHeader(QByteArrayLiteral("content-disposition"), QByteArrayLiteral("attachment")); + rr->setResponseBody(QByteArrayLiteral("a")); + rr->sendResponse(); + } else { + rr->setResponseStatus(404); + rr->sendResponse(); + } + }); + + // Set up profile and download handler + ScopedConnection sc2 = connect(m_profile, &QWebEngineProfile::downloadRequested, [&](QWebEngineDownloadItem *item) { + item->accept(); + connect(item, &QWebEngineDownloadItem::finished, [&, item]() { + QCOMPARE(item->state(), QWebEngineDownloadItem::DownloadCompleted); + QCOMPARE(item->isFinished(), true); + QCOMPARE(item->totalBytes(), item->receivedBytes()); + QVERIFY(item->receivedBytes() > 0); + QCOMPARE(item->interruptReason(), QWebEngineDownloadItem::NoReason); + QCOMPARE(item->page(), m_page); + downloadFinished = true; + downloadedFilePath = item->path(); + }); + }); + + // Set a non-existent directory for the default download location. + QString nonExistentDownloadPath(m_profile->downloadPath() + "/non_existent_dir"); + m_profile->setDownloadPath(nonExistentDownloadPath); + QCOMPARE(m_profile->downloadPath(), nonExistentDownloadPath); + m_page->setUrl(m_server->url("/" + fileName)); + QTRY_VERIFY(downloadFinished); + QVERIFY(QFile(downloadedFilePath).exists()); + QCOMPARE(downloadedFilePath, nonExistentDownloadPath + "/" + fileName); +} + +void tst_QWebEngineDownloadItem::downloadToReadOnlyDir() +{ + QString baseName("test(1.test)"); + QString extension("txt"); + QString fileName = QString("%1.%2").arg(baseName).arg(extension); + QString downloadedFilePath; + bool downloadAccepted = false; + bool downloadFinished = false; + + QTemporaryDir tmpDir; + QVERIFY(tmpDir.isValid()); + m_profile->setDownloadPath(tmpDir.path()); + + // Set up HTTP server + ScopedConnection sc1 = connect(m_server, &HttpServer::newRequest, [&](HttpReqRep *rr) { + if (rr->requestMethod() == "GET" && rr->requestPath() == ("/" + fileName)) { + rr->setResponseHeader(QByteArrayLiteral("content-type"), QByteArrayLiteral("application/octet-stream")); + rr->setResponseHeader(QByteArrayLiteral("content-disposition"), QByteArrayLiteral("attachment")); + rr->setResponseBody(QByteArrayLiteral("a")); + rr->sendResponse(); + } else { + rr->setResponseStatus(404); + rr->sendResponse(); + } + }); + + QPointer<QWebEngineDownloadItem> downloadItem; + ScopedConnection sc2 = connect(m_profile, &QWebEngineProfile::downloadRequested, [&](QWebEngineDownloadItem *item) { + downloadItem = item; + item->accept(); + connect(item, &QWebEngineDownloadItem::finished, [&, item]() { + downloadFinished = true; + }); + downloadAccepted = true; + }); + + // Change permission for directory. + QFile(m_profile->downloadPath()).setPermissions(QFileDevice::ReadOwner); + QVERIFY(QFile(m_profile->downloadPath()).exists()); + + m_page->setUrl(m_server->url("/" + fileName)); + QTRY_VERIFY(downloadAccepted); + + QVERIFY(downloadItem); + QTRY_COMPARE(downloadItem->state(), QWebEngineDownloadItem::DownloadInterrupted); + QCOMPARE(downloadItem->isFinished(), false); + QCOMPARE(downloadItem->interruptReason(), QWebEngineDownloadItem::FileAccessDenied); + QVERIFY(!QFile(downloadedFilePath).exists()); + + // Clear m_requestedDownloads explicitly because download is accepted but never finished. + m_requestedDownloads.clear(); + QVERIFY(!downloadFinished); + QFile(m_profile->downloadPath()).setPermissions(QFileDevice::WriteOwner); +} + QTEST_MAIN(tst_QWebEngineDownloadItem) #include "tst_qwebenginedownloaditem.moc" diff --git a/tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp b/tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp index fdff57254..0b519fe8a 100644 --- a/tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp +++ b/tests/auto/widgets/qwebenginepage/tst_qwebenginepage.cpp @@ -34,7 +34,6 @@ #include <QOpenGLWidget> #include <QPaintEngine> #include <QPushButton> -#include <QRegExp> #include <QScreen> #include <QStateMachine> #include <QtGui/QClipboard> @@ -90,11 +89,8 @@ public Q_SLOTS: private Q_SLOTS: void initTestCase(); void cleanupTestCase(); - void thirdPartyCookiePolicy(); void comboBoxPopupPositionAfterMove(); void comboBoxPopupPositionAfterChildMove(); - void contextMenuCopy(); - void contextMenuPopulatedOnce(); void acceptNavigationRequest(); void acceptNavigationRequestNavigationType(); void geolocationRequestJS_data(); @@ -103,26 +99,13 @@ private Q_SLOTS: void actionStates(); void pasteImage(); void popupFormSubmission(); - void userStyleSheet(); - void userStyleSheetFromLocalFileUrl(); - void userStyleSheetFromQrcUrl(); - void modified(); - void contextMenuCrash(); - void updatePositionDependentActionsCrash(); void callbackSpyDeleted(); void multipleProfilesAndLocalStorage(); - void cursorMovements(); void textSelection(); - void textEditing(); void backActionUpdate(); - void testOptionalJSObjects(); - void testLocalStorageVisibility(); - void testEnablePersistentStorage(); + void localStorageVisibility(); void consoleOutput(); - void errorPageExtension(); - void errorPageExtensionLoadFinished(); void userAgentNewlineStripping(); - void undoActionHaveCustomText(); void renderWidgetHostViewNotShowTopLevel(); void getUserMediaRequest_data(); void getUserMediaRequest(); @@ -134,30 +117,19 @@ private Q_SLOTS: void crashTests_LazyInitializationOfMainFrame(); - void screenshot_data(); - void screenshot(); - #if defined(ENABLE_WEBGL) && ENABLE_WEBGL void acceleratedWebGLScreenshotWithoutView(); void unacceleratedWebGLScreenshotWithoutView(); #endif void testJSPrompt(); - void testStopScheduledPageRefresh(); void findText(); void findTextResult(); void findTextSuccessiveShouldCallAllCallbacks(); - void supportedContentType(); - // [Qt] tst_QWebEnginePage::infiniteLoopJS() timeouts with DFG JIT - // https://bugs.webkit.org/show_bug.cgi?id=79040 - // void infiniteLoopJS(); void deleteQWebEngineViewTwice(); - void renderOnRepaintRequestedShouldNotRecurse(); void loadSignalsOrder_data(); void loadSignalsOrder(); void openWindowDefaultSize(); - void cssMediaTypeGlobalSetting(); - void cssMediaTypePageSetting(); #ifdef Q_OS_MAC void macCopyUnicodeToClipboard(); @@ -170,7 +142,6 @@ private Q_SLOTS: // Tests from tst_QWebEngineFrame - void horizontalScrollAfterBack(); void symmetricUrl(); void progressSignal(); void urlChange(); @@ -182,8 +153,6 @@ private Q_SLOTS: void setHtmlWithStylesheetResource(); void setHtmlWithBaseURL(); void setHtmlWithJSAlert(); - void inputFieldFocus(); - void hitTestContent(); void baseUrl_data(); void baseUrl(); void scrollPosition(); @@ -191,7 +160,6 @@ private Q_SLOTS: void evaluateWillCauseRepaint(); void setContent_data(); void setContent(); - void setCacheLoadControlAttribute(); void setUrlWithPendingLoads(); void setUrlToEmpty(); void setUrlToInvalid(); @@ -368,18 +336,6 @@ private: bool m_allowGeolocation; }; -// [Qt] tst_QWebEnginePage::infiniteLoopJS() timeouts with DFG JIT -// https://bugs.webkit.org/show_bug.cgi?id=79040 -/* -void tst_QWebEnginePage::infiniteLoopJS() -{ - JSTestPage newPage(m_view); - m_view->setPage(&newPage); - m_view->setHtml(QString("<html><body>test</body></html>"), QUrl()); - m_view->page()->evaluateJavaScript("var run = true; var a = 1; while (run) { a++; }"); -} -*/ - void tst_QWebEnginePage::geolocationRequestJS_data() { QTest::addColumn<bool>("allowed"); @@ -653,170 +609,6 @@ protected: } }; -void tst_QWebEnginePage::userStyleSheet() -{ -#if !defined(QWEBENGINEPAGE_SETNETWORKACCESSMANAGER) - QSKIP("QWEBENGINEPAGE_SETNETWORKACCESSMANAGER"); -#else - TestNetworkManager* networkManager = new TestNetworkManager(m_page); - m_page->setNetworkAccessManager(networkManager); - - m_page->settings()->setUserStyleSheetUrl(QUrl("data:text/css;charset=utf-8;base64," - + QByteArray("p { background-image: url('http://does.not/exist.png');}").toBase64())); - m_view->setHtml("<p>hello world</p>"); - QSignalSpy spyFinished(m_view, &QWebEngineView::loadFinished); - QVERIFY(spyFinished.wait()); - - QVERIFY(networkManager->requestedUrls.count() >= 1); - QCOMPARE(networkManager->requestedUrls.at(0), QUrl("http://does.not/exist.png")); -#endif -} - -void tst_QWebEnginePage::userStyleSheetFromLocalFileUrl() -{ -#if !defined(QWEBENGINEPAGE_SETNETWORKACCESSMANAGER) - QSKIP("QWEBENGINEPAGE_SETNETWORKACCESSMANAGER"); -#else - TestNetworkManager* networkManager = new TestNetworkManager(m_page); - m_page->setNetworkAccessManager(networkManager); - - QUrl styleSheetUrl = QUrl::fromLocalFile(TESTS_SOURCE_DIR + QLatin1String("qwebenginepage/resources/user.css")); - m_page->settings()->setUserStyleSheetUrl(styleSheetUrl); - m_view->setHtml("<p>hello world</p>"); - QSignalSpy spyFinished(m_view, &QWebEngineView::loadFinished); - QVERIFY(spyFinished.wait()); - - QVERIFY(networkManager->requestedUrls.count() >= 1); - QCOMPARE(networkManager->requestedUrls.at(0), QUrl("http://does.not/exist.png")); -#endif -} - -void tst_QWebEnginePage::userStyleSheetFromQrcUrl() -{ -#if !defined(QWEBENGINEPAGE_SETNETWORKACCESSMANAGER) - QSKIP("QWEBENGINEPAGE_SETNETWORKACCESSMANAGER"); -#else - TestNetworkManager* networkManager = new TestNetworkManager(m_page); - m_page->setNetworkAccessManager(networkManager); - - m_page->settings()->setUserStyleSheetUrl(QUrl("qrc:///resources/user.css")); - m_view->setHtml("<p>hello world</p>"); - QSignalSpy spyFinished(m_view, &QWebEngineView::loadFinished); - QVERIFY(spyFinished.wait()); - - QVERIFY(networkManager->requestedUrls.count() >= 1); - QCOMPARE(networkManager->requestedUrls.at(0), QUrl("http://does.not/exist.png")); -#endif -} - -void tst_QWebEnginePage::modified() -{ -#if !defined(QWEBENGINEPAGE_ISMODIFIED) - QSKIP("QWEBENGINEPAGE_ISMODIFIED"); -#else - m_page->setUrl(QUrl("data:text/html,<body>blub")); - QSignalSpy spyFinished(m_view, &QWebEngineView::loadFinished); - QVERIFY(spyFinished.wait()); - - m_page->setUrl(QUrl("data:text/html,<body id=foo contenteditable>blah")); - QSignalSpy spyFinished(m_view, &QWebEngineView::loadFinished); - QVERIFY(spyFinished.wait()); - - QVERIFY(!m_page->isModified()); - - m_page->runJavaScript("document.getElementById('foo').focus()"); - evaluateJavaScriptSync(m_page, "document.execCommand('InsertText', true, 'Test');"); - - QVERIFY(m_page->isModified()); - - evaluateJavaScriptSync(m_page, "document.execCommand('Undo', true);"); - - QVERIFY(!m_page->isModified()); - - evaluateJavaScriptSync(m_page, "document.execCommand('Redo', true);"); - - QVERIFY(m_page->isModified()); - - QVERIFY(m_page->history()->canGoBack()); - QVERIFY(!m_page->history()->canGoForward()); - QCOMPARE(m_page->history()->count(), 2); - QVERIFY(m_page->history()->backItem().isValid()); - QVERIFY(!m_page->history()->forwardItem().isValid()); - - m_page->history()->back(); - QSignalSpy spyFinished(m_view, &QWebEngineView::loadFinished); - QVERIFY(spyFinished.wait()); - - QVERIFY(!m_page->history()->canGoBack()); - QVERIFY(m_page->history()->canGoForward()); - - QVERIFY(!m_page->isModified()); - - QCOMPARE(m_page->history()->currentItemIndex(), 0); - - m_page->history()->setMaximumItemCount(3); - QCOMPARE(m_page->history()->maximumItemCount(), 3); - - QVariant variant("string test"); - m_page->history()->currentItem().setUserData(variant); - QVERIFY(m_page->history()->currentItem().userData().toString() == "string test"); - - m_page->setUrl(QUrl("data:text/html,<body>This is second page")); - m_page->setUrl(QUrl("data:text/html,<body>This is third page")); - QCOMPARE(m_page->history()->count(), 2); - m_page->setUrl(QUrl("data:text/html,<body>This is fourth page")); - QCOMPARE(m_page->history()->count(), 2); - m_page->setUrl(QUrl("data:text/html,<body>This is fifth page")); - QSignalSpy spy(m_page, &QWebEnginePage::saveFrameStateRequested); - QVERIFY(spy.wait()); -#endif -} - -// https://bugs.webkit.org/show_bug.cgi?id=51331 -void tst_QWebEnginePage::updatePositionDependentActionsCrash() -{ -#if !defined(QWEBENGINEPAGE_UPDATEPOSITIONDEPENDENTACTIONS) - QSKIP("QWEBENGINEPAGE_UPDATEPOSITIONDEPENDENTACTIONS"); -#else - QWebEngineView view; - view.setHtml("<p>test"); - QPoint pos(0, 0); - view.page()->updatePositionDependentActions(pos); - QMenu* contextMenu = 0; - const QList<QObject *> children = view.children(); - for (QObject *child : children) { - contextMenu = qobject_cast<QMenu*>(child); - if (contextMenu) - break; - } - QVERIFY(!contextMenu); -#endif -} - -// https://bugs.webkit.org/show_bug.cgi?id=20357 -void tst_QWebEnginePage::contextMenuCrash() -{ -#if !defined(QWEBENGINEPAGE_SWALLOWCONTEXTMENUEVENT) - QSKIP("QWEBENGINEPAGE_SWALLOWCONTEXTMENUEVENT"); -#else - QWebEngineView view; - view.setHtml("<p>test"); - QPoint pos(0, 0); - QContextMenuEvent event(QContextMenuEvent::Mouse, pos); - view.page()->swallowContextMenuEvent(&event); - view.page()->updatePositionDependentActions(pos); - QMenu* contextMenu = 0; - const QList<QObject *> children = view.children(); - for (QObject *child : children) { - contextMenu = qobject_cast<QMenu*>(child); - if (contextMenu) - break; - } - QVERIFY(contextMenu); - delete contextMenu; -#endif -} - void tst_QWebEnginePage::multipleProfilesAndLocalStorage() { QDir dir(tmpDirPath()); @@ -889,204 +681,6 @@ public: } }; -void tst_QWebEnginePage::cursorMovements() -{ -#if !defined(QWEBENGINEPAGE_SELECTEDTEXT) - QSKIP("QWEBENGINEPAGE_SELECTEDTEXT"); -#else - QScopedPointer<CursorTrackedPage> page(new CursorTrackedPage); - QString content("<html><body><p id=one>The quick brown fox</p><p id=two>jumps over the lazy dog</p><p>May the source<br/>be with you!</p></body></html>"); - page->setHtml(content); - - // this will select the first paragraph - QString script = "var range = document.createRange(); " \ - "var node = document.getElementById(\"one\"); " \ - "range.selectNode(node); " \ - "getSelection().addRange(range);"; - evaluateJavaScriptSync(page.data(), script); - QCOMPARE(page->selectedText().trimmed(), QString::fromLatin1("The quick brown fox")); - - QRegExp regExp(" style=\".*\""); - regExp.setMinimal(true); - QCOMPARE(page->selectedHtml().trimmed().replace(regExp, ""), QString::fromLatin1("<p id=\"one\">The quick brown fox</p>")); - - // these actions must exist - QVERIFY(page->action(QWebEnginePage::MoveToNextChar) != 0); - QVERIFY(page->action(QWebEnginePage::MoveToPreviousChar) != 0); - QVERIFY(page->action(QWebEnginePage::MoveToNextWord) != 0); - QVERIFY(page->action(QWebEnginePage::MoveToPreviousWord) != 0); - QVERIFY(page->action(QWebEnginePage::MoveToNextLine) != 0); - QVERIFY(page->action(QWebEnginePage::MoveToPreviousLine) != 0); - QVERIFY(page->action(QWebEnginePage::MoveToStartOfLine) != 0); - QVERIFY(page->action(QWebEnginePage::MoveToEndOfLine) != 0); - QVERIFY(page->action(QWebEnginePage::MoveToStartOfBlock) != 0); - QVERIFY(page->action(QWebEnginePage::MoveToEndOfBlock) != 0); - QVERIFY(page->action(QWebEnginePage::MoveToStartOfDocument) != 0); - QVERIFY(page->action(QWebEnginePage::MoveToEndOfDocument) != 0); - - // right now they are disabled because contentEditable is false - QCOMPARE(page->action(QWebEnginePage::MoveToNextChar)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::MoveToPreviousChar)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::MoveToNextWord)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::MoveToPreviousWord)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::MoveToNextLine)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::MoveToPreviousLine)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::MoveToStartOfLine)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::MoveToEndOfLine)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::MoveToStartOfBlock)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::MoveToEndOfBlock)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::MoveToStartOfDocument)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::MoveToEndOfDocument)->isEnabled(), false); - - // make it editable before navigating the cursor - page->setContentEditable(true); - - // here the actions are enabled after contentEditable is true - QCOMPARE(page->action(QWebEnginePage::MoveToNextChar)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::MoveToPreviousChar)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::MoveToNextWord)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::MoveToPreviousWord)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::MoveToNextLine)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::MoveToPreviousLine)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::MoveToStartOfLine)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::MoveToEndOfLine)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::MoveToStartOfBlock)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::MoveToEndOfBlock)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::MoveToStartOfDocument)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::MoveToEndOfDocument)->isEnabled(), true); - - // cursor will be before the word "jump" - page->triggerAction(QWebEnginePage::MoveToNextChar); - QVERIFY(page->isSelectionCollapsed()); - QCOMPARE(page->selectionStartOffset(), 0); - - // cursor will be between 'j' and 'u' in the word "jump" - page->triggerAction(QWebEnginePage::MoveToNextChar); - QVERIFY(page->isSelectionCollapsed()); - QCOMPARE(page->selectionStartOffset(), 1); - - // cursor will be between 'u' and 'm' in the word "jump" - page->triggerAction(QWebEnginePage::MoveToNextChar); - QVERIFY(page->isSelectionCollapsed()); - QCOMPARE(page->selectionStartOffset(), 2); - - // cursor will be after the word "jump" - page->triggerAction(QWebEnginePage::MoveToNextWord); - QVERIFY(page->isSelectionCollapsed()); - QCOMPARE(page->selectionStartOffset(), 5); - - // cursor will be after the word "lazy" - page->triggerAction(QWebEnginePage::MoveToNextWord); - page->triggerAction(QWebEnginePage::MoveToNextWord); - page->triggerAction(QWebEnginePage::MoveToNextWord); - QVERIFY(page->isSelectionCollapsed()); - QCOMPARE(page->selectionStartOffset(), 19); - - // cursor will be between 'z' and 'y' in "lazy" - page->triggerAction(QWebEnginePage::MoveToPreviousChar); - QVERIFY(page->isSelectionCollapsed()); - QCOMPARE(page->selectionStartOffset(), 18); - - // cursor will be between 'a' and 'z' in "lazy" - page->triggerAction(QWebEnginePage::MoveToPreviousChar); - QVERIFY(page->isSelectionCollapsed()); - QCOMPARE(page->selectionStartOffset(), 17); - - // cursor will be before the word "lazy" - page->triggerAction(QWebEnginePage::MoveToPreviousWord); - QVERIFY(page->isSelectionCollapsed()); - QCOMPARE(page->selectionStartOffset(), 15); - - // cursor will be before the word "quick" - page->triggerAction(QWebEnginePage::MoveToPreviousWord); - page->triggerAction(QWebEnginePage::MoveToPreviousWord); - page->triggerAction(QWebEnginePage::MoveToPreviousWord); - page->triggerAction(QWebEnginePage::MoveToPreviousWord); - page->triggerAction(QWebEnginePage::MoveToPreviousWord); - page->triggerAction(QWebEnginePage::MoveToPreviousWord); - QVERIFY(page->isSelectionCollapsed()); - QCOMPARE(page->selectionStartOffset(), 4); - - // cursor will be between 'p' and 's' in the word "jumps" - page->triggerAction(QWebEnginePage::MoveToNextWord); - page->triggerAction(QWebEnginePage::MoveToNextWord); - page->triggerAction(QWebEnginePage::MoveToNextWord); - page->triggerAction(QWebEnginePage::MoveToNextChar); - page->triggerAction(QWebEnginePage::MoveToNextChar); - page->triggerAction(QWebEnginePage::MoveToNextChar); - page->triggerAction(QWebEnginePage::MoveToNextChar); - page->triggerAction(QWebEnginePage::MoveToNextChar); - QVERIFY(page->isSelectionCollapsed()); - QCOMPARE(page->selectionStartOffset(), 4); - - // cursor will be before the word "jumps" - page->triggerAction(QWebEnginePage::MoveToStartOfLine); - QVERIFY(page->isSelectionCollapsed()); - QCOMPARE(page->selectionStartOffset(), 0); - - // cursor will be after the word "dog" - page->triggerAction(QWebEnginePage::MoveToEndOfLine); - QVERIFY(page->isSelectionCollapsed()); - QCOMPARE(page->selectionStartOffset(), 23); - - // cursor will be between 'w' and 'n' in "brown" - page->triggerAction(QWebEnginePage::MoveToStartOfLine); - page->triggerAction(QWebEnginePage::MoveToPreviousWord); - page->triggerAction(QWebEnginePage::MoveToPreviousWord); - page->triggerAction(QWebEnginePage::MoveToNextChar); - page->triggerAction(QWebEnginePage::MoveToNextChar); - page->triggerAction(QWebEnginePage::MoveToNextChar); - page->triggerAction(QWebEnginePage::MoveToNextChar); - QVERIFY(page->isSelectionCollapsed()); - QCOMPARE(page->selectionStartOffset(), 14); - - // cursor will be after the word "fox" - page->triggerAction(QWebEnginePage::MoveToEndOfLine); - QVERIFY(page->isSelectionCollapsed()); - QCOMPARE(page->selectionStartOffset(), 19); - - // cursor will be before the word "The" - page->triggerAction(QWebEnginePage::MoveToStartOfDocument); - QVERIFY(page->isSelectionCollapsed()); - QCOMPARE(page->selectionStartOffset(), 0); - - // cursor will be after the word "you!" - page->triggerAction(QWebEnginePage::MoveToEndOfDocument); - QVERIFY(page->isSelectionCollapsed()); - QCOMPARE(page->selectionStartOffset(), 12); - - // cursor will be before the word "be" - page->triggerAction(QWebEnginePage::MoveToStartOfBlock); - QVERIFY(page->isSelectionCollapsed()); - QCOMPARE(page->selectionStartOffset(), 0); - - // cursor will be after the word "you!" - page->triggerAction(QWebEnginePage::MoveToEndOfBlock); - QVERIFY(page->isSelectionCollapsed()); - QCOMPARE(page->selectionStartOffset(), 12); - - // try to move before the document start - page->triggerAction(QWebEnginePage::MoveToStartOfDocument); - page->triggerAction(QWebEnginePage::MoveToPreviousChar); - QVERIFY(page->isSelectionCollapsed()); - QCOMPARE(page->selectionStartOffset(), 0); - page->triggerAction(QWebEnginePage::MoveToStartOfDocument); - page->triggerAction(QWebEnginePage::MoveToPreviousWord); - QVERIFY(page->isSelectionCollapsed()); - QCOMPARE(page->selectionStartOffset(), 0); - - // try to move past the document end - page->triggerAction(QWebEnginePage::MoveToEndOfDocument); - page->triggerAction(QWebEnginePage::MoveToNextChar); - QVERIFY(page->isSelectionCollapsed()); - QCOMPARE(page->selectionStartOffset(), 12); - page->triggerAction(QWebEnginePage::MoveToEndOfDocument); - page->triggerAction(QWebEnginePage::MoveToNextWord); - QVERIFY(page->isSelectionCollapsed()); - QCOMPARE(page->selectionStartOffset(), 12); -#endif -} - void tst_QWebEnginePage::textSelection() { QWebEngineView view; @@ -1101,35 +695,6 @@ void tst_QWebEnginePage::textSelection() // these actions must exist QVERIFY(page->action(QWebEnginePage::SelectAll) != 0); -#if defined(QWEBENGINEPAGE_SELECTACTIONS) - QVERIFY(page->action(QWebEnginePage::SelectNextChar) != 0); - QVERIFY(page->action(QWebEnginePage::SelectPreviousChar) != 0); - QVERIFY(page->action(QWebEnginePage::SelectNextWord) != 0); - QVERIFY(page->action(QWebEnginePage::SelectPreviousWord) != 0); - QVERIFY(page->action(QWebEnginePage::SelectNextLine) != 0); - QVERIFY(page->action(QWebEnginePage::SelectPreviousLine) != 0); - QVERIFY(page->action(QWebEnginePage::SelectStartOfLine) != 0); - QVERIFY(page->action(QWebEnginePage::SelectEndOfLine) != 0); - QVERIFY(page->action(QWebEnginePage::SelectStartOfBlock) != 0); - QVERIFY(page->action(QWebEnginePage::SelectEndOfBlock) != 0); - QVERIFY(page->action(QWebEnginePage::SelectStartOfDocument) != 0); - QVERIFY(page->action(QWebEnginePage::SelectEndOfDocument) != 0); - - // right now they are disabled because contentEditable is false and - // there isn't an existing selection to modify - QCOMPARE(page->action(QWebEnginePage::SelectNextChar)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::SelectPreviousChar)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::SelectNextWord)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::SelectPreviousWord)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::SelectNextLine)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::SelectPreviousLine)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::SelectStartOfLine)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::SelectEndOfLine)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::SelectStartOfBlock)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::SelectEndOfBlock)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::SelectStartOfDocument)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::SelectEndOfDocument)->isEnabled(), false); -#endif // ..but SelectAll is awalys enabled QCOMPARE(page->action(QWebEnginePage::SelectAll)->isEnabled(), true); @@ -1144,173 +709,10 @@ void tst_QWebEnginePage::textSelection() "getSelection().addRange(range);"; evaluateJavaScriptSync(page, selectScript); QCOMPARE(page->selectedText().trimmed(), QString::fromLatin1("The quick brown fox")); -#if defined(QWEBENGINEPAGE_SELECTEDHTML) - QRegExp regExp(" style=\".*\""); - regExp.setMinimal(true); - QCOMPARE(page->selectedHtml().trimmed().replace(regExp, ""), QString::fromLatin1("<p id=\"one\">The quick brown fox</p>")); -#endif // Make sure hasSelection returns true, since there is selected text now... QCOMPARE(page->hasSelection(), true); - -#if defined(QWEBENGINEPAGE_SELECTACTIONS) - // here the actions are enabled after a selection has been created - QCOMPARE(page->action(QWebEnginePage::SelectNextChar)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::SelectPreviousChar)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::SelectNextWord)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::SelectPreviousWord)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::SelectNextLine)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::SelectPreviousLine)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::SelectStartOfLine)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::SelectEndOfLine)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::SelectStartOfBlock)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::SelectEndOfBlock)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::SelectStartOfDocument)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::SelectEndOfDocument)->isEnabled(), true); - - // make it editable before navigating the cursor - page->setContentEditable(true); - - // cursor will be before the word "The", this makes sure there is a charet - page->triggerAction(QWebEnginePage::MoveToStartOfDocument); - QVERIFY(page->isSelectionCollapsed()); - QCOMPARE(page->selectionStartOffset(), 0); - - // here the actions are enabled after contentEditable is true - QCOMPARE(page->action(QWebEnginePage::SelectNextChar)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::SelectPreviousChar)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::SelectNextWord)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::SelectPreviousWord)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::SelectNextLine)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::SelectPreviousLine)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::SelectStartOfLine)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::SelectEndOfLine)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::SelectStartOfBlock)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::SelectEndOfBlock)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::SelectStartOfDocument)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::SelectEndOfDocument)->isEnabled(), true); -#endif } -void tst_QWebEnginePage::textEditing() -{ -#if !defined(QWEBENGINEPAGE_EVALUATEJAVASCRIPT) - QSKIP("QWEBENGINEPAGE_EVALUATEJAVASCRIPT"); -#else - QScopedPointer<CursorTrackedPage> page(new CursorTrackedPage); - QString content("<html><body><p id=one>The quick brown fox</p>" \ - "<p id=two>jumps over the lazy dog</p>" \ - "<p>May the source<br/>be with you!</p></body></html>"); - page->setHtml(content); - - // these actions must exist - QVERIFY(page->action(QWebEnginePage::Cut) != 0); - QVERIFY(page->action(QWebEnginePage::Copy) != 0); - QVERIFY(page->action(QWebEnginePage::Paste) != 0); - QVERIFY(page->action(QWebEnginePage::DeleteStartOfWord) != 0); - QVERIFY(page->action(QWebEnginePage::DeleteEndOfWord) != 0); - QVERIFY(page->action(QWebEnginePage::SetTextDirectionDefault) != 0); - QVERIFY(page->action(QWebEnginePage::SetTextDirectionLeftToRight) != 0); - QVERIFY(page->action(QWebEnginePage::SetTextDirectionRightToLeft) != 0); - QVERIFY(page->action(QWebEnginePage::ToggleBold) != 0); - QVERIFY(page->action(QWebEnginePage::ToggleItalic) != 0); - QVERIFY(page->action(QWebEnginePage::ToggleUnderline) != 0); - QVERIFY(page->action(QWebEnginePage::InsertParagraphSeparator) != 0); - QVERIFY(page->action(QWebEnginePage::InsertLineSeparator) != 0); - QVERIFY(page->action(QWebEnginePage::PasteAndMatchStyle) != 0); - QVERIFY(page->action(QWebEnginePage::RemoveFormat) != 0); - QVERIFY(page->action(QWebEnginePage::ToggleStrikethrough) != 0); - QVERIFY(page->action(QWebEnginePage::ToggleSubscript) != 0); - QVERIFY(page->action(QWebEnginePage::ToggleSuperscript) != 0); - QVERIFY(page->action(QWebEnginePage::InsertUnorderedList) != 0); - QVERIFY(page->action(QWebEnginePage::InsertOrderedList) != 0); - QVERIFY(page->action(QWebEnginePage::Indent) != 0); - QVERIFY(page->action(QWebEnginePage::Outdent) != 0); - QVERIFY(page->action(QWebEnginePage::AlignCenter) != 0); - QVERIFY(page->action(QWebEnginePage::AlignJustified) != 0); - QVERIFY(page->action(QWebEnginePage::AlignLeft) != 0); - QVERIFY(page->action(QWebEnginePage::AlignRight) != 0); - - // right now they are disabled because contentEditable is false - QCOMPARE(page->action(QWebEnginePage::Cut)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::Paste)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::DeleteStartOfWord)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::DeleteEndOfWord)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::SetTextDirectionDefault)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::SetTextDirectionLeftToRight)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::SetTextDirectionRightToLeft)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::ToggleBold)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::ToggleItalic)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::ToggleUnderline)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::InsertParagraphSeparator)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::InsertLineSeparator)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::PasteAndMatchStyle)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::RemoveFormat)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::ToggleStrikethrough)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::ToggleSubscript)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::ToggleSuperscript)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::InsertUnorderedList)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::InsertOrderedList)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::Indent)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::Outdent)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::AlignCenter)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::AlignJustified)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::AlignLeft)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::AlignRight)->isEnabled(), false); - - // Select everything - page->triggerAction(QWebEnginePage::SelectAll); - - // make sure it is enabled since there is a selection - QCOMPARE(page->action(QWebEnginePage::Copy)->isEnabled(), true); - - // make it editable before navigating the cursor - page->setContentEditable(true); - - // clear the selection - page->triggerAction(QWebEnginePage::MoveToStartOfDocument); - QVERIFY(page->isSelectionCollapsed()); - QCOMPARE(page->selectionStartOffset(), 0); - - // make sure it is disabled since there isn't a selection - QCOMPARE(page->action(QWebEnginePage::Copy)->isEnabled(), false); - - // here the actions are enabled after contentEditable is true - QCOMPARE(page->action(QWebEnginePage::Paste)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::DeleteStartOfWord)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::DeleteEndOfWord)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::SetTextDirectionDefault)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::SetTextDirectionLeftToRight)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::SetTextDirectionRightToLeft)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::ToggleBold)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::ToggleItalic)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::ToggleUnderline)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::InsertParagraphSeparator)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::InsertLineSeparator)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::PasteAndMatchStyle)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::ToggleStrikethrough)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::ToggleSubscript)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::ToggleSuperscript)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::InsertUnorderedList)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::InsertOrderedList)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::Indent)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::Outdent)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::AlignCenter)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::AlignJustified)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::AlignLeft)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::AlignRight)->isEnabled(), true); - - // make sure these are disabled since there isn't a selection - QCOMPARE(page->action(QWebEnginePage::Cut)->isEnabled(), false); - QCOMPARE(page->action(QWebEnginePage::RemoveFormat)->isEnabled(), false); - - // make sure everything is selected - page->triggerAction(QWebEnginePage::SelectAll); - - // this is only true if there is an editable selection - QCOMPARE(page->action(QWebEnginePage::Cut)->isEnabled(), true); - QCOMPARE(page->action(QWebEnginePage::RemoveFormat)->isEnabled(), true); -#endif -} void tst_QWebEnginePage::backActionUpdate() { @@ -1331,243 +733,39 @@ void tst_QWebEnginePage::backActionUpdate() QVERIFY(action->isEnabled()); } -#if defined(QWEBENGINEPAGE_SETTINGS) -static inline bool testFlag(QWebEnginePage& webPage, QWebEngineSettings::WebAttribute settingAttribute, const QString& jsObjectName, bool settingValue) -{ - webPage.settings()->setAttribute(settingAttribute, settingValue); - return evaluateJavaScriptSync(&webPage, QString("(window.%1 != undefined)").arg(jsObjectName)).toBool(); -} -#endif - -void tst_QWebEnginePage::testOptionalJSObjects() -{ -#if !defined(QWEBENGINESETTINGS) - QSKIP("QWEBENGINSETTINGS"); -#else - // Once a feature is enabled and the JS object is accessed turning off the setting will not turn off - // the visibility of the JS object any more. For this reason this test uses two QWebEnginePage instances. - // Part of the test is to make sure that the QWebEnginePage instances do not interfere with each other so turning on - // a feature for one instance will not turn it on for another. - - QWebEnginePage webPage1; - QWebEnginePage webPage2; - - webPage1.currentFrame()->setHtml(QString("<html><body>test</body></html>"), QUrl("http://www.example.com/")); - webPage2.currentFrame()->setHtml(QString("<html><body>test</body></html>"), QUrl("http://www.example.com/")); - - QEXPECT_FAIL("","Feature enabled/disabled checking problem. Look at bugs.webkit.org/show_bug.cgi?id=29867", Continue); - QCOMPARE(testFlag(webPage1, QWebEngineSettings::OfflineWebApplicationCacheEnabled, "applicationCache", false), false); - QCOMPARE(testFlag(webPage2, QWebEngineSettings::OfflineWebApplicationCacheEnabled, "applicationCache", true), true); - QEXPECT_FAIL("","Feature enabled/disabled checking problem. Look at bugs.webkit.org/show_bug.cgi?id=29867", Continue); - QCOMPARE(testFlag(webPage1, QWebEngineSettings::OfflineWebApplicationCacheEnabled, "applicationCache", false), false); - QCOMPARE(testFlag(webPage2, QWebEngineSettings::OfflineWebApplicationCacheEnabled, "applicationCache", false), true); - - QCOMPARE(testFlag(webPage1, QWebEngineSettings::LocalStorageEnabled, "localStorage", false), false); - QCOMPARE(testFlag(webPage2, QWebEngineSettings::LocalStorageEnabled, "localStorage", true), true); - QCOMPARE(testFlag(webPage1, QWebEngineSettings::LocalStorageEnabled, "localStorage", false), false); - QCOMPARE(testFlag(webPage2, QWebEngineSettings::LocalStorageEnabled, "localStorage", false), true); -#endif -} - -#if defined(QWEBENGINEPAGE_SETTINGS) -static inline bool checkLocalStorageVisibility(QWebEnginePage& webPage, bool localStorageEnabled) -{ - webPage.settings()->setAttribute(QWebEngineSettings::LocalStorageEnabled, localStorageEnabled); - return evaluateJavaScriptSync(&webPage, QString("(window.localStorage != undefined)")).toBool(); -} -#endif - -void tst_QWebEnginePage::testLocalStorageVisibility() -{ -#if !defined(QWEBENGINEPAGE_SETTINGS) - QSKIP("QWEBENGINEPAGE_SETTINGS"); -#else - // Local storage's visibility depends on its security origin, which depends on base url. - // Initially, it will test it with base urls that get a globally unique origin, which may not - // be able to use local storage even if the feature is enabled. Then later the same test is - // done but with urls that would get a valid origin, so local storage could be used. - // Before every test case it checks if local storage is not already visible. - - QWebEnginePage webPage; - - webPage.currentFrame()->setHtml(QString("<html><body>test</body></html>"), QUrl()); - - QCOMPARE(checkLocalStorageVisibility(webPage, false), false); - QCOMPARE(checkLocalStorageVisibility(webPage, true), false); - - webPage.currentFrame()->setHtml(QString("<html><body>test</body></html>"), QUrl("invalid")); - - QCOMPARE(checkLocalStorageVisibility(webPage, false), false); - QCOMPARE(checkLocalStorageVisibility(webPage, true), false); - - webPage.currentFrame()->setHtml(QString("<html><body>test</body></html>"), QUrl("://misparsed.com")); - - QCOMPARE(checkLocalStorageVisibility(webPage, false), false); - QCOMPARE(checkLocalStorageVisibility(webPage, true), false); - - webPage.currentFrame()->setHtml(QString("<html><body>test</body></html>"), QUrl("http://")); - - QCOMPARE(checkLocalStorageVisibility(webPage, false), false); - QCOMPARE(checkLocalStorageVisibility(webPage, true), false); - - webPage.currentFrame()->setHtml(QString("<html><body>test</body></html>"), QUrl("about:blank")); - - QCOMPARE(checkLocalStorageVisibility(webPage, false), false); - QCOMPARE(checkLocalStorageVisibility(webPage, true), false); - - webPage.currentFrame()->setHtml(QString("<html><body>test</body></html>"), QUrl("data:text/html,test")); - - QCOMPARE(checkLocalStorageVisibility(webPage, false), false); - QCOMPARE(checkLocalStorageVisibility(webPage, true), false); - - webPage.currentFrame()->setHtml(QString("<html><body>test</body></html>"), QUrl("file:///")); - - QCOMPARE(checkLocalStorageVisibility(webPage, false), false); - QCOMPARE(checkLocalStorageVisibility(webPage, true), true); - - webPage.currentFrame()->setHtml(QString("<html><body>test</body></html>"), QUrl("http://www.example.com")); - - QCOMPARE(checkLocalStorageVisibility(webPage, false), false); - QCOMPARE(checkLocalStorageVisibility(webPage, true), true); - - webPage.currentFrame()->setHtml(QString("<html><body>test</body></html>"), QUrl("https://www.example.com")); - - QCOMPARE(checkLocalStorageVisibility(webPage, false), false); - QCOMPARE(checkLocalStorageVisibility(webPage, true), true); - - webPage.currentFrame()->setHtml(QString("<html><body>test</body></html>"), QUrl("ftp://files.example.com")); - - QCOMPARE(checkLocalStorageVisibility(webPage, false), false); - QCOMPARE(checkLocalStorageVisibility(webPage, true), true); - - webPage.currentFrame()->setHtml(QString("<html><body>test</body></html>"), QUrl("file:///path/to/index.html")); - - QCOMPARE(checkLocalStorageVisibility(webPage, false), false); - QCOMPARE(checkLocalStorageVisibility(webPage, true), true); -#endif -} - -void tst_QWebEnginePage::testEnablePersistentStorage() -{ -#if !defined(QWEBENGINESETTINGS) - QSKIP("QWEBENGINESETTINGS"); -#else - QWebEnginePage webPage; - - // By default all persistent options should be disabled - QCOMPARE(webPage.settings()->testAttribute(QWebEngineSettings::LocalStorageEnabled), false); - QCOMPARE(webPage.settings()->testAttribute(QWebEngineSettings::OfflineStorageDatabaseEnabled), false); - QCOMPARE(webPage.settings()->testAttribute(QWebEngineSettings::OfflineWebApplicationCacheEnabled), false); - QVERIFY(webPage.settings()->iconDatabasePath().isEmpty()); - - QWebEngineSettings::enablePersistentStorage(); - - - QTRY_COMPARE(webPage.settings()->testAttribute(QWebEngineSettings::LocalStorageEnabled), true); - QTRY_COMPARE(webPage.settings()->testAttribute(QWebEngineSettings::OfflineStorageDatabaseEnabled), true); - QTRY_COMPARE(webPage.settings()->testAttribute(QWebEngineSettings::OfflineWebApplicationCacheEnabled), true); - - QTRY_VERIFY(!webPage.settings()->offlineStoragePath().isEmpty()); - QTRY_VERIFY(!webPage.settings()->offlineWebApplicationCachePath().isEmpty()); - QTRY_VERIFY(!webPage.settings()->iconDatabasePath().isEmpty()); -#endif -} - - -#if defined(QWEBENGINEPAGE_ERRORPAGEEXTENSION) -class ErrorPage : public QWebEnginePage -{ -public: - - ErrorPage(QWidget* parent = 0): QWebEnginePage(parent) - { - } - - virtual bool supportsExtension(Extension extension) const - { - return extension == ErrorPageExtension; - } - - virtual bool extension(Extension, const ExtensionOption* option, ExtensionReturn* output) - { - ErrorPageExtensionReturn* errorPage = static_cast<ErrorPageExtensionReturn*>(output); - - errorPage->contentType = "text/html"; - errorPage->content = "error"; - return true; - } -}; -#endif - -void tst_QWebEnginePage::errorPageExtension() -{ -#if !defined(QWEBENGINEPAGE_ERRORPAGEEXTENSION) - QSKIP("QWEBENGINEPAGE_ERRORPAGEEXTENSION"); -#else - ErrorPage page; - m_view->setPage(&page); - - QSignalSpy spyLoadFinished(m_view, SIGNAL(loadFinished(bool))); - - m_view->setUrl(QUrl("data:text/html,foo")); - QTRY_COMPARE(spyLoadFinished.count(), 1); - - page.setUrl(QUrl("http://non.existent/url")); - QTRY_COMPARE(spyLoadFinished.count(), 2); - QCOMPARE(toPlainTextSync(&page), QString("error")); - QCOMPARE(page.history()->count(), 2); - QCOMPARE(page.history()->currentItem().url(), QUrl("http://non.existent/url")); - QCOMPARE(page.history()->canGoBack(), true); - QCOMPARE(page.history()->canGoForward(), false); - - page.triggerAction(QWebEnginePage::Back); - QTRY_COMPARE(page.history()->canGoBack(), false); - QTRY_COMPARE(page.history()->canGoForward(), true); - - page.triggerAction(QWebEnginePage::Forward); - QTRY_COMPARE(page.history()->canGoBack(), true); - QTRY_COMPARE(page.history()->canGoForward(), false); - - page.triggerAction(QWebEnginePage::Back); - QTRY_COMPARE(page.history()->canGoBack(), false); - QTRY_COMPARE(page.history()->canGoForward(), true); - QTRY_COMPARE(page.history()->currentItem().url(), QUrl("data:text/html,foo")); - - m_view->setPage(0); -#endif -} - -void tst_QWebEnginePage::errorPageExtensionLoadFinished() +void tst_QWebEnginePage::localStorageVisibility() { -#if !defined(QWEBENGINEPAGE_ERRORPAGEEXTENSION) - QSKIP("QWEBENGINEPAGE_ERRORPAGEEXTENSION"); -#else - ErrorPage page; - m_view->setPage(&page); - - QSignalSpy spyLoadFinished(m_view, SIGNAL(loadFinished(bool))); - QSignalSpy spyFrameLoadFinished(m_view->page(), SIGNAL(loadFinished(bool))); - - m_view->setUrl(QUrl("data:text/html,foo")); - QTRY_COMPARE(spyLoadFinished.count(), 1); - QTRY_COMPARE(spyFrameLoadFinished.count(), 1); - - const bool loadSucceded = spyLoadFinished.at(0).at(0).toBool(); - QVERIFY(loadSucceded); - const bool frameLoadSucceded = spyFrameLoadFinished.at(0).at(0).toBool(); - QVERIFY(frameLoadSucceded); - - m_view->page()->setUrl(QUrl("http://non.existent/url")); - QTRY_COMPARE(spyLoadFinished.count(), 2); - QTRY_COMPARE(spyFrameLoadFinished.count(), 2); - - const bool nonExistantLoadSucceded = spyLoadFinished.at(1).at(0).toBool(); - QVERIFY(nonExistantLoadSucceded); - const bool nonExistantFrameLoadSucceded = spyFrameLoadFinished.at(1).at(0).toBool(); - QVERIFY(nonExistantFrameLoadSucceded); - - m_view->setPage(0); -#endif + QWebEngineProfile profile; + QWebEnginePage webPage1(&profile); + QWebEnginePage webPage2(&profile); + + webPage1.settings()->setAttribute(QWebEngineSettings::LocalStorageEnabled, true); + webPage2.settings()->setAttribute(QWebEngineSettings::LocalStorageEnabled, false); + + QSignalSpy loadSpy1(&webPage1, &QWebEnginePage::loadFinished); + QSignalSpy loadSpy2(&webPage2, &QWebEnginePage::loadFinished); + webPage1.setHtml(QString("<html><body>test</body></html>"), QUrl("http://www.example.com/")); + webPage2.setHtml(QString("<html><body>test</body></html>"), QUrl("http://www.example.com/")); + QTRY_COMPARE(loadSpy1.count(), 1); + QTRY_COMPARE(loadSpy2.count(), 1); + + // The attribute determines the visibility of the window.localStorage object. + QVERIFY(evaluateJavaScriptSync(&webPage1, QString("(window.localStorage != undefined)")).toBool()); + QVERIFY(!evaluateJavaScriptSync(&webPage2, QString("(window.localStorage != undefined)")).toBool()); + + // Switching the feature off does not actively remove the object from webPage1. + webPage1.settings()->setAttribute(QWebEngineSettings::LocalStorageEnabled, false); + webPage2.settings()->setAttribute(QWebEngineSettings::LocalStorageEnabled, true); + QVERIFY(evaluateJavaScriptSync(&webPage1, QString("(window.localStorage != undefined)")).toBool()); + QVERIFY(evaluateJavaScriptSync(&webPage2, QString("(window.localStorage != undefined)")).toBool()); + + // The object disappears only after reloading. + webPage1.triggerAction(QWebEnginePage::Reload); + webPage2.triggerAction(QWebEnginePage::Reload); + QTRY_COMPARE(loadSpy1.count(), 2); + QTRY_COMPARE(loadSpy2.count(), 2); + QVERIFY(!evaluateJavaScriptSync(&webPage1, QString("(window.localStorage != undefined)")).toBool()); + QVERIFY(evaluateJavaScriptSync(&webPage2, QString("(window.localStorage != undefined)")).toBool()); } void tst_QWebEnginePage::userAgentNewlineStripping() @@ -1592,70 +790,9 @@ void tst_QWebEnginePage::crashTests_LazyInitializationOfMainFrame() webPage.selectedText(); } { -#if defined(QWEBENGINEPAGE_SELECTEDHTML) - QWebEnginePage webPage; - webPage.selectedHtml(); -#endif - } - { QWebEnginePage webPage; webPage.triggerAction(QWebEnginePage::Back, true); } - { -#if defined(QWEBENGINEPAGE_UPDATEPOSITIONDEPENDENTACTIONS) - QWebEnginePage webPage; - QPoint pos(10,10); - webPage.updatePositionDependentActions(pos); -#endif - } -} - -#if defined(QWEBENGINEPAGE_RENDER) -static void takeScreenshot(QWebEnginePage* page) -{ - page->setViewportSize(page->contentsSize()); - QImage image(page->viewportSize(), QImage::Format_ARGB32); - QPainter painter(&image); - page->render(&painter); - painter.end(); -} -#endif - -void tst_QWebEnginePage::screenshot_data() -{ - QTest::addColumn<QString>("html"); - QTest::newRow("WithoutPlugin") << "<html><body id='b'>text</body></html>"; - QTest::newRow("WindowedPlugin") << QString("<html><body id='b'>text<embed src='resources/test.swf'></embed></body></html>"); - QTest::newRow("WindowlessPlugin") << QString("<html><body id='b'>text<embed src='resources/test.swf' wmode='transparent'></embed></body></html>"); -} - -void tst_QWebEnginePage::screenshot() -{ -#if !defined(QWEBENGINESETTINGS) - QSKIP("QWEBENGINESETTINGS"); -#else - if (!QDir(TESTS_SOURCE_DIR).exists()) - W_QSKIP(QString("This test requires access to resources found in '%1'").arg(TESTS_SOURCE_DIR).toLatin1().constData(), SkipAll); - - QDir::setCurrent(TESTS_SOURCE_DIR); - - QFETCH(QString, html); - QWebEnginePage page; - page.settings()->setAttribute(QWebEngineSettings::PluginsEnabled, true); - page.setHtml(html, QUrl::fromLocalFile(TESTS_SOURCE_DIR)); - QVERIFY(spyFinished.wait(2000)); - - // take screenshot without a view - takeScreenshot(&page); - - QWebEngineView view; - view.setPage(&page); - - // take screenshot when attached to a view - takeScreenshot(&page); - - QDir::setCurrent(QApplication::applicationDirPath()); -#endif } #if defined(ENABLE_WEBGL) && ENABLE_WEBGL @@ -1754,37 +891,6 @@ void tst_QWebEnginePage::testJSPrompt() QVERIFY(res); } -void tst_QWebEnginePage::testStopScheduledPageRefresh() -{ -#if !defined(QWEBENGINEPAGE_SETNETWORKACCESSMANAGER) - QSKIP("QWEBENGINEPAGE_SETNETWORKACCESSMANAGER"); -#else - // Without QWebEnginePage::StopScheduledPageRefresh - QWebEnginePage page1; - page1.setNetworkAccessManager(new TestNetworkManager(&page1)); - page1.setHtml("<html><head>" - "<meta http-equiv=\"refresh\"content=\"0;URL=qrc:///resources/index.html\">" - "</head><body><h1>Page redirects immediately...</h1>" - "</body></html>"); - QSignalSpy spyFinished(&page1, &QWebEnginePage::loadFinished); - QVERIFY(spyFinished.wait(); - QTest::qWait(500); - QCOMPARE(page1.url(), QUrl(QLatin1String("qrc:///resources/index.html"))); - - // With QWebEnginePage::StopScheduledPageRefresh - QWebEnginePage page2; - page2.setNetworkAccessManager(new TestNetworkManager(&page2)); - page2.setHtml("<html><head>" - "<meta http-equiv=\"refresh\"content=\"1;URL=qrc:///resources/index.html\">" - "</head><body><h1>Page redirect test with 1 sec timeout...</h1>" - "</body></html>"); - page2.triggerAction(QWebEnginePage::StopScheduledPageRefresh); - QTest::qWait(1500); - QEXPECT_FAIL("", "https://bugs.webkit.org/show_bug.cgi?id=118673", Continue); - QCOMPARE(page2.url().toString(), QLatin1String("about:blank")); -#endif -} - void tst_QWebEnginePage::findText() { QSignalSpy loadSpy(m_view, SIGNAL(loadFinished(bool))); @@ -1884,95 +990,6 @@ void tst_QWebEnginePage::findTextSuccessiveShouldCallAllCallbacks() QVERIFY(spy5.wasCalled()); } -#if defined(QWEBENGINEPAGE_SUPPORTEDCONTENTTYPES) -static QString getMimeTypeForExtension(const QString &ext) -{ - QMimeType mimeType = QMimeDatabase().mimeTypeForFile(QStringLiteral("filename.") + ext.toLower(), QMimeDatabase::MatchExtension); - if (mimeType.isValid() && !mimeType.isDefault()) - return mimeType.name(); - - return QString(); -} -#endif - -void tst_QWebEnginePage::supportedContentType() -{ -#if !defined(QWEBENGINEPAGE_SUPPORTEDCONTENTTYPES) - QSKIP("QWEBENGINEPAGE_SUPPORTEDCONTENTTYPES"); -#else - QStringList contentTypes; - - // Add supported non image types... - contentTypes << "text/html" << "text/xml" << "text/xsl" << "text/plain" << "text/" - << "application/xml" << "application/xhtml+xml" << "application/vnd.wap.xhtml+xml" - << "application/rss+xml" << "application/atom+xml" << "application/json"; - -#if ENABLE_MHTML - contentTypes << "application/x-mimearchive"; -#endif - - // Add supported image types... - const QList<QByteArray> supportedImageFormats = QImageWriter::supportedImageFormats(); - for (const QByteArray &imageType : supportedImageFormats) { - const QString mimeType = getMimeTypeForExtension(imageType); - if (!mimeType.isEmpty()) - contentTypes << mimeType; - } - - // Get the mime types supported by webengine... - const QStringList supportedContentTypes = m_page->supportedContentTypes(); - - for (const QString &mimeType : qAsConst(contentTypes)) - QVERIFY2(supportedContentTypes.contains(mimeType), QString("'%1' is not a supported content type!").arg(mimeType).toLatin1()); - - for (const QString &mimeType : qAsConst(contentTypes)) - QVERIFY2(m_page->supportsContentType(mimeType), QString("Cannot handle content types '%1'!").arg(mimeType).toLatin1()); -#endif -} - -void tst_QWebEnginePage::thirdPartyCookiePolicy() -{ -#if !defined(DUMPRENDERTREESUPPORTQT) - QSKIP("DUMPRENDERTREESUPPORTQT"); -#else - QWebEngineSettings::globalSettings()->setThirdPartyCookiePolicy(QWebEngineSettings::AlwaysBlockThirdPartyCookies); - m_page->networkAccessManager()->setCookieJar(new QNetworkCookieJar()); - QVERIFY(m_page->networkAccessManager()->cookieJar()); - - // These are all first-party cookies, so should pass. - QVERIFY(DumpRenderTreeSupportQt::thirdPartyCookiePolicyAllows(m_page->handle(), - QUrl("http://www.example.com"), QUrl("http://example.com"))); - QVERIFY(DumpRenderTreeSupportQt::thirdPartyCookiePolicyAllows(m_page->handle(), - QUrl("http://www.example.com"), QUrl("http://doc.example.com"))); - QVERIFY(DumpRenderTreeSupportQt::thirdPartyCookiePolicyAllows(m_page->handle(), - QUrl("http://aaa.www.example.com"), QUrl("http://doc.example.com"))); - QVERIFY(DumpRenderTreeSupportQt::thirdPartyCookiePolicyAllows(m_page->handle(), - QUrl("http://example.com"), QUrl("http://www.example.com"))); - QVERIFY(DumpRenderTreeSupportQt::thirdPartyCookiePolicyAllows(m_page->handle(), - QUrl("http://www.example.co.uk"), QUrl("http://example.co.uk"))); - QVERIFY(DumpRenderTreeSupportQt::thirdPartyCookiePolicyAllows(m_page->handle(), - QUrl("http://www.example.co.uk"), QUrl("http://doc.example.co.uk"))); - QVERIFY(DumpRenderTreeSupportQt::thirdPartyCookiePolicyAllows(m_page->handle(), - QUrl("http://aaa.www.example.co.uk"), QUrl("http://doc.example.co.uk"))); - QVERIFY(DumpRenderTreeSupportQt::thirdPartyCookiePolicyAllows(m_page->handle(), - QUrl("http://example.co.uk"), QUrl("http://www.example.co.uk"))); - - // These are all third-party cookies, so should fail. - QVERIFY(!DumpRenderTreeSupportQt::thirdPartyCookiePolicyAllows(m_page->handle(), - QUrl("http://www.example.com"), QUrl("http://slashdot.org"))); - QVERIFY(!DumpRenderTreeSupportQt::thirdPartyCookiePolicyAllows(m_page->handle(), - QUrl("http://example.com"), QUrl("http://anotherexample.com"))); - QVERIFY(!DumpRenderTreeSupportQt::thirdPartyCookiePolicyAllows(m_page->handle(), - QUrl("http://anotherexample.com"), QUrl("http://example.com"))); - QVERIFY(!DumpRenderTreeSupportQt::thirdPartyCookiePolicyAllows(m_page->handle(), - QUrl("http://www.example.co.uk"), QUrl("http://slashdot.co.uk"))); - QVERIFY(!DumpRenderTreeSupportQt::thirdPartyCookiePolicyAllows(m_page->handle(), - QUrl("http://example.co.uk"), QUrl("http://anotherexample.co.uk"))); - QVERIFY(!DumpRenderTreeSupportQt::thirdPartyCookiePolicyAllows(m_page->handle(), - QUrl("http://anotherexample.co.uk"), QUrl("http://example.co.uk"))); -#endif -} - static QWindow *findNewTopLevelWindow(const QWindowList &oldTopLevelWindows) { const auto tlws = QGuiApplication::topLevelWindows(); @@ -2078,66 +1095,6 @@ void tst_QWebEnginePage::macCopyUnicodeToClipboard() } #endif -void tst_QWebEnginePage::contextMenuCopy() -{ -#if !defined(QWEBENGINEELEMENT) - QSKIP("QWEBENGINEELEMENT"); -#else - QWebEngineView view; - - view.setHtml("<a href=\"http://www.google.com\">You cant miss this</a>"); - - view.page()->triggerAction(QWebEnginePage::SelectAll); - QVERIFY(!view.page()->selectedText().isEmpty()); - - QWebEngineElement link = view.page()->mainFrame()->findFirstElement("a"); - QPoint pos(link.geometry().center()); - QContextMenuEvent event(QContextMenuEvent::Mouse, pos); - view.page()->swallowContextMenuEvent(&event); - view.page()->updatePositionDependentActions(pos); - - QList<QMenu*> contextMenus = view.findChildren<QMenu*>(); - QVERIFY(!contextMenus.isEmpty()); - QMenu* contextMenu = contextMenus.first(); - QVERIFY(contextMenu); - - QList<QAction *> list = contextMenu->actions(); - int index = list.indexOf(view.page()->action(QWebEnginePage::Copy)); - QVERIFY(index != -1); -#endif -} - -// https://bugs.webkit.org/show_bug.cgi?id=62139 -void tst_QWebEnginePage::contextMenuPopulatedOnce() -{ -#if !defined(QWEBENGINEELEMENT) - QSKIP("QWEBENGINEELEMENT"); -#else - QWebEngineView view; - - view.setHtml("<input type=\"text\">"); - - QWebEngineElement link = view.page()->mainFrame()->findFirstElement("input"); - QPoint pos(link.geometry().center()); - QContextMenuEvent event(QContextMenuEvent::Mouse, pos); - view.page()->swallowContextMenuEvent(&event); - view.page()->updatePositionDependentActions(pos); - - QList<QMenu*> contextMenus = view.findChildren<QMenu*>(); - QVERIFY(!contextMenus.isEmpty()); - QMenu* contextMenu = contextMenus.first(); - QVERIFY(contextMenu); - - QList<QAction *> list = contextMenu->actions(); - QStringList entries; - while (!list.isEmpty()) { - QString entry = list.takeFirst()->text(); - QVERIFY(!entries.contains(entry)); - entries << entry; - } -#endif -} - void tst_QWebEnginePage::deleteQWebEngineViewTwice() { for (int i = 0; i < 2; ++i) { @@ -2151,62 +1108,6 @@ void tst_QWebEnginePage::deleteQWebEngineViewTwice() } } -#if defined(QWEBENGINEPAGE_RENDER) -class RepaintRequestedRenderer : public QObject { - Q_OBJECT -public: - RepaintRequestedRenderer(QWebEnginePage* page, QPainter* painter) - : m_page(page) - , m_painter(painter) - , m_recursionCount(0) - { - connect(m_page, SIGNAL(repaintRequested(QRect)), this, SLOT(onRepaintRequested(QRect))); - } - -Q_SIGNALS: - void finished(); - -private Q_SLOTS: - void onRepaintRequested(const QRect& rect) - { - QCOMPARE(m_recursionCount, 0); - - m_recursionCount++; - m_page->render(m_painter, rect); - m_recursionCount--; - - QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection); - } - -private: - QWebEnginePage* m_page; - QPainter* m_painter; - int m_recursionCount; -}; -#endif - -void tst_QWebEnginePage::renderOnRepaintRequestedShouldNotRecurse() -{ -#if !defined(QWEBENGINEPAGE_RENDER) - QSKIP("QWEBENGINEPAGE_RENDER"); -#else - QSize viewportSize(720, 576); - QWebEnginePage page; - - QImage image(viewportSize, QImage::Format_ARGB32); - QPainter painter(&image); - - page.setPreferredContentsSize(viewportSize); - page.setViewportSize(viewportSize); - RepaintRequestedRenderer r(&page, &painter); - - page.setHtml("zalan loves trunk", QUrl()); - - QSignalSpy spyFinished(&r, &RepaintRequestedRenderer::finished); - QVERIFY(spyFinished.wait()); -#endif -} - class SpyForLoadSignalsOrder : public QStateMachine { Q_OBJECT public: @@ -2265,24 +1166,6 @@ void tst_QWebEnginePage::loadSignalsOrder() QTRY_VERIFY(loadSpy.isFinished()); } -void tst_QWebEnginePage::undoActionHaveCustomText() -{ -#if !defined(QWEBENGINEPAGE_UNDOACTION) - QSKIP("QWEBENGINEPAGE_UNDOACTION"); -#else - m_page->setHtml("<div id=test contenteditable></div>"); - evaluateJavaScriptSync(m_page, "document.getElementById('test').focus()"); - - evaluateJavaScriptSync(m_page, "document.execCommand('insertText', true, 'Test');"); - QString typingActionText = m_page->action(QWebEnginePage::Undo)->text(); - - evaluateJavaScriptSync(m_page, "document.execCommand('indent', true);"); - QString alignActionText = m_page->action(QWebEnginePage::Undo)->text(); - - QVERIFY(typingActionText != alignActionText); -#endif -} - void tst_QWebEnginePage::renderWidgetHostViewNotShowTopLevel() { QWebEnginePage page; @@ -2601,68 +1484,6 @@ void tst_QWebEnginePage::openWindowDefaultSize() QCOMPARE(requestedGeometry.height(), 100); } -void tst_QWebEnginePage::cssMediaTypeGlobalSetting() -{ -#if !defined(QWEBENGINESETTINGS_SETCSSMEDIATYPE) - QSKIP("QWEBENGINESETTINGS_SETCSSMEDIATYPE"); -#else - QString testHtml("<style>@media tv {body{background-color:red;}}@media handheld {body{background-color:green;}}@media screen {body{background-color:blue;}}</style>"); - QSignalSpy loadSpy(m_view, SIGNAL(loadFinished(bool))); - - QWebEngineSettings::globalSettings()->setCSSMediaType("tv"); - // Clear page specific setting to read from global setting - m_view->page()->settings()->setCSSMediaType(QString()); - m_view->setHtml(testHtml); - QTRY_COMPARE(loadSpy.count(), 1); - QVERIFY(evaluateJavaScriptSync(m_view->page(), "window.matchMedia('tv').matches == true").toBool()); - QVERIFY(QWebEngineSettings::globalSettings()->cssMediaType() == "tv"); - - QWebEngineSettings::globalSettings()->setCSSMediaType("handheld"); - // Clear page specific setting to read from global setting - m_view->page()->settings()->setCSSMediaType(QString()); - m_view->setHtml(testHtml); - QTRY_COMPARE(loadSpy.count(), 2); - QVERIFY(evaluateJavaScriptSync(m_view->page(), "window.matchMedia('handheld').matches == true").toBool()); - QVERIFY(QWebEngineSettings::globalSettings()->cssMediaType() == "handheld"); - - QWebEngineSettings::globalSettings()->setCSSMediaType("screen"); - // Clear page specific setting to read from global setting - m_view->page()->settings()->setCSSMediaType(QString()); - m_view->setHtml(testHtml); - QTRY_COMPARE(loadSpy.count(), 3); - QVERIFY(evaluateJavaScriptSync(m_view->page(), "window.matchMedia('screen').matches == true").toBool()); - QVERIFY(QWebEngineSettings::globalSettings()->cssMediaType() == "screen"); -#endif -} - -void tst_QWebEnginePage::cssMediaTypePageSetting() -{ -#if !defined(QWEBENGINESETTINGS_SETCSSMEDIATYPE) - QSKIP("QWEBENGINESETTINGS_SETCSSMEDIATYPE"); -#else - QString testHtml("<style>@media tv {body{background-color:red;}}@media handheld {body{background-color:green;}}@media screen {body{background-color:blue;}}</style>"); - QSignalSpy loadSpy(m_view, SIGNAL(loadFinished(bool))); - - m_view->page()->settings()->setCSSMediaType("tv"); - m_view->setHtml(testHtml); - QTRY_COMPARE(loadSpy.count(), 1); - QVERIFY(evaluateJavaScriptSync(m_view->page(), "window.matchMedia('tv').matches == true").toBool()); - QVERIFY(m_view->page()->settings()->cssMediaType() == "tv"); - - m_view->page()->settings()->setCSSMediaType("handheld"); - m_view->setHtml(testHtml); - QTRY_COMPARE(loadSpy.count(), 2); - QVERIFY(evaluateJavaScriptSync(m_view->page(), "window.matchMedia('handheld').matches == true").toBool()); - QVERIFY(m_view->page()->settings()->cssMediaType() == "handheld"); - - m_view->page()->settings()->setCSSMediaType("screen"); - m_view->setHtml(testHtml); - QTRY_COMPARE(loadSpy.count(), 3); - QVERIFY(evaluateJavaScriptSync(m_view->page(), "window.matchMedia('screen').matches == true").toBool()); - QVERIFY(m_view->page()->settings()->cssMediaType() == "screen"); -#endif -} - class JavaScriptCallbackBase { public: @@ -3244,55 +2065,6 @@ void tst_QWebEnginePage::setHtmlWithJSAlert() QCOMPARE(toHtmlSync(&page), html); } -void tst_QWebEnginePage::inputFieldFocus() -{ -#if !defined(QWEBENGINEELEMENT) - QSKIP("QWEBENGINEELEMENT"); -#else - QWebEngineView view; - view.setHtml("<html><body><input type=\"text\"></input></body></html>"); - view.resize(400, 100); - view.show(); - QTest::qWaitForWindowExposed(&view); - view.activateWindow(); - view.setFocus(); - QTRY_VERIFY(view.hasFocus()); - - // double the flashing time, should at least blink once already - int delay = qApp->cursorFlashTime() * 2; - - // focus the lineedit and check if it blinks - bool autoSipEnabled = qApp->autoSipEnabled(); - qApp->setAutoSipEnabled(false); - const QWebEngineElement inputElement = view.page()->documentElement().findFirst(QLatin1String("input[type=text]")); - QTest::mouseClick(&view, Qt::LeftButton, 0, inputElement.geometry().center()); - m_inputFieldsTestView = &view; - view.installEventFilter( this ); - QTest::qWait(delay); - QVERIFY2(m_inputFieldTestPaintCount >= 3, - "The input field should have a blinking caret"); - qApp->setAutoSipEnabled(autoSipEnabled); -#endif -} - -void tst_QWebEnginePage::hitTestContent() -{ -#if !defined(QWEBENGINEELEMENT) - QSKIP("QWEBENGINEELEMENT"); -#else - QString html("<html><body><p>A paragraph</p><br/><br/><br/><a href=\"about:blank\" target=\"_foo\" id=\"link\">link text</a></body></html>"); - - QWebEnginePage page; - page.setHtml(html); - page.setViewportSize(QSize(200, 0)); //no height so link is not visible - const QWebEngineElement linkElement = page.documentElement().findFirst(QLatin1String("a#link")); - QWebEngineHitTestResult result = page.hitTestContent(linkElement.geometry().center()); - QCOMPARE(result.linkText(), QString("link text")); - QWebEngineElement link = result.linkElement(); - QCOMPARE(link.attribute("target"), QString("_foo")); -#endif -} - void tst_QWebEnginePage::baseUrl_data() { QTest::addColumn<QString>("html"); @@ -3373,32 +2145,6 @@ void tst_QWebEnginePage::scrollbarsOff() QVERIFY(evaluateJavaScriptSync(view.page(), "innerWidth == document.documentElement.offsetWidth").toBool()); } -void tst_QWebEnginePage::horizontalScrollAfterBack() -{ -#if !defined(QWEBENGINESETTINGS) - QSKIP("QWEBENGINESETTINGS"); -#else - QWebEngineView view; - QSignalSpy loadSpy(view.page(), SIGNAL(loadFinished(bool))); - - view.page()->settings()->setMaximumPagesInCache(2); - view.page()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAsNeeded); - view.page()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAsNeeded); - - view.load(QUrl("qrc:/resources/testiframe2.html")); - view.resize(200, 200); - QTRY_COMPARE(loadSpy.count(), 1); - QTRY_VERIFY((view.page()->scrollBarGeometry(Qt::Horizontal)).height()); - - view.load(QUrl("qrc:/resources/testiframe.html")); - QTRY_COMPARE(loadSpy.count(), 2); - - view.page()->triggerAction(QWebEnginePage::Back); - QTRY_COMPARE(loadSpy.count(), 3); - QTRY_VERIFY((view.page()->scrollBarGeometry(Qt::Horizontal)).height()); -#endif -} - class WebView : public QWebEngineView { Q_OBJECT @@ -3493,35 +2239,6 @@ private: QNetworkRequest::CacheLoadControl m_lastCacheLoad; }; -void tst_QWebEnginePage::setCacheLoadControlAttribute() -{ -#if !defined(QWEBENGINEPAGE_SETNETWORKACCESSMANAGER) - QSKIP("QWEBENGINEPAGE_SETNETWORKACCESSMANAGER"); -#else - QWebEnginePage page; - CacheNetworkAccessManager* manager = new CacheNetworkAccessManager(&page); - page.setNetworkAccessManager(manager); - - QNetworkRequest request(QUrl("http://abcdef.abcdef/")); - - request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::AlwaysCache); - page.load(request); - QCOMPARE(manager->lastCacheLoad(), QNetworkRequest::AlwaysCache); - - request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache); - page.load(request); - QCOMPARE(manager->lastCacheLoad(), QNetworkRequest::PreferCache); - - request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::AlwaysNetwork); - page.load(request); - QCOMPARE(manager->lastCacheLoad(), QNetworkRequest::AlwaysNetwork); - - request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferNetwork); - page.load(request); - QCOMPARE(manager->lastCacheLoad(), QNetworkRequest::PreferNetwork); -#endif -} - void tst_QWebEnginePage::setUrlWithPendingLoads() { QWebEnginePage page; diff --git a/tests/auto/widgets/qwebengineprofile/tst_qwebengineprofile.cpp b/tests/auto/widgets/qwebengineprofile/tst_qwebengineprofile.cpp index 1fe0f0304..bf5d320b7 100644 --- a/tests/auto/widgets/qwebengineprofile/tst_qwebengineprofile.cpp +++ b/tests/auto/widgets/qwebengineprofile/tst_qwebengineprofile.cpp @@ -29,6 +29,7 @@ #include "../util.h" #include <QtCore/qbuffer.h> #include <QtTest/QtTest> +#include <QtWebEngineCore/qwebengineurlrequestinterceptor.h> #include <QtWebEngineCore/qwebengineurlrequestjob.h> #include <QtWebEngineCore/qwebengineurlschemehandler.h> #include <QtWebEngineWidgets/qwebengineprofile.h> @@ -52,6 +53,7 @@ private Q_SLOTS: void urlSchemeHandlerFailRequest(); void urlSchemeHandlerFailOnRead(); void urlSchemeHandlerStreaming(); + void urlSchemeHandlerRequestHeaders(); void customUserAgent(); void httpAcceptLanguage(); void downloadItem(); @@ -444,6 +446,65 @@ void tst_QWebEngineProfile::urlSchemeHandlerStreaming() QCOMPARE(toPlainTextSync(view.page()), QString::fromLatin1(result)); } +class ExtraHeaderInterceptor : public QWebEngineUrlRequestInterceptor +{ +public: + ExtraHeaderInterceptor() { } + + void setExtraHeader(const QByteArray &key, const QByteArray &value) + { + m_extraKey = key; + m_extraValue = value; + } + + void interceptRequest(QWebEngineUrlRequestInfo &info) override + { + if (info.requestUrl().scheme() == QLatin1String("myscheme")) + info.setHttpHeader(m_extraKey, m_extraValue); + } + + QByteArray m_extraKey; + QByteArray m_extraValue; +}; + +class RequestHeadersUrlSchemeHandler : public ReplyingUrlSchemeHandler +{ +public: + void setExpectedHeader(const QByteArray &key, const QByteArray &value) + { + m_expectedKey = key; + m_expectedValue = value; + } + void requestStarted(QWebEngineUrlRequestJob *job) override + { + const auto requestHeaders = job->requestHeaders(); + QVERIFY(requestHeaders.contains(m_expectedKey)); + QCOMPARE(requestHeaders.value(m_expectedKey), m_expectedValue); + ReplyingUrlSchemeHandler::requestStarted(job); + } + QByteArray m_expectedKey; + QByteArray m_expectedValue; +}; + +void tst_QWebEngineProfile::urlSchemeHandlerRequestHeaders() +{ + RequestHeadersUrlSchemeHandler handler; + ExtraHeaderInterceptor interceptor; + + handler.setExpectedHeader("Hello", "World"); + interceptor.setExtraHeader("Hello", "World"); + + QWebEngineProfile profile; + profile.installUrlSchemeHandler("myscheme", &handler); + profile.setRequestInterceptor(&interceptor); + + QWebEnginePage page(&profile); + QSignalSpy loadFinishedSpy(&page, SIGNAL(loadFinished(bool))); + page.load(QUrl(QStringLiteral("myscheme://whatever"))); + QVERIFY(loadFinishedSpy.wait()); +} + + void tst_QWebEngineProfile::customUserAgent() { QString defaultUserAgent = QWebEngineProfile::defaultProfile()->httpUserAgent(); diff --git a/tests/auto/widgets/qwebengineview/BLACKLIST b/tests/auto/widgets/qwebengineview/BLACKLIST index 7c86a72d6..e8758abcc 100644 --- a/tests/auto/widgets/qwebengineview/BLACKLIST +++ b/tests/auto/widgets/qwebengineview/BLACKLIST @@ -6,3 +6,6 @@ osx [textSelectionOutOfInputField] * + +[changeLocale] +* diff --git a/tests/auto/widgets/qwebengineview/resources/image2.png b/tests/auto/widgets/qwebengineview/resources/image2.png Binary files differnew file mode 100644 index 000000000..8d703640c --- /dev/null +++ b/tests/auto/widgets/qwebengineview/resources/image2.png diff --git a/tests/auto/widgets/qwebengineview/tst_qwebengineview.cpp b/tests/auto/widgets/qwebengineview/tst_qwebengineview.cpp index 3287ca309..f2810101b 100644 --- a/tests/auto/widgets/qwebengineview/tst_qwebengineview.cpp +++ b/tests/auto/widgets/qwebengineview/tst_qwebengineview.cpp @@ -46,6 +46,7 @@ #include <QTcpSocket> #include <QStyle> #include <QtWidgets/qaction.h> +#include <QtCore/qregularexpression.h> #define VERIFY_INPUTMETHOD_HINTS(actual, expect) \ QVERIFY(actual == (expect | Qt::ImhNoPredictiveText | Qt::ImhNoTextHandles | Qt::ImhNoEditMenu)); @@ -267,7 +268,7 @@ void tst_QWebEngineView::getWebKitVersion() void tst_QWebEngineView::changePage_data() { QString html = "<html><head><title>%1</title>" - "<link rel='icon' href='file://" TESTS_SOURCE_DIR "/resources/image2.png'></head></html>"; + "<link rel='icon' href='qrc:///resources/image2.png'></head></html>"; QUrl urlFrom("data:text/html," + html.arg("TitleFrom")); QUrl urlTo("data:text/html," + html.arg("TitleTo")); QUrl nullPage("data:text/html,<html/>"); @@ -1180,7 +1181,7 @@ void tst_QWebEngineView::changeLocale() QTRY_COMPARE_WITH_TIMEOUT(loadFinishedSpyDE.count(), 1, 20000); QTRY_VERIFY(!toPlainTextSync(viewDE.page()).isEmpty()); - errorLines = toPlainTextSync(viewDE.page()).split(QRegExp("[\r\n]"), QString::SkipEmptyParts); + errorLines = toPlainTextSync(viewDE.page()).split(QRegularExpression("[\r\n]"), QString::SkipEmptyParts); QCOMPARE(errorLines.first().toUtf8(), QByteArrayLiteral("Die Website ist nicht erreichbar")); QLocale::setDefault(QLocale("en")); @@ -1190,7 +1191,7 @@ void tst_QWebEngineView::changeLocale() QTRY_COMPARE_WITH_TIMEOUT(loadFinishedSpyEN.count(), 1, 20000); QTRY_VERIFY(!toPlainTextSync(viewEN.page()).isEmpty()); - errorLines = toPlainTextSync(viewEN.page()).split(QRegExp("[\r\n]"), QString::SkipEmptyParts); + errorLines = toPlainTextSync(viewEN.page()).split(QRegularExpression("[\r\n]"), QString::SkipEmptyParts); QCOMPARE(errorLines.first().toUtf8(), QByteArrayLiteral("This site can\xE2\x80\x99t be reached")); // Reset error page @@ -1203,7 +1204,7 @@ void tst_QWebEngineView::changeLocale() QTRY_COMPARE_WITH_TIMEOUT(loadFinishedSpyDE.count(), 1, 20000); QTRY_VERIFY(!toPlainTextSync(viewDE.page()).isEmpty()); - errorLines = toPlainTextSync(viewDE.page()).split(QRegExp("[\r\n]"), QString::SkipEmptyParts); + errorLines = toPlainTextSync(viewDE.page()).split(QRegularExpression("[\r\n]"), QString::SkipEmptyParts); QCOMPARE(errorLines.first().toUtf8(), QByteArrayLiteral("Die Website ist nicht erreichbar")); } @@ -2634,7 +2635,7 @@ void tst_QWebEngineView::imeJSInputEvents() view.show(); auto logLines = [&view]() -> QStringList { - return evaluateJavaScriptSync(view.page(), "log.textContent").toString().split("\n").filter(QRegExp(".+")); + return evaluateJavaScriptSync(view.page(), "log.textContent").toString().split("\n").filter(QRegularExpression(".+")); }; QSignalSpy loadFinishedSpy(&view, SIGNAL(loadFinished(bool))); diff --git a/tests/auto/widgets/qwebengineview/tst_qwebengineview.qrc b/tests/auto/widgets/qwebengineview/tst_qwebengineview.qrc index 53b11bca8..a09be0399 100644 --- a/tests/auto/widgets/qwebengineview/tst_qwebengineview.qrc +++ b/tests/auto/widgets/qwebengineview/tst_qwebengineview.qrc @@ -5,5 +5,6 @@ <file>resources/input_types.html</file> <file>resources/scrolltest_page.html</file> <file>resources/keyboardEvents.html</file> + <file>resources/image2.png</file> </qresource> </RCC> |