summaryrefslogtreecommitdiffstats
path: root/toolkit/components/jsdownloads/test/browser
diff options
context:
space:
mode:
Diffstat (limited to 'toolkit/components/jsdownloads/test/browser')
-rw-r--r--toolkit/components/jsdownloads/test/browser/.eslintrc.js7
-rw-r--r--toolkit/components/jsdownloads/test/browser/browser.ini7
-rw-r--r--toolkit/components/jsdownloads/test/browser/browser_DownloadPDFSaver.js97
-rw-r--r--toolkit/components/jsdownloads/test/browser/head.js87
-rw-r--r--toolkit/components/jsdownloads/test/browser/testFile.html9
5 files changed, 207 insertions, 0 deletions
diff --git a/toolkit/components/jsdownloads/test/browser/.eslintrc.js b/toolkit/components/jsdownloads/test/browser/.eslintrc.js
new file mode 100644
index 000000000..7c8021192
--- /dev/null
+++ b/toolkit/components/jsdownloads/test/browser/.eslintrc.js
@@ -0,0 +1,7 @@
+"use strict";
+
+module.exports = {
+ "extends": [
+ "../../../../../testing/mochitest/browser.eslintrc.js"
+ ]
+};
diff --git a/toolkit/components/jsdownloads/test/browser/browser.ini b/toolkit/components/jsdownloads/test/browser/browser.ini
new file mode 100644
index 000000000..131fc4ec8
--- /dev/null
+++ b/toolkit/components/jsdownloads/test/browser/browser.ini
@@ -0,0 +1,7 @@
+[DEFAULT]
+support-files =
+ head.js
+ testFile.html
+
+[browser_DownloadPDFSaver.js]
+skip-if = os != "win"
diff --git a/toolkit/components/jsdownloads/test/browser/browser_DownloadPDFSaver.js b/toolkit/components/jsdownloads/test/browser/browser_DownloadPDFSaver.js
new file mode 100644
index 000000000..80ed9665a
--- /dev/null
+++ b/toolkit/components/jsdownloads/test/browser/browser_DownloadPDFSaver.js
@@ -0,0 +1,97 @@
+/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set ts=2 et sw=2 tw=80: */
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+/**
+ * Tests the PDF download saver, and tests using a window as a
+ * source for the copy download saver.
+ */
+
+"use strict";
+
+/**
+ * Helper function to make sure a window reference exists on the download source.
+ */
+function* test_download_windowRef(aTab, aDownload) {
+ ok(aDownload.source.windowRef, "Download source had a window reference");
+ ok(aDownload.source.windowRef instanceof Ci.xpcIJSWeakReference, "Download window reference is a weak ref");
+ is(aDownload.source.windowRef.get(), aTab.linkedBrowser.contentWindow, "Download window exists during test");
+}
+
+/**
+ * Helper function to check the state of a completed download.
+ */
+function* test_download_state_complete(aTab, aDownload, aPrivate, aCanceled) {
+ ok(aDownload.source, "Download has a source");
+ is(aDownload.source.url, aTab.linkedBrowser.contentWindow.location, "Download source has correct url");
+ is(aDownload.source.isPrivate, aPrivate, "Download source has correct private state");
+ ok(aDownload.stopped, "Download is stopped");
+ is(aCanceled, aDownload.canceled, "Download has correct canceled state");
+ is(!aCanceled, aDownload.succeeded, "Download has correct succeeded state");
+ is(aDownload.error, null, "Download error is not defined");
+}
+
+function* test_createDownload_common(aPrivate, aType) {
+ let win = yield BrowserTestUtils.openNewBrowserWindow({ private : aPrivate});
+
+ let tab = yield BrowserTestUtils.openNewForegroundTab(win.gBrowser, getRootDirectory(gTestPath) + "testFile.html");
+ let download = yield Downloads.createDownload({
+ source: tab.linkedBrowser.contentWindow,
+ target: { path: getTempFile(TEST_TARGET_FILE_NAME_PDF).path },
+ saver: { type: aType }
+ });
+
+ yield test_download_windowRef(tab, download);
+ yield download.start();
+
+ yield test_download_state_complete(tab, download, aPrivate, false);
+ if (aType == "pdf") {
+ let signature = yield OS.File.read(download.target.path,
+ { bytes: 4, encoding: "us-ascii" });
+ is(signature, "%PDF", "File exists and signature matches");
+ } else {
+ ok((yield OS.File.exists(download.target.path)), "File exists");
+ }
+
+ win.gBrowser.removeTab(tab);
+ win.close()
+}
+
+add_task(function* test_createDownload_pdf_private() {
+ yield test_createDownload_common(true, "pdf");
+});
+add_task(function* test_createDownload_pdf_not_private() {
+ yield test_createDownload_common(false, "pdf");
+});
+
+// Even for the copy saver, using a window should produce valid results
+add_task(function* test_createDownload_copy_private() {
+ yield test_createDownload_common(true, "copy");
+});
+add_task(function* test_createDownload_copy_not_private() {
+ yield test_createDownload_common(false, "copy");
+});
+
+add_task(function* test_cancel_pdf_download() {
+ let tab = gBrowser.addTab(getRootDirectory(gTestPath) + "testFile.html");
+ yield promiseBrowserLoaded(tab.linkedBrowser);
+
+ let download = yield Downloads.createDownload({
+ source: tab.linkedBrowser.contentWindow,
+ target: { path: getTempFile(TEST_TARGET_FILE_NAME_PDF).path },
+ saver: "pdf",
+ });
+
+ yield test_download_windowRef(tab, download);
+ download.start().catch(() => {});
+
+ // Immediately cancel the download to test that it is erased correctly.
+ yield download.cancel();
+ yield test_download_state_complete(tab, download, false, true);
+
+ let exists = yield OS.File.exists(download.target.path)
+ ok(!exists, "Target file does not exist");
+
+ gBrowser.removeTab(tab);
+});
diff --git a/toolkit/components/jsdownloads/test/browser/head.js b/toolkit/components/jsdownloads/test/browser/head.js
new file mode 100644
index 000000000..769aaacb3
--- /dev/null
+++ b/toolkit/components/jsdownloads/test/browser/head.js
@@ -0,0 +1,87 @@
+/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set ts=2 et sw=2 tw=80: */
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+/**
+ * Provides infrastructure for automated download components tests.
+ */
+
+"use strict";
+
+// Globals
+
+var Cc = Components.classes;
+var Ci = Components.interfaces;
+var Cu = Components.utils;
+var Cr = Components.results;
+
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+
+XPCOMUtils.defineLazyModuleGetter(this, "DownloadPaths",
+ "resource://gre/modules/DownloadPaths.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "Downloads",
+ "resource://gre/modules/Downloads.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "FileUtils",
+ "resource://gre/modules/FileUtils.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "Services",
+ "resource://gre/modules/Services.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "HttpServer",
+ "resource://testing-common/httpd.js");
+XPCOMUtils.defineLazyModuleGetter(this, "OS",
+ "resource://gre/modules/osfile.jsm");
+
+const TEST_TARGET_FILE_NAME_PDF = "test-download.pdf";
+
+// Support functions
+
+// While the previous test file should have deleted all the temporary files it
+// used, on Windows these might still be pending deletion on the physical file
+// system. Thus, start from a new base number every time, to make a collision
+// with a file that is still pending deletion highly unlikely.
+var gFileCounter = Math.floor(Math.random() * 1000000);
+
+/**
+ * Returns a reference to a temporary file, that is guaranteed not to exist, and
+ * to have never been created before.
+ *
+ * @param aLeafName
+ * Suggested leaf name for the file to be created.
+ *
+ * @return nsIFile pointing to a non-existent file in a temporary directory.
+ *
+ * @note It is not enough to delete the file if it exists, or to delete the file
+ * after calling nsIFile.createUnique, because on Windows the delete
+ * operation in the file system may still be pending, preventing a new
+ * file with the same name to be created.
+ */
+function getTempFile(aLeafName)
+{
+ // Prepend a serial number to the extension in the suggested leaf name.
+ let [base, ext] = DownloadPaths.splitBaseNameAndExtension(aLeafName);
+ let leafName = base + "-" + gFileCounter + ext;
+ gFileCounter++;
+
+ // Get a file reference under the temporary directory for this test file.
+ let file = FileUtils.getFile("TmpD", [leafName]);
+ ok(!file.exists(), "Temp file does not exist");
+
+ registerCleanupFunction(function () {
+ if (file.exists()) {
+ file.remove(false);
+ }
+ });
+
+ return file;
+}
+
+function promiseBrowserLoaded(browser) {
+ return new Promise(resolve => {
+ browser.addEventListener("load", function onLoad(event) {
+ if (event.target == browser.contentDocument) {
+ browser.removeEventListener("load", onLoad, true);
+ resolve();
+ }
+ }, true);
+ });
+}
diff --git a/toolkit/components/jsdownloads/test/browser/testFile.html b/toolkit/components/jsdownloads/test/browser/testFile.html
new file mode 100644
index 000000000..ee413514b
--- /dev/null
+++ b/toolkit/components/jsdownloads/test/browser/testFile.html
@@ -0,0 +1,9 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Test Save as PDF</title>
+ </head>
+ <body>
+ <p>Save me as a PDF!</p>
+ </body>
+</html>