gecko-dev/dom/network/tests/test_networkstats_alarms.html

238 строки
6.3 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 () {
is(req.error.name, "InvalidInterface", "Get InvalidInterface error");
next();
}
},
function () {
ok(true, "Calling addAlarm() with invalid network or parameters.");
var msg = "TypeError: Not enough arguments to MozNetworkStatsManager.addAlarm.";
try {
navigator.mozNetworkStats.addAlarm();
} catch(ex) {
is(ex.toString(), msg, "addAlarm() throws \"" + msg + "\" when no parameters");
}
try {
navigator.mozNetworkStats.addAlarm(100000);
} catch(ex) {
is(ex.toString(), msg, "addAlarm() throws " + msg + " when no network");
}
try {
navigator.mozNetworkStats.addAlarm(new window.MozNetworkStatsInterface(wifi));
} catch(ex) {
is(ex.toString(), msg, "addAlarm() throws " + msg + " 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 () {
is(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 () {
is(req.result.length, 1, "Only one alarm");
is(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 MozNetworkStatsManager,
"navigator.mozNetworkStats should be a MozNetworkStatsManager object");
test();
});
});
</script>
</pre>
</body>
</html>