rename from toolkit/components/extensions/test/mochitest/test_chrome_ext_downloads_misc.html
rename to toolkit/components/extensions/test/xpcshell/test_ext_downloads_misc.js
--- a/toolkit/components/extensions/test/mochitest/test_chrome_ext_downloads_misc.html
+++ b/toolkit/components/extensions/test/xpcshell/test_ext_downloads_misc.js
@@ -1,44 +1,79 @@
-<!DOCTYPE HTML>
-<html>
-<head>
- <title>WebExtension test</title>
- <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
- <script src="chrome://mochikit/content/tests/SimpleTest/SpawnTask.js"></script>
- <script src="chrome://mochikit/content/tests/SimpleTest/ExtensionTestUtils.js"></script>
- <script type="text/javascript" src="head.js"></script>
- <link rel="stylesheet" href="chrome://mochikit/contents/tests/SimpleTest/test.css"/>
-</head>
-<body>
-
-<script type="text/javascript">
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
"use strict";
-const {
- interfaces: Ci,
- utils: Cu,
-} = Components;
-
-Cu.import("resource://gre/modules/Services.jsm");
-Cu.import("resource://gre/modules/FileUtils.jsm");
Cu.import("resource://gre/modules/Downloads.jsm");
-const BASE = "http://mochi.test:8888/chrome/toolkit/components/extensions/test/mochitest";
+const server = createHttpServer();
+server.registerDirectory("/data/", do_get_file("data"));
+
+const ROOT = `http://localhost:${server.identity.primaryPort}`;
+const BASE = `${ROOT}/data`;
const TXT_FILE = "file_download.txt";
const TXT_URL = BASE + "/" + TXT_FILE;
// Keep these in sync with code in interruptible.sjs
const INT_PARTIAL_LEN = 15;
const INT_TOTAL_LEN = 31;
+const TEST_DATA = "This is 31 bytes of sample data";
+const TOTAL_LEN = TEST_DATA.length;
+const PARTIAL_LEN = 15;
+
+// A handler to let us systematically test pausing/resuming/canceling
+// of downloads. This target represents a small text file but a simple
+// GET will stall after sending part of the data, to give the test code
+// a chance to pause or do other operations on an in-progress download.
+// A resumed download (ie, a GET with a Range: header) will allow the
+// download to complete.
+function handleRequest(request, response) {
+ response.setHeader("Content-Type", "text/plain", false);
+
+ if (request.hasHeader("Range")) {
+ let start, end;
+ let matches = request.getHeader("Range")
+ .match(/^\s*bytes=(\d+)?-(\d+)?\s*$/);
+ if (matches != null) {
+ start = matches[1] ? parseInt(matches[1], 10) : 0;
+ end = matches[2] ? parseInt(matches[2], 10) : (TOTAL_LEN - 1);
+ }
+
+ if (end == undefined || end >= TOTAL_LEN) {
+ response.setStatusLine(request.httpVersion, 416, "Requested Range Not Satisfiable");
+ response.setHeader("Content-Range", `*/${TOTAL_LEN}`, false);
+ response.finish();
+ return;
+ }
+
+ response.setStatusLine(request.httpVersion, 206, "Partial Content");
+ response.setHeader("Content-Range", `${start}-${end}/${TOTAL_LEN}`, false);
+ response.write(TEST_DATA.slice(start, end + 1));
+ } else {
+ response.processAsync();
+ response.setHeader("Content-Length", `${TOTAL_LEN}`, false);
+ response.write(TEST_DATA.slice(0, PARTIAL_LEN));
+ }
+
+ do_register_cleanup(() => {
+ try {
+ response.finish();
+ } catch (e) {
+ // This will throw, but we don't care at this point.
+ }
+ });
+}
+
+server.registerPathHandler("/interruptible.html", handleRequest);
+
let interruptibleCount = 0;
function getInterruptibleUrl() {
let n = interruptibleCount++;
- return `${BASE}/interruptible.sjs?count=${n}`;
+ return `${ROOT}/interruptible.html?count=${n}`;
}
function backgroundScript() {
let events = new Set();
let eventWaiter = null;
browser.downloads.onCreated.addListener(data => {
events.add({type: "onCreated", data});
@@ -197,83 +232,84 @@ function waitForProgress(url, bytes) {
list.addView(view);
}));
}
add_task(function* setup() {
const nsIFile = Ci.nsIFile;
downloadDir = FileUtils.getDir("TmpD", ["downloads"]);
downloadDir.createUnique(nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
- info(`downloadDir ${downloadDir.path}`);
+ do_print(`downloadDir ${downloadDir.path}`);
Services.prefs.setIntPref("browser.download.folderList", 2);
Services.prefs.setComplexValue("browser.download.dir", nsIFile, downloadDir);
- SimpleTest.registerCleanupFunction(() => {
+ do_register_cleanup(() => {
Services.prefs.clearUserPref("browser.download.folderList");
Services.prefs.clearUserPref("browser.download.dir");
downloadDir.remove(true);
+
return clearDownloads();
});
yield clearDownloads().then(downloads => {
- info(`removed ${downloads.length} pre-existing downloads from history`);
+ do_print(`removed ${downloads.length} pre-existing downloads from history`);
});
extension = ExtensionTestUtils.loadExtension({
- background: `(${backgroundScript})()`,
+ background: backgroundScript,
manifest: {
permissions: ["downloads"],
},
});
yield extension.startup();
yield extension.awaitMessage("ready");
- info("extension started");
});
add_task(function* test_events() {
let msg = yield runInExtension("download", {url: TXT_URL});
- is(msg.status, "success", "download() succeeded");
+ equal(msg.status, "success", "download() succeeded");
const id = msg.result;
msg = yield runInExtension("waitForEvents", [
{type: "onCreated", data: {id, url: TXT_URL}},
{
type: "onChanged",
data: {
id,
state: {
previous: "in_progress",
current: "complete",
},
},
},
]);
- is(msg.status, "success", "got onCreated and onChanged events");
+ equal(msg.status, "success", "got onCreated and onChanged events");
});
add_task(function* test_cancel() {
let url = getInterruptibleUrl();
+ do_print(url);
let msg = yield runInExtension("download", {url});
- is(msg.status, "success", "download() succeeded");
+ equal(msg.status, "success", "download() succeeded");
const id = msg.result;
let progressPromise = waitForProgress(url, INT_PARTIAL_LEN);
msg = yield runInExtension("waitForEvents", [
{type: "onCreated", data: {id}},
]);
- is(msg.status, "success", "got created and changed events");
+ equal(msg.status, "success", "got created and changed events");
yield progressPromise;
- info(`download reached ${INT_PARTIAL_LEN} bytes`);
+ do_print(`download reached ${INT_PARTIAL_LEN} bytes`);
msg = yield runInExtension("cancel", id);
- is(msg.status, "success", "cancel() succeeded");
+ equal(msg.status, "success", "cancel() succeeded");
// This sequence of events is bogus (bug 1256243)
msg = yield runInExtension("waitForEvents", [
{
type: "onChanged",
data: {
state: {
previous: "in_progress",
@@ -299,54 +335,54 @@ add_task(function* test_cancel() {
id,
paused: {
previous: true,
current: false,
},
},
},
]);
- is(msg.status, "success", "got onChanged events corresponding to cancel()");
+ equal(msg.status, "success", "got onChanged events corresponding to cancel()");
msg = yield runInExtension("search", {error: "USER_CANCELED"});
- is(msg.status, "success", "search() succeeded");
- is(msg.result.length, 1, "search() found 1 download");
- is(msg.result[0].id, id, "download.id is correct");
- is(msg.result[0].state, "interrupted", "download.state is correct");
- is(msg.result[0].paused, false, "download.paused is correct");
- is(msg.result[0].canResume, false, "download.canResume is correct");
- is(msg.result[0].error, "USER_CANCELED", "download.error is correct");
- is(msg.result[0].totalBytes, INT_TOTAL_LEN, "download.totalBytes is correct");
- is(msg.result[0].exists, false, "download.exists is correct");
+ equal(msg.status, "success", "search() succeeded");
+ equal(msg.result.length, 1, "search() found 1 download");
+ equal(msg.result[0].id, id, "download.id is correct");
+ equal(msg.result[0].state, "interrupted", "download.state is correct");
+ equal(msg.result[0].paused, false, "download.paused is correct");
+ equal(msg.result[0].canResume, false, "download.canResume is correct");
+ equal(msg.result[0].error, "USER_CANCELED", "download.error is correct");
+ equal(msg.result[0].totalBytes, INT_TOTAL_LEN, "download.totalBytes is correct");
+ equal(msg.result[0].exists, false, "download.exists is correct");
msg = yield runInExtension("pause", id);
- is(msg.status, "error", "cannot pause a canceled download");
+ equal(msg.status, "error", "cannot pause a canceled download");
msg = yield runInExtension("resume", id);
- is(msg.status, "error", "cannot resume a canceled download");
+ equal(msg.status, "error", "cannot resume a canceled download");
});
add_task(function* test_pauseresume() {
let url = getInterruptibleUrl();
let msg = yield runInExtension("download", {url});
- is(msg.status, "success", "download() succeeded");
+ equal(msg.status, "success", "download() succeeded");
const id = msg.result;
let progressPromise = waitForProgress(url, INT_PARTIAL_LEN);
msg = yield runInExtension("waitForEvents", [
{type: "onCreated", data: {id}},
]);
- is(msg.status, "success", "got created and changed events");
+ equal(msg.status, "success", "got created and changed events");
yield progressPromise;
- info(`download reached ${INT_PARTIAL_LEN} bytes`);
+ do_print(`download reached ${INT_PARTIAL_LEN} bytes`);
msg = yield runInExtension("pause", id);
- is(msg.status, "success", "pause() succeeded");
+ equal(msg.status, "success", "pause() succeeded");
msg = yield runInExtension("waitForEvents", [
{
type: "onChanged",
data: {
id,
state: {
previous: "in_progress",
@@ -366,40 +402,40 @@ add_task(function* test_pauseresume() {
data: {
id,
error: {
previous: null,
current: "USER_CANCELED",
},
},
}]);
- is(msg.status, "success", "got onChanged event corresponding to pause");
+ equal(msg.status, "success", "got onChanged event corresponding to pause");
msg = yield runInExtension("search", {paused: true});
- is(msg.status, "success", "search() succeeded");
- is(msg.result.length, 1, "search() found 1 download");
- is(msg.result[0].id, id, "download.id is correct");
- is(msg.result[0].state, "interrupted", "download.state is correct");
- is(msg.result[0].paused, true, "download.paused is correct");
- is(msg.result[0].canResume, true, "download.canResume is correct");
- is(msg.result[0].error, "USER_CANCELED", "download.error is correct");
- is(msg.result[0].bytesReceived, INT_PARTIAL_LEN, "download.bytesReceived is correct");
- is(msg.result[0].totalBytes, INT_TOTAL_LEN, "download.totalBytes is correct");
- is(msg.result[0].exists, false, "download.exists is correct");
+ equal(msg.status, "success", "search() succeeded");
+ equal(msg.result.length, 1, "search() found 1 download");
+ equal(msg.result[0].id, id, "download.id is correct");
+ equal(msg.result[0].state, "interrupted", "download.state is correct");
+ equal(msg.result[0].paused, true, "download.paused is correct");
+ equal(msg.result[0].canResume, true, "download.canResume is correct");
+ equal(msg.result[0].error, "USER_CANCELED", "download.error is correct");
+ equal(msg.result[0].bytesReceived, INT_PARTIAL_LEN, "download.bytesReceived is correct");
+ equal(msg.result[0].totalBytes, INT_TOTAL_LEN, "download.totalBytes is correct");
+ equal(msg.result[0].exists, false, "download.exists is correct");
msg = yield runInExtension("search", {error: "USER_CANCELED"});
- is(msg.status, "success", "search() succeeded");
+ equal(msg.status, "success", "search() succeeded");
let found = msg.result.filter(item => item.id == id);
- is(found.length, 1, "search() by error found the paused download");
+ equal(found.length, 1, "search() by error found the paused download");
msg = yield runInExtension("pause", id);
- is(msg.status, "error", "cannot pause an already paused download");
+ equal(msg.status, "error", "cannot pause an already paused download");
msg = yield runInExtension("resume", id);
- is(msg.status, "success", "resume() succeeded");
+ equal(msg.status, "success", "resume() succeeded");
msg = yield runInExtension("waitForEvents", [
{
type: "onChanged",
data: {
id,
state: {
previous: "interrupted",
@@ -425,54 +461,54 @@ add_task(function* test_pauseresume() {
id,
state: {
previous: "in_progress",
current: "complete",
},
},
},
]);
- is(msg.status, "success", "got onChanged events for resume and complete");
+ equal(msg.status, "success", "got onChanged events for resume and complete");
msg = yield runInExtension("search", {id});
- is(msg.status, "success", "search() succeeded");
- is(msg.result.length, 1, "search() found 1 download");
- is(msg.result[0].state, "complete", "download.state is correct");
- is(msg.result[0].paused, false, "download.paused is correct");
- is(msg.result[0].canResume, false, "download.canResume is correct");
- is(msg.result[0].error, null, "download.error is correct");
- is(msg.result[0].bytesReceived, INT_TOTAL_LEN, "download.bytesReceived is correct");
- is(msg.result[0].totalBytes, INT_TOTAL_LEN, "download.totalBytes is correct");
- is(msg.result[0].exists, true, "download.exists is correct");
+ equal(msg.status, "success", "search() succeeded");
+ equal(msg.result.length, 1, "search() found 1 download");
+ equal(msg.result[0].state, "complete", "download.state is correct");
+ equal(msg.result[0].paused, false, "download.paused is correct");
+ equal(msg.result[0].canResume, false, "download.canResume is correct");
+ equal(msg.result[0].error, null, "download.error is correct");
+ equal(msg.result[0].bytesReceived, INT_TOTAL_LEN, "download.bytesReceived is correct");
+ equal(msg.result[0].totalBytes, INT_TOTAL_LEN, "download.totalBytes is correct");
+ equal(msg.result[0].exists, true, "download.exists is correct");
msg = yield runInExtension("pause", id);
- is(msg.status, "error", "cannot pause a completed download");
+ equal(msg.status, "error", "cannot pause a completed download");
msg = yield runInExtension("resume", id);
- is(msg.status, "error", "cannot resume a completed download");
+ equal(msg.status, "error", "cannot resume a completed download");
});
add_task(function* test_pausecancel() {
let url = getInterruptibleUrl();
let msg = yield runInExtension("download", {url});
- is(msg.status, "success", "download() succeeded");
+ equal(msg.status, "success", "download() succeeded");
const id = msg.result;
let progressPromise = waitForProgress(url, INT_PARTIAL_LEN);
msg = yield runInExtension("waitForEvents", [
{type: "onCreated", data: {id}},
]);
- is(msg.status, "success", "got created and changed events");
+ equal(msg.status, "success", "got created and changed events");
yield progressPromise;
- info(`download reached ${INT_PARTIAL_LEN} bytes`);
+ do_print(`download reached ${INT_PARTIAL_LEN} bytes`);
msg = yield runInExtension("pause", id);
- is(msg.status, "success", "pause() succeeded");
+ equal(msg.status, "success", "pause() succeeded");
msg = yield runInExtension("waitForEvents", [
{
type: "onChanged",
data: {
id,
state: {
previous: "in_progress",
@@ -492,37 +528,37 @@ add_task(function* test_pausecancel() {
data: {
id,
error: {
previous: null,
current: "USER_CANCELED",
},
},
}]);
- is(msg.status, "success", "got onChanged event corresponding to pause");
+ equal(msg.status, "success", "got onChanged event corresponding to pause");
msg = yield runInExtension("search", {paused: true});
- is(msg.status, "success", "search() succeeded");
- is(msg.result.length, 1, "search() found 1 download");
- is(msg.result[0].id, id, "download.id is correct");
- is(msg.result[0].state, "interrupted", "download.state is correct");
- is(msg.result[0].paused, true, "download.paused is correct");
- is(msg.result[0].canResume, true, "download.canResume is correct");
- is(msg.result[0].error, "USER_CANCELED", "download.error is correct");
- is(msg.result[0].bytesReceived, INT_PARTIAL_LEN, "download.bytesReceived is correct");
- is(msg.result[0].totalBytes, INT_TOTAL_LEN, "download.totalBytes is correct");
- is(msg.result[0].exists, false, "download.exists is correct");
+ equal(msg.status, "success", "search() succeeded");
+ equal(msg.result.length, 1, "search() found 1 download");
+ equal(msg.result[0].id, id, "download.id is correct");
+ equal(msg.result[0].state, "interrupted", "download.state is correct");
+ equal(msg.result[0].paused, true, "download.paused is correct");
+ equal(msg.result[0].canResume, true, "download.canResume is correct");
+ equal(msg.result[0].error, "USER_CANCELED", "download.error is correct");
+ equal(msg.result[0].bytesReceived, INT_PARTIAL_LEN, "download.bytesReceived is correct");
+ equal(msg.result[0].totalBytes, INT_TOTAL_LEN, "download.totalBytes is correct");
+ equal(msg.result[0].exists, false, "download.exists is correct");
msg = yield runInExtension("search", {error: "USER_CANCELED"});
- is(msg.status, "success", "search() succeeded");
+ equal(msg.status, "success", "search() succeeded");
let found = msg.result.filter(item => item.id == id);
- is(found.length, 1, "search() by error found the paused download");
+ equal(found.length, 1, "search() by error found the paused download");
msg = yield runInExtension("cancel", id);
- is(msg.status, "success", "cancel() succeeded");
+ equal(msg.status, "success", "cancel() succeeded");
msg = yield runInExtension("waitForEvents", [
{
type: "onChanged",
data: {
id,
paused: {
previous: true,
@@ -530,96 +566,96 @@ add_task(function* test_pausecancel() {
},
canResume: {
previous: true,
current: false,
},
},
},
]);
- is(msg.status, "success", "got onChanged event for cancel");
+ equal(msg.status, "success", "got onChanged event for cancel");
msg = yield runInExtension("search", {id});
- is(msg.status, "success", "search() succeeded");
- is(msg.result.length, 1, "search() found 1 download");
- is(msg.result[0].state, "interrupted", "download.state is correct");
- is(msg.result[0].paused, false, "download.paused is correct");
- is(msg.result[0].canResume, false, "download.canResume is correct");
- is(msg.result[0].error, "USER_CANCELED", "download.error is correct");
- is(msg.result[0].totalBytes, INT_TOTAL_LEN, "download.totalBytes is correct");
- is(msg.result[0].exists, false, "download.exists is correct");
+ equal(msg.status, "success", "search() succeeded");
+ equal(msg.result.length, 1, "search() found 1 download");
+ equal(msg.result[0].state, "interrupted", "download.state is correct");
+ equal(msg.result[0].paused, false, "download.paused is correct");
+ equal(msg.result[0].canResume, false, "download.canResume is correct");
+ equal(msg.result[0].error, "USER_CANCELED", "download.error is correct");
+ equal(msg.result[0].totalBytes, INT_TOTAL_LEN, "download.totalBytes is correct");
+ equal(msg.result[0].exists, false, "download.exists is correct");
});
add_task(function* test_pause_resume_cancel_badargs() {
let BAD_ID = 1000;
let msg = yield runInExtension("pause", BAD_ID);
- info(JSON.stringify(msg));
- is(msg.status, "error", "pause() failed with a bad download id");
+ do_print(JSON.stringify(msg));
+ equal(msg.status, "error", "pause() failed with a bad download id");
ok(/Invalid download id/.test(msg.errmsg), "error message is descriptive");
msg = yield runInExtension("resume", BAD_ID);
- is(msg.status, "error", "resume() failed with a bad download id");
+ equal(msg.status, "error", "resume() failed with a bad download id");
ok(/Invalid download id/.test(msg.errmsg), "error message is descriptive");
msg = yield runInExtension("cancel", BAD_ID);
- is(msg.status, "error", "cancel() failed with a bad download id");
+ equal(msg.status, "error", "cancel() failed with a bad download id");
ok(/Invalid download id/.test(msg.errmsg), "error message is descriptive");
});
add_task(function* test_file_removal() {
let msg = yield runInExtension("download", {url: TXT_URL});
- is(msg.status, "success", "download() succeeded");
+ equal(msg.status, "success", "download() succeeded");
const id = msg.result;
msg = yield runInExtension("waitForEvents", [
{type: "onCreated", data: {id, url: TXT_URL}},
{
type: "onChanged",
data: {
id,
state: {
previous: "in_progress",
current: "complete",
},
},
},
]);
- is(msg.status, "success", "got onCreated and onChanged events");
+ equal(msg.status, "success", "got onCreated and onChanged events");
msg = yield runInExtension("removeFile", id);
- is(msg.status, "success", "removeFile() succeeded");
+ equal(msg.status, "success", "removeFile() succeeded");
msg = yield runInExtension("removeFile", id);
- is(msg.status, "error", "removeFile() fails since the file was already removed.");
+ equal(msg.status, "error", "removeFile() fails since the file was already removed.");
ok(/file doesn't exist/.test(msg.errmsg), "removeFile() failed on removed file.");
msg = yield runInExtension("removeFile", 1000);
ok(/Invalid download id/.test(msg.errmsg), "removeFile() failed due to non-existent id");
});
add_task(function* test_removal_of_incomplete_download() {
let url = getInterruptibleUrl();
let msg = yield runInExtension("download", {url});
- is(msg.status, "success", "download() succeeded");
+ equal(msg.status, "success", "download() succeeded");
const id = msg.result;
let progressPromise = waitForProgress(url, INT_PARTIAL_LEN);
msg = yield runInExtension("waitForEvents", [
{type: "onCreated", data: {id}},
]);
- is(msg.status, "success", "got created and changed events");
+ equal(msg.status, "success", "got created and changed events");
yield progressPromise;
- info(`download reached ${INT_PARTIAL_LEN} bytes`);
+ do_print(`download reached ${INT_PARTIAL_LEN} bytes`);
msg = yield runInExtension("pause", id);
- is(msg.status, "success", "pause() succeeded");
+ equal(msg.status, "success", "pause() succeeded");
msg = yield runInExtension("waitForEvents", [
{
type: "onChanged",
data: {
id,
state: {
previous: "in_progress",
@@ -639,25 +675,25 @@ add_task(function* test_removal_of_incom
data: {
id,
error: {
previous: null,
current: "USER_CANCELED",
},
},
}]);
- is(msg.status, "success", "got onChanged event corresponding to pause");
+ equal(msg.status, "success", "got onChanged event corresponding to pause");
msg = yield runInExtension("removeFile", id);
- is(msg.status, "error", "removeFile() on paused download failed");
+ equal(msg.status, "error", "removeFile() on paused download failed");
ok(/Cannot remove incomplete download/.test(msg.errmsg), "removeFile() failed due to download being incomplete");
msg = yield runInExtension("resume", id);
- is(msg.status, "success", "resume() succeeded");
+ equal(msg.status, "success", "resume() succeeded");
msg = yield runInExtension("waitForEvents", [
{
type: "onChanged",
data: {
id,
state: {
previous: "interrupted",
@@ -683,144 +719,145 @@ add_task(function* test_removal_of_incom
id,
state: {
previous: "in_progress",
current: "complete",
},
},
},
]);
- is(msg.status, "success", "got onChanged events for resume and complete");
+ equal(msg.status, "success", "got onChanged events for resume and complete");
msg = yield runInExtension("removeFile", id);
- is(msg.status, "success", "removeFile() succeeded following completion of resumed download.");
+ equal(msg.status, "success", "removeFile() succeeded following completion of resumed download.");
});
// Test erase(). We don't do elaborate testing of the query handling
// since it uses the exact same engine as search() which is tested
// more thoroughly in test_chrome_ext_downloads_search.html
add_task(function* test_erase() {
yield clearDownloads();
yield runInExtension("clearEvents");
function* download() {
let msg = yield runInExtension("download", {url: TXT_URL});
- is(msg.status, "success", "download succeeded");
+ equal(msg.status, "success", "download succeeded");
let id = msg.result;
msg = yield runInExtension("waitForEvents", [{
type: "onChanged", data: {id, state: {current: "complete"}},
}], {exact: false});
- is(msg.status, "success", "download finished");
+ equal(msg.status, "success", "download finished");
return id;
}
let ids = {};
ids.dl1 = yield download();
ids.dl2 = yield download();
ids.dl3 = yield download();
let msg = yield runInExtension("search", {});
- is(msg.status, "success", "search succeded");
- is(msg.result.length, 3, "search found 3 downloads");
+ equal(msg.status, "success", "search succeded");
+ equal(msg.result.length, 3, "search found 3 downloads");
msg = yield runInExtension("clearEvents");
msg = yield runInExtension("erase", {id: ids.dl1});
- is(msg.status, "success", "erase by id succeeded");
+ equal(msg.status, "success", "erase by id succeeded");
msg = yield runInExtension("waitForEvents", [
{type: "onErased", data: ids.dl1},
]);
- is(msg.status, "success", "received onErased event");
+ equal(msg.status, "success", "received onErased event");
msg = yield runInExtension("search", {});
- is(msg.status, "success", "search succeded");
- is(msg.result.length, 2, "search found 2 downloads");
+ equal(msg.status, "success", "search succeded");
+ equal(msg.result.length, 2, "search found 2 downloads");
msg = yield runInExtension("erase", {});
- is(msg.status, "success", "erase everything succeeded");
+ equal(msg.status, "success", "erase everything succeeded");
msg = yield runInExtension("waitForEvents", [
{type: "onErased", data: ids.dl2},
{type: "onErased", data: ids.dl3},
], {inorder: false});
- is(msg.status, "success", "received 2 onErased events");
+ equal(msg.status, "success", "received 2 onErased events");
msg = yield runInExtension("search", {});
- is(msg.status, "success", "search succeded");
- is(msg.result.length, 0, "search found 0 downloads");
+ equal(msg.status, "success", "search succeded");
+ equal(msg.result.length, 0, "search found 0 downloads");
});
function loadImage(img, data) {
return new Promise((resolve) => {
- let handle = () => {
- img.removeEventListener("load", handle);
- resolve();
- };
- img.addEventListener("load", handle);
img.src = data;
+ img.onload = resolve;
});
}
add_task(function* test_getFileIcon() {
- let img = document.createElement("img");
+ let webNav = Services.appShell.createWindowlessBrowser(false);
+ let docShell = webNav.QueryInterface(Ci.nsIInterfaceRequestor)
+ .getInterface(Ci.nsIDocShell);
+
+ let system = Services.scriptSecurityManager.getSystemPrincipal();
+ docShell.createAboutBlankContentViewer(system);
+
+ let img = webNav.document.createElement("img");
+
let msg = yield runInExtension("download", {url: TXT_URL});
- is(msg.status, "success", "download() succeeded");
+ equal(msg.status, "success", "download() succeeded");
const id = msg.result;
msg = yield runInExtension("getFileIcon", id);
- is(msg.status, "success", "getFileIcon() succeeded");
+ equal(msg.status, "success", "getFileIcon() succeeded");
yield loadImage(img, msg.result);
- is(img.height, 32, "returns an icon with the right height");
- is(img.width, 32, "returns an icon with the right width");
+ equal(img.height, 32, "returns an icon with the right height");
+ equal(img.width, 32, "returns an icon with the right width");
msg = yield runInExtension("waitForEvents", [
{type: "onCreated", data: {id, url: TXT_URL}},
{type: "onChanged"},
]);
- is(msg.status, "success", "got events");
+ equal(msg.status, "success", "got events");
msg = yield runInExtension("getFileIcon", id);
- is(msg.status, "success", "getFileIcon() succeeded");
+ equal(msg.status, "success", "getFileIcon() succeeded");
yield loadImage(img, msg.result);
- is(img.height, 32, "returns an icon with the right height after download");
- is(img.width, 32, "returns an icon with the right width after download");
+ equal(img.height, 32, "returns an icon with the right height after download");
+ equal(img.width, 32, "returns an icon with the right width after download");
msg = yield runInExtension("getFileIcon", id + 100);
- is(msg.status, "error", "getFileIcon() failed");
+ equal(msg.status, "error", "getFileIcon() failed");
ok(msg.errmsg.includes("Invalid download id"), "download id is invalid");
msg = yield runInExtension("getFileIcon", id, {size: 127});
- is(msg.status, "success", "getFileIcon() succeeded");
+ equal(msg.status, "success", "getFileIcon() succeeded");
yield loadImage(img, msg.result);
- is(img.height, 127, "returns an icon with the right custom height");
- is(img.width, 127, "returns an icon with the right custom width");
+ equal(img.height, 127, "returns an icon with the right custom height");
+ equal(img.width, 127, "returns an icon with the right custom width");
msg = yield runInExtension("getFileIcon", id, {size: 1});
- is(msg.status, "success", "getFileIcon() succeeded");
+ equal(msg.status, "success", "getFileIcon() succeeded");
yield loadImage(img, msg.result);
- is(img.height, 1, "returns an icon with the right custom height");
- is(img.width, 1, "returns an icon with the right custom width");
+ equal(img.height, 1, "returns an icon with the right custom height");
+ equal(img.width, 1, "returns an icon with the right custom width");
msg = yield runInExtension("getFileIcon", id, {size: "foo"});
- is(msg.status, "error", "getFileIcon() fails");
+ equal(msg.status, "error", "getFileIcon() fails");
ok(msg.errmsg.includes("Error processing size"), "size is not a number");
msg = yield runInExtension("getFileIcon", id, {size: 0});
- is(msg.status, "error", "getFileIcon() fails");
+ equal(msg.status, "error", "getFileIcon() fails");
ok(msg.errmsg.includes("Error processing size"), "size is too small");
msg = yield runInExtension("getFileIcon", id, {size: 128});
- is(msg.status, "error", "getFileIcon() fails");
+ equal(msg.status, "error", "getFileIcon() fails");
ok(msg.errmsg.includes("Error processing size"), "size is too big");
+
+ webNav.close();
});
add_task(function* cleanup() {
yield extension.unload();
});
-
-</script>
-
-</body>
-</html>