1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/mozapps/extensions/test/xpinstall/browser_concurrent_installs.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,109 @@ 1.4 +// Test that having two frames that request installs at the same time doesn't 1.5 +// cause callback ID conflicts (discussed in bug 926712) 1.6 + 1.7 +let {Promise} = Cu.import("resource://gre/modules/Promise.jsm"); 1.8 + 1.9 +let gConcurrentTabs = []; 1.10 +let gQueuedForInstall = []; 1.11 +let gResults = []; 1.12 + 1.13 +let gAddonAndWindowListener = { 1.14 + onOpenWindow: function(win) { 1.15 + var window = win.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindow); 1.16 + info("Window opened"); 1.17 + 1.18 + waitForFocus(function() { 1.19 + info("Focused!"); 1.20 + // Initially the accept button is disabled on a countdown timer 1.21 + let button = window.document.documentElement.getButton("accept"); 1.22 + button.disabled = false; 1.23 + if (gQueuedForInstall.length > 0) { 1.24 + // Start downloading the next add-on while we accept this dialog: 1.25 + installNext(); 1.26 + } 1.27 + window.document.documentElement.acceptDialog(); 1.28 + }, window); 1.29 + }, 1.30 + onCloseWindow: function(win) { }, 1.31 + onInstallEnded: function(install) { 1.32 + install.cancel(); 1.33 + }, 1.34 + QueryInterface: XPCOMUtils.generateQI([Ci.nsIWindowMediatorListener]) 1.35 +}; 1.36 + 1.37 +function installNext() { 1.38 + let tab = gQueuedForInstall.shift(); 1.39 + tab.linkedBrowser.contentDocument.getElementById("installnow").click(); 1.40 +} 1.41 + 1.42 +function winForTab(t) { 1.43 + return t.linkedBrowser.contentDocument.defaultView; 1.44 +} 1.45 + 1.46 +function test() { 1.47 + waitForExplicitFinish(); 1.48 + 1.49 + Services.prefs.setBoolPref(PREF_LOGGING_ENABLED, true); 1.50 + Services.wm.addListener(gAddonAndWindowListener); 1.51 + AddonManager.addInstallListener(gAddonAndWindowListener); 1.52 + registerCleanupFunction(function() { 1.53 + Services.wm.removeListener(gAddonAndWindowListener); 1.54 + AddonManager.removeInstallListener(gAddonAndWindowListener); 1.55 + Services.prefs.clearUserPref(PREF_LOGGING_ENABLED); 1.56 + 1.57 + Services.perms.remove("example.com", "install"); 1.58 + Services.perms.remove("example.org", "install"); 1.59 + 1.60 + while (gConcurrentTabs.length) { 1.61 + gBrowser.removeTab(gConcurrentTabs.shift()); 1.62 + } 1.63 + }); 1.64 + 1.65 + let pm = Services.perms; 1.66 + pm.add(makeURI("http://example.com/"), "install", pm.ALLOW_ACTION); 1.67 + pm.add(makeURI("http://example.org/"), "install", pm.ALLOW_ACTION); 1.68 + 1.69 + gConcurrentTabs.push(gBrowser.addTab(TESTROOT + "concurrent_installs.html")); 1.70 + gConcurrentTabs.push(gBrowser.addTab(TESTROOT2 + "concurrent_installs.html")); 1.71 + 1.72 + let promises = gConcurrentTabs.map((t) => { 1.73 + let deferred = Promise.defer(); 1.74 + t.linkedBrowser.addEventListener("load", () => { 1.75 + let win = winForTab(t); 1.76 + if (win.location.host.startsWith("example")) { 1.77 + win.wrappedJSObject.installTriggerCallback = function(rv) { 1.78 + gResults.push(rv); 1.79 + if (gResults.length == 2) { 1.80 + executeSoon(endThisTest); 1.81 + } 1.82 + }; 1.83 + deferred.resolve(); 1.84 + } 1.85 + }, true); 1.86 + return deferred.promise; 1.87 + }); 1.88 + 1.89 + Promise.all(promises).then(() => { 1.90 + gQueuedForInstall = [...gConcurrentTabs]; 1.91 + installNext(); 1.92 + }); 1.93 +} 1.94 + 1.95 +function endThisTest() { 1.96 + is(gResults.length, 2, "Should have two urls"); 1.97 + isnot(gResults[0].loc, gResults[1].loc, "Should not have results from the same page."); 1.98 + isnot(gResults[0].xpi, gResults[1].xpi, "Should not have the same XPIs."); 1.99 + for (let i = 0; i < 2; i++) { 1.100 + let {loc, xpi} = gResults[i]; 1.101 + if (loc.contains("example.org")) { 1.102 + ok(xpi.contains("example.org"), "Should get .org XPI for .org loc"); 1.103 + } else if (loc.contains("example.com")) { 1.104 + ok(xpi.contains("example.com"), "Should get .com XPI for .com loc"); 1.105 + } else { 1.106 + ok(false, "Should never get anything that isn't from example.org or example.com"); 1.107 + } 1.108 + } 1.109 + 1.110 + finish(); 1.111 +} 1.112 +