tenfourfox/dom/network/tests/test_networkstats_alarms.html
Cameron Kaiser c9b2922b70 hello FPR
2017-04-19 00:56:45 -07:00

240 lines
6.5 KiB
HTML

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Test for NetworkStats alarms</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">
</div>
<pre id="test">
<script type="application/javascript">
function test() {
ok(true, "Checking if no alarms are set.");
req = navigator.mozNetworkStats.getAllAlarms();
req.onsuccess = function () {
ok(true, "Succeeded to get alarms.");
ok(Array.isArray(req.result) && req.result.length == 0,
"There are no alarms set.");
next();
};
req.onerror = function () {
ok(false, "getAllAlarms() shouldn't fail!");
}
}
var req;
var index = -1;
var wifi = {'type': 0, 'id': '0'};
var mobile = {'type': 1, 'id': '1'};
var steps = [
function () {
ok(true, "Calling getAllAlarms() with invalid network.");
req = navigator.mozNetworkStats
.getAllAlarms(new window.MozNetworkStatsInterface(mobile));
req.onsuccess = function () {
ok(false, "getAllAlarms() shouldn't succeed!");
};
req.onerror = function () {
ok(req.error.name == "InvalidInterface", "Get InvalidInterface error");
next();
}
},
function () {
ok(true, "Calling addAlarm() with invalid network or parameters.");
try {
navigator.mozNetworkStats.addAlarm();
} catch(ex) {
ok(ex.result == SpecialPowers.Cr.NS_ERROR_XPC_NOT_ENOUGH_ARGS,
"addAlarm() throws NS_ERROR_XPC_NOT_ENOUGH_ARGS exception when no parameters");
}
try {
navigator.mozNetworkStats.addAlarm(100000);
} catch(ex) {
ok(ex.result == SpecialPowers.Cr.NS_ERROR_XPC_NOT_ENOUGH_ARGS,
"addAlarm() throws NS_ERROR_XPC_NOT_ENOUGH_ARGS exception when no network");
}
try {
navigator.mozNetworkStats.addAlarm(new window.MozNetworkStatsInterface(wifi));
} catch(ex) {
ok(ex.result == SpecialPowers.Cr.NS_ERROR_XPC_NOT_ENOUGH_ARGS,
"addAlarm() throws NS_ERROR_XPC_NOT_ENOUGH_ARGS exception when no threshold");
}
req = navigator.mozNetworkStats
.addAlarm(new window.MozNetworkStatsInterface(mobile), -100000);
req.onsuccess = function () {
ok(false, "addAlarm() shouldn't succeed with negative threshold.");
};
req.onerror = function () {
ok(req.error.name == "InvalidThresholdValue", "Get InvalidThresholdValue error");
next();
}
},
function () {
ok(true, "Calling addAlarm()");
req = navigator.mozNetworkStats
.addAlarm(new window.MozNetworkStatsInterface(wifi), 100000000);
req.onsuccess = function () {
ok(true, "Succeeded to add alarm. AlarmId: " + req.result);
next();
};
req.onerror = function () {
ok(false, "addAlarm() shouldn't fail.");
};
},
function () {
ok(true, "Calling getAllAlarms()");
req = navigator.mozNetworkStats
.getAllAlarms(new window.MozNetworkStatsInterface(wifi));
req.onsuccess = function () {
ok(req.result.length == 1, "Only one alarm");
ok(req.result[0].alarmId == 1, "Get correct alarmId");
next();
};
req.onerror = function () {
ok(false, "getAllAlarms() shouldn't fail.");
}
},
function () {
ok(true, "Calling removeAlarms() to remove alarms.");
req = navigator.mozNetworkStats.removeAlarms();
req.onsuccess = function () {
ok(req.result, "Succeeded to remove alarms.");
next();
};
req.onerror = function () {
ok(false, "removeAlarms() shouldn't fail.");
}
},
function () {
ok(true, "Checking if all alarms are removed.");
req = navigator.mozNetworkStats.getAllAlarms();
req.onsuccess = function () {
ok(Array.isArray(req.result) && req.result.length == 0,
"Succeeded to remove all alarms.");
next();
};
req.onerror = function () {
ok(false, "getAllAlarms() shouldn't fail.");
}
},
// Bug 1209654 - Test threshold value will overflow or not for 2^32.
function () {
ok(true, "Calling addAlarm() with threshold greater than 4GiB");
req = navigator.mozNetworkStats
.addAlarm(new window.MozNetworkStatsInterface(wifi), 5000000000);
req.onsuccess = function () {
ok(true, "Succeeded to add alarm. AlarmId: " + req.result);
next();
};
req.onerror = function () {
ok(false, "addAlarm() shouldn't fail.");
};
},
function () {
ok(true, "Calling getAllAlarms() and check threshold.");
req = navigator.mozNetworkStats.getAllAlarms();
req.onsuccess = function () {
ok(Array.isArray(req.result) && req.result[0].threshold == 5000000000,
"Succeeded to check threshold.");
next();
};
req.onerror = function () {
ok(false, "getAllAlarms() shouldn't fail.");
}
},
// Bug 1209654 - Test threshold value will overflow or not for signed long.
function () {
ok(true, "Calling addAlarm() with threshold 3 Gib");
req = navigator.mozNetworkStats
.addAlarm(new window.MozNetworkStatsInterface(wifi), 3000000000);
req.onsuccess = function () {
ok(true, "Succeeded to add alarm. AlarmId: " + req.result);
next();
};
req.onerror = function () {
ok(false, "addAlarm() shouldn't fail.");
};
},
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 SpecialPowers.Ci.nsIDOMMozNetworkStatsManager,
"navigator.mozNetworkStats should be a nsIDOMMozNetworkStatsManager object");
test();
});
});
</script>
</pre>
</body>
</html>