|
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 // This file tests the download manager backend |
|
6 |
|
7 const nsIDownloadManager = Ci.nsIDownloadManager; |
|
8 const dm = Cc["@mozilla.org/download-manager;1"].getService(nsIDownloadManager); |
|
9 |
|
10 function test_get_download_empty_queue() |
|
11 { |
|
12 try { |
|
13 dm.getDownload(0); |
|
14 do_throw("Hey! We expect to get an excpetion with this!"); |
|
15 } catch(e) { |
|
16 do_check_eq(Components.lastResult, Cr.NS_ERROR_NOT_AVAILABLE); |
|
17 } |
|
18 } |
|
19 |
|
20 function test_connection() |
|
21 { |
|
22 print("*** DOWNLOAD MANAGER TEST - test_connection"); |
|
23 var ds = dm.DBConnection; |
|
24 |
|
25 do_check_true(ds.connectionReady); |
|
26 |
|
27 do_check_true(ds.tableExists("moz_downloads")); |
|
28 } |
|
29 |
|
30 function test_count_empty_queue() |
|
31 { |
|
32 print("*** DOWNLOAD MANAGER TEST - test_count_empty_queue"); |
|
33 do_check_eq(0, dm.activeDownloadCount); |
|
34 |
|
35 do_check_false(dm.activeDownloads.hasMoreElements()); |
|
36 } |
|
37 |
|
38 function test_canCleanUp_empty_queue() |
|
39 { |
|
40 print("*** DOWNLOAD MANAGER TEST - test_canCleanUp_empty_queue"); |
|
41 do_check_false(dm.canCleanUp); |
|
42 } |
|
43 |
|
44 function test_pauseDownload_empty_queue() |
|
45 { |
|
46 print("*** DOWNLOAD MANAGER TEST - test_pauseDownload_empty_queue"); |
|
47 try { |
|
48 dm.pauseDownload(0); |
|
49 do_throw("This should not be reached"); |
|
50 } catch (e) { |
|
51 do_check_eq(Cr.NS_ERROR_FAILURE, e.result); |
|
52 } |
|
53 } |
|
54 |
|
55 function test_resumeDownload_empty_queue() |
|
56 { |
|
57 print("*** DOWNLOAD MANAGER TEST - test_resumeDownload_empty_queue"); |
|
58 try { |
|
59 dm.resumeDownload(0); |
|
60 do_throw("This should not be reached"); |
|
61 } catch (e) { |
|
62 do_check_eq(Cr.NS_ERROR_FAILURE, e.result); |
|
63 } |
|
64 } |
|
65 |
|
66 function test_addDownload_normal() |
|
67 { |
|
68 print("*** DOWNLOAD MANAGER TEST - Testing normal download adding"); |
|
69 addDownload(httpserv); |
|
70 } |
|
71 |
|
72 function test_addDownload_cancel() |
|
73 { |
|
74 print("*** DOWNLOAD MANAGER TEST - Testing download cancel"); |
|
75 var dl = addDownload(httpserv); |
|
76 |
|
77 dm.cancelDownload(dl.id); |
|
78 |
|
79 do_check_eq(nsIDownloadManager.DOWNLOAD_CANCELED, dl.state); |
|
80 } |
|
81 |
|
82 // This test is actually ran by the observer |
|
83 function test_dm_getDownload(aDl) |
|
84 { |
|
85 // this will get it from the database |
|
86 var dl = dm.getDownload(aDl.id); |
|
87 |
|
88 do_check_eq(aDl.displayName, dl.displayName); |
|
89 } |
|
90 |
|
91 var tests = [test_get_download_empty_queue, test_connection, |
|
92 test_count_empty_queue, test_canCleanUp_empty_queue, |
|
93 test_pauseDownload_empty_queue, test_resumeDownload_empty_queue, |
|
94 test_addDownload_normal, test_addDownload_cancel]; |
|
95 |
|
96 var httpserv = null; |
|
97 function run_test() |
|
98 { |
|
99 if (oldDownloadManagerDisabled()) { |
|
100 return; |
|
101 } |
|
102 |
|
103 httpserv = new HttpServer(); |
|
104 httpserv.registerDirectory("/", do_get_cwd()); |
|
105 httpserv.start(-1); |
|
106 |
|
107 // our download listener |
|
108 var listener = { |
|
109 // this listener checks to ensure activeDownloadCount is correct. |
|
110 onDownloadStateChange: function(aState, aDownload) |
|
111 { |
|
112 do_check_eq(gDownloadCount, dm.activeDownloadCount); |
|
113 }, |
|
114 onStateChange: function(a, b, c, d, e) { }, |
|
115 onProgressChange: function(a, b, c, d, e, f, g) { }, |
|
116 onSecurityChange: function(a, b, c, d) { } |
|
117 }; |
|
118 dm.addListener(listener); |
|
119 dm.addListener(getDownloadListener()); |
|
120 |
|
121 var observer = { |
|
122 observe: function(aSubject, aTopic, aData) { |
|
123 var dl = aSubject.QueryInterface(Ci.nsIDownload); |
|
124 do_check_eq(nsIDownloadManager.DOWNLOAD_CANCELED, dl.state); |
|
125 do_check_true(dm.canCleanUp); |
|
126 test_dm_getDownload(dl); |
|
127 } |
|
128 }; |
|
129 var os = Cc["@mozilla.org/observer-service;1"] |
|
130 .getService(Ci.nsIObserverService); |
|
131 os.addObserver(observer, "dl-cancel", false); |
|
132 |
|
133 for (var i = 0; i < tests.length; i++) |
|
134 tests[i](); |
|
135 } |