Bug 937041 - Update tests. r=gene
authorAlbert Crespell <acperez@tid.es>
Fri, 15 Nov 2013 16:56:17 +0100
changeset 156501 f067a2bcf0797022a90ef6b9fb82fb2980adc0b1
parent 156500 bbc9461bdd134aebc8d35486faed4e7db49153fd
child 156502 d75bd16671d259dce33c7d589893170ea5e8c176
push id25683
push userryanvm@gmail.com
push dateThu, 21 Nov 2013 01:42:57 +0000
treeherdermozilla-central@cf378dddfac8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgene
bugs937041
milestone28.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 937041 - Update 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
--- a/dom/network/tests/test_networkstats_basics.html
+++ b/dom/network/tests/test_networkstats_basics.html
@@ -14,21 +14,16 @@
 
 function test() {
   ok('mozNetworkStats' in navigator, "navigator.mozMozNetworkStats should exist");
   ok(navigator.mozNetworkStats, "navigator.mozNetworkStats returns an object");
 
   netStats = navigator.mozNetworkStats;
 
   // Test IDL attributes
-  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('maxStorageAge' in netStats,
    "maxStorageAge should be a NetworkStats attribute");
   ok(netStats.maxStorageAge > 0,
@@ -66,46 +61,66 @@ function compareNetworks(networkA, netwo
 }
 
 var req;
 var index = -1;
 var netStats = null;
 
 var steps = [
   function () {
+    // Test getAvailableNetworks
+    req = netStats.getAvailableNetworks();
+    req.onsuccess = function () {
+      ok(true, "getAvailableNetworks request ok");
+      ok(Array.isArray(req.result) && req.result.length > 0,
+         "getAvailableNetworks returns an array not empty");
+      next();
+    };
+    req.onerror = function () {
+      ok(false, "getAvailableNetworks failure!");
+    }
+  },
+  function () {
     // Test clearAllStats
     req = netStats.clearAllStats();
     req.onsuccess = function () {
       ok(true, "clearAllStats deleted the database");
       next();
     };
     req.onerror = function () {
       ok(false, "clearAllStats deleted the database");
     }
   },
   function () {
     // Check if getSamples launch exception when start is greather than end
 
     // Prepare get params
-    var network = netStats.availableNetworks[0];
-    // Get dates
-    var endDate = new Date();
-    var startDate = new Date(endDate.getTime() + 1000);
+    req = netStats.getAvailableNetworks();
+    req.onsuccess = function () {
+      var network = req.result[0];
+
+      // Get dates
+      var endDate = new Date();
+      var startDate = new Date(endDate.getTime() + 1000);
 
-    try {
-      netStats.getSamples(network, startDate, endDate);
-    } catch(ex) {
-      ok(true, "getSamples launch exception when start is greater than end");
+      try {
+        netStats.getSamples(network, startDate, endDate);
+      } catch(ex) {
+        ok(true, "getSamples launch exception when start is greater than end");
+        next();
+        return;
+      }
+
+      ok(false, "getSamples launch exception when start is greater than end");
       next();
       return;
+    };
+    req.onerror = function () {
+      ok(false, "Error getting networks!");
     }
-
-    ok(false, "getSamples launch exception when start is greater than end");
-    next();
-    return;
   },
   function () {
     // Test if call getSamples with network of type different than
     // nsIDOMMozNetworkStatsInterface launch an exception
 
     // Prepare get params
     var network = "wifi";
     var endDate = new Date();
@@ -122,129 +137,161 @@ var steps = [
 
     ok(false, "getSamples launch exception if network is not " +
               "a nsIDOMMozNetworkStatsInterface");
   },
   function () {
     // Test if call getSamples with start 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);
-    startDate = startDate.toString();
+    req = netStats.getAvailableNetworks();
+    req.onsuccess = function () {
+      var network = req.result[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;
-    }
+      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");
+      ok(false, "getSamples launch exception when start param is not a Date");
+    };
+    req.onerror = function () {
+      ok(false, "Error getting networks!");
+    };
   },
   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();
+    req = netStats.getAvailableNetworks();
+    req.onsuccess = function () {
+      var network = req.result[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;
-    }
+      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");
+      ok(false, "getSamples launch exception when end param is not a Date");
+    };
+    req.onerror = function () {
+      ok(false, "Error getting networks!");
+    };
   },
   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;
-    // 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;
+    req = netStats.getAvailableNetworks();
+    req.onsuccess = function () {
+      var network = req.result[0];
+      var diff = 2;
+      // Get samplerate in millis
+      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.getSamples(network, startDate, endDate);
-    req.onsuccess = function () {
-      ok(true, "Get system 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();
+      // Launch request
+      req = netStats.getSamples(network, startDate, endDate);
+      req.onsuccess = function () {
+        ok(true, "Get system 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 stats failure!");
+      }
     };
     req.onerror = function () {
-      ok(false, "Get stats failure!");
-    }
+      ok(false, "Error getting networks!");
+    };
   },
   function () {
     ok(true, "Get system stats for a network and dates not adapted to samplerate");
     // Prepare get params
-    var network = netStats.availableNetworks[0];
-    var diff = 2;
-    // Get samplerate in millis
-    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, 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;
+    req = netStats.getAvailableNetworks();
+    req.onsuccess = function () {
+      var network = req.result[0];
+      var diff = 2;
+      // Get samplerate in millis
+      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, 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.getSamples(network, startDate, endDate);
-    req.onsuccess = function () {
-      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();
+      // Launch request
+      req = netStats.getSamples(network, startDate, endDate);
+      req.onsuccess = function () {
+        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 stats failure!");
+      }
     };
     req.onerror = function () {
-      ok(false, "Get stats failure!");
-    }
+      ok(false, "Error getting networks!");
+    };
   },
   function () {
     // Test clearStats
-    var network = netStats.availableNetworks[0];
+    req = netStats.getAvailableNetworks();
+    req.onsuccess = function () {
+      var network = req.result[0];
 
-    req = netStats.clearStats(network);
-    req.onsuccess = function () {
-      ok(true, "clearStats deleted the database");
-      next();
+      req = netStats.clearStats(network);
+      req.onsuccess = function () {
+        ok(true, "clearStats deleted the database");
+        next();
+      };
+      req.onerror = function () {
+        ok(false, "clearStats deleted the database");
+      }
     };
     req.onerror = function () {
-      ok(false, "clearStats deleted the database");
-    }
+      ok(false, "Error getting networks!");
+    };
   },
   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.availableNetworks
+// Test to ensure NetworkStats is enabled but mozNetworkStats.getAvailableNetworks
 // 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.availableNetworks;
-  ok(false, "Accessing navigator.mozNetworkStats.availableNetworks should have thrown!");
+  navigator.mozNetworkStats.getAvailableNetworks;
+  ok(false, "Accessing navigator.mozNetworkStats.getAvailableNetworks should have thrown!");
 } catch (ex) {
   error = ex;
 }
-ok(error, "Got an exception accessing navigator.mozNetworkStats.availableNetworks");
+ok(error, "Got an exception accessing navigator.mozNetworkStats.getAvailableNetworks");
 
 </script>
 </pre>
 </body>
 </html>
--- a/dom/network/tests/unit_stats/test_networkstats_db.js
+++ b/dom/network/tests/unit_stats/test_networkstats_db.js
@@ -2,16 +2,22 @@
    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();
 
+function clearWholeDB(callback) {
+  netStatsDb.dbNewTxn("readwrite", function(aTxn, aStore) {
+    aStore.delete();
+  }, callback);
+}
+
 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 },
@@ -569,10 +575,14 @@ add_test(function test_saveMultipleAppSt
         netStatsDb.saveStats(cached[keys[index]], callback);
     });
   });
 });
 
 function run_test() {
   do_get_profile();
 
-  run_next_test();
+  // Clear whole database to avoid start tests with unknown state
+  // due to previous tests.
+  clearWholeDB(function(){
+    run_next_test();
+  });
 }
