2013-12-20 21:43:46 +00:00
|
|
|
<!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;
|
2013-12-10 06:55:27 +00:00
|
|
|
var todayDate = new Date();
|
|
|
|
var baseServeURL = "http://mochi.test:8888/tests/dom/downloads/tests/";
|
|
|
|
var lastKnownCurrentBytes = 0;
|
2013-12-20 21:43:46 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-10 06:55:27 +00:00
|
|
|
function checkConsistentDownloadAttributes(download) {
|
|
|
|
var href = document.getElementById("download1").getAttribute("href");
|
|
|
|
var expectedServeURL = baseServeURL + href;
|
2014-02-13 22:43:58 +00:00
|
|
|
var destinationRegEx = /test\(?[0-9]*\)?\.bin$/;
|
2013-12-10 06:55:27 +00:00
|
|
|
|
|
|
|
// bug 945323: Download path isn't honoring download attribute
|
2014-02-13 22:43:58 +00:00
|
|
|
ok(destinationRegEx.test(download.path),
|
|
|
|
"Download path '" + download.path +
|
|
|
|
"' should match '" + destinationRegEx + "' regexp.");
|
2013-12-10 06:55:27 +00:00
|
|
|
|
2014-01-09 00:21:46 +00:00
|
|
|
ok(download.startTime >= todayDate,
|
|
|
|
"Download start time should be greater than or equal to today");
|
2013-12-10 06:55:27 +00:00
|
|
|
|
2014-01-09 00:21:46 +00:00
|
|
|
is(download.error, null, "Download does not have an error");
|
2013-12-10 06:55:27 +00:00
|
|
|
|
|
|
|
is(download.url, expectedServeURL,
|
|
|
|
"Download URL = " + expectedServeURL);
|
|
|
|
ok(download.id !== null, "Download id is defined");
|
|
|
|
is(download.contentType, "application/octet-stream",
|
|
|
|
"Download content type is application/octet-stream");
|
|
|
|
}
|
|
|
|
|
2013-12-20 21:43:46 +00:00
|
|
|
function downloadChange(evt) {
|
|
|
|
var download = evt.download;
|
2013-12-10 06:55:27 +00:00
|
|
|
checkConsistentDownloadAttributes(download);
|
|
|
|
is(download.totalBytes, 1024, "Download total size is 1024 bytes");
|
|
|
|
|
|
|
|
if (download.state === "succeeded") {
|
|
|
|
is(download.currentBytes, 1024, "Download current size is 1024 bytes");
|
2013-12-20 21:43:46 +00:00
|
|
|
SimpleTest.finish();
|
2013-12-10 06:55:27 +00:00
|
|
|
} else if (download.state === "downloading") {
|
|
|
|
ok(download.currentBytes > lastKnownCurrentBytes,
|
|
|
|
"Download current size is larger than last download change event");
|
|
|
|
lastKnownCurrentBytes = download.currentBytes;
|
|
|
|
} else {
|
|
|
|
ok(false, "Unexpected download state = " + download.state);
|
2013-12-20 21:43:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-10 06:55:27 +00:00
|
|
|
function downloadStart(evt) {
|
|
|
|
var download = evt.download;
|
|
|
|
checkConsistentDownloadAttributes(download);
|
|
|
|
|
|
|
|
is(download.currentBytes, 0, "Download current size is zero");
|
|
|
|
is(download.state, "downloading", "Download state is downloading");
|
|
|
|
|
|
|
|
download.onstatechange = downloadChange;
|
|
|
|
}
|
|
|
|
|
2013-12-20 21:43:46 +00:00
|
|
|
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() {
|
2013-12-10 06:55:27 +00:00
|
|
|
navigator.mozDownloadManager.ondownloadstart = downloadStart;
|
2013-12-20 21:43:46 +00:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Click on the <a download> to start the download.
|
|
|
|
function() {
|
|
|
|
document.getElementById("download1").click();
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
next();
|
|
|
|
|
|
|
|
</script>
|
|
|
|
</pre>
|
|
|
|
</body>
|
|
|
|
</html>
|
2013-12-10 06:55:27 +00:00
|
|
|
|