|
1 /* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 Components.utils.import("resource://gre/modules/Services.jsm"); |
|
7 |
|
8 function test() { |
|
9 waitForExplicitFinish(); |
|
10 |
|
11 // network.proxy.type needs to be backed up and restored because mochitest |
|
12 // changes this setting from the default |
|
13 let oldNetworkProxyType = Services.prefs.getIntPref("network.proxy.type"); |
|
14 registerCleanupFunction(function() { |
|
15 Services.prefs.setIntPref("network.proxy.type", oldNetworkProxyType); |
|
16 Services.prefs.clearUserPref("network.proxy.no_proxies_on"); |
|
17 Services.prefs.clearUserPref("browser.preferences.instantApply"); |
|
18 }); |
|
19 |
|
20 let connectionURL = "chrome://browser/content/preferences/connection.xul"; |
|
21 let windowWatcher = Services.ww; |
|
22 |
|
23 // instantApply must be true, otherwise connection dialog won't save |
|
24 // when opened from in-content prefs |
|
25 Services.prefs.setBoolPref("browser.preferences.instantApply", true); |
|
26 |
|
27 // this observer is registered after the pref tab loads |
|
28 let observer = { |
|
29 observe: function(aSubject, aTopic, aData) { |
|
30 if (aTopic == "domwindowopened") { |
|
31 // when connection window loads, run tests and acceptDialog() |
|
32 let win = aSubject.QueryInterface(Components.interfaces.nsIDOMWindow); |
|
33 win.addEventListener("load", function winLoadListener() { |
|
34 win.removeEventListener("load", winLoadListener, false); |
|
35 if (win.location.href == connectionURL) { |
|
36 ok(true, "connection window opened"); |
|
37 runConnectionTests(win); |
|
38 win.document.documentElement.acceptDialog(); |
|
39 } |
|
40 }, false); |
|
41 } else if (aTopic == "domwindowclosed") { |
|
42 // finish up when connection window closes |
|
43 let win = aSubject.QueryInterface(Components.interfaces.nsIDOMWindow); |
|
44 if (win.location.href == connectionURL) { |
|
45 windowWatcher.unregisterNotification(observer); |
|
46 ok(true, "connection window closed"); |
|
47 // runConnectionTests will have changed this pref - make sure it was |
|
48 // sanitized correctly when the dialog was accepted |
|
49 is(Services.prefs.getCharPref("network.proxy.no_proxies_on"), |
|
50 ".a.com,.b.com,.c.com", "no_proxies_on pref has correct value"); |
|
51 gBrowser.removeCurrentTab(); |
|
52 finish(); |
|
53 } |
|
54 } |
|
55 } |
|
56 }; |
|
57 |
|
58 /* |
|
59 The connection dialog alone won't save onaccept since it uses type="child", |
|
60 so it has to be opened as a sub dialog of the main pref tab. |
|
61 Open the main tab here. |
|
62 */ |
|
63 open_preferences(function tabOpened(aContentWindow) { |
|
64 is(gBrowser.currentURI.spec, "about:preferences", "about:preferences loaded"); |
|
65 windowWatcher.registerNotification(observer); |
|
66 gBrowser.contentWindow.gAdvancedPane.showConnections(); |
|
67 }); |
|
68 } |
|
69 |
|
70 // run a bunch of tests on the window containing connection.xul |
|
71 function runConnectionTests(win) { |
|
72 let doc = win.document; |
|
73 let networkProxyNone = doc.getElementById("networkProxyNone"); |
|
74 let networkProxyNonePref = doc.getElementById("network.proxy.no_proxies_on"); |
|
75 let networkProxyTypePref = doc.getElementById("network.proxy.type"); |
|
76 |
|
77 // make sure the networkProxyNone textbox is formatted properly |
|
78 is(networkProxyNone.getAttribute("multiline"), "true", |
|
79 "networkProxyNone textbox is multiline"); |
|
80 is(networkProxyNone.getAttribute("rows"), "2", |
|
81 "networkProxyNone textbox has two rows"); |
|
82 |
|
83 // check if sanitizing the given input for the no_proxies_on pref results in |
|
84 // expected string |
|
85 function testSanitize(input, expected, errorMessage) { |
|
86 networkProxyNonePref.value = input; |
|
87 win.gConnectionsDialog.sanitizeNoProxiesPref(); |
|
88 is(networkProxyNonePref.value, expected, errorMessage); |
|
89 } |
|
90 |
|
91 // change this pref so proxy exceptions are actually configurable |
|
92 networkProxyTypePref.value = 1; |
|
93 is(networkProxyNone.disabled, false, "networkProxyNone textbox is enabled"); |
|
94 |
|
95 testSanitize(".a.com", ".a.com", |
|
96 "sanitize doesn't mess up single filter"); |
|
97 testSanitize(".a.com, .b.com, .c.com", ".a.com, .b.com, .c.com", |
|
98 "sanitize doesn't mess up multiple comma/space sep filters"); |
|
99 testSanitize(".a.com\n.b.com", ".a.com,.b.com", |
|
100 "sanitize turns line break into comma"); |
|
101 testSanitize(".a.com,\n.b.com", ".a.com,.b.com", |
|
102 "sanitize doesn't add duplicate comma after comma"); |
|
103 testSanitize(".a.com\n,.b.com", ".a.com,.b.com", |
|
104 "sanitize doesn't add duplicate comma before comma"); |
|
105 testSanitize(".a.com,\n,.b.com", ".a.com,,.b.com", |
|
106 "sanitize doesn't add duplicate comma surrounded by commas"); |
|
107 testSanitize(".a.com, \n.b.com", ".a.com, .b.com", |
|
108 "sanitize doesn't add comma after comma/space"); |
|
109 testSanitize(".a.com\n .b.com", ".a.com, .b.com", |
|
110 "sanitize adds comma before space"); |
|
111 testSanitize(".a.com\n\n\n;;\n;\n.b.com", ".a.com,.b.com", |
|
112 "sanitize only adds one comma per substring of bad chars"); |
|
113 testSanitize(".a.com,,.b.com", ".a.com,,.b.com", |
|
114 "duplicate commas from user are untouched"); |
|
115 testSanitize(".a.com\n.b.com\n.c.com,\n.d.com,\n.e.com", |
|
116 ".a.com,.b.com,.c.com,.d.com,.e.com", |
|
117 "sanitize replaces things globally"); |
|
118 |
|
119 // will check that this was sanitized properly after window closes |
|
120 networkProxyNonePref.value = ".a.com;.b.com\n.c.com"; |
|
121 } |