|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; |
|
5 |
|
6 const NETWORK_STATUS_READY = 0; |
|
7 const NETWORK_STATUS_STANDBY = 1; |
|
8 const NETWORK_STATUS_AWAY = 2; |
|
9 |
|
10 const QUEUE_TYPE_UPDATE_STATS = 0; |
|
11 |
|
12 var wifiId = '00'; |
|
13 |
|
14 function getNetworks(callback) { |
|
15 NetworkStatsService._db.getAvailableNetworks(function onGetNetworks(aError, aResult) { |
|
16 callback(aError, aResult); |
|
17 }); |
|
18 } |
|
19 |
|
20 add_test(function test_clearDB() { |
|
21 getNetworks(function onGetNetworks(error, result) { |
|
22 do_check_eq(error, null); |
|
23 var networks = result; |
|
24 networks.forEach(function(network, index) { |
|
25 networks[index] = {network: network, networkId: NetworkStatsService.getNetworkId(network.id, network.type)}; |
|
26 }, this); |
|
27 |
|
28 NetworkStatsService._db.clearStats(networks, function onDBCleared(error, result) { |
|
29 do_check_eq(error, null); |
|
30 run_next_test(); |
|
31 }); |
|
32 }); |
|
33 }); |
|
34 |
|
35 function getNetworkId(callback) { |
|
36 getNetworks(function onGetNetworks(error, result) { |
|
37 do_check_eq(error, null); |
|
38 var netId = NetworkStatsService.getNetworkId(result[0].id, result[0].type); |
|
39 callback(null, netId); |
|
40 }); |
|
41 } |
|
42 |
|
43 add_test(function test_networkStatsAvailable_ok() { |
|
44 getNetworkId(function onGetId(error, result) { |
|
45 do_check_eq(error, null); |
|
46 var netId = result; |
|
47 NetworkStatsService.networkStatsAvailable(function (success, msg) { |
|
48 do_check_eq(success, true); |
|
49 run_next_test(); |
|
50 }, netId, true, 1234, 4321, new Date()); |
|
51 }); |
|
52 }); |
|
53 |
|
54 add_test(function test_networkStatsAvailable_failure() { |
|
55 getNetworkId(function onGetId(error, result) { |
|
56 do_check_eq(error, null); |
|
57 var netId = result; |
|
58 NetworkStatsService.networkStatsAvailable(function (success, msg) { |
|
59 do_check_eq(success, false); |
|
60 run_next_test(); |
|
61 }, netId, false, 1234, 4321, new Date()); |
|
62 }); |
|
63 }); |
|
64 |
|
65 add_test(function test_update_invalidNetwork() { |
|
66 NetworkStatsService.update(-1, function (success, msg) { |
|
67 do_check_eq(success, false); |
|
68 do_check_eq(msg, "Invalid network -1"); |
|
69 run_next_test(); |
|
70 }); |
|
71 }); |
|
72 |
|
73 add_test(function test_update() { |
|
74 getNetworkId(function onGetId(error, result) { |
|
75 do_check_eq(error, null); |
|
76 var netId = result; |
|
77 NetworkStatsService.update(netId, function (success, msg) { |
|
78 do_check_eq(success, true); |
|
79 run_next_test(); |
|
80 }); |
|
81 }); |
|
82 }); |
|
83 |
|
84 add_test(function test_updateQueueIndex() { |
|
85 NetworkStatsService.updateQueue = [{netId: 0, callbacks: null, queueType: QUEUE_TYPE_UPDATE_STATS}, |
|
86 {netId: 1, callbacks: null, queueType: QUEUE_TYPE_UPDATE_STATS}, |
|
87 {netId: 2, callbacks: null, queueType: QUEUE_TYPE_UPDATE_STATS}, |
|
88 {netId: 3, callbacks: null, queueType: QUEUE_TYPE_UPDATE_STATS}, |
|
89 {netId: 4, callbacks: null, queueType: QUEUE_TYPE_UPDATE_STATS}]; |
|
90 var index = NetworkStatsService.updateQueueIndex(3); |
|
91 do_check_eq(index, 3); |
|
92 index = NetworkStatsService.updateQueueIndex(10); |
|
93 do_check_eq(index, -1); |
|
94 |
|
95 NetworkStatsService.updateQueue = []; |
|
96 run_next_test(); |
|
97 }); |
|
98 |
|
99 add_test(function test_updateAllStats() { |
|
100 NetworkStatsService._networks[wifiId].status = NETWORK_STATUS_READY; |
|
101 NetworkStatsService.updateAllStats(function(success, msg) { |
|
102 do_check_eq(success, true); |
|
103 NetworkStatsService._networks[wifiId].status = NETWORK_STATUS_STANDBY; |
|
104 NetworkStatsService.updateAllStats(function(success, msg) { |
|
105 do_check_eq(success, true); |
|
106 NetworkStatsService._networks[wifiId].status = NETWORK_STATUS_AWAY; |
|
107 NetworkStatsService.updateAllStats(function(success, msg) { |
|
108 do_check_eq(success, true); |
|
109 run_next_test(); |
|
110 }); |
|
111 }); |
|
112 }); |
|
113 }); |
|
114 |
|
115 add_test(function test_updateStats_ok() { |
|
116 getNetworkId(function onGetId(error, result) { |
|
117 do_check_eq(error, null); |
|
118 var netId = result; |
|
119 NetworkStatsService.updateStats(netId, function(success, msg){ |
|
120 do_check_eq(success, true); |
|
121 run_next_test(); |
|
122 }); |
|
123 }); |
|
124 }); |
|
125 |
|
126 add_test(function test_updateStats_failure() { |
|
127 NetworkStatsService.updateStats(-1, function(success, msg){ |
|
128 do_check_eq(success, false); |
|
129 run_next_test(); |
|
130 }); |
|
131 }); |
|
132 |
|
133 // Define Mockup function to simulate a request to netd |
|
134 function MockNetdRequest(aCallback) { |
|
135 var timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); |
|
136 var event = { |
|
137 notify: function (timer) { |
|
138 aCallback(); |
|
139 } |
|
140 }; |
|
141 |
|
142 timer.initWithCallback(event, 100, Ci.nsITimer.TYPE_ONE_SHOT); |
|
143 } |
|
144 |
|
145 add_test(function test_queue() { |
|
146 |
|
147 // Overwrite update function of NetworkStatsService to avoid netd errors due to use |
|
148 // fake interfaces. First, original function is stored to restore it at the end of the |
|
149 // test. |
|
150 var updateFunctionBackup = NetworkStatsService.update; |
|
151 |
|
152 NetworkStatsService.update = function update(aNetId, aCallback) { |
|
153 MockNetdRequest(function () { |
|
154 if (aCallback) { |
|
155 aCallback(true, "ok"); |
|
156 } |
|
157 }); |
|
158 }; |
|
159 |
|
160 // Fill networks with fake network interfaces to enable netd async requests. |
|
161 var network = {id: "1234", type: Ci.nsIDOMMozNetworkStatsManager.MOBILE}; |
|
162 var netId1 = NetworkStatsService.getNetworkId(network.id, network.type); |
|
163 NetworkStatsService._networks[netId1] = { network: network, |
|
164 interfaceName: "net1" }; |
|
165 |
|
166 network = {id: "5678", type: Ci.nsIDOMMozNetworkStatsManager.MOBILE}; |
|
167 var netId2 = NetworkStatsService.getNetworkId(network.id, network.type); |
|
168 NetworkStatsService._networks[netId2] = { network: network, |
|
169 interfaceName: "net2" }; |
|
170 |
|
171 NetworkStatsService.updateStats(netId1); |
|
172 NetworkStatsService.updateStats(netId2); |
|
173 do_check_eq(NetworkStatsService.updateQueue.length, 2); |
|
174 do_check_eq(NetworkStatsService.updateQueue[0].callbacks.length, 1); |
|
175 |
|
176 var i = 0; |
|
177 var updateCount = 0; |
|
178 var callback = function(success, msg) { |
|
179 i++; |
|
180 if (i >= updateCount) { |
|
181 NetworkStatsService.update = updateFunctionBackup; |
|
182 run_next_test(); |
|
183 } |
|
184 }; |
|
185 |
|
186 NetworkStatsService.updateStats(netId1, callback); |
|
187 updateCount++; |
|
188 NetworkStatsService.updateStats(netId2, callback); |
|
189 updateCount++; |
|
190 |
|
191 do_check_eq(NetworkStatsService.updateQueue.length, 2); |
|
192 do_check_eq(NetworkStatsService.updateQueue[0].callbacks.length, 2); |
|
193 do_check_eq(NetworkStatsService.updateQueue[0].callbacks[0], null); |
|
194 do_check_neq(NetworkStatsService.updateQueue[0].callbacks[1], null); |
|
195 }); |
|
196 |
|
197 add_test(function test_getAlarmQuota() { |
|
198 let alarm = { networkId: wifiId, absoluteThreshold: 10000 }; |
|
199 |
|
200 NetworkStatsService._getAlarmQuota(alarm, function onSet(error, quota){ |
|
201 do_check_eq(error, null); |
|
202 do_check_neq(quota, undefined); |
|
203 do_check_eq(alarm.absoluteThreshold, alarm.relativeThreshold); |
|
204 run_next_test(); |
|
205 }); |
|
206 }); |
|
207 |
|
208 var testPageURL = "http://test.com"; |
|
209 var testManifestURL = "http://test.com/manifest.webapp"; |
|
210 |
|
211 add_test(function test_setAlarm() { |
|
212 let alarm = { id: null, |
|
213 networkId: wifiId, |
|
214 threshold: 10000, |
|
215 absoluteThreshold: null, |
|
216 alarmStart: null, |
|
217 alarmEnd: null, |
|
218 data: null, |
|
219 pageURL: testPageURL, |
|
220 manifestURL: testManifestURL }; |
|
221 |
|
222 NetworkStatsService._setAlarm(alarm, function onSet(error, result) { |
|
223 do_check_eq(result, 1); |
|
224 run_next_test(); |
|
225 }); |
|
226 }); |
|
227 |
|
228 add_test(function test_setAlarm_invalid_threshold() { |
|
229 let alarm = { id: null, |
|
230 networkId: wifiId, |
|
231 threshold: -10000, |
|
232 absoluteThreshold: null, |
|
233 alarmStart: null, |
|
234 alarmEnd: null, |
|
235 data: null, |
|
236 pageURL: testPageURL, |
|
237 manifestURL: testManifestURL }; |
|
238 |
|
239 NetworkStatsService._networks[wifiId].status = NETWORK_STATUS_READY; |
|
240 |
|
241 NetworkStatsService._setAlarm(alarm, function onSet(error, result) { |
|
242 do_check_eq(error, "InvalidStateError"); |
|
243 run_next_test(); |
|
244 }); |
|
245 }); |
|
246 |
|
247 add_test(function test_fireAlarm() { |
|
248 // Add a fake alarm into database. |
|
249 let alarm = { id: null, |
|
250 networkId: wifiId, |
|
251 threshold: 10000, |
|
252 absoluteThreshold: null, |
|
253 alarmStart: null, |
|
254 alarmEnd: null, |
|
255 data: null, |
|
256 pageURL: testPageURL, |
|
257 manifestURL: testManifestURL }; |
|
258 |
|
259 // Set wifi status to standby to avoid connecting to netd when adding an alarm. |
|
260 NetworkStatsService._networks[wifiId].status = NETWORK_STATUS_STANDBY; |
|
261 |
|
262 NetworkStatsService._db.addAlarm(alarm, function addSuccessCb(error, newId) { |
|
263 NetworkStatsService._db.getAlarms(Ci.nsINetworkInterface.NETWORK_TYPE_WIFI, |
|
264 testManifestURL, function onGet(error, result) { |
|
265 do_check_eq(error, null); |
|
266 do_check_eq(result.length, 1); |
|
267 |
|
268 // Result of getAlarms is based on expected child's data format, so |
|
269 // some changes are needed to be able to use it. |
|
270 result[0].networkId = wifiId; |
|
271 result[0].pageURL = testPageURL; |
|
272 result[0].manifestURL = testManifestURL; |
|
273 |
|
274 NetworkStatsService._fireAlarm(result[0], false); |
|
275 NetworkStatsService._db.getAlarms(Ci.nsINetworkInterface.NETWORK_TYPE_WIFI, |
|
276 testManifestURL, function onGet(error, result) { |
|
277 do_check_eq(error, undefined); |
|
278 do_check_eq(result.length, 0); |
|
279 run_next_test(); |
|
280 }); |
|
281 }); |
|
282 }); |
|
283 }); |
|
284 |
|
285 function run_test() { |
|
286 do_get_profile(); |
|
287 |
|
288 Cu.import("resource://gre/modules/NetworkStatsService.jsm"); |
|
289 run_next_test(); |
|
290 } |