UNPKG

17 kBJavaScriptView Raw
1/*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 */
21
22exports.defineAutoTests = function () {
23
24 describe('Battery (navigator.battery)', function () {
25
26 it("battery.spec.1 should exist", function () {
27 expect(navigator.battery).toBeDefined();
28 });
29 });
30
31 describe('Battery Events', function () {
32 // BatteryStatus
33 beforeEach(function () {
34 // Custom Matcher
35 jasmine.Expectation.addMatchers({
36 toBatteryStatus : function () {
37 return {
38 compare : function (status, message) {
39 var pass = status;
40 return {
41 pass : pass,
42 message : message
43 };
44 }
45 };
46 }
47 });
48 });
49
50 it("battery.spec.2 should fire batterystatus events", function (done) {
51 var onEvent = function () {
52 window.removeEventListener("batterystatus", onEvent, false);
53 onEventFlag = true;
54 },
55 onEventFlag = false;
56 // batterystatus -> 30
57 window.addEventListener("batterystatus", onEvent, false);
58
59 navigator.battery._status({
60 level : 30,
61 isPlugged : false
62 });
63 setTimeout(function () {
64 expect(onEventFlag).toBatteryStatus('Listener: "batterystatus" event, it did not fire');
65 done();
66 }, 100);
67
68 });
69
70 it("battery.spec.3 should fire batterylow event (30 -> 20)", function (done) {
71
72 var onEvent = function () {
73 window.removeEventListener("batterylow", onEvent, false);
74 onEventFlag = true;
75 },
76 onEventFlag = false;
77 // batterylow 30 -> 20
78 navigator.battery._status({
79 level : 30,
80 isPlugged : false
81 });
82 window.addEventListener("batterylow", onEvent, false);
83 navigator.battery._status({
84 level : 20,
85 isPlugged : false
86 });
87
88 setTimeout(function () {
89 expect(onEventFlag).toBatteryStatus('Listener: "batterylow" event (30 -> 20), it did not fire');
90 done();
91 }, 100);
92
93 });
94
95 it("battery.spec.3.1 should fire batterylow event (30 -> 19)", function (done) {
96
97 var onEvent = function () {
98 window.removeEventListener("batterylow", onEvent, false);
99 onEventFlag = true;
100 },
101 onEventFlag = false;
102
103 // batterylow 30 -> 19
104
105 navigator.battery._status({
106 level : 30,
107 isPlugged : false
108 });
109 window.addEventListener("batterylow", onEvent, false);
110 navigator.battery._status({
111 level : 19,
112 isPlugged : false
113 });
114
115 setTimeout(function () {
116 expect(onEventFlag).toBatteryStatus('Listener: "batterylow" event (30 -> 19), it did not fire');
117 done();
118 }, 100);
119 });
120
121 it("battery.spec.3.2 should not fire batterylow event (5 -> 20)", function (done) {
122
123 var onEvent = function () {
124 window.removeEventListener("batterylow", onEvent, false);
125 onEventFlag = false;
126 },
127 onEventFlag = true;
128
129 // batterylow should not fire when level increases (5->20) ( CB-4519 )
130
131 navigator.battery._status({
132 level : 5,
133 isPlugged : false
134 });
135 window.addEventListener("batterylow", onEvent, false);
136 navigator.battery._status({
137 level : 20,
138 isPlugged : false
139 });
140
141 setTimeout(function () {
142 expect(onEventFlag).toBatteryStatus('Listener: "batterylow" event (5 -> 20), should not be fired');
143 done();
144 }, 100);
145 });
146
147 it("battery.spec.3.3 batterylow event(21 -> 20) should not fire if charging", function (done) {
148
149 var onEvent = function () {
150 window.removeEventListener("batterylow", onEvent, false);
151 onEventFlag = false;
152 },
153 onEventFlag = true;
154
155 // batterylow should NOT fire if we are charging ( CB-4520 )
156
157 navigator.battery._status({
158 level : 21,
159 isPlugged : true
160 });
161 window.addEventListener("batterylow", onEvent, false);
162 navigator.battery._status({
163 level : 20,
164 isPlugged : true
165 });
166
167 setTimeout(function () {
168 expect(onEventFlag).toBatteryStatus('Listener: "batterylow" event (21 -> 20), should not be fired if charging');
169 done();
170 }, 100);
171 });
172
173 it("battery.spec.4 should fire batterycritical events (19 -> 5)", function (done) {
174 var onEvent = function () {
175 window.removeEventListener("batterycritical", onEvent, false);
176 onEventFlag = true;
177 },
178 onEventFlag = false;
179
180 // batterycritical 19->5
181 navigator.battery._status({
182 level : 19,
183 isPlugged : false
184 });
185 window.addEventListener("batterycritical", onEvent, false);
186 navigator.battery._status({
187 level : 5,
188 isPlugged : false
189 });
190
191 setTimeout(function () {
192 expect(onEventFlag).toBatteryStatus('Listener: "batterycritical" event (19 -> 5), it did not fire');
193 done();
194 }, 100);
195
196 });
197
198 it("battery.spec.4.1 should fire batterycritical (19 -> 4) events", function (done) {
199 var onEvent = function () {
200 window.removeEventListener("batterycritical", onEvent, false);
201 onEventFlag = true;
202 },
203 onEventFlag = false;
204
205 // batterycritical 19->4
206 navigator.battery._status({
207 level : 19,
208 isPlugged : false
209 });
210 window.addEventListener("batterycritical", onEvent, false);
211 navigator.battery._status({
212 level : 4,
213 isPlugged : false
214 });
215
216 setTimeout(function () {
217 expect(onEventFlag).toBatteryStatus('Listener: "batterycritical" event (19 -> 4), it did not fire');
218 done();
219 }, 100);
220
221 });
222
223 it("battery.spec.4.2 should fire batterycritical event (100 -> 4) when decreases", function (done) {
224 var onEvent = function () {
225 window.removeEventListener("batterycritical", onEvent, false);
226 onEventFlag = true;
227 },
228 onEventFlag = false;
229
230 // setup: batterycritical should fire when level decreases (100->4) ( CB-4519 )
231 navigator.battery._status({
232 level : 100,
233 isPlugged : false
234 });
235 window.addEventListener("batterycritical", onEvent, false);
236 navigator.battery._status({
237 level : 4,
238 isPlugged : false
239 });
240
241 setTimeout(function () {
242 expect(onEventFlag).toBatteryStatus('Listener: "batterycritical" event (100 -> 4), it did not fire');
243 done();
244 }, 100);
245 });
246
247 it("battery.spec.4.3 should not fire batterycritical event (4 -> 5) when increasing", function (done) {
248 var onEvent = function () {
249 window.removeEventListener("batterycritical", onEvent, false);
250 onEventFlag = false;
251 },
252 onEventFlag = true;
253
254 // batterycritical should not fire when level increases (4->5)( CB-4519 )
255 navigator.battery._status({
256 level : 4,
257 isPlugged : false
258 });
259 window.addEventListener("batterycritical", onEvent, false);
260 navigator.battery._status({
261 level : 5,
262 isPlugged : false
263 });
264
265 setTimeout(function () {
266 expect(onEventFlag).toBatteryStatus('Listener: "batterycritical" event (4 -> 5), should not be fired');
267 done();
268 }, 100);
269 });
270
271 it("battery.spec.4.4 should not fire batterycritical event (6 -> 5) if charging", function (done) {
272 var onEvent = function () {
273 window.removeEventListener("batterycritical", onEvent, false);
274 onEventFlag = false;
275 },
276 onEventFlag = true;
277
278 // batterycritical should NOT fire if we are charging ( CB-4520 )
279 navigator.battery._status({
280 level : 6,
281 isPlugged : true
282 });
283 window.addEventListener("batterycritical", onEvent, false);
284 navigator.battery._status({
285 level : 5,
286 isPlugged : true
287 });
288
289 setTimeout(function () {
290 expect(onEventFlag).toBatteryStatus('Listener: "batterycritical" event (6 -> 5), should not be fired if charging');
291 done();
292 }, 100);
293 });
294
295 });
296};
297
298//******************************************************************************************
299//***************************************Manual Tests***************************************
300//******************************************************************************************
301
302exports.defineManualTests = function (contentEl, createActionButton) {
303
304 /* Battery */
305 function updateInfo(info) {
306 document.getElementById('levelValue').innerText = info.level;
307 document.getElementById('pluggedValue').innerText = info.isPlugged;
308 if (info.level > 5) {
309 document.getElementById('criticalValue').innerText = "false";
310 }
311 if (info.level > 20) {
312 document.getElementById('lowValue').innerText = "false";
313 }
314 }
315
316 function batteryLow(info) {
317 document.getElementById('lowValue').innerText = "true";
318 }
319
320 function batteryCritical(info) {
321 document.getElementById('criticalValue').innerText = "true";
322 }
323
324 function addBattery() {
325 window.addEventListener("batterystatus", updateInfo, false);
326 }
327
328 function removeBattery() {
329 window.removeEventListener("batterystatus", updateInfo, false);
330 }
331
332 function addLow() {
333 window.addEventListener("batterylow", batteryLow, false);
334 }
335
336 function removeLow() {
337 window.removeEventListener("batterylow", batteryLow, false);
338 }
339
340 function addCritical() {
341 window.addEventListener("batterycritical", batteryCritical, false);
342 }
343
344 function removeCritical() {
345 window.removeEventListener("batterycritical", batteryCritical, false);
346 }
347
348 //Generate Dynamic Table
349 function generateTable(tableId, rows, cells, elements) {
350 var table = document.createElement('table');
351 for (var r = 0; r < rows; r++) {
352 var row = table.insertRow(r);
353 for (var c = 0; c < cells; c++) {
354 var cell = row.insertCell(c);
355 cell.setAttribute("align", "center");
356 for (var e in elements) {
357 if (elements[e].position.row == r && elements[e].position.cell == c) {
358 var htmlElement = document.createElement(elements[e].tag);
359 var content;
360
361 if (elements[e].content !== "") {
362 content = document.createTextNode(elements[e].content);
363 htmlElement.appendChild(content);
364 }
365 if (elements[e].type) {
366 htmlElement.type = elements[e].type;
367 }
368 htmlElement.setAttribute("id", elements[e].id);
369 cell.appendChild(htmlElement);
370 }
371 }
372 }
373 }
374 table.setAttribute("align", "center");
375 table.setAttribute("id", tableId);
376 return table;
377 }
378 // Battery Elements
379 var batteryElements =
380 [{
381 id : "statusTag",
382 content : "Status:",
383 tag : "div",
384 position : {
385 row : 0,
386 cell : 0
387 }
388 }, {
389 id : "statusValue",
390 content : "",
391 tag : "div",
392 position : {
393 row : 0,
394 cell : 1
395 }
396 }, {
397 id : "levelTag",
398 content : "Level:",
399 tag : "div",
400 position : {
401 row : 1,
402 cell : 0
403 }
404 }, {
405 id : "levelValue",
406 content : "",
407 tag : "div",
408 position : {
409 row : 1,
410 cell : 1
411 }
412 }, {
413 id : "pluggedTag",
414 content : "Plugged:",
415 tag : "div",
416 position : {
417 row : 2,
418 cell : 0
419 }
420 }, {
421 id : "pluggedValue",
422 content : "",
423 tag : "div",
424 position : {
425 row : 2,
426 cell : 1
427 }
428 }, {
429 id : "lowTag",
430 content : "Low:",
431 tag : "div",
432 position : {
433 row : 3,
434 cell : 0
435 }
436 }, {
437 id : "lowValue",
438 content : "",
439 tag : "div",
440 position : {
441 row : 3,
442 cell : 1
443 }
444 }, {
445 id : "criticalTag",
446 content : "Critical:",
447 tag : "div",
448 position : {
449 row : 4,
450 cell : 0
451 }
452 }, {
453 id : "criticalValue",
454 content : "",
455 tag : "div",
456 position : {
457 row : 4,
458 cell : 1
459 }
460 }
461 ];
462
463 //Title audio results
464 var div = document.createElement('h2');
465 div.appendChild(document.createTextNode('Battery Status'));
466 div.setAttribute("align", "center");
467 contentEl.appendChild(div);
468
469 var batteryTable = generateTable('info', 5, 3, batteryElements);
470 contentEl.appendChild(batteryTable);
471
472 div = document.createElement('h2');
473 div.appendChild(document.createTextNode('Actions'));
474 div.setAttribute("align", "center");
475 contentEl.appendChild(div);
476
477 contentEl.innerHTML += '<h3>Battery Status Tests</h3>' +
478 'Will update values for level and plugged when they change. If battery low and critical values are false, they will get updated in status box, but only once' +
479 '<div id="addBS"></div><div id="remBs"></div>' +
480 '<h3>Battery Low Tests</h3>' +
481 '</p> Will update value for battery low to true when battery is below 20%' +
482 '<div id="addBl"></div><div id="remBl"></div>' +
483 '<h3>Battery Critical Tests</h3>' +
484 '</p> Will update value for battery critical to true when battery is below 5%' +
485 '<div id="addBc"></div><div id="remBc"></div>';
486
487 createActionButton('Add "batterystatus" listener', function () {
488 addBattery();
489 }, 'addBS');
490 createActionButton('Remove "batterystatus" listener', function () {
491 removeBattery();
492 }, 'remBs');
493 createActionButton('Add "batterylow" listener', function () {
494 addLow();
495 }, 'addBl');
496 createActionButton('Remove "batterylow" listener', function () {
497 removeLow();
498 }, 'remBl');
499 createActionButton('Add "batterycritical" listener', function () {
500 addCritical();
501 }, 'addBc');
502 createActionButton('Remove "batterycritical" listener', function () {
503 removeCritical();
504 }, 'remBc');
505};