|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 * http://creativecommons.org/publicdomain/zero/1.0/ |
|
3 */ |
|
4 |
|
5 // Tests bug 567127 - Add install button to the add-ons manager |
|
6 |
|
7 var MockFilePicker = SpecialPowers.MockFilePicker; |
|
8 MockFilePicker.init(window); |
|
9 |
|
10 var gManagerWindow; |
|
11 var gSawInstallNotification = false; |
|
12 |
|
13 // This listens for the next opened window and checks it is of the right url. |
|
14 // opencallback is called when the new window is fully loaded |
|
15 // closecallback is called when the window is closed |
|
16 function WindowOpenListener(url, opencallback, closecallback) { |
|
17 this.url = url; |
|
18 this.opencallback = opencallback; |
|
19 this.closecallback = closecallback; |
|
20 |
|
21 var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"] |
|
22 .getService(Components.interfaces.nsIWindowMediator); |
|
23 wm.addListener(this); |
|
24 } |
|
25 |
|
26 WindowOpenListener.prototype = { |
|
27 url: null, |
|
28 opencallback: null, |
|
29 closecallback: null, |
|
30 window: null, |
|
31 domwindow: null, |
|
32 |
|
33 handleEvent: function(event) { |
|
34 is(this.domwindow.document.location.href, this.url, "Should have opened the correct window"); |
|
35 |
|
36 this.domwindow.removeEventListener("load", this, false); |
|
37 // Allow any other load handlers to execute |
|
38 var self = this; |
|
39 executeSoon(function() { self.opencallback(self.domwindow); } ); |
|
40 }, |
|
41 |
|
42 onWindowTitleChange: function(window, title) { |
|
43 }, |
|
44 |
|
45 onOpenWindow: function(window) { |
|
46 if (this.window) |
|
47 return; |
|
48 |
|
49 this.window = window; |
|
50 this.domwindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor) |
|
51 .getInterface(Components.interfaces.nsIDOMWindow); |
|
52 this.domwindow.addEventListener("load", this, false); |
|
53 }, |
|
54 |
|
55 onCloseWindow: function(window) { |
|
56 if (this.window != window) |
|
57 return; |
|
58 |
|
59 var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"] |
|
60 .getService(Components.interfaces.nsIWindowMediator); |
|
61 wm.removeListener(this); |
|
62 this.opencallback = null; |
|
63 this.window = null; |
|
64 this.domwindow = null; |
|
65 |
|
66 // Let the window close complete |
|
67 executeSoon(this.closecallback); |
|
68 this.closecallback = null; |
|
69 } |
|
70 }; |
|
71 |
|
72 |
|
73 var gInstallNotificationObserver = { |
|
74 observe: function(aSubject, aTopic, aData) { |
|
75 var installInfo = aSubject.QueryInterface(Ci.amIWebInstallInfo); |
|
76 isnot(installInfo.originatingWindow, null, "Notification should have non-null originatingWindow"); |
|
77 gSawInstallNotification = true; |
|
78 Services.obs.removeObserver(this, "addon-install-started"); |
|
79 } |
|
80 }; |
|
81 |
|
82 |
|
83 function test_confirmation(aWindow, aExpectedURLs) { |
|
84 var list = aWindow.document.getElementById("itemList"); |
|
85 is(list.childNodes.length, aExpectedURLs.length, "Should be the right number of installs"); |
|
86 |
|
87 for (let url of aExpectedURLs) { |
|
88 let found = false; |
|
89 for (let node of list.children) { |
|
90 if (node.url == url) { |
|
91 found = true; |
|
92 break; |
|
93 } |
|
94 } |
|
95 ok(found, "Should have seen " + url + " in the list"); |
|
96 } |
|
97 |
|
98 aWindow.document.documentElement.cancelDialog(); |
|
99 } |
|
100 |
|
101 add_task(function* test_install_from_file() { |
|
102 gManagerWindow = yield open_manager("addons://list/extension"); |
|
103 |
|
104 var filePaths = [ |
|
105 get_addon_file_url("browser_bug567127_1.xpi"), |
|
106 get_addon_file_url("browser_bug567127_2.xpi") |
|
107 ]; |
|
108 MockFilePicker.returnFiles = filePaths.map(function(aPath) aPath.file); |
|
109 |
|
110 Services.obs.addObserver(gInstallNotificationObserver, |
|
111 "addon-install-started", false); |
|
112 |
|
113 // Set handler that executes the core test after the window opens, |
|
114 // and resolves the promise when the window closes |
|
115 let pInstallURIClosed = new Promise((resolve, reject) => { |
|
116 new WindowOpenListener(INSTALL_URI, function(aWindow) { |
|
117 try { |
|
118 test_confirmation(aWindow, filePaths.map(function(aPath) aPath.spec)); |
|
119 } catch(e) { |
|
120 reject(e); |
|
121 } |
|
122 }, resolve); |
|
123 }); |
|
124 |
|
125 gManagerWindow.gViewController.doCommand("cmd_installFromFile"); |
|
126 |
|
127 yield pInstallURIClosed; |
|
128 |
|
129 is(gSawInstallNotification, true, "Should have seen addon-install-started notification."); |
|
130 |
|
131 MockFilePicker.cleanup(); |
|
132 yield close_manager(gManagerWindow); |
|
133 }); |