mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-26 06:11:37 +00:00
Bug 746069: Part 4: Tests for NetworkStats. r=philikon r=marshall
This commit is contained in:
parent
abc51198a7
commit
c450877df2
@ -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
|
||||
|
306
dom/network/tests/test_networkstats_basics.html
Normal file
306
dom/network/tests/test_networkstats_basics.html
Normal file
@ -0,0 +1,306 @@
|
||||
<!DOCTYPE HTML>
|
||||
<html>
|
||||
<head>
|
||||
<title>Test for NetworkStats</title>
|
||||
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
|
||||
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
|
||||
</head>
|
||||
<body>
|
||||
<p id="display"></p>
|
||||
<div id="content" style="display: none">
|
||||
</div>
|
||||
<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() {
|
||||
var gNetworkStatsEnabled = SpecialPowers.getBoolPref("dom.mozNetworkStats.enabled");
|
||||
|
||||
if (!gNetworkStatsEnabled) {
|
||||
is(navigator.mozNetworkStats, null, "mozNetworkStats is null when not enabled.");
|
||||
SimpleTest.finish();
|
||||
}
|
||||
|
||||
// Test interfaces
|
||||
checkInterface("NetworkStatsManager");
|
||||
checkInterface("NetworkStats");
|
||||
checkInterface("NetworkStatsData");
|
||||
|
||||
ok('mozNetworkStats' in navigator, "navigator.mozMozNetworkStats should exist");
|
||||
ok(navigator.mozNetworkStats, "navigator.mozNetworkStats returns an object");
|
||||
|
||||
SpecialPowers.addPermission("networkstats-manage", true, document);
|
||||
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('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.");
|
||||
|
||||
// Test IDL methods
|
||||
next();
|
||||
return;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
var counter = 0;
|
||||
var date = start;
|
||||
var success = true;
|
||||
|
||||
do {
|
||||
if(data[counter].date.getTime() != date) {
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
date += sampleRate;
|
||||
counter++;
|
||||
} while (date <= end);
|
||||
|
||||
ok(success, "data result has correct dates");
|
||||
}
|
||||
|
||||
var req;
|
||||
var index = -1;
|
||||
var netStats = null;
|
||||
|
||||
var steps = [
|
||||
function () {
|
||||
// Test clearAlldata
|
||||
req = netStats.clearAllData();
|
||||
req.onsuccess = function () {
|
||||
ok(true, "clearAllData deleted the database");
|
||||
next();
|
||||
};
|
||||
req.onerror = function () {
|
||||
ok(false, "clearAllData deleted the database");
|
||||
}
|
||||
},
|
||||
function () {
|
||||
// Check if getNetworkStats launch exception when start is greather than end
|
||||
|
||||
// Prepare get params
|
||||
var type = netStats.connectionTypes[0];
|
||||
// Get dates
|
||||
var endDate = new Date();
|
||||
var startDate = new Date(endDate.getTime() + 1000);
|
||||
|
||||
try {
|
||||
netStats.getNetworkStats({start: startDate, end: endDate});
|
||||
} catch(ex) {
|
||||
ok(true, "getNetworkStats launch exception when start is greater than end");
|
||||
next();
|
||||
return;
|
||||
}
|
||||
|
||||
ok(false, "getNetworkStats launch exceptionwhen start is greater than end");
|
||||
next();
|
||||
return;
|
||||
},
|
||||
function () {
|
||||
// Test if call getNetworkStats with undefined start param 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;
|
||||
}
|
||||
|
||||
ok(false, "getNetworkStats launch exception when start param does not exist");
|
||||
}, 1000);
|
||||
},
|
||||
function () {
|
||||
// Test if call getNetworkStats with undefined end param 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;
|
||||
}
|
||||
|
||||
ok(false, "getNetworkStats launch exception when end param does not exist");
|
||||
}, 1000);
|
||||
},
|
||||
function () {
|
||||
ok(true, "Get stats for a connectionType and dates adapted to samplerate");
|
||||
// Prepare get params
|
||||
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});
|
||||
req.onsuccess = function () {
|
||||
ok(true, "Get stats request ok");
|
||||
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 stats for a connectionType failure!");
|
||||
}
|
||||
},
|
||||
function () {
|
||||
ok(true, "Get stats for all connectionTypes and dates 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 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.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 stats for all connectionTypes failure!");
|
||||
}
|
||||
},
|
||||
function () {
|
||||
ok(true, "Get 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 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, connectionType: type});
|
||||
req.onsuccess = function () {
|
||||
ok(true, "Get stats request ok");
|
||||
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 stats for a connectionType failure!");
|
||||
}
|
||||
},
|
||||
function () {
|
||||
ok(true, "Get 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.onsuccess = function () {
|
||||
ok(true, "Get stats request ok");
|
||||
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 stats for all connectionType failure!");
|
||||
}
|
||||
},
|
||||
function () {
|
||||
ok(true, "all done!\n");
|
||||
SimpleTest.finish();
|
||||
return;
|
||||
}
|
||||
];
|
||||
|
||||
function next() {
|
||||
index += 1;
|
||||
if (index >= steps.length) {
|
||||
ok(false, "Shouldn't get here!");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
steps[index]();
|
||||
} catch(ex) {
|
||||
ok(false, "Caught exception", ex);
|
||||
}
|
||||
}
|
||||
|
||||
SimpleTest.waitForExplicitFinish();
|
||||
addLoadEvent(test);
|
||||
|
||||
</script>
|
||||
</pre>
|
||||
</body>
|
||||
</html>
|
387
dom/network/tests/unit_stats/test_networkstats_db.js
Normal file
387
dom/network/tests/unit_stats/test_networkstats_db.js
Normal file
@ -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();
|
||||
}
|
105
dom/network/tests/unit_stats/test_networkstats_service.js
Normal file
105
dom/network/tests/unit_stats/test_networkstats_service.js
Normal file
@ -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();
|
||||
}
|
6
dom/network/tests/unit_stats/xpcshell.ini
Normal file
6
dom/network/tests/unit_stats/xpcshell.ini
Normal file
@ -0,0 +1,6 @@
|
||||
[DEFAULT]
|
||||
head =
|
||||
tail =
|
||||
|
||||
[test_networkstats_service.js]
|
||||
[test_networkstats_db.js]
|
@ -539,6 +539,9 @@ var interfaceNamesInGlobalScope =
|
||||
"PermissionSettings",
|
||||
"DataErrorEvent",
|
||||
"DataChannel"
|
||||
"MozNetworkStatsManager",
|
||||
"MozNetworkStats",
|
||||
"MozNetworkStatsData"
|
||||
]
|
||||
|
||||
for (var i in SpecialPowers.Components.interfaces) {
|
||||
|
@ -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]
|
||||
|
Loading…
Reference in New Issue
Block a user