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