diff --git a/dom/network/tests/Makefile.in b/dom/network/tests/Makefile.in index 52347bae4fa..3583ac0bfeb 100644 --- a/dom/network/tests/Makefile.in +++ b/dom/network/tests/Makefile.in @@ -21,8 +21,18 @@ MOCHITEST_FILES = \ test_tcpsocket_enabled_with_perm.html \ $(NULL) +ifdef MOZ_B2G_RIL +MOCHITEST_FILES = \ + test_networkstats_basics.html \ + $(NULL) +endif + MODULE = test_dom_socket XPCSHELL_TESTS = unit unit_ipc +ifdef MOZ_B2G_RIL +XPCSHELL_TESTS += unit_stats +endif + include $(topsrcdir)/config/rules.mk diff --git a/dom/network/tests/test_networkstats_basics.html b/dom/network/tests/test_networkstats_basics.html new file mode 100644 index 00000000000..d4103f20f7a --- /dev/null +++ b/dom/network/tests/test_networkstats_basics.html @@ -0,0 +1,306 @@ + + +
++ ++ + diff --git a/dom/network/tests/unit_stats/test_networkstats_db.js b/dom/network/tests/unit_stats/test_networkstats_db.js new file mode 100644 index 00000000000..834a7b419ec --- /dev/null +++ b/dom/network/tests/unit_stats/test_networkstats_db.js @@ -0,0 +1,387 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; + +Cu.import("resource://gre/modules/NetworkStatsDB.jsm"); + +const netStatsDb = new NetworkStatsDB(this); + +function filterTimestamp(date) { + var sampleRate = netStatsDb.sampleRate; + var offset = date.getTimezoneOffset() * 60 * 1000; + return Math.floor((date.getTime() - offset) / sampleRate) * sampleRate + offset; +} + +add_test(function test_sampleRate() { + var sampleRate = netStatsDb.sampleRate; + do_check_true(sampleRate > 0); + netStatsDb.sampleRate = 0; + sampleRate = netStatsDb.sampleRate; + do_check_true(sampleRate > 0); + + run_next_test(); +}); + +add_test(function test_maxStorageSamples() { + var maxStorageSamples = netStatsDb.maxStorageSamples; + do_check_true(maxStorageSamples > 0); + netStatsDb.maxStorageSamples = 0; + maxStorageSamples = netStatsDb.maxStorageSamples; + do_check_true(maxStorageSamples > 0); + + run_next_test(); +}); + +add_test(function test_fillResultSamples_emptyData() { + var samples = 3; + var data = []; + var start = filterTimestamp(new Date()); + var sampleRate = netStatsDb.sampleRate; + var end = start + (sampleRate * samples); + netStatsDb.fillResultSamples(start, end, data); + do_check_eq(data.length, samples + 1); + + var aux = start; + var success = true; + for (var i = 0; i <= samples; i++) { + if (data[i].date.getTime() != aux || data[i].rxBytes != undefined || data[i].txBytes != undefined) { + success = false; + break; + } + aux += sampleRate; + } + do_check_true(success); + + run_next_test(); +}); + +add_test(function test_fillResultSamples_noEmptyData() { + var samples = 3; + var sampleRate = netStatsDb.sampleRate; + var start = filterTimestamp(new Date()); + var end = start + (sampleRate * samples); + var data = [{date: new Date(start + sampleRate), + rxBytes: 0, + txBytes: 0}]; + netStatsDb.fillResultSamples(start, end, data); + do_check_eq(data.length, samples + 1); + + var aux = start; + var success = true; + for (var i = 0; i <= samples; i++) { + if (i == 1) { + if (data[i].date.getTime() != aux || data[i].rxBytes != 0 || data[i].txBytes != 0) { + success = false; + break; + } + } else { + if (data[i].date.getTime() != aux || data[i].rxBytes != undefined || data[i].txBytes != undefined) { + success = false; + break; + } + } + aux += sampleRate; + } + do_check_true(success); + + run_next_test(); +}); + +add_test(function test_clear() { + netStatsDb.clear(function (error, result) { + do_check_eq(error, null); + run_next_test(); + }); +}); + +add_test(function test_internalSaveStats_singleSample() { + var stats = {connectionType: "wifi", + timestamp: Date.now(), + rxBytes: 0, + txBytes: 0, + rxTotalBytes: 1234, + txTotalBytes: 1234}; + + netStatsDb.dbNewTxn("readwrite", function(txn, store) { + netStatsDb._saveStats(txn, store, stats); + }, function(error, result) { + do_check_eq(error, null); + + netStatsDb.logAllRecords(function(error, result) { + do_check_eq(error, null); + do_check_eq(result.length, 1); + do_check_eq(result[0].connectionType, stats.connectionType); + do_check_eq(result[0].timestamp, stats.timestamp); + do_check_eq(result[0].rxBytes, stats.rxBytes); + do_check_eq(result[0].txBytes, stats.txBytes); + do_check_eq(result[0].rxTotalBytes, stats.rxTotalBytes); + do_check_eq(result[0].txTotalBytes, stats.txTotalBytes); + run_next_test(); + }); + }); +}); + +add_test(function test_internalSaveStats_arraySamples() { + netStatsDb.clear(function (error, result) { + do_check_eq(error, null); + + var samples = 2; + var stats = []; + for (var i = 0; i < samples; i++) { + stats.push({connectionType: "wifi", + timestamp: Date.now() + (10 * i), + rxBytes: 0, + txBytes: 0, + rxTotalBytes: 1234, + txTotalBytes: 1234}); + } + + netStatsDb.dbNewTxn("readwrite", function(txn, store) { + netStatsDb._saveStats(txn, store, stats); + }, function(error, result) { + do_check_eq(error, null); + + netStatsDb.logAllRecords(function(error, result) { + do_check_eq(error, null); + do_check_eq(result.length, samples); + + var success = true; + for (var i = 0; i < samples; i++) { + if (result[i].connectionType != stats[i].connectionType || + result[i].timestamp != stats[i].timestamp || + result[i].rxBytes != stats[i].rxBytes || + result[i].txBytes != stats[i].txBytes || + result[i].rxTotalBytes != stats[i].rxTotalBytes || + result[i].txTotalBytes != stats[i].txTotalBytes) { + success = false; + break; + } + } + do_check_true(success); + run_next_test(); + }); + }); + }); +}); + +add_test(function test_internalRemoveOldStats() { + netStatsDb.clear(function (error, result) { + do_check_eq(error, null); + + var samples = 10; + var stats = []; + for (var i = 0; i < samples - 1; i++) { + stats.push({connectionType: "wifi", timestamp: Date.now() + (10 * i), + rxBytes: 0, txBytes: 0, + rxTotalBytes: 1234, txTotalBytes: 1234}); + } + + stats.push({connectionType: "wifi", timestamp: Date.now() + (10 * samples), + rxBytes: 0, txBytes: 0, + rxTotalBytes: 1234, txTotalBytes: 1234}); + + netStatsDb.dbNewTxn("readwrite", function(txn, store) { + netStatsDb._saveStats(txn, store, stats); + var date = stats[stats.length -1].timestamp + + (netStatsDb.sampleRate * netStatsDb.maxStorageSamples - 1) - 1; + netStatsDb._removeOldStats(txn, store, "wifi", date); + }, function(error, result) { + do_check_eq(error, null); + + netStatsDb.logAllRecords(function(error, result) { + do_check_eq(error, null); + do_check_eq(result.length, 1); + + run_next_test(); + }); + }); + }); +}); + +function processSamplesDiff(lastStat, newStat, callback) { + netStatsDb.clear(function (error, result){ + do_check_eq(error, null); + netStatsDb.dbNewTxn("readwrite", function(txn, store) { + netStatsDb._saveStats(txn, store, lastStat); + }, function(error, result) { + netStatsDb.dbNewTxn("readwrite", function(txn, store) { + let request = store.index("connectionType").openCursor(newStat.connectionType, "prev"); + request.onsuccess = function onsuccess(event) { + let cursor = event.target.result; + do_check_neq(cursor, null); + netStatsDb._processSamplesDiff(txn, store, cursor, newStat); + }; + }, function(error, result) { + do_check_eq(error, null); + netStatsDb.logAllRecords(function(error, result) { + do_check_eq(error, null); + callback(result); + }); + }); + }); + }); +} + +add_test(function test_processSamplesDiffSameSample() { + var sampleRate = netStatsDb.sampleRate; + var date = filterTimestamp(new Date()); + var lastStat = {connectionType: "wifi", timestamp: date, + rxBytes: 0, txBytes: 0, + rxTotalBytes: 1234, txTotalBytes: 1234}; + + var newStat = {connectionType: "wifi", timestamp: date, + rxBytes: 0, txBytes: 0, + rxTotalBytes: 2234, txTotalBytes: 2234}; + + processSamplesDiff(lastStat, newStat, function(result) { + do_check_eq(result.length, 1); + do_check_eq(result[0].connectionType, newStat.connectionType); + do_check_eq(result[0].timestamp, newStat.timestamp); + do_check_eq(result[0].rxBytes, newStat.rxTotalBytes - lastStat.rxTotalBytes); + do_check_eq(result[0].txBytes, newStat.txTotalBytes - lastStat.txTotalBytes); + do_check_eq(result[0].rxTotalBytes, newStat.rxTotalBytes); + do_check_eq(result[0].txTotalBytes, newStat.txTotalBytes); + run_next_test(); + }); +}); + +add_test(function test_processSamplesDiffNextSample() { + var sampleRate = netStatsDb.sampleRate; + var date = filterTimestamp(new Date()); + var lastStat = {connectionType: "wifi", timestamp: date, + rxBytes: 0, txBytes: 0, + rxTotalBytes: 1234, txTotalBytes: 1234}; + + var newStat = {connectionType: "wifi", timestamp: date + sampleRate, + rxBytes: 0, txBytes: 0, + rxTotalBytes: 500, txTotalBytes: 500}; + + processSamplesDiff(lastStat, newStat, function(result) { + do_check_eq(result.length, 2); + do_check_eq(result[1].connectionType, newStat.connectionType); + do_check_eq(result[1].timestamp, newStat.timestamp); + do_check_eq(result[1].rxBytes, newStat.rxTotalBytes); + do_check_eq(result[1].txBytes, newStat.txTotalBytes); + do_check_eq(result[1].rxTotalBytes, newStat.rxTotalBytes); + do_check_eq(result[1].txTotalBytes, newStat.txTotalBytes); + run_next_test(); + }); +}); + +add_test(function test_processSamplesDiffSamplesLost() { + var samples = 5; + var sampleRate = netStatsDb.sampleRate; + var date = filterTimestamp(new Date()); + var lastStat = {connectionType: "wifi", timestamp: date, + rxBytes: 0, txBytes: 0, + rxTotalBytes: 1234, txTotalBytes: 1234}; + + var newStat = {connectionType: "wifi", timestamp: date + (sampleRate * samples), + rxBytes: 0, txBytes: 0, + rxTotalBytes: 2234, txTotalBytes: 2234}; + + processSamplesDiff(lastStat, newStat, function(result) { + do_check_eq(result.length, samples + 1); + do_check_eq(result[samples].connectionType, newStat.connectionType); + do_check_eq(result[samples].timestamp, newStat.timestamp); + do_check_eq(result[samples].rxBytes, newStat.rxTotalBytes - lastStat.rxTotalBytes); + do_check_eq(result[samples].txBytes, newStat.txTotalBytes - lastStat.txTotalBytes); + do_check_eq(result[samples].rxTotalBytes, newStat.rxTotalBytes); + do_check_eq(result[samples].txTotalBytes, newStat.txTotalBytes); + run_next_test(); + }); +}); + +add_test(function test_saveStats() { + var stats = { connectionType: "wifi", + date: new Date(), + rxBytes: 2234, + txBytes: 2234}; + + netStatsDb.clear(function (error, result) { + do_check_eq(error, null); + netStatsDb.saveStats(stats, function(error, result) { + do_check_eq(error, null); + netStatsDb.logAllRecords(function(error, result) { + do_check_eq(error, null); + do_check_eq(result.length, 1); + do_check_eq(result[0].connectionType, stats.connectionType); + let timestamp = filterTimestamp(stats.date); + do_check_eq(result[0].timestamp, timestamp); + do_check_eq(result[0].rxBytes, 0); + do_check_eq(result[0].txBytes, 0); + do_check_eq(result[0].rxTotalBytes, stats.rxBytes); + do_check_eq(result[0].txTotalBytes, stats.txBytes); + run_next_test(); + }); + }); + }); +}); + +function prepareFind(stats, callback) { + netStatsDb.clear(function (error, result) { + do_check_eq(error, null); + netStatsDb.dbNewTxn("readwrite", function(txn, store) { + netStatsDb._saveStats(txn, store, stats); + }, function(error, result) { + callback(error, result); + }); + }); +} + +add_test(function test_find () { + var samples = 5; + var sampleRate = netStatsDb.sampleRate; + var start = Date.now(); + var saveDate = filterTimestamp(new Date()); + var end = start + (sampleRate * (samples - 1)); + start -= sampleRate; + var stats = []; + for (var i = 0; i < samples; i++) {i + stats.push({connectionType: "wifi", timestamp: saveDate + (sampleRate * i), + rxBytes: 0, txBytes: 10, + rxTotalBytes: 0, txTotalBytes: 0}); + + stats.push({connectionType: "mobile", timestamp: saveDate + (sampleRate * i), + rxBytes: 0, txBytes: 10, + rxTotalBytes: 0, txTotalBytes: 0}); + } + + prepareFind(stats, function(error, result) { + do_check_eq(error, null); + netStatsDb.find(function (error, result) { + do_check_eq(error, null); + do_check_eq(result.connectionType, "wifi"); + do_check_eq(result.start.getTime(), start); + do_check_eq(result.end.getTime(), end); + do_check_eq(result.data.length, samples + 1); + do_check_eq(result.data[0].rxBytes, null); + do_check_eq(result.data[1].rxBytes, 0); + do_check_eq(result.data[samples].rxBytes, 0); + + netStatsDb.findAll(function (error, result) { + do_check_eq(error, null); + do_check_eq(result.connectionType, null); + do_check_eq(result.start.getTime(), start); + do_check_eq(result.end.getTime(), end); + do_check_eq(result.data.length, samples + 1); + do_check_eq(result.data[0].rxBytes, null); + do_check_eq(result.data[1].rxBytes, 0); + do_check_eq(result.data[1].txBytes, 20); + do_check_eq(result.data[samples].rxBytes, 0); + run_next_test(); + }, {start: start, end: end}); + }, {start: start, end: end, connectionType: "wifi"}); + }); +}); + +function run_test() { + do_get_profile(); + + var idbManager = Cc["@mozilla.org/dom/indexeddb/manager;1"]. + getService(Ci.nsIIndexedDatabaseManager); + idbManager.initWindowless(this); + + run_next_test(); +} diff --git a/dom/network/tests/unit_stats/test_networkstats_service.js b/dom/network/tests/unit_stats/test_networkstats_service.js new file mode 100644 index 00000000000..2037cd8a879 --- /dev/null +++ b/dom/network/tests/unit_stats/test_networkstats_service.js @@ -0,0 +1,105 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; + +add_test(function test_clearDB() { + NetworkStatsService._db.clear(function onDBCleared(error, result) { + do_check_eq(result, null); + run_next_test(); + }); +}); + + +add_test(function test_networkStatsAvailable_ok() { + NetworkStatsService.networkStatsAvailable(function (success, msg) { + do_check_eq(success, true); + run_next_test(); + }, true, Ci.nsINetworkInterface.NETWORK_TYPE_WIFI, 1234, 4321, new Date()); +}); + +add_test(function test_networkStatsAvailable_failure() { + NetworkStatsService.networkStatsAvailable(function (success, msg) { + do_check_eq(success, false); + run_next_test(); + }, false, Ci.nsINetworkInterface.NETWORK_TYPE_WIFI, 1234, 4321, new Date()); +}); + +add_test(function test_update_invalidConnection() { + NetworkStatsService.update(-1, function (success, msg) { + do_check_eq(success, false); + do_check_eq(msg, "Invalid network type -1"); + run_next_test(); + }); +}); + +add_test(function test_update() { + NetworkStatsService.update(Ci.nsINetworkInterface.NETWORK_TYPE_WIFI, function (success, msg) { + do_check_eq(success, true); + run_next_test(); + }); +}); + +add_test(function test_updateQueueIndex() { + NetworkStatsService.updateQueue = [{type: 0, callbacks: null}, + {type: 1, callbacks: null}, + {type: 2, callbacks: null}, + {type: 3, callbacks: null}, + {type: 4, callbacks: null}]; + var index = NetworkStatsService.updateQueueIndex(3); + do_check_eq(index, 3); + index = NetworkStatsService.updateQueueIndex(10); + do_check_eq(index, -1); + + NetworkStatsService.updateQueue = []; + run_next_test(); +}); + +add_test(function test_updateAllStats() { + NetworkStatsService.updateAllStats(function(success, msg) { + do_check_eq(success, true); + run_next_test(); + }); +}); + +add_test(function test_updateStats_ok() { + NetworkStatsService.updateStats(Ci.nsINetworkInterface.NETWORK_TYPE_WIFI, function(success, msg){ + do_check_eq(success, true); + run_next_test(); + }); +}); + +add_test(function test_updateStats_failure() { + NetworkStatsService.updateStats(-1, function(success, msg){ + do_check_eq(success, false); + run_next_test(); + }); +}); + +add_test(function test_queue() { + NetworkStatsService.updateStats(Ci.nsINetworkInterface.NETWORK_TYPE_WIFI); + NetworkStatsService.updateStats(Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE); + do_check_eq(NetworkStatsService.updateQueue.length, 2); + do_check_eq(NetworkStatsService.updateQueue[0].callbacks.length, 1); + + NetworkStatsService.updateStats(Ci.nsINetworkInterface.NETWORK_TYPE_WIFI, function(success, msg){ + do_check_eq(NetworkStatsService.updateQueue.length, 1); + }); + + NetworkStatsService.updateStats(Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE, function(success, msg){ + do_check_eq(NetworkStatsService.updateQueue.length, 0); + run_next_test(); + }); + + do_check_eq(NetworkStatsService.updateQueue.length, 2); + do_check_eq(NetworkStatsService.updateQueue[0].callbacks.length, 2); + do_check_eq(NetworkStatsService.updateQueue[0].callbacks[0], null); + do_check_neq(NetworkStatsService.updateQueue[0].callbacks[1], null); +}); + +function run_test() { + do_get_profile(); + + Cu.import("resource://gre/modules/NetworkStatsService.jsm"); + run_next_test(); +} diff --git a/dom/network/tests/unit_stats/xpcshell.ini b/dom/network/tests/unit_stats/xpcshell.ini new file mode 100644 index 00000000000..a6fea9b8a4a --- /dev/null +++ b/dom/network/tests/unit_stats/xpcshell.ini @@ -0,0 +1,6 @@ +[DEFAULT] +head = +tail = + +[test_networkstats_service.js] +[test_networkstats_db.js] diff --git a/dom/tests/mochitest/general/test_interfaces.html b/dom/tests/mochitest/general/test_interfaces.html index fd1a0cb7327..c169b639f20 100644 --- a/dom/tests/mochitest/general/test_interfaces.html +++ b/dom/tests/mochitest/general/test_interfaces.html @@ -539,6 +539,9 @@ var interfaceNamesInGlobalScope = "PermissionSettings", "DataErrorEvent", "DataChannel" + "MozNetworkStatsManager", + "MozNetworkStats", + "MozNetworkStatsData" ] for (var i in SpecialPowers.Components.interfaces) { diff --git a/testing/xpcshell/xpcshell.ini b/testing/xpcshell/xpcshell.ini index 70f030c7014..437afc7a889 100644 --- a/testing/xpcshell/xpcshell.ini +++ b/testing/xpcshell/xpcshell.ini @@ -15,6 +15,7 @@ [include:dom/mms/tests/xpcshell.ini] [include:dom/network/tests/unit/xpcshell.ini] [include:dom/network/tests/unit_ipc/xpcshell.ini] +[include:dom/network/tests/unit_stats/xpcshell.ini] [include:dom/src/json/test/unit/xpcshell.ini] [include:dom/system/gonk/tests/xpcshell.ini] [include:dom/tests/unit/xpcshell.ini]