toolkit/components/prompts/test/prompt_common.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/prompts/test/prompt_common.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,123 @@
     1.4 +const Ci = SpecialPowers.Ci;
     1.5 +const Cc = SpecialPowers.Cc;
     1.6 +ok(Ci != null, "Access Ci");
     1.7 +ok(Cc != null, "Access Cc");
     1.8 +
     1.9 +var didDialog;
    1.10 +
    1.11 +var isSelectDialog = false;
    1.12 +var isTabModal = false;
    1.13 +var usePromptService = true;
    1.14 +
    1.15 +var timer; // keep in outer scope so it's not GC'd before firing
    1.16 +function startCallbackTimer() {
    1.17 +    didDialog = false;
    1.18 +
    1.19 +    // Delay before the callback twiddles the prompt.
    1.20 +    const dialogDelay = 10;
    1.21 +
    1.22 +    // Use a timer to invoke a callback to twiddle the authentication dialog
    1.23 +    timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
    1.24 +    timer.init(observer, dialogDelay, Ci.nsITimer.TYPE_ONE_SHOT);
    1.25 +}
    1.26 +
    1.27 +
    1.28 +var observer = {
    1.29 +    QueryInterface : function (iid) {
    1.30 +        const interfaces = [Ci.nsIObserver,
    1.31 +                            Ci.nsISupports, Ci.nsISupportsWeakReference];
    1.32 +
    1.33 +        if (!interfaces.some( function(v) { return iid.equals(v) } ))
    1.34 +            throw SpecialPowers.Cr.NS_ERROR_NO_INTERFACE;
    1.35 +        return this;
    1.36 +    },
    1.37 +
    1.38 +    observe : SpecialPowers.wrapCallback(function (subject, topic, data) {
    1.39 +      try {
    1.40 +        if (isTabModal) {
    1.41 +          var promptBox = getTabModalPromptBox(window);
    1.42 +          ok(promptBox, "got tabmodal promptbox");
    1.43 +          var prompts = SpecialPowers.wrap(promptBox).listPrompts();
    1.44 +          if (prompts.length)
    1.45 +              handleDialog(prompts[0].Dialog.ui, testNum);
    1.46 +          else
    1.47 +              startCallbackTimer(); // try again in a bit
    1.48 +        } else {
    1.49 +          var doc = getDialogDoc();
    1.50 +          if (isSelectDialog && doc)
    1.51 +              handleDialog(doc, testNum);
    1.52 +          else if (doc)
    1.53 +              handleDialog(doc.defaultView.Dialog.ui, testNum);
    1.54 +          else
    1.55 +              startCallbackTimer(); // try again in a bit
    1.56 +        }
    1.57 +      } catch (e) {
    1.58 +        ok(false, "Exception thrown in the timer callback: " + e + " at " + (e.fileName || e.filename) + ":" + (e.lineNumber || e.linenumber));
    1.59 +      }
    1.60 +    })
    1.61 +};
    1.62 +
    1.63 +function getTabModalPromptBox(domWin) {
    1.64 +    var promptBox = null;
    1.65 +
    1.66 +    // Given a content DOM window, returns the chrome window it's in.
    1.67 +    function getChromeWindow(aWindow) {
    1.68 +        var chromeWin = SpecialPowers.wrap(aWindow).QueryInterface(Ci.nsIInterfaceRequestor)
    1.69 +                               .getInterface(Ci.nsIWebNavigation)
    1.70 +                               .QueryInterface(Ci.nsIDocShell)
    1.71 +                               .chromeEventHandler.ownerDocument.defaultView;
    1.72 +        return chromeWin;
    1.73 +    }
    1.74 +
    1.75 +    try {
    1.76 +        // Get the topmost window, in case we're in a frame.
    1.77 +        var promptWin = domWin.top;
    1.78 +
    1.79 +        // Get the chrome window for the content window we're using.
    1.80 +        var chromeWin = getChromeWindow(promptWin);
    1.81 +
    1.82 +        if (chromeWin.getTabModalPromptBox)
    1.83 +            promptBox = chromeWin.getTabModalPromptBox(promptWin);
    1.84 +    } catch (e) {
    1.85 +        // If any errors happen, just assume no tabmodal prompter.
    1.86 +    }
    1.87 +
    1.88 +    // Callers get confused by a wrapped promptBox here.
    1.89 +    return SpecialPowers.unwrap(promptBox);
    1.90 +}
    1.91 +
    1.92 +function getDialogDoc() {
    1.93 +  // Trudge through all the open windows, until we find the one
    1.94 +  // that has either commonDialog.xul or selectDialog.xul loaded.
    1.95 +  var wm = Cc["@mozilla.org/appshell/window-mediator;1"].
    1.96 +           getService(Ci.nsIWindowMediator);
    1.97 +  //var enumerator = wm.getEnumerator("navigator:browser");
    1.98 +  var enumerator = wm.getXULWindowEnumerator(null);
    1.99 +
   1.100 +  while (enumerator.hasMoreElements()) {
   1.101 +    var win = enumerator.getNext();
   1.102 +    var windowDocShell = win.QueryInterface(Ci.nsIXULWindow).docShell;
   1.103 +
   1.104 +    var containedDocShells = windowDocShell.getDocShellEnumerator(
   1.105 +                                      Ci.nsIDocShellTreeItem.typeChrome,
   1.106 +                                      Ci.nsIDocShell.ENUMERATE_FORWARDS);
   1.107 +    while (containedDocShells.hasMoreElements()) {
   1.108 +        // Get the corresponding document for this docshell
   1.109 +        var childDocShell = containedDocShells.getNext();
   1.110 +        // We don't want it if it's not done loading.
   1.111 +        if (childDocShell.busyFlags != Ci.nsIDocShell.BUSY_FLAGS_NONE)
   1.112 +          continue;
   1.113 +        var childDoc = childDocShell.QueryInterface(Ci.nsIDocShell)
   1.114 +                                    .contentViewer
   1.115 +                                    .DOMDocument;
   1.116 +
   1.117 +        //ok(true, "Got window: " + childDoc.location.href);
   1.118 +        if (childDoc.location.href == "chrome://global/content/commonDialog.xul")
   1.119 +          return childDoc;
   1.120 +        if (childDoc.location.href == "chrome://global/content/selectDialog.xul")
   1.121 +          return childDoc;
   1.122 +    }
   1.123 +  }
   1.124 +
   1.125 +  return null;
   1.126 +}

mercurial