Bug 964228 - Part 2: Tests update. r=albert
authorJohn Shih <jshih@mozilla.com>
Thu, 06 Mar 2014 10:03:35 +0800
changeset 190861 49ec48262f79a8da0f8a915eeeda2eb311b36c20
parent 190860 861f6c26ef3a1eaf4881c41ae23b406637fc6760
child 190862 77b90f9bc6dbe97c6147d95b4329eeb3449ef6bb
push idunknown
push userunknown
push dateunknown
reviewersalbert
bugs964228
milestone30.0a1
Bug 964228 - Part 2: Tests update. r=albert
dom/network/tests/unit_stats/test_networkstats_service.js
dom/network/tests/unit_stats/test_networkstats_service_proxy.js
--- a/dom/network/tests/unit_stats/test_networkstats_service.js
+++ b/dom/network/tests/unit_stats/test_networkstats_service.js
@@ -2,16 +2,18 @@
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
 
 const NETWORK_STATUS_READY   = 0;
 const NETWORK_STATUS_STANDBY = 1;
 const NETWORK_STATUS_AWAY    = 2;
 
+const QUEUE_TYPE_UPDATE_STATS = 0;
+
 var wifiId = '00';
 
 function getNetworks(callback) {
   NetworkStatsService._db.getAvailableNetworks(function onGetNetworks(aError, aResult) {
     callback(aError, aResult);
   });
 }
 
@@ -75,21 +77,21 @@ add_test(function test_update() {
     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},
-                                     {netId: 4, callbacks: null}];
+  NetworkStatsService.updateQueue = [{netId: 0, callbacks: null, queueType: QUEUE_TYPE_UPDATE_STATS},
+                                     {netId: 1, callbacks: null, queueType: QUEUE_TYPE_UPDATE_STATS},
+                                     {netId: 2, callbacks: null, queueType: QUEUE_TYPE_UPDATE_STATS},
+                                     {netId: 3, callbacks: null, queueType: QUEUE_TYPE_UPDATE_STATS},
+                                     {netId: 4, callbacks: null, queueType: QUEUE_TYPE_UPDATE_STATS}];
   var index = NetworkStatsService.updateQueueIndex(3);
   do_check_eq(index, 3);
   index = NetworkStatsService.updateQueueIndex(10);
   do_check_eq(index, -1);
 
   NetworkStatsService.updateQueue = [];
   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
