зеркало из https://github.com/mozilla/gecko-dev.git
347 строки
11 KiB
HTML
347 строки
11 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">
|
|
|
|
function test() {
|
|
netStats = window.navigator.mozNetworkStats;
|
|
ok(netStats, "mozNetworkStats exists");
|
|
|
|
// Test IDL attributes
|
|
ok('sampleRate' in netStats,
|
|
"sampleRate should be a NetworkStats attribute");
|
|
ok(netStats.sampleRate > 0,
|
|
"sampleRate 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) {
|
|
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");
|
|
}
|
|
|
|
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 getAvailableNetworks
|
|
req = netStats.getAvailableNetworks();
|
|
req.onsuccess = function () {
|
|
ok(true, "getAvailableNetworks request ok");
|
|
ok(Array.isArray(req.result) && req.result.length > 0,
|
|
"getAvailableNetworks returns an array not empty");
|
|
next();
|
|
};
|
|
req.onerror = function () {
|
|
ok(false, "getAvailableNetworks failure!");
|
|
}
|
|
},
|
|
function () {
|
|
// Test clearAllStats
|
|
req = netStats.clearAllStats();
|
|
req.onsuccess = function () {
|
|
ok(true, "clearAllStats deleted the database");
|
|
next();
|
|
};
|
|
req.onerror = function () {
|
|
ok(false, "clearAllStats deleted the database");
|
|
}
|
|
},
|
|
function () {
|
|
// Check if getSamples throws exception when start is greather than end
|
|
|
|
// Prepare get params
|
|
req = netStats.getAvailableNetworks();
|
|
req.onsuccess = function () {
|
|
var network = req.result[0];
|
|
|
|
// Get dates
|
|
var endDate = new Date();
|
|
var startDate = new Date(endDate.getTime() + 1000);
|
|
|
|
try {
|
|
netStats.getSamples(network, startDate, endDate);
|
|
} catch(ex) {
|
|
ok(true, "getSamples throws exception when start is greater than end");
|
|
next();
|
|
return;
|
|
}
|
|
|
|
ok(false, "getSamples throws exception when start is greater than end");
|
|
next();
|
|
return;
|
|
};
|
|
req.onerror = function () {
|
|
ok(false, "Error getting networks!");
|
|
}
|
|
},
|
|
function () {
|
|
// Test if call getSamples with network of type different than
|
|
// MozNetworkStatsInterface throws an exception
|
|
|
|
// Prepare get params
|
|
var network = "wifi";
|
|
var endDate = new Date();
|
|
var startDate = new Date(endDate.getTime() - 1000);
|
|
|
|
try {
|
|
netStats.getSamples(network, new Date(), new Date());
|
|
} catch(ex) {
|
|
ok(true, "getSamples throws exception if network is not " +
|
|
"a MozNetworkStatsInterface");
|
|
next();
|
|
return;
|
|
}
|
|
|
|
ok(false, "getSamples throws exception if network is not " +
|
|
"a MozNetworkStatsInterface");
|
|
},
|
|
function () {
|
|
// Test if call getSamples with start parameter type different than Date throws an exception
|
|
|
|
// Prepare get params
|
|
req = netStats.getAvailableNetworks();
|
|
req.onsuccess = function () {
|
|
var network = req.result[0];
|
|
|
|
var endDate = new Date();
|
|
var startDate = new Date(endDate.getTime() - 1000);
|
|
startDate = startDate.toString();
|
|
|
|
try {
|
|
netStats.getSamples(network, startDate, endDate);
|
|
} catch(ex) {
|
|
ok(true, "getSamples throws exception when start param is not a Date");
|
|
next();
|
|
return;
|
|
}
|
|
|
|
ok(false, "getSamples throws exception when start param is not a Date");
|
|
};
|
|
req.onerror = function () {
|
|
ok(false, "Error getting networks!");
|
|
};
|
|
},
|
|
function () {
|
|
// Test if call getSamples with end parameter type different than Date throws an exception
|
|
|
|
// Prepare get params
|
|
req = netStats.getAvailableNetworks();
|
|
req.onsuccess = function () {
|
|
var network = req.result[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 throws exception when end param is not a Date");
|
|
next();
|
|
return;
|
|
}
|
|
|
|
ok(false, "getSamples throws exception when end param is not a Date");
|
|
};
|
|
req.onerror = function () {
|
|
ok(false, "Error getting networks!");
|
|
};
|
|
},
|
|
function () {
|
|
ok(true, "Get stats for a network and dates adapted to samplerate");
|
|
// Prepare get params
|
|
req = netStats.getAvailableNetworks();
|
|
req.onsuccess = function () {
|
|
var network = req.result[0];
|
|
var diff = 2;
|
|
// Get samplerate in millis
|
|
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)
|
|
* 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.getSamples(network, startDate, endDate);
|
|
req.onsuccess = function () {
|
|
ok(true, "Get system stats request ok");
|
|
ok(req.result.manifestURL == null, "manifestURL 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;
|
|
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 failure!");
|
|
}
|
|
};
|
|
req.onerror = function () {
|
|
ok(false, "Error getting networks!");
|
|
};
|
|
},
|
|
function () {
|
|
ok(true, "Get system stats for a network and dates not adapted to samplerate");
|
|
// Prepare get params
|
|
req = netStats.getAvailableNetworks();
|
|
req.onsuccess = function () {
|
|
var network = req.result[0];
|
|
var diff = 2;
|
|
// Get samplerate in millis
|
|
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
|
|
// 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.getSamples(network, startDate, endDate);
|
|
req.onsuccess = function () {
|
|
ok(true, "Get stats request ok");
|
|
ok(req.result.manifestURL == null, "manifestURL 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;
|
|
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 failure!");
|
|
}
|
|
};
|
|
req.onerror = function () {
|
|
ok(false, "Error getting networks!");
|
|
};
|
|
},
|
|
function () {
|
|
// Test clearStats
|
|
req = netStats.getAvailableNetworks();
|
|
req.onsuccess = function () {
|
|
var network = req.result[0];
|
|
|
|
req = netStats.clearStats(network);
|
|
req.onsuccess = function () {
|
|
ok(true, "clearStats deleted the database");
|
|
next();
|
|
};
|
|
req.onerror = function () {
|
|
ok(false, "clearStats deleted the database");
|
|
}
|
|
};
|
|
req.onerror = function () {
|
|
ok(false, "Error getting networks!");
|
|
};
|
|
},
|
|
function () {
|
|
// Test getAvailableServiceTypes
|
|
req = netStats.getAvailableServiceTypes();
|
|
req.onsuccess = function () {
|
|
ok(true, "getAvailableServiceTypes request ok");
|
|
ok(Array.isArray(req.result) && req.result.length == 0,
|
|
"getAvailableServiceTypes returns an empty array");
|
|
next();
|
|
};
|
|
req.onerror = function () {
|
|
ok(false, "getAvailableServiceTypes 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();
|
|
|
|
SpecialPowers.pushPrefEnv({'set': [["dom.mozNetworkStats.enabled", true]]},
|
|
function() {
|
|
SpecialPowers.pushPermissions([{ 'type': 'networkstats-manage', 'allow': 1, 'context': window.document }],
|
|
function() {
|
|
ok(SpecialPowers.hasPermission("networkstats-manage", document),
|
|
"Has permission 'networkstats-manage'.");
|
|
|
|
ok(SpecialPowers.getBoolPref("dom.mozNetworkStats.enabled"),
|
|
"Preference 'dom.mozNetworkStats.enabled' is true.");
|
|
|
|
ok('mozNetworkStats' in navigator, "navigator.mozNetworkStats should exist");
|
|
|
|
ok(navigator.mozNetworkStats instanceof MozNetworkStatsManager,
|
|
"navigator.mozNetworkStats should be a MozNetworkStatsManager object");
|
|
|
|
test();
|
|
});
|
|
});
|
|
|
|
</script>
|
|
</pre>
|
|
</body>
|
|
</html>
|