diff options
Diffstat (limited to 'tests/auto/quick/qquickwebengineview/tst_qquickwebengineview.cpp')
-rw-r--r-- | tests/auto/quick/qquickwebengineview/tst_qquickwebengineview.cpp | 269 |
1 files changed, 198 insertions, 71 deletions
diff --git a/tests/auto/quick/qquickwebengineview/tst_qquickwebengineview.cpp b/tests/auto/quick/qquickwebengineview/tst_qquickwebengineview.cpp index ac3397417..dbfa1cb33 100644 --- a/tests/auto/quick/qquickwebengineview/tst_qquickwebengineview.cpp +++ b/tests/auto/quick/qquickwebengineview/tst_qquickwebengineview.cpp @@ -1,33 +1,11 @@ -/**************************************************************************** -** -** Copyright (C) 2016 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$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 + +#undef QT_NO_FOREACH // this file contains unported legacy Q_FOREACH uses #include "testwindow.h" #include "quickutil.h" +#include "util.h" #include <QScopedPointer> #include <QtCore/qelapsedtimer.h> @@ -38,10 +16,11 @@ #include <QtQml/QQmlEngine> #include <QtTest/QtTest> #include <QtWebEngineQuick/QQuickWebEngineProfile> -#include <QtWebEngineQuick/QQuickWebEngineScriptCollection> #include <QtGui/private/qinputmethod_p.h> -#include <QtWebEngineQuick/private/qquickwebengineview_p.h> +#include <QtWebEngineQuick/private/qquickwebenginescriptcollection_p.h> #include <QtWebEngineQuick/private/qquickwebenginesettings_p.h> +#include <QtWebEngineQuick/private/qquickwebenginedownloadrequest_p.h> +#include <QtWebEngineQuick/private/qquickwebengineview_p.h> #include <QtWebEngineCore/private/qtwebenginecore-config_p.h> #include <qpa/qplatforminputcontext.h> #include <QtTest/private/qemulationdetector_p.h> @@ -94,8 +73,13 @@ private Q_SLOTS: void javascriptClipboard_data(); void javascriptClipboard(); void setProfile(); - void focusChild(); +#if QT_CONFIG(accessibility) void focusChild_data(); + void focusChild(); +#endif + void htmlSelectPopup(); + void savePage_data(); + void savePage(); private: inline QQuickWebEngineView *newWebEngineView(); @@ -105,6 +89,11 @@ private: QString m_testSourceDirPath; QScopedPointer<TestWindow> m_window; QScopedPointer<QQmlComponent> m_component; + + QPointingDevice *touchDevice() { + static auto d = QScopedPointer<QPointingDevice>(QTest::createTouchDevice()); + return d.get(); + } }; tst_QQuickWebEngineView::tst_QQuickWebEngineView() @@ -118,8 +107,8 @@ tst_QQuickWebEngineView::tst_QQuickWebEngineView() static QQmlEngine *engine = new QQmlEngine(this); m_component.reset(new QQmlComponent(engine, this)); - m_component->setData(QByteArrayLiteral("import QtQuick 2.0\n" - "import QtWebEngine 1.2\n" + m_component->setData(QByteArrayLiteral("import QtQuick\n" + "import QtWebEngine\n" "WebEngineView {}") , QUrl()); } @@ -442,10 +431,10 @@ void tst_QQuickWebEngineView::transparentWebEngineViews() for (int i = 0; i < image.width(); i++) for (int j = 0; j < image.height(); j++) colors.insert(image.pixel(i, j)); - return colors.count() > 1; + return colors.size() > 1; }); - QVERIFY(colors.count() > 1); + QVERIFY(colors.size() > 1); QVERIFY(colors.contains(qRgb(0, 0, 0))); // black QVERIFY(colors.contains(qRgb(255, 0, 0))); // red for (auto color : colors) { @@ -457,7 +446,6 @@ void tst_QQuickWebEngineView::transparentWebEngineViews() void tst_QQuickWebEngineView::inputMethod() { m_window->show(); - QTRY_VERIFY(qApp->focusObject()); QQuickItem *input; QQuickWebEngineView *view = webEngineView(); @@ -465,18 +453,21 @@ void tst_QQuickWebEngineView::inputMethod() view->setUrl(urlFromTestPath("html/inputmethod.html")); QVERIFY(waitForLoadSucceeded(view)); + QTRY_VERIFY(qobject_cast<QQuickItem *>(qApp->focusObject())); input = qobject_cast<QQuickItem *>(qApp->focusObject()); QVERIFY(!input->flags().testFlag(QQuickItem::ItemAcceptsInputMethod)); QVERIFY(!view->flags().testFlag(QQuickItem::ItemAcceptsInputMethod)); runJavaScript("document.getElementById('inputField').focus();"); QTRY_COMPARE(activeElementId(view), QStringLiteral("inputField")); + QTRY_VERIFY(qobject_cast<QQuickItem *>(qApp->focusObject())); input = qobject_cast<QQuickItem *>(qApp->focusObject()); QTRY_VERIFY(input->flags().testFlag(QQuickItem::ItemAcceptsInputMethod)); QVERIFY(view->flags().testFlag(QQuickItem::ItemAcceptsInputMethod)); runJavaScript("document.getElementById('inputField').blur();"); QTRY_VERIFY(activeElementId(view).isEmpty()); + QTRY_VERIFY(qobject_cast<QQuickItem *>(qApp->focusObject())); input = qobject_cast<QQuickItem *>(qApp->focusObject()); QTRY_VERIFY(!input->flags().testFlag(QQuickItem::ItemAcceptsInputMethod)); QVERIFY(!view->flags().testFlag(QQuickItem::ItemAcceptsInputMethod)); @@ -587,9 +578,8 @@ void tst_QQuickWebEngineView::interruptImeTextComposition() QTest::mouseClick(view->window(), Qt::LeftButton, {}, textInputCenter); } else if (eventType == "Touch") { QPoint textInputCenter = elementCenter(view, QStringLiteral("input2")); - QPointingDevice *touchDevice = QTest::createTouchDevice(); - QTest::touchEvent(view->window(), touchDevice).press(0, textInputCenter, view->window()); - QTest::touchEvent(view->window(), touchDevice).release(0, textInputCenter, view->window()); + QTest::touchEvent(view->window(), touchDevice()).press(0, textInputCenter, view->window()); + QTest::touchEvent(view->window(), touchDevice()).release(0, textInputCenter, view->window()); } QTRY_COMPARE(evaluateJavaScriptSync(view, "document.activeElement.id").toString(), QStringLiteral("input2")); #ifndef Q_OS_WIN @@ -617,12 +607,12 @@ void tst_QQuickWebEngineView::inputContextQueryInput() " <input type='text' id='input1' />" "</body></html>"); QVERIFY(waitForLoadSucceeded(view)); - QCOMPARE(testContext.infos.count(), 0); + QCOMPARE(testContext.infos.size(), 0); // Set focus on an input field. QPoint textInputCenter = elementCenter(view, "input1"); QTest::mouseClick(view->window(), Qt::LeftButton, {}, textInputCenter); - QTRY_COMPARE(testContext.infos.count(), 2); + QTRY_COMPARE(testContext.infos.size(), 2); QCOMPARE(evaluateJavaScriptSync(view, "document.activeElement.id").toString(), QStringLiteral("input1")); foreach (const InputMethodInfo &info, testContext.infos) { QCOMPARE(info.cursorPosition, 0); @@ -634,7 +624,7 @@ void tst_QQuickWebEngineView::inputContextQueryInput() // Change content of an input field from JavaScript. evaluateJavaScriptSync(view, "document.getElementById('input1').value='QtWebEngine';"); - QTRY_COMPARE(testContext.infos.count(), 1); + QTRY_COMPARE(testContext.infos.size(), 1); QCOMPARE(testContext.infos[0].cursorPosition, 11); QCOMPARE(testContext.infos[0].anchorPosition, 11); QCOMPARE(testContext.infos[0].surroundingText, QStringLiteral("QtWebEngine")); @@ -643,7 +633,7 @@ void tst_QQuickWebEngineView::inputContextQueryInput() // Change content of an input field by key press. QTest::keyClick(view->window(), Qt::Key_Exclam); - QTRY_COMPARE(testContext.infos.count(), 1); + QTRY_COMPARE(testContext.infos.size(), 1); QCOMPARE(testContext.infos[0].cursorPosition, 12); QCOMPARE(testContext.infos[0].anchorPosition, 12); QCOMPARE(testContext.infos[0].surroundingText, QStringLiteral("QtWebEngine!")); @@ -652,7 +642,7 @@ void tst_QQuickWebEngineView::inputContextQueryInput() // Change cursor position. QTest::keyClick(view->window(), Qt::Key_Left); - QTRY_COMPARE(testContext.infos.count(), 1); + QTRY_COMPARE(testContext.infos.size(), 1); QCOMPARE(testContext.infos[0].cursorPosition, 11); QCOMPARE(testContext.infos[0].anchorPosition, 11); QCOMPARE(testContext.infos[0].surroundingText, QStringLiteral("QtWebEngine!")); @@ -667,7 +657,7 @@ void tst_QQuickWebEngineView::inputContextQueryInput() QInputMethodEvent event("", attributes); QGuiApplication::sendEvent(qApp->focusObject(), &event); } - QTRY_COMPARE(testContext.infos.count(), 2); + QTRY_COMPARE(testContext.infos.size(), 2); // As a first step, Chromium moves the cursor to the start of the selection. // We don't filter this in QtWebEngine because we don't know yet if this is part of a selection. @@ -691,7 +681,7 @@ void tst_QQuickWebEngineView::inputContextQueryInput() QInputMethodEvent event("", attributes); QGuiApplication::sendEvent(qApp->focusObject(), &event); } - QTRY_COMPARE(testContext.infos.count(), 1); + QTRY_COMPARE(testContext.infos.size(), 1); QCOMPARE(testContext.infos[0].cursorPosition, 0); QCOMPARE(testContext.infos[0].anchorPosition, 0); QCOMPARE(testContext.infos[0].surroundingText, QStringLiteral("QtWebEngine!")); @@ -704,9 +694,9 @@ void tst_QQuickWebEngineView::inputContextQueryInput() QInputMethodEvent event("123", attributes); QGuiApplication::sendEvent(qApp->focusObject(), &event); } - QTRY_COMPARE(testContext.infos.count(), 1); - QCOMPARE(testContext.infos[0].cursorPosition, 3); - QCOMPARE(testContext.infos[0].anchorPosition, 3); + QTRY_COMPARE(testContext.infos.size(), 1); + QCOMPARE(testContext.infos[0].cursorPosition, 0); + QCOMPARE(testContext.infos[0].anchorPosition, 0); QCOMPARE(testContext.infos[0].surroundingText, QStringLiteral("QtWebEngine!")); QCOMPARE(testContext.infos[0].selectedText, QStringLiteral("")); QCOMPARE(evaluateJavaScriptSync(view, "document.getElementById('input1').value").toString(), QStringLiteral("123QtWebEngine!")); @@ -718,7 +708,7 @@ void tst_QQuickWebEngineView::inputContextQueryInput() QInputMethodEvent event("", attributes); QGuiApplication::sendEvent(qApp->focusObject(), &event); } - QTRY_COMPARE(testContext.infos.count(), 2); + QTRY_COMPARE(testContext.infos.size(), 2); foreach (const InputMethodInfo &info, testContext.infos) { QCOMPARE(info.cursorPosition, 0); QCOMPARE(info.anchorPosition, 0); @@ -735,7 +725,7 @@ void tst_QQuickWebEngineView::inputContextQueryInput() event.setCommitString(QStringLiteral("123"), 0, 0); QGuiApplication::sendEvent(qApp->focusObject(), &event); } - QTRY_COMPARE(testContext.infos.count(), 1); + QTRY_COMPARE(testContext.infos.size(), 1); QCOMPARE(testContext.infos[0].cursorPosition, 3); QCOMPARE(testContext.infos[0].anchorPosition, 3); QCOMPARE(testContext.infos[0].surroundingText, QStringLiteral("123QtWebEngine!")); @@ -745,7 +735,7 @@ void tst_QQuickWebEngineView::inputContextQueryInput() // Focus out. QTest::keyPress(view->window(), Qt::Key_Tab); - QTRY_COMPARE(testContext.infos.count(), 1); + QTRY_COMPARE(testContext.infos.size(), 1); QTRY_COMPARE(evaluateJavaScriptSync(view, "document.activeElement.id").toString(), QStringLiteral("")); testContext.infos.clear(); } @@ -802,20 +792,44 @@ void tst_QQuickWebEngineView::inputMethodHints() void tst_QQuickWebEngineView::setZoomFactor() { QQuickWebEngineView *view = webEngineView(); + m_window->show(); + view->setSize(QSizeF(320, 240)); - QVERIFY(qFuzzyCompare(view->zoomFactor(), 1.0)); + QCOMPARE(view->zoomFactor(), 1.0); view->setZoomFactor(2.5); - QVERIFY(qFuzzyCompare(view->zoomFactor(), 2.5)); + QCOMPARE(view->zoomFactor(), 2.5); - view->setUrl(urlFromTestPath("html/basic_page.html")); + const QUrl url1 = urlFromTestPath("html/basic_page.html"), url2 = urlFromTestPath("html/basic_page2.html"); + + view->setUrl(url1); QVERIFY(waitForLoadSucceeded(view)); - QVERIFY(qFuzzyCompare(view->zoomFactor(), 2.5)); + QCOMPARE(view->zoomFactor(), 2.5); view->setZoomFactor(0.1); - QVERIFY(qFuzzyCompare(view->zoomFactor(), 2.5)); + QCOMPARE(view->zoomFactor(), 2.5); view->setZoomFactor(5.5); - QVERIFY(qFuzzyCompare(view->zoomFactor(), 2.5)); + QCOMPARE(view->zoomFactor(), 2.5); + + QScopedPointer<QQuickWebEngineView> view2(newWebEngineView()); + view2->setSize(QSizeF(320, 240)); + view2->setParentItem(m_window->contentItem()); + + // try loading different url and check new values after load + for (auto &&p : { + qMakePair(view, 2.5), // navigating away to different url should keep zoom + qMakePair(view2.get(), 1.0), // same url navigation in diffent page shouldn't be affected + }) { + auto &&view = p.first; auto zoomFactor = p.second; + view->setUrl(url2); + QVERIFY(waitForLoadSucceeded(view)); + QCOMPARE(view->zoomFactor(), zoomFactor); + } + + // should have no influence on first page + view2->setZoomFactor(3.5); + for (auto &&p : { qMakePair(view, 2.5), qMakePair(view2.get(), 3.5), }) + QCOMPARE(p.first->zoomFactor(), p.second); } void tst_QQuickWebEngineView::printToPdf() @@ -832,7 +846,7 @@ void tst_QQuickWebEngineView::printToPdf() QSignalSpy savePdfSpy(view, SIGNAL(pdfPrintingFinished(const QString&, bool))); QString path = tempDir.path() + "/print_success.pdf"; view->printToPdf(path, QQuickWebEngineView::A4, QQuickWebEngineView::Portrait); - QTRY_VERIFY2(savePdfSpy.count() == 1, "Printing to PDF file failed without signal"); + QTRY_VERIFY2(savePdfSpy.size() == 1, "Printing to PDF file failed without signal"); QList<QVariant> successArguments = savePdfSpy.takeFirst(); QVERIFY2(successArguments.at(0).toString() == path, "File path for first saved PDF does not match arguments"); QVERIFY2(successArguments.at(1).toBool() == true, "Printing to PDF file failed though it should succeed"); @@ -843,7 +857,7 @@ void tst_QQuickWebEngineView::printToPdf() path = tempDir.path() + "/print_|fail.pdf"; #endif // #if !defined(Q_OS_WIN) view->printToPdf(path, QQuickWebEngineView::A4, QQuickWebEngineView::Portrait); - QTRY_VERIFY2(savePdfSpy.count() == 1, "Printing to PDF file failed without signal"); + QTRY_VERIFY2(savePdfSpy.size() == 1, "Printing to PDF file failed without signal"); QList<QVariant> failedArguments = savePdfSpy.takeFirst(); QVERIFY2(failedArguments.at(0).toString() == path, "File path for second saved PDF does not match arguments"); QVERIFY2(failedArguments.at(1).toBool() == false, "Printing to PDF file succeeded though it should fail"); @@ -987,11 +1001,9 @@ void tst_QQuickWebEngineView::inputEventForwardingDisabledWhenActiveFocusOnPress QTest::mousePress(view->window(), Qt::LeftButton); QTest::mouseRelease(view->window(), Qt::LeftButton); - QPointingDevice *device = QTest::createTouchDevice(); - - QTest::touchEvent(view->window(), device).press(0, QPoint(0,0), view->window()); - QTest::touchEvent(view->window(), device).move(0, QPoint(1, 1), view->window()); - QTest::touchEvent(view->window(), device).release(0, QPoint(1, 1), view->window()); + QTest::touchEvent(view->window(), touchDevice()).press(0, QPoint(0,0), view->window()); + QTest::touchEvent(view->window(), touchDevice()).move(0, QPoint(1, 1), view->window()); + QTest::touchEvent(view->window(), touchDevice()).release(0, QPoint(1, 1), view->window()); // We expect to catch 7 events - click = 2, press + release = 2, touches = 3. QCOMPARE(item.eventCount(), 7); @@ -1113,7 +1125,7 @@ void tst_QQuickWebEngineView::javascriptClipboard() // - return value of queryCommandEnabled and // - return value of execCommand // - comparing the clipboard / input field - QGuiApplication::clipboard()->clear(); + QGuiApplication::clipboard()->setText(QString()); QCOMPARE(evaluateJavaScriptSync(view, "document.queryCommandEnabled('copy')").toBool(), copyResult); QCOMPARE(evaluateJavaScriptSync(view, "document.execCommand('copy')").toBool(), copyResult); @@ -1140,9 +1152,9 @@ void tst_QQuickWebEngineView::javascriptClipboard() "if (result.state == 'prompt') accessPrompt = true;" "})")); - QTRY_COMPARE(evaluateJavaScriptSync(view, "accessGranted").toBool(), copyResult); - QTRY_COMPARE(evaluateJavaScriptSync(view, "accessDenied").toBool(), !javascriptCanAccessClipboard); - QTRY_COMPARE(evaluateJavaScriptSync(view, "accessPrompt").toBool(), false); + QTRY_COMPARE(evaluateJavaScriptSync(view, "accessGranted").toBool(), javascriptCanAccessClipboard && javascriptCanPaste); + QTRY_COMPARE(evaluateJavaScriptSync(view, "accessDenied").toBool(), false); + QTRY_COMPARE(evaluateJavaScriptSync(view, "accessPrompt").toBool(), !javascriptCanAccessClipboard || !javascriptCanPaste); evaluateJavaScriptSync(view, QStringLiteral( @@ -1156,9 +1168,9 @@ void tst_QQuickWebEngineView::javascriptClipboard() "if (result.state == 'prompt') accessPrompt = true;" "})")); - QTRY_COMPARE(evaluateJavaScriptSync(view, "accessGranted").toBool(), pasteResult); - QTRY_COMPARE(evaluateJavaScriptSync(view, "accessDenied").toBool(), !javascriptCanAccessClipboard || !javascriptCanPaste); - QTRY_COMPARE(evaluateJavaScriptSync(view, "accessPrompt").toBool(), false); + QTRY_COMPARE(evaluateJavaScriptSync(view, "accessGranted").toBool(), javascriptCanAccessClipboard && javascriptCanPaste); + QTRY_COMPARE(evaluateJavaScriptSync(view, "accessDenied").toBool(), false); + QTRY_COMPARE(evaluateJavaScriptSync(view, "accessPrompt").toBool(), !javascriptCanAccessClipboard || !javascriptCanPaste); } void tst_QQuickWebEngineView::setProfile() { @@ -1170,10 +1182,13 @@ void tst_QQuickWebEngineView::setProfile() { QVERIFY(waitForLoadSucceeded(webEngineView())); QCOMPARE(loadSpy.size(), 4); QQuickWebEngineProfile *profile = new QQuickWebEngineProfile(); + auto oldProfile = webEngineView()->profile(); + auto sc = qScopeGuard([&] () { webEngineView()->setProfile(oldProfile); delete profile; }); webEngineView()->setProfile(profile); QTRY_COMPARE(webEngineView()->url() ,urlFromTestPath("html/basic_page2.html")); } +#if QT_CONFIG(accessibility) void tst_QQuickWebEngineView::focusChild_data() { QTest::addColumn<QString>("interfaceName"); @@ -1236,9 +1251,121 @@ void tst_QQuickWebEngineView::focusChild() // <html> -> <body> -> <input> QCOMPARE(traverseToWebDocumentAccessibleInterface(iface)->child(0)->child(0), iface->focusChild()); } +#endif // QT_CONFIG(accessibility) +void tst_QQuickWebEngineView::htmlSelectPopup() +{ + m_window->show(); + QQuickWebEngineView &view = *webEngineView(); + view.settings()->setFocusOnNavigationEnabled(true); + view.setSize(QSizeF(640, 480)); + view.loadHtml("<html><body>" + "<select id='select' onchange='console.log(\"option changed to: \" + this.value)'>" + "<option value='O1'>O1</option><option value='O2'>O2</option><option value='O3'>O3</option></select>" + "</body></html>"); + QVERIFY(waitForLoadSucceeded(&view)); + + auto makeTouch = [this] (QWindow *w, const QPoint &p) { + QTest::touchEvent(w, touchDevice()).press(1, p); + QTest::touchEvent(w, touchDevice()).release(1, p); + }; + + makeTouch(view.window(), elementCenter(&view, "select")); + QPointer<QQuickWindow> popup; + QTRY_VERIFY((popup = m_window->findChild<QQuickWindow *>())); + QCOMPARE(activeElementId(&view), QStringLiteral("select")); + + makeTouch(popup, QPoint(popup->width() / 2, popup->height() / 2)); + QTRY_VERIFY(!popup); + QCOMPARE(evaluateJavaScriptSync(&view, "document.getElementById('select').value").toString(), QStringLiteral("O2")); +} + +void tst_QQuickWebEngineView::savePage_data() +{ + QTest::addColumn<QWebEngineDownloadRequest::SavePageFormat>("savePageFormat"); + + QTest::newRow("SingleHtmlSaveFormat") << QWebEngineDownloadRequest::SingleHtmlSaveFormat; + QTest::newRow("CompleteHtmlSaveFormat") << QWebEngineDownloadRequest::CompleteHtmlSaveFormat; + QTest::newRow("MimeHtmlSaveFormat") << QWebEngineDownloadRequest::MimeHtmlSaveFormat; +} + +void tst_QQuickWebEngineView::savePage() +{ + QFETCH(QWebEngineDownloadRequest::SavePageFormat, savePageFormat); + + QTemporaryDir tempDir(QDir::tempPath() + "/tst_QQuickWebEngineView-XXXXXX"); + QVERIFY(tempDir.isValid()); + const QString filePath = tempDir.path() + "/saved_page.html"; + + QQuickWebEngineView *view = webEngineView(); + int acceptedCount = 0; + int finishedCount = 0; + ScopedConnection sc1 = connect( + view->profile(), &QQuickWebEngineProfile::downloadRequested, + [&](QQuickWebEngineDownloadRequest *downloadRequest) { + QCOMPARE(downloadRequest->state(), + QQuickWebEngineDownloadRequest::DownloadInProgress); + QCOMPARE(downloadRequest->isSavePageDownload(), true); + QCOMPARE(downloadRequest->savePageFormat(), savePageFormat); + QCOMPARE(QDir(downloadRequest->downloadDirectory()) + .filePath(downloadRequest->downloadFileName()), + filePath); + QCOMPARE(downloadRequest->url(), view->url()); + + connect(downloadRequest, &QQuickWebEngineDownloadRequest::isFinishedChanged, + [&, downloadRequest]() { + QCOMPARE(downloadRequest->state(), + QQuickWebEngineDownloadRequest::DownloadCompleted); + QCOMPARE(downloadRequest->isSavePageDownload(), true); + QCOMPARE(downloadRequest->isFinished(), true); + QCOMPARE(downloadRequest->savePageFormat(), savePageFormat); + QCOMPARE(downloadRequest->totalBytes(), + downloadRequest->receivedBytes()); + QVERIFY(downloadRequest->receivedBytes() > 0); + QCOMPARE(QDir(downloadRequest->downloadDirectory()) + .filePath(downloadRequest->downloadFileName()), + filePath); + QCOMPARE(downloadRequest->url(), view->url()); + finishedCount++; + }); + acceptedCount++; + }); + + const QString originalData = QStringLiteral("Basic page"); + view->setUrl(urlFromTestPath("html/basic_page.html")); + QVERIFY(waitForLoadSucceeded(view)); + QCOMPARE(evaluateJavaScriptSync(view, "document.getElementsByTagName('h1')[0].innerText") + .toString(), + originalData); + + // Save the loaded page as HTML. + view->save(filePath, savePageFormat); + QTRY_COMPARE(acceptedCount, 1); + QTRY_COMPARE(finishedCount, 1); + QFile file(filePath); + QVERIFY(file.exists()); + + // Load something else. + view->setUrl(urlFromTestPath("html/basic_page2.html")); + QVERIFY(waitForLoadSucceeded(view)); + QVERIFY(evaluateJavaScriptSync(view, "document.getElementsByTagName('h1')[0].innerText") + .toString() + != originalData); + + // Load the saved page and compare the contents. + view->setUrl(QUrl::fromLocalFile(filePath)); + QVERIFY(waitForLoadSucceeded(view)); + QCOMPARE(evaluateJavaScriptSync(view, "document.getElementsByTagName('h1')[0].innerText") + .toString(), + originalData); +} + +#if QT_CONFIG(accessibility) static QByteArrayList params = QByteArrayList() << "--force-renderer-accessibility"; +#else +static QByteArrayList params; +#endif W_QTEST_MAIN(tst_QQuickWebEngineView, params) #include "tst_qquickwebengineview.moc" |