mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-17 07:15:46 +00:00
Bug 887699 - Part 4/4 Tests. r=gene
This commit is contained in:
parent
9391a1773f
commit
342d051d20
@ -12,45 +12,34 @@
|
||||
<pre id="test">
|
||||
<script type="application/javascript">
|
||||
|
||||
// Test for NetworkStats
|
||||
function checkInterface(aInterface) {
|
||||
ok(!(aInterface in window), aInterface + " should be prefixed");
|
||||
ok(("Moz" + aInterface) in window, aInterface + " should be prefixed");
|
||||
}
|
||||
|
||||
function test() {
|
||||
// Test interfaces
|
||||
checkInterface("NetworkStatsManager");
|
||||
checkInterface("NetworkStats");
|
||||
checkInterface("NetworkStatsData");
|
||||
|
||||
ok('mozNetworkStats' in navigator, "navigator.mozMozNetworkStats should exist");
|
||||
ok(navigator.mozNetworkStats, "navigator.mozNetworkStats returns an object");
|
||||
|
||||
netStats = navigator.mozNetworkStats;
|
||||
|
||||
// Test IDL attributes
|
||||
ok('connectionTypes' in netStats,
|
||||
"connectionTypes should be a NetworkStats attribute");
|
||||
ok(Array.isArray(netStats.connectionTypes) && netStats.connectionTypes.length > 0,
|
||||
"connectionTypes is an array not empty.");
|
||||
ok('availableNetworks' in netStats,
|
||||
"availableNetworks should be a NetworkStats attribute");
|
||||
ok(Array.isArray(netStats.availableNetworks) && netStats.availableNetworks.length > 0,
|
||||
"availableNetworks is an array not empty.");
|
||||
|
||||
ok('sampleRate' in netStats,
|
||||
"sampleRate should be a NetworkStats attribute");
|
||||
ok(netStats.sampleRate > 0,
|
||||
"sampleRate is greater than 0.");
|
||||
|
||||
ok('maxStorageSamples' in netStats,
|
||||
"maxStorageSamples should be a NetworkStats attribute");
|
||||
ok(netStats.maxStorageSamples > 0,
|
||||
"maxStorageSamples is greater than 0.");
|
||||
ok('maxStorageAge' in netStats,
|
||||
"maxStorageAge should be a NetworkStats attribute");
|
||||
ok(netStats.maxStorageAge > 0,
|
||||
"maxStorageAge is greater than 0.");
|
||||
|
||||
// Test IDL methods
|
||||
next();
|
||||
return;
|
||||
}
|
||||
|
||||
function checkDataDates(data, start, end, sampleRate){
|
||||
function checkDataDates(data, start, end, sampleRate) {
|
||||
var offset = (new Date()).getTimezoneOffset() * 60 * 1000;
|
||||
start = Math.floor((start.getTime() - offset) / sampleRate) * sampleRate + offset;
|
||||
end = Math.floor((end.getTime() - offset) / sampleRate) * sampleRate + offset;
|
||||
@ -71,84 +60,114 @@ function checkDataDates(data, start, end, sampleRate){
|
||||
ok(success, "data result has correct dates");
|
||||
}
|
||||
|
||||
function compareNetworks(networkA, networkB) {
|
||||
return (networkA.id == networkB.id &&
|
||||
networkA.type == networkB.type);
|
||||
}
|
||||
|
||||
var req;
|
||||
var index = -1;
|
||||
var netStats = null;
|
||||
|
||||
var steps = [
|
||||
function () {
|
||||
// Test clearAlldata
|
||||
req = netStats.clearAllData();
|
||||
// Test clearAllStats
|
||||
req = netStats.clearAllStats();
|
||||
req.onsuccess = function () {
|
||||
ok(true, "clearAllData deleted the database");
|
||||
ok(true, "clearAllStats deleted the database");
|
||||
next();
|
||||
};
|
||||
req.onerror = function () {
|
||||
ok(false, "clearAllData deleted the database");
|
||||
ok(false, "clearAllStats deleted the database");
|
||||
}
|
||||
},
|
||||
function () {
|
||||
// Check if getNetworkStats launch exception when start is greather than end
|
||||
// Check if getSamples launch exception when start is greather than end
|
||||
|
||||
// Prepare get params
|
||||
var type = netStats.connectionTypes[0];
|
||||
var network = netStats.availableNetworks[0];
|
||||
// Get dates
|
||||
var endDate = new Date();
|
||||
var startDate = new Date(endDate.getTime() + 1000);
|
||||
|
||||
try {
|
||||
netStats.getNetworkStats({start: startDate, end: endDate});
|
||||
netStats.getSamples(network, startDate, endDate);
|
||||
} catch(ex) {
|
||||
ok(true, "getNetworkStats launch exception when start is greater than end");
|
||||
ok(true, "getSamples launch exception when start is greater than end");
|
||||
next();
|
||||
return;
|
||||
}
|
||||
|
||||
ok(false, "getNetworkStats launch exceptionwhen start is greater than end");
|
||||
ok(false, "getSamples launch exception when start is greater than end");
|
||||
next();
|
||||
return;
|
||||
},
|
||||
function () {
|
||||
// Test if call getNetworkStats with undefined start param launch an exception
|
||||
// Test if call getSamples with network of type different than
|
||||
// nsIDOMMozNetworkStatsInterface launch an exception
|
||||
|
||||
// Prepare get params
|
||||
var type = netStats.connectionTypes[0];
|
||||
setTimeout(function() {
|
||||
try {
|
||||
netStats.getNetworkStats({end: new Date()});
|
||||
} catch(ex) {
|
||||
ok(true, "getNetworkStats launch exception when start param does not exist");
|
||||
next();
|
||||
return;
|
||||
}
|
||||
var network = "wifi";
|
||||
var endDate = new Date();
|
||||
var startDate = new Date(endDate.getTime() - 1000);
|
||||
|
||||
ok(false, "getNetworkStats launch exception when start param does not exist");
|
||||
}, 1000);
|
||||
try {
|
||||
netStats.getSamples(network, new Date(), new Date());
|
||||
} catch(ex) {
|
||||
ok(true, "getSamples launch exception if network is not " +
|
||||
"a nsIDOMMozNetworkStatsInterface");
|
||||
next();
|
||||
return;
|
||||
}
|
||||
|
||||
ok(false, "getSamples launch exception if network is not " +
|
||||
"a nsIDOMMozNetworkStatsInterface");
|
||||
},
|
||||
function () {
|
||||
// Test if call getNetworkStats with undefined end param launch an exception
|
||||
// Test if call getSamples with start parameter type different than Date launch an exception
|
||||
|
||||
// Prepare get params
|
||||
var type = netStats.connectionTypes[0];
|
||||
setTimeout(function() {
|
||||
try {
|
||||
netStats.getNetworkStats({start: new Date()});
|
||||
} catch(ex) {
|
||||
ok(true, "getNetworkStats launch exception when end param does not exist");
|
||||
next();
|
||||
return;
|
||||
}
|
||||
var network = netStats.availableNetworks[0];
|
||||
var endDate = new Date();
|
||||
var startDate = new Date(endDate.getTime() - 1000);
|
||||
startDate = startDate.toString();
|
||||
|
||||
ok(false, "getNetworkStats launch exception when end param does not exist");
|
||||
}, 1000);
|
||||
try {
|
||||
netStats.getSamples(network, startDate, endDate);
|
||||
} catch(ex) {
|
||||
ok(true, "getSamples launch exception when start param is not a Date");
|
||||
next();
|
||||
return;
|
||||
}
|
||||
|
||||
ok(false, "getSamples launch exception when start param is not a Date");
|
||||
},
|
||||
function () {
|
||||
ok(true, "Get system stats for a connectionType and dates adapted to samplerate");
|
||||
// Test if call getSamples with end parameter type different than Date launch an exception
|
||||
|
||||
// Prepare get params
|
||||
var type = netStats.connectionTypes[0];
|
||||
var network = netStats.availableNetworks[0];
|
||||
var endDate = new Date();
|
||||
var startDate = new Date(endDate.getTime() - 1000);
|
||||
endDate = startDate.toString();
|
||||
|
||||
try {
|
||||
netStats.getSamples(network, startDate, endDate);
|
||||
} catch(ex) {
|
||||
ok(true, "getSamples launch exception when end param is not a Date");
|
||||
next();
|
||||
return;
|
||||
}
|
||||
|
||||
ok(false, "getSamples launch exception when end param is not a Date");
|
||||
},
|
||||
function () {
|
||||
ok(true, "Get stats for a network and dates adapted to samplerate");
|
||||
// Prepare get params
|
||||
var network = netStats.availableNetworks[0];
|
||||
var diff = 2;
|
||||
// Get samplerate in millis
|
||||
var sampleRate = netStats.sampleRate * 1000;
|
||||
var sampleRate = netStats.sampleRate;
|
||||
// Get date with samplerate's precision
|
||||
var offset = new Date().getTimezoneOffset() * 60 * 1000;
|
||||
var endDate = new Date(Math.floor((new Date().getTime() - offset) / sampleRate)
|
||||
@ -159,11 +178,11 @@ var steps = [
|
||||
var samples = (endDate.getTime() - startDate.getTime()) / sampleRate + 1;
|
||||
|
||||
// Launch request
|
||||
req = netStats.getNetworkStats({start: startDate, end: endDate, connectionType: type});
|
||||
req = netStats.getSamples(network, startDate, endDate);
|
||||
req.onsuccess = function () {
|
||||
ok(true, "Get system stats request ok");
|
||||
ok(req.result.manifestURL == null, "manifestURL should be null");
|
||||
ok(req.result.connectionType == type, "connectionTypes should be equals");
|
||||
ok(compareNetworks(req.result.network, network), "networks should be equals");
|
||||
ok(req.result.start.getTime() == startDate.getTime(), "starts should be equals");
|
||||
ok(req.result.end.getTime() == endDate.getTime(), "ends should be equals");
|
||||
var data = req.result.data;
|
||||
@ -173,123 +192,16 @@ var steps = [
|
||||
next();
|
||||
};
|
||||
req.onerror = function () {
|
||||
ok(false, "Get system stats for a connectionType failure!");
|
||||
ok(false, "Get stats failure!");
|
||||
}
|
||||
},
|
||||
function () {
|
||||
ok(true, "Get system stats for all connectionTypes and dates adapted to samplerate");
|
||||
ok(true, "Get system stats for a network and dates not adapted to samplerate");
|
||||
// Prepare get params
|
||||
var network = netStats.availableNetworks[0];
|
||||
var diff = 2;
|
||||
// Get samplerate in millis
|
||||
var sampleRate = netStats.sampleRate * 1000;
|
||||
// Get date with samplerate's precision
|
||||
var offset = new Date().getTimezoneOffset() * 60 * 1000;
|
||||
var endDate = new Date(Math.floor((new Date().getTime() - offset) / sampleRate)
|
||||
* sampleRate + offset);
|
||||
var startDate = new Date(endDate.getTime() - (sampleRate * diff));
|
||||
// Calculate the number of samples that should be returned based on the
|
||||
// the samplerate and including final and initial samples.
|
||||
var samples = (endDate.getTime() - startDate.getTime()) / sampleRate + 1;
|
||||
|
||||
// Launch request
|
||||
req = netStats.getNetworkStats({start: startDate, end: endDate});
|
||||
req.onsuccess = function () {
|
||||
ok(true, "Get stats request ok");
|
||||
ok(req.result.manifestURL == null, "manifestURL should be null");
|
||||
ok(req.result.connectionType == null, "connectionTypes should be null");
|
||||
ok(req.result.start.getTime() == startDate.getTime(), "starts should be equals");
|
||||
ok(req.result.end.getTime() == endDate.getTime(), "ends should be equals");
|
||||
var data = req.result.data;
|
||||
ok(Array.isArray(data) && data.length == samples,
|
||||
"data is an array of length " + samples);
|
||||
checkDataDates(data, startDate, endDate, sampleRate);
|
||||
next();
|
||||
};
|
||||
req.onerror = function () {
|
||||
ok(false, "Get system stats for all connectionTypes failure!");
|
||||
}
|
||||
},
|
||||
function () {
|
||||
ok(true, "Get app stats for a connectionType and dates adapted to samplerate");
|
||||
// Prepare get params
|
||||
var url = 'app://browser.gaiamobile.org/manifest.webapp';
|
||||
var type = netStats.connectionTypes[0];
|
||||
var diff = 2;
|
||||
// Get samplerate in millis
|
||||
var sampleRate = netStats.sampleRate * 1000;
|
||||
// Get date with samplerate's precision
|
||||
var offset = new Date().getTimezoneOffset() * 60 * 1000;
|
||||
var endDate = new Date(Math.floor((new Date().getTime() - offset) / sampleRate)
|
||||
* sampleRate + offset);
|
||||
var startDate = new Date(endDate.getTime() - (sampleRate * diff));
|
||||
// Calculate the number of samples that should be returned based on the
|
||||
// the samplerate and including final and initial samples.
|
||||
var samples = (endDate.getTime() - startDate.getTime()) / sampleRate + 1;
|
||||
|
||||
// Launch request
|
||||
req = netStats.getNetworkStats({start: startDate,
|
||||
end: endDate,
|
||||
connectionType: type,
|
||||
manifestURL: url});
|
||||
req.onsuccess = function () {
|
||||
ok(true, "Get app stats request ok");
|
||||
ok(req.result.manifestURL == url, "manifestURL should be equals");
|
||||
ok(req.result.connectionType == type, "connectionTypes should be equals");
|
||||
ok(req.result.start.getTime() == startDate.getTime(), "starts should be equals");
|
||||
ok(req.result.end.getTime() == endDate.getTime(), "ends should be equals");
|
||||
var data = req.result.data;
|
||||
ok(Array.isArray(data) && data.length == samples,
|
||||
"data is an array of length " + samples);
|
||||
checkDataDates(data, startDate, endDate, sampleRate);
|
||||
next();
|
||||
};
|
||||
req.onerror = function () {
|
||||
ok(false, "Get app stats for a connectionType failure!");
|
||||
}
|
||||
},
|
||||
function () {
|
||||
ok(true, "Get app stats for all connectionTypes and dates adapted to samplerate");
|
||||
// Prepare get params
|
||||
var url = 'app://browser.gaiamobile.org/manifest.webapp';
|
||||
var diff = 2;
|
||||
// Get samplerate in millis
|
||||
var sampleRate = netStats.sampleRate * 1000;
|
||||
// Get date with samplerate's precision
|
||||
var offset = new Date().getTimezoneOffset() * 60 * 1000;
|
||||
var endDate = new Date(Math.floor((new Date().getTime() - offset) / sampleRate)
|
||||
* sampleRate + offset);
|
||||
var startDate = new Date(endDate.getTime() - (sampleRate * diff));
|
||||
// Calculate the number of samples that should be returned based on the
|
||||
// the samplerate and including final and initial samples.
|
||||
var samples = (endDate.getTime() - startDate.getTime()) / sampleRate + 1;
|
||||
|
||||
// Launch request
|
||||
req = netStats.getNetworkStats({start: startDate,
|
||||
end: endDate,
|
||||
manifestURL: url});
|
||||
req.onsuccess = function () {
|
||||
ok(true, "Get app stats request ok");
|
||||
ok(req.result.manifestURL == url, "manifestURL should be equals");
|
||||
ok(req.result.connectionType == null, "connectionTypes should be null");
|
||||
ok(req.result.start.getTime() == startDate.getTime(), "starts should be equals");
|
||||
ok(req.result.end.getTime() == endDate.getTime(), "ends should be equals");
|
||||
var data = req.result.data;
|
||||
ok(Array.isArray(data) && data.length == samples,
|
||||
"data is an array of length " + samples);
|
||||
checkDataDates(data, startDate, endDate, sampleRate);
|
||||
next();
|
||||
};
|
||||
req.onerror = function () {
|
||||
ok(false, "Get app stats for all connectionTypes failure!");
|
||||
}
|
||||
},
|
||||
function () {
|
||||
ok(true, "Get system stats for a connectionType and dates not adapted to samplerate");
|
||||
// Prepare get params
|
||||
var type = netStats.connectionTypes[0];
|
||||
var diff = 2;
|
||||
// Get samplerate in millis
|
||||
var sampleRate = netStats.sampleRate * 1000;
|
||||
var sampleRate = netStats.sampleRate;
|
||||
var endDate = new Date();
|
||||
var startDate = new Date(endDate.getTime() - (sampleRate * diff));
|
||||
// Calculate the number of samples that should be returned based on the
|
||||
@ -299,44 +211,11 @@ var steps = [
|
||||
Math.floor(startDate.getTime() / (sampleRate)) * sampleRate) / sampleRate + 1;
|
||||
|
||||
// Launch request
|
||||
req = netStats.getNetworkStats({start: startDate, end: endDate, connectionType: type});
|
||||
req.onsuccess = function () {
|
||||
ok(true, "Get system stats request ok");
|
||||
ok(req.result.manifestURL == null, "manifestURL should be null");
|
||||
ok(req.result.connectionType == type, "connectionTypes should be equals");
|
||||
ok(req.result.start.getTime() == startDate.getTime(), "starts should be equals");
|
||||
ok(req.result.end.getTime() == endDate.getTime(), "ends should be equals");
|
||||
var data = req.result.data;
|
||||
ok(Array.isArray(data) && data.length == samples,
|
||||
"data is an array of length " + samples);
|
||||
checkDataDates(data, startDate, endDate, sampleRate);
|
||||
next();
|
||||
};
|
||||
req.onerror = function () {
|
||||
ok(false, "Get system stats for a connectionType failure!");
|
||||
}
|
||||
},
|
||||
function () {
|
||||
ok(true, "Get system stats for all connectionTypes and dates not adapted to samplerate");
|
||||
// Prepare get params
|
||||
var diff = 2;
|
||||
// Get samplerate in millis
|
||||
var sampleRate = netStats.sampleRate * 1000;
|
||||
// Get date with samplerate's precision
|
||||
var endDate = new Date();
|
||||
var startDate = new Date(endDate.getTime() - (sampleRate * diff));
|
||||
// Calculate the number of samples that should be returned based on the
|
||||
// the samplerate, including final and initial samples and taking into
|
||||
// account that these will be filtered according to precision.
|
||||
var samples = (Math.floor(endDate.getTime() / (sampleRate)) * sampleRate -
|
||||
Math.floor(startDate.getTime() / (sampleRate)) * sampleRate) / sampleRate + 1;
|
||||
|
||||
// Launch request
|
||||
req = netStats.getNetworkStats({start: startDate, end: endDate});
|
||||
req = netStats.getSamples(network, startDate, endDate);
|
||||
req.onsuccess = function () {
|
||||
ok(true, "Get stats request ok");
|
||||
ok(req.result.manifestURL == null, "manifestURL should be null");
|
||||
ok(req.result.connectionType == null, "connectionTypes should be null");
|
||||
ok(compareNetworks(req.result.network, network), "networks should be equals");
|
||||
ok(req.result.start.getTime() == startDate.getTime(), "starts should be equals");
|
||||
ok(req.result.end.getTime() == endDate.getTime(), "ends should be equals");
|
||||
var data = req.result.data;
|
||||
@ -346,7 +225,20 @@ var steps = [
|
||||
next();
|
||||
};
|
||||
req.onerror = function () {
|
||||
ok(false, "Get system stats for all connectionType failure!");
|
||||
ok(false, "Get stats failure!");
|
||||
}
|
||||
},
|
||||
function () {
|
||||
// Test clearStats
|
||||
var network = netStats.availableNetworks[0];
|
||||
|
||||
req = netStats.clearStats(network);
|
||||
req.onsuccess = function () {
|
||||
ok(true, "clearStats deleted the database");
|
||||
next();
|
||||
};
|
||||
req.onerror = function () {
|
||||
ok(false, "clearStats deleted the database");
|
||||
}
|
||||
},
|
||||
function () {
|
||||
|
@ -12,7 +12,7 @@
|
||||
<pre id="test">
|
||||
<script type="application/javascript">
|
||||
|
||||
// Test to ensure NetworkStats is enabled but mozNetworkStats.connectionTypes
|
||||
// Test to ensure NetworkStats is enabled but mozNetworkStats.availableNetworks
|
||||
// does not work in content.
|
||||
|
||||
SpecialPowers.setBoolPref("dom.mozNetworkStats.enabled", true);
|
||||
@ -22,12 +22,12 @@ ok('mozNetworkStats' in navigator, "navigator.mozNetworkStats should be accessib
|
||||
|
||||
var error;
|
||||
try {
|
||||
navigator.mozNetworkStats.connectionTypes;
|
||||
ok(false, "Accessing navigator.mozNetworkStats.connectionTypes should have thrown!");
|
||||
navigator.mozNetworkStats.availableNetworks;
|
||||
ok(false, "Accessing navigator.mozNetworkStats.availableNetworks should have thrown!");
|
||||
} catch (ex) {
|
||||
error = ex;
|
||||
}
|
||||
ok(error, "Got an exception accessing navigator.mozNetworkStats.connectionTypes");
|
||||
ok(error, "Got an exception accessing navigator.mozNetworkStats.availableNetworks");
|
||||
|
||||
</script>
|
||||
</pre>
|
||||
|
@ -5,7 +5,7 @@ const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
|
||||
|
||||
Cu.import("resource://gre/modules/NetworkStatsDB.jsm");
|
||||
|
||||
const netStatsDb = new NetworkStatsDB(this);
|
||||
const netStatsDb = new NetworkStatsDB();
|
||||
|
||||
function filterTimestamp(date) {
|
||||
var sampleRate = netStatsDb.sampleRate;
|
||||
@ -13,6 +13,15 @@ function filterTimestamp(date) {
|
||||
return Math.floor((date.getTime() - offset) / sampleRate) * sampleRate;
|
||||
}
|
||||
|
||||
function getNetworks() {
|
||||
return [{ id: '0', type: Ci.nsIDOMMozNetworkStatsManager.WIFI },
|
||||
{ id: '1234', type: Ci.nsIDOMMozNetworkStatsManager.MOBILE }];
|
||||
}
|
||||
|
||||
function compareNetworks(networkA, networkB) {
|
||||
return (networkA[0] == networkB[0] && networkA[1] == networkB[1]);
|
||||
}
|
||||
|
||||
add_test(function test_sampleRate() {
|
||||
var sampleRate = netStatsDb.sampleRate;
|
||||
do_check_true(sampleRate > 0);
|
||||
@ -89,20 +98,31 @@ add_test(function test_fillResultSamples_noEmptyData() {
|
||||
});
|
||||
|
||||
add_test(function test_clear() {
|
||||
netStatsDb.clear(function (error, result) {
|
||||
var networks = getNetworks();
|
||||
netStatsDb.clearStats(networks, function (error, result) {
|
||||
do_check_eq(error, null);
|
||||
run_next_test();
|
||||
});
|
||||
});
|
||||
|
||||
add_test(function test_clear_interface() {
|
||||
var networks = getNetworks();
|
||||
netStatsDb.clearInterfaceStats(networks[0], function (error, result) {
|
||||
do_check_eq(error, null);
|
||||
run_next_test();
|
||||
});
|
||||
});
|
||||
|
||||
add_test(function test_internalSaveStats_singleSample() {
|
||||
var stats = {appId: 0,
|
||||
connectionType: "wifi",
|
||||
timestamp: Date.now(),
|
||||
rxBytes: 0,
|
||||
txBytes: 0,
|
||||
rxTotalBytes: 1234,
|
||||
txTotalBytes: 1234};
|
||||
var networks = getNetworks();
|
||||
|
||||
var stats = { appId: 0,
|
||||
network: [networks[0].id, networks[0].type],
|
||||
timestamp: Date.now(),
|
||||
rxBytes: 0,
|
||||
txBytes: 0,
|
||||
rxTotalBytes: 1234,
|
||||
txTotalBytes: 1234 };
|
||||
|
||||
netStatsDb.dbNewTxn("readwrite", function(txn, store) {
|
||||
netStatsDb._saveStats(txn, store, stats);
|
||||
@ -113,7 +133,7 @@ add_test(function test_internalSaveStats_singleSample() {
|
||||
do_check_eq(error, null);
|
||||
do_check_eq(result.length, 1);
|
||||
do_check_eq(result[0].appId, stats.appId);
|
||||
do_check_eq(result[0].connectionType, stats.connectionType);
|
||||
do_check_true(compareNetworks(result[0].network, stats.network));
|
||||
do_check_eq(result[0].timestamp, stats.timestamp);
|
||||
do_check_eq(result[0].rxBytes, stats.rxBytes);
|
||||
do_check_eq(result[0].txBytes, stats.txBytes);
|
||||
@ -125,19 +145,23 @@ add_test(function test_internalSaveStats_singleSample() {
|
||||
});
|
||||
|
||||
add_test(function test_internalSaveStats_arraySamples() {
|
||||
netStatsDb.clear(function (error, result) {
|
||||
var networks = getNetworks();
|
||||
|
||||
netStatsDb.clearStats(networks, function (error, result) {
|
||||
do_check_eq(error, null);
|
||||
|
||||
var network = [networks[0].id, networks[0].type];
|
||||
|
||||
var samples = 2;
|
||||
var stats = [];
|
||||
for (var i = 0; i < samples; i++) {
|
||||
stats.push({appId: 0,
|
||||
connectionType: "wifi",
|
||||
timestamp: Date.now() + (10 * i),
|
||||
rxBytes: 0,
|
||||
txBytes: 0,
|
||||
rxTotalBytes: 1234,
|
||||
txTotalBytes: 1234});
|
||||
stats.push({ appId: 0,
|
||||
network: network,
|
||||
timestamp: Date.now() + (10 * i),
|
||||
rxBytes: 0,
|
||||
txBytes: 0,
|
||||
rxTotalBytes: 1234,
|
||||
txTotalBytes: 1234 });
|
||||
}
|
||||
|
||||
netStatsDb.dbNewTxn("readwrite", function(txn, store) {
|
||||
@ -147,12 +171,16 @@ add_test(function test_internalSaveStats_arraySamples() {
|
||||
|
||||
netStatsDb.logAllRecords(function(error, result) {
|
||||
do_check_eq(error, null);
|
||||
|
||||
// Result has one sample more than samples because clear inserts
|
||||
// an empty sample to keep totalBytes synchronized with netd counters
|
||||
result.shift();
|
||||
do_check_eq(result.length, samples);
|
||||
|
||||
var success = true;
|
||||
for (var i = 0; i < samples; i++) {
|
||||
for (var i = 1; i < samples; i++) {
|
||||
if (result[i].appId != stats[i].appId ||
|
||||
result[i].connectionType != stats[i].connectionType ||
|
||||
!compareNetworks(result[i].network, stats[i].network) ||
|
||||
result[i].timestamp != stats[i].timestamp ||
|
||||
result[i].rxBytes != stats[i].rxBytes ||
|
||||
result[i].txBytes != stats[i].txBytes ||
|
||||
@ -170,28 +198,31 @@ add_test(function test_internalSaveStats_arraySamples() {
|
||||
});
|
||||
|
||||
add_test(function test_internalRemoveOldStats() {
|
||||
netStatsDb.clear(function (error, result) {
|
||||
var networks = getNetworks();
|
||||
|
||||
netStatsDb.clearStats(networks, function (error, result) {
|
||||
do_check_eq(error, null);
|
||||
|
||||
var network = [networks[0].id, networks[0].type];
|
||||
var samples = 10;
|
||||
var stats = [];
|
||||
for (var i = 0; i < samples - 1; i++) {
|
||||
stats.push({appId: 0,
|
||||
connectionType: "wifi", timestamp: Date.now() + (10 * i),
|
||||
rxBytes: 0, txBytes: 0,
|
||||
rxTotalBytes: 1234, txTotalBytes: 1234});
|
||||
stats.push({ appId: 0,
|
||||
network: network, timestamp: Date.now() + (10 * i),
|
||||
rxBytes: 0, txBytes: 0,
|
||||
rxTotalBytes: 1234, txTotalBytes: 1234 });
|
||||
}
|
||||
|
||||
stats.push({appId: 0,
|
||||
connectionType: "wifi", timestamp: Date.now() + (10 * samples),
|
||||
rxBytes: 0, txBytes: 0,
|
||||
rxTotalBytes: 1234, txTotalBytes: 1234});
|
||||
stats.push({ appId: 0,
|
||||
network: network, 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
|
||||
var date = stats[stats.length - 1].timestamp
|
||||
+ (netStatsDb.sampleRate * netStatsDb.maxStorageSamples - 1) - 1;
|
||||
netStatsDb._removeOldStats(txn, store, 0, "wifi", date);
|
||||
netStatsDb._removeOldStats(txn, store, 0, network, date);
|
||||
}, function(error, result) {
|
||||
do_check_eq(error, null);
|
||||
|
||||
@ -205,14 +236,14 @@ add_test(function test_internalRemoveOldStats() {
|
||||
});
|
||||
});
|
||||
|
||||
function processSamplesDiff(lastStat, newStat, callback) {
|
||||
netStatsDb.clear(function (error, result){
|
||||
function processSamplesDiff(networks, lastStat, newStat, callback) {
|
||||
netStatsDb.clearStats(networks, 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");
|
||||
let request = store.index("network").openCursor(newStat.network, "prev");
|
||||
request.onsuccess = function onsuccess(event) {
|
||||
let cursor = event.target.result;
|
||||
do_check_neq(cursor, null);
|
||||
@ -230,22 +261,26 @@ function processSamplesDiff(lastStat, newStat, callback) {
|
||||
}
|
||||
|
||||
add_test(function test_processSamplesDiffSameSample() {
|
||||
var networks = getNetworks();
|
||||
var network = [networks[0].id, networks[0].type];
|
||||
|
||||
var sampleRate = netStatsDb.sampleRate;
|
||||
var date = filterTimestamp(new Date());
|
||||
var lastStat = {appId: 0,
|
||||
connectionType: "wifi", timestamp: date,
|
||||
rxBytes: 0, txBytes: 0,
|
||||
rxTotalBytes: 1234, txTotalBytes: 1234};
|
||||
|
||||
var newStat = {appId: 0,
|
||||
connectionType: "wifi", timestamp: date,
|
||||
rxBytes: 0, txBytes: 0,
|
||||
rxTotalBytes: 2234, txTotalBytes: 2234};
|
||||
var lastStat = { appId: 0,
|
||||
network: network, timestamp: date,
|
||||
rxBytes: 0, txBytes: 0,
|
||||
rxTotalBytes: 1234, txTotalBytes: 1234 };
|
||||
|
||||
processSamplesDiff(lastStat, newStat, function(result) {
|
||||
var newStat = { appId: 0,
|
||||
network: network, timestamp: date,
|
||||
rxBytes: 0, txBytes: 0,
|
||||
rxTotalBytes: 2234, txTotalBytes: 2234 };
|
||||
|
||||
processSamplesDiff(networks, lastStat, newStat, function(result) {
|
||||
do_check_eq(result.length, 1);
|
||||
do_check_eq(result[0].appId, newStat.appId);
|
||||
do_check_eq(result[0].connectionType, newStat.connectionType);
|
||||
do_check_true(compareNetworks(result[0].network, newStat.network));
|
||||
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);
|
||||
@ -256,22 +291,26 @@ add_test(function test_processSamplesDiffSameSample() {
|
||||
});
|
||||
|
||||
add_test(function test_processSamplesDiffNextSample() {
|
||||
var networks = getNetworks();
|
||||
var network = [networks[0].id, networks[0].type];
|
||||
|
||||
var sampleRate = netStatsDb.sampleRate;
|
||||
var date = filterTimestamp(new Date());
|
||||
var lastStat = {appId: 0,
|
||||
connectionType: "wifi", timestamp: date,
|
||||
rxBytes: 0, txBytes: 0,
|
||||
rxTotalBytes: 1234, txTotalBytes: 1234};
|
||||
|
||||
var newStat = {appId: 0,
|
||||
connectionType: "wifi", timestamp: date + sampleRate,
|
||||
rxBytes: 0, txBytes: 0,
|
||||
rxTotalBytes: 500, txTotalBytes: 500};
|
||||
var lastStat = { appId: 0,
|
||||
network: network, timestamp: date,
|
||||
rxBytes: 0, txBytes: 0,
|
||||
rxTotalBytes: 1234, txTotalBytes: 1234 };
|
||||
|
||||
processSamplesDiff(lastStat, newStat, function(result) {
|
||||
var newStat = { appId: 0,
|
||||
network: network, timestamp: date + sampleRate,
|
||||
rxBytes: 0, txBytes: 0,
|
||||
rxTotalBytes: 500, txTotalBytes: 500 };
|
||||
|
||||
processSamplesDiff(networks, lastStat, newStat, function(result) {
|
||||
do_check_eq(result.length, 2);
|
||||
do_check_eq(result[1].appId, newStat.appId);
|
||||
do_check_eq(result[1].connectionType, newStat.connectionType);
|
||||
do_check_true(compareNetworks(result[1].network, newStat.network));
|
||||
do_check_eq(result[1].timestamp, newStat.timestamp);
|
||||
do_check_eq(result[1].rxBytes, newStat.rxTotalBytes);
|
||||
do_check_eq(result[1].txBytes, newStat.txTotalBytes);
|
||||
@ -282,23 +321,25 @@ add_test(function test_processSamplesDiffNextSample() {
|
||||
});
|
||||
|
||||
add_test(function test_processSamplesDiffSamplesLost() {
|
||||
var networks = getNetworks();
|
||||
var network = [networks[0].id, networks[0].type];
|
||||
var samples = 5;
|
||||
var sampleRate = netStatsDb.sampleRate;
|
||||
var date = filterTimestamp(new Date());
|
||||
var lastStat = {appId: 0,
|
||||
connectionType: "wifi", timestamp: date,
|
||||
rxBytes: 0, txBytes: 0,
|
||||
rxTotalBytes: 1234, txTotalBytes: 1234};
|
||||
var lastStat = { appId: 0,
|
||||
network: network, timestamp: date,
|
||||
rxBytes: 0, txBytes: 0,
|
||||
rxTotalBytes: 1234, txTotalBytes: 1234 };
|
||||
|
||||
var newStat = {appId: 0,
|
||||
connectionType: "wifi", timestamp: date + (sampleRate * samples),
|
||||
rxBytes: 0, txBytes: 0,
|
||||
rxTotalBytes: 2234, txTotalBytes: 2234};
|
||||
var newStat = { appId: 0,
|
||||
network: network, timestamp: date + (sampleRate * samples),
|
||||
rxBytes: 0, txBytes: 0,
|
||||
rxTotalBytes: 2234, txTotalBytes: 2234 };
|
||||
|
||||
processSamplesDiff(lastStat, newStat, function(result) {
|
||||
processSamplesDiff(networks, lastStat, newStat, function(result) {
|
||||
do_check_eq(result.length, samples + 1);
|
||||
do_check_eq(result[0].appId, newStat.appId);
|
||||
do_check_eq(result[samples].connectionType, newStat.connectionType);
|
||||
do_check_true(compareNetworks(result[samples].network, newStat.network));
|
||||
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);
|
||||
@ -309,13 +350,17 @@ add_test(function test_processSamplesDiffSamplesLost() {
|
||||
});
|
||||
|
||||
add_test(function test_saveStats() {
|
||||
var stats = {appId: 0,
|
||||
connectionType: "wifi",
|
||||
date: new Date(),
|
||||
rxBytes: 2234,
|
||||
txBytes: 2234};
|
||||
var networks = getNetworks();
|
||||
var network = [networks[0].id, networks[0].type];
|
||||
|
||||
netStatsDb.clear(function (error, result) {
|
||||
var stats = { appId: 0,
|
||||
networkId: networks[0].id,
|
||||
networkType: networks[0].type,
|
||||
date: new Date(),
|
||||
rxBytes: 2234,
|
||||
txBytes: 2234};
|
||||
|
||||
netStatsDb.clearStats(networks, function (error, result) {
|
||||
do_check_eq(error, null);
|
||||
netStatsDb.saveStats(stats, function(error, result) {
|
||||
do_check_eq(error, null);
|
||||
@ -323,7 +368,7 @@ add_test(function test_saveStats() {
|
||||
do_check_eq(error, null);
|
||||
do_check_eq(result.length, 1);
|
||||
do_check_eq(result[0].appId, stats.appId);
|
||||
do_check_eq(result[0].connectionType, stats.connectionType);
|
||||
do_check_true(compareNetworks(result[0].network, network));
|
||||
let timestamp = filterTimestamp(stats.date);
|
||||
do_check_eq(result[0].timestamp, timestamp);
|
||||
do_check_eq(result[0].rxBytes, 0);
|
||||
@ -337,35 +382,44 @@ add_test(function test_saveStats() {
|
||||
});
|
||||
|
||||
add_test(function test_saveAppStats() {
|
||||
var stats = {appId: 1,
|
||||
connectionType: "wifi",
|
||||
date: new Date(),
|
||||
rxBytes: 2234,
|
||||
txBytes: 2234};
|
||||
var networks = getNetworks();
|
||||
var network = [networks[0].id, networks[0].type];
|
||||
|
||||
netStatsDb.clear(function (error, result) {
|
||||
var stats = { appId: 1,
|
||||
networkId: networks[0].id,
|
||||
networkType: networks[0].type,
|
||||
date: new Date(),
|
||||
rxBytes: 2234,
|
||||
txBytes: 2234};
|
||||
|
||||
netStatsDb.clearStats(networks, 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].appId, stats.appId);
|
||||
do_check_eq(result[0].connectionType, stats.connectionType);
|
||||
// The clear function clears all records of the datbase but
|
||||
// inserts a new element for each [appId, connectionId, connectionType]
|
||||
// record to keep the track of rxTotalBytes / txTotalBytes.
|
||||
// So at this point, we have two records, one for the appId 0 used in
|
||||
// past tests and the new one for appId 1
|
||||
do_check_eq(result.length, 2);
|
||||
do_check_eq(result[1].appId, stats.appId);
|
||||
do_check_true(compareNetworks(result[1].network, network));
|
||||
let timestamp = filterTimestamp(stats.date);
|
||||
do_check_eq(result[0].timestamp, timestamp);
|
||||
do_check_eq(result[0].rxBytes, stats.rxBytes);
|
||||
do_check_eq(result[0].txBytes, stats.txBytes);
|
||||
do_check_eq(result[0].rxTotalBytes, 0);
|
||||
do_check_eq(result[0].txTotalBytes, 0);
|
||||
do_check_eq(result[1].timestamp, timestamp);
|
||||
do_check_eq(result[1].rxBytes, stats.rxBytes);
|
||||
do_check_eq(result[1].txBytes, stats.txBytes);
|
||||
do_check_eq(result[1].rxTotalBytes, 0);
|
||||
do_check_eq(result[1].txTotalBytes, 0);
|
||||
run_next_test();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
function prepareFind(stats, callback) {
|
||||
netStatsDb.clear(function (error, result) {
|
||||
function prepareFind(network, stats, callback) {
|
||||
netStatsDb.clearStats(network, function (error, result) {
|
||||
do_check_eq(error, null);
|
||||
netStatsDb.dbNewTxn("readwrite", function(txn, store) {
|
||||
netStatsDb._saveStats(txn, store, stats);
|
||||
@ -376,6 +430,11 @@ function prepareFind(stats, callback) {
|
||||
}
|
||||
|
||||
add_test(function test_find () {
|
||||
var networks = getNetworks();
|
||||
var networkWifi = [networks[0].id, networks[0].type];
|
||||
var networkMobile = [networks[1].id, networks[1].type]; // Fake mobile interface
|
||||
var appId = 0;
|
||||
|
||||
var samples = 5;
|
||||
var sampleRate = netStatsDb.sampleRate;
|
||||
var start = Date.now();
|
||||
@ -384,46 +443,39 @@ add_test(function test_find () {
|
||||
start = new Date(start - sampleRate);
|
||||
var stats = [];
|
||||
for (var i = 0; i < samples; i++) {
|
||||
stats.push({appId: 0,
|
||||
connectionType: "wifi", timestamp: saveDate + (sampleRate * i),
|
||||
rxBytes: 0, txBytes: 10,
|
||||
rxTotalBytes: 0, txTotalBytes: 0});
|
||||
stats.push({ appId: appId,
|
||||
network: networkWifi, timestamp: saveDate + (sampleRate * i),
|
||||
rxBytes: 0, txBytes: 10,
|
||||
rxTotalBytes: 0, txTotalBytes: 0 });
|
||||
|
||||
stats.push({appId: 0,
|
||||
connectionType: "mobile", timestamp: saveDate + (sampleRate * i),
|
||||
rxBytes: 0, txBytes: 10,
|
||||
rxTotalBytes: 0, txTotalBytes: 0});
|
||||
stats.push({ appId: appId,
|
||||
network: networkMobile, timestamp: saveDate + (sampleRate * i),
|
||||
rxBytes: 0, txBytes: 10,
|
||||
rxTotalBytes: 0, txTotalBytes: 0 });
|
||||
}
|
||||
|
||||
prepareFind(stats, function(error, result) {
|
||||
prepareFind(networks[0], 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.network.id, networks[0].id);
|
||||
do_check_eq(result.network.type, networks[0].type);
|
||||
do_check_eq(result.start.getTime(), start.getTime());
|
||||
do_check_eq(result.end.getTime(), end.getTime());
|
||||
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.getTime());
|
||||
do_check_eq(result.end.getTime(), end.getTime());
|
||||
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();
|
||||
}, {appId: 0, start: start, end: end});
|
||||
}, {start: start, end: end, connectionType: "wifi", appId: 0});
|
||||
run_next_test();
|
||||
}, networks[0], start, end, appId);
|
||||
});
|
||||
});
|
||||
|
||||
add_test(function test_findAppStats () {
|
||||
var networks = getNetworks();
|
||||
var networkWifi = [networks[0].id, networks[0].type];
|
||||
var networkMobile = [networks[1].id, networks[1].type]; // Fake mobile interface
|
||||
|
||||
var samples = 5;
|
||||
var sampleRate = netStatsDb.sampleRate;
|
||||
var start = Date.now();
|
||||
@ -432,69 +484,63 @@ add_test(function test_findAppStats () {
|
||||
start = new Date(start - sampleRate);
|
||||
var stats = [];
|
||||
for (var i = 0; i < samples; i++) {
|
||||
stats.push({appId: 1,
|
||||
connectionType: "wifi", timestamp: saveDate + (sampleRate * i),
|
||||
rxBytes: 0, txBytes: 10,
|
||||
rxTotalBytes: 0, txTotalBytes: 0});
|
||||
stats.push({ appId: 1,
|
||||
network: networkWifi, timestamp: saveDate + (sampleRate * i),
|
||||
rxBytes: 0, txBytes: 10,
|
||||
rxTotalBytes: 0, txTotalBytes: 0 });
|
||||
|
||||
stats.push({appId: 1,
|
||||
connectionType: "mobile", timestamp: saveDate + (sampleRate * i),
|
||||
rxBytes: 0, txBytes: 10,
|
||||
rxTotalBytes: 0, txTotalBytes: 0});
|
||||
stats.push({ appId: 1,
|
||||
network: networkMobile, timestamp: saveDate + (sampleRate * i),
|
||||
rxBytes: 0, txBytes: 10,
|
||||
rxTotalBytes: 0, txTotalBytes: 0 });
|
||||
}
|
||||
|
||||
prepareFind(stats, function(error, result) {
|
||||
prepareFind(networks[0], 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.network.id, networks[0].id);
|
||||
do_check_eq(result.network.type, networks[0].type);
|
||||
do_check_eq(result.start.getTime(), start.getTime());
|
||||
do_check_eq(result.end.getTime(), end.getTime());
|
||||
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.getTime());
|
||||
do_check_eq(result.end.getTime(), end.getTime());
|
||||
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, appId: 1});
|
||||
}, {start: start, end: end, connectionType: "wifi", appId: 1});
|
||||
run_next_test();
|
||||
}, networks[0], start, end, 1);
|
||||
});
|
||||
});
|
||||
|
||||
add_test(function test_saveMultipleAppStats () {
|
||||
var networks = getNetworks();
|
||||
var networkWifi = networks[0];
|
||||
var networkMobile = networks[1]; // Fake mobile interface
|
||||
|
||||
var saveDate = filterTimestamp(new Date());
|
||||
var cached = Object.create(null);
|
||||
|
||||
cached['1wifi'] = {appId: 1,
|
||||
connectionType: "wifi", date: new Date(),
|
||||
rxBytes: 0, txBytes: 10};
|
||||
cached['1wifi'] = { appId: 1, date: new Date(),
|
||||
networkId: networkWifi.id, networkType: networkWifi.type,
|
||||
rxBytes: 0, txBytes: 10 };
|
||||
|
||||
cached['1mobile'] = {appId: 1,
|
||||
connectionType: "mobile", date: new Date(),
|
||||
rxBytes: 0, txBytes: 10};
|
||||
cached['1mobile'] = { appId: 1, date: new Date(),
|
||||
networkId: networkMobile.id, networkType: networkMobile.type,
|
||||
rxBytes: 0, txBytes: 10 };
|
||||
|
||||
cached['2wifi'] = {appId: 2,
|
||||
connectionType: "wifi", date: new Date(),
|
||||
rxBytes: 0, txBytes: 10};
|
||||
cached['2wifi'] = { appId: 2, date: new Date(),
|
||||
networkId: networkWifi.id, networkType: networkWifi.type,
|
||||
rxBytes: 0, txBytes: 10 };
|
||||
|
||||
cached['2mobile'] = {appId: 2,
|
||||
connectionType: "mobile", date: new Date(),
|
||||
rxBytes: 0, txBytes: 10};
|
||||
cached['2mobile'] = { appId: 2, date: new Date(),
|
||||
networkId: networkMobile.id, networkType: networkMobile.type,
|
||||
rxBytes: 0, txBytes: 10 };
|
||||
|
||||
let keys = Object.keys(cached);
|
||||
let index = 0;
|
||||
|
||||
netStatsDb.clear(function (error, result) {
|
||||
networks.push(networkMobile);
|
||||
netStatsDb.clearStats(networks, function (error, result) {
|
||||
do_check_eq(error, null);
|
||||
netStatsDb.saveStats(cached[keys[index]],
|
||||
function callback(error, result) {
|
||||
@ -502,10 +548,17 @@ add_test(function test_saveMultipleAppStats () {
|
||||
|
||||
if (index == keys.length - 1) {
|
||||
netStatsDb.logAllRecords(function(error, result) {
|
||||
// Again, result has two samples more than expected samples because
|
||||
// clear inserts one empty sample for each network to keep totalBytes
|
||||
// synchronized with netd counters. so the first two samples have to
|
||||
// be discarted.
|
||||
result.shift();
|
||||
result.shift();
|
||||
|
||||
do_check_eq(error, null);
|
||||
do_check_eq(result.length, 4);
|
||||
do_check_eq(result[0].appId, 1);
|
||||
do_check_eq(result[0].connectionType, 'mobile');
|
||||
do_check_true(compareNetworks(result[0].network,[networkWifi.id, networkWifi.type]));
|
||||
do_check_eq(result[0].rxBytes, 0);
|
||||
do_check_eq(result[0].txBytes, 10);
|
||||
run_next_test();
|
||||
|
@ -4,48 +4,56 @@
|
||||
const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
|
||||
|
||||
add_test(function test_clearDB() {
|
||||
NetworkStatsService._db.clear(function onDBCleared(error, result) {
|
||||
var networks = NetworkStatsService.availableNetworks();
|
||||
NetworkStatsService._db.clearStats(networks, function onDBCleared(error, result) {
|
||||
do_check_eq(result, null);
|
||||
run_next_test();
|
||||
});
|
||||
});
|
||||
|
||||
function getNetworkId() {
|
||||
var network = (NetworkStatsService.availableNetworks())[0];
|
||||
return NetworkStatsService.getNetworkId(network.id, network.type);
|
||||
}
|
||||
|
||||
add_test(function test_networkStatsAvailable_ok() {
|
||||
var netId = getNetworkId();
|
||||
NetworkStatsService.networkStatsAvailable(function (success, msg) {
|
||||
do_check_eq(success, true);
|
||||
run_next_test();
|
||||
}, Ci.nsINetworkInterface.NETWORK_TYPE_WIFI, true, 1234, 4321, new Date());
|
||||
}, netId, true, 1234, 4321, new Date());
|
||||
});
|
||||
|
||||
add_test(function test_networkStatsAvailable_failure() {
|
||||
var netId = getNetworkId();
|
||||
NetworkStatsService.networkStatsAvailable(function (success, msg) {
|
||||
do_check_eq(success, false);
|
||||
run_next_test();
|
||||
}, Ci.nsINetworkInterface.NETWORK_TYPE_WIFI, false, 1234, 4321, new Date());
|
||||
}, netId, false, 1234, 4321, new Date());
|
||||
});
|
||||
|
||||
add_test(function test_update_invalidConnection() {
|
||||
add_test(function test_update_invalidNetwork() {
|
||||
NetworkStatsService.update(-1, function (success, msg) {
|
||||
do_check_eq(success, false);
|
||||
do_check_eq(msg, "Invalid network type -1");
|
||||
do_check_eq(msg, "Invalid network -1");
|
||||
run_next_test();
|
||||
});
|
||||
});
|
||||
|
||||
add_test(function test_update() {
|
||||
NetworkStatsService.update(Ci.nsINetworkInterface.NETWORK_TYPE_WIFI, function (success, msg) {
|
||||
var netId = getNetworkId();
|
||||
NetworkStatsService.update(netId, 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}];
|
||||
NetworkStatsService.updateQueue = [{netId: 0, callbacks: null},
|
||||
{netId: 1, callbacks: null},
|
||||
{netId: 2, callbacks: null},
|
||||
{netId: 3, callbacks: null},
|
||||
{netId: 4, callbacks: null}];
|
||||
var index = NetworkStatsService.updateQueueIndex(3);
|
||||
do_check_eq(index, 3);
|
||||
index = NetworkStatsService.updateQueueIndex(10);
|
||||
@ -63,7 +71,8 @@ add_test(function test_updateAllStats() {
|
||||
});
|
||||
|
||||
add_test(function test_updateStats_ok() {
|
||||
NetworkStatsService.updateStats(Ci.nsINetworkInterface.NETWORK_TYPE_WIFI, function(success, msg){
|
||||
var netId = getNetworkId();
|
||||
NetworkStatsService.updateStats(netId, function(success, msg){
|
||||
do_check_eq(success, true);
|
||||
run_next_test();
|
||||
});
|
||||
@ -77,15 +86,20 @@ add_test(function test_updateStats_failure() {
|
||||
});
|
||||
|
||||
add_test(function test_queue() {
|
||||
// Fill connections with fake network interfaces (wlan0 and rmnet0)
|
||||
// Fill networks with fake network interfaces
|
||||
// to enable netd async requests
|
||||
NetworkStatsService._connectionTypes[Ci.nsINetworkInterface.NETWORK_TYPE_WIFI]
|
||||
.network.name = 'wlan0';
|
||||
NetworkStatsService._connectionTypes[Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE]
|
||||
.network.name = 'rmnet0';
|
||||
var network = {id: "1234", type: Ci.nsIDOMMozNetworkStatsManager.MOBILE};
|
||||
var netId1 = NetworkStatsService.getNetworkId(network.id, network.type);
|
||||
NetworkStatsService._networks[netId1] = { network: network,
|
||||
interfaceName: "net1" };
|
||||
|
||||
NetworkStatsService.updateStats(Ci.nsINetworkInterface.NETWORK_TYPE_WIFI);
|
||||
NetworkStatsService.updateStats(Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE);
|
||||
network = {id: "5678", type: Ci.nsIDOMMozNetworkStatsManager.MOBILE};
|
||||
var netId2 = NetworkStatsService.getNetworkId(network.id, network.type);
|
||||
NetworkStatsService._networks[netId2] = { network: network,
|
||||
interfaceName: "net2" };
|
||||
|
||||
NetworkStatsService.updateStats(netId1);
|
||||
NetworkStatsService.updateStats(netId2);
|
||||
do_check_eq(NetworkStatsService.updateQueue.length, 2);
|
||||
do_check_eq(NetworkStatsService.updateQueue[0].callbacks.length, 1);
|
||||
|
||||
@ -93,8 +107,8 @@ add_test(function test_queue() {
|
||||
return;
|
||||
};
|
||||
|
||||
NetworkStatsService.updateStats(Ci.nsINetworkInterface.NETWORK_TYPE_WIFI, callback);
|
||||
NetworkStatsService.updateStats(Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE, callback);
|
||||
NetworkStatsService.updateStats(netId1, callback);
|
||||
NetworkStatsService.updateStats(netId2, callback);
|
||||
|
||||
do_check_eq(NetworkStatsService.updateQueue.length, 2);
|
||||
do_check_eq(NetworkStatsService.updateQueue[0].callbacks.length, 2);
|
||||
|
@ -9,33 +9,66 @@ XPCOMUtils.defineLazyServiceGetter(this, "nssProxy",
|
||||
"@mozilla.org/networkstatsServiceProxy;1",
|
||||
"nsINetworkStatsServiceProxy");
|
||||
|
||||
function mokConvertNetworkInterface() {
|
||||
NetworkStatsService.convertNetworkInterface = function(aNetwork) {
|
||||
if (aNetwork.type != Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE &&
|
||||
aNetwork.type != Ci.nsINetworkInterface.NETWORK_TYPE_WIFI) {
|
||||
return null;
|
||||
}
|
||||
|
||||
let id = '0';
|
||||
if (aNetwork.type == Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE) {
|
||||
id = '1234'
|
||||
}
|
||||
|
||||
let netId = this.getNetworkId(id, aNetwork.type);
|
||||
|
||||
if (!this._networks[netId]) {
|
||||
this._networks[netId] = Object.create(null);
|
||||
this._networks[netId].network = { id: id,
|
||||
type: aNetwork.type };
|
||||
}
|
||||
|
||||
return netId;
|
||||
};
|
||||
}
|
||||
|
||||
add_test(function test_saveAppStats() {
|
||||
var cachedAppStats = NetworkStatsService.cachedAppStats;
|
||||
var timestamp = NetworkStatsService.cachedAppStatsDate.getTime();
|
||||
var samples = 5;
|
||||
|
||||
// Create to fake nsINetworkInterfaces. As nsINetworkInterface can not
|
||||
// be instantiated, these two vars will emulate it by filling the properties
|
||||
// that will be used.
|
||||
var wifi = {type: Ci.nsINetworkInterface.NETWORK_TYPE_WIFI, id: "0"};
|
||||
var mobile = {type: Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE, id: "1234"};
|
||||
|
||||
// Insert fake mobile network interface in NetworkStatsService
|
||||
var mobileNetId = NetworkStatsService.getNetworkId(mobile.id, mobile.type);
|
||||
|
||||
do_check_eq(Object.keys(cachedAppStats).length, 0);
|
||||
|
||||
for (var i = 0; i < samples; i++) {
|
||||
nssProxy.saveAppStats(1, Ci.nsINetworkInterface.NETWORK_TYPE_WIFI,
|
||||
timestamp, 10, 20);
|
||||
nssProxy.saveAppStats(1, wifi, timestamp, 10, 20);
|
||||
|
||||
nssProxy.saveAppStats(1, Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE,
|
||||
timestamp, 10, 20);
|
||||
nssProxy.saveAppStats(1, mobile, timestamp, 10, 20);
|
||||
}
|
||||
|
||||
var key1 = 1 + 'wifi';
|
||||
var key2 = 1 + 'mobile';
|
||||
var key1 = 1 + NetworkStatsService.getNetworkId(wifi.id, wifi.type);
|
||||
var key2 = 1 + mobileNetId;
|
||||
|
||||
do_check_eq(Object.keys(cachedAppStats).length, 2);
|
||||
do_check_eq(cachedAppStats[key1].appId, 1);
|
||||
do_check_eq(cachedAppStats[key1].connectionType, 'wifi');
|
||||
do_check_eq(cachedAppStats[key1].networkId, wifi.id);
|
||||
do_check_eq(cachedAppStats[key1].networkType, wifi.type);
|
||||
do_check_eq(new Date(cachedAppStats[key1].date).getTime() / 1000,
|
||||
Math.floor(timestamp / 1000));
|
||||
do_check_eq(cachedAppStats[key1].rxBytes, 50);
|
||||
do_check_eq(cachedAppStats[key1].txBytes, 100);
|
||||
do_check_eq(cachedAppStats[key2].appId, 1);
|
||||
do_check_eq(cachedAppStats[key2].connectionType, 'mobile');
|
||||
do_check_eq(cachedAppStats[key2].networkId, mobile.id);
|
||||
do_check_eq(cachedAppStats[key2].networkType, mobile.type);
|
||||
do_check_eq(new Date(cachedAppStats[key2].date).getTime() / 1000,
|
||||
Math.floor(timestamp / 1000));
|
||||
do_check_eq(cachedAppStats[key2].rxBytes, 50);
|
||||
@ -47,7 +80,11 @@ add_test(function test_saveAppStats() {
|
||||
add_test(function test_saveAppStatsWithDifferentDates() {
|
||||
var today = NetworkStatsService.cachedAppStatsDate;
|
||||
var tomorrow = new Date(today.getTime() + (24 * 60 * 60 * 1000));
|
||||
var key = 1 + 'wifi';
|
||||
|
||||
var wifi = {type: Ci.nsINetworkInterface.NETWORK_TYPE_WIFI, id: "0"};
|
||||
var mobile = {type: Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE, id: "1234"};
|
||||
|
||||
var key = 1 + NetworkStatsService.getNetworkId(wifi.id, wifi.type);
|
||||
|
||||
NetworkStatsService.updateCachedAppStats(
|
||||
function (success, msg) {
|
||||
@ -55,21 +92,20 @@ add_test(function test_saveAppStatsWithDifferentDates() {
|
||||
|
||||
do_check_eq(Object.keys(NetworkStatsService.cachedAppStats).length, 0);
|
||||
|
||||
nssProxy.saveAppStats(1, Ci.nsINetworkInterface.NETWORK_TYPE_WIFI,
|
||||
today.getTime(), 10, 20);
|
||||
nssProxy.saveAppStats(1, wifi, today.getTime(), 10, 20);
|
||||
|
||||
nssProxy.saveAppStats(1, Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE,
|
||||
today.getTime(), 10, 20);
|
||||
nssProxy.saveAppStats(1, mobile, today.getTime(), 10, 20);
|
||||
|
||||
var saveAppStatsCb = {
|
||||
notify: function notify(success, message) {
|
||||
do_check_eq(success, true);
|
||||
|
||||
var cachedAppStats = NetworkStatsService.cachedAppStats;
|
||||
var key = 2 + 'mobile';
|
||||
var key = 2 + NetworkStatsService.getNetworkId(mobile.id, mobile.type);
|
||||
do_check_eq(Object.keys(cachedAppStats).length, 1);
|
||||
do_check_eq(cachedAppStats[key].appId, 2);
|
||||
do_check_eq(cachedAppStats[key].connectionType, 'mobile');
|
||||
do_check_eq(cachedAppStats[key].networkId, mobile.id);
|
||||
do_check_eq(cachedAppStats[key].networkType, mobile.type);
|
||||
do_check_eq(new Date(cachedAppStats[key].date).getTime() / 1000,
|
||||
Math.floor(tomorrow.getTime() / 1000));
|
||||
do_check_eq(cachedAppStats[key].rxBytes, 30);
|
||||
@ -79,8 +115,7 @@ add_test(function test_saveAppStatsWithDifferentDates() {
|
||||
}
|
||||
};
|
||||
|
||||
nssProxy.saveAppStats(2, Ci.nsINetworkInterface.NETWORK_TYPE_MOBILE,
|
||||
tomorrow.getTime(), 30, 40, saveAppStatsCb);
|
||||
nssProxy.saveAppStats(2, mobile, tomorrow.getTime(), 30, 40, saveAppStatsCb);
|
||||
}
|
||||
);
|
||||
});
|
||||
@ -88,6 +123,7 @@ add_test(function test_saveAppStatsWithDifferentDates() {
|
||||
add_test(function test_saveAppStatsWithMaxCachedTraffic() {
|
||||
var timestamp = NetworkStatsService.cachedAppStatsDate.getTime();
|
||||
var maxtraffic = NetworkStatsService.maxCachedTraffic;
|
||||
var wifi = {type: Ci.nsINetworkInterface.NETWORK_TYPE_WIFI, id: "0"};
|
||||
|
||||
NetworkStatsService.updateCachedAppStats(
|
||||
function (success, msg) {
|
||||
@ -96,13 +132,11 @@ add_test(function test_saveAppStatsWithMaxCachedTraffic() {
|
||||
var cachedAppStats = NetworkStatsService.cachedAppStats;
|
||||
do_check_eq(Object.keys(cachedAppStats).length, 0);
|
||||
|
||||
nssProxy.saveAppStats(1, Ci.nsINetworkInterface.NETWORK_TYPE_WIFI,
|
||||
timestamp, 10, 20);
|
||||
nssProxy.saveAppStats(1, wifi, timestamp, 10, 20);
|
||||
|
||||
do_check_eq(Object.keys(cachedAppStats).length, 1);
|
||||
|
||||
nssProxy.saveAppStats(1, Ci.nsINetworkInterface.NETWORK_TYPE_WIFI,
|
||||
timestamp, maxtraffic, 20);
|
||||
nssProxy.saveAppStats(1, wifi, timestamp, maxtraffic, 20);
|
||||
|
||||
do_check_eq(Object.keys(cachedAppStats).length, 0);
|
||||
|
||||
@ -115,5 +149,9 @@ function run_test() {
|
||||
|
||||
Cu.import("resource://gre/modules/NetworkStatsService.jsm");
|
||||
|
||||
// Function convertNetworkInterface of NetworkStatsService causes errors when dealing
|
||||
// with RIL to get the iccid, so overwrite it.
|
||||
mokConvertNetworkInterface();
|
||||
|
||||
run_next_test();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user