Bug 965319 - Part 2: Update tests. r=gene

This commit is contained in:
Albert Crespell 2014-02-09 12:43:49 +01:00
Родитель aa521c403f
Коммит 0b2dbcded8
2 изменённых файлов: 82 добавлений и 43 удалений

Просмотреть файл

@ -734,30 +734,34 @@ var exampleManifestURL = "http://example.com/manifest.webapp";
var testPageURL = "http://test.com/index.html";
var testManifestURL = "http://test.com/manifest.webapp";
var alarms = [{ id: null,
networkId: networkWifi,
threshold: 10000,
data: {foo: "something"},
pageURL: examplePageURL,
manifestURL: exampleManifestURL },
{ id: null,
networkId: networkWifi,
threshold: 1000,
data: {foo: "else"},
pageURL: examplePageURL,
manifestURL: exampleManifestURL },
{ id: null,
networkId: networkMobile,
threshold: 100,
data: {foo: "to"},
pageURL: examplePageURL,
manifestURL: exampleManifestURL },
{ id: null,
networkId: networkMobile,
threshold: 10,
data: {foo: "test"},
pageURL: testPageURL,
manifestURL: testManifestURL }];
var alarms = [{ id: null,
networkId: networkWifi,
absoluteThreshold: 10000,
relativeThreshold: 10000,
data: {foo: "something"},
pageURL: examplePageURL,
manifestURL: exampleManifestURL },
{ id: null,
networkId: networkWifi,
absoluteThreshold: 1000,
relativeThreshold: 1000,
data: {foo: "else"},
pageURL: examplePageURL,
manifestURL: exampleManifestURL },
{ id: null,
networkId: networkMobile,
absoluteThreshold: 100,
relativeThreshold: 100,
data: {foo: "to"},
pageURL: examplePageURL,
manifestURL: exampleManifestURL },
{ id: null,
networkId: networkMobile,
absoluteThreshold: 10,
relativeThreshold: 10,
data: {foo: "test"},
pageURL: testPageURL,
manifestURL: testManifestURL }];
var alarmsDbId = 1;
@ -772,7 +776,8 @@ add_test(function test_addAlarm() {
do_check_eq(result.length, 1);
do_check_eq(result[0].id, alarmsDbId);
do_check_eq(result[0].networkId, alarms[0].networkId);
do_check_eq(result[0].threshold, alarms[0].threshold);
do_check_eq(result[0].absoluteThreshold, alarms[0].absoluteThreshold);
do_check_eq(result[0].relativeThreshold, alarms[0].relativeThreshold);
do_check_eq(result[0].data.foo, alarms[0].data.foo);
run_next_test();
});
@ -790,7 +795,8 @@ add_test(function test_getFirstAlarm() {
do_check_eq(error, null);
do_check_eq(result.id, alarmsDbId);
do_check_eq(result.networkId, alarms[1].networkId);
do_check_eq(result.threshold, alarms[1].threshold);
do_check_eq(result.absoluteThreshold, alarms[1].absoluteThreshold);
do_check_eq(result.relativeThreshold, alarms[1].relativeThreshold);
do_check_eq(result.data.foo, alarms[1].data.foo);
do_check_eq(result.pageURL, alarms[1].pageURL);
do_check_eq(result.manifestURL, alarms[1].manifestURL);
@ -808,7 +814,8 @@ add_test(function test_removeAlarm() {
do_check_eq(error, null);
do_check_eq(result.id, alarmsDbId - 1);
do_check_eq(result.networkId, alarms[0].networkId);
do_check_eq(result.threshold, alarms[0].threshold);
do_check_eq(result.absoluteThreshold, alarms[0].absoluteThreshold);
do_check_eq(result.relativeThreshold, alarms[0].relativeThreshold);
do_check_eq(result.data.foo, alarms[0].data.foo);
do_check_eq(result.pageURL, alarms[0].pageURL);
do_check_eq(result.manifestURL, alarms[0].manifestURL);
@ -897,7 +904,8 @@ add_test(function test_updateAlarm() {
do_check_eq(error, null);
do_check_eq(result.id, updatedAlarm.id);
do_check_eq(result.networkId, updatedAlarm.networkId);
do_check_eq(result.threshold, updatedAlarm.threshold);
do_check_eq(result.absoluteThreshold, updatedAlarm.absoluteThreshold);
do_check_eq(result.relativeThreshold, updatedAlarm.relativeThreshold);
do_check_eq(result.data.foo, updatedAlarm.data.foo);
do_check_eq(result.pageURL, updatedAlarm.pageURL);
do_check_eq(result.manifestURL, updatedAlarm.manifestURL);

Просмотреть файл

@ -7,6 +7,8 @@ const NETWORK_STATUS_READY = 0;
const NETWORK_STATUS_STANDBY = 1;
const NETWORK_STATUS_AWAY = 2;
var wifiId = '00';
function getNetworks(callback) {
NetworkStatsService._db.getAvailableNetworks(function onGetNetworks(aError, aResult) {
callback(aError, aResult);
@ -122,9 +124,34 @@ add_test(function test_updateStats_failure() {
});
});
// Define Mockup function to simulate a request to netd
function MockNetdRequest(aCallback) {
var timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
var event = {
notify: function (timer) {
aCallback();
}
};
timer.initWithCallback(event, 100, Ci.nsITimer.TYPE_ONE_SHOT);
}
add_test(function test_queue() {
// Fill networks with fake network interfaces
// to enable netd async requests
// Overwrite update function of NetworkStatsService to avoid netd errors due to use
// fake interfaces. First, original function is stored to restore it at the end of the
// test.
var updateFunctionBackup = NetworkStatsService.update;
NetworkStatsService.update = function update(aNetId, aCallback) {
MockNetdRequest(function () {
if (aCallback) {
aCallback(true, "ok");
}
});
};
// Fill networks with fake network interfaces to enable netd async requests.
var network = {id: "1234", type: Ci.nsIDOMMozNetworkStatsManager.MOBILE};
var netId1 = NetworkStatsService.getNetworkId(network.id, network.type);
NetworkStatsService._networks[netId1] = { network: network,
@ -140,33 +167,34 @@ add_test(function test_queue() {
do_check_eq(NetworkStatsService.updateQueue.length, 2);
do_check_eq(NetworkStatsService.updateQueue[0].callbacks.length, 1);
var i = 0;
var updateCount = 0;
var callback = function(success, msg) {
return;
i++;
if (i >= updateCount) {
NetworkStatsService.update = updateFunctionBackup;
run_next_test();
}
};
NetworkStatsService.updateStats(netId1, callback);
updateCount++;
NetworkStatsService.updateStats(netId2, callback);
updateCount++;
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);
// Clear queue because in test environment requests for mobile networks
// can not be handled.
NetworkStatsService.updateQueue = [];
run_next_test();
});
var wifiId = '00';
add_test(function test_getAlarmQuota() {
let alarm = { networkId: wifiId, absoluteThreshold: 10000 };
add_test(function test_updateThreshold() {
let alarm = { networkId: wifiId, threshold: 10000 };
NetworkStatsService._updateThreshold(alarm, function onSet(error, threshold){
NetworkStatsService._getAlarmQuota(alarm, function onSet(error, quota){
do_check_eq(error, null);
do_check_neq(threshold.systemThreshold, undefined);
do_check_neq(threshold.absoluteThreshold, undefined);
do_check_neq(quota, undefined);
do_check_eq(alarm.absoluteThreshold, alarm.relativeThreshold);
run_next_test();
});
});
@ -222,6 +250,9 @@ add_test(function test_fireAlarm() {
pageURL: testPageURL,
manifestURL: testManifestURL };
// Set wifi status to standby to avoid connecting to netd when adding an alarm.
NetworkStatsService._networks[wifiId].status = NETWORK_STATUS_STANDBY;
NetworkStatsService._db.addAlarm(alarm, function addSuccessCb(error, newId) {
NetworkStatsService._db.getAlarms(Ci.nsINetworkInterface.NETWORK_TYPE_WIFI,
testManifestURL, function onGet(error, result) {