Bug 1022896 - Handle B2G network setup and teardown transparently. r=jib, a=test-only
authorNils Ohlmeier [:drno] <drno@ohlmeier.org>
Fri, 13 Jun 2014 16:56:00 +0200
changeset 200811 87da0cff32d4919330ab970fbc2c4f12165cce30
parent 200810 7d5bd94dfe9c47defb29ea8d3580feb041a223d2
child 200812 7056302f99fe4d29e0cef33ec1bf007a1c141f21
push id6339
push userryanvm@gmail.com
push dateWed, 02 Jul 2014 15:55:05 +0000
treeherdermozilla-aurora@234b0fa642ad [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjib, test-only
bugs1022896
milestone32.0a2
Bug 1022896 - Handle B2G network setup and teardown transparently. r=jib, a=test-only
dom/media/tests/identity/test_getIdentityAssertion.html
dom/media/tests/identity/test_peerConnection_peerIdentity.html
dom/media/tests/identity/test_setIdentityProvider.html
dom/media/tests/identity/test_setIdentityProviderWithErrors.html
dom/media/tests/mochitest/pc.js
dom/media/tests/mochitest/test_dataChannel_basicAudio.html
dom/media/tests/mochitest/test_dataChannel_basicAudioVideo.html
dom/media/tests/mochitest/test_dataChannel_basicAudioVideoCombined.html
dom/media/tests/mochitest/test_dataChannel_basicDataOnly.html
dom/media/tests/mochitest/test_dataChannel_basicVideo.html
dom/media/tests/mochitest/test_dataChannel_bug1013809.html
dom/media/tests/mochitest/test_dataChannel_noOffer.html
dom/media/tests/mochitest/test_peerConnection_addCandidateInHaveLocalOffer.html
dom/media/tests/mochitest/test_peerConnection_basicAudio.html
dom/media/tests/mochitest/test_peerConnection_basicAudioVideo.html
dom/media/tests/mochitest/test_peerConnection_basicAudioVideoCombined.html
dom/media/tests/mochitest/test_peerConnection_basicVideo.html
dom/media/tests/mochitest/test_peerConnection_bug1013809.html
dom/media/tests/mochitest/test_peerConnection_bug822674.html
dom/media/tests/mochitest/test_peerConnection_bug825703.html
dom/media/tests/mochitest/test_peerConnection_bug827843.html
dom/media/tests/mochitest/test_peerConnection_bug834153.html
dom/media/tests/mochitest/test_peerConnection_bug835370.html
dom/media/tests/mochitest/test_peerConnection_close.html
dom/media/tests/mochitest/test_peerConnection_errorCallbacks.html
dom/media/tests/mochitest/test_peerConnection_offerRequiresReceiveAudio.html
dom/media/tests/mochitest/test_peerConnection_offerRequiresReceiveVideo.html
dom/media/tests/mochitest/test_peerConnection_offerRequiresReceiveVideoAudio.html
dom/media/tests/mochitest/test_peerConnection_setLocalAnswerInHaveLocalOffer.html
dom/media/tests/mochitest/test_peerConnection_setLocalAnswerInStable.html
dom/media/tests/mochitest/test_peerConnection_setLocalOfferInHaveRemoteOffer.html
dom/media/tests/mochitest/test_peerConnection_setRemoteAnswerInHaveRemoteOffer.html
dom/media/tests/mochitest/test_peerConnection_setRemoteAnswerInStable.html
dom/media/tests/mochitest/test_peerConnection_setRemoteOfferInHaveLocalOffer.html
dom/media/tests/mochitest/test_peerConnection_throwInCallbacks.html
dom/media/tests/mochitest/test_peerConnection_toJSON.html
dom/media/tests/mochitest/test_zmedia_cleanup.html
--- a/dom/media/tests/identity/test_getIdentityAssertion.html
+++ b/dom/media/tests/identity/test_getIdentityAssertion.html
@@ -105,14 +105,14 @@ function theTest() {
         test.next();
       };
       test.pcLocal._pc.getIdentityAssertion();
     }
   ]
   ]);
   test.run();
 }
-runTest(theTest);
+runNetworkTest(theTest);
 
 </script>
 </pre>
 </body>
 </html>
--- a/dom/media/tests/identity/test_peerConnection_peerIdentity.html
+++ b/dom/media/tests/identity/test_peerConnection_peerIdentity.html
@@ -78,14 +78,14 @@ function theTest() {
 
       audioIsSilence(true, remoteStream, done);
       videoIsBlack(true, remoteStream, done);
     }
   ],
   ]);
   test.run();
 }
-runTest(theTest);
+runNetworkTest(theTest);
 
 </script>
 </pre>
 </body>
 </html>
--- a/dom/media/tests/identity/test_setIdentityProvider.html
+++ b/dom/media/tests/identity/test_setIdentityProvider.html
@@ -100,16 +100,16 @@ function theTest() {
       ok(test.pcRemote.remoteDescription.sdp.contains("a=identity"),
                          "a=identity is in the remote copy of the answer");
       test.next();
     }
   ]
   ]);
   test.run();
 }
-runTest(theTest);
+runNetworkTest(theTest);
 
 
 
 </script>
 </pre>
 </body>
 </html>
--- a/dom/media/tests/identity/test_setIdentityProviderWithErrors.html
+++ b/dom/media/tests/identity/test_setIdentityProviderWithErrors.html
@@ -11,17 +11,17 @@
 <body>
 <pre id="test">
 <script type="application/javascript">
   createHTML({
     title: "Identity Provider returning errors is handled correctly"
   });
 
 var test;
