|
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/ */ |
|
4 |
|
5 Components.utils.import("resource://gre/modules/Services.jsm"); |
|
6 |
|
7 function test() { |
|
8 waitForExplicitFinish(); |
|
9 let connectionTests = runConnectionTestsGen(); |
|
10 connectionTests.next(); |
|
11 const connectionURL = "chrome://browser/content/preferences/connection.xul"; |
|
12 const preferencesURL = "chrome://browser/content/preferences/preferences.xul"; |
|
13 let closeable = false; |
|
14 let final = false; |
|
15 let prefWin; |
|
16 |
|
17 // The changed preferences need to be backed up and restored because this mochitest |
|
18 // changes them setting from the default |
|
19 let oldNetworkProxyType = Services.prefs.getIntPref("network.proxy.type"); |
|
20 registerCleanupFunction(function() { |
|
21 Services.prefs.setIntPref("network.proxy.type", oldNetworkProxyType); |
|
22 Services.prefs.clearUserPref("network.proxy.share_proxy_settings"); |
|
23 for (let proxyType of ["http", "ssl", "ftp", "socks"]) { |
|
24 Services.prefs.clearUserPref("network.proxy." + proxyType); |
|
25 Services.prefs.clearUserPref("network.proxy." + proxyType + "_port"); |
|
26 if (proxyType == "http") { |
|
27 continue; |
|
28 } |
|
29 Services.prefs.clearUserPref("network.proxy.backup." + proxyType); |
|
30 Services.prefs.clearUserPref("network.proxy.backup." + proxyType + "_port"); |
|
31 } |
|
32 try { |
|
33 Services.ww.unregisterNotification(observer); |
|
34 } catch(e) { |
|
35 // Do nothing, if the test was successful the above line should fail silently. |
|
36 } |
|
37 }); |
|
38 |
|
39 // this observer is registered after the pref tab loads |
|
40 let observer = { |
|
41 observe: function(aSubject, aTopic, aData) { |
|
42 if (aTopic == "domwindowopened") { |
|
43 // when the connection window loads, proceed forward in test |
|
44 let win = aSubject.QueryInterface(Components.interfaces.nsIDOMWindow); |
|
45 win.addEventListener("load", function winLoadListener() { |
|
46 win.removeEventListener("load", winLoadListener); |
|
47 if (win.location.href == connectionURL) { |
|
48 // If this is a connection window, run the next test |
|
49 connectionTests.next(win); |
|
50 } else if (win.location.href == preferencesURL) { |
|
51 // If this is the preferences window, initiate the tests by showing the connection pane |
|
52 prefWin = win; |
|
53 prefWin.gAdvancedPane.showConnections(); |
|
54 |
|
55 // Since the above method immediately triggers the observer chain, |
|
56 // the cleanup below won't happen until all the tests finish successfully. |
|
57 prefWin.close(); |
|
58 Services.prefs.setIntPref("network.proxy.type",0); |
|
59 finish(); |
|
60 } |
|
61 }); |
|
62 } else if (aTopic == "domwindowclosed") { |
|
63 // Check if the window should have closed, and respawn another window for further testing |
|
64 let win = aSubject.QueryInterface(Components.interfaces.nsIDOMWindow); |
|
65 if (win.location.href == connectionURL) { |
|
66 ok(closeable, "Connection dialog closed"); |
|
67 |
|
68 // Last close event, don't respawn |
|
69 if(final){ |
|
70 Services.ww.unregisterNotification(observer); |
|
71 return; |
|
72 } |
|
73 |
|
74 // Open another connection pane for the next test |
|
75 prefWin.gAdvancedPane.showConnections(); |
|
76 } |
|
77 } |
|
78 } |
|
79 } |
|
80 |
|
81 // The actual tests to run, in a generator |
|
82 function* runConnectionTestsGen() { |
|
83 let doc, connectionWin, proxyTypePref, sharePref, httpPref, httpPortPref, ftpPref, ftpPortPref; |
|
84 |
|
85 // Convenient function to reset the variables for the new window |
|
86 function setDoc(win){ |
|
87 doc = win.document; |
|
88 connectionWin = win; |
|
89 proxyTypePref = doc.getElementById("network.proxy.type"); |
|
90 sharePref = doc.getElementById("network.proxy.share_proxy_settings"); |
|
91 httpPref = doc.getElementById("network.proxy.http"); |
|
92 httpPortPref = doc.getElementById("network.proxy.http_port"); |
|
93 ftpPref = doc.getElementById("network.proxy.ftp"); |
|
94 ftpPortPref = doc.getElementById("network.proxy.ftp_port"); |
|
95 } |
|
96 |
|
97 // This batch of tests should not close the dialog |
|
98 setDoc(yield null); |
|
99 |
|
100 // Testing HTTP port 0 with share on |
|
101 proxyTypePref.value = 1; |
|
102 sharePref.value = true; |
|
103 httpPref.value = "localhost"; |
|
104 httpPortPref.value = 0; |
|
105 doc.documentElement.acceptDialog(); |
|
106 |
|
107 // Testing HTTP port 0 + FTP port 80 with share off |
|
108 sharePref.value = false; |
|
109 ftpPref.value = "localhost"; |
|
110 ftpPortPref.value = 80; |
|
111 doc.documentElement.acceptDialog(); |
|
112 |
|
113 // Testing HTTP port 80 + FTP port 0 with share off |
|
114 httpPortPref.value = 80; |
|
115 ftpPortPref.value = 0; |
|
116 doc.documentElement.acceptDialog(); |
|
117 |
|
118 // From now on, the dialog should close since we are giving it legitimate inputs. |
|
119 // The test will timeout if the onbeforeaccept kicks in erroneously. |
|
120 closeable = true; |
|
121 |
|
122 // Both ports 80, share on |
|
123 httpPortPref.value = 80; |
|
124 ftpPortPref.value = 80; |
|
125 doc.documentElement.acceptDialog(); |
|
126 |
|
127 // HTTP 80, FTP 0, with share on |
|
128 setDoc(yield null); |
|
129 proxyTypePref.value = 1; |
|
130 sharePref.value = true; |
|
131 ftpPref.value = "localhost"; |
|
132 httpPref.value = "localhost"; |
|
133 httpPortPref.value = 80; |
|
134 ftpPortPref.value = 0; |
|
135 doc.documentElement.acceptDialog(); |
|
136 |
|
137 // HTTP host empty, port 0 with share on |
|
138 setDoc(yield null); |
|
139 proxyTypePref.value = 1; |
|
140 sharePref.value = true; |
|
141 httpPref.value = ""; |
|
142 httpPortPref.value = 0; |
|
143 doc.documentElement.acceptDialog(); |
|
144 |
|
145 // HTTP 0, but in no proxy mode |
|
146 setDoc(yield null); |
|
147 proxyTypePref.value = 0; |
|
148 sharePref.value = true; |
|
149 httpPref.value = "localhost"; |
|
150 httpPortPref.value = 0; |
|
151 |
|
152 final = true; // This is the final test, don't spawn another connection window |
|
153 doc.documentElement.acceptDialog(); |
|
154 yield null; |
|
155 } |
|
156 |
|
157 Services.ww.registerNotification(observer); |
|
158 openDialog(preferencesURL, "Preferences", |
|
159 "chrome,titlebar,toolbar,centerscreen,dialog=no", "paneAdvanced"); |
|
160 } |