|
1 <!DOCTYPE HTML> |
|
2 <html> |
|
3 <head> |
|
4 <title>Test for NetworkStats</title> |
|
5 <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
|
6 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> |
|
7 </head> |
|
8 <body> |
|
9 <p id="display"></p> |
|
10 <div id="content" style="display: none"> |
|
11 </div> |
|
12 <pre id="test"> |
|
13 <script type="application/javascript"> |
|
14 |
|
15 function test() { |
|
16 netStats = window.navigator.mozNetworkStats; |
|
17 ok(netStats, "mozNetworkStats exists"); |
|
18 |
|
19 // Test IDL attributes |
|
20 ok('sampleRate' in netStats, |
|
21 "sampleRate should be a NetworkStats attribute"); |
|
22 ok(netStats.sampleRate > 0, |
|
23 "sampleRate is greater than 0."); |
|
24 |
|
25 ok('maxStorageAge' in netStats, |
|
26 "maxStorageAge should be a NetworkStats attribute"); |
|
27 ok(netStats.maxStorageAge > 0, |
|
28 "maxStorageAge is greater than 0."); |
|
29 |
|
30 // Test IDL methods |
|
31 next(); |
|
32 return; |
|
33 } |
|
34 |
|
35 function checkDataDates(data, start, end, sampleRate) { |
|
36 var offset = (new Date()).getTimezoneOffset() * 60 * 1000; |
|
37 start = Math.floor((start.getTime() - offset) / sampleRate) * sampleRate + offset; |
|
38 end = Math.floor((end.getTime() - offset) / sampleRate) * sampleRate + offset; |
|
39 |
|
40 var counter = 0; |
|
41 var date = start; |
|
42 var success = true; |
|
43 |
|
44 do { |
|
45 if(data[counter].date.getTime() != date) { |
|
46 success = false; |
|
47 break; |
|
48 } |
|
49 date += sampleRate; |
|
50 counter++; |
|
51 } while (date <= end); |
|
52 |
|
53 ok(success, "data result has correct dates"); |
|
54 } |
|
55 |
|
56 function compareNetworks(networkA, networkB) { |
|
57 return (networkA.id == networkB.id && |
|
58 networkA.type == networkB.type); |
|
59 } |
|
60 |
|
61 var req; |
|
62 var index = -1; |
|
63 var netStats = null; |
|
64 |
|
65 var steps = [ |
|
66 function () { |
|
67 // Test getAvailableNetworks |
|
68 req = netStats.getAvailableNetworks(); |
|
69 req.onsuccess = function () { |
|
70 ok(true, "getAvailableNetworks request ok"); |
|
71 ok(Array.isArray(req.result) && req.result.length > 0, |
|
72 "getAvailableNetworks returns an array not empty"); |
|
73 next(); |
|
74 }; |
|
75 req.onerror = function () { |
|
76 ok(false, "getAvailableNetworks failure!"); |
|
77 } |
|
78 }, |
|
79 function () { |
|
80 // Test clearAllStats |
|
81 req = netStats.clearAllStats(); |
|
82 req.onsuccess = function () { |
|
83 ok(true, "clearAllStats deleted the database"); |
|
84 next(); |
|
85 }; |
|
86 req.onerror = function () { |
|
87 ok(false, "clearAllStats deleted the database"); |
|
88 } |
|
89 }, |
|
90 function () { |
|
91 // Check if getSamples launch exception when start is greather than end |
|
92 |
|
93 // Prepare get params |
|
94 req = netStats.getAvailableNetworks(); |
|
95 req.onsuccess = function () { |
|
96 var network = req.result[0]; |
|
97 |
|
98 // Get dates |
|
99 var endDate = new Date(); |
|
100 var startDate = new Date(endDate.getTime() + 1000); |
|
101 |
|
102 try { |
|
103 netStats.getSamples(network, startDate, endDate); |
|
104 } catch(ex) { |
|
105 ok(true, "getSamples launch exception when start is greater than end"); |
|
106 next(); |
|
107 return; |
|
108 } |
|
109 |
|
110 ok(false, "getSamples launch exception when start is greater than end"); |
|
111 next(); |
|
112 return; |
|
113 }; |
|
114 req.onerror = function () { |
|
115 ok(false, "Error getting networks!"); |
|
116 } |
|
117 }, |
|
118 function () { |
|
119 // Test if call getSamples with network of type different than |
|
120 // nsIDOMMozNetworkStatsInterface launch an exception |
|
121 |
|
122 // Prepare get params |
|
123 var network = "wifi"; |
|
124 var endDate = new Date(); |
|
125 var startDate = new Date(endDate.getTime() - 1000); |
|
126 |
|
127 try { |
|
128 netStats.getSamples(network, new Date(), new Date()); |
|
129 } catch(ex) { |
|
130 ok(true, "getSamples launch exception if network is not " + |
|
131 "a nsIDOMMozNetworkStatsInterface"); |
|
132 next(); |
|
133 return; |
|
134 } |
|
135 |
|
136 ok(false, "getSamples launch exception if network is not " + |
|
137 "a nsIDOMMozNetworkStatsInterface"); |
|
138 }, |
|
139 function () { |
|
140 // Test if call getSamples with start parameter type different than Date launch an exception |
|
141 |
|
142 // Prepare get params |
|
143 req = netStats.getAvailableNetworks(); |
|
144 req.onsuccess = function () { |
|
145 var network = req.result[0]; |
|
146 |
|
147 var endDate = new Date(); |
|
148 var startDate = new Date(endDate.getTime() - 1000); |
|
149 startDate = startDate.toString(); |
|
150 |
|
151 try { |
|
152 netStats.getSamples(network, startDate, endDate); |
|
153 } catch(ex) { |
|
154 ok(true, "getSamples launch exception when start param is not a Date"); |
|
155 next(); |
|
156 return; |
|
157 } |
|
158 |
|
159 ok(false, "getSamples launch exception when start param is not a Date"); |
|
160 }; |
|
161 req.onerror = function () { |
|
162 ok(false, "Error getting networks!"); |
|
163 }; |
|
164 }, |
|
165 function () { |
|
166 // Test if call getSamples with end parameter type different than Date launch an exception |
|
167 |
|
168 // Prepare get params |
|
169 req = netStats.getAvailableNetworks(); |
|
170 req.onsuccess = function () { |
|
171 var network = req.result[0]; |
|
172 |
|
173 var endDate = new Date(); |
|
174 var startDate = new Date(endDate.getTime() - 1000); |
|
175 endDate = startDate.toString(); |
|
176 |
|
177 try { |
|
178 netStats.getSamples(network, startDate, endDate); |
|
179 } catch(ex) { |
|
180 ok(true, "getSamples launch exception when end param is not a Date"); |
|
181 next(); |
|
182 return; |
|
183 } |
|
184 |
|
185 ok(false, "getSamples launch exception when end param is not a Date"); |
|
186 }; |
|
187 req.onerror = function () { |
|
188 ok(false, "Error getting networks!"); |
|
189 }; |
|
190 }, |
|
191 function () { |
|
192 ok(true, "Get stats for a network and dates adapted to samplerate"); |
|
193 // Prepare get params |
|
194 req = netStats.getAvailableNetworks(); |
|
195 req.onsuccess = function () { |
|
196 var network = req.result[0]; |
|
197 var diff = 2; |
|
198 // Get samplerate in millis |
|
199 var sampleRate = netStats.sampleRate; |
|
200 // Get date with samplerate's precision |
|
201 var offset = new Date().getTimezoneOffset() * 60 * 1000; |
|
202 var endDate = new Date(Math.floor((new Date().getTime() - offset) / sampleRate) |
|
203 * sampleRate + offset); |
|
204 var startDate = new Date(endDate.getTime() - (sampleRate * diff)); |
|
205 // Calculate the number of samples that should be returned based on the |
|
206 // the samplerate and including final and initial samples. |
|
207 var samples = (endDate.getTime() - startDate.getTime()) / sampleRate + 1; |
|
208 |
|
209 // Launch request |
|
210 req = netStats.getSamples(network, startDate, endDate); |
|
211 req.onsuccess = function () { |
|
212 ok(true, "Get system stats request ok"); |
|
213 ok(req.result.manifestURL == null, "manifestURL should be null"); |
|
214 ok(compareNetworks(req.result.network, network), "networks should be equals"); |
|
215 ok(req.result.start.getTime() == startDate.getTime(), "starts should be equals"); |
|
216 ok(req.result.end.getTime() == endDate.getTime(), "ends should be equals"); |
|
217 var data = req.result.data; |
|
218 ok(Array.isArray(data) && data.length == samples, |
|
219 "data is an array of length " + samples); |
|
220 checkDataDates(data, startDate, endDate, sampleRate); |
|
221 next(); |
|
222 }; |
|
223 req.onerror = function () { |
|
224 ok(false, "Get stats failure!"); |
|
225 } |
|
226 }; |
|
227 req.onerror = function () { |
|
228 ok(false, "Error getting networks!"); |
|
229 }; |
|
230 }, |
|
231 function () { |
|
232 ok(true, "Get system stats for a network and dates not adapted to samplerate"); |
|
233 // Prepare get params |
|
234 req = netStats.getAvailableNetworks(); |
|
235 req.onsuccess = function () { |
|
236 var network = req.result[0]; |
|
237 var diff = 2; |
|
238 // Get samplerate in millis |
|
239 var sampleRate = netStats.sampleRate; |
|
240 var endDate = new Date(); |
|
241 var startDate = new Date(endDate.getTime() - (sampleRate * diff)); |
|
242 // Calculate the number of samples that should be returned based on the |
|
243 // the samplerate, including final and initial samples and taking into |
|
244 // account that these will be filtered according to precision. |
|
245 var samples = (Math.floor(endDate.getTime() / (sampleRate)) * sampleRate - |
|
246 Math.floor(startDate.getTime() / (sampleRate)) * sampleRate) / sampleRate + 1; |
|
247 |
|
248 // Launch request |
|
249 req = netStats.getSamples(network, startDate, endDate); |
|
250 req.onsuccess = function () { |
|
251 ok(true, "Get stats request ok"); |
|
252 ok(req.result.manifestURL == null, "manifestURL should be null"); |
|
253 ok(compareNetworks(req.result.network, network), "networks should be equals"); |
|
254 ok(req.result.start.getTime() == startDate.getTime(), "starts should be equals"); |
|
255 ok(req.result.end.getTime() == endDate.getTime(), "ends should be equals"); |
|
256 var data = req.result.data; |
|
257 ok(Array.isArray(data) && data.length == samples, |
|
258 "data is an array of length " + samples); |
|
259 checkDataDates(data, startDate, endDate, sampleRate); |
|
260 next(); |
|
261 }; |
|
262 req.onerror = function () { |
|
263 ok(false, "Get stats failure!"); |
|
264 } |
|
265 }; |
|
266 req.onerror = function () { |
|
267 ok(false, "Error getting networks!"); |
|
268 }; |
|
269 }, |
|
270 function () { |
|
271 // Test clearStats |
|
272 req = netStats.getAvailableNetworks(); |
|
273 req.onsuccess = function () { |
|
274 var network = req.result[0]; |
|
275 |
|
276 req = netStats.clearStats(network); |
|
277 req.onsuccess = function () { |
|
278 ok(true, "clearStats deleted the database"); |
|
279 next(); |
|
280 }; |
|
281 req.onerror = function () { |
|
282 ok(false, "clearStats deleted the database"); |
|
283 } |
|
284 }; |
|
285 req.onerror = function () { |
|
286 ok(false, "Error getting networks!"); |
|
287 }; |
|
288 }, |
|
289 function () { |
|
290 // Test getAvailableServiceTypes |
|
291 req = netStats.getAvailableServiceTypes(); |
|
292 req.onsuccess = function () { |
|
293 ok(true, "getAvailableServiceTypes request ok"); |
|
294 ok(Array.isArray(req.result) && req.result.length == 0, |
|
295 "getAvailableServiceTypes returns an empty array"); |
|
296 next(); |
|
297 }; |
|
298 req.onerror = function () { |
|
299 ok(false, "getAvailableServiceTypes failure!"); |
|
300 } |
|
301 }, |
|
302 function () { |
|
303 ok(true, "all done!\n"); |
|
304 SpecialPowers.removePermission("networkstats-manage", document); |
|
305 SimpleTest.finish(); |
|
306 return; |
|
307 } |
|
308 ]; |
|
309 |
|
310 function next() { |
|
311 index += 1; |
|
312 if (index >= steps.length) { |
|
313 ok(false, "Shouldn't get here!"); |
|
314 return; |
|
315 } |
|
316 try { |
|
317 steps[index](); |
|
318 } catch(ex) { |
|
319 ok(false, "Caught exception", ex); |
|
320 } |
|
321 } |
|
322 |
|
323 SimpleTest.waitForExplicitFinish(); |
|
324 |
|
325 SpecialPowers.addPermission("networkstats-manage", true, document); |
|
326 SpecialPowers.pushPrefEnv({'set': [["dom.mozNetworkStats.enabled", true]]}, test); |
|
327 |
|
328 </script> |
|
329 </pre> |
|
330 </body> |
|
331 </html> |