-runTest(function () {
+runNetworkTest(function () {
   test = new PeerConnectionTest();
   test.setMediaConstraints([{audio: true}], [{audio: true}]);
   // first example generates an error
   test.setIdentityProvider(test.pcLocal, 'example.com', 'idp.html#error', 'nobody');
   // second generates a bad assertion; which fails to validate
   test.setIdentityProvider(test.pcRemote, 'example.com', 'idp.html#bad', 'nobody');
 
   var localEvents = trapIdentityEvents(test.pcLocal._pc);
--- a/dom/media/tests/mochitest/pc.js
+++ b/dom/media/tests/mochitest/pc.js
@@ -345,17 +345,17 @@ function isNetworkReady() {
     var num = itfList.getNumberOfInterface();
     for (var i = 0; i < num; i++) {
       var ips = {};
       var prefixLengths = {};
       var length = itfList.getInterface(i).getAddresses(ips, prefixLengths);
 
       for (var j = 0; j < length; j++) {
         var ip = ips.value[j];
-        // skip IPv6 address
+        // skip IPv6 address until bug 797262 is implemented
         if (ip.indexOf(":") < 0) {
           info("Network interface is ready with address: " + ip);
           return true;
         }
       }
     }
     // ip address is not available
     info("Network interface is not ready, required additional network setup");
@@ -375,43 +375,85 @@ function getNetworkUtils() {
   var script = SpecialPowers.loadChromeScript(url);
 
   var utils = {
     /**
      * Utility for setting up data connection.
      *
      * @param aCallback callback after data connection is ready.
      */
-    prepareNetwork: function(aCallback) {
+    prepareNetwork: function(onSuccess) {
       script.addMessageListener('network-ready', function (message) {
         info("Network interface is ready");
-        aCallback();
+        onSuccess();
       });
-      info("Setup network interface");
+      info("Setting up network interface");
       script.sendAsyncMessage("prepare-network", true);
     },
     /**
      * Utility for tearing down data connection.
      *
      * @param aCallback callback after data connection is closed.
      */
-    tearDownNetwork: function(aCallback) {
-      script.addMessageListener('network-disabled', function (message) {
-        ok(true, 'network-disabled');
-        script.destroy();
-        aCallback();
-      });
-      script.sendAsyncMessage("network-cleanup", true);
+    tearDownNetwork: function(onSuccess, onFailure) {
+      if (isNetworkReady()) {
+        script.addMessageListener('network-disabled', function (message) {
+          info("Network interface torn down");
+          script.destroy();
+          onSuccess();
+        });
+        info("Tearing down network interface");
+        script.sendAsyncMessage("network-cleanup", true);
+      } else {
+        info("No network to tear down");
+        onFailure();
+      }
     }
   };
 
   return utils;
 }
 
 /**
+ * Setup network on Gonk if needed and execute test once network is up
+ *
+ */
+function startNetworkAndTest(onSuccess) {
+  if (!isNetworkReady()) {
+    SimpleTest.waitForExplicitFinish();
+    var utils = getNetworkUtils();
+    // Trigger network setup to obtain IP address before creating any PeerConnection.
+    utils.prepareNetwork(onSuccess);
+  } else {
+    onSuccess();
+  }
+}
+
+/**
+ * A wrapper around SimpleTest.finish() to handle B2G network teardown
+ */
+function networkTestFinished() {
+  if ("nsINetworkInterfaceListService" in SpecialPowers.Ci) {
+    var utils = getNetworkUtils();
+    utils.tearDownNetwork(SimpleTest.finish, SimpleTest.finish);
+  } else {
+    SimpleTest.finish();
+  }
+}
+
+/**
+ * A wrapper around runTest() which handles B2G network setup and teardown
+ */
+function runNetworkTest(testFunction) {
+  startNetworkAndTest(function() {
+    runTest(testFunction);
+  });
+}
+
+/**
  * This class handles tests for peer connections.
  *
  * @constructor
  * @param {object} [options={}]
  *        Optional options for the peer connection test
  * @param {object} [options.commands=commandsPeerConnection]
  *        Commands to run for the test
  * @param {bool}   [options.is_local=true]
@@ -445,37 +487,16 @@ function PeerConnectionTest(options) {
         options.config_remote = {};
       }
       if (!options.config_remote.hasOwnProperty("iceServers")) {
         options.config_remote.iceServers = turnServers.remote.iceServers;
       }
     }
   }
 
-  var netTeardownCommand = null;
-  if (!isNetworkReady()) {
-    var utils = getNetworkUtils();
-    // Trigger network setup to obtain IP address before creating any PeerConnection.
-    utils.prepareNetwork(function() {
-      ok(isNetworkReady(),'setup network connection successfully');
-    });
-
-    netTeardownCommand = [
-      [
-        'TEARDOWN_NETWORK',
-        function(test) {
-          utils.tearDownNetwork(function() {
-            info('teardown network connection');
-            test.next();
-          });
-        }
-      ]
-    ];
-  }
-
   if (options.is_local)
     this.pcLocal = new PeerConnectionWrapper('pcLocal', options.config_local);
   else
     this.pcLocal = null;
 
   if (options.is_remote)
     this.pcRemote = new PeerConnectionWrapper('pcRemote', options.config_remote || options.config_local);
   else
@@ -485,21 +506,16 @@ function PeerConnectionTest(options) {
   this.chain = new CommandChain(this, options.commands);
   if (!options.is_local) {
     this.chain.filterOut(/^PC_LOCAL/);
   }
   if (!options.is_remote) {
     this.chain.filterOut(/^PC_REMOTE/);
   }
 
-  // Insert network teardown after testcase execution.
-  if (netTeardownCommand) {
-    this.chain.append(netTeardownCommand);
-  }
-
   var self = this;
   this.chain.onFinished = function () {
     self.teardown();
   };
 }
 
 /**
  * Closes the peer connection if it is active
@@ -756,17 +772,17 @@ PeerConnectionTest.prototype.run = funct
 
 /**
  * Clean up the objects used by the test
  */
 PeerConnectionTest.prototype.teardown = function PCT_teardown() {
   this.close(function () {
     info("Test finished");
     if (window.SimpleTest)
-      SimpleTest.finish();
+      networkTestFinished();
     else
       finish();
   });
 };
 
 /**
  * This class handles tests for data channels.
  *
--- a/dom/media/tests/mochitest/test_dataChannel_basicAudio.html
+++ b/dom/media/tests/mochitest/test_dataChannel_basicAudio.html
@@ -12,18 +12,18 @@
 <pre id="test">
 <script type="application/javascript">
   createHTML({
     bug: "796895",
     title: "Basic data channel audio connection"
   });
 
   var test;
-  runTest(function () {
+  runNetworkTest(function () {
     test = new DataChannelTest();
     test.setMediaConstraints([{audio: true}], [{audio: true}]);
     test.run();
-  }, true);
+  });
 
 </script>
 </pre>
 </body>
 </html>
--- a/dom/media/tests/mochitest/test_dataChannel_basicAudioVideo.html
+++ b/dom/media/tests/mochitest/test_dataChannel_basicAudioVideo.html
@@ -12,19 +12,19 @@
 <pre id="test">
 <script type="application/javascript">
   createHTML({
     bug: "796891",
     title: "Basic data channel audio/video connection"
   });
 
   var test;
-  runTest(function () {
+  runNetworkTest(function () {
     test = new DataChannelTest();
     test.setMediaConstraints([{audio: true}, {video: true}],
                              [{audio: true}, {video: true}]);
     test.run();
-  }, true);
+  });
 
 </script>
 </pre>
 </body>
 </html>
--- a/dom/media/tests/mochitest/test_dataChannel_basicAudioVideoCombined.html
+++ b/dom/media/tests/mochitest/test_dataChannel_basicAudioVideoCombined.html
@@ -12,19 +12,19 @@
 <pre id="test">
 <script type="application/javascript">
   createHTML({
     bug: "796891",
     title: "Basic data channel audio/video connection"
   });
 
   var test;
-  runTest(function () {
+  runNetworkTest(function () {
     test = new DataChannelTest();
     test.setMediaConstraints([{audio: true, video: true}],
                              [{audio: true, video: true}]);
     test.run();
-  }, true);
+  });
 
 </script>
 </pre>
 </body>
 </html>
--- a/dom/media/tests/mochitest/test_dataChannel_basicDataOnly.html
+++ b/dom/media/tests/mochitest/test_dataChannel_basicDataOnly.html
@@ -12,17 +12,17 @@
 <pre id="test">
 <script type="application/javascript">
   createHTML({
     bug: "796894",
     title: "Basic datachannel only connection"
   });
 
   var test;
-  runTest(function () {
+  runNetworkTest(function () {
     test = new DataChannelTest();
     test.run();
-  }, true);
+  });
 
 </script>
 </pre>
 </body>
 </html>
--- a/dom/media/tests/mochitest/test_dataChannel_basicVideo.html
+++ b/dom/media/tests/mochitest/test_dataChannel_basicVideo.html
@@ -12,18 +12,18 @@
 <pre id="test">
 <script type="application/javascript">
   createHTML({
     bug: "796889",
     title: "Basic data channel video connection"
   });
 
   var test;
-  runTest(function () {
+  runNetworkTest(function () {
     test = new DataChannelTest();
     test.setMediaConstraints([{video: true}], [{video: true}]);
     test.run();
-  }, true);
+  });
 
 </script>
 </pre>
 </body>
 </html>
--- a/dom/media/tests/mochitest/test_dataChannel_bug1013809.html
+++ b/dom/media/tests/mochitest/test_dataChannel_bug1013809.html
@@ -12,20 +12,20 @@
 <pre id="test">
 <script type="application/javascript">
   createHTML({
     bug: "796895",
     title: "Basic data channel audio connection"
   });
 
   var test;
-  runTest(function () {
+  runNetworkTest(function () {
     test = new DataChannelTest();
     var sld = test.chain.remove("PC_REMOTE_SET_LOCAL_DESCRIPTION");
     test.chain.insertAfter("PC_LOCAL_SET_REMOTE_DESCRIPTION", sld);
     test.setMediaConstraints([{audio: true}], [{audio: true}]);
     test.run();
-  }, true);
+  });
 
 </script>
 </pre>
 </body>
 </html>
--- a/dom/media/tests/mochitest/test_dataChannel_noOffer.html
+++ b/dom/media/tests/mochitest/test_dataChannel_noOffer.html
@@ -1,33 +1,34 @@
 <!DOCTYPE HTML>
 <html>
 <head>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
   <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
   <script type="application/javascript" src="head.js"></script>
+  <script type="application/javascript" src="pc.js"></script>
 </head>
 <body>
 <pre id="test">
 <script type="application/javascript">
   createHTML({
     bug: "856319",
     title: "Don't offer m=application unless createDataChannel is called first"
   });
 
-  runTest(function () {
+  runNetworkTest(function () {
     var pc = new mozRTCPeerConnection();
 
     // necessary to circumvent bug 864109
     var options = { mandatory: { OfferToReceiveAudio: true} };
 
     pc.createOffer(function (offer) {
       ok(!offer.sdp.contains("m=application"),
         "m=application is not contained in the SDP");
 
-      SimpleTest.finish();
+      networkTestFinished();
     }, generateErrorCallback(), options);
-  }, true);
+  });
 
 </script>
 </pre>
 </body>
 </html>
--- a/dom/media/tests/mochitest/test_peerConnection_addCandidateInHaveLocalOffer.html
+++ b/dom/media/tests/mochitest/test_peerConnection_addCandidateInHaveLocalOffer.html
@@ -13,17 +13,17 @@
 <pre id="test">
 <script type="application/javascript">
   createHTML({
     bug: "784519",
     title: "addCandidate (answer) in 'have-local-offer'"
   });
 
   var test;
-  runTest(function () {
+  runNetworkTest(function () {
     test = new PeerConnectionTest();
     test.setMediaConstraints([{audio: true}], [{audio: true}]);
     test.chain.removeAfter("PC_LOCAL_SET_LOCAL_DESCRIPTION");
 
     test.chain.append([[
       "PC_LOCAL_ADD_CANDIDATE",
       function (test) {
         test.pcLocal.addIceCandidateAndFail(
--- a/dom/media/tests/mochitest/test_peerConnection_basicAudio.html
+++ b/dom/media/tests/mochitest/test_peerConnection_basicAudio.html
@@ -13,17 +13,17 @@
 <pre id="test">
 <script type="application/javascript">
   createHTML({
     bug: "796892",
     title: "Basic audio-only peer connection"
   });
 
   var test;
-  runTest(function (options) {
+  runNetworkTest(function (options) {
     test = new PeerConnectionTest(options);
     test.setMediaConstraints([{audio: true}], [{audio: true}]);
     test.run();
   });
 </script>
 </pre>
 </body>
 </html>
--- a/dom/media/tests/mochitest/test_peerConnection_basicAudioVideo.html
+++ b/dom/media/tests/mochitest/test_peerConnection_basicAudioVideo.html
@@ -14,17 +14,17 @@
 <script type="application/javascript">
 
   createHTML({
     bug: "796890",
     title: "Basic audio/video (separate) peer connection"
   });
 
   var test;
-  runTest(function (options) {
+  runNetworkTest(function (options) {
     test = new PeerConnectionTest(options);
     test.setMediaConstraints([{audio: true}, {video: true}],
                              [{audio: true}, {video: true}]);
     test.run();
   });
 </script>
 </pre>
 </body>
--- a/dom/media/tests/mochitest/test_peerConnection_basicAudioVideoCombined.html
+++ b/dom/media/tests/mochitest/test_peerConnection_basicAudioVideoCombined.html
@@ -14,17 +14,17 @@
 <script type="application/javascript">
 
   createHTML({
     bug: "796890",
     title: "Basic audio/video (combined) peer connection"
   });
 
   var test;
-  runTest(function (options) {
+  runNetworkTest(function (options) {
     test = new PeerConnectionTest(options);
     test.setMediaConstraints([{audio: true, video: true}],
                              [{audio: true, video: true}]);
     test.run();
   });
 </script>
 </pre>
 </body>
--- a/dom/media/tests/mochitest/test_peerConnection_basicVideo.html
+++ b/dom/media/tests/mochitest/test_peerConnection_basicVideo.html
@@ -13,17 +13,17 @@
 <pre id="test">
 <script type="application/javascript">
   createHTML({
     bug: "796888",
     title: "Basic video-only peer connection"
   });
 
   var test;
-  runTest(function (options) {
+  runNetworkTest(function (options) {
     test = new PeerConnectionTest(options);
     test.setMediaConstraints([{video: true}], [{video: true}]);
     test.run();
   });
 </script>
 </pre>
 </body>
 </html>
--- a/dom/media/tests/mochitest/test_peerConnection_bug1013809.html
+++ b/dom/media/tests/mochitest/test_peerConnection_bug1013809.html
@@ -12,17 +12,17 @@
 <pre id="test">
 <script type="application/javascript">
   createHTML({
     bug: "1013809",
     title: "Audio-only peer connection with swapped setLocal and setRemote steps"
   });
 
   var test;
-  runTest(function (options) {
+  runNetworkTest(function (options) {
     test = new PeerConnectionTest(options);
     var sld = test.chain.remove("PC_REMOTE_SET_LOCAL_DESCRIPTION");
     test.chain.insertAfter("PC_LOCAL_SET_REMOTE_DESCRIPTION", sld);
     test.setMediaConstraints([{audio: true}], [{audio: true}]);
     test.run();
   });
 </script>
 </pre>
--- a/dom/media/tests/mochitest/test_peerConnection_bug822674.html
+++ b/dom/media/tests/mochitest/test_peerConnection_bug822674.html
@@ -1,29 +1,30 @@
 <!DOCTYPE HTML>
 <html>
 <head>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
   <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
   <script type="application/javascript" src="head.js"></script>
+  <script type="application/javascript" src="pc.js"></script>
 </head>
 <body>
 <pre id="test">
 <script type="application/javascript">
   createHTML({
     bug: "822674",
     title: "mozRTCPeerConnection isn't a true javascript object as it should be"
   });
 
-  runTest(function () {
+  runNetworkTest(function () {
     var pc = new mozRTCPeerConnection();
 
     pc.thereIsNeverGoingToBeAPropertyWithThisNameOnThisInterface = 1;
     is(pc.thereIsNeverGoingToBeAPropertyWithThisNameOnThisInterface, 1,
        "Can set expandos on an RTCPeerConnection");
 
     pc = null;
-    SimpleTest.finish();
+    networkTestFinished();
   });
 </script>
 </pre>
 </body>
 </html>
--- a/dom/media/tests/mochitest/test_peerConnection_bug825703.html
+++ b/dom/media/tests/mochitest/test_peerConnection_bug825703.html
@@ -1,14 +1,15 @@
 <!DOCTYPE HTML>
 <html>
 <head>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
   <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
   <script type="application/javascript" src="head.js"></script>
+  <script type="application/javascript" src="pc.js"></script>
 </head>
 <body>
 <pre id="test">
 <script type="application/javascript">
   createHTML({
     bug: "825703",
     title: "RTCConfiguration valid/invalid permutations"
   });
@@ -33,17 +34,17 @@
       } else {
           ok(exception, "mozRTCPeerConnection(" +
              JSON.stringify(config) + ") throws");
       }
   }
 
   // This is a test of the iceServers parsing code + readable errors
 
-  runTest(function () {
+  runNetworkTest(function () {
     var pcs = null;
     var exception = null;
     var config;
 
     try {
       pcs = new mozRTCPeerConnection();
     } catch (e) {
       exception = e;
@@ -81,14 +82,14 @@
         ok(e.message.indexOf("http") > 0,
            "mozRTCPeerConnection() constructor has readable exceptions");
     }
     if (pcs !== null) {
       pcs.close();
     }
     pcs = null;
 
-    SimpleTest.finish();
+    networkTestFinished();
   });
 </script>
 </pre>
 </body>
 </html>
--- a/dom/media/tests/mochitest/test_peerConnection_bug827843.html
+++ b/dom/media/tests/mochitest/test_peerConnection_bug827843.html
@@ -87,17 +87,17 @@
         ok(exception, "Attempt to access remoteDescription of pcRemote after close throws exception");
 
         test.next();
       }
     ]
   ];
 
   var test;
-  runTest(function () {
+  runNetworkTest(function () {
     test = new PeerConnectionTest();
     test.setMediaConstraints([{audio: true}], [{audio: true}]);
     test.chain.append(steps);
     test.run();
   });
 </script>
 </pre>
 </body>
--- a/dom/media/tests/mochitest/test_peerConnection_bug834153.html
+++ b/dom/media/tests/mochitest/test_peerConnection_bug834153.html
@@ -1,45 +1,46 @@
 <!DOCTYPE HTML>
 <html>
 <head>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
   <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
   <script type="application/javascript" src="head.js"></script>
+  <script type="application/javascript" src="pc.js"></script>
 </head>
 <body>
 <pre id="test">
 <script type="application/javascript">
   createHTML({
     bug: "834153",
     title: "Queue CreateAnswer in PeerConnection.js"
   });
 
   function croak(msg) {
     ok(0, msg);
     pc1.close();
     pc2.close();
-    SimpleTest.finish();
+    networkTestFinished();
   }
 
-  runTest(function () {
+  runNetworkTest(function () {
     var pc1 = new mozRTCPeerConnection();
 
     pc1.createOffer(function (d) {
       var pc2 = new mozRTCPeerConnection();
 
       // The whole point of this test is not to wait for the
       // setRemoteDescription call to succesfully complete, so we
       // don't do anything in its callbacks.
       pc2.setRemoteDescription(d, function (x) {}, function (x) {});
       pc2.createAnswer(function (d) {
         is(d.type,"answer","CreateAnswer created an answer");
         pc1.close();
         pc2.close();
-        SimpleTest.finish();
+        networkTestFinished();
       }, function (err) {
         croak("createAnswer failed: " + err);
       });
     }, function (err) {
         croak("createOffer failed: " + err);
     },  { mandatory: { OfferToReceiveAudio: true} });
   });
 </script>
--- a/dom/media/tests/mochitest/test_peerConnection_bug835370.html
+++ b/dom/media/tests/mochitest/test_peerConnection_bug835370.html
@@ -1,24 +1,25 @@
 <!DOCTYPE HTML>
 <html>
 <head>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
   <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
   <script type="application/javascript" src="head.js"></script>
+  <script type="application/javascript" src="pc.js"></script>
 </head>
 <body>
 <pre id="test">
 <script type="application/javascript">
   createHTML({
     bug: "825703",
     title: "PeerConnection.createOffer valid/invalid constraints permutations"
   });
 
-  runTest(function () {
+  runNetworkTest(function () {
     var pconnect  = new mozRTCPeerConnection();
     var pconnects = new mozRTCPeerConnection();
 
     function step1(offer) {}
     function failed(code) {}
 
     var exception = null;
     try { pconnects.createOffer(step1, failed); } catch (e) { exception = e; }
@@ -48,14 +49,14 @@
     ok(exception, "createOffer(step1, failed, { optional: [{ OfferToReceiveVideo: false, OfferToReceiveAudio: true, }] }) throws");
     exception = null;
     try { pconnects.createOffer(step1, failed, { mandatory: { OfferToReceiveVideo: false, OfferToReceiveAudio: true, MozDontOfferDataChannel: true}, optional: [{ VoiceActivityDetection: true }, { FooBar: "42"  }] }); } catch (e) { exception = e; }
     ok(!exception, "createOffer(step1, failed, { mandatory: { OfferToReceiveVideo: false, OfferToReceiveAudio: true, MozDontOfferDataChannel: true}, optional: [{ VoiceActivityDetection: true }, { FooBar: \"42\"  }] }) succeeds");
     pconnect.close();
     pconnects.close();
     pconnect = null;
     pconnects = null;
-    SimpleTest.finish();
+    networkTestFinished();
   });
 </script>
 </pre>
 </body>
 </html>
--- a/dom/media/tests/mochitest/test_peerConnection_close.html
+++ b/dom/media/tests/mochitest/test_peerConnection_close.html
@@ -1,24 +1,25 @@
 <!DOCTYPE HTML>
 <html>
 <head>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
   <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
   <script type="application/javascript" src="head.js"></script>
+  <script type="application/javascript" src="pc.js"></script>
 </head>
 <body>
 <pre id="test">
 <script type="application/javascript">
   createHTML({
     bug: "991877",
     title: "Basic RTCPeerConnection.close() tests"
   });
 
-  runTest(function () {
+  runNetworkTest(function () {
     var pc = new mozRTCPeerConnection();
     var signalStateChanged = false;
     var exception = null;
     var eTimeout = null;
 
     // everything should be in initial state
     is(pc.signalingState, "stable", "Initial signalingState is 'stable'");
     is(pc.iceConnectionState, "new", "Initial iceConnectionState is 'new'");
@@ -79,29 +80,29 @@
 
       SimpleTest.doesThrow(function() {
         pc.setIdentityProvider("Invalid Provider")},
         "setIdentityProvider() on closed PC raised expected exception");
 
       // in case we are ending the test from within here
       if(pc !== null) {
         pc = null;
-        SimpleTest.finish();
+        networkTestFinished();
       }
     }
 
     // This is only a shortcut to prevent a mochitest timeout in case the
     // event does not fire
     eTimeout = setTimeout(function() {
       ok(signalStateChanged, "Failed to receive expected onsignalingstatechange event in 60s");
 
       // in case we are ending the test in this timeout
       if (pc !== null) {
         pc = null;
-        SimpleTest.finish();
+        networkTestFinished();
       }
     }, 60000);
 
     try {
       pc.close();
     } catch (e) {
       exception = e;
     }
@@ -111,17 +112,17 @@
       is(pc.iceConnectionState, "closed", "Final iceConnectionState is 'closed'");
     }
 
     if (signalStateChanged) {
       clearTimeout(eTimeout);
       // in case we are ending the test outside the even handler
       if (pc !== null) {
         pc = null;
-        SimpleTest.finish();
+        networkTestFinished();
       }
     }
 
   });
 </script>
 </pre>
 </body>
 </html>
--- a/dom/media/tests/mochitest/test_peerConnection_errorCallbacks.html
+++ b/dom/media/tests/mochitest/test_peerConnection_errorCallbacks.html
@@ -1,14 +1,15 @@
 <!DOCTYPE HTML>
 <html>
 <head>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
   <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
   <script type="application/javascript" src="head.js"></script>
+  <script type="application/javascript" src="pc.js"></script>
 </head>
 <body>
 <pre id="test">
 <script type="application/javascript">
   createHTML({
     bug: "834270",
     title: "Align PeerConnection error handling with WebRTC specification"
   });
@@ -49,21 +50,21 @@
 
   function testAddIceCandidateError() {
     var pc = new mozRTCPeerConnection();
     info ("Testing addIceCandidate error callback");
     pc.addIceCandidate(new mozRTCIceCandidate({ candidate: "Pony Lords, jump!",
                                                 sdpMid: "whee",
                                                 sdpMLineIndex: 1 }),
       generateErrorCallback("addIceCandidate with nonsense candidate should fail"),
-      errorCallback(SimpleTest.finish));
+      errorCallback(networkTestFinished));
   };
 
   // No test for createOffer errors -- there's nothing we can do at this
   // level to evoke an error in createOffer.
 
-  runTest(function () {
+  runNetworkTest(function () {
     testCreateAnswerError();
   });
 </script>
 </pre>
 </body>
 </html>
--- a/dom/media/tests/mochitest/test_peerConnection_offerRequiresReceiveAudio.html
+++ b/dom/media/tests/mochitest/test_peerConnection_offerRequiresReceiveAudio.html
@@ -11,17 +11,17 @@
 <body>
 <pre id="test">
 <script type="application/javascript">
   createHTML({
     bug: "850275",
     title: "Simple offer media constraint test with audio"
   });
 
-  runTest(function() {
+  runNetworkTest(function() {
     var test = new PeerConnectionTest();
     test.setOfferConstraints({ mandatory: { OfferToReceiveAudio: true } });
     test.run();
   });
 </script>
 </pre>
 </body>
 </html>
--- a/dom/media/tests/mochitest/test_peerConnection_offerRequiresReceiveVideo.html
+++ b/dom/media/tests/mochitest/test_peerConnection_offerRequiresReceiveVideo.html
@@ -11,17 +11,17 @@
 <body>
 <pre id="test">
 <script type="application/javascript">
   createHTML({
     bug: "850275",
     title: "Simple offer media constraint test with video"
   });
 
-  runTest(function() {
+  runNetworkTest(function() {
     var test = new PeerConnectionTest();
     test.setOfferConstraints({ mandatory: { OfferToReceiveVideo: true } });
     test.run();
   });
 </script>
 </pre>
 </body>
 </html>
--- a/dom/media/tests/mochitest/test_peerConnection_offerRequiresReceiveVideoAudio.html
+++ b/dom/media/tests/mochitest/test_peerConnection_offerRequiresReceiveVideoAudio.html
@@ -11,17 +11,17 @@
 <body>
 <pre id="test">
 <script type="application/javascript">
   createHTML({
     bug: "850275",
     title: "Simple offer media constraint test with video/audio"
   });
 
-  runTest(function() {
+  runNetworkTest(function() {
     var test = new PeerConnectionTest();
     test.setOfferConstraints({ mandatory: {
       OfferToReceiveVideo: true,
       OfferToReceiveAudio: true
     }});
     test.run();
   });
 </script>
--- a/dom/media/tests/mochitest/test_peerConnection_setLocalAnswerInHaveLocalOffer.html
+++ b/dom/media/tests/mochitest/test_peerConnection_setLocalAnswerInHaveLocalOffer.html
@@ -13,17 +13,17 @@
 <pre id="test">
 <script type="application/javascript">
   createHTML({
     bug: "784519",
     title: "setLocalDescription (answer) in 'have-local-offer'"
   });
 
   var test;
-  runTest(function () {
+  runNetworkTest(function () {
     test = new PeerConnectionTest();
     test.setMediaConstraints([{audio: true}], [{audio: true}]);
     test.chain.removeAfter("PC_LOCAL_SET_LOCAL_DESCRIPTION");
 
     test.chain.append([[
       "PC_LOCAL_SET_LOCAL_ANSWER",
       function (test) {
         test.pcLocal._last_offer.type="answer";
--- a/dom/media/tests/mochitest/test_peerConnection_setLocalAnswerInStable.html
+++ b/dom/media/tests/mochitest/test_peerConnection_setLocalAnswerInStable.html
@@ -13,17 +13,17 @@
 <pre id="test">
 <script type="application/javascript">
   createHTML({
     bug: "784519",
     title: "setLocalDescription (answer) in 'stable'"
   });
 
   var test;
-  runTest(function () {
+  runNetworkTest(function () {
     test = new PeerConnectionTest();
     test.setMediaConstraints([{audio: true}], [{audio: true}]);
     test.chain.removeAfter("PC_LOCAL_CREATE_OFFER");
 
     test.chain.append([[
       "PC_LOCAL_SET_LOCAL_ANSWER",
       function (test) {
         test.pcLocal._last_offer.type="answer";
--- a/dom/media/tests/mochitest/test_peerConnection_setLocalOfferInHaveRemoteOffer.html
+++ b/dom/media/tests/mochitest/test_peerConnection_setLocalOfferInHaveRemoteOffer.html
@@ -13,17 +13,17 @@
 <pre id="test">
 <script type="application/javascript">
   createHTML({
     bug: "784519",
     title: "setLocalDescription (offer) in 'have-remote-offer'"
   });
 
   var test;
-  runTest(function () {
+  runNetworkTest(function () {
     test = new PeerConnectionTest();
     test.setMediaConstraints([{audio: true}], [{audio: true}]);
     test.chain.removeAfter("PC_REMOTE_SET_REMOTE_DESCRIPTION");
 
     test.chain.append([[
       "PC_REMOTE_SET_LOCAL_OFFER",
       function (test) {
         test.pcRemote.setLocalDescriptionAndFail(test.pcLocal._last_offer,
--- a/dom/media/tests/mochitest/test_peerConnection_setRemoteAnswerInHaveRemoteOffer.html
+++ b/dom/media/tests/mochitest/test_peerConnection_setRemoteAnswerInHaveRemoteOffer.html
@@ -13,17 +13,17 @@
 <pre id="test">
 <script type="application/javascript">
   createHTML({
     bug: "784519",
     title: "setRemoteDescription (answer) in 'have-remote-offer'"
   });
 
   var test;
-  runTest(function () {
+  runNetworkTest(function () {
     test = new PeerConnectionTest();
     test.setMediaConstraints([{audio: true}], [{audio: true}]);
     test.chain.removeAfter("PC_REMOTE_SET_REMOTE_DESCRIPTION");
 
     test.chain.append([[
       "PC_REMOTE_SET_REMOTE_ANSWER",
       function (test) {
         test.pcLocal._last_offer.type="answer";
--- a/dom/media/tests/mochitest/test_peerConnection_setRemoteAnswerInStable.html
+++ b/dom/media/tests/mochitest/test_peerConnection_setRemoteAnswerInStable.html
@@ -13,17 +13,17 @@
 <pre id="test">
 <script type="application/javascript">
   createHTML({
     bug: "784519",
     title: "setRemoteDescription (answer) in 'stable'"
   });
 
   var test;
-  runTest(function () {
+  runNetworkTest(function () {
     test = new PeerConnectionTest();
     test.setMediaConstraints([{audio: true}], [{audio: true}]);
     test.chain.removeAfter("PC_LOCAL_CREATE_OFFER");
 
     test.chain.append([[
       "PC_LOCAL_SET_REMOTE_ANSWER",
       function (test) {
         test.pcLocal._last_offer.type="answer";
--- a/dom/media/tests/mochitest/test_peerConnection_setRemoteOfferInHaveLocalOffer.html
+++ b/dom/media/tests/mochitest/test_peerConnection_setRemoteOfferInHaveLocalOffer.html
@@ -13,17 +13,17 @@
 <pre id="test">
 <script type="application/javascript">
   createHTML({
     bug: "784519",
     title: "setRemoteDescription (offer) in 'have-local-offer'"
   });
 
   var test;
-  runTest(function () {
+  runNetworkTest(function () {
     test = new PeerConnectionTest();
     test.setMediaConstraints([{audio: true}], [{audio: true}]);
     test.chain.removeAfter("PC_LOCAL_SET_LOCAL_DESCRIPTION");
 
     test.chain.append([[
       "PC_LOCAL_SET_REMOTE_OFFER",
       function (test) {
         test.pcLocal.setRemoteDescriptionAndFail(test.pcLocal._last_offer,
--- a/dom/media/tests/mochitest/test_peerConnection_throwInCallbacks.html
+++ b/dom/media/tests/mochitest/test_peerConnection_throwInCallbacks.html
@@ -1,15 +1,16 @@
 <!DOCTYPE HTML>
 <html>
 <head>
   <meta charset="utf-8">
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
   <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
   <script type="application/javascript" src="head.js"></script>
+  <script type="application/javascript" src="pc.js"></script>
 </head>
 <body>
 <pre id="test">
 <script type="application/javascript;version=1.8">
   createHTML({
     bug: "857765",
     title: "Throw in PeerConnection callbacks"
   });
@@ -26,17 +27,17 @@
       finish();
     }
     throw new Error("window.onerror may throw");
     return false;
   }
 
   let pc0, pc1, pc2;
 
-  runTest(function () {
+  runNetworkTest(function () {
     error_count = 0;
 
     // Test failure callbacks (limited to 1 for now)
     pc0 = new mozRTCPeerConnection();
     pc0.createOffer(getFail(), function(err) {
       pc1 = new mozRTCPeerConnection();
       pc2 = new mozRTCPeerConnection();
 
@@ -64,17 +65,17 @@
       }, getFail());
       throw new Error("Expected");
     });
   });
 
   function finish() {
     window.onerror = oldOnError;
     is(error_count, 7, "Seven expected errors verified.");
-    SimpleTest.finish();
+    networkTestFinished();
   }
 
   function getFail() {
     return function (err) {
       window.onerror = oldOnError;
       generateErrorCallback()(err);
     };
   }
--- a/dom/media/tests/mochitest/test_peerConnection_toJSON.html
+++ b/dom/media/tests/mochitest/test_peerConnection_toJSON.html
@@ -4,26 +4,27 @@
 https://bugzilla.mozilla.org/show_bug.cgi?id=872377
 -->
 <head>
   <meta charset="utf-8">
   <title>Test for Bug 872377 and Bug 928304</title>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
   <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
   <script type="application/javascript" src="head.js"></script>
+  <script type="application/javascript" src="pc.js"></script>
 </head>
 <body>
 <pre id="test">
 <script type="application/javascript">
   createHTML({
     bug: "928304",
     title: "test toJSON() on mozRTCSessionDescription and mozRTCIceCandidate"
   });
 
-  runTest(function () {
+  runNetworkTest(function () {
     /** Test for Bug 872377 **/
 
     var rtcSession = new mozRTCSessionDescription({ sdp: "Picklechips!",
                                                     type: "offer" });
     var jsonCopy = JSON.parse(JSON.stringify(rtcSession));
     for (key in rtcSession) {
       if (typeof(rtcSession[key]) == "function") continue;
       is(rtcSession[key], jsonCopy[key], "key " + key + " should match.");
@@ -34,14 +35,14 @@ https://bugzilla.mozilla.org/show_bug.cg
     var rtcIceCandidate = new mozRTCIceCandidate({ candidate: "dummy",
                                                    sdpMid: "test",
                                                    sdpMLineIndex: 3 });
     jsonCopy = JSON.parse(JSON.stringify(rtcIceCandidate));
     for (key in rtcIceCandidate) {
       if (typeof(rtcIceCandidate[key]) == "function") continue;
       is(rtcIceCandidate[key], jsonCopy[key], "key " + key + " should match.");
     }
-    SimpleTest.finish();
+    networkTestFinished();
   });
 </script>
 </pre>
 </body>
 </html>
--- a/dom/media/tests/mochitest/test_zmedia_cleanup.html
+++ b/dom/media/tests/mochitest/test_zmedia_cleanup.html
@@ -1,29 +1,30 @@
 <!DOCTYPE HTML>
 <html>
   <head>
     <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
     <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+    <script type="application/javascript" src="pc.js"></script>
   </head>
 <body>
 <pre id="test">
 <script type="application/javascript">
 SimpleTest.waitForExplicitFinish();
 
 if ("nsINetworkInterfaceListService" in SpecialPowers.Ci) {
-  var url = SimpleTest.getTestFileURL("NetworkPreparationChromeScript.js");
-  var script = SpecialPowers.loadChromeScript(url);
-  script.addMessageListener('network-disabled', function (message) {
-    ok(true, 'network-disabled');
-    script.destroy();
+  var utils = getNetworkUtils();
+  utils.tearDownNetwork(function() {
+    ok(true, 'Successfully teared down network interface');
+    SimpleTest.finish();
+  }, function() {
+    ok(true, 'Network interface was in down state already');
     SimpleTest.finish();
   });
-  script.sendAsyncMessage("network-cleanup", true);
 } else {
-  ok(true, 'no need to cleanup network interface');
+  ok(true, 'No need to cleanup network interface');
   SimpleTest.finish();
 }
 
 </script>
 </pre>
 </body>
 </html>