Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
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 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;
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 });
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();
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");
68 // Last close event, don't respawn
69 if(final){
70 Services.ww.unregisterNotification(observer);
71 return;
72 }
74 // Open another connection pane for the next test
75 prefWin.gAdvancedPane.showConnections();
76 }
77 }
78 }
79 }
81 // The actual tests to run, in a generator
82 function* runConnectionTestsGen() {
83 let doc, connectionWin, proxyTypePref, sharePref, httpPref, httpPortPref, ftpPref, ftpPortPref;
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 }
97 // This batch of tests should not close the dialog
98 setDoc(yield null);
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();
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();
113 // Testing HTTP port 80 + FTP port 0 with share off
114 httpPortPref.value = 80;
115 ftpPortPref.value = 0;
116 doc.documentElement.acceptDialog();
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;
122 // Both ports 80, share on
123 httpPortPref.value = 80;
124 ftpPortPref.value = 80;
125 doc.documentElement.acceptDialog();
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();
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();
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;
152 final = true; // This is the final test, don't spawn another connection window
153 doc.documentElement.acceptDialog();
154 yield null;
155 }
157 Services.ww.registerNotification(observer);
158 openDialog(preferencesURL, "Preferences",
159 "chrome,titlebar,toolbar,centerscreen,dialog=no", "paneAdvanced");
160 }