|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 * http://creativecommons.org/publicdomain/zero/1.0/ |
|
3 */ |
|
4 |
|
5 // This tests simulated drag and drop of files into the add-ons manager. |
|
6 // We test with the add-ons manager in its own tab if in Firefox otherwise |
|
7 // in its own window. |
|
8 // Tests are only simulations of the drag and drop events, we cannot really do |
|
9 // this automatically. |
|
10 |
|
11 // Instead of loading ChromeUtils.js into the test scope in browser-test.js for all tests, |
|
12 // we only need ChromeUtils.js for a few files which is why we are using loadSubScript. |
|
13 var gManagerWindow; |
|
14 var ChromeUtils = {}; |
|
15 this._scriptLoader = Cc["@mozilla.org/moz/jssubscript-loader;1"]. |
|
16 getService(Ci.mozIJSSubScriptLoader); |
|
17 this._scriptLoader.loadSubScript("chrome://mochikit/content/tests/SimpleTest/ChromeUtils.js", ChromeUtils); |
|
18 |
|
19 // This listens for the next opened window and checks it is of the right url. |
|
20 // opencallback is called when the new window is fully loaded |
|
21 // closecallback is called when the window is closed |
|
22 function WindowOpenListener(url, opencallback, closecallback) { |
|
23 this.url = url; |
|
24 this.opencallback = opencallback; |
|
25 this.closecallback = closecallback; |
|
26 |
|
27 var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"] |
|
28 .getService(Components.interfaces.nsIWindowMediator); |
|
29 wm.addListener(this); |
|
30 } |
|
31 |
|
32 WindowOpenListener.prototype = { |
|
33 url: null, |
|
34 opencallback: null, |
|
35 closecallback: null, |
|
36 window: null, |
|
37 domwindow: null, |
|
38 |
|
39 handleEvent: function(event) { |
|
40 is(this.domwindow.document.location.href, this.url, "Should have opened the correct window"); |
|
41 |
|
42 this.domwindow.removeEventListener("load", this, false); |
|
43 // Allow any other load handlers to execute |
|
44 var self = this; |
|
45 executeSoon(function() { self.opencallback(self.domwindow); } ); |
|
46 }, |
|
47 |
|
48 onWindowTitleChange: function(window, title) { |
|
49 }, |
|
50 |
|
51 onOpenWindow: function(window) { |
|
52 if (this.window) |
|
53 return; |
|
54 |
|
55 this.window = window; |
|
56 this.domwindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor) |
|
57 .getInterface(Components.interfaces.nsIDOMWindow); |
|
58 this.domwindow.addEventListener("load", this, false); |
|
59 }, |
|
60 |
|
61 onCloseWindow: function(window) { |
|
62 if (this.window != window) |
|
63 return; |
|
64 |
|
65 var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"] |
|
66 .getService(Components.interfaces.nsIWindowMediator); |
|
67 wm.removeListener(this); |
|
68 this.opencallback = null; |
|
69 this.window = null; |
|
70 this.domwindow = null; |
|
71 |
|
72 // Let the window close complete |
|
73 executeSoon(this.closecallback); |
|
74 this.closecallback = null; |
|
75 } |
|
76 }; |
|
77 |
|
78 var gSawInstallNotification = false; |
|
79 var gInstallNotificationObserver = { |
|
80 observe: function(aSubject, aTopic, aData) { |
|
81 var installInfo = aSubject.QueryInterface(Ci.amIWebInstallInfo); |
|
82 isnot(installInfo.originatingWindow, null, "Notification should have non-null originatingWindow"); |
|
83 gSawInstallNotification = true; |
|
84 Services.obs.removeObserver(this, "addon-install-started"); |
|
85 } |
|
86 }; |
|
87 |
|
88 |
|
89 function test() { |
|
90 waitForExplicitFinish(); |
|
91 |
|
92 open_manager("addons://list/extension", function(aWindow) { |
|
93 gManagerWindow = aWindow; |
|
94 run_next_test(); |
|
95 }); |
|
96 } |
|
97 |
|
98 function end_test() { |
|
99 close_manager(gManagerWindow, function() { |
|
100 finish(); |
|
101 }); |
|
102 } |
|
103 |
|
104 function test_confirmation(aWindow, aExpectedURLs) { |
|
105 var list = aWindow.document.getElementById("itemList"); |
|
106 is(list.childNodes.length, aExpectedURLs.length, "Should be the right number of installs"); |
|
107 |
|
108 for (let url of aExpectedURLs) { |
|
109 let found = false; |
|
110 for (let node of list.children) { |
|
111 if (node.url == url) { |
|
112 found = true; |
|
113 break; |
|
114 } |
|
115 } |
|
116 ok(found, "Should have seen " + url + " in the list"); |
|
117 } |
|
118 |
|
119 aWindow.document.documentElement.cancelDialog(); |
|
120 } |
|
121 |
|
122 // Simulates dropping a URL onto the manager |
|
123 add_test(function() { |
|
124 var url = TESTROOT + "addons/browser_dragdrop1.xpi"; |
|
125 |
|
126 Services.obs.addObserver(gInstallNotificationObserver, |
|
127 "addon-install-started", false); |
|
128 |
|
129 new WindowOpenListener(INSTALL_URI, function(aWindow) { |
|
130 test_confirmation(aWindow, [url]); |
|
131 }, function() { |
|
132 is(gSawInstallNotification, true, "Should have seen addon-install-started notification."); |
|
133 run_next_test(); |
|
134 }); |
|
135 |
|
136 var viewContainer = gManagerWindow.document.getElementById("view-port"); |
|
137 var effect = ChromeUtils.synthesizeDrop(viewContainer, viewContainer, |
|
138 [[{type: "text/x-moz-url", data: url}]], |
|
139 "copy", gManagerWindow); |
|
140 is(effect, "copy", "Drag should be accepted"); |
|
141 }); |
|
142 |
|
143 // Simulates dropping a file onto the manager |
|
144 add_test(function() { |
|
145 var fileurl = get_addon_file_url("browser_dragdrop1.xpi"); |
|
146 |
|
147 Services.obs.addObserver(gInstallNotificationObserver, |
|
148 "addon-install-started", false); |
|
149 |
|
150 new WindowOpenListener(INSTALL_URI, function(aWindow) { |
|
151 test_confirmation(aWindow, [fileurl.spec]); |
|
152 }, function() { |
|
153 is(gSawInstallNotification, true, "Should have seen addon-install-started notification."); |
|
154 run_next_test(); |
|
155 }); |
|
156 |
|
157 var viewContainer = gManagerWindow.document.getElementById("view-port"); |
|
158 var effect = ChromeUtils.synthesizeDrop(viewContainer, viewContainer, |
|
159 [[{type: "application/x-moz-file", data: fileurl.file}]], |
|
160 "copy", gManagerWindow); |
|
161 is(effect, "copy", "Drag should be accepted"); |
|
162 }); |
|
163 |
|
164 // Simulates dropping two urls onto the manager |
|
165 add_test(function() { |
|
166 var url1 = TESTROOT + "addons/browser_dragdrop1.xpi"; |
|
167 var url2 = TESTROOT2 + "addons/browser_dragdrop2.xpi"; |
|
168 |
|
169 Services.obs.addObserver(gInstallNotificationObserver, |
|
170 "addon-install-started", false); |
|
171 |
|
172 new WindowOpenListener(INSTALL_URI, function(aWindow) { |
|
173 test_confirmation(aWindow, [url1, url2]); |
|
174 }, function() { |
|
175 is(gSawInstallNotification, true, "Should have seen addon-install-started notification."); |
|
176 run_next_test(); |
|
177 }); |
|
178 |
|
179 var viewContainer = gManagerWindow.document.getElementById("view-port"); |
|
180 var effect = ChromeUtils.synthesizeDrop(viewContainer, viewContainer, |
|
181 [[{type: "text/x-moz-url", data: url1}], |
|
182 [{type: "text/x-moz-url", data: url2}]], |
|
183 "copy", gManagerWindow); |
|
184 is(effect, "copy", "Drag should be accepted"); |
|
185 }); |
|
186 |
|
187 // Simulates dropping two files onto the manager |
|
188 add_test(function() { |
|
189 var fileurl1 = get_addon_file_url("browser_dragdrop1.xpi"); |
|
190 var fileurl2 = get_addon_file_url("browser_dragdrop2.xpi"); |
|
191 |
|
192 Services.obs.addObserver(gInstallNotificationObserver, |
|
193 "addon-install-started", false); |
|
194 |
|
195 new WindowOpenListener(INSTALL_URI, function(aWindow) { |
|
196 test_confirmation(aWindow, [fileurl1.spec, fileurl2.spec]); |
|
197 }, function() { |
|
198 is(gSawInstallNotification, true, "Should have seen addon-install-started notification."); |
|
199 run_next_test(); |
|
200 }); |
|
201 |
|
202 var viewContainer = gManagerWindow.document.getElementById("view-port"); |
|
203 var effect = ChromeUtils.synthesizeDrop(viewContainer, viewContainer, |
|
204 [[{type: "application/x-moz-file", data: fileurl1.file}], |
|
205 [{type: "application/x-moz-file", data: fileurl2.file}]], |
|
206 "copy", gManagerWindow); |
|
207 is(effect, "copy", "Drag should be accepted"); |
|
208 }); |
|
209 |
|
210 // Simulates dropping a file and a url onto the manager (weird, but should still work) |
|
211 add_test(function() { |
|
212 var url = TESTROOT + "addons/browser_dragdrop1.xpi"; |
|
213 var fileurl = get_addon_file_url("browser_dragdrop2.xpi"); |
|
214 |
|
215 Services.obs.addObserver(gInstallNotificationObserver, |
|
216 "addon-install-started", false); |
|
217 |
|
218 new WindowOpenListener(INSTALL_URI, function(aWindow) { |
|
219 test_confirmation(aWindow, [url, fileurl.spec]); |
|
220 }, function() { |
|
221 is(gSawInstallNotification, true, "Should have seen addon-install-started notification."); |
|
222 run_next_test(); |
|
223 }); |
|
224 |
|
225 var viewContainer = gManagerWindow.document.getElementById("view-port"); |
|
226 var effect = ChromeUtils.synthesizeDrop(viewContainer, viewContainer, |
|
227 [[{type: "text/x-moz-url", data: url}], |
|
228 [{type: "application/x-moz-file", data: fileurl.file}]], |
|
229 "copy", gManagerWindow); |
|
230 is(effect, "copy", "Drag should be accepted"); |
|
231 }); |