toolkit/mozapps/extensions/test/browser/browser_bug567127.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial