|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim: set ts=2 et sw=2 tw=80: */ |
|
3 /* Any copyright is dedicated to the Public Domain. |
|
4 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
5 |
|
6 /** |
|
7 * Provides infrastructure for automated download components tests. |
|
8 */ |
|
9 |
|
10 //////////////////////////////////////////////////////////////////////////////// |
|
11 //// Globals |
|
12 |
|
13 XPCOMUtils.defineLazyModuleGetter(this, "Downloads", |
|
14 "resource://gre/modules/Downloads.jsm"); |
|
15 XPCOMUtils.defineLazyModuleGetter(this, "DownloadsCommon", |
|
16 "resource:///modules/DownloadsCommon.jsm"); |
|
17 XPCOMUtils.defineLazyModuleGetter(this, "FileUtils", |
|
18 "resource://gre/modules/FileUtils.jsm"); |
|
19 XPCOMUtils.defineLazyModuleGetter(this, "Promise", |
|
20 "resource://gre/modules/Promise.jsm"); |
|
21 XPCOMUtils.defineLazyModuleGetter(this, "Task", |
|
22 "resource://gre/modules/Task.jsm"); |
|
23 const nsIDM = Ci.nsIDownloadManager; |
|
24 |
|
25 let gTestTargetFile = FileUtils.getFile("TmpD", ["dm-ui-test.file"]); |
|
26 gTestTargetFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE); |
|
27 registerCleanupFunction(function () { |
|
28 gTestTargetFile.remove(false); |
|
29 }); |
|
30 |
|
31 //////////////////////////////////////////////////////////////////////////////// |
|
32 //// Infrastructure |
|
33 |
|
34 function test() |
|
35 { |
|
36 waitForExplicitFinish(); |
|
37 Task.spawn(test_task).then(null, ex => ok(false, ex)).then(finish); |
|
38 } |
|
39 |
|
40 //////////////////////////////////////////////////////////////////////////////// |
|
41 //// Asynchronous support subroutines |
|
42 |
|
43 function promiseFocus() |
|
44 { |
|
45 let deferred = Promise.defer(); |
|
46 waitForFocus(deferred.resolve); |
|
47 return deferred.promise; |
|
48 } |
|
49 |
|
50 function promisePanelOpened() |
|
51 { |
|
52 let deferred = Promise.defer(); |
|
53 |
|
54 if (DownloadsPanel.panel && DownloadsPanel.panel.state == "open") { |
|
55 return deferred.resolve(); |
|
56 } |
|
57 |
|
58 // Hook to wait until the panel is shown. |
|
59 let originalOnPopupShown = DownloadsPanel.onPopupShown; |
|
60 DownloadsPanel.onPopupShown = function () { |
|
61 DownloadsPanel.onPopupShown = originalOnPopupShown; |
|
62 originalOnPopupShown.apply(this, arguments); |
|
63 |
|
64 // Defer to the next tick of the event loop so that we don't continue |
|
65 // processing during the DOM event handler itself. |
|
66 setTimeout(deferred.resolve, 0); |
|
67 }; |
|
68 |
|
69 return deferred.promise; |
|
70 } |
|
71 |
|
72 function task_resetState() |
|
73 { |
|
74 // Remove all downloads. |
|
75 let publicList = yield Downloads.getList(Downloads.PUBLIC); |
|
76 let downloads = yield publicList.getAll(); |
|
77 for (let download of downloads) { |
|
78 publicList.remove(download); |
|
79 yield download.finalize(true); |
|
80 } |
|
81 |
|
82 DownloadsPanel.hidePanel(); |
|
83 |
|
84 yield promiseFocus(); |
|
85 } |
|
86 |
|
87 function task_addDownloads(aItems) |
|
88 { |
|
89 let startTimeMs = Date.now(); |
|
90 |
|
91 let publicList = yield Downloads.getList(Downloads.PUBLIC); |
|
92 for (let item of aItems) { |
|
93 publicList.add(yield Downloads.createDownload({ |
|
94 source: "http://www.example.com/test-download.txt", |
|
95 target: gTestTargetFile, |
|
96 succeeded: item.state == nsIDM.DOWNLOAD_FINISHED, |
|
97 canceled: item.state == nsIDM.DOWNLOAD_CANCELED || |
|
98 item.state == nsIDM.DOWNLOAD_PAUSED, |
|
99 error: item.state == nsIDM.DOWNLOAD_FAILED ? new Error("Failed.") : null, |
|
100 hasPartialData: item.state == nsIDM.DOWNLOAD_PAUSED, |
|
101 startTime: new Date(startTimeMs++), |
|
102 })); |
|
103 } |
|
104 } |
|
105 |
|
106 function task_openPanel() |
|
107 { |
|
108 yield promiseFocus(); |
|
109 |
|
110 let promise = promisePanelOpened(); |
|
111 DownloadsPanel.showPanel(); |
|
112 yield promise; |
|
113 } |