Bug 964270 - Part 2: Update tests. r=gene, a=1.3+
[gecko.git] / dom / network / tests / unit_stats / test_networkstats_service.js
blob4e7db18683729093d405ab6f49c223b69b60effe
1 /* Any copyright is dedicated to the Public Domain.
2    http://creativecommons.org/publicdomain/zero/1.0/ */
4 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
6 const NETWORK_STATUS_READY   = 0;
7 const NETWORK_STATUS_STANDBY = 1;
8 const NETWORK_STATUS_AWAY    = 2;
10 function getNetworks(callback) {
11   NetworkStatsService._db.getAvailableNetworks(function onGetNetworks(aError, aResult) {
12     callback(aError, aResult);
13   });
16 add_test(function test_clearDB() {
17   getNetworks(function onGetNetworks(error, result) {
18     do_check_eq(error, null);
19     var networks = result;
20     NetworkStatsService._db.clearStats(networks, function onDBCleared(error, result) {
21       do_check_eq(result, null);
22       run_next_test();
23     });
24   });
25 });
27 function getNetworkId(callback) {
28   getNetworks(function onGetNetworks(error, result) {
29     do_check_eq(error, null);
30     var netId = NetworkStatsService.getNetworkId(result[0].id, result[0].type);
31     callback(null, netId);
32   });
35 add_test(function test_networkStatsAvailable_ok() {
36   getNetworkId(function onGetId(error, result) {
37     do_check_eq(error, null);
38     var netId = result;
39     NetworkStatsService.networkStatsAvailable(function (success, msg) {
40       do_check_eq(success, true);
41       run_next_test();
42     }, netId, true, 1234, 4321, new Date());
43   });
44 });
46 add_test(function test_networkStatsAvailable_failure() {
47   getNetworkId(function onGetId(error, result) {
48     do_check_eq(error, null);
49     var netId = result;
50     NetworkStatsService.networkStatsAvailable(function (success, msg) {
51       do_check_eq(success, false);
52       run_next_test();
53     }, netId, false, 1234, 4321, new Date());
54   });
55 });
57 add_test(function test_update_invalidNetwork() {
58   NetworkStatsService.update(-1, function (success, msg) {
59     do_check_eq(success, false);
60     do_check_eq(msg, "Invalid network -1");
61     run_next_test();
62   });
63 });
65 add_test(function test_update() {
66   getNetworkId(function onGetId(error, result) {
67     do_check_eq(error, null);
68     var netId = result;
69     NetworkStatsService.update(netId, function (success, msg) {
70       do_check_eq(success, true);
71       run_next_test();
72     });
73   });
74 });
76 add_test(function test_updateQueueIndex() {
77   NetworkStatsService.updateQueue = [{netId: 0, callbacks: null},
78                                      {netId: 1, callbacks: null},
79                                      {netId: 2, callbacks: null},
80                                      {netId: 3, callbacks: null},
81                                      {netId: 4, callbacks: null}];
82   var index = NetworkStatsService.updateQueueIndex(3);
83   do_check_eq(index, 3);
84   index = NetworkStatsService.updateQueueIndex(10);
85   do_check_eq(index, -1);
87   NetworkStatsService.updateQueue = [];
88   run_next_test();
89 });
91 add_test(function test_updateAllStats() {
92   NetworkStatsService._networks[wifiId].status = NETWORK_STATUS_READY;
93   NetworkStatsService.updateAllStats(function(success, msg) {
94     do_check_eq(success, true);
95     NetworkStatsService._networks[wifiId].status = NETWORK_STATUS_STANDBY;
96     NetworkStatsService.updateAllStats(function(success, msg) {
97       do_check_eq(success, true);
98       NetworkStatsService._networks[wifiId].status = NETWORK_STATUS_AWAY;
99       NetworkStatsService.updateAllStats(function(success, msg) {
100         do_check_eq(success, true);
101         run_next_test();
102       });
103     });
104   });
107 add_test(function test_updateStats_ok() {
108   getNetworkId(function onGetId(error, result) {
109     do_check_eq(error, null);
110     var netId = result;
111     NetworkStatsService.updateStats(netId, function(success, msg){
112       do_check_eq(success, true);
113       run_next_test();
114     });
115   });
118 add_test(function test_updateStats_failure() {
119   NetworkStatsService.updateStats(-1, function(success, msg){
120     do_check_eq(success, false);
121     run_next_test();
122   });
125 add_test(function test_queue() {
126   // Fill networks with fake network interfaces
127   // to enable netd async requests
128   var network = {id: "1234", type: Ci.nsIDOMMozNetworkStatsManager.MOBILE};
129   var netId1 = NetworkStatsService.getNetworkId(network.id, network.type);
130   NetworkStatsService._networks[netId1] = { network: network,
131                                             interfaceName: "net1" };
133   network = {id: "5678", type: Ci.nsIDOMMozNetworkStatsManager.MOBILE};
134   var netId2 = NetworkStatsService.getNetworkId(network.id, network.type);
135   NetworkStatsService._networks[netId2] = { network: network,
136                                             interfaceName: "net2" };
138   NetworkStatsService.updateStats(netId1);
139   NetworkStatsService.updateStats(netId2);
140   do_check_eq(NetworkStatsService.updateQueue.length, 2);
141   do_check_eq(NetworkStatsService.updateQueue[0].callbacks.length, 1);
143   var callback = function(success, msg) {
144     return;
145   };
147   NetworkStatsService.updateStats(netId1, callback);
148   NetworkStatsService.updateStats(netId2, callback);
150   do_check_eq(NetworkStatsService.updateQueue.length, 2);
151   do_check_eq(NetworkStatsService.updateQueue[0].callbacks.length, 2);
152   do_check_eq(NetworkStatsService.updateQueue[0].callbacks[0], null);
153   do_check_neq(NetworkStatsService.updateQueue[0].callbacks[1], null);
155   // Clear queue because in test environment requests for mobile networks
156   // can not be handled.
157   NetworkStatsService.updateQueue =  [];
158   run_next_test();
161 var wifiId = '00';
163 add_test(function test_updateThreshold() {
164   let alarm = { networkId: wifiId, threshold: 10000 };
166   NetworkStatsService._updateThreshold(alarm, function onSet(error, threshold){
167     do_check_eq(error, null);
168     do_check_neq(threshold.systemThreshold, undefined);
169     do_check_neq(threshold.absoluteThreshold, undefined);
170     run_next_test();
171   });
174 var testPageURL = "http://test.com";
175 var testManifestURL = "http://test.com/manifest.webapp";
177 add_test(function test_setAlarm() {
178   let alarm = { id: null,
179                 networkId: wifiId,
180                 threshold: 10000,
181                 absoluteThreshold: null,
182                 alarmStart: null,
183                 alarmEnd: null,
184                 data: null,
185                 pageURL: testPageURL,
186                 manifestURL: testManifestURL };
188   NetworkStatsService._setAlarm(alarm, function onSet(error, result) {
189     do_check_eq(result, 1);
190     run_next_test();
191   });
194 add_test(function test_setAlarm_invalid_threshold() {
195   let alarm = { id: null,
196                 networkId: wifiId,
197                 threshold: -10000,
198                 absoluteThreshold: null,
199                 alarmStart: null,
200                 alarmEnd: null,
201                 data: null,
202                 pageURL: testPageURL,
203                 manifestURL: testManifestURL };
205   NetworkStatsService._networks[wifiId].status = NETWORK_STATUS_READY;
207   NetworkStatsService._setAlarm(alarm, function onSet(error, result) {
208     do_check_eq(error, "InvalidStateError");
209     run_next_test();
210   });
213 add_test(function test_fireAlarm() {
214   // Add a fake alarm into database.
215   let alarm = { id: null,
216                 networkId: wifiId,
217                 threshold: 10000,
218                 absoluteThreshold: null,
219                 alarmStart: null,
220                 alarmEnd: null,
221                 data: null,
222                 pageURL: testPageURL,
223                 manifestURL: testManifestURL };
225   NetworkStatsService._db.addAlarm(alarm, function addSuccessCb(error, newId) {
226     NetworkStatsService._db.getAlarms(Ci.nsINetworkInterface.NETWORK_TYPE_WIFI,
227                                       testManifestURL, function onGet(error, result) {
228       do_check_eq(error, null);
229       do_check_eq(result.length, 1);
231       // Result of getAlarms is based on expected child's data format, so
232       // some changes are needed to be able to use it.
233       result[0].networkId = wifiId;
234       result[0].pageURL = testPageURL;
235       result[0].manifestURL = testManifestURL;
237       NetworkStatsService._fireAlarm(result[0], false);
238       NetworkStatsService._db.getAlarms(Ci.nsINetworkInterface.NETWORK_TYPE_WIFI,
239                                         testManifestURL, function onGet(error, result) {
240         do_check_eq(error, undefined);
241         do_check_eq(result.length, 0);
242         run_next_test();
243       });
244     });
245   });
248 function run_test() {
249   do_get_profile();
251   Cu.import("resource://gre/modules/NetworkStatsService.jsm");
252   run_next_test();