michael@0: /* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: Components.utils.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: function test() { michael@0: waitForExplicitFinish(); michael@0: michael@0: // network.proxy.type needs to be backed up and restored because mochitest michael@0: // changes this setting from the default michael@0: let oldNetworkProxyType = Services.prefs.getIntPref("network.proxy.type"); michael@0: registerCleanupFunction(function() { michael@0: Services.prefs.setIntPref("network.proxy.type", oldNetworkProxyType); michael@0: Services.prefs.clearUserPref("network.proxy.no_proxies_on"); michael@0: Services.prefs.clearUserPref("browser.preferences.instantApply"); michael@0: }); michael@0: michael@0: let connectionURL = "chrome://browser/content/preferences/connection.xul"; michael@0: let windowWatcher = Services.ww; michael@0: michael@0: // instantApply must be true, otherwise connection dialog won't save michael@0: // when opened from in-content prefs michael@0: Services.prefs.setBoolPref("browser.preferences.instantApply", true); michael@0: michael@0: // this observer is registered after the pref tab loads michael@0: let observer = { michael@0: observe: function(aSubject, aTopic, aData) { michael@0: if (aTopic == "domwindowopened") { michael@0: // when connection window loads, run tests and acceptDialog() michael@0: let win = aSubject.QueryInterface(Components.interfaces.nsIDOMWindow); michael@0: win.addEventListener("load", function winLoadListener() { michael@0: win.removeEventListener("load", winLoadListener, false); michael@0: if (win.location.href == connectionURL) { michael@0: ok(true, "connection window opened"); michael@0: runConnectionTests(win); michael@0: win.document.documentElement.acceptDialog(); michael@0: } michael@0: }, false); michael@0: } else if (aTopic == "domwindowclosed") { michael@0: // finish up when connection window closes michael@0: let win = aSubject.QueryInterface(Components.interfaces.nsIDOMWindow); michael@0: if (win.location.href == connectionURL) { michael@0: windowWatcher.unregisterNotification(observer); michael@0: ok(true, "connection window closed"); michael@0: // runConnectionTests will have changed this pref - make sure it was michael@0: // sanitized correctly when the dialog was accepted michael@0: is(Services.prefs.getCharPref("network.proxy.no_proxies_on"), michael@0: ".a.com,.b.com,.c.com", "no_proxies_on pref has correct value"); michael@0: gBrowser.removeCurrentTab(); michael@0: finish(); michael@0: } michael@0: } michael@0: } michael@0: }; michael@0: michael@0: /* michael@0: The connection dialog alone won't save onaccept since it uses type="child", michael@0: so it has to be opened as a sub dialog of the main pref tab. michael@0: Open the main tab here. michael@0: */ michael@0: open_preferences(function tabOpened(aContentWindow) { michael@0: is(gBrowser.currentURI.spec, "about:preferences", "about:preferences loaded"); michael@0: windowWatcher.registerNotification(observer); michael@0: gBrowser.contentWindow.gAdvancedPane.showConnections(); michael@0: }); michael@0: } michael@0: michael@0: // run a bunch of tests on the window containing connection.xul michael@0: function runConnectionTests(win) { michael@0: let doc = win.document; michael@0: let networkProxyNone = doc.getElementById("networkProxyNone"); michael@0: let networkProxyNonePref = doc.getElementById("network.proxy.no_proxies_on"); michael@0: let networkProxyTypePref = doc.getElementById("network.proxy.type"); michael@0: michael@0: // make sure the networkProxyNone textbox is formatted properly michael@0: is(networkProxyNone.getAttribute("multiline"), "true", michael@0: "networkProxyNone textbox is multiline"); michael@0: is(networkProxyNone.getAttribute("rows"), "2", michael@0: "networkProxyNone textbox has two rows"); michael@0: michael@0: // check if sanitizing the given input for the no_proxies_on pref results in michael@0: // expected string michael@0: function testSanitize(input, expected, errorMessage) { michael@0: networkProxyNonePref.value = input; michael@0: win.gConnectionsDialog.sanitizeNoProxiesPref(); michael@0: is(networkProxyNonePref.value, expected, errorMessage); michael@0: } michael@0: michael@0: // change this pref so proxy exceptions are actually configurable michael@0: networkProxyTypePref.value = 1; michael@0: is(networkProxyNone.disabled, false, "networkProxyNone textbox is enabled"); michael@0: michael@0: testSanitize(".a.com", ".a.com", michael@0: "sanitize doesn't mess up single filter"); michael@0: testSanitize(".a.com, .b.com, .c.com", ".a.com, .b.com, .c.com", michael@0: "sanitize doesn't mess up multiple comma/space sep filters"); michael@0: testSanitize(".a.com\n.b.com", ".a.com,.b.com", michael@0: "sanitize turns line break into comma"); michael@0: testSanitize(".a.com,\n.b.com", ".a.com,.b.com", michael@0: "sanitize doesn't add duplicate comma after comma"); michael@0: testSanitize(".a.com\n,.b.com", ".a.com,.b.com", michael@0: "sanitize doesn't add duplicate comma before comma"); michael@0: testSanitize(".a.com,\n,.b.com", ".a.com,,.b.com", michael@0: "sanitize doesn't add duplicate comma surrounded by commas"); michael@0: testSanitize(".a.com, \n.b.com", ".a.com, .b.com", michael@0: "sanitize doesn't add comma after comma/space"); michael@0: testSanitize(".a.com\n .b.com", ".a.com, .b.com", michael@0: "sanitize adds comma before space"); michael@0: testSanitize(".a.com\n\n\n;;\n;\n.b.com", ".a.com,.b.com", michael@0: "sanitize only adds one comma per substring of bad chars"); michael@0: testSanitize(".a.com,,.b.com", ".a.com,,.b.com", michael@0: "duplicate commas from user are untouched"); michael@0: testSanitize(".a.com\n.b.com\n.c.com,\n.d.com,\n.e.com", michael@0: ".a.com,.b.com,.c.com,.d.com,.e.com", michael@0: "sanitize replaces things globally"); michael@0: michael@0: // will check that this was sanitized properly after window closes michael@0: networkProxyNonePref.value = ".a.com;.b.com\n.c.com"; michael@0: }