1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/components/preferences/tests/browser_connection_bug388287.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,160 @@ 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 +function test() { 1.11 + waitForExplicitFinish(); 1.12 + let connectionTests = runConnectionTestsGen(); 1.13 + connectionTests.next(); 1.14 + const connectionURL = "chrome://browser/content/preferences/connection.xul"; 1.15 + const preferencesURL = "chrome://browser/content/preferences/preferences.xul"; 1.16 + let closeable = false; 1.17 + let final = false; 1.18 + let prefWin; 1.19 + 1.20 + // The changed preferences need to be backed up and restored because this mochitest 1.21 + // changes them setting from the default 1.22 + let oldNetworkProxyType = Services.prefs.getIntPref("network.proxy.type"); 1.23 + registerCleanupFunction(function() { 1.24 + Services.prefs.setIntPref("network.proxy.type", oldNetworkProxyType); 1.25 + Services.prefs.clearUserPref("network.proxy.share_proxy_settings"); 1.26 + for (let proxyType of ["http", "ssl", "ftp", "socks"]) { 1.27 + Services.prefs.clearUserPref("network.proxy." + proxyType); 1.28 + Services.prefs.clearUserPref("network.proxy." + proxyType + "_port"); 1.29 + if (proxyType == "http") { 1.30 + continue; 1.31 + } 1.32 + Services.prefs.clearUserPref("network.proxy.backup." + proxyType); 1.33 + Services.prefs.clearUserPref("network.proxy.backup." + proxyType + "_port"); 1.34 + } 1.35 + try { 1.36 + Services.ww.unregisterNotification(observer); 1.37 + } catch(e) { 1.38 + // Do nothing, if the test was successful the above line should fail silently. 1.39 + } 1.40 + }); 1.41 + 1.42 + // this observer is registered after the pref tab loads 1.43 + let observer = { 1.44 + observe: function(aSubject, aTopic, aData) { 1.45 + if (aTopic == "domwindowopened") { 1.46 + // when the connection window loads, proceed forward in test 1.47 + let win = aSubject.QueryInterface(Components.interfaces.nsIDOMWindow); 1.48 + win.addEventListener("load", function winLoadListener() { 1.49 + win.removeEventListener("load", winLoadListener); 1.50 + if (win.location.href == connectionURL) { 1.51 + // If this is a connection window, run the next test 1.52 + connectionTests.next(win); 1.53 + } else if (win.location.href == preferencesURL) { 1.54 + // If this is the preferences window, initiate the tests by showing the connection pane 1.55 + prefWin = win; 1.56 + prefWin.gAdvancedPane.showConnections(); 1.57 + 1.58 + // Since the above method immediately triggers the observer chain, 1.59 + // the cleanup below won't happen until all the tests finish successfully. 1.60 + prefWin.close(); 1.61 + Services.prefs.setIntPref("network.proxy.type",0); 1.62 + finish(); 1.63 + } 1.64 + }); 1.65 + } else if (aTopic == "domwindowclosed") { 1.66 + // Check if the window should have closed, and respawn another window for further testing 1.67 + let win = aSubject.QueryInterface(Components.interfaces.nsIDOMWindow); 1.68 + if (win.location.href == connectionURL) { 1.69 + ok(closeable, "Connection dialog closed"); 1.70 + 1.71 + // Last close event, don't respawn 1.72 + if(final){ 1.73 + Services.ww.unregisterNotification(observer); 1.74 + return; 1.75 + } 1.76 + 1.77 + // Open another connection pane for the next test 1.78 + prefWin.gAdvancedPane.showConnections(); 1.79 + } 1.80 + } 1.81 + } 1.82 + } 1.83 + 1.84 + // The actual tests to run, in a generator 1.85 + function* runConnectionTestsGen() { 1.86 + let doc, connectionWin, proxyTypePref, sharePref, httpPref, httpPortPref, ftpPref, ftpPortPref; 1.87 + 1.88 + // Convenient function to reset the variables for the new window 1.89 + function setDoc(win){ 1.90 + doc = win.document; 1.91 + connectionWin = win; 1.92 + proxyTypePref = doc.getElementById("network.proxy.type"); 1.93 + sharePref = doc.getElementById("network.proxy.share_proxy_settings"); 1.94 + httpPref = doc.getElementById("network.proxy.http"); 1.95 + httpPortPref = doc.getElementById("network.proxy.http_port"); 1.96 + ftpPref = doc.getElementById("network.proxy.ftp"); 1.97 + ftpPortPref = doc.getElementById("network.proxy.ftp_port"); 1.98 + } 1.99 + 1.100 + // This batch of tests should not close the dialog 1.101 + setDoc(yield null); 1.102 + 1.103 + // Testing HTTP port 0 with share on 1.104 + proxyTypePref.value = 1; 1.105 + sharePref.value = true; 1.106 + httpPref.value = "localhost"; 1.107 + httpPortPref.value = 0; 1.108 + doc.documentElement.acceptDialog(); 1.109 + 1.110 + // Testing HTTP port 0 + FTP port 80 with share off 1.111 + sharePref.value = false; 1.112 + ftpPref.value = "localhost"; 1.113 + ftpPortPref.value = 80; 1.114 + doc.documentElement.acceptDialog(); 1.115 + 1.116 + // Testing HTTP port 80 + FTP port 0 with share off 1.117 + httpPortPref.value = 80; 1.118 + ftpPortPref.value = 0; 1.119 + doc.documentElement.acceptDialog(); 1.120 + 1.121 + // From now on, the dialog should close since we are giving it legitimate inputs. 1.122 + // The test will timeout if the onbeforeaccept kicks in erroneously. 1.123 + closeable = true; 1.124 + 1.125 + // Both ports 80, share on 1.126 + httpPortPref.value = 80; 1.127 + ftpPortPref.value = 80; 1.128 + doc.documentElement.acceptDialog(); 1.129 + 1.130 + // HTTP 80, FTP 0, with share on 1.131 + setDoc(yield null); 1.132 + proxyTypePref.value = 1; 1.133 + sharePref.value = true; 1.134 + ftpPref.value = "localhost"; 1.135 + httpPref.value = "localhost"; 1.136 + httpPortPref.value = 80; 1.137 + ftpPortPref.value = 0; 1.138 + doc.documentElement.acceptDialog(); 1.139 + 1.140 + // HTTP host empty, port 0 with share on 1.141 + setDoc(yield null); 1.142 + proxyTypePref.value = 1; 1.143 + sharePref.value = true; 1.144 + httpPref.value = ""; 1.145 + httpPortPref.value = 0; 1.146 + doc.documentElement.acceptDialog(); 1.147 + 1.148 + // HTTP 0, but in no proxy mode 1.149 + setDoc(yield null); 1.150 + proxyTypePref.value = 0; 1.151 + sharePref.value = true; 1.152 + httpPref.value = "localhost"; 1.153 + httpPortPref.value = 0; 1.154 + 1.155 + final = true; // This is the final test, don't spawn another connection window 1.156 + doc.documentElement.acceptDialog(); 1.157 + yield null; 1.158 + } 1.159 + 1.160 + Services.ww.registerNotification(observer); 1.161 + openDialog(preferencesURL, "Preferences", 1.162 + "chrome,titlebar,toolbar,centerscreen,dialog=no", "paneAdvanced"); 1.163 +}