@@ -31,181 +31,170 @@ function mokConvertNetworkInterface() {
 
     return netId;
   };
 }
 
 add_test(function test_saveAppStats() {
   var cachedStats = NetworkStatsService.cachedStats;
   var timestamp = NetworkStatsService.cachedStatsDate.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(cachedStats).length, 0);
 
-  for (var i = 0; i < samples; i++) {
-    nssProxy.saveAppStats(1, wifi, timestamp, 10, 20, false);
-
-    nssProxy.saveAppStats(1, mobile, timestamp, 10, 20, false);
-  }
-
-  var key1 = 1 + "" + NetworkStatsService.getNetworkId(wifi.id, wifi.type);
-  var key2 = 1 + "" + mobileNetId + "";
+  nssProxy.saveAppStats(1, wifi, timestamp, 10, 20, false,
+                        function (success, message) {
+    do_check_eq(success, true);
+    nssProxy.saveAppStats(1, mobile, timestamp, 10, 20, false,
+                          function (success, message) {
+      var key1 = 1 + "" + NetworkStatsService.getNetworkId(wifi.id, wifi.type);
+      var key2 = 1 + "" + mobileNetId + "";
 
-  do_check_eq(Object.keys(cachedStats).length, 2);
-  do_check_eq(cachedStats[key1].appId, 1);
-  do_check_eq(cachedStats[key1].serviceType.length, 0);
-  do_check_eq(cachedStats[key1].networkId, wifi.id);
-  do_check_eq(cachedStats[key1].networkType, wifi.type);
-  do_check_eq(new Date(cachedStats[key1].date).getTime() / 1000,
-              Math.floor(timestamp / 1000));
-  do_check_eq(cachedStats[key1].rxBytes, 50);
-  do_check_eq(cachedStats[key1].txBytes, 100);
-  do_check_eq(cachedStats[key2].appId, 1);
-  do_check_eq(cachedStats[key1].serviceType.length, 0);
-  do_check_eq(cachedStats[key2].networkId, mobile.id);
-  do_check_eq(cachedStats[key2].networkType, mobile.type);
-  do_check_eq(new Date(cachedStats[key2].date).getTime() / 1000,
-              Math.floor(timestamp / 1000));
-  do_check_eq(cachedStats[key2].rxBytes, 50);
-  do_check_eq(cachedStats[key2].txBytes, 100);
+      do_check_eq(Object.keys(cachedStats).length, 2);
+      do_check_eq(cachedStats[key1].appId, 1);
+      do_check_eq(cachedStats[key1].serviceType.length, 0);
+      do_check_eq(cachedStats[key1].networkId, wifi.id);
+      do_check_eq(cachedStats[key1].networkType, wifi.type);
+      do_check_eq(new Date(cachedStats[key1].date).getTime() / 1000,
+                  Math.floor(timestamp / 1000));
+      do_check_eq(cachedStats[key1].rxBytes, 10);
+      do_check_eq(cachedStats[key1].txBytes, 20);
+      do_check_eq(cachedStats[key2].appId, 1);
+      do_check_eq(cachedStats[key1].serviceType.length, 0);
+      do_check_eq(cachedStats[key2].networkId, mobile.id);
+      do_check_eq(cachedStats[key2].networkType, mobile.type);
+      do_check_eq(new Date(cachedStats[key2].date).getTime() / 1000,
+                  Math.floor(timestamp / 1000));
+      do_check_eq(cachedStats[key2].rxBytes, 10);
+      do_check_eq(cachedStats[key2].txBytes, 20);
 
-  run_next_test();
+      run_next_test();
+    });
+  });
 });
 
 add_test(function test_saveServiceStats() {
   var timestamp = NetworkStatsService.cachedStatsDate.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);
 
-  NetworkStatsService.updateCachedStats(
-    function (success, msg) {
-      do_check_eq(success, true);
+  NetworkStatsService.updateCachedStats(function (success, msg) {
+    do_check_eq(success, true);
 
-      var cachedStats = NetworkStatsService.cachedStats;
-      do_check_eq(Object.keys(cachedStats).length, 0);
+    var cachedStats = NetworkStatsService.cachedStats;
+    do_check_eq(Object.keys(cachedStats).length, 0);
 
-      var serviceType = 'FakeType';
-      for (var i = 0; i < samples; i++) {
-        nssProxy.saveServiceStats(serviceType, wifi, timestamp, 10, 20, false);
-
-        nssProxy.saveServiceStats(serviceType, mobile, timestamp, 10, 20, false);
-      }
-
-      var key1 = 0 + "" + serviceType +
-                 NetworkStatsService.getNetworkId(wifi.id, wifi.type);
-      var key2 = 0 + "" + serviceType + mobileNetId + "";
+    var serviceType = 'FakeType';
+    nssProxy.saveServiceStats(serviceType, wifi, timestamp, 10, 20, false,
+                              function (success, message) {
+      do_check_eq(success, true);
+      nssProxy.saveServiceStats(serviceType, mobile, timestamp, 10, 20, false,
+                                function (success, message) {
+        do_check_eq(success, true);
+        var key1 = 0 + "" + serviceType +
+                   NetworkStatsService.getNetworkId(wifi.id, wifi.type);
+        var key2 = 0 + "" + serviceType + mobileNetId + "";
 
-      do_check_eq(Object.keys(cachedStats).length, 2);
-      do_check_eq(cachedStats[key1].appId, 0);
-      do_check_eq(cachedStats[key1].serviceType, serviceType);
-      do_check_eq(cachedStats[key1].networkId, wifi.id);
-      do_check_eq(cachedStats[key1].networkType, wifi.type);
-      do_check_eq(new Date(cachedStats[key1].date).getTime() / 1000,
-                  Math.floor(timestamp / 1000));
-      do_check_eq(cachedStats[key1].rxBytes, 50);
-      do_check_eq(cachedStats[key1].txBytes, 100);
-      do_check_eq(cachedStats[key2].appId, 0);
-      do_check_eq(cachedStats[key1].serviceType, serviceType);
-      do_check_eq(cachedStats[key2].networkId, mobile.id);
-      do_check_eq(cachedStats[key2].networkType, mobile.type);
-      do_check_eq(new Date(cachedStats[key2].date).getTime() / 1000,
-                  Math.floor(timestamp / 1000));
-      do_check_eq(cachedStats[key2].rxBytes, 50);
-      do_check_eq(cachedStats[key2].txBytes, 100);
+        do_check_eq(Object.keys(cachedStats).length, 2);
+        do_check_eq(cachedStats[key1].appId, 0);
+        do_check_eq(cachedStats[key1].serviceType, serviceType);
+        do_check_eq(cachedStats[key1].networkId, wifi.id);
+        do_check_eq(cachedStats[key1].networkType, wifi.type);
+        do_check_eq(new Date(cachedStats[key1].date).getTime() / 1000,
+                    Math.floor(timestamp / 1000));
+        do_check_eq(cachedStats[key1].rxBytes, 10);
+        do_check_eq(cachedStats[key1].txBytes, 20);
+        do_check_eq(cachedStats[key2].appId, 0);
+        do_check_eq(cachedStats[key1].serviceType, serviceType);
+        do_check_eq(cachedStats[key2].networkId, mobile.id);
+        do_check_eq(cachedStats[key2].networkType, mobile.type);
+        do_check_eq(new Date(cachedStats[key2].date).getTime() / 1000,
+                    Math.floor(timestamp / 1000));
+        do_check_eq(cachedStats[key2].rxBytes, 10);
+        do_check_eq(cachedStats[key2].txBytes, 20);
 
-      run_next_test();
-    }
-  );
+        run_next_test();
+      });
+    });
+  });
 });
 
 add_test(function test_saveStatsWithDifferentDates() {
   var today = NetworkStatsService.cachedStatsDate;
   var tomorrow = new Date(today.getTime() + (24 * 60 * 60 * 1000));
 
-  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.updateCachedStats(
-    function (success, msg) {
-      do_check_eq(success, true);
-
-      do_check_eq(Object.keys(NetworkStatsService.cachedStats).length, 0);
+  NetworkStatsService.updateCachedStats(function (success, message) {
+    do_check_eq(success, true);
 
-      nssProxy.saveAppStats(1, wifi, today.getTime(), 10, 20, false);
-
-      nssProxy.saveAppStats(1, mobile, today.getTime(), 10, 20, false);
-
-      var saveStatsCb = {
-        notify: function notify(success, message) {
-          do_check_eq(success, true);
+    do_check_eq(Object.keys(NetworkStatsService.cachedStats).length, 0);
+    nssProxy.saveAppStats(1, mobile, today.getTime(), 10, 20, false,
+                          function (success, message) {
+      do_check_eq(success, true);
+      nssProxy.saveAppStats(2, mobile, tomorrow.getTime(), 30, 40, false,
+                            function (success, message) {
+        do_check_eq(success, true);
 
-          var cachedStats = NetworkStatsService.cachedStats;
-          var key = 2 + "" +
-                    NetworkStatsService.getNetworkId(mobile.id, mobile.type);
-          do_check_eq(Object.keys(cachedStats).length, 1);
-          do_check_eq(cachedStats[key].appId, 2);
-          do_check_eq(cachedStats[key].networkId, mobile.id);
-          do_check_eq(cachedStats[key].networkType, mobile.type);
-          do_check_eq(new Date(cachedStats[key].date).getTime() / 1000,
-                      Math.floor(tomorrow.getTime() / 1000));
-          do_check_eq(cachedStats[key].rxBytes, 30);
-          do_check_eq(cachedStats[key].txBytes, 40);
+        var cachedStats = NetworkStatsService.cachedStats;
+        var key = 2 + "" +
+                  NetworkStatsService.getNetworkId(mobile.id, mobile.type);
+        do_check_eq(Object.keys(cachedStats).length, 1);
+        do_check_eq(cachedStats[key].appId, 2);
+        do_check_eq(cachedStats[key].networkId, mobile.id);
+        do_check_eq(cachedStats[key].networkType, mobile.type);
+        do_check_eq(new Date(cachedStats[key].date).getTime() / 1000,
+                    Math.floor(tomorrow.getTime() / 1000));
+        do_check_eq(cachedStats[key].rxBytes, 30);
+        do_check_eq(cachedStats[key].txBytes, 40);
 
-          run_next_test();
-        }
-      };
-
-      nssProxy.saveAppStats(2, mobile, tomorrow.getTime(), 30, 40, false,
-                            saveStatsCb);
-    }
-  );
+        run_next_test();
+      });
+    });
+  });
 });
 
 add_test(function test_saveStatsWithMaxCachedTraffic() {
   var timestamp = NetworkStatsService.cachedStatsDate.getTime();
   var maxtraffic = NetworkStatsService.maxCachedTraffic;
   var wifi = {type: Ci.nsINetworkInterface.NETWORK_TYPE_WIFI, id: "0"};
 
-  NetworkStatsService.updateCachedStats(
-    function (success, msg) {
-      do_check_eq(success, true);
-
-      var cachedStats = NetworkStatsService.cachedStats;
-      do_check_eq(Object.keys(cachedStats).length, 0);
-
-      nssProxy.saveAppStats(1, wifi, timestamp, 10, 20, false);
+  NetworkStatsService.updateCachedStats(function (success, message) {
+    do_check_eq(success, true);
 
+    var cachedStats = NetworkStatsService.cachedStats;
+    do_check_eq(Object.keys(cachedStats).length, 0);
+    nssProxy.saveAppStats(1, wifi, timestamp, 10, 20, false,
+                          function (success, message) {
+      do_check_eq(success, true);
       do_check_eq(Object.keys(cachedStats).length, 1);
-
-      nssProxy.saveAppStats(1, wifi, timestamp, maxtraffic, 20, false);
+      nssProxy.saveAppStats(1, wifi, timestamp, maxtraffic, 20, false,
+                            function (success, message) {
+        do_check_eq(success, true);
+        do_check_eq(Object.keys(cachedStats).length, 0);
 
-      do_check_eq(Object.keys(cachedStats).length, 0);
-
-      run_next_test();
-    }
-  );
+        run_next_test();
+      });
+    });
+  });
 });
 
 function run_test() {
   do_get_profile();
 
   Cu.import("resource://gre/modules/NetworkStatsService.jsm");
 
   // Function convertNetworkInterface of NetworkStatsService causes errors when dealing