Bug 938023 - Implement the download API : Part 3, tests. r=auswerk

This commit is contained in:
Fabrice Desré 2013-11-27 23:18:12 -08:00
parent 2d148375b6
commit 93bfbf3600
11 changed files with 564 additions and 0 deletions

View File

@ -1537,6 +1537,13 @@ Navigator::DoNewResolve(JSContext* aCx, JS::Handle<JSObject*> aObject,
}
}
if (name.EqualsLiteral("mozDownloadManager")) {
if (!CheckPermission("downloads")) {
aValue.setNull();
return true;
}
}
domObject = construct(aCx, naviObj);
if (!domObject) {
return Throw(aCx, NS_ERROR_FAILURE);

View File

@ -4,4 +4,7 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
if CONFIG["MOZ_B2G"]:
TEST_DIRS += ['tests']
PARALLEL_DIRS += ['src']

View File

@ -23,6 +23,7 @@ function debug(aStr) {
}
function sendPromiseMessage(aMm, aMessageName, aData, aError) {
debug("sendPromiseMessage " + aMessageName);
let msg = {
id: aData.id,
promiseId: aData.promiseId

View File

@ -0,0 +1,9 @@
[DEFAULT]
support-files =
serve_file.sjs
[test_downloads_navigator_object.html]
[test_downloads_basic.html]
[test_downloads_large.html]
[test_downloads_pause_remove.html]
[test_downloads_pause_resume.html]

View File

@ -0,0 +1,7 @@
# -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*-
# vim: set filetype=python:
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
MOCHITEST_MANIFESTS += ['mochitest.ini']

View File

@ -0,0 +1,34 @@
// Serves a file with a given mime type and size.
function getQuery(request) {
var query = {};
request.queryString.split('&').forEach(function (val) {
var [name, value] = val.split('=');
query[name] = unescape(value);
});
return query;
}
function handleRequest(request, response) {
var query = getQuery(request);
// Default values for content type and size.
var contentType = "text/plain";
var size = 1024;
if ("contentType" in query) {
contentType = query["contentType"];
}
if ("size" in query) {
size = query["size"];
}
// Generate the content.
response.setHeader("Content-Type", contentType, false);
for (let i = 0; i < size; i++) {
response.write("0");
}
}

View File

@ -0,0 +1,84 @@
<!DOCTYPE html>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=938023
-->
<head>
<title>Test for Bug 938023 Downloads API</title>
<script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=938023">Mozilla Bug 938023</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<a href="serve_file.sjs?contentType=application/octet-stream&size=1024" download="test.bin" id="download1">Download #1</a>
<pre id="test">
<script class="testbody" type="text/javascript;version=1.7">
// Testing a simple download, waiting for it to be done.
SimpleTest.waitForExplicitFinish();
var index = -1;
function next() {
index += 1;
if (index >= steps.length) {
ok(false, "Shouldn't get here!");
return;
}
try {
steps[index]();
} catch(ex) {
ok(false, "Caught exception", ex);
}
}
function downloadChange(evt) {
var download = evt.download;
if (download.state == "succeeded") {
ok(download.totalBytes == 1024, "Download size is 1024 bytes.");
ok(download.contentType == "application/octet-stream",
"contentType is application/octet-stream.");
SimpleTest.finish();
}
}
var steps = [
// Start by setting the pref to true.
function() {
SpecialPowers.pushPrefEnv({
set: [["dom.mozDownloads.enabled", true]]
}, next);
},
// Setup the event listeners.
function() {
SpecialPowers.pushPermissions([
{type: "downloads", allow: true, context: document}
], function() {
navigator.mozDownloadManager.ondownloadstart =
function(evt) {
ok(true, "Download started");
evt.download.addEventListener("statechange", downloadChange);
}
next();
});
},
// Click on the <a download> to start the download.
function() {
document.getElementById("download1").click();
}
];
next();
</script>
</pre>
</body>
</html>

View File

@ -0,0 +1,109 @@
<!DOCTYPE html>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=938023
-->
<head>
<title>Test for Bug 938023 Downloads API</title>
<script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=938023">Mozilla Bug 938023</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<a href="serve_file.sjs?contentType=application/octet-stream&size=102400" download="test.bin" id="download1">Large Download</a>
<pre id="test">
<script class="testbody" type="text/javascript;version=1.7">
// Testing downloading a file, then checking getDownloads() and clearAllDone().
SimpleTest.waitForExplicitFinish();
var index = -1;
function next(args) {
index += 1;
if (index >= steps.length) {
ok(false, "Shouldn't get here!");
return;
}
try {
steps[index](args);
} catch(ex) {
ok(false, "Caught exception", ex);
}
}
// Catch all error function.
function error() {
ok(false, "API failure");
SimpleTest.finish();
}
function getDownloads(downloads) {
ok(downloads.length == 1, "One downloads after getDownloads");
navigator.mozDownloadManager.clearAllDone().then(clearAllDone, error);
}
function clearAllDone(downloads) {
ok(downloads.length == 0, "No downloads after clearAllDone");
SimpleTest.finish();
}
function downloadChange(evt) {
var download = evt.download;
if (download.state == "succeeded") {
ok(download.totalBytes == 102400, "Download size is 100k bytes.");
navigator.mozDownloadManager.getDownloads().then(getDownloads, error);
}
}
var steps = [
// Start by setting the pref to true.
function() {
SpecialPowers.pushPrefEnv({
set: [["dom.mozDownloads.enabled", true]]
}, next);
},
// Setup permission and clear current list.
function() {
SpecialPowers.pushPermissions([
{type: "downloads", allow: true, context: document}
], function() {
navigator.mozDownloadManager.clearAllDone().then(next, error);
});
},
function(downloads) {
ok(downloads.length == 0, "Start with an empty download list.");
next();
},
// Setup the event listeners.
function() {
navigator.mozDownloadManager.ondownloadstart =
function(evt) {
ok(true, "Download started");
evt.download.addEventListener("statechange", downloadChange);
}
next();
},
// Click on the <a download> to start the download.
function() {
document.getElementById("download1").click();
}
];
next();
</script>
</pre>
</body>
</html>

View File

@ -0,0 +1,75 @@
<!DOCTYPE html>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=938023
-->
<head>
<title>Test for Bug 938023 Downloads API</title>
<script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=938023">Mozilla Bug 938023</a>
<p id="display"></p>
<div id="content" style="display: none">
<iframe></iframe>
</div>
<pre id="test">
<script class="testbody" type="text/javascript;version=1.7">
SimpleTest.waitForExplicitFinish();
var index = -1;
function next() {
index += 1;
if (index >= steps.length) {
ok(false, "Shouldn't get here!");
return;
}
try {
steps[index]();
} catch(ex) {
ok(false, "Caught exception", ex);
}
}
var steps = [
// Start by setting the pref to true.
function() {
SpecialPowers.pushPrefEnv({
set: [["dom.mozDownloads.enabled", true]]
}, next);
},
function() {
SpecialPowers.pushPermissions([
{type: "downloads", allow: 0, context: document}
], function() {
ise(frames[0].navigator.mozDownloadManager, null, "navigator.mozDownloadManager is null when the page doesn't have permissions");
next();
});
},
function() {
SpecialPowers.pushPrefEnv({
set: [["dom.mozDownloads.enabled", false]]
}, function() {
ise(navigator.mozDownloadManager, undefined, "navigator.mozDownloadManager is undefined");
next();
});
},
function() {
SimpleTest.finish();
}
];
next();
</script>
</pre>
</body>
</html>

View File

@ -0,0 +1,116 @@
<!DOCTYPE html>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=938023
-->
<head>
<title>Test for Bug 938023 Downloads API</title>
<script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=938023">Mozilla Bug 938023</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<a href="serve_file.sjs?contentType=application/octet-stream&size=102400" download="test.bin" id="download1">Large Download</a>
<pre id="test">
<script class="testbody" type="text/javascript;version=1.7">
// Testing pausing a download and then removing it.
SimpleTest.waitForExplicitFinish();
var index = -1;
function next(args) {
index += 1;
if (index >= steps.length) {
ok(false, "Shouldn't get here!");
return;
}
try {
steps[index](args);
} catch(ex) {
ok(false, "Caught exception", ex);
}
}
var pausing = false;
// Catch all error function.
function error() {
ok(false, "API failure");
SimpleTest.finish();
}
function checkDownloadList(downloads) {
ok(downloads.length == 0, "No downloads left");
SimpleTest.finish();
}
function checkRemoved(download) {
ok(download.state == "finalized", "Download removed.");
navigator.mozDownloadManager.getDownloads()
.then(checkDownloadList, error);
}
function downloadChange(evt) {
var download = evt.download;
if (download.state == "downloading" && !pausing) {
pausing = true;
download.pause();
} else if (download.state == "stopped") {
ok(pausing, "Download stopped by pause()");
navigator.mozDownloadManager.remove(download)
.then(checkRemoved, error);
}
}
var steps = [
// Start by setting the pref to true.
function() {
SpecialPowers.pushPrefEnv({
set: [["dom.mozDownloads.enabled", true]]
}, next);
},
// Setup permission and clear current list.
function() {
SpecialPowers.pushPermissions([
{type: "downloads", allow: true, context: document}
], function() {
navigator.mozDownloadManager.clearAllDone().then(next, error);
});
},
function(downloads) {
ok(downloads.length == 0, "Start with an empty download list.");
next();
},
// Setup the event listeners.
function() {
navigator.mozDownloadManager.ondownloadstart =
function(evt) {
ok(true, "Download started");
evt.download.addEventListener("statechange", downloadChange);
}
next();
},
// Click on the <a download> to start the download.
function() {
document.getElementById("download1").click();
}
];
next();
</script>
</pre>
</body>
</html>

View File

@ -0,0 +1,119 @@
<!DOCTYPE html>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=938023
-->
<head>
<title>Test for Bug 938023 Downloads API</title>
<script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=938023">Mozilla Bug 938023</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<a href="serve_file.sjs?contentType=application/octet-stream&size=102400" download="test.bin" id="download1">Large Download</a>
<pre id="test">
<script class="testbody" type="text/javascript;version=1.7">
// Testing pausing a download and then resuming it.
SimpleTest.waitForExplicitFinish();
var index = -1;
function next(args) {
index += 1;
if (index >= steps.length) {
ok(false, "Shouldn't get here!");
return;
}
try {
steps[index](args);
} catch(ex) {
ok(false, "Caught exception", ex);
}
}
var pausing = false;
var resuming = false;
// Catch all error function.
function error() {
ok(false, "API failure");
SimpleTest.finish();
}
function checkDownloadList(downloads) {
ok(downloads.length == 0, "No downloads left");
SimpleTest.finish();
}
function checkResumedFailed(download) {
ok(download.state == "stopped", "Download fails to resume.");
navigator.mozDownloadManager.clearAllDone()
.then(checkDownloadList, error);
}
function downloadChange(evt) {
var download = evt.download;
if (download.state == "downloading" && !pausing) {
pausing = true;
download.pause();
} else if (download.state == "stopped" && !resuming) {
resuming = true;
ok(pausing, "Download stopped by pause()");
// serve_file.sjs does not support resuming, so that should fail.
download.resume()
.then(error, function() { checkResumedFailed(download); });
}
}
var steps = [
// Start by setting the pref to true.
function() {
SpecialPowers.pushPrefEnv({
set: [["dom.mozDownloads.enabled", true]]
}, next);
},
// Setup permission and clear current list.
function() {
SpecialPowers.pushPermissions([
{type: "downloads", allow: true, context: document}
], function() {
navigator.mozDownloadManager.clearAllDone().then(next, error);
});
},
function(downloads) {
ok(downloads.length == 0, "Start with an empty download list.");
next();
},
// Setup the event listeners.
function() {
navigator.mozDownloadManager.ondownloadstart =
function(evt) {
ok(true, "Download started");
evt.download.addEventListener("statechange", downloadChange);
}
next();
},
// Click on the <a download> to start the download.
function() {
document.getElementById("download1").click();
}
];
next();
</script>
</pre>
</body>
</html>