gecko/dom/network/tests/test_networkstats_basics.html
2013-01-07 15:26:42 -08:00

302 lines
10 KiB
HTML

<!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() {
// 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('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");
SpecialPowers.removePermission("networkstats-manage", document);
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();
SpecialPowers.addPermission("networkstats-manage", true, document);
SpecialPowers.pushPrefEnv({'set': [["dom.mozNetworkStats.enabled", true]]}, test);
</script>
</pre>
</body>
</html>