Bug 922926 - Part 4: Tests. r=gene, r=albert

This commit is contained in:
John Shih 2013-11-22 11:51:11 +08:00
parent c7c4b1e5c3
commit c14e842504
2 changed files with 291 additions and 100 deletions

View File

@ -22,7 +22,7 @@ function clearStore(store, callback) {
add_test(function prepareDatabase() {
// Clear whole database to avoid starting tests with unknown state
// due to the previous tests.
clearStore('net_stats', function() {
clearStore('net_stats_store', function() {
clearStore('net_alarm', function() {
run_next_test();
});
@ -139,6 +139,7 @@ add_test(function test_internalSaveStats_singleSample() {
var networks = getNetworks();
var stats = { appId: 0,
serviceType: "",
network: [networks[0].id, networks[0].type],
timestamp: Date.now(),
rxBytes: 0,
@ -148,7 +149,7 @@ add_test(function test_internalSaveStats_singleSample() {
rxTotalBytes: 1234,
txTotalBytes: 1234 };
netStatsDb.dbNewTxn("net_stats", "readwrite", function(txn, store) {
netStatsDb.dbNewTxn("net_stats_store", "readwrite", function(txn, store) {
netStatsDb._saveStats(txn, store, stats);
}, function(error, result) {
do_check_eq(error, null);
@ -157,6 +158,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].serviceType, stats.serviceType);
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);
@ -182,6 +184,7 @@ add_test(function test_internalSaveStats_arraySamples() {
var stats = [];
for (var i = 0; i < samples; i++) {
stats.push({ appId: 0,
serviceType: "",
network: network,
timestamp: Date.now() + (10 * i),
rxBytes: 0,
@ -192,7 +195,7 @@ add_test(function test_internalSaveStats_arraySamples() {
txTotalBytes: 1234 });
}
netStatsDb.dbNewTxn("net_stats", "readwrite", function(txn, store) {
netStatsDb.dbNewTxn("net_stats_store", "readwrite", function(txn, store) {
netStatsDb._saveStats(txn, store, stats);
}, function(error, result) {
do_check_eq(error, null);
@ -207,6 +210,7 @@ add_test(function test_internalSaveStats_arraySamples() {
var success = true;
for (var i = 1; i < samples; i++) {
if (result[i].appId != stats[i].appId ||
result[i].serviceType != stats[i].serviceType ||
!compareNetworks(result[i].network, stats[i].network) ||
result[i].timestamp != stats[i].timestamp ||
result[i].rxBytes != stats[i].rxBytes ||
@ -236,24 +240,24 @@ add_test(function test_internalRemoveOldStats() {
var samples = 10;
var stats = [];
for (var i = 0; i < samples - 1; i++) {
stats.push({ appId: 0,
stats.push({ appId: 0, serviceType: "",
network: network, timestamp: Date.now() + (10 * i),
rxBytes: 0, txBytes: 0,
rxSystemBytes: 1234, txSystemBytes: 1234,
rxTotalBytes: 1234, txTotalBytes: 1234 });
}
stats.push({ appId: 0,
stats.push({ appId: 0, serviceType: "",
network: network, timestamp: Date.now() + (10 * samples),
rxBytes: 0, txBytes: 0,
rxSystemBytes: 1234, txSystemBytes: 1234,
rxTotalBytes: 1234, txTotalBytes: 1234 });
netStatsDb.dbNewTxn("net_stats", "readwrite", function(txn, store) {
netStatsDb.dbNewTxn("net_stats_store", "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, 0, network, date);
netStatsDb._removeOldStats(txn, store, 0, "", network, date);
}, function(error, result) {
do_check_eq(error, null);
@ -270,15 +274,15 @@ add_test(function test_internalRemoveOldStats() {
function processSamplesDiff(networks, lastStat, newStat, callback) {
netStatsDb.clearStats(networks, function (error, result){
do_check_eq(error, null);
netStatsDb.dbNewTxn("net_stats", "readwrite", function(txn, store) {
netStatsDb.dbNewTxn("net_stats_store", "readwrite", function(txn, store) {
netStatsDb._saveStats(txn, store, lastStat);
}, function(error, result) {
netStatsDb.dbNewTxn("net_stats", "readwrite", function(txn, store) {
netStatsDb.dbNewTxn("net_stats_store", "readwrite", function(txn, store) {
let request = store.index("network").openCursor(newStat.network, "prev");
request.onsuccess = function onsuccess(event) {
let cursor = event.target.result;
do_check_neq(cursor, null);
netStatsDb._processSamplesDiff(txn, store, cursor, newStat);
netStatsDb._processSamplesDiff(txn, store, cursor, newStat, true);
};
}, function(error, result) {
do_check_eq(error, null);
@ -298,13 +302,13 @@ add_test(function test_processSamplesDiffSameSample() {
var sampleRate = netStatsDb.sampleRate;
var date = filterTimestamp(new Date());
var lastStat = { appId: 0,
var lastStat = { appId: 0, serviceType: "",
network: network, timestamp: date,
rxBytes: 0, txBytes: 0,
rxSystemBytes: 1234, txSystemBytes: 1234,
rxTotalBytes: 2234, txTotalBytes: 2234 };
var newStat = { appId: 0,
var newStat = { appId: 0, serviceType: "",
network: network, timestamp: date,
rxBytes: 0, txBytes: 0,
rxSystemBytes: 2234, txSystemBytes: 2234,
@ -313,6 +317,7 @@ add_test(function test_processSamplesDiffSameSample() {
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].serviceType, newStat.serviceType);
do_check_true(compareNetworks(result[0].network, newStat.network));
do_check_eq(result[0].timestamp, newStat.timestamp);
do_check_eq(result[0].rxBytes, newStat.rxSystemBytes - lastStat.rxSystemBytes);
@ -332,13 +337,13 @@ add_test(function test_processSamplesDiffNextSample() {
var sampleRate = netStatsDb.sampleRate;
var date = filterTimestamp(new Date());
var lastStat = { appId: 0,
var lastStat = { appId: 0, serviceType: "",
network: network, timestamp: date,
rxBytes: 0, txBytes: 0,
rxSystemBytes: 1234, txSystemBytes: 1234,
rxTotalBytes: 2234, txTotalBytes: 2234 };
var newStat = { appId: 0,
var newStat = { appId: 0, serviceType: "",
network: network, timestamp: date + sampleRate,
rxBytes: 0, txBytes: 0,
rxSystemBytes: 1734, txSystemBytes: 1734,
@ -347,6 +352,7 @@ add_test(function test_processSamplesDiffNextSample() {
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].serviceType, newStat.serviceType);
do_check_true(compareNetworks(result[1].network, newStat.network));
do_check_eq(result[1].timestamp, newStat.timestamp);
do_check_eq(result[1].rxBytes, newStat.rxSystemBytes - lastStat.rxSystemBytes);
@ -365,13 +371,13 @@ add_test(function test_processSamplesDiffSamplesLost() {
var samples = 5;
var sampleRate = netStatsDb.sampleRate;
var date = filterTimestamp(new Date());
var lastStat = { appId: 0,
var lastStat = { appId: 0, serviceType: "",
network: network, timestamp: date,
rxBytes: 0, txBytes: 0,
rxSystemBytes: 1234, txSystemBytes: 1234,
rxTotalBytes: 2234, txTotalBytes: 2234};
var newStat = { appId: 0,
var newStat = { appId: 0, serviceType: "",
network: network, timestamp: date + (sampleRate * samples),
rxBytes: 0, txBytes: 0,
rxSystemBytes: 2234, txSystemBytes: 2234,
@ -380,6 +386,7 @@ add_test(function test_processSamplesDiffSamplesLost() {
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[0].serviceType, newStat.serviceType);
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);
@ -396,20 +403,23 @@ add_test(function test_saveStats() {
var networks = getNetworks();
var network = [networks[0].id, networks[0].type];
var stats = { appId: 0,
networkId: networks[0].id,
networkType: networks[0].type,
date: new Date(),
rxBytes: 2234,
txBytes: 2234};
var stats = { appId: 0,
serviceType: "",
networkId: networks[0].id,
networkType: networks[0].type,
date: new Date(),
rxBytes: 2234,
txBytes: 2234,
isAccumulative: true };
clearStore('net_stats', function() {
clearStore('net_stats_store', function() {
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].serviceType, stats.serviceType);
do_check_true(compareNetworks(result[0].network, network));
let timestamp = filterTimestamp(stats.date);
do_check_eq(result[0].timestamp, timestamp);
@ -429,12 +439,14 @@ add_test(function test_saveAppStats() {
var networks = getNetworks();
var network = [networks[0].id, networks[0].type];
var stats = { appId: 1,
networkId: networks[0].id,
networkType: networks[0].type,
date: new Date(),
rxBytes: 2234,
txBytes: 2234};
var stats = { appId: 1,
serviceType: "",
networkId: networks[0].id,
networkType: networks[0].type,
date: new Date(),
rxBytes: 2234,
txBytes: 2234,
isAccumulative: false };
netStatsDb.clearStats(networks, function (error, result) {
do_check_eq(error, null);
@ -449,6 +461,47 @@ add_test(function test_saveAppStats() {
// 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_eq(result[1].serviceType, stats.serviceType);
do_check_true(compareNetworks(result[1].network, network));
let timestamp = filterTimestamp(stats.date);
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].rxSystemBytes, 0);
do_check_eq(result[1].txSystemBytes, 0);
do_check_eq(result[1].rxTotalBytes, 0);
do_check_eq(result[1].txTotalBytes, 0);
run_next_test();
});
});
});
});
add_test(function test_saveServiceStats() {
var networks = getNetworks();
var network = [networks[0].id, networks[0].type];
var stats = { appId: 0,
serviceType: "FakeType",
networkId: networks[0].id,
networkType: networks[0].type,
date: new Date(),
rxBytes: 2234,
txBytes: 2234,
isAccumulative: false };
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);
// Again, at this point, we have two records, one for the appId 0 and
// empty serviceType used in past tests and the new one for appId 0 and
// non-empty serviceType.
do_check_eq(result.length, 2);
do_check_eq(result[1].appId, stats.appId);
do_check_eq(result[1].serviceType, stats.serviceType);
do_check_true(compareNetworks(result[1].network, network));
let timestamp = filterTimestamp(stats.date);
do_check_eq(result[1].timestamp, timestamp);
@ -467,7 +520,7 @@ add_test(function test_saveAppStats() {
function prepareFind(network, stats, callback) {
netStatsDb.clearStats(network, function (error, result) {
do_check_eq(error, null);
netStatsDb.dbNewTxn("net_stats", "readwrite", function(txn, store) {
netStatsDb.dbNewTxn("net_stats_store", "readwrite", function(txn, store) {
netStatsDb._saveStats(txn, store, stats);
}, function(error, result) {
callback(error, result);
@ -480,6 +533,7 @@ add_test(function test_find () {
var networkWifi = [networks[0].id, networks[0].type];
var networkMobile = [networks[1].id, networks[1].type]; // Fake mobile interface
var appId = 0;
var serviceType = "";
var samples = 5;
var sampleRate = netStatsDb.sampleRate;
@ -489,24 +543,25 @@ add_test(function test_find () {
start = new Date(start - sampleRate);
var stats = [];
for (var i = 0; i < samples; i++) {
stats.push({ appId: appId,
stats.push({ appId: appId, serviceType: serviceType,
network: networkWifi, timestamp: saveDate + (sampleRate * i),
rxBytes: 0, txBytes: 10,
rxSystemBytes: 0, txSystemBytes: 0,
rxTotalBytes: 0, txTotalBytes: 0});
rxTotalBytes: 0, txTotalBytes: 0 });
stats.push({ appId: appId,
stats.push({ appId: appId, serviceType: serviceType,
network: networkMobile, timestamp: saveDate + (sampleRate * i),
rxBytes: 0, txBytes: 10,
rxSystemBytes: 0, txSystemBytes: 0,
rxTotalBytes: 0, txTotalBytes: 0});
rxTotalBytes: 0, txTotalBytes: 0 });
}
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.serviceType, serviceType);
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());
@ -516,7 +571,7 @@ add_test(function test_find () {
do_check_eq(result.data[1].rxBytes, 0);
do_check_eq(result.data[samples].rxBytes, 0);
run_next_test();
}, networks[0], start, end, appId);
}, appId, serviceType, networks[0], start, end);
});
});
@ -524,6 +579,8 @@ 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 appId = 1;
var serviceType = "";
var samples = 5;
var sampleRate = netStatsDb.sampleRate;
@ -533,12 +590,12 @@ add_test(function test_findAppStats () {
start = new Date(start - sampleRate);
var stats = [];
for (var i = 0; i < samples; i++) {
stats.push({ appId: 1,
stats.push({ appId: appId, serviceType: serviceType,
network: networkWifi, timestamp: saveDate + (sampleRate * i),
rxBytes: 0, txBytes: 10,
rxTotalBytes: 0, txTotalBytes: 0 });
stats.push({ appId: 1,
stats.push({ appId: appId, serviceType: serviceType,
network: networkMobile, timestamp: saveDate + (sampleRate * i),
rxBytes: 0, txBytes: 10,
rxTotalBytes: 0, txTotalBytes: 0 });
@ -548,6 +605,7 @@ add_test(function test_findAppStats () {
do_check_eq(error, null);
netStatsDb.find(function (error, result) {
do_check_eq(error, null);
do_check_eq(result.serviceType, serviceType);
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());
@ -557,7 +615,51 @@ add_test(function test_findAppStats () {
do_check_eq(result.data[1].rxBytes, 0);
do_check_eq(result.data[samples].rxBytes, 0);
run_next_test();
}, networks[0], start, end, 1);
}, appId, serviceType, networks[0], start, end);
});
});
add_test(function test_findServiceStats () {
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 serviceType = "FakeType";
var samples = 5;
var sampleRate = netStatsDb.sampleRate;
var start = Date.now();
var saveDate = filterTimestamp(new Date());
var end = new Date(start + (sampleRate * (samples - 1)));
start = new Date(start - sampleRate);
var stats = [];
for (var i = 0; i < samples; i++) {
stats.push({ appId: appId, serviceType: serviceType,
network: networkWifi, timestamp: saveDate + (sampleRate * i),
rxBytes: 0, txBytes: 10,
rxTotalBytes: 0, txTotalBytes: 0 });
stats.push({ appId: appId, serviceType: serviceType,
network: networkMobile, timestamp: saveDate + (sampleRate * i),
rxBytes: 0, txBytes: 10,
rxTotalBytes: 0, txTotalBytes: 0 });
}
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.serviceType, serviceType);
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);
run_next_test();
}, appId, serviceType, networks[0], start, end);
});
});
@ -568,22 +670,51 @@ add_test(function test_saveMultipleAppStats () {
var saveDate = filterTimestamp(new Date());
var cached = Object.create(null);
var serviceType = "FakeType";
var wifiNetId = networkWifi.id + '' + networkWifi.type;
var mobileNetId = networkMobile.id + '' + networkMobile.type;
cached['1wifi'] = { appId: 1, date: new Date(),
networkId: networkWifi.id, networkType: networkWifi.type,
rxBytes: 0, txBytes: 10 };
cached[0 + '' + serviceType + wifiNetId] = {
appId: 0, date: new Date(),
networkId: networkWifi.id, networkType: networkWifi.type,
rxBytes: 0, txBytes: 10,
serviceType: serviceType, isAccumulative: false
};
cached['1mobile'] = { appId: 1, date: new Date(),
networkId: networkMobile.id, networkType: networkMobile.type,
rxBytes: 0, txBytes: 10 };
cached[0 + '' + serviceType + mobileNetId] = {
appId: 0, date: new Date(),
networkId: networkMobile.id, networkType: networkMobile.type,
rxBytes: 0, txBytes: 10,
serviceType: serviceType, isAccumulative: false
};
cached['2wifi'] = { appId: 2, date: new Date(),
networkId: networkWifi.id, networkType: networkWifi.type,
rxBytes: 0, txBytes: 10 };
cached[1 + '' + wifiNetId] = {
appId: 1, date: new Date(),
networkId: networkWifi.id, networkType: networkWifi.type,
rxBytes: 0, txBytes: 10,
serviceType: "", isAccumulative: false
};
cached['2mobile'] = { appId: 2, date: new Date(),
networkId: networkMobile.id, networkType: networkMobile.type,
rxBytes: 0, txBytes: 10 };
cached[1 + '' + mobileNetId] = {
appId: 1, date: new Date(),
networkId: networkMobile.id, networkType: networkMobile.type,
rxBytes: 0, txBytes: 10,
serviceType: "", isAccumulative: false
};
cached[2 + '' + wifiNetId] = {
appId: 2, date: new Date(),
networkId: networkWifi.id, networkType: networkWifi.type,
rxBytes: 0, txBytes: 10,
serviceType: "", isAccumulative: false
};
cached[2 + '' + mobileNetId] = {
appId: 2, date: new Date(),
networkId: networkMobile.id, networkType: networkMobile.type,
rxBytes: 0, txBytes: 10,
serviceType: "", isAccumulative: false
};
let keys = Object.keys(cached);
let index = 0;
@ -605,8 +736,9 @@ add_test(function test_saveMultipleAppStats () {
result.shift();
do_check_eq(error, null);
do_check_eq(result.length, 4);
do_check_eq(result[0].appId, 1);
do_check_eq(result.length, 6);
do_check_eq(result[0].serviceType, serviceType);
do_check_eq(result[3].appId, 1);
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);

View File

@ -34,8 +34,8 @@ function mokConvertNetworkInterface() {
}
add_test(function test_saveAppStats() {
var cachedAppStats = NetworkStatsService.cachedAppStats;
var timestamp = NetworkStatsService.cachedAppStatsDate.getTime();
var cachedStats = NetworkStatsService.cachedStats;
var timestamp = NetworkStatsService.cachedStatsDate.getTime();
var samples = 5;
// Create to fake nsINetworkInterfaces. As nsINetworkInterface can not
@ -47,101 +47,160 @@ add_test(function test_saveAppStats() {
// Insert fake mobile network interface in NetworkStatsService
var mobileNetId = NetworkStatsService.getNetworkId(mobile.id, mobile.type);
do_check_eq(Object.keys(cachedAppStats).length, 0);
do_check_eq(Object.keys(cachedStats).length, 0);
for (var i = 0; i < samples; i++) {
nssProxy.saveAppStats(1, wifi, timestamp, 10, 20);
nssProxy.saveAppStats(1, wifi, timestamp, 10, 20, false);
nssProxy.saveAppStats(1, mobile, timestamp, 10, 20);
nssProxy.saveAppStats(1, mobile, timestamp, 10, 20, false);
}
var key1 = 1 + NetworkStatsService.getNetworkId(wifi.id, wifi.type);
var key2 = 1 + mobileNetId;
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].networkId, wifi.id);
do_check_eq(cachedAppStats[key1].networkType, wifi.type);
do_check_eq(new Date(cachedAppStats[key1].date).getTime() / 1000,
do_check_eq(Object.keys(cachedStats).length, 2);
do_check_eq(cachedStats[key1].appId, 1);
do_check_eq(cachedStats[key1].serviceType.length, 0);
do_check_eq(cachedStats[key1].networkId, wifi.id);
do_check_eq(cachedStats[key1].networkType, wifi.type);
do_check_eq(new Date(cachedStats[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].networkId, mobile.id);
do_check_eq(cachedAppStats[key2].networkType, mobile.type);
do_check_eq(new Date(cachedAppStats[key2].date).getTime() / 1000,
do_check_eq(cachedStats[key1].rxBytes, 50);
do_check_eq(cachedStats[key1].txBytes, 100);
do_check_eq(cachedStats[key2].appId, 1);
do_check_eq(cachedStats[key1].serviceType.length, 0);
do_check_eq(cachedStats[key2].networkId, mobile.id);
do_check_eq(cachedStats[key2].networkType, mobile.type);
do_check_eq(new Date(cachedStats[key2].date).getTime() / 1000,
Math.floor(timestamp / 1000));
do_check_eq(cachedAppStats[key2].rxBytes, 50);
do_check_eq(cachedAppStats[key2].txBytes, 100);
do_check_eq(cachedStats[key2].rxBytes, 50);
do_check_eq(cachedStats[key2].txBytes, 100);
run_next_test();
});
add_test(function test_saveAppStatsWithDifferentDates() {
var today = NetworkStatsService.cachedAppStatsDate;
add_test(function test_saveServiceStats() {
var timestamp = NetworkStatsService.cachedStatsDate.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);
NetworkStatsService.updateCachedStats(
function (success, msg) {
do_check_eq(success, true);
var cachedStats = NetworkStatsService.cachedStats;
do_check_eq(Object.keys(cachedStats).length, 0);
var serviceType = 'FakeType';
for (var i = 0; i < samples; i++) {
nssProxy.saveServiceStats(serviceType, wifi, timestamp, 10, 20, false);
nssProxy.saveServiceStats(serviceType, mobile, timestamp, 10, 20, false);
}
var key1 = 0 + "" + serviceType +
NetworkStatsService.getNetworkId(wifi.id, wifi.type);
var key2 = 0 + "" + serviceType + mobileNetId + "";
do_check_eq(Object.keys(cachedStats).length, 2);
do_check_eq(cachedStats[key1].appId, 0);
do_check_eq(cachedStats[key1].serviceType, serviceType);
do_check_eq(cachedStats[key1].networkId, wifi.id);
do_check_eq(cachedStats[key1].networkType, wifi.type);
do_check_eq(new Date(cachedStats[key1].date).getTime() / 1000,
Math.floor(timestamp / 1000));
do_check_eq(cachedStats[key1].rxBytes, 50);
do_check_eq(cachedStats[key1].txBytes, 100);
do_check_eq(cachedStats[key2].appId, 0);
do_check_eq(cachedStats[key1].serviceType, serviceType);
do_check_eq(cachedStats[key2].networkId, mobile.id);
do_check_eq(cachedStats[key2].networkType, mobile.type);
do_check_eq(new Date(cachedStats[key2].date).getTime() / 1000,
Math.floor(timestamp / 1000));
do_check_eq(cachedStats[key2].rxBytes, 50);
do_check_eq(cachedStats[key2].txBytes, 100);
}
);
run_next_test();
});
add_test(function test_saveStatsWithDifferentDates() {
var today = NetworkStatsService.cachedStatsDate;
var tomorrow = new Date(today.getTime() + (24 * 60 * 60 * 1000));
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);
var key = 1 + "" + NetworkStatsService.getNetworkId(wifi.id, wifi.type);
NetworkStatsService.updateCachedAppStats(
NetworkStatsService.updateCachedStats(
function (success, msg) {
do_check_eq(success, true);
do_check_eq(Object.keys(NetworkStatsService.cachedAppStats).length, 0);
do_check_eq(Object.keys(NetworkStatsService.cachedStats).length, 0);
nssProxy.saveAppStats(1, wifi, today.getTime(), 10, 20);
nssProxy.saveAppStats(1, wifi, today.getTime(), 10, 20, false);
nssProxy.saveAppStats(1, mobile, today.getTime(), 10, 20);
nssProxy.saveAppStats(1, mobile, today.getTime(), 10, 20, false);
var saveAppStatsCb = {
var saveStatsCb = {
notify: function notify(success, message) {
do_check_eq(success, true);
var cachedAppStats = NetworkStatsService.cachedAppStats;
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].networkId, mobile.id);
do_check_eq(cachedAppStats[key].networkType, mobile.type);
do_check_eq(new Date(cachedAppStats[key].date).getTime() / 1000,
var cachedStats = NetworkStatsService.cachedStats;
var key = 2 + "" +
NetworkStatsService.getNetworkId(mobile.id, mobile.type);
do_check_eq(Object.keys(cachedStats).length, 1);
do_check_eq(cachedStats[key].appId, 2);
do_check_eq(cachedStats[key].networkId, mobile.id);
do_check_eq(cachedStats[key].networkType, mobile.type);
do_check_eq(new Date(cachedStats[key].date).getTime() / 1000,
Math.floor(tomorrow.getTime() / 1000));
do_check_eq(cachedAppStats[key].rxBytes, 30);
do_check_eq(cachedAppStats[key].txBytes, 40);
do_check_eq(cachedStats[key].rxBytes, 30);
do_check_eq(cachedStats[key].txBytes, 40);
run_next_test();
}
};
nssProxy.saveAppStats(2, mobile, tomorrow.getTime(), 30, 40, saveAppStatsCb);
nssProxy.saveAppStats(2, mobile, tomorrow.getTime(), 30, 40, false,
saveStatsCb);
}
);
});
add_test(function test_saveAppStatsWithMaxCachedTraffic() {
var timestamp = NetworkStatsService.cachedAppStatsDate.getTime();
add_test(function test_saveStatsWithMaxCachedTraffic() {
var timestamp = NetworkStatsService.cachedStatsDate.getTime();
var maxtraffic = NetworkStatsService.maxCachedTraffic;
var wifi = {type: Ci.nsINetworkInterface.NETWORK_TYPE_WIFI, id: "0"};
NetworkStatsService.updateCachedAppStats(
NetworkStatsService.updateCachedStats(
function (success, msg) {
do_check_eq(success, true);
var cachedAppStats = NetworkStatsService.cachedAppStats;
do_check_eq(Object.keys(cachedAppStats).length, 0);
var cachedStats = NetworkStatsService.cachedStats;
do_check_eq(Object.keys(cachedStats).length, 0);
nssProxy.saveAppStats(1, wifi, timestamp, 10, 20);
nssProxy.saveAppStats(1, wifi, timestamp, 10, 20, false);
do_check_eq(Object.keys(cachedAppStats).length, 1);
do_check_eq(Object.keys(cachedStats).length, 1);
nssProxy.saveAppStats(1, wifi, timestamp, maxtraffic, 20);
nssProxy.saveAppStats(1, wifi, timestamp, maxtraffic, 20, false);
do_check_eq(Object.keys(cachedAppStats).length, 0);
do_check_eq(Object.keys(cachedStats).length, 0);
run_next_test();
});
}
);
});
function run_test() {