1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/components/preferences/in-content/tests/browser_connection_bug388287.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,168 @@ 1.4 +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ 1.5 +/* Any copyright is dedicated to the Public Domain. 1.6 + * http://creativecommons.org/publicdomain/zero/1.0/ */ 1.7 + 1.8 +Components.utils.import("resource://gre/modules/Services.jsm"); 1.9 + 1.10 +// From browser/components/preferences/in-content/test/head.js 1.11 +function open_preferences(aCallback) { 1.12 + gBrowser.selectedTab = gBrowser.addTab("about:preferences"); 1.13 + let newTabBrowser = gBrowser.getBrowserForTab(gBrowser.selectedTab); 1.14 + newTabBrowser.addEventListener("Initialized", function () { 1.15 + newTabBrowser.removeEventListener("Initialized", arguments.callee, true); 1.16 + aCallback(gBrowser.contentWindow); 1.17 + }, true); 1.18 +} 1.19 + 1.20 +function test() { 1.21 + waitForExplicitFinish(); 1.22 + let connectionTests = runConnectionTestsGen(); 1.23 + connectionTests.next(); 1.24 + const connectionURL = "chrome://browser/content/preferences/connection.xul"; 1.25 + let closeable = false; 1.26 + let finalTest = false; 1.27 + let prefWin; 1.28 + 1.29 + // The changed preferences need to be backed up and restored because this mochitest 1.30 + // changes them setting from the default 1.31 + let oldNetworkProxyType = Services.prefs.getIntPref("network.proxy.type"); 1.32 + registerCleanupFunction(function() { 1.33 + Services.prefs.setIntPref("network.proxy.type", oldNetworkProxyType); 1.34 + Services.prefs.clearUserPref("network.proxy.share_proxy_settings"); 1.35 + for (let proxyType of ["http", "ssl", "ftp", "socks"]) { 1.36 + Services.prefs.clearUserPref("network.proxy." + proxyType); 1.37 + Services.prefs.clearUserPref("network.proxy." + proxyType + "_port"); 1.38 + if (proxyType == "http") { 1.39 + continue; 1.40 + } 1.41 + Services.prefs.clearUserPref("network.proxy.backup." + proxyType); 1.42 + Services.prefs.clearUserPref("network.proxy.backup." + proxyType + "_port"); 1.43 + } 1.44 + try { 1.45 + Services.ww.unregisterNotification(observer); 1.46 + } catch(e) { 1.47 + // Do nothing, if the test was successful the above line should fail silently. 1.48 + } 1.49 + }); 1.50 + 1.51 + // this observer is registered after the pref tab loads 1.52 + let observer = { 1.53 + observe: function(aSubject, aTopic, aData) { 1.54 + if (aTopic == "domwindowopened") { 1.55 + // when the connection window loads, proceed forward in test 1.56 + let win = aSubject.QueryInterface(Components.interfaces.nsIDOMWindow); 1.57 + win.addEventListener("load", function winLoadListener() { 1.58 + win.removeEventListener("load", winLoadListener); 1.59 + if (win.location.href == connectionURL) { 1.60 + // If this is a connection window, run the next test 1.61 + connectionTests.next(win); 1.62 + } 1.63 + }); 1.64 + } else if (aTopic == "domwindowclosed") { 1.65 + // Check if the window should have closed, and respawn another window for further testing 1.66 + let win = aSubject.QueryInterface(Components.interfaces.nsIDOMWindow); 1.67 + if (win.location.href == connectionURL) { 1.68 + ok(closeable, "Connection dialog closed"); 1.69 + 1.70 + // Last close event, don't respawn, and clean up 1.71 + if (finalTest) { 1.72 + Services.ww.unregisterNotification(observer); 1.73 + gBrowser.removeCurrentTab(); 1.74 + finish(); 1.75 + return; 1.76 + } 1.77 + 1.78 + // Open another connection pane for the next test 1.79 + gBrowser.contentWindow.gAdvancedPane.showConnections(); 1.80 + } 1.81 + } 1.82 + } 1.83 + }; 1.84 + 1.85 + // The actual tests to run, in a generator 1.86 + function* runConnectionTestsGen() { 1.87 + let doc, connectionWin, proxyTypePref, sharePref, httpPref, httpPortPref, ftpPref, ftpPortPref; 1.88 + 1.89 + // Convenient function to reset the variables for the new window 1.90 + function setDoc(win) { 1.91 + doc = win.document; 1.92 + connectionWin = win; 1.93 + proxyTypePref = doc.getElementById("network.proxy.type"); 1.94 + sharePref = doc.getElementById("network.proxy.share_proxy_settings"); 1.95 + httpPref = doc.getElementById("network.proxy.http"); 1.96 + httpPortPref = doc.getElementById("network.proxy.http_port"); 1.97 + ftpPref = doc.getElementById("network.proxy.ftp"); 1.98 + ftpPortPref = doc.getElementById("network.proxy.ftp_port"); 1.99 + } 1.100 + 1.101 + // This batch of tests should not close the dialog 1.102 + setDoc(yield null); 1.103 + 1.104 + // Testing HTTP port 0 with share on 1.105 + proxyTypePref.value = 1; 1.106 + sharePref.value = true; 1.107 + httpPref.value = "localhost"; 1.108 + httpPortPref.value = 0; 1.109 + doc.documentElement.acceptDialog(); 1.110 + 1.111 + // Testing HTTP port 0 + FTP port 80 with share off 1.112 + sharePref.value = false; 1.113 + ftpPref.value = "localhost"; 1.114 + ftpPortPref.value = 80; 1.115 + doc.documentElement.acceptDialog(); 1.116 + 1.117 + // Testing HTTP port 80 + FTP port 0 with share off 1.118 + httpPortPref.value = 80; 1.119 + ftpPortPref.value = 0; 1.120 + doc.documentElement.acceptDialog(); 1.121 + 1.122 + // From now on, the dialog should close since we are giving it legitimate inputs. 1.123 + // The test will timeout if the onbeforeaccept kicks in erroneously. 1.124 + closeable = true; 1.125 + 1.126 + // Both ports 80, share on 1.127 + httpPortPref.value = 80; 1.128 + ftpPortPref.value = 80; 1.129 + doc.documentElement.acceptDialog(); 1.130 + 1.131 + // HTTP 80, FTP 0, with share on 1.132 + setDoc(yield null); 1.133 + proxyTypePref.value = 1; 1.134 + sharePref.value = true; 1.135 + ftpPref.value = "localhost"; 1.136 + httpPref.value = "localhost"; 1.137 + httpPortPref.value = 80; 1.138 + ftpPortPref.value = 0; 1.139 + doc.documentElement.acceptDialog(); 1.140 + 1.141 + // HTTP host empty, port 0 with share on 1.142 + setDoc(yield null); 1.143 + proxyTypePref.value = 1; 1.144 + sharePref.value = true; 1.145 + httpPref.value = ""; 1.146 + httpPortPref.value = 0; 1.147 + doc.documentElement.acceptDialog(); 1.148 + 1.149 + // HTTP 0, but in no proxy mode 1.150 + setDoc(yield null); 1.151 + proxyTypePref.value = 0; 1.152 + sharePref.value = true; 1.153 + httpPref.value = "localhost"; 1.154 + httpPortPref.value = 0; 1.155 + 1.156 + // This is the final test, don't spawn another connection window 1.157 + finalTest = true; 1.158 + doc.documentElement.acceptDialog(); 1.159 + yield null; 1.160 + } 1.161 + 1.162 + /* 1.163 + The connection dialog alone won't save onaccept since it uses type="child", 1.164 + so it has to be opened as a sub dialog of the main pref tab. 1.165 + Open the main tab here. 1.166 + */ 1.167 + open_preferences(function tabOpened(aContentWindow) { 1.168 + Services.ww.registerNotification(observer); 1.169 + gBrowser.contentWindow.gAdvancedPane.showConnections(); 1.170 + }); 1.171 +}