Backed out changeset 12313cce9281 (bug 1083449) for suspicion of causing B2G debug...
[gecko.git] / dom / network / tests / test_networkstats_alarms.html
blob0464a8a10dd6240e598f93490d89017c4f1f59ce
1 <!DOCTYPE HTML>
2 <html>
3 <head>
4 <meta charset="utf-8">
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"/>
8 </head>
9 <body>
10 <p id="display"></p>
11 <div id="content">
12 </div>
13 <pre id="test">
14 <script type="application/javascript">
16 function test() {
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.");
25 next();
28 req.onerror = function () {
29 ok(false, "getAllAlarms() shouldn't fail!");
33 var req;
34 var index = -1;
36 var wifi = {'type': 0, 'id': '0'};
37 var mobile = {'type': 1, 'id': '1'};
39 var steps = [
40 function () {
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");
52 next();
55 function () {
56 ok(true, "Calling addAlarm() with invalid network or parameters.");
58 try {
59 navigator.mozNetworkStats.addAlarm();
60 } catch(ex) {
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");
65 try {
66 navigator.mozNetworkStats.addAlarm(100000);
67 } catch(ex) {
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");
72 try {
73 navigator.mozNetworkStats.addAlarm(new window.MozNetworkStatsInterface(wifi));
74 } catch(ex) {
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");
88 next();
91 function () {
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);
99 next();
101 req.onerror = function () {
102 ok(false, "addAlarm() shouldn't fail.");
105 function () {
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");
114 next();
117 req.onerror = function () {
118 ok(false, "getAllAlarms() shouldn't fail.");
121 function () {
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.");
128 next();
131 req.onerror = function () {
132 ok(false, "removeAlarms() shouldn't fail.");
135 function () {
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.");
143 next();
146 req.onerror = function () {
147 ok(false, "getAllAlarms() shouldn't fail.");
150 function () {
151 ok(true, "all done!\n");
152 SpecialPowers.removePermission("networkstats-manage", document);
153 SimpleTest.finish();
154 return;
158 function next() {
159 index += 1;
160 if (index >= steps.length) {
161 ok(false, "Shouldn't get here!");
162 return;
164 try {
165 steps[index]();
166 } catch(ex) {
167 ok(false, "Caught exception", ex);
171 SimpleTest.waitForExplicitFinish();
173 SpecialPowers.addPermission("networkstats-manage", true, document);
174 SpecialPowers.pushPrefEnv({'set': [["dom.mozNetworkStats.enabled", true]]},
175 function() {
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");
187 test();
190 </script>
191 </pre>
192 </body>
193 </html>