1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/network/tests/unit_stats/test_networkstats_service.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,290 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; 1.8 + 1.9 +const NETWORK_STATUS_READY = 0; 1.10 +const NETWORK_STATUS_STANDBY = 1; 1.11 +const NETWORK_STATUS_AWAY = 2; 1.12 + 1.13 +const QUEUE_TYPE_UPDATE_STATS = 0; 1.14 + 1.15 +var wifiId = '00'; 1.16 + 1.17 +function getNetworks(callback) { 1.18 + NetworkStatsService._db.getAvailableNetworks(function onGetNetworks(aError, aResult) { 1.19 + callback(aError, aResult); 1.20 + }); 1.21 +} 1.22 + 1.23 +add_test(function test_clearDB() { 1.24 + getNetworks(function onGetNetworks(error, result) { 1.25 + do_check_eq(error, null); 1.26 + var networks = result; 1.27 + networks.forEach(function(network, index) { 1.28 + networks[index] = {network: network, networkId: NetworkStatsService.getNetworkId(network.id, network.type)}; 1.29 + }, this); 1.30 + 1.31 + NetworkStatsService._db.clearStats(networks, function onDBCleared(error, result) { 1.32 + do_check_eq(error, null); 1.33 + run_next_test(); 1.34 + }); 1.35 + }); 1.36 +}); 1.37 + 1.38 +function getNetworkId(callback) { 1.39 + getNetworks(function onGetNetworks(error, result) { 1.40 + do_check_eq(error, null); 1.41 + var netId = NetworkStatsService.getNetworkId(result[0].id, result[0].type); 1.42 + callback(null, netId); 1.43 + }); 1.44 +} 1.45 + 1.46 +add_test(function test_networkStatsAvailable_ok() { 1.47 + getNetworkId(function onGetId(error, result) { 1.48 + do_check_eq(error, null); 1.49 + var netId = result; 1.50 + NetworkStatsService.networkStatsAvailable(function (success, msg) { 1.51 + do_check_eq(success, true); 1.52 + run_next_test(); 1.53 + }, netId, true, 1234, 4321, new Date()); 1.54 + }); 1.55 +}); 1.56 + 1.57 +add_test(function test_networkStatsAvailable_failure() { 1.58 + getNetworkId(function onGetId(error, result) { 1.59 + do_check_eq(error, null); 1.60 + var netId = result; 1.61 + NetworkStatsService.networkStatsAvailable(function (success, msg) { 1.62 + do_check_eq(success, false); 1.63 + run_next_test(); 1.64 + }, netId, false, 1234, 4321, new Date()); 1.65 + }); 1.66 +}); 1.67 + 1.68 +add_test(function test_update_invalidNetwork() { 1.69 + NetworkStatsService.update(-1, function (success, msg) { 1.70 + do_check_eq(success, false); 1.71 + do_check_eq(msg, "Invalid network -1"); 1.72 + run_next_test(); 1.73 + }); 1.74 +}); 1.75 + 1.76 +add_test(function test_update() { 1.77 + getNetworkId(function onGetId(error, result) { 1.78 + do_check_eq(error, null); 1.79 + var netId = result; 1.80 + NetworkStatsService.update(netId, function (success, msg) { 1.81 + do_check_eq(success, true); 1.82 + run_next_test(); 1.83 + }); 1.84 + }); 1.85 +}); 1.86 + 1.87 +add_test(function test_updateQueueIndex() { 1.88 + NetworkStatsService.updateQueue = [{netId: 0, callbacks: null, queueType: QUEUE_TYPE_UPDATE_STATS}, 1.89 + {netId: 1, callbacks: null, queueType: QUEUE_TYPE_UPDATE_STATS}, 1.90 + {netId: 2, callbacks: null, queueType: QUEUE_TYPE_UPDATE_STATS}, 1.91 + {netId: 3, callbacks: null, queueType: QUEUE_TYPE_UPDATE_STATS}, 1.92 + {netId: 4, callbacks: null, queueType: QUEUE_TYPE_UPDATE_STATS}]; 1.93 + var index = NetworkStatsService.updateQueueIndex(3); 1.94 + do_check_eq(index, 3); 1.95 + index = NetworkStatsService.updateQueueIndex(10); 1.96 + do_check_eq(index, -1); 1.97 + 1.98 + NetworkStatsService.updateQueue = []; 1.99 + run_next_test(); 1.100 +}); 1.101 + 1.102 +add_test(function test_updateAllStats() { 1.103 + NetworkStatsService._networks[wifiId].status = NETWORK_STATUS_READY; 1.104 + NetworkStatsService.updateAllStats(function(success, msg) { 1.105 + do_check_eq(success, true); 1.106 + NetworkStatsService._networks[wifiId].status = NETWORK_STATUS_STANDBY; 1.107 + NetworkStatsService.updateAllStats(function(success, msg) { 1.108 + do_check_eq(success, true); 1.109 + NetworkStatsService._networks[wifiId].status = NETWORK_STATUS_AWAY; 1.110 + NetworkStatsService.updateAllStats(function(success, msg) { 1.111 + do_check_eq(success, true); 1.112 + run_next_test(); 1.113 + }); 1.114 + }); 1.115 + }); 1.116 +}); 1.117 + 1.118 +add_test(function test_updateStats_ok() { 1.119 + getNetworkId(function onGetId(error, result) { 1.120 + do_check_eq(error, null); 1.121 + var netId = result; 1.122 + NetworkStatsService.updateStats(netId, function(success, msg){ 1.123 + do_check_eq(success, true); 1.124 + run_next_test(); 1.125 + }); 1.126 + }); 1.127 +}); 1.128 + 1.129 +add_test(function test_updateStats_failure() { 1.130 + NetworkStatsService.updateStats(-1, function(success, msg){ 1.131 + do_check_eq(success, false); 1.132 + run_next_test(); 1.133 + }); 1.134 +}); 1.135 + 1.136 +// Define Mockup function to simulate a request to netd 1.137 +function MockNetdRequest(aCallback) { 1.138 + var timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); 1.139 + var event = { 1.140 + notify: function (timer) { 1.141 + aCallback(); 1.142 + } 1.143 + }; 1.144 + 1.145 + timer.initWithCallback(event, 100, Ci.nsITimer.TYPE_ONE_SHOT); 1.146 +} 1.147 + 1.148 +add_test(function test_queue() { 1.149 + 1.150 + // Overwrite update function of NetworkStatsService to avoid netd errors due to use 1.151 + // fake interfaces. First, original function is stored to restore it at the end of the 1.152 + // test. 1.153 + var updateFunctionBackup = NetworkStatsService.update; 1.154 + 1.155 + NetworkStatsService.update = function update(aNetId, aCallback) { 1.156 + MockNetdRequest(function () { 1.157 + if (aCallback) { 1.158 + aCallback(true, "ok"); 1.159 + } 1.160 + }); 1.161 + }; 1.162 + 1.163 + // Fill networks with fake network interfaces to enable netd async requests. 1.164 + var network = {id: "1234", type: Ci.nsIDOMMozNetworkStatsManager.MOBILE}; 1.165 + var netId1 = NetworkStatsService.getNetworkId(network.id, network.type); 1.166 + NetworkStatsService._networks[netId1] = { network: network, 1.167 + interfaceName: "net1" }; 1.168 + 1.169 + network = {id: "5678", type: Ci.nsIDOMMozNetworkStatsManager.MOBILE}; 1.170 + var netId2 = NetworkStatsService.getNetworkId(network.id, network.type); 1.171 + NetworkStatsService._networks[netId2] = { network: network, 1.172 + interfaceName: "net2" }; 1.173 + 1.174 + NetworkStatsService.updateStats(netId1); 1.175 + NetworkStatsService.updateStats(netId2); 1.176 + do_check_eq(NetworkStatsService.updateQueue.length, 2); 1.177 + do_check_eq(NetworkStatsService.updateQueue[0].callbacks.length, 1); 1.178 + 1.179 + var i = 0; 1.180 + var updateCount = 0; 1.181 + var callback = function(success, msg) { 1.182 + i++; 1.183 + if (i >= updateCount) { 1.184 + NetworkStatsService.update = updateFunctionBackup; 1.185 + run_next_test(); 1.186 + } 1.187 + }; 1.188 + 1.189 + NetworkStatsService.updateStats(netId1, callback); 1.190 + updateCount++; 1.191 + NetworkStatsService.updateStats(netId2, callback); 1.192 + updateCount++; 1.193 + 1.194 + do_check_eq(NetworkStatsService.updateQueue.length, 2); 1.195 + do_check_eq(NetworkStatsService.updateQueue[0].callbacks.length, 2); 1.196 + do_check_eq(NetworkStatsService.updateQueue[0].callbacks[0], null); 1.197 + do_check_neq(NetworkStatsService.updateQueue[0].callbacks[1], null); 1.198 +}); 1.199 + 1.200 +add_test(function test_getAlarmQuota() { 1.201 + let alarm = { networkId: wifiId, absoluteThreshold: 10000 }; 1.202 + 1.203 + NetworkStatsService._getAlarmQuota(alarm, function onSet(error, quota){ 1.204 + do_check_eq(error, null); 1.205 + do_check_neq(quota, undefined); 1.206 + do_check_eq(alarm.absoluteThreshold, alarm.relativeThreshold); 1.207 + run_next_test(); 1.208 + }); 1.209 +}); 1.210 + 1.211 +var testPageURL = "http://test.com"; 1.212 +var testManifestURL = "http://test.com/manifest.webapp"; 1.213 + 1.214 +add_test(function test_setAlarm() { 1.215 + let alarm = { id: null, 1.216 + networkId: wifiId, 1.217 + threshold: 10000, 1.218 + absoluteThreshold: null, 1.219 + alarmStart: null, 1.220 + alarmEnd: null, 1.221 + data: null, 1.222 + pageURL: testPageURL, 1.223 + manifestURL: testManifestURL }; 1.224 + 1.225 + NetworkStatsService._setAlarm(alarm, function onSet(error, result) { 1.226 + do_check_eq(result, 1); 1.227 + run_next_test(); 1.228 + }); 1.229 +}); 1.230 + 1.231 +add_test(function test_setAlarm_invalid_threshold() { 1.232 + let alarm = { id: null, 1.233 + networkId: wifiId, 1.234 + threshold: -10000, 1.235 + absoluteThreshold: null, 1.236 + alarmStart: null, 1.237 + alarmEnd: null, 1.238 + data: null, 1.239 + pageURL: testPageURL, 1.240 + manifestURL: testManifestURL }; 1.241 + 1.242 + NetworkStatsService._networks[wifiId].status = NETWORK_STATUS_READY; 1.243 + 1.244 + NetworkStatsService._setAlarm(alarm, function onSet(error, result) { 1.245 + do_check_eq(error, "InvalidStateError"); 1.246 + run_next_test(); 1.247 + }); 1.248 +}); 1.249 + 1.250 +add_test(function test_fireAlarm() { 1.251 + // Add a fake alarm into database. 1.252 + let alarm = { id: null, 1.253 + networkId: wifiId, 1.254 + threshold: 10000, 1.255 + absoluteThreshold: null, 1.256 + alarmStart: null, 1.257 + alarmEnd: null, 1.258 + data: null, 1.259 + pageURL: testPageURL, 1.260 + manifestURL: testManifestURL }; 1.261 + 1.262 + // Set wifi status to standby to avoid connecting to netd when adding an alarm. 1.263 + NetworkStatsService._networks[wifiId].status = NETWORK_STATUS_STANDBY; 1.264 + 1.265 + NetworkStatsService._db.addAlarm(alarm, function addSuccessCb(error, newId) { 1.266 + NetworkStatsService._db.getAlarms(Ci.nsINetworkInterface.NETWORK_TYPE_WIFI, 1.267 + testManifestURL, function onGet(error, result) { 1.268 + do_check_eq(error, null); 1.269 + do_check_eq(result.length, 1); 1.270 + 1.271 + // Result of getAlarms is based on expected child's data format, so 1.272 + // some changes are needed to be able to use it. 1.273 + result[0].networkId = wifiId; 1.274 + result[0].pageURL = testPageURL; 1.275 + result[0].manifestURL = testManifestURL; 1.276 + 1.277 + NetworkStatsService._fireAlarm(result[0], false); 1.278 + NetworkStatsService._db.getAlarms(Ci.nsINetworkInterface.NETWORK_TYPE_WIFI, 1.279 + testManifestURL, function onGet(error, result) { 1.280 + do_check_eq(error, undefined); 1.281 + do_check_eq(result.length, 0); 1.282 + run_next_test(); 1.283 + }); 1.284 + }); 1.285 + }); 1.286 +}); 1.287 + 1.288 +function run_test() { 1.289 + do_get_profile(); 1.290 + 1.291 + Cu.import("resource://gre/modules/NetworkStatsService.jsm"); 1.292 + run_next_test(); 1.293 +}