michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ michael@0: */ michael@0: michael@0: // Tests bug 567127 - Add install button to the add-ons manager michael@0: michael@0: var MockFilePicker = SpecialPowers.MockFilePicker; michael@0: MockFilePicker.init(window); michael@0: michael@0: var gManagerWindow; michael@0: var gSawInstallNotification = false; michael@0: michael@0: // This listens for the next opened window and checks it is of the right url. michael@0: // opencallback is called when the new window is fully loaded michael@0: // closecallback is called when the window is closed michael@0: function WindowOpenListener(url, opencallback, closecallback) { michael@0: this.url = url; michael@0: this.opencallback = opencallback; michael@0: this.closecallback = closecallback; michael@0: michael@0: var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"] michael@0: .getService(Components.interfaces.nsIWindowMediator); michael@0: wm.addListener(this); michael@0: } michael@0: michael@0: WindowOpenListener.prototype = { michael@0: url: null, michael@0: opencallback: null, michael@0: closecallback: null, michael@0: window: null, michael@0: domwindow: null, michael@0: michael@0: handleEvent: function(event) { michael@0: is(this.domwindow.document.location.href, this.url, "Should have opened the correct window"); michael@0: michael@0: this.domwindow.removeEventListener("load", this, false); michael@0: // Allow any other load handlers to execute michael@0: var self = this; michael@0: executeSoon(function() { self.opencallback(self.domwindow); } ); michael@0: }, michael@0: michael@0: onWindowTitleChange: function(window, title) { michael@0: }, michael@0: michael@0: onOpenWindow: function(window) { michael@0: if (this.window) michael@0: return; michael@0: michael@0: this.window = window; michael@0: this.domwindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor) michael@0: .getInterface(Components.interfaces.nsIDOMWindow); michael@0: this.domwindow.addEventListener("load", this, false); michael@0: }, michael@0: michael@0: onCloseWindow: function(window) { michael@0: if (this.window != window) michael@0: return; michael@0: michael@0: var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"] michael@0: .getService(Components.interfaces.nsIWindowMediator); michael@0: wm.removeListener(this); michael@0: this.opencallback = null; michael@0: this.window = null; michael@0: this.domwindow = null; michael@0: michael@0: // Let the window close complete michael@0: executeSoon(this.closecallback); michael@0: this.closecallback = null; michael@0: } michael@0: }; michael@0: michael@0: michael@0: var gInstallNotificationObserver = { michael@0: observe: function(aSubject, aTopic, aData) { michael@0: var installInfo = aSubject.QueryInterface(Ci.amIWebInstallInfo); michael@0: isnot(installInfo.originatingWindow, null, "Notification should have non-null originatingWindow"); michael@0: gSawInstallNotification = true; michael@0: Services.obs.removeObserver(this, "addon-install-started"); michael@0: } michael@0: }; michael@0: michael@0: michael@0: function test_confirmation(aWindow, aExpectedURLs) { michael@0: var list = aWindow.document.getElementById("itemList"); michael@0: is(list.childNodes.length, aExpectedURLs.length, "Should be the right number of installs"); michael@0: michael@0: for (let url of aExpectedURLs) { michael@0: let found = false; michael@0: for (let node of list.children) { michael@0: if (node.url == url) { michael@0: found = true; michael@0: break; michael@0: } michael@0: } michael@0: ok(found, "Should have seen " + url + " in the list"); michael@0: } michael@0: michael@0: aWindow.document.documentElement.cancelDialog(); michael@0: } michael@0: michael@0: add_task(function* test_install_from_file() { michael@0: gManagerWindow = yield open_manager("addons://list/extension"); michael@0: michael@0: var filePaths = [ michael@0: get_addon_file_url("browser_bug567127_1.xpi"), michael@0: get_addon_file_url("browser_bug567127_2.xpi") michael@0: ]; michael@0: MockFilePicker.returnFiles = filePaths.map(function(aPath) aPath.file); michael@0: michael@0: Services.obs.addObserver(gInstallNotificationObserver, michael@0: "addon-install-started", false); michael@0: michael@0: // Set handler that executes the core test after the window opens, michael@0: // and resolves the promise when the window closes michael@0: let pInstallURIClosed = new Promise((resolve, reject) => { michael@0: new WindowOpenListener(INSTALL_URI, function(aWindow) { michael@0: try { michael@0: test_confirmation(aWindow, filePaths.map(function(aPath) aPath.spec)); michael@0: } catch(e) { michael@0: reject(e); michael@0: } michael@0: }, resolve); michael@0: }); michael@0: michael@0: gManagerWindow.gViewController.doCommand("cmd_installFromFile"); michael@0: michael@0: yield pInstallURIClosed; michael@0: michael@0: is(gSawInstallNotification, true, "Should have seen addon-install-started notification."); michael@0: michael@0: MockFilePicker.cleanup(); michael@0: yield close_manager(gManagerWindow); michael@0: });