Bug 463046: Network connectivity checking breaks running netwerk unit tests and mochitests with no connection. r=bz, r=jwalden
authorDave Camp <dcamp@mozilla.com>
Mon, 12 Jan 2009 21:52:00 -0800
changeset 23578 5eeab4033c20a1031e84d532a62bb3caa97290dc
parent 23577 cfcb51bdb85201f86cbf94cca5c87151499ddecc
child 23579 9dad087b466e6379bc2003cdb8c589395e1aa6a1
push id4619
push userdcamp@mozilla.com
push dateTue, 13 Jan 2009 05:52:41 +0000
treeherdermozilla-central@44890ee1d15f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbz, jwalden
bugs463046
milestone1.9.2a1pre
Bug 463046: Network connectivity checking breaks running netwerk unit tests and mochitests with no connection. r=bz, r=jwalden
build/pgo/automation.py.in
layout/tools/reftest/reftest-cmdline.js
netwerk/base/src/nsIOService.cpp
testing/mochitest/server.js
tools/test-harness/xpcshell-simple/head.js
--- a/build/pgo/automation.py.in
+++ b/build/pgo/automation.py.in
@@ -304,16 +304,17 @@ user_pref("browser.shell.checkDefaultBro
 user_pref("shell.checkDefaultClient", false);
 user_pref("browser.warnOnQuit", false);
 user_pref("accessibility.typeaheadfind.autostart", false);
 user_pref("javascript.options.showInConsole", true);
 user_pref("layout.debug.enable_data_xbl", true);
 user_pref("browser.EULA.override", true);
 user_pref("javascript.options.jit.content", true);
 user_pref("gfx.color_management.force_srgb", true);
+user_pref("network.manage-offline-status", false);
 
 user_pref("camino.warn_when_closing", false); // Camino-only, harmless to others
 """
   prefs.append(part)
 
   locations = readLocations()
 
   # Grant God-power to all the privileged servers on which tests run.
--- a/layout/tools/reftest/reftest-cmdline.js
+++ b/layout/tools/reftest/reftest-cmdline.js
@@ -92,16 +92,22 @@ RefTestCmdLineHandler.prototype =
       catch (e) {
         return;
       }
     }
     catch (e) {
       cmdLine.handleFlag("reftest", true);
     }
 
+    /* Ignore the platform's online/offline status while running reftests. */
+    var ios = Components.classes["@mozilla.org/network/io-service;1"]
+              .getService(Components.interfaces.nsIIOService2);
+    ios.manageOfflineStatus = false;
+    ios.offline = false;
+
     /* Force sRGB as an output profile for color management before we load a
        window. */
     var prefs = Components.classes["@mozilla.org/preferences-service;1"].
                 getService(Components.interfaces.nsIPrefBranch2);
     prefs.setBoolPref("gfx.color_management.force_srgb", true);
 
     var wwatch = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
                            .getService(nsIWindowWatcher);
--- a/netwerk/base/src/nsIOService.cpp
+++ b/netwerk/base/src/nsIOService.cpp
@@ -71,19 +71,20 @@
 #include "nsNetUtil.h"
 #include "nsThreadUtils.h"
 #include "nsIPermissionManager.h"
 
 #if defined(XP_WIN)
 #include "nsNativeConnectionHelper.h"
 #endif
 
-#define PORT_PREF_PREFIX     "network.security.ports."
-#define PORT_PREF(x)         PORT_PREF_PREFIX x
-#define AUTODIAL_PREF        "network.autodial-helper.enabled"
+#define PORT_PREF_PREFIX           "network.security.ports."
+#define PORT_PREF(x)               PORT_PREF_PREFIX x
+#define AUTODIAL_PREF              "network.autodial-helper.enabled"
+#define MANAGE_OFFLINE_STATUS_PREF "network.manage-offline-status"
 
 #define MAX_RECURSION_COUNT 50
 
 nsIOService* gIOService = nsnull;
 
 // A general port blacklist.  Connections to these ports will not be allowed unless 
 // the protocol overrides.
 //
@@ -158,17 +159,17 @@ static const char kProfileChangeNetResto
 // Necko buffer cache
 nsIMemory* nsIOService::gBufferCache = nsnull;
 
 ////////////////////////////////////////////////////////////////////////////////
 
 nsIOService::nsIOService()
     : mOffline(PR_FALSE)
     , mOfflineForProfileChange(PR_FALSE)
-    , mManageOfflineStatus(PR_FALSE)
+    , mManageOfflineStatus(PR_TRUE)
     , mChannelEventSinks(NS_CHANNEL_EVENT_SINK_CATEGORY)
     , mContentSniffers(NS_CONTENT_SNIFFER_CATEGORY)
 {
     // Get the allocator ready
     if (!gBufferCache)
     {
         nsresult rv = NS_OK;
         nsCOMPtr<nsIRecyclingAllocator> recyclingAllocator =
@@ -222,16 +223,17 @@ nsIOService::Init()
         mRestrictedPortList.AppendElement(reinterpret_cast<void *>(gBadPortList[i]));
 
     // Further modifications to the port list come from prefs
     nsCOMPtr<nsIPrefBranch2> prefBranch;
     GetPrefBranch(getter_AddRefs(prefBranch));
     if (prefBranch) {
         prefBranch->AddObserver(PORT_PREF_PREFIX, this, PR_TRUE);
         prefBranch->AddObserver(AUTODIAL_PREF, this, PR_TRUE);
+        prefBranch->AddObserver(MANAGE_OFFLINE_STATUS_PREF, this, PR_TRUE);
         PrefsChanged(prefBranch);
     }
     
     // Register for profile change notifications
     nsCOMPtr<nsIObserverService> observerService =
         do_GetService("@mozilla.org/observer-service;1");
     if (observerService) {
         observerService->AddObserver(this, kProfileChangeNetTeardownTopic, PR_TRUE);
@@ -241,20 +243,21 @@ nsIOService::Init()
     }
     else
         NS_WARNING("failed to get observer service");
         
     gIOService = this;
     
     // go into managed mode if we can
     mNetworkLinkService = do_GetService(NS_NETWORK_LINK_SERVICE_CONTRACTID);
-    if (mNetworkLinkService) {
-        mManageOfflineStatus = PR_TRUE;
+    if (!mNetworkLinkService)
+        mManageOfflineStatus = PR_FALSE;
+
+    if (mManageOfflineStatus)
         TrackNetworkLinkStatusForOffline();
-    }
 
     return NS_OK;
 }
 
 
 nsIOService::~nsIOService()
 {
     gIOService = nsnull;
@@ -723,16 +726,23 @@ nsIOService::PrefsChanged(nsIPrefBranch 
         PRBool enableAutodial = PR_FALSE;
         nsresult rv = prefs->GetBoolPref(AUTODIAL_PREF, &enableAutodial);
         // If pref not found, default to disabled.
         if (NS_SUCCEEDED(rv)) {
             if (mSocketTransportService)
                 mSocketTransportService->SetAutodialEnabled(enableAutodial);
         }
     }
+
+    if (!pref || strcmp(pref, MANAGE_OFFLINE_STATUS_PREF) == 0) {
+        PRBool manage;
+        if (NS_SUCCEEDED(prefs->GetBoolPref(MANAGE_OFFLINE_STATUS_PREF,
+                                            &manage)))
+            SetManageOfflineStatus(manage);
+    }
 }
 
 void
 nsIOService::ParsePortList(nsIPrefBranch *prefBranch, const char *pref, PRBool remove)
 {
     nsXPIDLCString portList;
 
     // Get a pref string and chop it up into a list of ports.
--- a/testing/mochitest/server.js
+++ b/testing/mochitest/server.js
@@ -36,16 +36,24 @@
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 // Note that the server script itself already defines Cc, Ci, and Cr for us,
 // and because they're constants it's not safe to redefine them.  Scope leakage
 // sucks.
 
+// Disable automatic network detection, so tests work correctly when
+// not connected to a network.
+let (ios = Cc["@mozilla.org/network/io-service;1"]
+           .getService(Ci.nsIIOService2)) {
+  ios.manageOfflineStatus = false;
+  ios.offline = false;
+}
+
 const SERVER_PORT = 8888;
 var server; // for use in the shutdown handler, if necessary
 
 //
 // HTML GENERATION
 //
 var tags = ['A', 'ABBR', 'ACRONYM', 'ADDRESS', 'APPLET', 'AREA', 'B', 'BASE',
             'BASEFONT', 'BDO', 'BIG', 'BLOCKQUOTE', 'BODY', 'BR', 'BUTTON',
--- a/tools/test-harness/xpcshell-simple/head.js
+++ b/tools/test-harness/xpcshell-simple/head.js
@@ -42,16 +42,24 @@
  * See http://developer.mozilla.org/en/docs/Writing_xpcshell-based_unit_tests
  * for more information
  */
 
 var _quit = false;
 var _fail = false;
 var _tests_pending = 0;
 
+// Disable automatic network detection, so tests work correctly when
+// not connected to a network.
+let (ios = Components.classes["@mozilla.org/network/io-service;1"]
+           .getService(Components.interfaces.nsIIOService2)) {
+  ios.manageOfflineStatus = false;
+  ios.offline = false;
+}
+
 function _TimerCallback(expr) {
   this._expr = expr;
 }
 _TimerCallback.prototype = {
   _expr: "",
   QueryInterface: function(iid) {
     if (iid.Equals(Components.interfaces.nsITimerCallback) ||
         iid.Equals(Components.interfaces.nsISupports))