browser/components/preferences/in-content/tests/browser_connection_bug388287.js

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

     1 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
     2 /* Any copyright is dedicated to the Public Domain.
     3  * http://creativecommons.org/publicdomain/zero/1.0/ */
     5 Components.utils.import("resource://gre/modules/Services.jsm");
     7 // From browser/components/preferences/in-content/test/head.js
     8 function open_preferences(aCallback) {
     9   gBrowser.selectedTab = gBrowser.addTab("about:preferences");
    10   let newTabBrowser = gBrowser.getBrowserForTab(gBrowser.selectedTab);
    11   newTabBrowser.addEventListener("Initialized", function () {
    12     newTabBrowser.removeEventListener("Initialized", arguments.callee, true);
    13     aCallback(gBrowser.contentWindow);
    14   }, true);
    15 }
    17 function test() {
    18   waitForExplicitFinish();
    19   let connectionTests = runConnectionTestsGen();
    20   connectionTests.next();
    21   const connectionURL = "chrome://browser/content/preferences/connection.xul";
    22   let closeable = false;
    23   let finalTest = false;
    24   let prefWin;
    26   // The changed preferences need to be backed up and restored because this mochitest
    27   // changes them setting from the default
    28   let oldNetworkProxyType = Services.prefs.getIntPref("network.proxy.type");
    29   registerCleanupFunction(function() {
    30     Services.prefs.setIntPref("network.proxy.type", oldNetworkProxyType);
    31     Services.prefs.clearUserPref("network.proxy.share_proxy_settings");
    32     for (let proxyType of ["http", "ssl", "ftp", "socks"]) {
    33       Services.prefs.clearUserPref("network.proxy." + proxyType);
    34       Services.prefs.clearUserPref("network.proxy." + proxyType + "_port");
    35       if (proxyType == "http") {
    36         continue;
    37       }
    38       Services.prefs.clearUserPref("network.proxy.backup." + proxyType);
    39       Services.prefs.clearUserPref("network.proxy.backup." + proxyType + "_port");
    40     }
    41     try {
    42       Services.ww.unregisterNotification(observer);
    43     } catch(e) {
    44       // Do nothing, if the test was successful the above line should fail silently.
    45     }
    46   });
    48   // this observer is registered after the pref tab loads
    49   let observer = {
    50     observe: function(aSubject, aTopic, aData) {
    51       if (aTopic == "domwindowopened") {
    52         // when the connection window loads, proceed forward in test
    53         let win = aSubject.QueryInterface(Components.interfaces.nsIDOMWindow);
    54         win.addEventListener("load", function winLoadListener() {
    55           win.removeEventListener("load", winLoadListener);
    56           if (win.location.href == connectionURL) {
    57             // If this is a connection window, run the next test
    58             connectionTests.next(win);
    59           }
    60         });
    61       } else if (aTopic == "domwindowclosed") {
    62         // Check if the window should have closed, and respawn another window for further testing
    63         let win = aSubject.QueryInterface(Components.interfaces.nsIDOMWindow);
    64         if (win.location.href == connectionURL) {
    65           ok(closeable, "Connection dialog closed");
    67           // Last close event, don't respawn, and clean up
    68           if (finalTest) {
    69             Services.ww.unregisterNotification(observer);
    70             gBrowser.removeCurrentTab();
    71             finish();
    72             return;
    73           }
    75           // Open another connection pane for the next test
    76           gBrowser.contentWindow.gAdvancedPane.showConnections();
    77         }
    78       }
    79     }
    80   };
    82   // The actual tests to run, in a generator
    83   function* runConnectionTestsGen() {
    84     let doc, connectionWin, proxyTypePref, sharePref, httpPref, httpPortPref, ftpPref, ftpPortPref;
    86     // Convenient function to reset the variables for the new window
    87     function setDoc(win) {
    88       doc = win.document;
    89       connectionWin = win;
    90       proxyTypePref = doc.getElementById("network.proxy.type");
    91       sharePref = doc.getElementById("network.proxy.share_proxy_settings");
    92       httpPref = doc.getElementById("network.proxy.http");
    93       httpPortPref = doc.getElementById("network.proxy.http_port");
    94       ftpPref = doc.getElementById("network.proxy.ftp");
    95       ftpPortPref = doc.getElementById("network.proxy.ftp_port");
    96     }
    98     // This batch of tests should not close the dialog
    99     setDoc(yield null);
   101     // Testing HTTP port 0 with share on
   102     proxyTypePref.value = 1;
   103     sharePref.value = true;
   104     httpPref.value = "localhost";
   105     httpPortPref.value = 0;
   106     doc.documentElement.acceptDialog();
   108     // Testing HTTP port 0 + FTP port 80 with share off
   109     sharePref.value = false;
   110     ftpPref.value = "localhost";
   111     ftpPortPref.value = 80;
   112     doc.documentElement.acceptDialog();
   114     // Testing HTTP port 80 + FTP port 0 with share off
   115     httpPortPref.value = 80;
   116     ftpPortPref.value = 0;
   117     doc.documentElement.acceptDialog();
   119     // From now on, the dialog should close since we are giving it legitimate inputs.
   120     // The test will timeout if the onbeforeaccept kicks in erroneously.
   121     closeable = true;
   123     // Both ports 80, share on
   124     httpPortPref.value = 80;
   125     ftpPortPref.value = 80;
   126     doc.documentElement.acceptDialog();
   128     // HTTP 80, FTP 0, with share on
   129     setDoc(yield null);
   130     proxyTypePref.value = 1;
   131     sharePref.value = true;
   132     ftpPref.value = "localhost";
   133     httpPref.value = "localhost";
   134     httpPortPref.value = 80;
   135     ftpPortPref.value = 0;
   136     doc.documentElement.acceptDialog();
   138     // HTTP host empty, port 0 with share on
   139     setDoc(yield null);
   140     proxyTypePref.value = 1;
   141     sharePref.value = true;
   142     httpPref.value = "";
   143     httpPortPref.value = 0;
   144     doc.documentElement.acceptDialog();
   146     // HTTP 0, but in no proxy mode
   147     setDoc(yield null);
   148     proxyTypePref.value = 0;
   149     sharePref.value = true;
   150     httpPref.value = "localhost";
   151     httpPortPref.value = 0;
   153     // This is the final test, don't spawn another connection window
   154     finalTest = true;
   155     doc.documentElement.acceptDialog();
   156     yield null;
   157   }
   159   /*
   160    The connection dialog alone won't save onaccept since it uses type="child",
   161    so it has to be opened as a sub dialog of the main pref tab.
   162    Open the main tab here.
   163    */
   164   open_preferences(function tabOpened(aContentWindow) {
   165     Services.ww.registerNotification(observer);
   166     gBrowser.contentWindow.gAdvancedPane.showConnections();
   167   });
   168 }

mercurial