1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/passwordmgr/test/prompt_common.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,72 @@ 1.4 +var Ci = SpecialPowers.Ci; 1.5 +ok(Ci != null, "Access Ci"); 1.6 +var Cc = SpecialPowers.Cc; 1.7 +ok(Cc != null, "Access Cc"); 1.8 + 1.9 +var didDialog; 1.10 + 1.11 +var timer; // keep in outer scope so it's not GC'd before firing 1.12 +function startCallbackTimer() { 1.13 + didDialog = false; 1.14 + 1.15 + // Delay before the callback twiddles the prompt. 1.16 + const dialogDelay = 10; 1.17 + 1.18 + // Use a timer to invoke a callback to twiddle the authentication dialog 1.19 + timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); 1.20 + timer.init(observer, dialogDelay, Ci.nsITimer.TYPE_ONE_SHOT); 1.21 +} 1.22 + 1.23 + 1.24 +var observer = SpecialPowers.wrapCallbackObject({ 1.25 + QueryInterface : function (iid) { 1.26 + const interfaces = [Ci.nsIObserver, 1.27 + Ci.nsISupports, Ci.nsISupportsWeakReference]; 1.28 + 1.29 + if (!interfaces.some( function(v) { return iid.equals(v) } )) 1.30 + throw SpecialPowers.Components.results.NS_ERROR_NO_INTERFACE; 1.31 + return this; 1.32 + }, 1.33 + 1.34 + observe : function (subject, topic, data) { 1.35 + var doc = getDialogDoc(); 1.36 + if (doc) 1.37 + handleDialog(doc, testNum); 1.38 + else 1.39 + startCallbackTimer(); // try again in a bit 1.40 + } 1.41 +}); 1.42 + 1.43 +function getDialogDoc() { 1.44 + // Find the <browser> which contains notifyWindow, by looking 1.45 + // through all the open windows and all the <browsers> in each. 1.46 + var wm = Cc["@mozilla.org/appshell/window-mediator;1"]. 1.47 + getService(Ci.nsIWindowMediator); 1.48 + //var enumerator = wm.getEnumerator("navigator:browser"); 1.49 + var enumerator = wm.getXULWindowEnumerator(null); 1.50 + 1.51 + while (enumerator.hasMoreElements()) { 1.52 + var win = enumerator.getNext(); 1.53 + var windowDocShell = win.QueryInterface(Ci.nsIXULWindow).docShell; 1.54 + 1.55 + var containedDocShells = windowDocShell.getDocShellEnumerator( 1.56 + Ci.nsIDocShellTreeItem.typeChrome, 1.57 + Ci.nsIDocShell.ENUMERATE_FORWARDS); 1.58 + while (containedDocShells.hasMoreElements()) { 1.59 + // Get the corresponding document for this docshell 1.60 + var childDocShell = containedDocShells.getNext(); 1.61 + // We don't want it if it's not done loading. 1.62 + if (childDocShell.busyFlags != Ci.nsIDocShell.BUSY_FLAGS_NONE) 1.63 + continue; 1.64 + var childDoc = childDocShell.QueryInterface(Ci.nsIDocShell) 1.65 + .contentViewer 1.66 + .DOMDocument; 1.67 + 1.68 + //ok(true, "Got window: " + childDoc.location.href); 1.69 + if (childDoc.location.href == "chrome://global/content/commonDialog.xul") 1.70 + return childDoc; 1.71 + } 1.72 + } 1.73 + 1.74 + return null; 1.75 +}