summaryrefslogtreecommitdiffstats
path: root/dom/downloads/tests/test_downloads_basic.html
blob: 051a1faa1baea66bdf9ae6a577c599c2ba20cf8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<!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;
var todayDate = new Date();
var baseServeURL = "http://mochi.test:8888/tests/dom/downloads/tests/";
var lastKnownCurrentBytes = 0;

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 checkConsistentDownloadAttributes(download) {
  var href = document.getElementById("download1").getAttribute("href");
  var expectedServeURL = baseServeURL + href;
  var destinationRegEx = /test\(?[0-9]*\)?\.bin$/;

  // bug 945323: Download path isn't honoring download attribute
  ok(destinationRegEx.test(download.path),
     "Download path '" + download.path +
     "' should match '" + destinationRegEx + "' regexp.");

  ok(download.startTime >= todayDate,
     "Download start time should be greater than or equal to today");

  is(download.error, null, "Download does not have an error");

  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");
}

function downloadChange(evt) {
  var download = evt.download;
  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");
    SimpleTest.finish();
  } else if (download.state === "downloading") {
    // Note that this case may or may not trigger, depending on whether the
    // download is initially reported with 0 bytes (we should happen) or with
    // 1024 bytes (we should not happen).  If we do happen, an additional 8
    // TEST-PASS events should be logged.
    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);
  }
}

function downloadStart(evt) {
  var download = evt.download;
  checkConsistentDownloadAttributes(download);

  // We used to check that the currentBytes was 0.  This was incorrect.  It
  // is very common to first hear about the download already at 1024 bytes.
  is(download.state, "downloading", "Download state is downloading");

  download.onstatechange = downloadChange;
}

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 = downloadStart;
      next();
    });
  },

  // Click on the <a download> to start the download.
  function() {
    document.getElementById("download1").click();
  }
];

next();

</script>
</pre>
</body>
</html>