summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJüri Valdmann <juri.valdmann@qt.io>2017-08-14 09:16:04 +0200
committerJüri Valdmann <juri.valdmann@qt.io>2017-09-12 08:21:25 +0000
commit4fddefdcf2f25c52bd3258ce846233217bf7e465 (patch)
treeb44b121bf9f9d61dab303a8fba9233e6d84f7ba7
parent803034049b25c556c21264fc62bf2c720ba8165c (diff)
Add test for saving a page over HTTP
Change-Id: I6d9261292e44484cded421402fc06ee2eb08bdea Reviewed-by: Michal Klocek <michal.klocek@qt.io>
-rw-r--r--tests/auto/widgets/qwebenginedownloads/tst_qwebenginedownloads.cpp85
1 files changed, 85 insertions, 0 deletions
diff --git a/tests/auto/widgets/qwebenginedownloads/tst_qwebenginedownloads.cpp b/tests/auto/widgets/qwebenginedownloads/tst_qwebenginedownloads.cpp
index b8d73f2e..79962fae 100644
--- a/tests/auto/widgets/qwebenginedownloads/tst_qwebenginedownloads.cpp
+++ b/tests/auto/widgets/qwebenginedownloads/tst_qwebenginedownloads.cpp
@@ -44,6 +44,8 @@ private Q_SLOTS:
void downloadLink_data();
void downloadLink();
void downloadTwoLinks();
+ void downloadPage_data();
+ void downloadPage();
};
enum DownloadTestUserAction {
@@ -506,5 +508,88 @@ void tst_QWebEngineDownloads::downloadTwoLinks()
QVERIFY(item2);
}
+void tst_QWebEngineDownloads::downloadPage_data()
+{
+ QTest::addColumn<QWebEngineDownloadItem::SavePageFormat>("savePageFormat");
+ QTest::newRow("SingleHtmlSaveFormat") << QWebEngineDownloadItem::SingleHtmlSaveFormat;
+ QTest::newRow("CompleteHtmlSaveFormat") << QWebEngineDownloadItem::CompleteHtmlSaveFormat;
+ QTest::newRow("MimeHtmlSaveFormat") << QWebEngineDownloadItem::MimeHtmlSaveFormat;
+}
+
+void tst_QWebEngineDownloads::downloadPage()
+{
+ QFETCH(QWebEngineDownloadItem::SavePageFormat, savePageFormat);
+
+ HttpServer server;
+ QWebEngineProfile profile;
+ QWebEnginePage page(&profile);
+ QWebEngineView view;
+ view.setPage(&page);
+
+ view.load(server.url());
+ view.show();
+ auto indexRR = waitForRequest(&server);
+ QVERIFY(indexRR);
+ QCOMPARE(indexRR->requestMethod(), QByteArrayLiteral("GET"));
+ QCOMPARE(indexRR->requestPath(), QByteArrayLiteral("/"));
+ indexRR->setResponseHeader(QByteArrayLiteral("content-type"), QByteArrayLiteral("text/html"));
+ indexRR->setResponseBody(QByteArrayLiteral("<html><body>Hello</body></html>"));
+ indexRR->sendResponse();
+ bool loadOk = false;
+ QVERIFY(waitForSignal(&page, &QWebEnginePage::loadFinished, [&](bool ok){ loadOk = ok; }));
+ QVERIFY(loadOk);
+
+ auto favIconRR = waitForRequest(&server);
+ QVERIFY(favIconRR);
+ QCOMPARE(favIconRR->requestMethod(), QByteArrayLiteral("GET"));
+ QCOMPARE(favIconRR->requestPath(), QByteArrayLiteral("/favicon.ico"));
+ favIconRR->setResponseStatus(404);
+ favIconRR->sendResponse();
+
+ QTemporaryDir tmpDir;
+ QVERIFY(tmpDir.isValid());
+ QString downloadPath = tmpDir.path() + QStringLiteral("/test.html");
+ page.save(downloadPath, savePageFormat);
+
+ QWebEngineDownloadItem *downloadItem = nullptr;
+ QUrl downloadUrl = server.url("/");
+ QVERIFY(waitForSignal(&profile, &QWebEngineProfile::downloadRequested,
+ [&](QWebEngineDownloadItem *item) {
+ QCOMPARE(item->state(), QWebEngineDownloadItem::DownloadInProgress);
+ QCOMPARE(item->isFinished(), false);
+ QCOMPARE(item->totalBytes(), -1);
+ QCOMPARE(item->receivedBytes(), 0);
+ QCOMPARE(item->interruptReason(), QWebEngineDownloadItem::NoReason);
+ QCOMPARE(item->type(), QWebEngineDownloadItem::SavePage);
+ // FIXME why is mimeType always the same?
+ QCOMPARE(item->mimeType(), QStringLiteral("application/x-mimearchive"));
+ QCOMPARE(item->path(), downloadPath);
+ QCOMPARE(item->savePageFormat(), savePageFormat);
+ QCOMPARE(item->url(), downloadUrl);
+ // no need to call item->accept()
+ downloadItem = item;
+ }));
+ QVERIFY(downloadItem);
+ bool finishOk = false;
+ QVERIFY(waitForSignal(downloadItem, &QWebEngineDownloadItem::finished, [&]() {
+ auto item = downloadItem;
+ 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::SavePage);
+ QCOMPARE(item->mimeType(), QStringLiteral("application/x-mimearchive"));
+ QCOMPARE(item->path(), downloadPath);
+ QCOMPARE(item->savePageFormat(), savePageFormat);
+ QCOMPARE(item->url(), downloadUrl);
+ finishOk = true;
+ }));
+ QVERIFY(finishOk);
+
+ QFile file(downloadPath);
+ QVERIFY(file.exists());
+}
+
QTEST_MAIN(tst_QWebEngineDownloads)
#include "tst_qwebenginedownloads.moc"