dom/network/tests/test_networkstats_alarms.html

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:04dabc1777df
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">
15
16 function test() {
17 ok(true, "Checking if no alarms are set.");
18
19 req = navigator.mozNetworkStats.getAllAlarms();
20
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();
26 };
27
28 req.onerror = function () {
29 ok(false, "getAllAlarms() shouldn't fail!");
30 }
31 }
32
33 var req;
34 var index = -1;
35
36 var wifi = {'type': 0, 'id': '0'};
37 var mobile = {'type': 1, 'id': '1'};
38
39 var steps = [
40 function () {
41 ok(true, "Calling getAllAlarms() with invalid network.");
42
43 req = navigator.mozNetworkStats.getAllAlarms(mobile);
44
45 req.onsuccess = function () {
46 ok(false, "getAllAlarms() shouldn't succeed!");
47 };
48
49 req.onerror = function () {
50 ok(req.error.name == "InvalidInterface", "Get InvalidInterface error");
51 next();
52 }
53 },
54 function () {
55 ok(true, "Calling addAlarm() with invalid network or parameters.");
56
57 try {
58 navigator.mozNetworkStats.addAlarm();
59 } catch(ex) {
60 ok(ex.result == SpecialPowers.Cr.NS_ERROR_XPC_NOT_ENOUGH_ARGS,
61 "addAlarm() throws NS_ERROR_XPC_NOT_ENOUGH_ARGS exception when no parameters");
62 }
63
64 try {
65 navigator.mozNetworkStats.addAlarm(100000);
66 } catch(ex) {
67 ok(ex.result == SpecialPowers.Cr.NS_ERROR_XPC_NOT_ENOUGH_ARGS,
68 "addAlarm() throws NS_ERROR_XPC_NOT_ENOUGH_ARGS exception when no network");
69 }
70
71 try {
72 navigator.mozNetworkStats.addAlarm(wifi);
73 } catch(ex) {
74 ok(ex.result == SpecialPowers.Cr.NS_ERROR_XPC_NOT_ENOUGH_ARGS,
75 "addAlarm() throws NS_ERROR_XPC_NOT_ENOUGH_ARGS exception when no threshold");
76 }
77
78 req = navigator.mozNetworkStats.addAlarm(mobile, -100000);
79
80 req.onsuccess = function () {
81 ok(false, "addAlarm() shouldn't succeed with negative threshold.");
82 };
83
84 req.onerror = function () {
85 ok(req.error.name == "InvalidThresholdValue", "Get InvalidThresholdValue error");
86 next();
87 }
88 },
89 function () {
90 ok(true, "Calling addAlarm()");
91
92 req = navigator.mozNetworkStats.addAlarm(wifi, 1000000);
93
94 req.onsuccess = function () {
95 ok(true, "Succeeded to add alarm. AlarmId: " + req.result);
96 next();
97 };
98 req.onerror = function () {
99 ok(false, "addAlarm() shouldn't fail.");
100 };
101 },
102 function () {
103 ok(true, "Calling getAllAlarms()");
104
105 req = navigator.mozNetworkStats.getAllAlarms(wifi);
106
107 req.onsuccess = function () {
108 ok(req.result.length == 1, "Only one alarm");
109 ok(req.result[0].alarmId == 1, "Get correct alarmId");
110 next();
111 };
112
113 req.onerror = function () {
114 ok(false, "getAllAlarms() shouldn't fail.");
115 }
116 },
117 function () {
118 ok(true, "Calling removeAlarms() to remove alarms.");
119
120 req = navigator.mozNetworkStats.removeAlarms();
121
122 req.onsuccess = function () {
123 ok(req.result, "Succeeded to remove alarms.");
124 next();
125 };
126
127 req.onerror = function () {
128 ok(false, "removeAlarms() shouldn't fail.");
129 }
130 },
131 function () {
132 ok(true, "Checking if all alarms are removed.");
133
134 req = navigator.mozNetworkStats.getAllAlarms();
135
136 req.onsuccess = function () {
137 ok(Array.isArray(req.result) && req.result.length == 0,
138 "Succeeded to remove all alarms.");
139 next();
140 };
141
142 req.onerror = function () {
143 ok(false, "getAllAlarms() shouldn't fail.");
144 }
145 },
146 function () {
147 ok(true, "all done!\n");
148 SpecialPowers.removePermission("networkstats-manage", document);
149 SimpleTest.finish();
150 return;
151 }
152 ];
153
154 function next() {
155 index += 1;
156 if (index >= steps.length) {
157 ok(false, "Shouldn't get here!");
158 return;
159 }
160 try {
161 steps[index]();
162 } catch(ex) {
163 ok(false, "Caught exception", ex);
164 }
165 }
166
167 SimpleTest.waitForExplicitFinish();
168
169 SpecialPowers.addPermission("networkstats-manage", true, document);
170 SpecialPowers.pushPrefEnv({'set': [["dom.mozNetworkStats.enabled", true]]}, test);
171
172 </script>
173 </pre>
174 </body>
175 </html>

mercurial