toolkit/components/downloads/test/unit/test_bug_406857.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:2a4e90b07a0f
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 /**
6 * Test bug 406857 to make sure a download's referrer doesn't disappear when
7 * retrying the download.
8 */
9
10 function run_test()
11 {
12 if (oldDownloadManagerDisabled()) {
13 return;
14 }
15
16 let dm = Cc["@mozilla.org/download-manager;1"].
17 getService(Ci.nsIDownloadManager);
18 let db = dm.DBConnection;
19 var httpserv = new HttpServer();
20 httpserv.start(-1);
21
22 let stmt = db.createStatement(
23 "INSERT INTO moz_downloads (source, target, state, referrer) " +
24 "VALUES (?1, ?2, ?3, ?4)");
25
26 // Download from the test http server
27 stmt.bindByIndex(0, "http://localhost:"+ httpserv.identity.primaryPort +
28 "/httpd.js");
29
30 // Download to a temp local file
31 let file = Cc["@mozilla.org/file/directory_service;1"].
32 getService(Ci.nsIProperties).get("TmpD", Ci.nsIFile);
33 file.append("retry");
34 file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
35 stmt.bindByIndex(1, Cc["@mozilla.org/network/io-service;1"].
36 getService(Ci.nsIIOService).newFileURI(file).spec);
37
38 // Start it as canceled
39 stmt.bindByIndex(2, dm.DOWNLOAD_CANCELED);
40
41 // Add a referrer to make sure it doesn't disappear
42 let referrer = "http://referrer.goes/here";
43 stmt.bindByIndex(3, referrer);
44
45 // Add it!
46 stmt.execute();
47 stmt.finalize();
48
49 let listener = {
50 onDownloadStateChange: function(aState, aDownload)
51 {
52 switch (aDownload.state) {
53 case dm.DOWNLOAD_DOWNLOADING:
54 do_check_eq(aDownload.referrer.spec, referrer);
55 break;
56 case dm.DOWNLOAD_FINISHED:
57 do_check_eq(aDownload.referrer.spec, referrer);
58
59 dm.removeListener(listener);
60 try { file.remove(false); } catch(e) { /* stupid windows box */ }
61 httpserv.stop(do_test_finished);
62 break;
63 case dm.DOWNLOAD_FAILED:
64 case dm.DOWNLOAD_CANCELED:
65 httpserv.stop(function () {});
66 do_throw("Unexpected download state change received, state: " +
67 aDownload.state);
68 break;
69 }
70 }
71 };
72 dm.addListener(listener);
73
74 // Retry the download, and wait.
75 dm.retryDownload(1);
76
77 do_test_pending();
78 }

mercurial