summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorTamas Zakor <ztamas@inf.u-szeged.hu>2019-05-28 13:54:28 +0200
committerTamas Zakor <ztamas@inf.u-szeged.hu>2019-07-05 08:57:30 +0200
commit0884fab3b161b62c1e2bb1bac6bbd74560f06232 (patch)
treeec07c0b9473c5eb05880cd1783ca11dd5bc09aa0 /tests
parentfbc2700180d97d9c84b079b7c4da9eace19abf23 (diff)
Add API to change download directory path and file name
Add functions and property to change the download directory and file name in QWebEngineDownloadItem and QQuickWebEngineDownloadItem and deprecate the path() and setPath(). Regenerating the uniquifying download filename after change the download directory. [ChangeLog][DownloadItem] Add functions and property to change the download directory and file name in QWebEngineDownloadItem and QQuickWebEngineDownloadItem and deprecate the path() and setPath(). Task-number: QTBUG-56978 Change-Id: I6e63da82a187add8bc3206cc80c8bf6865fbdd35 Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/quick/publicapi/tst_publicapi.cpp4
-rw-r--r--tests/auto/quick/qmltests/data/tst_download.qml47
-rw-r--r--tests/auto/widgets/loadsignals/tst_loadsignals.cpp3
-rw-r--r--tests/auto/widgets/qwebenginedownloaditem/tst_qwebenginedownloaditem.cpp156
4 files changed, 198 insertions, 12 deletions
diff --git a/tests/auto/quick/publicapi/tst_publicapi.cpp b/tests/auto/quick/publicapi/tst_publicapi.cpp
index 8b8eb8636..f464d58d0 100644
--- a/tests/auto/quick/publicapi/tst_publicapi.cpp
+++ b/tests/auto/quick/publicapi/tst_publicapi.cpp
@@ -262,6 +262,10 @@ static const QStringList expectedAPI = QStringList()
<< "QQuickWebEngineDownloadItem.view --> QQuickWebEngineView*"
<< "QQuickWebEngineDownloadItem.url --> QUrl"
<< "QQuickWebEngineDownloadItem.suggestedFileName --> QString"
+ << "QQuickWebEngineDownloadItem.downloadDirectory --> QString"
+ << "QQuickWebEngineDownloadItem.downloadDirectoryChanged() --> void"
+ << "QQuickWebEngineDownloadItem.downloadFileName --> QString"
+ << "QQuickWebEngineDownloadItem.downloadFileNameChanged() --> void"
<< "QQuickWebEngineFileDialogRequest.FileModeOpen --> FileMode"
<< "QQuickWebEngineFileDialogRequest.FileModeOpenMultiple --> FileMode"
<< "QQuickWebEngineFileDialogRequest.FileModeSave --> FileMode"
diff --git a/tests/auto/quick/qmltests/data/tst_download.qml b/tests/auto/quick/qmltests/data/tst_download.qml
index c8f783b03..ff57c76c7 100644
--- a/tests/auto/quick/qmltests/data/tst_download.qml
+++ b/tests/auto/quick/qmltests/data/tst_download.qml
@@ -44,6 +44,12 @@ TestWebEngineView {
property var downloadInterruptReason: null
property url downloadUrl: ""
property string suggestedFileName: ""
+ property string downloadDirectory: ""
+ property string downloadFileName: ""
+ property string downloadedPath: ""
+ property int downloadDirectoryChanged: 0
+ property int downloadFileNameChanged: 0
+ property int downloadPathChanged: 0
function urlToPath(url) {
var path = url.toString()
@@ -68,23 +74,36 @@ TestWebEngineView {
ignoreUnknownSignals: true
onStateChanged: downloadState.push(target.state)
onInterruptReasonChanged: downloadInterruptReason = target.interruptReason
+ onDownloadDirectoryChanged: downloadDirectoryChanged++
+ onDownloadFileNameChanged: downloadFileNameChanged++
+ onPathChanged: downloadPathChanged++
}
WebEngineProfile {
id: testDownloadProfile
onDownloadRequested: {
+ testDownloadProfile.downloadPath = urlToPath(StandardPaths.writableLocation(StandardPaths.TempLocation))
downloadState.push(download.state)
downloadItemConnections.target = download
if (cancelDownload) {
download.cancel()
} else {
totalBytes = download.totalBytes
- download.path = "testfile.zip"
+ download.downloadDirectory = testDownloadProfile.downloadPath
+ download.downloadFileName = "testfile.zip"
+
+ if (downloadDirectory.length != 0)
+ download.downloadDirectory = testDownloadProfile.downloadPath + downloadDirectory
+
+ if (downloadFileName.length != 0)
+ download.downloadFileName = downloadFileName
+
download.accept()
}
downloadUrl = download.url
suggestedFileName = download.suggestedFileName
+ downloadedPath = download.downloadDirectory + download.downloadFileName
}
onDownloadFinished: {
receivedBytes = download.receivedBytes;
@@ -103,6 +122,9 @@ TestWebEngineView {
downloadItemConnections.target = null
downloadState = []
downloadInterruptReason = null
+ downloadDirectoryChanged = 0
+ downloadFileNameChanged = 0
+ downloadPathChanged = 0
}
function test_downloadRequest() {
@@ -165,5 +187,28 @@ TestWebEngineView {
testDownloadProfile.downloadPath = downloadPath;
compare(testDownloadProfile.downloadPath, downloadPath);
}
+
+ function test_downloadToDirectoryWithFileName() {
+ compare(downLoadRequestedSpy.count, 0);
+ compare(downloadDirectoryChanged, 0);
+ compare(downloadFileNameChanged, 0);
+ downloadDirectory = "/test/";
+ downloadFileName = "test.zip";
+ webEngineView.url = Qt.resolvedUrl("download.zip");
+ downLoadRequestedSpy.wait();
+ compare(downLoadRequestedSpy.count, 1);
+ compare(downloadUrl, webEngineView.url);
+ compare(suggestedFileName, "download.zip");
+ compare(downloadState[0], WebEngineDownloadItem.DownloadRequested);
+ tryCompare(downloadState, "1", WebEngineDownloadItem.DownloadInProgress);
+ compare(downloadedPath, testDownloadProfile.downloadPath + downloadDirectory + downloadFileName);
+ compare(downloadDirectoryChanged, 1);
+ compare(downloadFileNameChanged, 3);
+ compare(downloadPathChanged, 4);
+ downloadFinishedSpy.wait();
+ compare(totalBytes, receivedBytes);
+ tryCompare(downloadState, "2", WebEngineDownloadItem.DownloadCompleted);
+ verify(!downloadInterruptReason);
+ }
}
}
diff --git a/tests/auto/widgets/loadsignals/tst_loadsignals.cpp b/tests/auto/widgets/loadsignals/tst_loadsignals.cpp
index e614f3751..c0bb8d5c5 100644
--- a/tests/auto/widgets/loadsignals/tst_loadsignals.cpp
+++ b/tests/auto/widgets/loadsignals/tst_loadsignals.cpp
@@ -238,7 +238,8 @@ void tst_LoadSignals::fileDownloadDoesNotTriggerLoadSignals_qtbug66661()
connect(item, &QWebEngineDownloadItem::stateChanged, [&downloadState](QWebEngineDownloadItem::DownloadState newState){
downloadState = newState;
});
- item->setPath(tempDir.filePath(QFileInfo(item->path()).fileName()));
+ item->setDownloadDirectory(tempDir.filePath(QFileInfo(item->path()).path()));
+ item->setDownloadFileName(QFileInfo(item->path()).fileName());
item->accept();
});
diff --git a/tests/auto/widgets/qwebenginedownloaditem/tst_qwebenginedownloaditem.cpp b/tests/auto/widgets/qwebenginedownloaditem/tst_qwebenginedownloaditem.cpp
index 8ae2e9aea..6dc7f03c1 100644
--- a/tests/auto/widgets/qwebenginedownloaditem/tst_qwebenginedownloaditem.cpp
+++ b/tests/auto/widgets/qwebenginedownloaditem/tst_qwebenginedownloaditem.cpp
@@ -78,6 +78,7 @@ private Q_SLOTS:
void downloadToNonExistentDir();
void downloadToReadOnlyDir();
void downloadPathValidation();
+ void downloadToDirectoryWithFileName();
private:
void saveLink(QPoint linkPos);
@@ -447,6 +448,8 @@ void tst_QWebEngineDownloadItem::downloadLink()
QByteArray slashFileName = QByteArrayLiteral("/") + fileName;
QString suggestedPath =
QStandardPaths::writableLocation(QStandardPaths::DownloadLocation) + slashFileName;
+ QString downloadDirectory = tmpDir.path();
+ QString downloadFileName = fileName;
QString downloadPath = tmpDir.path() + slashFileName;
QUrl downloadUrl = m_server->url(slashFileName);
int acceptedCount = 0;
@@ -460,7 +463,7 @@ void tst_QWebEngineDownloadItem::downloadLink()
QCOMPARE(item->type(), expectedDownloadType(userAction, fileDisposition));
QCOMPARE(item->isSavePageDownload(), false);
QCOMPARE(item->mimeType(), QString(fileMimeTypeDetected));
- QCOMPARE(item->path(), suggestedPath);
+ QCOMPARE(QDir(item->downloadDirectory()).filePath(item->downloadFileName()), suggestedPath);
QCOMPARE(item->savePageFormat(), QWebEngineDownloadItem::UnknownSaveFormat);
QCOMPARE(item->url(), downloadUrl);
QCOMPARE(item->page(), m_page);
@@ -474,14 +477,15 @@ void tst_QWebEngineDownloadItem::downloadLink()
QCOMPARE(item->type(), expectedDownloadType(userAction, fileDisposition));
QCOMPARE(item->isSavePageDownload(), false);
QCOMPARE(item->mimeType(), QString(fileMimeTypeDetected));
- QCOMPARE(item->path(), downloadPath);
+ QCOMPARE(QDir(item->downloadDirectory()).filePath(item->downloadFileName()), downloadPath);
QCOMPARE(item->savePageFormat(), QWebEngineDownloadItem::UnknownSaveFormat);
QCOMPARE(item->url(), downloadUrl);
QCOMPARE(item->page(), m_page);
finishedCount++;
});
- item->setPath(downloadPath);
+ item->setDownloadDirectory(downloadDirectory);
+ item->setDownloadFileName(downloadFileName);
item->accept();
acceptedCount++;
@@ -575,7 +579,8 @@ void tst_QWebEngineDownloadItem::downloadTwoLinks()
QCOMPARE(item->savePageFormat(), QWebEngineDownloadItem::UnknownSaveFormat);
QCOMPARE(item->mimeType(), QStringLiteral("text/plain"));
QString filePart = QChar('/') + item->url().fileName();
- QCOMPARE(item->path(), standardDir + filePart);
+ QString fileName = item->url().fileName();
+ QCOMPARE(QDir(item->downloadDirectory()).filePath(item->downloadFileName()), standardDir + filePart);
// type() is broken due to race condition in DownloadManagerDelegateQt
if (action1 == ClickLink && action2 == ClickLink) {
@@ -590,7 +595,8 @@ void tst_QWebEngineDownloadItem::downloadTwoLinks()
connect(item, &QWebEngineDownloadItem::finished, [&]() {
finishedCount++;
});
- item->setPath(tmpDir.path() + filePart);
+ item->setDownloadDirectory(tmpDir.path());
+ item->setDownloadFileName(fileName);
item->accept();
acceptedCount++;
@@ -655,7 +661,7 @@ void tst_QWebEngineDownloadItem::downloadPage()
QCOMPARE(item->isSavePageDownload(), true);
// FIXME(juvaldma): why is mimeType always the same?
QCOMPARE(item->mimeType(), QStringLiteral("application/x-mimearchive"));
- QCOMPARE(item->path(), downloadPath);
+ QCOMPARE(QDir(item->downloadDirectory()).filePath(item->downloadFileName()), downloadPath);
QCOMPARE(item->savePageFormat(), savePageFormat);
QCOMPARE(item->url(), downloadUrl);
QCOMPARE(item->page(), m_page);
@@ -670,7 +676,7 @@ void tst_QWebEngineDownloadItem::downloadPage()
QCOMPARE(item->type(), QWebEngineDownloadItem::SavePage);
QCOMPARE(item->isSavePageDownload(), true);
QCOMPARE(item->mimeType(), QStringLiteral("application/x-mimearchive"));
- QCOMPARE(item->path(), downloadPath);
+ QCOMPARE(QDir(item->downloadDirectory()).filePath(item->downloadFileName()), downloadPath);
QCOMPARE(item->savePageFormat(), savePageFormat);
QCOMPARE(item->url(), downloadUrl);
QCOMPARE(item->page(), m_page);
@@ -901,7 +907,7 @@ void tst_QWebEngineDownloadItem::downloadUniqueFilename()
QCOMPARE(item->interruptReason(), QWebEngineDownloadItem::NoReason);
QCOMPARE(item->type(), QWebEngineDownloadItem::Attachment);
QCOMPARE(item->isSavePageDownload(), false);
- downloadedFilePath = item->path();
+ downloadedFilePath = QDir(item->downloadDirectory()).filePath(item->downloadFileName());
downloadFinished = true;
});
});
@@ -959,7 +965,7 @@ void tst_QWebEngineDownloadItem::downloadUniqueFilenameWithTimestamp()
QCOMPARE(item->interruptReason(), QWebEngineDownloadItem::NoReason);
QCOMPARE(item->page(), m_page);
downloadFinished = true;
- downloadedFilePath = item->path();
+ downloadedFilePath = QDir(item->downloadDirectory()).filePath(item->downloadFileName());
});
});
@@ -1053,7 +1059,7 @@ void tst_QWebEngineDownloadItem::downloadToNonExistentDir()
QCOMPARE(item->interruptReason(), QWebEngineDownloadItem::NoReason);
QCOMPARE(item->page(), m_page);
downloadFinished = true;
- downloadedFilePath = item->path();
+ downloadedFilePath = QDir(item->downloadDirectory()).filePath(item->downloadFileName());
});
});
@@ -1246,5 +1252,135 @@ void tst_QWebEngineDownloadItem::downloadPathValidation()
QDir::setCurrent(oldPath);
}
+void tst_QWebEngineDownloadItem::downloadToDirectoryWithFileName()
+{
+ QString downloadDirectory;
+ QString downloadFileName;
+ QString downloadedFilePath;
+ QString downloadedSuggestedFileName;
+ QString fileName = "test.txt";
+ QString uniqueFileName = "test (1).txt";
+
+ 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) {
+
+ if (!downloadDirectory.isEmpty()) {
+ item->setDownloadDirectory(downloadDirectory);
+ QCOMPARE(item->downloadDirectory(), downloadDirectory);
+ }
+
+ if (!downloadFileName.isEmpty()) {
+ item->setDownloadFileName(downloadFileName);
+ QCOMPARE(item->downloadFileName(), downloadFileName);
+ }
+
+ QCOMPARE(item->path(), QDir(item->downloadDirectory()).filePath(item->downloadFileName()));
+ 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 = QDir(item->downloadDirectory()).filePath(item->downloadFileName());
+ downloadedSuggestedFileName = item->suggestedFileName();
+ });
+ });
+
+ // Download file to the default download directory.
+ downloadDirectory = "";
+ downloadFileName = "";
+ m_page->setUrl(m_server->url("/" + fileName));
+ QTRY_VERIFY(downloadFinished);
+ QVERIFY(QFile(downloadedFilePath).exists());
+ QCOMPARE(downloadedFilePath, QDir(m_profile->downloadPath()).filePath(fileName));
+ QCOMPARE(downloadedSuggestedFileName, fileName);
+
+ // Download the same file to another directory
+ downloadFinished = false;
+ downloadDirectory = m_profile->downloadPath() + QDir::separator() + "test1" + QDir::separator();
+ downloadFileName = "";
+ m_page->setUrl(m_server->url("/" + fileName));
+ QTRY_VERIFY(downloadFinished);
+ QVERIFY(QFile(downloadedFilePath).exists());
+ QCOMPARE(downloadedFilePath, QDir(downloadDirectory).filePath(fileName));
+ QCOMPARE(downloadedSuggestedFileName, fileName);
+
+ // Download the same file to the same directory and the file name must be unique.
+ downloadFinished = false;
+ downloadDirectory = m_profile->downloadPath() + QDir::separator() + "test1" + QDir::separator();
+ downloadFileName = "";
+ m_page->setUrl(m_server->url("/" + fileName));
+ QTRY_VERIFY(downloadFinished);
+ QVERIFY(QFile(downloadedFilePath).exists());
+ QCOMPARE(downloadedFilePath, QDir(downloadDirectory).filePath(uniqueFileName));
+ QCOMPARE(downloadedSuggestedFileName, fileName);
+
+ // Download another file to the same directory and set file name by
+ // QWebEngineDownloadItem::setDownloadDirectory() and setDownloadFileName() to avoid uniquification.
+ downloadFinished = false;
+ downloadDirectory = m_profile->downloadPath() + QDir::separator() + "test1" + QDir::separator();
+ downloadFileName = "test1.txt";
+ m_page->setUrl(m_server->url("/" + fileName));
+ QTRY_VERIFY(downloadFinished);
+ QVERIFY(QFile(downloadedFilePath).exists());
+ QCOMPARE(downloadedFilePath, QDir(downloadDirectory).filePath(downloadFileName));
+ QCOMPARE(downloadedSuggestedFileName, fileName);
+
+ // Download the same file to another directory without uniquifying the file name
+ downloadFinished = false;
+ downloadDirectory = m_profile->downloadPath() + QDir::separator() + "test2" + QDir::separator();
+ downloadFileName = "test1.txt";
+ m_page->setUrl(m_server->url("/" + fileName));
+ QTRY_VERIFY(downloadFinished);
+ QVERIFY(QFile(downloadedFilePath).exists());
+ QCOMPARE(downloadedFilePath, QDir(downloadDirectory).filePath(downloadFileName));
+ QCOMPARE(downloadedSuggestedFileName, fileName);
+
+ // Download the same file to same directory and set file name by
+ // QWebEngineDownloadItem::setDownloadDirectory() and setDownloadFileName() to avoid uniquification.
+ downloadFinished = false;
+ downloadDirectory = m_profile->downloadPath() + QDir::separator() + "test2" + QDir::separator();
+ downloadFileName = "test1.txt";
+ m_page->setUrl(m_server->url("/" + fileName));
+ QTRY_VERIFY(downloadFinished);
+ QVERIFY(QFile(downloadedFilePath).exists());
+ QCOMPARE(downloadedFilePath, QDir(downloadDirectory).filePath(downloadFileName));
+ QCOMPARE(downloadedSuggestedFileName, fileName);
+
+ // Download the same file in the same directory.
+ // Use the suggested file name (test.txt) and the file name will not be unique because this file name don't yet exists.
+ downloadFinished = false;
+ downloadDirectory = m_profile->downloadPath() + QDir::separator() + "test2" + QDir::separator();
+ downloadFileName = "";
+ m_page->setUrl(m_server->url("/" + fileName));
+ QTRY_VERIFY(downloadFinished);
+ QVERIFY(QFile(downloadedFilePath).exists());
+ QCOMPARE(downloadedFilePath, QDir(downloadDirectory).filePath(fileName));
+ QCOMPARE(downloadedSuggestedFileName, fileName);
+}
+
QTEST_MAIN(tst_QWebEngineDownloadItem)
#include "tst_qwebenginedownloaditem.moc"