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 | /* Any: copyright is dedicated to the Public Domain. |
michael@0 | 2 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; |
michael@0 | 5 | |
michael@0 | 6 | Cu.import("resource://gre/modules/NetworkStatsDB.jsm"); |
michael@0 | 7 | |
michael@0 | 8 | const netStatsDb = new NetworkStatsDB(); |
michael@0 | 9 | |
michael@0 | 10 | function clearStore(store, callback) { |
michael@0 | 11 | netStatsDb.dbNewTxn(store, "readwrite", function(aTxn, aStore) { |
michael@0 | 12 | aStore.openCursor().onsuccess = function (event) { |
michael@0 | 13 | let cursor = event.target.result; |
michael@0 | 14 | if (cursor){ |
michael@0 | 15 | cursor.delete(); |
michael@0 | 16 | cursor.continue(); |
michael@0 | 17 | } |
michael@0 | 18 | }; |
michael@0 | 19 | }, callback); |
michael@0 | 20 | } |
michael@0 | 21 | |
michael@0 | 22 | function getNetworkId(aIccId, aNetworkType) { |
michael@0 | 23 | return aIccId + '' + aNetworkType; |
michael@0 | 24 | } |
michael@0 | 25 | |
michael@0 | 26 | add_test(function prepareDatabase() { |
michael@0 | 27 | // Clear whole database to avoid starting tests with unknown state |
michael@0 | 28 | // due to the previous tests. |
michael@0 | 29 | clearStore('net_stats_store', function() { |
michael@0 | 30 | clearStore('net_alarm', function() { |
michael@0 | 31 | run_next_test(); |
michael@0 | 32 | }); |
michael@0 | 33 | }); |
michael@0 | 34 | }); |
michael@0 | 35 | |
michael@0 | 36 | function filterTimestamp(date) { |
michael@0 | 37 | var sampleRate = netStatsDb.sampleRate; |
michael@0 | 38 | var offset = date.getTimezoneOffset() * 60 * 1000; |
michael@0 | 39 | return Math.floor((date.getTime() - offset) / sampleRate) * sampleRate; |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | function getNetworks() { |
michael@0 | 43 | return [{ id: '0', type: Ci.nsIDOMMozNetworkStatsManager.WIFI }, |
michael@0 | 44 | { id: '1234', type: Ci.nsIDOMMozNetworkStatsManager.MOBILE }]; |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | function compareNetworks(networkA, networkB) { |
michael@0 | 48 | return (networkA[0] == networkB[0] && networkA[1] == networkB[1]); |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | add_test(function test_sampleRate() { |
michael@0 | 52 | var sampleRate = netStatsDb.sampleRate; |
michael@0 | 53 | do_check_true(sampleRate > 0); |
michael@0 | 54 | netStatsDb.sampleRate = 0; |
michael@0 | 55 | sampleRate = netStatsDb.sampleRate; |
michael@0 | 56 | do_check_true(sampleRate > 0); |
michael@0 | 57 | |
michael@0 | 58 | run_next_test(); |
michael@0 | 59 | }); |
michael@0 | 60 | |
michael@0 | 61 | add_test(function test_maxStorageSamples() { |
michael@0 | 62 | var maxStorageSamples = netStatsDb.maxStorageSamples; |
michael@0 | 63 | do_check_true(maxStorageSamples > 0); |
michael@0 | 64 | netStatsDb.maxStorageSamples = 0; |
michael@0 | 65 | maxStorageSamples = netStatsDb.maxStorageSamples; |
michael@0 | 66 | do_check_true(maxStorageSamples > 0); |
michael@0 | 67 | |
michael@0 | 68 | run_next_test(); |
michael@0 | 69 | }); |
michael@0 | 70 | |
michael@0 | 71 | add_test(function test_fillResultSamples_emptyData() { |
michael@0 | 72 | var samples = 3; |
michael@0 | 73 | var data = []; |
michael@0 | 74 | var start = filterTimestamp(new Date()); |
michael@0 | 75 | var sampleRate = netStatsDb.sampleRate; |
michael@0 | 76 | var end = start + (sampleRate * samples); |
michael@0 | 77 | netStatsDb.fillResultSamples(start, end, data); |
michael@0 | 78 | do_check_eq(data.length, samples + 1); |
michael@0 | 79 | |
michael@0 | 80 | var aux = start; |
michael@0 | 81 | var success = true; |
michael@0 | 82 | for (var i = 0; i <= samples; i++) { |
michael@0 | 83 | if (data[i].date.getTime() != aux || data[i].rxBytes != undefined || data[i].txBytes != undefined) { |
michael@0 | 84 | success = false; |
michael@0 | 85 | break; |
michael@0 | 86 | } |
michael@0 | 87 | aux += sampleRate; |
michael@0 | 88 | } |
michael@0 | 89 | do_check_true(success); |
michael@0 | 90 | |
michael@0 | 91 | run_next_test(); |
michael@0 | 92 | }); |
michael@0 | 93 | |
michael@0 | 94 | add_test(function test_fillResultSamples_noEmptyData() { |
michael@0 | 95 | var samples = 3; |
michael@0 | 96 | var sampleRate = netStatsDb.sampleRate; |
michael@0 | 97 | var start = filterTimestamp(new Date()); |
michael@0 | 98 | var end = start + (sampleRate * samples); |
michael@0 | 99 | var data = [{date: new Date(start + sampleRate), |
michael@0 | 100 | rxBytes: 0, |
michael@0 | 101 | txBytes: 0}]; |
michael@0 | 102 | netStatsDb.fillResultSamples(start, end, data); |
michael@0 | 103 | do_check_eq(data.length, samples + 1); |
michael@0 | 104 | |
michael@0 | 105 | var aux = start; |
michael@0 | 106 | var success = true; |
michael@0 | 107 | for (var i = 0; i <= samples; i++) { |
michael@0 | 108 | if (i == 1) { |
michael@0 | 109 | if (data[i].date.getTime() != aux || data[i].rxBytes != 0 || data[i].txBytes != 0) { |
michael@0 | 110 | success = false; |
michael@0 | 111 | break; |
michael@0 | 112 | } |
michael@0 | 113 | } else { |
michael@0 | 114 | if (data[i].date.getTime() != aux || data[i].rxBytes != undefined || data[i].txBytes != undefined) { |
michael@0 | 115 | success = false; |
michael@0 | 116 | break; |
michael@0 | 117 | } |
michael@0 | 118 | } |
michael@0 | 119 | aux += sampleRate; |
michael@0 | 120 | } |
michael@0 | 121 | do_check_true(success); |
michael@0 | 122 | |
michael@0 | 123 | run_next_test(); |
michael@0 | 124 | }); |
michael@0 | 125 | |
michael@0 | 126 | add_test(function test_clear() { |
michael@0 | 127 | var networks = getNetworks(); |
michael@0 | 128 | networks.forEach(function(network, index) { |
michael@0 | 129 | networks[index] = {network: network, networkId: getNetworkId(network.id, network.type)}; |
michael@0 | 130 | }, this); |
michael@0 | 131 | |
michael@0 | 132 | netStatsDb.clearStats(networks, function (error, result) { |
michael@0 | 133 | do_check_eq(error, null); |
michael@0 | 134 | run_next_test(); |
michael@0 | 135 | }); |
michael@0 | 136 | }); |
michael@0 | 137 | |
michael@0 | 138 | add_test(function test_clear_interface() { |
michael@0 | 139 | var networks = getNetworks(); |
michael@0 | 140 | networks.forEach(function(network, index) { |
michael@0 | 141 | networks[index] = {network: network, networkId: getNetworkId(network.id, network.type)}; |
michael@0 | 142 | }, this); |
michael@0 | 143 | |
michael@0 | 144 | netStatsDb.clearInterfaceStats(networks[0], function (error, result) { |
michael@0 | 145 | do_check_eq(error, null); |
michael@0 | 146 | run_next_test(); |
michael@0 | 147 | }); |
michael@0 | 148 | }); |
michael@0 | 149 | |
michael@0 | 150 | add_test(function test_internalSaveStats_singleSample() { |
michael@0 | 151 | var networks = getNetworks(); |
michael@0 | 152 | |
michael@0 | 153 | var stats = { appId: 0, |
michael@0 | 154 | serviceType: "", |
michael@0 | 155 | network: [networks[0].id, networks[0].type], |
michael@0 | 156 | timestamp: Date.now(), |
michael@0 | 157 | rxBytes: 0, |
michael@0 | 158 | txBytes: 0, |
michael@0 | 159 | rxSystemBytes: 1234, |
michael@0 | 160 | txSystemBytes: 1234, |
michael@0 | 161 | rxTotalBytes: 1234, |
michael@0 | 162 | txTotalBytes: 1234 }; |
michael@0 | 163 | |
michael@0 | 164 | netStatsDb.dbNewTxn("net_stats_store", "readwrite", function(txn, store) { |
michael@0 | 165 | netStatsDb._saveStats(txn, store, stats); |
michael@0 | 166 | }, function(error, result) { |
michael@0 | 167 | do_check_eq(error, null); |
michael@0 | 168 | |
michael@0 | 169 | netStatsDb.logAllRecords(function(error, result) { |
michael@0 | 170 | do_check_eq(error, null); |
michael@0 | 171 | do_check_eq(result.length, 1); |
michael@0 | 172 | do_check_eq(result[0].appId, stats.appId); |
michael@0 | 173 | do_check_eq(result[0].serviceType, stats.serviceType); |
michael@0 | 174 | do_check_true(compareNetworks(result[0].network, stats.network)); |
michael@0 | 175 | do_check_eq(result[0].timestamp, stats.timestamp); |
michael@0 | 176 | do_check_eq(result[0].rxBytes, stats.rxBytes); |
michael@0 | 177 | do_check_eq(result[0].txBytes, stats.txBytes); |
michael@0 | 178 | do_check_eq(result[0].rxSystemBytes, stats.rxSystemBytes); |
michael@0 | 179 | do_check_eq(result[0].txSystemBytes, stats.txSystemBytes); |
michael@0 | 180 | do_check_eq(result[0].rxTotalBytes, stats.rxTotalBytes); |
michael@0 | 181 | do_check_eq(result[0].txTotalBytes, stats.txTotalBytes); |
michael@0 | 182 | run_next_test(); |
michael@0 | 183 | }); |
michael@0 | 184 | }); |
michael@0 | 185 | }); |
michael@0 | 186 | |
michael@0 | 187 | add_test(function test_internalSaveStats_arraySamples() { |
michael@0 | 188 | clearStore('net_stats_store', function() { |
michael@0 | 189 | var networks = getNetworks(); |
michael@0 | 190 | var network = [networks[0].id, networks[0].type]; |
michael@0 | 191 | |
michael@0 | 192 | var samples = 2; |
michael@0 | 193 | var stats = []; |
michael@0 | 194 | for (var i = 0; i < samples; i++) { |
michael@0 | 195 | stats.push({ appId: 0, |
michael@0 | 196 | serviceType: "", |
michael@0 | 197 | network: network, |
michael@0 | 198 | timestamp: Date.now() + (10 * i), |
michael@0 | 199 | rxBytes: 0, |
michael@0 | 200 | txBytes: 0, |
michael@0 | 201 | rxSystemBytes: 1234, |
michael@0 | 202 | txSystemBytes: 1234, |
michael@0 | 203 | rxTotalBytes: 1234, |
michael@0 | 204 | txTotalBytes: 1234 }); |
michael@0 | 205 | } |
michael@0 | 206 | |
michael@0 | 207 | netStatsDb.dbNewTxn("net_stats_store", "readwrite", function(txn, store) { |
michael@0 | 208 | netStatsDb._saveStats(txn, store, stats); |
michael@0 | 209 | }, function(error, result) { |
michael@0 | 210 | do_check_eq(error, null); |
michael@0 | 211 | |
michael@0 | 212 | netStatsDb.logAllRecords(function(error, result) { |
michael@0 | 213 | do_check_eq(error, null); |
michael@0 | 214 | |
michael@0 | 215 | do_check_eq(result.length, samples); |
michael@0 | 216 | var success = true; |
michael@0 | 217 | for (var i = 0; i < samples; i++) { |
michael@0 | 218 | if (result[i].appId != stats[i].appId || |
michael@0 | 219 | result[i].serviceType != stats[i].serviceType || |
michael@0 | 220 | !compareNetworks(result[i].network, stats[i].network) || |
michael@0 | 221 | result[i].timestamp != stats[i].timestamp || |
michael@0 | 222 | result[i].rxBytes != stats[i].rxBytes || |
michael@0 | 223 | result[i].txBytes != stats[i].txBytes || |
michael@0 | 224 | result[i].rxSystemBytes != stats[i].rxSystemBytes || |
michael@0 | 225 | result[i].txSystemBytes != stats[i].txSystemBytes || |
michael@0 | 226 | result[i].rxTotalBytes != stats[i].rxTotalBytes || |
michael@0 | 227 | result[i].txTotalBytes != stats[i].txTotalBytes) { |
michael@0 | 228 | success = false; |
michael@0 | 229 | break; |
michael@0 | 230 | } |
michael@0 | 231 | } |
michael@0 | 232 | do_check_true(success); |
michael@0 | 233 | run_next_test(); |
michael@0 | 234 | }); |
michael@0 | 235 | }); |
michael@0 | 236 | }); |
michael@0 | 237 | }); |
michael@0 | 238 | |
michael@0 | 239 | add_test(function test_internalRemoveOldStats() { |
michael@0 | 240 | clearStore('net_stats_store', function() { |
michael@0 | 241 | var networks = getNetworks(); |
michael@0 | 242 | var network = [networks[0].id, networks[0].type]; |
michael@0 | 243 | var samples = 10; |
michael@0 | 244 | var stats = []; |
michael@0 | 245 | for (var i = 0; i < samples - 1; i++) { |
michael@0 | 246 | stats.push({ appId: 0, serviceType: "", |
michael@0 | 247 | network: network, timestamp: Date.now() + (10 * i), |
michael@0 | 248 | rxBytes: 0, txBytes: 0, |
michael@0 | 249 | rxSystemBytes: 1234, txSystemBytes: 1234, |
michael@0 | 250 | rxTotalBytes: 1234, txTotalBytes: 1234 }); |
michael@0 | 251 | } |
michael@0 | 252 | |
michael@0 | 253 | stats.push({ appId: 0, serviceType: "", |
michael@0 | 254 | network: network, timestamp: Date.now() + (10 * samples), |
michael@0 | 255 | rxBytes: 0, txBytes: 0, |
michael@0 | 256 | rxSystemBytes: 1234, txSystemBytes: 1234, |
michael@0 | 257 | rxTotalBytes: 1234, txTotalBytes: 1234 }); |
michael@0 | 258 | |
michael@0 | 259 | netStatsDb.dbNewTxn("net_stats_store", "readwrite", function(txn, store) { |
michael@0 | 260 | netStatsDb._saveStats(txn, store, stats); |
michael@0 | 261 | var date = stats[stats.length - 1].timestamp |
michael@0 | 262 | + (netStatsDb.sampleRate * netStatsDb.maxStorageSamples - 1) - 1; |
michael@0 | 263 | netStatsDb._removeOldStats(txn, store, 0, "", network, date); |
michael@0 | 264 | }, function(error, result) { |
michael@0 | 265 | do_check_eq(error, null); |
michael@0 | 266 | |
michael@0 | 267 | netStatsDb.logAllRecords(function(error, result) { |
michael@0 | 268 | do_check_eq(error, null); |
michael@0 | 269 | do_check_eq(result.length, 1); |
michael@0 | 270 | |
michael@0 | 271 | run_next_test(); |
michael@0 | 272 | }); |
michael@0 | 273 | }); |
michael@0 | 274 | }); |
michael@0 | 275 | }); |
michael@0 | 276 | |
michael@0 | 277 | function processSamplesDiff(networks, lastStat, newStat, callback) { |
michael@0 | 278 | clearStore('net_stats_store', function() { |
michael@0 | 279 | netStatsDb.dbNewTxn("net_stats_store", "readwrite", function(txn, store) { |
michael@0 | 280 | netStatsDb._saveStats(txn, store, lastStat); |
michael@0 | 281 | }, function(error, result) { |
michael@0 | 282 | netStatsDb.dbNewTxn("net_stats_store", "readwrite", function(txn, store) { |
michael@0 | 283 | let request = store.index("network").openCursor(newStat.network, "prev"); |
michael@0 | 284 | request.onsuccess = function onsuccess(event) { |
michael@0 | 285 | let cursor = event.target.result; |
michael@0 | 286 | do_check_neq(cursor, null); |
michael@0 | 287 | netStatsDb._processSamplesDiff(txn, store, cursor, newStat, true); |
michael@0 | 288 | }; |
michael@0 | 289 | }, function(error, result) { |
michael@0 | 290 | do_check_eq(error, null); |
michael@0 | 291 | netStatsDb.logAllRecords(function(error, result) { |
michael@0 | 292 | do_check_eq(error, null); |
michael@0 | 293 | callback(result); |
michael@0 | 294 | }); |
michael@0 | 295 | }); |
michael@0 | 296 | }); |
michael@0 | 297 | }); |
michael@0 | 298 | } |
michael@0 | 299 | |
michael@0 | 300 | add_test(function test_processSamplesDiffSameSample() { |
michael@0 | 301 | var networks = getNetworks(); |
michael@0 | 302 | var network = [networks[0].id, networks[0].type]; |
michael@0 | 303 | |
michael@0 | 304 | var sampleRate = netStatsDb.sampleRate; |
michael@0 | 305 | var date = filterTimestamp(new Date()); |
michael@0 | 306 | |
michael@0 | 307 | var lastStat = { appId: 0, serviceType: "", |
michael@0 | 308 | network: network, timestamp: date, |
michael@0 | 309 | rxBytes: 0, txBytes: 0, |
michael@0 | 310 | rxSystemBytes: 1234, txSystemBytes: 1234, |
michael@0 | 311 | rxTotalBytes: 2234, txTotalBytes: 2234 }; |
michael@0 | 312 | |
michael@0 | 313 | var newStat = { appId: 0, serviceType: "", |
michael@0 | 314 | network: network, timestamp: date, |
michael@0 | 315 | rxBytes: 0, txBytes: 0, |
michael@0 | 316 | rxSystemBytes: 2234, txSystemBytes: 2234, |
michael@0 | 317 | rxTotalBytes: 2234, txTotalBytes: 2234 }; |
michael@0 | 318 | |
michael@0 | 319 | processSamplesDiff(networks, lastStat, newStat, function(result) { |
michael@0 | 320 | do_check_eq(result.length, 1); |
michael@0 | 321 | do_check_eq(result[0].appId, newStat.appId); |
michael@0 | 322 | do_check_eq(result[0].serviceType, newStat.serviceType); |
michael@0 | 323 | do_check_true(compareNetworks(result[0].network, newStat.network)); |
michael@0 | 324 | do_check_eq(result[0].timestamp, newStat.timestamp); |
michael@0 | 325 | do_check_eq(result[0].rxBytes, newStat.rxSystemBytes - lastStat.rxSystemBytes); |
michael@0 | 326 | do_check_eq(result[0].txBytes, newStat.txSystemBytes - lastStat.txSystemBytes); |
michael@0 | 327 | do_check_eq(result[0].rxTotalBytes, lastStat.rxTotalBytes + newStat.rxSystemBytes - lastStat.rxSystemBytes); |
michael@0 | 328 | do_check_eq(result[0].txTotalBytes, lastStat.txTotalBytes + newStat.txSystemBytes - lastStat.txSystemBytes); |
michael@0 | 329 | do_check_eq(result[0].rxSystemBytes, newStat.rxSystemBytes); |
michael@0 | 330 | do_check_eq(result[0].txSystemBytes, newStat.txSystemBytes); |
michael@0 | 331 | run_next_test(); |
michael@0 | 332 | }); |
michael@0 | 333 | }); |
michael@0 | 334 | |
michael@0 | 335 | add_test(function test_processSamplesDiffNextSample() { |
michael@0 | 336 | var networks = getNetworks(); |
michael@0 | 337 | var network = [networks[0].id, networks[0].type]; |
michael@0 | 338 | |
michael@0 | 339 | var sampleRate = netStatsDb.sampleRate; |
michael@0 | 340 | var date = filterTimestamp(new Date()); |
michael@0 | 341 | |
michael@0 | 342 | var lastStat = { appId: 0, serviceType: "", |
michael@0 | 343 | network: network, timestamp: date, |
michael@0 | 344 | rxBytes: 0, txBytes: 0, |
michael@0 | 345 | rxSystemBytes: 1234, txSystemBytes: 1234, |
michael@0 | 346 | rxTotalBytes: 2234, txTotalBytes: 2234 }; |
michael@0 | 347 | |
michael@0 | 348 | var newStat = { appId: 0, serviceType: "", |
michael@0 | 349 | network: network, timestamp: date + sampleRate, |
michael@0 | 350 | rxBytes: 0, txBytes: 0, |
michael@0 | 351 | rxSystemBytes: 1734, txSystemBytes: 1734, |
michael@0 | 352 | rxTotalBytes: 0, txTotalBytes: 0 }; |
michael@0 | 353 | |
michael@0 | 354 | processSamplesDiff(networks, lastStat, newStat, function(result) { |
michael@0 | 355 | do_check_eq(result.length, 2); |
michael@0 | 356 | do_check_eq(result[1].appId, newStat.appId); |
michael@0 | 357 | do_check_eq(result[1].serviceType, newStat.serviceType); |
michael@0 | 358 | do_check_true(compareNetworks(result[1].network, newStat.network)); |
michael@0 | 359 | do_check_eq(result[1].timestamp, newStat.timestamp); |
michael@0 | 360 | do_check_eq(result[1].rxBytes, newStat.rxSystemBytes - lastStat.rxSystemBytes); |
michael@0 | 361 | do_check_eq(result[1].txBytes, newStat.txSystemBytes - lastStat.txSystemBytes); |
michael@0 | 362 | do_check_eq(result[1].rxSystemBytes, newStat.rxSystemBytes); |
michael@0 | 363 | do_check_eq(result[1].txSystemBytes, newStat.txSystemBytes); |
michael@0 | 364 | do_check_eq(result[1].rxTotalBytes, lastStat.rxTotalBytes + newStat.rxSystemBytes - lastStat.rxSystemBytes); |
michael@0 | 365 | do_check_eq(result[1].txTotalBytes, lastStat.txTotalBytes + newStat.txSystemBytes - lastStat.txSystemBytes); |
michael@0 | 366 | run_next_test(); |
michael@0 | 367 | }); |
michael@0 | 368 | }); |
michael@0 | 369 | |
michael@0 | 370 | add_test(function test_processSamplesDiffSamplesLost() { |
michael@0 | 371 | var networks = getNetworks(); |
michael@0 | 372 | var network = [networks[0].id, networks[0].type]; |
michael@0 | 373 | var samples = 5; |
michael@0 | 374 | var sampleRate = netStatsDb.sampleRate; |
michael@0 | 375 | var date = filterTimestamp(new Date()); |
michael@0 | 376 | var lastStat = { appId: 0, serviceType: "", |
michael@0 | 377 | network: network, timestamp: date, |
michael@0 | 378 | rxBytes: 0, txBytes: 0, |
michael@0 | 379 | rxSystemBytes: 1234, txSystemBytes: 1234, |
michael@0 | 380 | rxTotalBytes: 2234, txTotalBytes: 2234}; |
michael@0 | 381 | |
michael@0 | 382 | var newStat = { appId: 0, serviceType: "", |
michael@0 | 383 | network: network, timestamp: date + (sampleRate * samples), |
michael@0 | 384 | rxBytes: 0, txBytes: 0, |
michael@0 | 385 | rxSystemBytes: 2234, txSystemBytes: 2234, |
michael@0 | 386 | rxTotalBytes: 0, txTotalBytes: 0 }; |
michael@0 | 387 | |
michael@0 | 388 | processSamplesDiff(networks, lastStat, newStat, function(result) { |
michael@0 | 389 | do_check_eq(result.length, samples + 1); |
michael@0 | 390 | do_check_eq(result[0].appId, newStat.appId); |
michael@0 | 391 | do_check_eq(result[0].serviceType, newStat.serviceType); |
michael@0 | 392 | do_check_true(compareNetworks(result[samples].network, newStat.network)); |
michael@0 | 393 | do_check_eq(result[samples].timestamp, newStat.timestamp); |
michael@0 | 394 | do_check_eq(result[samples].rxBytes, newStat.rxTotalBytes - lastStat.rxTotalBytes); |
michael@0 | 395 | do_check_eq(result[samples].txBytes, newStat.txTotalBytes - lastStat.txTotalBytes); |
michael@0 | 396 | do_check_eq(result[samples].rxSystemBytes, newStat.rxSystemBytes); |
michael@0 | 397 | do_check_eq(result[samples].txSystemBytes, newStat.txSystemBytes); |
michael@0 | 398 | do_check_eq(result[samples].rxTotalBytes, lastStat.rxTotalBytes + newStat.rxSystemBytes - lastStat.rxSystemBytes); |
michael@0 | 399 | do_check_eq(result[samples].txTotalBytes, lastStat.txTotalBytes + newStat.txSystemBytes - lastStat.txSystemBytes); |
michael@0 | 400 | run_next_test(); |
michael@0 | 401 | }); |
michael@0 | 402 | }); |
michael@0 | 403 | |
michael@0 | 404 | add_test(function test_saveStats() { |
michael@0 | 405 | var networks = getNetworks(); |
michael@0 | 406 | var network = [networks[0].id, networks[0].type]; |
michael@0 | 407 | |
michael@0 | 408 | var stats = { appId: 0, |
michael@0 | 409 | serviceType: "", |
michael@0 | 410 | networkId: networks[0].id, |
michael@0 | 411 | networkType: networks[0].type, |
michael@0 | 412 | date: new Date(), |
michael@0 | 413 | rxBytes: 2234, |
michael@0 | 414 | txBytes: 2234, |
michael@0 | 415 | isAccumulative: true }; |
michael@0 | 416 | |
michael@0 | 417 | clearStore('net_stats_store', function() { |
michael@0 | 418 | netStatsDb.saveStats(stats, function(error, result) { |
michael@0 | 419 | do_check_eq(error, null); |
michael@0 | 420 | netStatsDb.logAllRecords(function(error, result) { |
michael@0 | 421 | do_check_eq(error, null); |
michael@0 | 422 | do_check_eq(result.length, 1); |
michael@0 | 423 | do_check_eq(result[0].appId, stats.appId); |
michael@0 | 424 | do_check_eq(result[0].serviceType, stats.serviceType); |
michael@0 | 425 | do_check_true(compareNetworks(result[0].network, network)); |
michael@0 | 426 | let timestamp = filterTimestamp(stats.date); |
michael@0 | 427 | do_check_eq(result[0].timestamp, timestamp); |
michael@0 | 428 | do_check_eq(result[0].rxBytes, stats.rxBytes); |
michael@0 | 429 | do_check_eq(result[0].txBytes, stats.txBytes); |
michael@0 | 430 | do_check_eq(result[0].rxSystemBytes, stats.rxBytes); |
michael@0 | 431 | do_check_eq(result[0].txSystemBytes, stats.txBytes); |
michael@0 | 432 | do_check_eq(result[0].rxTotalBytes, stats.rxBytes); |
michael@0 | 433 | do_check_eq(result[0].txTotalBytes, stats.txBytes); |
michael@0 | 434 | run_next_test(); |
michael@0 | 435 | }); |
michael@0 | 436 | }); |
michael@0 | 437 | }); |
michael@0 | 438 | }); |
michael@0 | 439 | |
michael@0 | 440 | add_test(function test_saveAppStats() { |
michael@0 | 441 | var networks = getNetworks(); |
michael@0 | 442 | var network = [networks[0].id, networks[0].type]; |
michael@0 | 443 | |
michael@0 | 444 | var stats = { appId: 1, |
michael@0 | 445 | serviceType: "", |
michael@0 | 446 | networkId: networks[0].id, |
michael@0 | 447 | networkType: networks[0].type, |
michael@0 | 448 | date: new Date(), |
michael@0 | 449 | rxBytes: 2234, |
michael@0 | 450 | txBytes: 2234, |
michael@0 | 451 | isAccumulative: false }; |
michael@0 | 452 | |
michael@0 | 453 | clearStore('net_stats_store', function() { |
michael@0 | 454 | netStatsDb.saveStats(stats, function(error, result) { |
michael@0 | 455 | do_check_eq(error, null); |
michael@0 | 456 | netStatsDb.logAllRecords(function(error, result) { |
michael@0 | 457 | do_check_eq(error, null); |
michael@0 | 458 | do_check_eq(result.length, 1); |
michael@0 | 459 | do_check_eq(result[0].appId, stats.appId); |
michael@0 | 460 | do_check_eq(result[0].serviceType, stats.serviceType); |
michael@0 | 461 | do_check_true(compareNetworks(result[0].network, network)); |
michael@0 | 462 | let timestamp = filterTimestamp(stats.date); |
michael@0 | 463 | do_check_eq(result[0].timestamp, timestamp); |
michael@0 | 464 | do_check_eq(result[0].rxBytes, stats.rxBytes); |
michael@0 | 465 | do_check_eq(result[0].txBytes, stats.txBytes); |
michael@0 | 466 | do_check_eq(result[0].rxSystemBytes, 0); |
michael@0 | 467 | do_check_eq(result[0].txSystemBytes, 0); |
michael@0 | 468 | do_check_eq(result[0].rxTotalBytes, 0); |
michael@0 | 469 | do_check_eq(result[0].txTotalBytes, 0); |
michael@0 | 470 | run_next_test(); |
michael@0 | 471 | }); |
michael@0 | 472 | }); |
michael@0 | 473 | }); |
michael@0 | 474 | }); |
michael@0 | 475 | |
michael@0 | 476 | add_test(function test_saveServiceStats() { |
michael@0 | 477 | var networks = getNetworks(); |
michael@0 | 478 | var network = [networks[0].id, networks[0].type]; |
michael@0 | 479 | |
michael@0 | 480 | var stats = { appId: 0, |
michael@0 | 481 | serviceType: "FakeType", |
michael@0 | 482 | networkId: networks[0].id, |
michael@0 | 483 | networkType: networks[0].type, |
michael@0 | 484 | date: new Date(), |
michael@0 | 485 | rxBytes: 2234, |
michael@0 | 486 | txBytes: 2234, |
michael@0 | 487 | isAccumulative: false }; |
michael@0 | 488 | |
michael@0 | 489 | clearStore('net_stats_store', function() { |
michael@0 | 490 | netStatsDb.saveStats(stats, function(error, result) { |
michael@0 | 491 | do_check_eq(error, null); |
michael@0 | 492 | netStatsDb.logAllRecords(function(error, result) { |
michael@0 | 493 | do_check_eq(error, null); |
michael@0 | 494 | do_check_eq(result.length, 1); |
michael@0 | 495 | do_check_eq(result[0].appId, stats.appId); |
michael@0 | 496 | do_check_eq(result[0].serviceType, stats.serviceType); |
michael@0 | 497 | do_check_true(compareNetworks(result[0].network, network)); |
michael@0 | 498 | let timestamp = filterTimestamp(stats.date); |
michael@0 | 499 | do_check_eq(result[0].timestamp, timestamp); |
michael@0 | 500 | do_check_eq(result[0].rxBytes, stats.rxBytes); |
michael@0 | 501 | do_check_eq(result[0].txBytes, stats.txBytes); |
michael@0 | 502 | do_check_eq(result[0].rxSystemBytes, 0); |
michael@0 | 503 | do_check_eq(result[0].txSystemBytes, 0); |
michael@0 | 504 | do_check_eq(result[0].rxTotalBytes, 0); |
michael@0 | 505 | do_check_eq(result[0].txTotalBytes, 0); |
michael@0 | 506 | run_next_test(); |
michael@0 | 507 | }); |
michael@0 | 508 | }); |
michael@0 | 509 | }); |
michael@0 | 510 | }); |
michael@0 | 511 | |
michael@0 | 512 | function prepareFind(stats, callback) { |
michael@0 | 513 | clearStore('net_stats_store', function() { |
michael@0 | 514 | netStatsDb.dbNewTxn("net_stats_store", "readwrite", function(txn, store) { |
michael@0 | 515 | netStatsDb._saveStats(txn, store, stats); |
michael@0 | 516 | }, function(error, result) { |
michael@0 | 517 | callback(error, result); |
michael@0 | 518 | }); |
michael@0 | 519 | }); |
michael@0 | 520 | } |
michael@0 | 521 | |
michael@0 | 522 | add_test(function test_find () { |
michael@0 | 523 | var networks = getNetworks(); |
michael@0 | 524 | var networkWifi = [networks[0].id, networks[0].type]; |
michael@0 | 525 | var networkMobile = [networks[1].id, networks[1].type]; // Fake mobile interface |
michael@0 | 526 | var appId = 0; |
michael@0 | 527 | var serviceType = ""; |
michael@0 | 528 | |
michael@0 | 529 | var samples = 5; |
michael@0 | 530 | var sampleRate = netStatsDb.sampleRate; |
michael@0 | 531 | var start = Date.now(); |
michael@0 | 532 | var saveDate = filterTimestamp(new Date()); |
michael@0 | 533 | var end = new Date(start + (sampleRate * (samples - 1))); |
michael@0 | 534 | start = new Date(start - sampleRate); |
michael@0 | 535 | var stats = []; |
michael@0 | 536 | for (var i = 0; i < samples; i++) { |
michael@0 | 537 | stats.push({ appId: appId, serviceType: serviceType, |
michael@0 | 538 | network: networkWifi, timestamp: saveDate + (sampleRate * i), |
michael@0 | 539 | rxBytes: 0, txBytes: 10, |
michael@0 | 540 | rxSystemBytes: 0, txSystemBytes: 0, |
michael@0 | 541 | rxTotalBytes: 0, txTotalBytes: 0 }); |
michael@0 | 542 | |
michael@0 | 543 | |
michael@0 | 544 | stats.push({ appId: appId, serviceType: serviceType, |
michael@0 | 545 | network: networkMobile, timestamp: saveDate + (sampleRate * i), |
michael@0 | 546 | rxBytes: 0, txBytes: 10, |
michael@0 | 547 | rxSystemBytes: 0, txSystemBytes: 0, |
michael@0 | 548 | rxTotalBytes: 0, txTotalBytes: 0 }); |
michael@0 | 549 | } |
michael@0 | 550 | |
michael@0 | 551 | prepareFind(stats, function(error, result) { |
michael@0 | 552 | do_check_eq(error, null); |
michael@0 | 553 | netStatsDb.find(function (error, result) { |
michael@0 | 554 | do_check_eq(error, null); |
michael@0 | 555 | do_check_eq(result.serviceType, serviceType); |
michael@0 | 556 | do_check_eq(result.network.id, networks[0].id); |
michael@0 | 557 | do_check_eq(result.network.type, networks[0].type); |
michael@0 | 558 | do_check_eq(result.start.getTime(), start.getTime()); |
michael@0 | 559 | do_check_eq(result.end.getTime(), end.getTime()); |
michael@0 | 560 | do_check_eq(result.data.length, samples + 1); |
michael@0 | 561 | do_check_eq(result.data[0].rxBytes, null); |
michael@0 | 562 | do_check_eq(result.data[1].rxBytes, 0); |
michael@0 | 563 | do_check_eq(result.data[samples].rxBytes, 0); |
michael@0 | 564 | run_next_test(); |
michael@0 | 565 | }, appId, serviceType, networks[0], start, end); |
michael@0 | 566 | }); |
michael@0 | 567 | }); |
michael@0 | 568 | |
michael@0 | 569 | add_test(function test_findAppStats () { |
michael@0 | 570 | var networks = getNetworks(); |
michael@0 | 571 | var networkWifi = [networks[0].id, networks[0].type]; |
michael@0 | 572 | var networkMobile = [networks[1].id, networks[1].type]; // Fake mobile interface |
michael@0 | 573 | var appId = 1; |
michael@0 | 574 | var serviceType = ""; |
michael@0 | 575 | |
michael@0 | 576 | var samples = 5; |
michael@0 | 577 | var sampleRate = netStatsDb.sampleRate; |
michael@0 | 578 | var start = Date.now(); |
michael@0 | 579 | var saveDate = filterTimestamp(new Date()); |
michael@0 | 580 | var end = new Date(start + (sampleRate * (samples - 1))); |
michael@0 | 581 | start = new Date(start - sampleRate); |
michael@0 | 582 | var stats = []; |
michael@0 | 583 | for (var i = 0; i < samples; i++) { |
michael@0 | 584 | stats.push({ appId: appId, serviceType: serviceType, |
michael@0 | 585 | network: networkWifi, timestamp: saveDate + (sampleRate * i), |
michael@0 | 586 | rxBytes: 0, txBytes: 10, |
michael@0 | 587 | rxTotalBytes: 0, txTotalBytes: 0 }); |
michael@0 | 588 | |
michael@0 | 589 | stats.push({ appId: appId, serviceType: serviceType, |
michael@0 | 590 | network: networkMobile, timestamp: saveDate + (sampleRate * i), |
michael@0 | 591 | rxBytes: 0, txBytes: 10, |
michael@0 | 592 | rxTotalBytes: 0, txTotalBytes: 0 }); |
michael@0 | 593 | } |
michael@0 | 594 | |
michael@0 | 595 | prepareFind(stats, function(error, result) { |
michael@0 | 596 | do_check_eq(error, null); |
michael@0 | 597 | netStatsDb.find(function (error, result) { |
michael@0 | 598 | do_check_eq(error, null); |
michael@0 | 599 | do_check_eq(result.serviceType, serviceType); |
michael@0 | 600 | do_check_eq(result.network.id, networks[0].id); |
michael@0 | 601 | do_check_eq(result.network.type, networks[0].type); |
michael@0 | 602 | do_check_eq(result.start.getTime(), start.getTime()); |
michael@0 | 603 | do_check_eq(result.end.getTime(), end.getTime()); |
michael@0 | 604 | do_check_eq(result.data.length, samples + 1); |
michael@0 | 605 | do_check_eq(result.data[0].rxBytes, null); |
michael@0 | 606 | do_check_eq(result.data[1].rxBytes, 0); |
michael@0 | 607 | do_check_eq(result.data[samples].rxBytes, 0); |
michael@0 | 608 | run_next_test(); |
michael@0 | 609 | }, appId, serviceType, networks[0], start, end); |
michael@0 | 610 | }); |
michael@0 | 611 | }); |
michael@0 | 612 | |
michael@0 | 613 | add_test(function test_findServiceStats () { |
michael@0 | 614 | var networks = getNetworks(); |
michael@0 | 615 | var networkWifi = [networks[0].id, networks[0].type]; |
michael@0 | 616 | var networkMobile = [networks[1].id, networks[1].type]; // Fake mobile interface |
michael@0 | 617 | var appId = 0; |
michael@0 | 618 | var serviceType = "FakeType"; |
michael@0 | 619 | |
michael@0 | 620 | var samples = 5; |
michael@0 | 621 | var sampleRate = netStatsDb.sampleRate; |
michael@0 | 622 | var start = Date.now(); |
michael@0 | 623 | var saveDate = filterTimestamp(new Date()); |
michael@0 | 624 | var end = new Date(start + (sampleRate * (samples - 1))); |
michael@0 | 625 | start = new Date(start - sampleRate); |
michael@0 | 626 | var stats = []; |
michael@0 | 627 | for (var i = 0; i < samples; i++) { |
michael@0 | 628 | stats.push({ appId: appId, serviceType: serviceType, |
michael@0 | 629 | network: networkWifi, timestamp: saveDate + (sampleRate * i), |
michael@0 | 630 | rxBytes: 0, txBytes: 10, |
michael@0 | 631 | rxTotalBytes: 0, txTotalBytes: 0 }); |
michael@0 | 632 | |
michael@0 | 633 | stats.push({ appId: appId, serviceType: serviceType, |
michael@0 | 634 | network: networkMobile, timestamp: saveDate + (sampleRate * i), |
michael@0 | 635 | rxBytes: 0, txBytes: 10, |
michael@0 | 636 | rxTotalBytes: 0, txTotalBytes: 0 }); |
michael@0 | 637 | } |
michael@0 | 638 | |
michael@0 | 639 | prepareFind(stats, function(error, result) { |
michael@0 | 640 | do_check_eq(error, null); |
michael@0 | 641 | netStatsDb.find(function (error, result) { |
michael@0 | 642 | do_check_eq(error, null); |
michael@0 | 643 | do_check_eq(result.serviceType, serviceType); |
michael@0 | 644 | do_check_eq(result.network.id, networks[0].id); |
michael@0 | 645 | do_check_eq(result.network.type, networks[0].type); |
michael@0 | 646 | do_check_eq(result.start.getTime(), start.getTime()); |
michael@0 | 647 | do_check_eq(result.end.getTime(), end.getTime()); |
michael@0 | 648 | do_check_eq(result.data.length, samples + 1); |
michael@0 | 649 | do_check_eq(result.data[0].rxBytes, null); |
michael@0 | 650 | do_check_eq(result.data[1].rxBytes, 0); |
michael@0 | 651 | do_check_eq(result.data[samples].rxBytes, 0); |
michael@0 | 652 | run_next_test(); |
michael@0 | 653 | }, appId, serviceType, networks[0], start, end); |
michael@0 | 654 | }); |
michael@0 | 655 | }); |
michael@0 | 656 | |
michael@0 | 657 | add_test(function test_saveMultipleAppStats () { |
michael@0 | 658 | var networks = getNetworks(); |
michael@0 | 659 | var networkWifi = networks[0]; |
michael@0 | 660 | var networkMobile = networks[1]; // Fake mobile interface |
michael@0 | 661 | |
michael@0 | 662 | var saveDate = filterTimestamp(new Date()); |
michael@0 | 663 | var cached = Object.create(null); |
michael@0 | 664 | var serviceType = "FakeType"; |
michael@0 | 665 | var wifiNetId = networkWifi.id + '' + networkWifi.type; |
michael@0 | 666 | var mobileNetId = networkMobile.id + '' + networkMobile.type; |
michael@0 | 667 | |
michael@0 | 668 | cached[0 + '' + serviceType + wifiNetId] = { |
michael@0 | 669 | appId: 0, date: new Date(), |
michael@0 | 670 | networkId: networkWifi.id, networkType: networkWifi.type, |
michael@0 | 671 | rxBytes: 0, txBytes: 10, |
michael@0 | 672 | serviceType: serviceType, isAccumulative: false |
michael@0 | 673 | }; |
michael@0 | 674 | |
michael@0 | 675 | cached[0 + '' + serviceType + mobileNetId] = { |
michael@0 | 676 | appId: 0, date: new Date(), |
michael@0 | 677 | networkId: networkMobile.id, networkType: networkMobile.type, |
michael@0 | 678 | rxBytes: 0, txBytes: 10, |
michael@0 | 679 | serviceType: serviceType, isAccumulative: false |
michael@0 | 680 | }; |
michael@0 | 681 | |
michael@0 | 682 | cached[1 + '' + wifiNetId] = { |
michael@0 | 683 | appId: 1, date: new Date(), |
michael@0 | 684 | networkId: networkWifi.id, networkType: networkWifi.type, |
michael@0 | 685 | rxBytes: 0, txBytes: 10, |
michael@0 | 686 | serviceType: "", isAccumulative: false |
michael@0 | 687 | }; |
michael@0 | 688 | |
michael@0 | 689 | cached[1 + '' + mobileNetId] = { |
michael@0 | 690 | appId: 1, date: new Date(), |
michael@0 | 691 | networkId: networkMobile.id, networkType: networkMobile.type, |
michael@0 | 692 | rxBytes: 0, txBytes: 10, |
michael@0 | 693 | serviceType: "", isAccumulative: false |
michael@0 | 694 | }; |
michael@0 | 695 | |
michael@0 | 696 | cached[2 + '' + wifiNetId] = { |
michael@0 | 697 | appId: 2, date: new Date(), |
michael@0 | 698 | networkId: networkWifi.id, networkType: networkWifi.type, |
michael@0 | 699 | rxBytes: 0, txBytes: 10, |
michael@0 | 700 | serviceType: "", isAccumulative: false |
michael@0 | 701 | }; |
michael@0 | 702 | |
michael@0 | 703 | cached[2 + '' + mobileNetId] = { |
michael@0 | 704 | appId: 2, date: new Date(), |
michael@0 | 705 | networkId: networkMobile.id, networkType: networkMobile.type, |
michael@0 | 706 | rxBytes: 0, txBytes: 10, |
michael@0 | 707 | serviceType: "", isAccumulative: false |
michael@0 | 708 | }; |
michael@0 | 709 | |
michael@0 | 710 | let keys = Object.keys(cached); |
michael@0 | 711 | let index = 0; |
michael@0 | 712 | |
michael@0 | 713 | networks.push(networkMobile); |
michael@0 | 714 | |
michael@0 | 715 | clearStore('net_stats_store', function() { |
michael@0 | 716 | netStatsDb.saveStats(cached[keys[index]], |
michael@0 | 717 | function callback(error, result) { |
michael@0 | 718 | do_check_eq(error, null); |
michael@0 | 719 | |
michael@0 | 720 | if (index == keys.length - 1) { |
michael@0 | 721 | netStatsDb.logAllRecords(function(error, result) { |
michael@0 | 722 | do_check_eq(error, null); |
michael@0 | 723 | do_check_eq(result.length, 6); |
michael@0 | 724 | do_check_eq(result[0].serviceType, serviceType); |
michael@0 | 725 | do_check_eq(result[3].appId, 1); |
michael@0 | 726 | do_check_true(compareNetworks(result[0].network, [networkWifi.id, networkWifi.type])); |
michael@0 | 727 | do_check_eq(result[0].rxBytes, 0); |
michael@0 | 728 | do_check_eq(result[0].txBytes, 10); |
michael@0 | 729 | run_next_test(); |
michael@0 | 730 | }); |
michael@0 | 731 | return; |
michael@0 | 732 | } |
michael@0 | 733 | |
michael@0 | 734 | index += 1; |
michael@0 | 735 | netStatsDb.saveStats(cached[keys[index]], callback); |
michael@0 | 736 | }); |
michael@0 | 737 | }); |
michael@0 | 738 | }); |
michael@0 | 739 | |
michael@0 | 740 | var networkWifi = '00'; |
michael@0 | 741 | var networkMobile = '11'; |
michael@0 | 742 | |
michael@0 | 743 | var examplePageURL = "http://example.com/index.html"; |
michael@0 | 744 | var exampleManifestURL = "http://example.com/manifest.webapp"; |
michael@0 | 745 | |
michael@0 | 746 | var testPageURL = "http://test.com/index.html"; |
michael@0 | 747 | var testManifestURL = "http://test.com/manifest.webapp"; |
michael@0 | 748 | |
michael@0 | 749 | var alarms = [{ id: null, |
michael@0 | 750 | networkId: networkWifi, |
michael@0 | 751 | absoluteThreshold: 10000, |
michael@0 | 752 | relativeThreshold: 10000, |
michael@0 | 753 | data: {foo: "something"}, |
michael@0 | 754 | pageURL: examplePageURL, |
michael@0 | 755 | manifestURL: exampleManifestURL }, |
michael@0 | 756 | { id: null, |
michael@0 | 757 | networkId: networkWifi, |
michael@0 | 758 | absoluteThreshold: 1000, |
michael@0 | 759 | relativeThreshold: 1000, |
michael@0 | 760 | data: {foo: "else"}, |
michael@0 | 761 | pageURL: examplePageURL, |
michael@0 | 762 | manifestURL: exampleManifestURL }, |
michael@0 | 763 | { id: null, |
michael@0 | 764 | networkId: networkMobile, |
michael@0 | 765 | absoluteThreshold: 100, |
michael@0 | 766 | relativeThreshold: 100, |
michael@0 | 767 | data: {foo: "to"}, |
michael@0 | 768 | pageURL: examplePageURL, |
michael@0 | 769 | manifestURL: exampleManifestURL }, |
michael@0 | 770 | { id: null, |
michael@0 | 771 | networkId: networkMobile, |
michael@0 | 772 | absoluteThreshold: 10, |
michael@0 | 773 | relativeThreshold: 10, |
michael@0 | 774 | data: {foo: "test"}, |
michael@0 | 775 | pageURL: testPageURL, |
michael@0 | 776 | manifestURL: testManifestURL }]; |
michael@0 | 777 | |
michael@0 | 778 | var alarmsDbId = 1; |
michael@0 | 779 | |
michael@0 | 780 | add_test(function test_addAlarm() { |
michael@0 | 781 | // Add alarms[0] -> DB: [ alarms[0] (id: 1) ] |
michael@0 | 782 | // Check the insertion is OK. |
michael@0 | 783 | netStatsDb.addAlarm(alarms[0], function(error, result) { |
michael@0 | 784 | do_check_eq(error, null); |
michael@0 | 785 | alarmsDbId = result; |
michael@0 | 786 | netStatsDb.getAlarms(Ci.nsINetworkInterface.NETWORK_TYPE_WIFI, exampleManifestURL, function(error, result) { |
michael@0 | 787 | do_check_eq(error, null); |
michael@0 | 788 | do_check_eq(result.length, 1); |
michael@0 | 789 | do_check_eq(result[0].id, alarmsDbId); |
michael@0 | 790 | do_check_eq(result[0].networkId, alarms[0].networkId); |
michael@0 | 791 | do_check_eq(result[0].absoluteThreshold, alarms[0].absoluteThreshold); |
michael@0 | 792 | do_check_eq(result[0].relativeThreshold, alarms[0].relativeThreshold); |
michael@0 | 793 | do_check_eq(result[0].data.foo, alarms[0].data.foo); |
michael@0 | 794 | run_next_test(); |
michael@0 | 795 | }); |
michael@0 | 796 | }); |
michael@0 | 797 | }); |
michael@0 | 798 | |
michael@0 | 799 | add_test(function test_getFirstAlarm() { |
michael@0 | 800 | // Add alarms[1] -> DB: [ alarms[0] (id: 1), alarms[1] (id: 2) ] |
michael@0 | 801 | // Check first alarm is alarms[1] because threshold is lower. |
michael@0 | 802 | alarmsDbId += 1; |
michael@0 | 803 | netStatsDb.addAlarm(alarms[1], function (error, result) { |
michael@0 | 804 | do_check_eq(error, null); |
michael@0 | 805 | do_check_eq(result, alarmsDbId); |
michael@0 | 806 | netStatsDb.getFirstAlarm(networkWifi, function(error, result) { |
michael@0 | 807 | do_check_eq(error, null); |
michael@0 | 808 | do_check_eq(result.id, alarmsDbId); |
michael@0 | 809 | do_check_eq(result.networkId, alarms[1].networkId); |
michael@0 | 810 | do_check_eq(result.absoluteThreshold, alarms[1].absoluteThreshold); |
michael@0 | 811 | do_check_eq(result.relativeThreshold, alarms[1].relativeThreshold); |
michael@0 | 812 | do_check_eq(result.data.foo, alarms[1].data.foo); |
michael@0 | 813 | do_check_eq(result.pageURL, alarms[1].pageURL); |
michael@0 | 814 | do_check_eq(result.manifestURL, alarms[1].manifestURL); |
michael@0 | 815 | run_next_test(); |
michael@0 | 816 | }); |
michael@0 | 817 | }); |
michael@0 | 818 | }); |
michael@0 | 819 | |
michael@0 | 820 | add_test(function test_removeAlarm() { |
michael@0 | 821 | // Remove alarms[1] (id: 2) -> DB: [ alarms[0] (id: 1) ] |
michael@0 | 822 | // Check get first return alarms[0]. |
michael@0 | 823 | netStatsDb.removeAlarm(alarmsDbId, alarms[0].manifestURL, function (error, result) { |
michael@0 | 824 | do_check_eq(error, null); |
michael@0 | 825 | netStatsDb.getFirstAlarm(networkWifi, function(error, result) { |
michael@0 | 826 | do_check_eq(error, null); |
michael@0 | 827 | do_check_eq(result.id, alarmsDbId - 1); |
michael@0 | 828 | do_check_eq(result.networkId, alarms[0].networkId); |
michael@0 | 829 | do_check_eq(result.absoluteThreshold, alarms[0].absoluteThreshold); |
michael@0 | 830 | do_check_eq(result.relativeThreshold, alarms[0].relativeThreshold); |
michael@0 | 831 | do_check_eq(result.data.foo, alarms[0].data.foo); |
michael@0 | 832 | do_check_eq(result.pageURL, alarms[0].pageURL); |
michael@0 | 833 | do_check_eq(result.manifestURL, alarms[0].manifestURL); |
michael@0 | 834 | run_next_test(); |
michael@0 | 835 | }); |
michael@0 | 836 | }); |
michael@0 | 837 | }); |
michael@0 | 838 | |
michael@0 | 839 | add_test(function test_removeAppAlarm() { |
michael@0 | 840 | // Remove alarms[0] (id: 1) -> DB: [ ] |
michael@0 | 841 | netStatsDb.removeAlarm(alarmsDbId - 1, alarms[0].manifestURL, function (error, result) { |
michael@0 | 842 | do_check_eq(error, null); |
michael@0 | 843 | netStatsDb.getAlarms(networkWifi, exampleManifestURL, function(error, result) { |
michael@0 | 844 | do_check_eq(error, null); |
michael@0 | 845 | do_check_eq(result.length, 0); |
michael@0 | 846 | run_next_test(); |
michael@0 | 847 | }); |
michael@0 | 848 | }); |
michael@0 | 849 | }); |
michael@0 | 850 | |
michael@0 | 851 | add_test(function test_getAlarms() { |
michael@0 | 852 | // Add all alarms -> DB: [ alarms[0] (id: 3), |
michael@0 | 853 | // alarms[1] (id: 4), |
michael@0 | 854 | // alarms[2] (id: 5), |
michael@0 | 855 | // alarms[3] (id: 6) ] |
michael@0 | 856 | // Check that getAlarms for wifi returns 2 alarms. |
michael@0 | 857 | // Check that getAlarms for all connections returns 3 alarms. |
michael@0 | 858 | |
michael@0 | 859 | var callback = function () { |
michael@0 | 860 | netStatsDb.getAlarms(networkWifi, exampleManifestURL, function(error, result) { |
michael@0 | 861 | do_check_eq(error, null); |
michael@0 | 862 | do_check_eq(result.length, 2); |
michael@0 | 863 | netStatsDb.getAlarms(null, exampleManifestURL, function(error, result) { |
michael@0 | 864 | do_check_eq(error, null); |
michael@0 | 865 | do_check_eq(result.length, 3); |
michael@0 | 866 | run_next_test(); |
michael@0 | 867 | }); |
michael@0 | 868 | }); |
michael@0 | 869 | }; |
michael@0 | 870 | |
michael@0 | 871 | var index = 0; |
michael@0 | 872 | |
michael@0 | 873 | var addFunction = function () { |
michael@0 | 874 | alarmsDbId += 1; |
michael@0 | 875 | netStatsDb.addAlarm(alarms[index], function (error, result) { |
michael@0 | 876 | do_check_eq(error, null); |
michael@0 | 877 | index += 1; |
michael@0 | 878 | do_check_eq(result, alarmsDbId); |
michael@0 | 879 | if (index >= alarms.length) { |
michael@0 | 880 | callback(); |
michael@0 | 881 | return; |
michael@0 | 882 | } |
michael@0 | 883 | addFunction(); |
michael@0 | 884 | }); |
michael@0 | 885 | }; |
michael@0 | 886 | |
michael@0 | 887 | addFunction(); |
michael@0 | 888 | }); |
michael@0 | 889 | |
michael@0 | 890 | add_test(function test_removeAppAllAlarms() { |
michael@0 | 891 | // Remove all alarms for exampleManifestURL -> DB: [ alarms[3] (id: 6) ] |
michael@0 | 892 | netStatsDb.removeAlarms(exampleManifestURL, function (error, result) { |
michael@0 | 893 | do_check_eq(error, null); |
michael@0 | 894 | netStatsDb.getAlarms(null, exampleManifestURL, function(error, result) { |
michael@0 | 895 | do_check_eq(error, null); |
michael@0 | 896 | do_check_eq(result.length, 0); |
michael@0 | 897 | netStatsDb.getAlarms(null, testManifestURL, function(error, result) { |
michael@0 | 898 | do_check_eq(error, null); |
michael@0 | 899 | do_check_eq(result.length, 1); |
michael@0 | 900 | run_next_test(); |
michael@0 | 901 | }); |
michael@0 | 902 | }); |
michael@0 | 903 | }); |
michael@0 | 904 | }); |
michael@0 | 905 | |
michael@0 | 906 | add_test(function test_updateAlarm() { |
michael@0 | 907 | // Update alarms[3] (id: 6) -> DB: [ alarms[3]* (id: 6) ] |
michael@0 | 908 | |
michael@0 | 909 | var updatedAlarm = alarms[1]; |
michael@0 | 910 | updatedAlarm.id = alarmsDbId; |
michael@0 | 911 | updatedAlarm.threshold = 10; |
michael@0 | 912 | |
michael@0 | 913 | netStatsDb.updateAlarm(updatedAlarm, function (error, result) { |
michael@0 | 914 | do_check_eq(error, null); |
michael@0 | 915 | netStatsDb.getFirstAlarm(networkWifi, function(error, result) { |
michael@0 | 916 | do_check_eq(error, null); |
michael@0 | 917 | do_check_eq(result.id, updatedAlarm.id); |
michael@0 | 918 | do_check_eq(result.networkId, updatedAlarm.networkId); |
michael@0 | 919 | do_check_eq(result.absoluteThreshold, updatedAlarm.absoluteThreshold); |
michael@0 | 920 | do_check_eq(result.relativeThreshold, updatedAlarm.relativeThreshold); |
michael@0 | 921 | do_check_eq(result.data.foo, updatedAlarm.data.foo); |
michael@0 | 922 | do_check_eq(result.pageURL, updatedAlarm.pageURL); |
michael@0 | 923 | do_check_eq(result.manifestURL, updatedAlarm.manifestURL); |
michael@0 | 924 | run_next_test(); |
michael@0 | 925 | }); |
michael@0 | 926 | }); |
michael@0 | 927 | }); |
michael@0 | 928 | |
michael@0 | 929 | function run_test() { |
michael@0 | 930 | do_get_profile(); |
michael@0 | 931 | run_next_test(); |
michael@0 | 932 | } |