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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/components/preferences/in-content/tests/browser_connection.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,121 @@
     1.4 +/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.7 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +Components.utils.import("resource://gre/modules/Services.jsm");
    1.10 +
    1.11 +function test() {
    1.12 +  waitForExplicitFinish();
    1.13 +
    1.14 +  // network.proxy.type needs to be backed up and restored because mochitest
    1.15 +  // changes this setting from the default
    1.16 +  let oldNetworkProxyType = Services.prefs.getIntPref("network.proxy.type");
    1.17 +  registerCleanupFunction(function() {
    1.18 +    Services.prefs.setIntPref("network.proxy.type", oldNetworkProxyType);
    1.19 +    Services.prefs.clearUserPref("network.proxy.no_proxies_on");
    1.20 +    Services.prefs.clearUserPref("browser.preferences.instantApply");
    1.21 +  });
    1.22 +
    1.23 +  let connectionURL = "chrome://browser/content/preferences/connection.xul";
    1.24 +  let windowWatcher = Services.ww;
    1.25 +
    1.26 +  // instantApply must be true, otherwise connection dialog won't save
    1.27 +  // when opened from in-content prefs
    1.28 +  Services.prefs.setBoolPref("browser.preferences.instantApply", true);
    1.29 +
    1.30 +  // this observer is registered after the pref tab loads
    1.31 +  let observer = {
    1.32 +    observe: function(aSubject, aTopic, aData) {
    1.33 +      if (aTopic == "domwindowopened") {
    1.34 +        // when connection window loads, run tests and acceptDialog()
    1.35 +        let win = aSubject.QueryInterface(Components.interfaces.nsIDOMWindow);
    1.36 +        win.addEventListener("load", function winLoadListener() {
    1.37 +          win.removeEventListener("load", winLoadListener, false);
    1.38 +          if (win.location.href == connectionURL) {
    1.39 +            ok(true, "connection window opened");
    1.40 +            runConnectionTests(win);
    1.41 +            win.document.documentElement.acceptDialog();
    1.42 +          }
    1.43 +        }, false);
    1.44 +      } else if (aTopic == "domwindowclosed") {
    1.45 +        // finish up when connection window closes
    1.46 +        let win = aSubject.QueryInterface(Components.interfaces.nsIDOMWindow);
    1.47 +        if (win.location.href == connectionURL) {
    1.48 +          windowWatcher.unregisterNotification(observer);
    1.49 +          ok(true, "connection window closed");
    1.50 +          // runConnectionTests will have changed this pref - make sure it was
    1.51 +          // sanitized correctly when the dialog was accepted
    1.52 +          is(Services.prefs.getCharPref("network.proxy.no_proxies_on"),
    1.53 +             ".a.com,.b.com,.c.com", "no_proxies_on pref has correct value");
    1.54 +          gBrowser.removeCurrentTab();
    1.55 +          finish();
    1.56 +        }
    1.57 +      }
    1.58 +    }
    1.59 +  };
    1.60 +
    1.61 +  /*
    1.62 +  The connection dialog alone won't save onaccept since it uses type="child",
    1.63 +  so it has to be opened as a sub dialog of the main pref tab.
    1.64 +  Open the main tab here.
    1.65 +  */
    1.66 +  open_preferences(function tabOpened(aContentWindow) {
    1.67 +    is(gBrowser.currentURI.spec, "about:preferences", "about:preferences loaded");
    1.68 +    windowWatcher.registerNotification(observer);
    1.69 +    gBrowser.contentWindow.gAdvancedPane.showConnections();
    1.70 +  });
    1.71 +}
    1.72 +
    1.73 +// run a bunch of tests on the window containing connection.xul
    1.74 +function runConnectionTests(win) {
    1.75 +  let doc = win.document;
    1.76 +  let networkProxyNone = doc.getElementById("networkProxyNone");
    1.77 +  let networkProxyNonePref = doc.getElementById("network.proxy.no_proxies_on");
    1.78 +  let networkProxyTypePref = doc.getElementById("network.proxy.type");
    1.79 +
    1.80 +  // make sure the networkProxyNone textbox is formatted properly
    1.81 +  is(networkProxyNone.getAttribute("multiline"), "true",
    1.82 +     "networkProxyNone textbox is multiline");
    1.83 +  is(networkProxyNone.getAttribute("rows"), "2",
    1.84 +     "networkProxyNone textbox has two rows");
    1.85 +
    1.86 +  // check if sanitizing the given input for the no_proxies_on pref results in
    1.87 +  // expected string
    1.88 +  function testSanitize(input, expected, errorMessage) {
    1.89 +    networkProxyNonePref.value = input;
    1.90 +    win.gConnectionsDialog.sanitizeNoProxiesPref();
    1.91 +    is(networkProxyNonePref.value, expected, errorMessage);
    1.92 +  }
    1.93 +
    1.94 +  // change this pref so proxy exceptions are actually configurable
    1.95 +  networkProxyTypePref.value = 1;
    1.96 +  is(networkProxyNone.disabled, false, "networkProxyNone textbox is enabled");
    1.97 +
    1.98 +  testSanitize(".a.com", ".a.com",
    1.99 +               "sanitize doesn't mess up single filter");
   1.100 +  testSanitize(".a.com, .b.com, .c.com", ".a.com, .b.com, .c.com",
   1.101 +               "sanitize doesn't mess up multiple comma/space sep filters");
   1.102 +  testSanitize(".a.com\n.b.com", ".a.com,.b.com",
   1.103 +               "sanitize turns line break into comma");
   1.104 +  testSanitize(".a.com,\n.b.com", ".a.com,.b.com",
   1.105 +               "sanitize doesn't add duplicate comma after comma");
   1.106 +  testSanitize(".a.com\n,.b.com", ".a.com,.b.com",
   1.107 +               "sanitize doesn't add duplicate comma before comma");
   1.108 +  testSanitize(".a.com,\n,.b.com", ".a.com,,.b.com",
   1.109 +               "sanitize doesn't add duplicate comma surrounded by commas");
   1.110 +  testSanitize(".a.com, \n.b.com", ".a.com, .b.com",
   1.111 +               "sanitize doesn't add comma after comma/space");
   1.112 +  testSanitize(".a.com\n .b.com", ".a.com, .b.com",
   1.113 +               "sanitize adds comma before space");
   1.114 +  testSanitize(".a.com\n\n\n;;\n;\n.b.com", ".a.com,.b.com",
   1.115 +               "sanitize only adds one comma per substring of bad chars");
   1.116 +  testSanitize(".a.com,,.b.com", ".a.com,,.b.com",
   1.117 +               "duplicate commas from user are untouched");
   1.118 +  testSanitize(".a.com\n.b.com\n.c.com,\n.d.com,\n.e.com",
   1.119 +               ".a.com,.b.com,.c.com,.d.com,.e.com",
   1.120 +               "sanitize replaces things globally");
   1.121 +
   1.122 +  // will check that this was sanitized properly after window closes
   1.123 +  networkProxyNonePref.value = ".a.com;.b.com\n.c.com";
   1.124 +}

mercurial