1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/mozapps/extensions/test/browser/browser_openDialog.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,169 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ 1.6 + */ 1.7 + 1.8 +// Tests the dialog open by the Options button for addons that provide a 1.9 +// custom chrome-like protocol for optionsURL. 1.10 + 1.11 +let CustomChromeProtocol = { 1.12 + scheme: "khrome", 1.13 + defaultPort: -1, 1.14 + protocolFlags: Ci.nsIProtocolHandler.URI_DANGEROUS_TO_LOAD | 1.15 + Ci.nsIProtocolHandler.URI_IS_LOCAL_RESOURCE | 1.16 + Ci.nsIProtocolHandler.URI_NORELATIVE | 1.17 + Ci.nsIProtocolHandler.URI_NOAUTH, 1.18 + 1.19 + newURI: function CCP_newURI(aSpec, aOriginCharset, aBaseUri) { 1.20 + let uri = Cc["@mozilla.org/network/simple-uri;1"]. 1.21 + createInstance(Ci.nsIURI); 1.22 + uri.spec = aSpec; 1.23 + return uri; 1.24 + }, 1.25 + 1.26 + newChannel: function CCP_newChannel(aURI) { 1.27 + let url = "chrome:" + aURI.path; 1.28 + let ch = NetUtil.newChannel(url); 1.29 + ch.originalURI = aURI; 1.30 + return ch; 1.31 + }, 1.32 + 1.33 + allowPort: function CCP_allowPort(aPort, aScheme) { 1.34 + return false; 1.35 + }, 1.36 + 1.37 + QueryInterface: XPCOMUtils.generateQI([ 1.38 + Ci.nsIProtocolHandler 1.39 + ]), 1.40 + 1.41 + classID: Components.ID("{399cb2d1-05dd-4363-896f-63b78e008cf8}"), 1.42 + 1.43 + factory: { 1.44 + registrar: Components.manager.QueryInterface(Ci.nsIComponentRegistrar), 1.45 + 1.46 + register: function CCP_register() { 1.47 + this.registrar.registerFactory( 1.48 + CustomChromeProtocol.classID, 1.49 + "CustomChromeProtocol", 1.50 + "@mozilla.org/network/protocol;1?name=khrome", 1.51 + this 1.52 + ); 1.53 + }, 1.54 + 1.55 + unregister: function CCP_register() { 1.56 + this.registrar.unregisterFactory(CustomChromeProtocol.classID, this); 1.57 + }, 1.58 + 1.59 + // nsIFactory 1.60 + createInstance: function BNPH_createInstance(aOuter, aIID) { 1.61 + if (aOuter) { 1.62 + throw Components.Exception("Class does not allow aggregation", 1.63 + Components.results.NS_ERROR_NO_AGGREGATION); 1.64 + } 1.65 + return CustomChromeProtocol.QueryInterface(aIID); 1.66 + }, 1.67 + 1.68 + lockFactory: function BNPH_lockFactory(aLock) { 1.69 + throw Components.Exception("Function lockFactory is not implemented", 1.70 + Components.results.NS_ERROR_NOT_IMPLEMENTED); 1.71 + }, 1.72 + 1.73 + QueryInterface: XPCOMUtils.generateQI([ 1.74 + Ci.nsIFactory 1.75 + ]) 1.76 + } 1.77 +} 1.78 + 1.79 +function test() { 1.80 + waitForExplicitFinish(); 1.81 + requestLongerTimeout(2); 1.82 + 1.83 + info("Registering custom chrome-like protocol."); 1.84 + CustomChromeProtocol.factory.register(); 1.85 + registerCleanupFunction(function () CustomChromeProtocol.factory.unregister()); 1.86 + 1.87 + const ADDONS_LIST = [ 1.88 + { id: "test1@tests.mozilla.org", 1.89 + name: "Test add-on 1", 1.90 + optionsURL: CHROMEROOT + "addon_prefs.xul" }, 1.91 + { id: "test2@tests.mozilla.org", 1.92 + name: "Test add-on 2", 1.93 + optionsURL: (CHROMEROOT + "addon_prefs.xul").replace("chrome:", "khrome:") }, 1.94 + ]; 1.95 + 1.96 + var gProvider = new MockProvider(); 1.97 + gProvider.createAddons(ADDONS_LIST); 1.98 + 1.99 + open_manager("addons://list/extension", function(aManager) { 1.100 + let addonList = aManager.document.getElementById("addon-list"); 1.101 + let currentAddon; 1.102 + let instantApply = Services.prefs.getBoolPref("browser.preferences.instantApply"); 1.103 + 1.104 + function getAddonByName(aName) { 1.105 + for (let addonItem of addonList.childNodes) { 1.106 + if (addonItem.hasAttribute("name") && 1.107 + addonItem.getAttribute("name") == aName) 1.108 + return addonItem; 1.109 + } 1.110 + return null; 1.111 + } 1.112 + 1.113 + function observer(aSubject, aTopic, aData) { 1.114 + switch (aTopic) { 1.115 + case "domwindowclosed": 1.116 + // Give the preference window a chance to finish closing before 1.117 + // closing the add-ons manager. 1.118 + waitForFocus(function () { 1.119 + test_next_addon(); 1.120 + }); 1.121 + break; 1.122 + case "domwindowopened": 1.123 + let win = aSubject.QueryInterface(Ci.nsIDOMEventTarget); 1.124 + waitForFocus(function () { 1.125 + // If the openDialog privileges are wrong a new browser window 1.126 + // will open, let the test proceed (and fail) rather than timeout. 1.127 + if (win.location != currentAddon.optionsURL && 1.128 + win.location != "chrome://browser/content/browser.xul") 1.129 + return; 1.130 + 1.131 + is(win.location, currentAddon.optionsURL, 1.132 + "The correct addon pref window should have opened"); 1.133 + 1.134 + let chromeFlags = win.QueryInterface(Ci.nsIInterfaceRequestor). 1.135 + getInterface(Ci.nsIWebNavigation). 1.136 + QueryInterface(Ci.nsIDocShellTreeItem).treeOwner. 1.137 + QueryInterface(Ci.nsIInterfaceRequestor). 1.138 + getInterface(Ci.nsIXULWindow).chromeFlags; 1.139 + ok(chromeFlags & Ci.nsIWebBrowserChrome.CHROME_OPENAS_CHROME && 1.140 + (instantApply || chromeFlags & Ci.nsIWebBrowserChrome.CHROME_OPENAS_DIALOG), 1.141 + "Window was open as a chrome dialog."); 1.142 + 1.143 + win.close(); 1.144 + }, win); 1.145 + break; 1.146 + } 1.147 + } 1.148 + 1.149 + function test_next_addon() { 1.150 + currentAddon = ADDONS_LIST.shift(); 1.151 + if (!currentAddon) { 1.152 + Services.ww.unregisterNotification(observer); 1.153 + close_manager(aManager, finish); 1.154 + return; 1.155 + } 1.156 + 1.157 + info("Testing " + currentAddon.name); 1.158 + let addonItem = getAddonByName(currentAddon.name, addonList); 1.159 + let optionsBtn = 1.160 + aManager.document.getAnonymousElementByAttribute(addonItem, "anonid", 1.161 + "preferences-btn"); 1.162 + is(optionsBtn.hidden, false, "Prefs button should be visible.") 1.163 + 1.164 + addonList.ensureElementIsVisible(addonItem); 1.165 + EventUtils.synthesizeMouseAtCenter(optionsBtn, { }, aManager); 1.166 + } 1.167 + 1.168 + Services.ww.registerNotification(observer); 1.169 + test_next_addon(); 1.170 + }); 1.171 + 1.172 +}