summaryrefslogtreecommitdiffstats
path: root/tests/auto/widgets/qwebenginedownloaditem/tst_qwebenginedownloaditem.cpp
diff options
context:
space:
mode:
authorAllan Sandfeld Jensen <allan.jensen@qt.io>2019-05-23 10:36:26 +0200
committerAllan Sandfeld Jensen <allan.jensen@qt.io>2019-05-24 10:42:53 +0200
commitb7a905a4f2f43b856e5f365439fb21b70a03682e (patch)
tree0712f43bbeec8c03e739110ddfa584943bf6fdac /tests/auto/widgets/qwebenginedownloaditem/tst_qwebenginedownloaditem.cpp
parente710d15b7243f2fbc99f98c3ca79d325eb459ac7 (diff)
parente02bcb0855ebee0612cab0f3cd3f9fd494497336 (diff)
Merge remote-tracking branch 'origin/5.12' into 5.13
Conflicts: src/3rdparty src/core/net/network_delegate_qt.cpp tests/auto/widgets/qwebenginedownloaditem/tst_qwebenginedownloaditem.cpp Change-Id: Ib715b3047213583e0441b915d3cabb801d9d4ba8
Diffstat (limited to 'tests/auto/widgets/qwebenginedownloaditem/tst_qwebenginedownloaditem.cpp')
-rw-r--r--tests/auto/widgets/qwebenginedownloaditem/tst_qwebenginedownloaditem.cpp123
1 files changed, 123 insertions, 0 deletions
diff --git a/tests/auto/widgets/qwebenginedownloaditem/tst_qwebenginedownloaditem.cpp b/tests/auto/widgets/qwebenginedownloaditem/tst_qwebenginedownloaditem.cpp
index 3a38c115d..bc474457a 100644
--- a/tests/auto/widgets/qwebenginedownloaditem/tst_qwebenginedownloaditem.cpp
+++ b/tests/auto/widgets/qwebenginedownloaditem/tst_qwebenginedownloaditem.cpp
@@ -77,6 +77,7 @@ private Q_SLOTS:
void downloadToDefaultLocation();
void downloadToNonExistentDir();
void downloadToReadOnlyDir();
+ void downloadPathValidation();
private:
void saveLink(QPoint linkPos);
@@ -1115,5 +1116,127 @@ void tst_QWebEngineDownloadItem::downloadToReadOnlyDir()
QFile(m_profile->downloadPath()).setPermissions(QFileDevice::WriteOwner);
}
+void tst_QWebEngineDownloadItem::downloadPathValidation()
+{
+ const QString fileName = "test.txt";
+ QString downloadPath;
+ QString originalDownloadPath;
+
+ QTemporaryDir tmpDir;
+ QVERIFY(tmpDir.isValid());
+
+ // 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
+ QPointer<QWebEngineDownloadItem> downloadItem;
+ ScopedConnection sc2 = connect(m_profile, &QWebEngineProfile::downloadRequested, [&](QWebEngineDownloadItem *item) {
+ downloadItem = item;
+ originalDownloadPath = item->path();
+
+ item->setPath(downloadPath);
+ // TODO: Do not cancel download from 5.13. This is for not messing up system download path.
+ // Use m_profile->setDownloadPath(tmpDir.path()) at initialization.
+ if (item->path() != downloadPath)
+ item->cancel();
+ else
+ item->accept();
+
+ connect(item, &QWebEngineDownloadItem::stateChanged, [&, item](QWebEngineDownloadItem::DownloadState downloadState) {
+ if (downloadState == QWebEngineDownloadItem::DownloadInterrupted) {
+ item->cancel();
+ }
+ });
+
+ connect(item, &QWebEngineDownloadItem::finished, [&, item]() {
+ QCOMPARE(item->isFinished(), true);
+ QCOMPARE(item->totalBytes(), item->receivedBytes());
+ QVERIFY(item->receivedBytes() > 0);
+ QCOMPARE(item->page(), m_page);
+ });
+ });
+
+ QString oldPath = QDir::currentPath();
+ QDir::setCurrent(tmpDir.path());
+
+ // Set only the file name.
+ downloadItem.clear();
+ originalDownloadPath = "";
+ downloadPath = fileName;
+ m_page->setUrl(m_server->url("/" + fileName));
+ QTRY_VERIFY(downloadItem);
+ QTRY_COMPARE(downloadItem->state(), QWebEngineDownloadItem::DownloadCompleted);
+ QCOMPARE(downloadItem->interruptReason(), QWebEngineDownloadItem::NoReason);
+ QCOMPARE(downloadItem->path(), fileName);
+
+ // Set only the directory path.
+ downloadItem.clear();
+ originalDownloadPath = "";
+ downloadPath = tmpDir.path();
+ m_page->setUrl(m_server->url("/" + fileName));
+ QTRY_VERIFY(downloadItem);
+ QTRY_COMPARE(downloadItem->state(), QWebEngineDownloadItem::DownloadCancelled);
+ QCOMPARE(downloadItem->interruptReason(), QWebEngineDownloadItem::UserCanceled);
+ QCOMPARE(downloadItem->path(), originalDownloadPath);
+
+ // Set only the directory path with separator.
+ downloadItem.clear();
+ originalDownloadPath = "";
+ downloadPath = tmpDir.path() + QDir::separator();
+ m_page->setUrl(m_server->url("/" + fileName));
+ QTRY_VERIFY(downloadItem);
+ QTRY_COMPARE(downloadItem->state(), QWebEngineDownloadItem::DownloadCancelled);
+ QCOMPARE(downloadItem->interruptReason(), QWebEngineDownloadItem::UserCanceled);
+ QCOMPARE(downloadItem->path(), originalDownloadPath);
+
+ // Set only the directory with the current directory path without ending separator.
+ downloadItem.clear();
+ originalDownloadPath = "";
+ downloadPath = ".";
+ m_page->setUrl(m_server->url("/" + fileName));
+ QTRY_VERIFY(downloadItem);
+ QTRY_COMPARE(downloadItem->state(), QWebEngineDownloadItem::DownloadCancelled);
+ QCOMPARE(downloadItem->interruptReason(), QWebEngineDownloadItem::UserCanceled);
+ QCOMPARE(downloadItem->path(), originalDownloadPath);
+
+ // Set only the directory with the current directory path with ending separator.
+ downloadItem.clear();
+ originalDownloadPath = "";
+ downloadPath = "./";
+ m_page->setUrl(m_server->url("/" + fileName));
+ QTRY_VERIFY(downloadItem);
+ QTRY_COMPARE(downloadItem->state(), QWebEngineDownloadItem::DownloadCancelled);
+ QCOMPARE(downloadItem->interruptReason(), QWebEngineDownloadItem::UserCanceled);
+ QCOMPARE(downloadItem->path(), originalDownloadPath);
+
+
+
+ downloadItem.clear();
+ originalDownloadPath = "";
+ downloadPath = "...";
+ m_page->setUrl(m_server->url("/" + fileName));
+ QTRY_VERIFY(downloadItem);
+ QTRY_COMPARE(downloadItem->state(), QWebEngineDownloadItem::DownloadCancelled);
+#if !defined(Q_OS_WIN)
+ QCOMPARE(downloadItem->interruptReason(), QWebEngineDownloadItem::FileFailed);
+ QCOMPARE(downloadItem->path(), downloadPath);
+#else
+ // Windows interprets the "..." path as a valid path. It will be the current path.
+ QCOMPARE(downloadItem->interruptReason(), QWebEngineDownloadItem::UserCanceled);
+ QCOMPARE(downloadItem->path(), originalDownloadPath);
+#endif // !defined(Q_OS_WIN)
+ QDir::setCurrent(oldPath);
+}
+
QTEST_MAIN(tst_QWebEngineDownloadItem)
#include "tst_qwebenginedownloaditem.moc"