Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 /* Any copyright is dedicated to the Public Domain.
2 * http://creativecommons.org/publicdomain/zero/1.0/
3 */
5 // Tests bug 567127 - Add install button to the add-ons manager
7 var MockFilePicker = SpecialPowers.MockFilePicker;
8 MockFilePicker.init(window);
10 var gManagerWindow;
11 var gSawInstallNotification = false;
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;
21 var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
22 .getService(Components.interfaces.nsIWindowMediator);
23 wm.addListener(this);
24 }
26 WindowOpenListener.prototype = {
27 url: null,
28 opencallback: null,
29 closecallback: null,
30 window: null,
31 domwindow: null,
33 handleEvent: function(event) {
34 is(this.domwindow.document.location.href, this.url, "Should have opened the correct window");
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 },
42 onWindowTitleChange: function(window, title) {
43 },
45 onOpenWindow: function(window) {
46 if (this.window)
47 return;
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 },
55 onCloseWindow: function(window) {
56 if (this.window != window)
57 return;
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;
66 // Let the window close complete
67 executeSoon(this.closecallback);
68 this.closecallback = null;
69 }
70 };
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 };
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");
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 }
98 aWindow.document.documentElement.cancelDialog();
99 }
101 add_task(function* test_install_from_file() {
102 gManagerWindow = yield open_manager("addons://list/extension");
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);
110 Services.obs.addObserver(gInstallNotificationObserver,
111 "addon-install-started", false);
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 });
125 gManagerWindow.gViewController.doCommand("cmd_installFromFile");
127 yield pInstallURIClosed;
129 is(gSawInstallNotification, true, "Should have seen addon-install-started notification.");
131 MockFilePicker.cleanup();
132 yield close_manager(gManagerWindow);
133 });