5 <title>Test for NetworkStats alarms
</title>
6 <script type=
"application/javascript" src=
"/tests/SimpleTest/SimpleTest.js"></script>
7 <link rel=
"stylesheet" type=
"text/css" href=
"/tests/SimpleTest/test.css"/>
14 <script type=
"application/javascript">
17 ok(true,
"Checking if no alarms are set.");
19 req = navigator.mozNetworkStats.getAllAlarms();
21 req.onsuccess = function () {
22 ok(true,
"Succeeded to get alarms.");
23 ok(Array.isArray(req.result) && req.result.length ==
0,
24 "There are no alarms set.");
28 req.onerror = function () {
29 ok(false,
"getAllAlarms() shouldn't fail!");
36 var wifi = {'type':
0, 'id': '
0'};
37 var mobile = {'type':
1, 'id': '
1'};
41 ok(true,
"Calling getAllAlarms() with invalid network.");
43 req = navigator.mozNetworkStats
44 .getAllAlarms(new window.MozNetworkStatsInterface(mobile));
46 req.onsuccess = function () {
47 ok(false,
"getAllAlarms() shouldn't succeed!");
50 req.onerror = function () {
51 ok(req.error.name ==
"InvalidInterface",
"Get InvalidInterface error");
56 ok(true,
"Calling addAlarm() with invalid network or parameters.");
59 navigator.mozNetworkStats.addAlarm();
61 ok(ex.result == SpecialPowers.Cr.NS_ERROR_XPC_NOT_ENOUGH_ARGS,
62 "addAlarm() throws NS_ERROR_XPC_NOT_ENOUGH_ARGS exception when no parameters");
66 navigator.mozNetworkStats.addAlarm(
100000);
68 ok(ex.result == SpecialPowers.Cr.NS_ERROR_XPC_NOT_ENOUGH_ARGS,
69 "addAlarm() throws NS_ERROR_XPC_NOT_ENOUGH_ARGS exception when no network");
73 navigator.mozNetworkStats.addAlarm(new window.MozNetworkStatsInterface(wifi));
75 ok(ex.result == SpecialPowers.Cr.NS_ERROR_XPC_NOT_ENOUGH_ARGS,
76 "addAlarm() throws NS_ERROR_XPC_NOT_ENOUGH_ARGS exception when no threshold");
79 req = navigator.mozNetworkStats
80 .addAlarm(new window.MozNetworkStatsInterface(mobile), -
100000);
82 req.onsuccess = function () {
83 ok(false,
"addAlarm() shouldn't succeed with negative threshold.");
86 req.onerror = function () {
87 ok(req.error.name ==
"InvalidThresholdValue",
"Get InvalidThresholdValue error");
92 ok(true,
"Calling addAlarm()");
94 req = navigator.mozNetworkStats
95 .addAlarm(new window.MozNetworkStatsInterface(wifi),
100000000);
97 req.onsuccess = function () {
98 ok(true,
"Succeeded to add alarm. AlarmId: " + req.result);
101 req.onerror = function () {
102 ok(false,
"addAlarm() shouldn't fail.");
106 ok(true,
"Calling getAllAlarms()");
108 req = navigator.mozNetworkStats
109 .getAllAlarms(new window.MozNetworkStatsInterface(wifi));
111 req.onsuccess = function () {
112 ok(req.result.length ==
1,
"Only one alarm");
113 ok(req.result[
0].alarmId ==
1,
"Get correct alarmId");
117 req.onerror = function () {
118 ok(false,
"getAllAlarms() shouldn't fail.");
122 ok(true,
"Calling removeAlarms() to remove alarms.");
124 req = navigator.mozNetworkStats.removeAlarms();
126 req.onsuccess = function () {
127 ok(req.result,
"Succeeded to remove alarms.");
131 req.onerror = function () {
132 ok(false,
"removeAlarms() shouldn't fail.");
136 ok(true,
"Checking if all alarms are removed.");
138 req = navigator.mozNetworkStats.getAllAlarms();
140 req.onsuccess = function () {
141 ok(Array.isArray(req.result) && req.result.length ==
0,
142 "Succeeded to remove all alarms.");
146 req.onerror = function () {
147 ok(false,
"getAllAlarms() shouldn't fail.");
151 ok(true,
"all done!\n");
152 SpecialPowers.removePermission(
"networkstats-manage", document);
160 if (index
>= steps.length) {
161 ok(false,
"Shouldn't get here!");
167 ok(false,
"Caught exception", ex);
171 SimpleTest.waitForExplicitFinish();
173 SpecialPowers.addPermission(
"networkstats-manage", true, document);
174 SpecialPowers.pushPrefEnv({'set': [[
"dom.mozNetworkStats.enabled", true]]},
176 ok(SpecialPowers.hasPermission(
"networkstats-manage", document),
177 "Has permission 'networkstats-manage'.");
179 ok(SpecialPowers.getBoolPref(
"dom.mozNetworkStats.enabled"),
180 "Preference 'dom.mozNetworkStats.enabled' is true.");
182 ok('mozNetworkStats' in navigator,
"navigator.mozNetworkStats should exist");
184 ok(navigator.mozNetworkStats instanceof SpecialPowers.Ci.nsIDOMMozNetworkStatsManager,
185 "navigator.mozNetworkStats should be a nsIDOMMozNetworkStatsManager object");