--- a/dom/network/tests/unit_stats/test_networkstats_service.js
+++ b/dom/network/tests/unit_stats/test_networkstats_service.js
@@ -1,55 +1,76 @@
 /* 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;
 
+function getNetworks(callback) {
+  NetworkStatsService._db.getAvailableNetworks(function onGetNetworks(aError, aResult) {
+    callback(aError, aResult);
+  });
+}
+
 add_test(function test_clearDB() {
-  var networks = NetworkStatsService.availableNetworks();
-  NetworkStatsService._db.clearStats(networks, function onDBCleared(error, result) {
-    do_check_eq(result, null);
-    run_next_test();
+  getNetworks(function onGetNetworks(error, result) {
+    do_check_eq(error, null);
+    var networks = result;
+    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);
+function getNetworkId(callback) {
+  getNetworks(function onGetNetworks(error, result) {
+    do_check_eq(error, null);
+    var netId = NetworkStatsService.getNetworkId(result[0].id, result[0].type);
+    callback(null, netId);
+  });
 }
 
 add_test(function test_networkStatsAvailable_ok() {
-  var netId = getNetworkId();
-  NetworkStatsService.networkStatsAvailable(function (success, msg) {
-    do_check_eq(success, true);
-    run_next_test();
-  }, netId, true, 1234, 4321, new Date());
+  getNetworkId(function onGetId(error, result) {
+    do_check_eq(error, null);
+    var netId = result;
+    NetworkStatsService.networkStatsAvailable(function (success, msg) {
+      do_check_eq(success, true);
+      run_next_test();
+    }, 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();
-  }, netId, false, 1234, 4321, new Date());
+  getNetworkId(function onGetId(error, result) {
+    do_check_eq(error, null);
+    var netId = result;
+    NetworkStatsService.networkStatsAvailable(function (success, msg) {
+      do_check_eq(success, false);
+      run_next_test();
+    }, netId, false, 1234, 4321, new Date());
+  });
 });
 
 add_test(function test_update_invalidNetwork() {
   NetworkStatsService.update(-1, function (success, msg) {
     do_check_eq(success, false);
     do_check_eq(msg, "Invalid network -1");
     run_next_test();
   });
 });
 
 add_test(function test_update() {
-  var netId = getNetworkId();
-  NetworkStatsService.update(netId, function (success, msg) {
-    do_check_eq(success, true);
-    run_next_test();
+  getNetworkId(function onGetId(error, result) {
+    do_check_eq(error, null);
+    var netId = result;
+    NetworkStatsService.update(netId, function (success, msg) {
+      do_check_eq(success, true);
+      run_next_test();
+    });
   });
 });
 
 add_test(function test_updateQueueIndex() {
   NetworkStatsService.updateQueue = [{netId: 0, callbacks: null},
                                      {netId: 1, callbacks: null},
                                      {netId: 2, callbacks: null},
                                      {netId: 3, callbacks: null},
@@ -66,20 +87,23 @@ 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() {
-  var netId = getNetworkId();
-  NetworkStatsService.updateStats(netId, function(success, msg){
-    do_check_eq(success, true);
-    run_next_test();
+  getNetworkId(function onGetId(error, result) {
+    do_check_eq(error, null);
+    var netId = result;
+    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();
   });