Bug 887699 - Part 4/4 Tests. r=gene
authorAlbert Crespell <acperez@tid.es>
Tue, 08 Oct 2013 08:10:10 +0200
changeset 166979 5cdf2c46b548ca3d1574c349393fa735b7e0e48b
parent 166978 7f85957616d4fdba57f9fee7fbdec3b2cec8f71b
child 166980 98f85c22db228c3bfb87f05d9dfea56094fc6ab1
push id428
push userbbajaj@mozilla.com
push dateTue, 28 Jan 2014 00:16:25 +0000
treeherdermozilla-release@cd72a7ff3a75 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgene
bugs887699
milestone27.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 887699 - Part 4/4 Tests. r=gene
dom/network/tests/test_networkstats_basics.html
dom/network/tests/test_networkstats_enabled_no_perm.html
dom/network/tests/unit_stats/test_networkstats_db.js
dom/network/tests/unit_stats/test_networkstats_service.js
dom/network/tests/unit_stats/test_networkstats_service_proxy.js
--- a/dom/network/tests/test_networkstats_basics.html
+++ b/dom/network/tests/test_networkstats_basics.html
@@ -7,55 +7,44 @@
 </head>
 <body>
 <p id="display"></p>
 <div id="content" style="display: none">
 </div>
 <pre id="test">
 <script type="application/javascript">
 
-// Test for NetworkStats
-function checkInterface(aInterface) {
-  ok(!(aInterface in window), aInterface + " should be prefixed");
-  ok(("Moz" + aInterface) in window, aInterface + " should be prefixed");
-}
-
 function test() {
-  // Test interfaces
-  checkInterface("NetworkStatsManager");
-  checkInterface("NetworkStats");
-  checkInterface("NetworkStatsData");
-
   ok('mozNetworkStats' in navigator, "navigator.mozMozNetworkStats should exist");
   ok(navigator.mozNetworkStats, "navigator.mozNetworkStats returns an object");
 
   netStats = navigator.mozNetworkStats;
 
   // Test IDL attributes
-  ok('connectionTypes' in netStats,
-   "connectionTypes should be a NetworkStats attribute");
-  ok(Array.isArray(netStats.connectionTypes) && netStats.connectionTypes.length > 0,
-   "connectionTypes is an array not empty.");
+  ok('availableNetworks' in netStats,
+   "availableNetworks should be a NetworkStats attribute");
+  ok(Array.isArray(netStats.availableNetworks) && netStats.availableNetworks.length > 0,
+   "availableNetworks is an array not empty.");
 
   ok('sampleRate' in netStats,
    "sampleRate should be a NetworkStats attribute");
   ok(netStats.sampleRate > 0,
    "sampleRate is greater than 0.");
 
-  ok('maxStorageSamples' in netStats,
-   "maxStorageSamples should be a NetworkStats attribute");
-  ok(netStats.maxStorageSamples > 0,
-   "maxStorageSamples is greater than 0.");
+  ok('maxStorageAge' in netStats,
+   "maxStorageAge should be a NetworkStats attribute");
+  ok(netStats.maxStorageAge > 0,
+   "maxStorageAge is greater than 0.");
 
   // Test IDL methods
   next();
   return;
 }
 
-function checkDataDates(data, start, end, sampleRate){
+function checkDataDates(data, start, end, sampleRate) {
   var offset = (new Date()).getTimezoneOffset() * 60 * 1000;
   start = Math.floor((start.getTime() - offset) / sampleRate) * sampleRate + offset;
   end = Math.floor((end.getTime() - offset) / sampleRate) * sampleRate + offset;
 
   var counter = 0;
   var date = start;
   var success = true;
 
@@ -66,292 +55,195 @@ function checkDataDates(data, start, end
     }
     date += sampleRate;
     counter++;
   } while (date <= end);
 
   ok(success, "data result has correct dates");
 }
 
+function compareNetworks(networkA, networkB) {
+  return (networkA.id == networkB.id &&
+          networkA.type == networkB.type);
+}
+
 var req;
 var index = -1;
 var netStats = null;
 
 var steps = [
   function () {
-    // Test clearAlldata
-    req = netStats.clearAllData();
+    // Test clearAllStats
+    req = netStats.clearAllStats();
     req.onsuccess = function () {
-      ok(true, "clearAllData deleted the database");
+      ok(true, "clearAllStats deleted the database");
       next();
     };
     req.onerror = function () {
-      ok(false, "clearAllData deleted the database");
+      ok(false, "clearAllStats deleted the database");
     }
   },
   function () {
-    // Check if getNetworkStats launch exception when start is greather than end
+    // Check if getSamples launch exception when start is greather than end
 
     // Prepare get params
-    var type = netStats.connectionTypes[0];
+    var network = netStats.availableNetworks[0];
     // Get dates
     var endDate = new Date();
     var startDate = new Date(endDate.getTime() + 1000);
 
     try {
-      netStats.getNetworkStats({start: startDate, end: endDate});
+      netStats.getSamples(network, startDate, endDate);
     } catch(ex) {
-      ok(true, "getNetworkStats launch exception when start is greater than end");
+      ok(true, "getSamples launch exception when start is greater than end");
       next();
       return;
     }
 
-    ok(false, "getNetworkStats launch exceptionwhen start is greater than end");
+    ok(false, "getSamples launch exception when start is greater than end");
     next();
     return;
   },
   function () {
-    // Test if call getNetworkStats with undefined start param launch an exception
+    // Test if call getSamples with network of type different than
+    // nsIDOMMozNetworkStatsInterface launch an exception
 
     // Prepare get params
-    var type = netStats.connectionTypes[0];
-    setTimeout(function() {
-      try {
-        netStats.getNetworkStats({end: new Date()});
-      } catch(ex) {
-        ok(true, "getNetworkStats launch exception when start param does not exist");
-        next();
-        return;
-      }
+    var network = "wifi";
+    var endDate = new Date();
+    var startDate = new Date(endDate.getTime() - 1000);
 
-      ok(false, "getNetworkStats launch exception when start param does not exist");
-    }, 1000);
-  },
-  function () {
-    // Test if call getNetworkStats with undefined end param launch an exception
+    try {
+      netStats.getSamples(network, new Date(), new Date());
+    } catch(ex) {
+      ok(true, "getSamples launch exception if network is not " +
+               "a nsIDOMMozNetworkStatsInterface");
+      next();
+      return;
+    }
 
-    // Prepare get params
-    var type = netStats.connectionTypes[0];
-    setTimeout(function() {
-      try {
-        netStats.getNetworkStats({start: new Date()});
-      } catch(ex) {
-        ok(true, "getNetworkStats launch exception when end param does not exist");
-        next();
-        return;
-      }
-
-      ok(false, "getNetworkStats launch exception when end param does not exist");
-    }, 1000);
+    ok(false, "getSamples launch exception if network is not " +
+              "a nsIDOMMozNetworkStatsInterface");
   },
   function () {
-    ok(true, "Get system stats for a connectionType and dates adapted to samplerate");
+    // Test if call getSamples with start parameter type different than Date launch an exception
+
     // Prepare get params
-    var type = netStats.connectionTypes[0];
+    var network = netStats.availableNetworks[0];
+    var endDate = new Date();
+    var startDate = new Date(endDate.getTime() - 1000);
+    startDate = startDate.toString();
+
+    try {
+      netStats.getSamples(network, startDate, endDate);
+    } catch(ex) {
+      ok(true, "getSamples launch exception when start param is not a Date");
+      next();
+      return;
+    }
+
+    ok(false, "getSamples launch exception when start param is not a Date");
+  },
+  function () {
+    // Test if call getSamples with end parameter type different than Date launch an exception
+
+    // Prepare get params
+    var network = netStats.availableNetworks[0];
+    var endDate = new Date();
+    var startDate = new Date(endDate.getTime() - 1000);
+    endDate = startDate.toString();
+
+    try {
+      netStats.getSamples(network, startDate, endDate);
+    } catch(ex) {
+      ok(true, "getSamples launch exception when end param is not a Date");
+      next();
+      return;
+    }
+
+    ok(false, "getSamples launch exception when end param is not a Date");
+  },
+  function () {
+    ok(true, "Get stats for a network and dates adapted to samplerate");
+    // Prepare get params
+    var network = netStats.availableNetworks[0];
     var diff = 2;
     // Get samplerate in millis
-    var sampleRate = netStats.sampleRate * 1000;
-    // Get date with samplerate's precision
-    var offset = new Date().getTimezoneOffset() * 60 * 1000;
-    var endDate = new Date(Math.floor((new Date().getTime() - offset) / sampleRate)
-                           * sampleRate + offset);
-    var startDate = new Date(endDate.getTime() - (sampleRate * diff));
-    // Calculate the number of samples that should be returned based on the
-    // the samplerate and including final and initial samples.
-    var samples = (endDate.getTime() - startDate.getTime()) / sampleRate + 1;
-
-    // Launch request
-    req = netStats.getNetworkStats({start: startDate, end: endDate, connectionType: type});
-    req.onsuccess = function () {
-      ok(true, "Get system stats request ok");
-      ok(req.result.manifestURL == null, "manifestURL should be null");
-      ok(req.result.connectionType == type, "connectionTypes should be equals");
-      ok(req.result.start.getTime() == startDate.getTime(), "starts should be equals");
-      ok(req.result.end.getTime() == endDate.getTime(), "ends should be equals");
-      var data = req.result.data;
-      ok(Array.isArray(data) && data.length == samples,
-         "data is an array of length " + samples);
-      checkDataDates(data, startDate, endDate, sampleRate);
-      next();
-    };
-    req.onerror = function () {
-      ok(false, "Get system stats for a connectionType failure!");
-    }
-  },
-  function () {
-    ok(true, "Get system stats for all connectionTypes and dates adapted to samplerate");
-    // Prepare get params
-    var diff = 2;
-    // Get samplerate in millis
-    var sampleRate = netStats.sampleRate * 1000;
+    var sampleRate = netStats.sampleRate;
     // Get date with samplerate's precision
     var offset = new Date().getTimezoneOffset() * 60 * 1000;
     var endDate = new Date(Math.floor((new Date().getTime() - offset) / sampleRate)
                            * sampleRate + offset);
     var startDate = new Date(endDate.getTime() - (sampleRate * diff));
     // Calculate the number of samples that should be returned based on the
     // the samplerate and including final and initial samples.
     var samples = (endDate.getTime() - startDate.getTime()) / sampleRate + 1;
 
     // Launch request
-    req = netStats.getNetworkStats({start: startDate, end: endDate});
+    req = netStats.getSamples(network, startDate, endDate);
     req.onsuccess = function () {
-      ok(true, "Get stats request ok");
+      ok(true, "Get system stats request ok");
       ok(req.result.manifestURL == null, "manifestURL should be null");
-      ok(req.result.connectionType == null, "connectionTypes should be null");
-      ok(req.result.start.getTime() == startDate.getTime(), "starts should be equals");
-      ok(req.result.end.getTime() == endDate.getTime(), "ends should be equals");
-      var data = req.result.data;
-      ok(Array.isArray(data) && data.length == samples,
-         "data is an array of length " + samples);
-      checkDataDates(data, startDate, endDate, sampleRate);
-      next();
-    };
-    req.onerror = function () {
-      ok(false, "Get system stats for all connectionTypes failure!");
-    }
-  },
-  function () {
-    ok(true, "Get app stats for a connectionType and dates adapted to samplerate");
-    // Prepare get params
-    var url = 'app://browser.gaiamobile.org/manifest.webapp';
-    var type = netStats.connectionTypes[0];
-    var diff = 2;
-    // Get samplerate in millis
-    var sampleRate = netStats.sampleRate * 1000;
-    // Get date with samplerate's precision
-    var offset = new Date().getTimezoneOffset() * 60 * 1000;
-    var endDate = new Date(Math.floor((new Date().getTime() - offset) / sampleRate)
-                           * sampleRate + offset);
-    var startDate = new Date(endDate.getTime() - (sampleRate * diff));
-    // Calculate the number of samples that should be returned based on the
-    // the samplerate and including final and initial samples.
-    var samples = (endDate.getTime() - startDate.getTime()) / sampleRate + 1;
-
-    // Launch request
-    req = netStats.getNetworkStats({start:          startDate,
-                                    end:            endDate,
-                                    connectionType: type,
-                                    manifestURL:    url});
-    req.onsuccess = function () {
-      ok(true, "Get app stats request ok");
-      ok(req.result.manifestURL == url, "manifestURL should be equals");
-      ok(req.result.connectionType == type, "connectionTypes should be equals");
+      ok(compareNetworks(req.result.network, network), "networks should be equals");
       ok(req.result.start.getTime() == startDate.getTime(), "starts should be equals");
       ok(req.result.end.getTime() == endDate.getTime(), "ends should be equals");
       var data = req.result.data;
       ok(Array.isArray(data) && data.length == samples,
          "data is an array of length " + samples);
       checkDataDates(data, startDate, endDate, sampleRate);
       next();
     };
     req.onerror = function () {
-      ok(false, "Get app stats for a connectionType failure!");
+      ok(false, "Get stats failure!");
     }
   },
   function () {
-    ok(true, "Get app stats for all connectionTypes and dates adapted to samplerate");
+    ok(true, "Get system stats for a network and dates not adapted to samplerate");
     // Prepare get params
-    var url = 'app://browser.gaiamobile.org/manifest.webapp';
+    var network = netStats.availableNetworks[0];
     var diff = 2;
     // Get samplerate in millis
-    var sampleRate = netStats.sampleRate * 1000;
-    // Get date with samplerate's precision
-    var offset = new Date().getTimezoneOffset() * 60 * 1000;
-    var endDate = new Date(Math.floor((new Date().getTime() - offset) / sampleRate)
-                           * sampleRate + offset);
+    var sampleRate = netStats.sampleRate;
+    var endDate = new Date();
     var startDate = new Date(endDate.getTime() - (sampleRate * diff));
     // Calculate the number of samples that should be returned based on the
-    // the samplerate and including final and initial samples.
-    var samples = (endDate.getTime() - startDate.getTime()) / sampleRate + 1;
+    // the samplerate, including final and initial samples and taking into
+    // account that these will be filtered according to precision.
+    var samples = (Math.floor(endDate.getTime() / (sampleRate)) * sampleRate -
+                   Math.floor(startDate.getTime() / (sampleRate)) * sampleRate) / sampleRate + 1;
 
     // Launch request
-    req = netStats.getNetworkStats({start:       startDate,
-                                    end:         endDate,
-                                    manifestURL: url});
+    req = netStats.getSamples(network, startDate, endDate);
     req.onsuccess = function () {
-      ok(true, "Get app stats request ok");
-      ok(req.result.manifestURL == url, "manifestURL should be equals");
-      ok(req.result.connectionType == null, "connectionTypes should be null");
+      ok(true, "Get stats request ok");
+      ok(req.result.manifestURL == null, "manifestURL should be null");
+      ok(compareNetworks(req.result.network, network), "networks should be equals");
       ok(req.result.start.getTime() == startDate.getTime(), "starts should be equals");
       ok(req.result.end.getTime() == endDate.getTime(), "ends should be equals");
       var data = req.result.data;
       ok(Array.isArray(data) && data.length == samples,
          "data is an array of length " + samples);
       checkDataDates(data, startDate, endDate, sampleRate);
       next();
     };
     req.onerror = function () {
-      ok(false, "Get app stats for all connectionTypes failure!");
+      ok(false, "Get stats failure!");
     }
   },
   function () {
-    ok(true, "Get system stats for a connectionType and dates not adapted to samplerate");
-    // Prepare get params
-    var type = netStats.connectionTypes[0];
-    var diff = 2;
-    // Get samplerate in millis
-    var sampleRate = netStats.sampleRate * 1000;
-    var endDate = new Date();
-    var startDate = new Date(endDate.getTime() - (sampleRate * diff));
-    // Calculate the number of samples that should be returned based on the
-    // the samplerate, including final and initial samples and taking into
-    // account that these will be filtered according to precision.
-    var samples = (Math.floor(endDate.getTime() / (sampleRate)) * sampleRate -
-                   Math.floor(startDate.getTime() / (sampleRate)) * sampleRate) / sampleRate + 1;
+    // Test clearStats
+    var network = netStats.availableNetworks[0];
 
-    // Launch request
-    req = netStats.getNetworkStats({start: startDate, end: endDate, connectionType: type});
+    req = netStats.clearStats(network);
     req.onsuccess = function () {
-      ok(true, "Get system stats request ok");
-      ok(req.result.manifestURL == null, "manifestURL should be null");
-      ok(req.result.connectionType == type, "connectionTypes should be equals");
-      ok(req.result.start.getTime() == startDate.getTime(), "starts should be equals");
-      ok(req.result.end.getTime() == endDate.getTime(), "ends should be equals");
-      var data = req.result.data;
-      ok(Array.isArray(data) && data.length == samples,
-         "data is an array of length " + samples);
-      checkDataDates(data, startDate, endDate, sampleRate);
+      ok(true, "clearStats deleted the database");
       next();
     };
     req.onerror = function () {
-      ok(false, "Get system stats for a connectionType failure!");
-    }
-  },
-  function () {
-    ok(true, "Get system stats for all connectionTypes and dates not adapted to samplerate");
-    // Prepare get params
-    var diff = 2;
-    // Get samplerate in millis
-    var sampleRate = netStats.sampleRate * 1000;
-    // Get date with samplerate's precision
-    var endDate = new Date();
-    var startDate = new Date(endDate.getTime() - (sampleRate * diff));
-    // Calculate the number of samples that should be returned based on the
-    // the samplerate, including final and initial samples and taking into
-    // account that these will be filtered according to precision.
-    var samples = (Math.floor(endDate.getTime() / (sampleRate)) * sampleRate -
-                   Math.floor(startDate.getTime() / (sampleRate)) * sampleRate) / sampleRate + 1;
-
-    // Launch request
-    req = netStats.getNetworkStats({start: startDate, end: endDate});
-    req.onsuccess = function () {
-      ok(true, "Get stats request ok");
-      ok(req.result.manifestURL == null, "manifestURL should be null");
-      ok(req.result.connectionType == null, "connectionTypes should be null");
-      ok(req.result.start.getTime() == startDate.getTime(), "starts should be equals");
-      ok(req.result.end.getTime() == endDate.getTime(), "ends should be equals");
-      var data = req.result.data;
-      ok(Array.isArray(data) && data.length == samples,
-         "data is an array of length " + samples);
-      checkDataDates(data, startDate, endDate, sampleRate);
-      next();
-    };
-    req.onerror = function () {
-      ok(false, "Get system stats for all connectionType failure!");
+      ok(false, "clearStats deleted the database");
     }
   },
   function () {
     ok(true, "all done!\n");
     SpecialPowers.removePermission("networkstats-manage", document);
     SimpleTest.finish();
     return;
   }
--- a/dom/network/tests/test_networkstats_enabled_no_perm.html
+++ b/dom/network/tests/test_networkstats_enabled_no_perm.html
@@ -7,29 +7,29 @@
 </head>
 <body>
 <p id="display"></p>
 <div id="content" style="display: none">
 </div>
 <pre id="test">
 <script type="application/javascript">
 
-// Test to ensure NetworkStats is enabled but mozNetworkStats.connectionTypes
+// Test to ensure NetworkStats is enabled but mozNetworkStats.availableNetworks
 // does not work in content.
 
 SpecialPowers.setBoolPref("dom.mozNetworkStats.enabled", true);
 SpecialPowers.removePermission("networkstats-manage", document);
 
 ok('mozNetworkStats' in navigator, "navigator.mozNetworkStats should be accessible if dom.mozNetworkStats.enabled is true");
 
 var error;
 try {
-  navigator.mozNetworkStats.connectionTypes;
-  ok(false, "Accessing navigator.mozNetworkStats.connectionTypes should have thrown!");
+  navigator.mozNetworkStats.availableNetworks;
+  ok(false, "Accessing navigator.mozNetworkStats.availableNetworks should have thrown!");
 } catch (ex) {
   error = ex;
 }
-ok(error, "Got an exception accessing navigator.mozNetworkStats.connectionTypes");
+ok(error, "Got an exception accessing navigator.mozNetworkStats.availableNetworks");
 
 </script>
 </pre>
 </body>
 </html>
--- a/dom/network/tests/unit_stats/test_networkstats_db.js
+++ b/dom/network/tests/unit_stats/test_networkstats_db.js
@@ -1,23 +1,32 @@
 /* Any: copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
 
 Cu.import("resource://gre/modules/NetworkStatsDB.jsm");
 
-const netStatsDb = new NetworkStatsDB(this);
+const netStatsDb = new NetworkStatsDB();
 
 function filterTimestamp(date) {
   var sampleRate = netStatsDb.sampleRate;
   var offset = date.getTimezoneOffset() * 60 * 1000;
   return Math.floor((date.getTime() - offset) / sampleRate) * sampleRate;
 }
 
+function getNetworks() {
+  return [{ id: '0', type: Ci.nsIDOMMozNetworkStatsManager.WIFI },
+          { id: '1234', type: Ci.nsIDOMMozNetworkStatsManager.MOBILE }];
+}
+
+function compareNetworks(networkA, networkB) {
+  return (networkA[0] == networkB[0] && networkA[1] == networkB[1]);
+}
+
 add_test(function test_sampleRate() {
   var sampleRate = netStatsDb.sampleRate;
   do_check_true(sampleRate > 0);
   netStatsDb.sampleRate = 0;
   sampleRate = netStatsDb.sampleRate;
   do_check_true(sampleRate > 0);
 
   run_next_test();
@@ -84,80 +93,99 @@ add_test(function test_fillResultSamples
     aux += sampleRate;
   }
   do_check_true(success);
 
   run_next_test();
 });
 
 add_test(function test_clear() {
-  netStatsDb.clear(function (error, result) {
+  var networks = getNetworks();
+  netStatsDb.clearStats(networks, function (error, result) {
+    do_check_eq(error, null);
+    run_next_test();
+  });
+});
+
+add_test(function test_clear_interface() {
+  var networks = getNetworks();
+  netStatsDb.clearInterfaceStats(networks[0], function (error, result) {
     do_check_eq(error, null);
     run_next_test();
   });
 });
 
 add_test(function test_internalSaveStats_singleSample() {
-  var stats = {appId:          0,
-               connectionType: "wifi",
-               timestamp:      Date.now(),
-               rxBytes:        0,
-               txBytes:        0,
-               rxTotalBytes:   1234,
-               txTotalBytes:   1234};
+  var networks = getNetworks();
+
+  var stats = { appId:        0,
+                network:      [networks[0].id, networks[0].type],
+                timestamp:    Date.now(),
+                rxBytes:      0,
+                txBytes:      0,
+                rxTotalBytes: 1234,
+                txTotalBytes: 1234 };
 
   netStatsDb.dbNewTxn("readwrite", function(txn, store) {
     netStatsDb._saveStats(txn, store, stats);
   }, function(error, result) {
     do_check_eq(error, null);
 
     netStatsDb.logAllRecords(function(error, result) {
       do_check_eq(error, null);
       do_check_eq(result.length, 1);
       do_check_eq(result[0].appId, stats.appId);
-      do_check_eq(result[0].connectionType, stats.connectionType);
+      do_check_true(compareNetworks(result[0].network, stats.network));
       do_check_eq(result[0].timestamp, stats.timestamp);
       do_check_eq(result[0].rxBytes, stats.rxBytes);
       do_check_eq(result[0].txBytes, stats.txBytes);
       do_check_eq(result[0].rxTotalBytes, stats.rxTotalBytes);
       do_check_eq(result[0].txTotalBytes, stats.txTotalBytes);
       run_next_test();
     });
   });
 });
 
 add_test(function test_internalSaveStats_arraySamples() {
-  netStatsDb.clear(function (error, result) {
+  var networks = getNetworks();
+
+  netStatsDb.clearStats(networks, function (error, result) {
     do_check_eq(error, null);
 
+    var network = [networks[0].id, networks[0].type];
+
     var samples = 2;
     var stats = [];
     for (var i = 0; i < samples; i++) {
-      stats.push({appId:          0,
-                  connectionType: "wifi",
-                  timestamp:      Date.now() + (10 * i),
-                  rxBytes:        0,
-                  txBytes:        0,
-                  rxTotalBytes:   1234,
-                  txTotalBytes:   1234});
+      stats.push({ appId:        0,
+                   network:      network,
+                   timestamp:    Date.now() + (10 * i),
+                   rxBytes:      0,
+                   txBytes:      0,
+                   rxTotalBytes: 1234,
+                   txTotalBytes: 1234 });
     }
 
     netStatsDb.dbNewTxn("readwrite", function(txn, store) {
       netStatsDb._saveStats(txn, store, stats);
     }, function(error, result) {
       do_check_eq(error, null);
 
       netStatsDb.logAllRecords(function(error, result) {
         do_check_eq(error, null);
+
+        // Result has one sample more than samples because clear inserts
+        // an empty sample to keep totalBytes synchronized with netd counters
+        result.shift();
         do_check_eq(result.length, samples);
 
         var success = true;
-        for (var i = 0; i < samples; i++) {
+        for (var i = 1; i < samples; i++) {
           if (result[i].appId != stats[i].appId ||
-              result[i].connectionType != stats[i].connectionType ||
+              !compareNetworks(result[i].network, stats[i].network) ||
               result[i].timestamp != stats[i].timestamp ||
               result[i].rxBytes != stats[i].rxBytes ||
               result[i].txBytes != stats[i].txBytes ||
               result[i].rxTotalBytes != stats[i].rxTotalBytes ||
               result[i].txTotalBytes != stats[i].txTotalBytes) {
             success = false;
             break;
           }
@@ -165,59 +193,62 @@ add_test(function test_internalSaveStats
         do_check_true(success);
         run_next_test();
       });
     });
   });
 });
 
 add_test(function test_internalRemoveOldStats() {
-  netStatsDb.clear(function (error, result) {
+  var networks = getNetworks();
+
+  netStatsDb.clearStats(networks, function (error, result) {
     do_check_eq(error, null);
 
+    var network = [networks[0].id, networks[0].type];
     var samples = 10;
     var stats = [];
     for (var i = 0; i < samples - 1; i++) {
-      stats.push({appId:               0,
-                  connectionType: "wifi", timestamp:    Date.now() + (10 * i),
-                  rxBytes:             0, txBytes:      0,
-                  rxTotalBytes:     1234, txTotalBytes: 1234});
+      stats.push({ appId:              0,
+                   network:      network, timestamp:    Date.now() + (10 * i),
+                   rxBytes:            0, txBytes:      0,
+                   rxTotalBytes:    1234, txTotalBytes: 1234 });
     }
 
-    stats.push({appId:               0,
-                connectionType: "wifi", timestamp:    Date.now() + (10 * samples),
-                rxBytes:             0, txBytes:      0,
-                rxTotalBytes:     1234, txTotalBytes: 1234});
+    stats.push({ appId:              0,
+                 network:      network, timestamp:    Date.now() + (10 * samples),
+                 rxBytes:            0, txBytes:      0,
+                 rxTotalBytes:    1234, txTotalBytes: 1234 });
 
     netStatsDb.dbNewTxn("readwrite", function(txn, store) {
       netStatsDb._saveStats(txn, store, stats);
-      var date = stats[stats.length -1].timestamp
+      var date = stats[stats.length - 1].timestamp
                  + (netStatsDb.sampleRate * netStatsDb.maxStorageSamples - 1) - 1;
-      netStatsDb._removeOldStats(txn, store, 0, "wifi", date);
+      netStatsDb._removeOldStats(txn, store, 0, network, date);
     }, function(error, result) {
       do_check_eq(error, null);
 
       netStatsDb.logAllRecords(function(error, result) {
         do_check_eq(error, null);
         do_check_eq(result.length, 1);
 
         run_next_test();
       });
     });
   });
 });
 
-function processSamplesDiff(lastStat, newStat, callback) {
-  netStatsDb.clear(function (error, result){
+function processSamplesDiff(networks, lastStat, newStat, callback) {
+  netStatsDb.clearStats(networks, function (error, result){
     do_check_eq(error, null);
     netStatsDb.dbNewTxn("readwrite", function(txn, store) {
       netStatsDb._saveStats(txn, store, lastStat);
     }, function(error, result) {
       netStatsDb.dbNewTxn("readwrite", function(txn, store) {
-        let request = store.index("connectionType").openCursor(newStat.connectionType, "prev");
+        let request = store.index("network").openCursor(newStat.network, "prev");
         request.onsuccess = function onsuccess(event) {
           let cursor = event.target.result;
           do_check_neq(cursor, null);
           netStatsDb._processSamplesDiff(txn, store, cursor, newStat);
         };
       }, function(error, result) {
         do_check_eq(error, null);
         netStatsDb.logAllRecords(function(error, result) {
@@ -225,292 +256,314 @@ function processSamplesDiff(lastStat, ne
           callback(result);
         });
       });
     });
   });
 }
 
 add_test(function test_processSamplesDiffSameSample() {
+  var networks = getNetworks();
+  var network = [networks[0].id, networks[0].type];
+
   var sampleRate = netStatsDb.sampleRate;
   var date = filterTimestamp(new Date());
-  var lastStat = {appId:               0,
-                  connectionType: "wifi", timestamp:    date,
-                  rxBytes:             0, txBytes:      0,
-                  rxTotalBytes:     1234, txTotalBytes: 1234};
+
+  var lastStat = { appId:              0,
+                   network:      network, timestamp:    date,
+                   rxBytes:            0, txBytes:      0,
+                   rxTotalBytes:    1234, txTotalBytes: 1234 };
 
-  var newStat = {appId:                 0,
-                 connectionType:   "wifi", timestamp:    date,
-                 rxBytes:               0, txBytes:      0,
-                 rxTotalBytes:       2234, txTotalBytes: 2234};
+  var newStat = { appId:              0,
+                  network:      network, timestamp:    date,
+                  rxBytes:            0, txBytes:      0,
+                  rxTotalBytes:    2234, txTotalBytes: 2234 };
 
-  processSamplesDiff(lastStat, newStat, function(result) {
+  processSamplesDiff(networks, lastStat, newStat, function(result) {
     do_check_eq(result.length, 1);
     do_check_eq(result[0].appId, newStat.appId);
-    do_check_eq(result[0].connectionType, newStat.connectionType);
+    do_check_true(compareNetworks(result[0].network, newStat.network));
     do_check_eq(result[0].timestamp, newStat.timestamp);
     do_check_eq(result[0].rxBytes, newStat.rxTotalBytes - lastStat.rxTotalBytes);
     do_check_eq(result[0].txBytes, newStat.txTotalBytes - lastStat.txTotalBytes);
     do_check_eq(result[0].rxTotalBytes, newStat.rxTotalBytes);
     do_check_eq(result[0].txTotalBytes, newStat.txTotalBytes);
     run_next_test();
   });
 });
 
 add_test(function test_processSamplesDiffNextSample() {
+  var networks = getNetworks();
+  var network = [networks[0].id, networks[0].type];
+
   var sampleRate = netStatsDb.sampleRate;
   var date = filterTimestamp(new Date());
-  var lastStat = {appId:               0,
-                  connectionType: "wifi", timestamp:    date,
-                  rxBytes:             0, txBytes:      0,
-                  rxTotalBytes:     1234, txTotalBytes: 1234};
+
+  var lastStat = { appId:              0,
+                   network:      network, timestamp:    date,
+                   rxBytes:            0, txBytes:      0,
+                   rxTotalBytes:    1234, txTotalBytes: 1234 };
 
-  var newStat = {appId:                 0,
-                 connectionType:   "wifi", timestamp:    date + sampleRate,
-                 rxBytes:               0, txBytes:      0,
-                 rxTotalBytes:        500, txTotalBytes: 500};
+  var newStat = { appId:              0,
+                  network:      network, timestamp:    date + sampleRate,
+                  rxBytes:            0, txBytes:      0,
+                  rxTotalBytes:     500, txTotalBytes: 500 };
 
-  processSamplesDiff(lastStat, newStat, function(result) {
+  processSamplesDiff(networks, lastStat, newStat, function(result) {
     do_check_eq(result.length, 2);
     do_check_eq(result[1].appId, newStat.appId);
-    do_check_eq(result[1].connectionType, newStat.connectionType);
+    do_check_true(compareNetworks(result[1].network, newStat.network));
     do_check_eq(result[1].timestamp, newStat.timestamp);
     do_check_eq(result[1].rxBytes, newStat.rxTotalBytes);
     do_check_eq(result[1].txBytes, newStat.txTotalBytes);
     do_check_eq(result[1].rxTotalBytes, newStat.rxTotalBytes);
     do_check_eq(result[1].txTotalBytes, newStat.txTotalBytes);
     run_next_test();
   });
 });
 
 add_test(function test_processSamplesDiffSamplesLost() {
+  var networks = getNetworks();
+  var network = [networks[0].id, networks[0].type];
   var samples = 5;
   var sampleRate = netStatsDb.sampleRate;
   var date = filterTimestamp(new Date());
-  var lastStat = {appId:               0,
-                  connectionType: "wifi", timestamp:    date,
-                  rxBytes:             0, txBytes:      0,
-                  rxTotalBytes:     1234, txTotalBytes: 1234};
+  var lastStat = { appId:             0,
+                   network:     network, timestamp:    date,
+                   rxBytes:           0, txBytes:      0,
+                   rxTotalBytes:   1234, txTotalBytes: 1234 };
 
-  var newStat = {appId:                0,
-                 connectionType:  "wifi", timestamp:    date + (sampleRate * samples),
-                 rxBytes:              0, txBytes:      0,
-                 rxTotalBytes:      2234, txTotalBytes: 2234};
+  var newStat = { appId:              0,
+                  network:      network, timestamp:    date + (sampleRate * samples),
+                  rxBytes:            0, txBytes:      0,
+                  rxTotalBytes:    2234, txTotalBytes: 2234 };
 
-  processSamplesDiff(lastStat, newStat, function(result) {
+  processSamplesDiff(networks, lastStat, newStat, function(result) {
     do_check_eq(result.length, samples + 1);
     do_check_eq(result[0].appId, newStat.appId);
-    do_check_eq(result[samples].connectionType, newStat.connectionType);
+    do_check_true(compareNetworks(result[samples].network, newStat.network));
     do_check_eq(result[samples].timestamp, newStat.timestamp);
     do_check_eq(result[samples].rxBytes, newStat.rxTotalBytes - lastStat.rxTotalBytes);
     do_check_eq(result[samples].txBytes, newStat.txTotalBytes - lastStat.txTotalBytes);
     do_check_eq(result[samples].rxTotalBytes, newStat.rxTotalBytes);
     do_check_eq(result[samples].txTotalBytes, newStat.txTotalBytes);
     run_next_test();
   });
 });
 
 add_test(function test_saveStats() {
-  var stats = {appId:          0,
-               connectionType: "wifi",
-               date:           new Date(),
-               rxBytes:        2234,
-               txBytes:        2234};
+  var networks = getNetworks();
+  var network = [networks[0].id, networks[0].type];
 
-  netStatsDb.clear(function (error, result) {
+  var stats = { appId:       0,
+                networkId:   networks[0].id,
+                networkType: networks[0].type,
+                date:        new Date(),
+                rxBytes:     2234,
+                txBytes:     2234};
+
+  netStatsDb.clearStats(networks, function (error, result) {
     do_check_eq(error, null);
     netStatsDb.saveStats(stats, function(error, result) {
       do_check_eq(error, null);
       netStatsDb.logAllRecords(function(error, result) {
         do_check_eq(error, null);
         do_check_eq(result.length, 1);
         do_check_eq(result[0].appId, stats.appId);
-        do_check_eq(result[0].connectionType, stats.connectionType);
+        do_check_true(compareNetworks(result[0].network, network));
         let timestamp = filterTimestamp(stats.date);
         do_check_eq(result[0].timestamp, timestamp);
         do_check_eq(result[0].rxBytes, 0);
         do_check_eq(result[0].txBytes, 0);
         do_check_eq(result[0].rxTotalBytes, stats.rxBytes);
         do_check_eq(result[0].txTotalBytes, stats.txBytes);
         run_next_test();
       });
     });
   });
 });
 
 add_test(function test_saveAppStats() {
-  var stats = {appId:          1,
-               connectionType: "wifi",
-               date:           new Date(),
-               rxBytes:        2234,
-               txBytes:        2234};
+  var networks = getNetworks();
+  var network = [networks[0].id, networks[0].type];
 
-  netStatsDb.clear(function (error, result) {
+  var stats = { appId:       1,
+                networkId:   networks[0].id,
+                networkType: networks[0].type,
+                date:        new Date(),
+                rxBytes:     2234,
+                txBytes:     2234};
+
+  netStatsDb.clearStats(networks, function (error, result) {
     do_check_eq(error, null);
     netStatsDb.saveStats(stats, function(error, result) {
       do_check_eq(error, null);
       netStatsDb.logAllRecords(function(error, result) {
         do_check_eq(error, null);
-        do_check_eq(result.length, 1);
-        do_check_eq(result[0].appId, stats.appId);
-        do_check_eq(result[0].connectionType, stats.connectionType);
+        // The clear function clears all records of the datbase but
+        // inserts a new element for each [appId, connectionId, connectionType]
+        // record to keep the track of rxTotalBytes / txTotalBytes.
+        // So at this point, we have two records, one for the appId 0 used in
+        // past tests and the new one for appId 1
+        do_check_eq(result.length, 2);
+        do_check_eq(result[1].appId, stats.appId);
+        do_check_true(compareNetworks(result[1].network, network));
         let timestamp = filterTimestamp(stats.date);
-        do_check_eq(result[0].timestamp, timestamp);
-        do_check_eq(result[0].rxBytes, stats.rxBytes);
-        do_check_eq(result[0].txBytes, stats.txBytes);
-        do_check_eq(result[0].rxTotalBytes, 0);
-        do_check_eq(result[0].txTotalBytes, 0);
+        do_check_eq(result[1].timestamp, timestamp);
+        do_check_eq(result[1].rxBytes, stats.rxBytes);
+        do_check_eq(result[1].txBytes, stats.txBytes);
+        do_check_eq(result[1].rxTotalBytes, 0);
+        do_check_eq(result[1].txTotalBytes, 0);
         run_next_test();
       });
     });
   });
 });
 
-function prepareFind(stats, callback) {
-  netStatsDb.clear(function (error, result) {
+function prepareFind(network, stats, callback) {
+  netStatsDb.clearStats(network, function (error, result) {
     do_check_eq(error, null);
     netStatsDb.dbNewTxn("readwrite", function(txn, store) {
       netStatsDb._saveStats(txn, store, stats);
     }, function(error, result) {
         callback(error, result);
     });
   });
 }
 
 add_test(function test_find () {
+  var networks = getNetworks();
+  var networkWifi = [networks[0].id, networks[0].type];
+  var networkMobile = [networks[1].id, networks[1].type]; // Fake mobile interface
+  var appId = 0;
+
   var samples = 5;
   var sampleRate = netStatsDb.sampleRate;
   var start = Date.now();
   var saveDate = filterTimestamp(new Date());
   var end = new Date(start + (sampleRate * (samples - 1)));
   start = new Date(start - sampleRate);
   var stats = [];
   for (var i = 0; i < samples; i++) {
-    stats.push({appId:               0,
-                connectionType: "wifi", timestamp:    saveDate + (sampleRate * i),
-                rxBytes:             0, txBytes:      10,
-                rxTotalBytes:        0, txTotalBytes: 0});
+    stats.push({ appId:              appId,
+                 network:      networkWifi, timestamp:    saveDate + (sampleRate * i),
+                 rxBytes:                0, txBytes:      10,
+                 rxTotalBytes:           0, txTotalBytes: 0 });
 
-    stats.push({appId:                 0,
-                connectionType: "mobile", timestamp:    saveDate + (sampleRate * i),
-                rxBytes:               0, txBytes:      10,
-                rxTotalBytes:          0, txTotalBytes: 0});
+    stats.push({ appId:                appId,
+                 network:      networkMobile, timestamp:    saveDate + (sampleRate * i),
+                 rxBytes:                  0, txBytes:      10,
+                 rxTotalBytes:             0, txTotalBytes: 0 });
   }
 
-  prepareFind(stats, function(error, result) {
+  prepareFind(networks[0], stats, function(error, result) {
     do_check_eq(error, null);
     netStatsDb.find(function (error, result) {
       do_check_eq(error, null);
-      do_check_eq(result.connectionType, "wifi");
+      do_check_eq(result.network.id, networks[0].id);
+      do_check_eq(result.network.type, networks[0].type);
       do_check_eq(result.start.getTime(), start.getTime());
       do_check_eq(result.end.getTime(), end.getTime());
       do_check_eq(result.data.length, samples + 1);
       do_check_eq(result.data[0].rxBytes, null);
       do_check_eq(result.data[1].rxBytes, 0);
       do_check_eq(result.data[samples].rxBytes, 0);
-
-      netStatsDb.findAll(function (error, result) {
-        do_check_eq(error, null);
-        do_check_eq(result.connectionType, null);
-        do_check_eq(result.start.getTime(), start.getTime());
-        do_check_eq(result.end.getTime(), end.getTime());
-        do_check_eq(result.data.length, samples + 1);
-        do_check_eq(result.data[0].rxBytes, null);
-        do_check_eq(result.data[1].rxBytes, 0);
-        do_check_eq(result.data[1].txBytes, 20);
-        do_check_eq(result.data[samples].rxBytes, 0);
-        run_next_test();
-      }, {appId: 0, start: start, end: end});
-    }, {start: start, end: end, connectionType: "wifi", appId: 0});
+      run_next_test();
+    }, networks[0], start, end, appId);
   });
 });
 
 add_test(function test_findAppStats () {
+  var networks = getNetworks();
+  var networkWifi = [networks[0].id, networks[0].type];
+  var networkMobile = [networks[1].id, networks[1].type]; // Fake mobile interface
+
   var samples = 5;
   var sampleRate = netStatsDb.sampleRate;
   var start = Date.now();
   var saveDate = filterTimestamp(new Date());
   var end = new Date(start + (sampleRate * (samples - 1)));
   start = new Date(start - sampleRate);
   var stats = [];
   for (var i = 0; i < samples; i++) {
-    stats.push({appId:               1,
-                connectionType: "wifi", timestamp:    saveDate + (sampleRate * i),
-                rxBytes:             0, txBytes:      10,
-                rxTotalBytes:        0, txTotalBytes: 0});
+    stats.push({ appId:                  1,
+                 network:      networkWifi, timestamp:    saveDate + (sampleRate * i),
+                 rxBytes:                0, txBytes:      10,
+                 rxTotalBytes:           0, txTotalBytes: 0 });
 
-    stats.push({appId:                 1,
-                connectionType: "mobile", timestamp:    saveDate + (sampleRate * i),
-                rxBytes:               0, txBytes:      10,
-                rxTotalBytes:          0, txTotalBytes: 0});
+    stats.push({ appId:                    1,
+                 network:      networkMobile, timestamp:    saveDate + (sampleRate * i),
+                 rxBytes:                  0, txBytes:      10,
+                 rxTotalBytes:             0, txTotalBytes: 0 });
   }
 
-  prepareFind(stats, function(error, result) {
+  prepareFind(networks[0], stats, function(error, result) {
     do_check_eq(error, null);
     netStatsDb.find(function (error, result) {
       do_check_eq(error, null);
-      do_check_eq(result.connectionType, "wifi");
+      do_check_eq(result.network.id, networks[0].id);
+      do_check_eq(result.network.type, networks[0].type);
       do_check_eq(result.start.getTime(), start.getTime());
       do_check_eq(result.end.getTime(), end.getTime());
       do_check_eq(result.data.length, samples + 1);
       do_check_eq(result.data[0].rxBytes, null);
       do_check_eq(result.data[1].rxBytes, 0);
       do_check_eq(result.data[samples].rxBytes, 0);
-
-      netStatsDb.findAll(function (error, result) {
-        do_check_eq(error, null);
-        do_check_eq(result.connectionType, null);
-        do_check_eq(result.start.getTime(), start.getTime());
-        do_check_eq(result.end.getTime(), end.getTime());
-        do_check_eq(result.data.length, samples + 1);
-        do_check_eq(result.data[0].rxBytes, null);
-        do_check_eq(result.data[1].rxBytes, 0);
-        do_check_eq(result.data[1].txBytes, 20);
-        do_check_eq(result.data[samples].rxBytes, 0);
-        run_next_test();
-      }, {start: start, end: end, appId: 1});
-    }, {start: start, end: end, connectionType: "wifi", appId: 1});
+      run_next_test();
+    }, networks[0], start, end, 1);
   });
 });
 
 add_test(function test_saveMultipleAppStats () {
+  var networks = getNetworks();
+  var networkWifi = networks[0];
+  var networkMobile = networks[1]; // Fake mobile interface
+
   var saveDate = filterTimestamp(new Date());
   var cached = Object.create(null);
 
-  cached['1wifi'] = {appId:                 1,
-                     connectionType:   "wifi", date:    new Date(),
-                     rxBytes:               0, txBytes:      10};
+  cached['1wifi'] = { appId:                      1, date:           new Date(),
+                      networkId:     networkWifi.id, networkType: networkWifi.type,
+                      rxBytes:                    0, txBytes:      10 };
 
-  cached['1mobile'] = {appId:                 1,
-                       connectionType: "mobile", date:    new Date(),
-                       rxBytes:               0, txBytes:      10};
+  cached['1mobile'] = { appId:                    1, date:           new Date(),
+                        networkId: networkMobile.id, networkType: networkMobile.type,
+                        rxBytes:                  0, txBytes:      10 };
 
-  cached['2wifi'] = {appId:                 2,
-                     connectionType:   "wifi", date:    new Date(),
-                     rxBytes:               0, txBytes:      10};
+  cached['2wifi'] = { appId:                      2, date:           new Date(),
+                      networkId:     networkWifi.id, networkType: networkWifi.type,
+                      rxBytes:                    0, txBytes:      10 };
 
-  cached['2mobile'] = {appId:                 2,
-                       connectionType: "mobile", date:    new Date(),
-                       rxBytes:               0, txBytes:      10};
+  cached['2mobile'] = { appId:                    2, date:           new Date(),
+                        networkId: networkMobile.id, networkType: networkMobile.type,
+                        rxBytes:                  0, txBytes:      10 };
 
   let keys = Object.keys(cached);
   let index = 0;
 
-  netStatsDb.clear(function (error, result) {
+  networks.push(networkMobile);
+  netStatsDb.clearStats(networks, function (error, result) {
     do_check_eq(error, null);
     netStatsDb.saveStats(cached[keys[index]],
       function callback(error, result) {
         do_check_eq(error, null);
 
         if (index == keys.length - 1) {
           netStatsDb.logAllRecords(function(error, result) {
+          // Again, result has two samples more than expected samples because
+          // clear inserts one empty sample for each network to keep totalBytes
+          // synchronized with netd counters. so the first two samples have to
+          // be discarted.
+          result.shift();
+          result.shift();
+
           do_check_eq(error, null);
           do_check_eq(result.length, 4);
           do_check_eq(result[0].appId, 1);
-          do_check_eq(result[0].connectionType, 'mobile');
+          do_check_true(compareNetworks(result[0].network,[networkWifi.id, networkWifi.type]));
           do_check_eq(result[0].rxBytes, 0);
           do_check_eq(result[0].txBytes, 10);
           run_next_test();
           });
         }
 
         index += 1;
         netStatsDb.saveStats(cached[keys[index]], callback);
--- a/dom/network/tests/unit_stats/test_networkstats_service.js
+++ b/dom/network/tests/unit_stats/test_networkstats_service.js
@@ -1,56 +1,64 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
 
 add_test(function test_clearDB() {
-  NetworkStatsService._db.clear(function onDBCleared(error, result) {
+  var networks = NetworkStatsService.availableNetworks();
+  NetworkStatsService._db.clearStats(networks, function onDBCleared(error, result) {
     do_check_eq(result, null);
     run_next_test();
   });
 });
 
+function getNetworkId() {
+  var network = (NetworkStatsService.availableNetworks())[0];
+  return NetworkStatsService.getNetworkId(network.id, network.type);
+}
 
 add_test(function test_networkStatsAvailable_ok() {
+  var netId = getNetworkId();
   NetworkStatsService.networkStatsAvailable(function (success, msg) {
     do_check_eq(success, true);
     run_next_test();
-  }, Ci.nsINetworkInterface.NETWORK_TYPE_WIFI, true, 1234, 4321, new Date());
+  }, netId, true, 1234, 4321, new Date());
 });
 
 add_test(function test_networkStatsAvailable_failure() {
+  var netId = getNetworkId();
   NetworkStatsService.networkStatsAvailable(function (success, msg) {
     do_check_eq(success, false);
     run_next_test();
-  }, Ci.nsINetworkInterface.NETWORK_TYPE_WIFI, false, 1234, 4321, new Date());
+  }, netId, false, 1234, 4321, new Date());
 });
 
-add_test(function test_update_invalidConnection() {
+add_test(function test_update_invalidNetwork() {
   NetworkStatsService.update(-1, function (success, msg) {
     do_check_eq(success, false);
-    do_check_eq(msg, "Invalid network type -1");
+    do_check_eq(msg, "Invalid network -1");
     run_next_test();
   });
 });
 
 add_test(function test_update() {
-  NetworkStatsService.update(Ci.nsINetworkInterface.NETWORK_TYPE_WIFI, function (success, msg) {
+  var netId = getNetworkId();
+  NetworkStatsService.update(netId, function (success, msg) {
     do_check_eq(success, true);
     run_next_test();
   });
 });
 
 add_test(function test_updateQueueIndex() {
-  NetworkStatsService.updateQueue = [{type: 0, callbacks: null},
-                                     {type: 1, callbacks: null},
-                                     {type: 2, callbacks: null},
-                                     {type: 3, callbacks: null},
-                                     {type: 4, callbacks: null}];
+  NetworkStatsService.updateQueue = [{netId: 0, callbacks: null},
+                                     {netId: 1, callbacks: null},
+                                     {netId: 2, callbacks: null},
+                                     {netId: 3, callbacks: null},
+                                     {netId: 4, callbacks: null}];
   var index = NetworkStatsService.updateQueueIndex(3);
   do_check_eq(index, 3);
   index = NetworkStatsService.updateQueueIndex(10);
   do_check_eq(index, -1);
 
   NetworkStatsService.updateQueue = [];
   run_next_test();
 });
@@ -58,48 +66,54 @@ add_test(function test_updateQueueIndex(
 add_test(function test_updateAllStats() {
   NetworkStatsService.updateAllStats(function(success, msg) {
     do_check_eq(success, true);
     run_next_test();
   });
 });
 
 add_test(function test_updateStats_ok() {
-  NetworkStatsService.updateStats(Ci.nsINetworkInterface.NETWORK_TYPE_WIFI, function(success, msg){
+  var netId = getNetworkId();
+  NetworkStatsService.updateStats(netId, function(success, msg){
     do_check_eq(success, true);
     run_next_test();
   });
 });
 
 add_test(function test_updateStats_failure() {
   NetworkStatsService.updateStats(-1, function(success, msg){
     do_check_eq(success, false);
     run_next_test();
   });
 });
 
 add_test(function test_queue() {
-  // Fill connections with fake network interfaces (wlan0 and rmnet0)
+  // Fill networks with fake network interfaces
   // to enable netd async requests
-  NetworkStatsService._connectionTypes[Ci.nsINetworkInterface.NETWORK_TYPE_WIFI]
-                     .network.name = 'wlan0';
-  NetworkStatsService._connectionTypes[Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE]
-                     .network.name = 'rmnet0';
+  var network = {id: "1234", type: Ci.nsIDOMMozNetworkStatsManager.MOBILE};
+  var netId1 = NetworkStatsService.getNetworkId(network.id, network.type);
+  NetworkStatsService._networks[netId1] = { network: network,
+                                            interfaceName: "net1" };
 
-  NetworkStatsService.updateStats(Ci.nsINetworkInterface.NETWORK_TYPE_WIFI);
-  NetworkStatsService.updateStats(Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE);
+  network = {id: "5678", type: Ci.nsIDOMMozNetworkStatsManager.MOBILE};
+  var netId2 = NetworkStatsService.getNetworkId(network.id, network.type);
+  NetworkStatsService._networks[netId2] = { network: network,
+                                            interfaceName: "net2" };
+
+  NetworkStatsService.updateStats(netId1);
+  NetworkStatsService.updateStats(netId2);
   do_check_eq(NetworkStatsService.updateQueue.length, 2);
   do_check_eq(NetworkStatsService.updateQueue[0].callbacks.length, 1);
 
   var callback = function(success, msg) {
     return;
   };
 
-  NetworkStatsService.updateStats(Ci.nsINetworkInterface.NETWORK_TYPE_WIFI, callback);
-  NetworkStatsService.updateStats(Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE, callback);
+  NetworkStatsService.updateStats(netId1, callback);
+  NetworkStatsService.updateStats(netId2, callback);
 
   do_check_eq(NetworkStatsService.updateQueue.length, 2);
   do_check_eq(NetworkStatsService.updateQueue[0].callbacks.length, 2);
   do_check_eq(NetworkStatsService.updateQueue[0].callbacks[0], null);
   do_check_neq(NetworkStatsService.updateQueue[0].callbacks[1], null);
 
   run_next_test();
 });
--- a/dom/network/tests/unit_stats/test_networkstats_service_proxy.js
+++ b/dom/network/tests/unit_stats/test_networkstats_service_proxy.js
@@ -4,116 +4,154 @@
 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
 
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 
 XPCOMUtils.defineLazyServiceGetter(this, "nssProxy",
                                    "@mozilla.org/networkstatsServiceProxy;1",
                                    "nsINetworkStatsServiceProxy");
 
+function mokConvertNetworkInterface() {
+  NetworkStatsService.convertNetworkInterface = function(aNetwork) {
+    if (aNetwork.type != Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE &&
+        aNetwork.type != Ci.nsINetworkInterface.NETWORK_TYPE_WIFI) {
+      return null;
+    }
+
+    let id = '0';
+    if (aNetwork.type == Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE) {
+      id = '1234'
+    }
+
+    let netId = this.getNetworkId(id, aNetwork.type);
+
+    if (!this._networks[netId]) {
+      this._networks[netId] = Object.create(null);
+      this._networks[netId].network = { id: id,
+                                        type: aNetwork.type };
+    }
+
+    return netId;
+  };
+}
+
 add_test(function test_saveAppStats() {
   var cachedAppStats = NetworkStatsService.cachedAppStats;
   var timestamp = NetworkStatsService.cachedAppStatsDate.getTime();
   var samples = 5;
 
+  // Create to fake nsINetworkInterfaces. As nsINetworkInterface can not
+  // be instantiated, these two vars will emulate it by filling the properties
+  // that will be used.
+  var wifi = {type: Ci.nsINetworkInterface.NETWORK_TYPE_WIFI, id: "0"};
+  var mobile = {type: Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE, id: "1234"};
+
+  // Insert fake mobile network interface in NetworkStatsService
+  var mobileNetId = NetworkStatsService.getNetworkId(mobile.id, mobile.type);
+
   do_check_eq(Object.keys(cachedAppStats).length, 0);
 
   for (var i = 0; i < samples; i++) {
-    nssProxy.saveAppStats(1, Ci.nsINetworkInterface.NETWORK_TYPE_WIFI,
-                          timestamp, 10, 20);
+    nssProxy.saveAppStats(1, wifi, timestamp, 10, 20);
 
-    nssProxy.saveAppStats(1, Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE,
-                          timestamp, 10, 20);
+    nssProxy.saveAppStats(1, mobile, timestamp, 10, 20);
   }
 
-  var key1 = 1 + 'wifi';
-  var key2 = 1 + 'mobile';
+  var key1 = 1 + NetworkStatsService.getNetworkId(wifi.id, wifi.type);
+  var key2 = 1 + mobileNetId;
 
   do_check_eq(Object.keys(cachedAppStats).length, 2);
   do_check_eq(cachedAppStats[key1].appId, 1);
-  do_check_eq(cachedAppStats[key1].connectionType, 'wifi');
+  do_check_eq(cachedAppStats[key1].networkId, wifi.id);
+  do_check_eq(cachedAppStats[key1].networkType, wifi.type);
   do_check_eq(new Date(cachedAppStats[key1].date).getTime() / 1000,
               Math.floor(timestamp / 1000));
   do_check_eq(cachedAppStats[key1].rxBytes, 50);
   do_check_eq(cachedAppStats[key1].txBytes, 100);
   do_check_eq(cachedAppStats[key2].appId, 1);
-  do_check_eq(cachedAppStats[key2].connectionType, 'mobile');
+  do_check_eq(cachedAppStats[key2].networkId, mobile.id);
+  do_check_eq(cachedAppStats[key2].networkType, mobile.type);
   do_check_eq(new Date(cachedAppStats[key2].date).getTime() / 1000,
               Math.floor(timestamp / 1000));
   do_check_eq(cachedAppStats[key2].rxBytes, 50);
   do_check_eq(cachedAppStats[key2].txBytes, 100);
 
   run_next_test();
 });
 
 add_test(function test_saveAppStatsWithDifferentDates() {
   var today = NetworkStatsService.cachedAppStatsDate;
   var tomorrow = new Date(today.getTime() + (24 * 60 * 60 * 1000));
-  var key = 1 + 'wifi';
+
+  var wifi = {type: Ci.nsINetworkInterface.NETWORK_TYPE_WIFI, id: "0"};
+  var mobile = {type: Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE, id: "1234"};
+
+  var key = 1 + NetworkStatsService.getNetworkId(wifi.id, wifi.type);
 
   NetworkStatsService.updateCachedAppStats(
     function (success, msg) {
       do_check_eq(success, true);
 
       do_check_eq(Object.keys(NetworkStatsService.cachedAppStats).length, 0);
 
-      nssProxy.saveAppStats(1, Ci.nsINetworkInterface.NETWORK_TYPE_WIFI,
-                            today.getTime(), 10, 20);
+      nssProxy.saveAppStats(1, wifi, today.getTime(), 10, 20);
 
-      nssProxy.saveAppStats(1, Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE,
-                            today.getTime(), 10, 20);
+      nssProxy.saveAppStats(1, mobile, today.getTime(), 10, 20);
 
       var saveAppStatsCb = {
         notify: function notify(success, message) {
           do_check_eq(success, true);
 
           var cachedAppStats = NetworkStatsService.cachedAppStats;
-          var key = 2 + 'mobile';
+          var key = 2 + NetworkStatsService.getNetworkId(mobile.id, mobile.type);
           do_check_eq(Object.keys(cachedAppStats).length, 1);
           do_check_eq(cachedAppStats[key].appId, 2);
-          do_check_eq(cachedAppStats[key].connectionType, 'mobile');
+          do_check_eq(cachedAppStats[key].networkId, mobile.id);
+          do_check_eq(cachedAppStats[key].networkType, mobile.type);
           do_check_eq(new Date(cachedAppStats[key].date).getTime() / 1000,
                       Math.floor(tomorrow.getTime() / 1000));
           do_check_eq(cachedAppStats[key].rxBytes, 30);
           do_check_eq(cachedAppStats[key].txBytes, 40);
 
           run_next_test();
         }
       };
 
-      nssProxy.saveAppStats(2, Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE,
-                            tomorrow.getTime(), 30, 40, saveAppStatsCb);
+      nssProxy.saveAppStats(2, mobile, tomorrow.getTime(), 30, 40, saveAppStatsCb);
     }
   );
 });
 
 add_test(function test_saveAppStatsWithMaxCachedTraffic() {
   var timestamp = NetworkStatsService.cachedAppStatsDate.getTime();
   var maxtraffic = NetworkStatsService.maxCachedTraffic;
+  var wifi = {type: Ci.nsINetworkInterface.NETWORK_TYPE_WIFI, id: "0"};
 
   NetworkStatsService.updateCachedAppStats(
     function (success, msg) {
       do_check_eq(success, true);
 
       var cachedAppStats = NetworkStatsService.cachedAppStats;
       do_check_eq(Object.keys(cachedAppStats).length, 0);
 
-      nssProxy.saveAppStats(1, Ci.nsINetworkInterface.NETWORK_TYPE_WIFI,
-                            timestamp, 10, 20);
+      nssProxy.saveAppStats(1, wifi, timestamp, 10, 20);
 
       do_check_eq(Object.keys(cachedAppStats).length, 1);
 
-      nssProxy.saveAppStats(1, Ci.nsINetworkInterface.NETWORK_TYPE_WIFI,
-                            timestamp, maxtraffic, 20);
+      nssProxy.saveAppStats(1, wifi, timestamp, maxtraffic, 20);
 
       do_check_eq(Object.keys(cachedAppStats).length, 0);
 
       run_next_test();
   });
 });
 
 function run_test() {
   do_get_profile();
 
   Cu.import("resource://gre/modules/NetworkStatsService.jsm");
 
+  // Function convertNetworkInterface of NetworkStatsService causes errors when dealing
+  // with RIL to get the iccid, so overwrite it.
+  mokConvertNetworkInterface();
+
   run_next_test();
 }