tests.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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. /* eslint-env jasmine */
  22. /* global Windows, WinJS */
  23. exports.defineAutoTests = function () {
  24. var hasPowerManagerAPI = cordova.platformId === 'windows' && // eslint-disable-line no-undef
  25. Windows && Windows.System && Windows.System.Power &&
  26. Windows.System.Power.PowerManager;
  27. var batteryStatusUnsupported = (cordova.platformId === 'windows8' || // eslint-disable-line no-undef
  28. // We don't test battery status on Windows when there is no corresponding APIs available
  29. cordova.platformId === 'windows') && !(hasPowerManagerAPI || WinJS.Utilities.isPhone); // eslint-disable-line no-undef
  30. var onEvent;
  31. describe('Battery (navigator.battery)', function () {
  32. it('battery.spec.1 should exist', function () {
  33. if (batteryStatusUnsupported) {
  34. pending('Battery status is not supported on windows store');
  35. }
  36. expect(navigator.battery).toBeDefined();
  37. });
  38. });
  39. describe('Battery Events', function () {
  40. describe('batterystatus', function () {
  41. afterEach(function () {
  42. if (!batteryStatusUnsupported) {
  43. try {
  44. window.removeEventListener('batterystatus', onEvent, false);
  45. } catch (e) {
  46. console.error('Error removing batterystatus event listener: ' + e);
  47. }
  48. }
  49. });
  50. it('battery.spec.2 should fire batterystatus events', function (done) {
  51. if (batteryStatusUnsupported) {
  52. pending('Battery status is not supported on windows store');
  53. }
  54. onEvent = jasmine.createSpy('BatteryStatus');
  55. // batterystatus -> 30
  56. window.addEventListener('batterystatus', onEvent, false);
  57. navigator.battery._status({
  58. level: 30,
  59. isPlugged: false
  60. });
  61. setTimeout(function () {
  62. expect(onEvent).toHaveBeenCalled();
  63. done();
  64. }, 100);
  65. });
  66. });
  67. describe('batterylow', function () {
  68. afterEach(function () {
  69. if (!batteryStatusUnsupported) {
  70. try {
  71. window.removeEventListener('batterylow', onEvent, false);
  72. } catch (e) {
  73. console.error('Error removing batterylow event listener: ' + e);
  74. }
  75. }
  76. });
  77. it('battery.spec.3 should fire batterylow event (30 -> 20)', function (done) {
  78. if (batteryStatusUnsupported) {
  79. pending('Battery status is not supported on windows store');
  80. }
  81. onEvent = jasmine.createSpy('BatteryLow');
  82. // batterylow 30 -> 20
  83. window.addEventListener('batterylow', onEvent, false);
  84. navigator.battery._status({
  85. level: 30,
  86. isPlugged: false
  87. });
  88. navigator.battery._status({
  89. level: 20,
  90. isPlugged: false
  91. });
  92. setTimeout(function () {
  93. expect(onEvent).toHaveBeenCalled();
  94. done();
  95. }, 100);
  96. });
  97. it('battery.spec.3.1 should fire batterylow event (30 -> 19)', function (done) {
  98. if (batteryStatusUnsupported) {
  99. pending('Battery status is not supported on windows store');
  100. }
  101. onEvent = jasmine.createSpy('BatteryLow');
  102. // batterylow 30 -> 19
  103. window.addEventListener('batterylow', onEvent, false);
  104. navigator.battery._status({
  105. level: 30,
  106. isPlugged: false
  107. });
  108. navigator.battery._status({
  109. level: 19,
  110. isPlugged: false
  111. });
  112. setTimeout(function () {
  113. expect(onEvent).toHaveBeenCalled();
  114. done();
  115. }, 100);
  116. });
  117. it('battery.spec.3.2 should not fire batterylow event (5 -> 20)', function (done) {
  118. if (batteryStatusUnsupported) {
  119. pending('Battery status is not supported on windows store');
  120. }
  121. onEvent = jasmine.createSpy('BatteryLow');
  122. // batterylow should not fire when level increases (5->20) ( CB-4519 )
  123. window.addEventListener('batterylow', onEvent, false);
  124. navigator.battery._status({
  125. level: 5,
  126. isPlugged: false
  127. });
  128. navigator.battery._status({
  129. level: 20,
  130. isPlugged: false
  131. });
  132. setTimeout(function () {
  133. expect(onEvent).not.toHaveBeenCalled();
  134. done();
  135. }, 100);
  136. });
  137. it('battery.spec.3.3 batterylow event(21 -> 20) should not fire if charging', function (done) {
  138. if (batteryStatusUnsupported) {
  139. pending('Battery status is not supported on windows store');
  140. }
  141. onEvent = jasmine.createSpy('BatteryLow');
  142. // batterylow should NOT fire if we are charging ( CB-4520 )
  143. window.addEventListener('batterylow', onEvent, false);
  144. navigator.battery._status({
  145. level: 21,
  146. isPlugged: true
  147. });
  148. navigator.battery._status({
  149. level: 20,
  150. isPlugged: true
  151. });
  152. setTimeout(function () {
  153. expect(onEvent).not.toHaveBeenCalled();
  154. done();
  155. }, 100);
  156. });
  157. });
  158. describe('batterycritical', function () {
  159. afterEach(function () {
  160. if (!batteryStatusUnsupported) {
  161. try {
  162. window.removeEventListener('batterycritical', onEvent, false);
  163. } catch (e) {
  164. console.error('Error removing batterycritical event listener: ' + e);
  165. }
  166. }
  167. });
  168. it('battery.spec.4 should fire batterycritical event (19 -> 5)', function (done) {
  169. if (batteryStatusUnsupported) {
  170. pending('Battery status is not supported on windows store');
  171. }
  172. onEvent = jasmine.createSpy('BatteryCritical');
  173. // batterycritical 19->5
  174. window.addEventListener('batterycritical', onEvent, false);
  175. navigator.battery._status({
  176. level: 19,
  177. isPlugged: false
  178. });
  179. navigator.battery._status({
  180. level: 5,
  181. isPlugged: false
  182. });
  183. setTimeout(function () {
  184. expect(onEvent).toHaveBeenCalled();
  185. done();
  186. }, 100);
  187. });
  188. it('battery.spec.4.1 should fire batterycritical event (19 -> 4)', function (done) {
  189. if (batteryStatusUnsupported) {
  190. pending('Battery status is not supported on windows store');
  191. }
  192. onEvent = jasmine.createSpy('BatteryCritical');
  193. // batterycritical 19->4
  194. window.addEventListener('batterycritical', onEvent, false);
  195. navigator.battery._status({
  196. level: 19,
  197. isPlugged: false
  198. });
  199. navigator.battery._status({
  200. level: 4,
  201. isPlugged: false
  202. });
  203. setTimeout(function () {
  204. expect(onEvent).toHaveBeenCalled();
  205. done();
  206. }, 100);
  207. });
  208. it('battery.spec.4.2 should fire batterycritical event (100 -> 4) when decreases', function (done) {
  209. if (batteryStatusUnsupported) {
  210. pending('Battery status is not supported on windows store');
  211. }
  212. onEvent = jasmine.createSpy('BatteryCritical');
  213. // setup: batterycritical should fire when level decreases (100->4) ( CB-4519 )
  214. window.addEventListener('batterycritical', onEvent, false);
  215. navigator.battery._status({
  216. level: 100,
  217. isPlugged: false
  218. });
  219. navigator.battery._status({
  220. level: 4,
  221. isPlugged: false
  222. });
  223. setTimeout(function () {
  224. expect(onEvent).toHaveBeenCalled();
  225. done();
  226. }, 100);
  227. });
  228. it('battery.spec.4.3 should not fire batterycritical event (4 -> 5) when increasing', function (done) {
  229. if (batteryStatusUnsupported) {
  230. pending('Battery status is not supported on windows store');
  231. }
  232. onEvent = jasmine.createSpy('BatteryCritical');
  233. window.addEventListener('batterycritical', onEvent, false);
  234. // batterycritical should not fire when level increases (4->5)( CB-4519 )
  235. navigator.battery._status({
  236. level: 4,
  237. isPlugged: false
  238. });
  239. navigator.battery._status({
  240. level: 5,
  241. isPlugged: false
  242. });
  243. setTimeout(function () {
  244. expect(onEvent.calls.count()).toBeLessThan(2);
  245. done();
  246. }, 100);
  247. });
  248. it('battery.spec.4.4 should not fire batterycritical event (6 -> 5) if charging', function (done) {
  249. if (batteryStatusUnsupported) {
  250. pending('Battery status is not supported on windows store');
  251. }
  252. onEvent = jasmine.createSpy('BatteryCritical');
  253. window.addEventListener('batterycritical', onEvent, false);
  254. // batterycritical should NOT fire if we are charging ( CB-4520 )
  255. navigator.battery._status({
  256. level: 6,
  257. isPlugged: true
  258. });
  259. navigator.battery._status({
  260. level: 5,
  261. isPlugged: true
  262. });
  263. setTimeout(function () {
  264. expect(onEvent).not.toHaveBeenCalled();
  265. done();
  266. }, 100);
  267. });
  268. });
  269. });
  270. };
  271. //* *****************************************************************************************
  272. //* **************************************Manual Tests***************************************
  273. //* *****************************************************************************************
  274. exports.defineManualTests = function (contentEl, createActionButton) {
  275. /* Battery */
  276. function updateInfo (info) {
  277. document.getElementById('levelValue').innerText = info.level;
  278. document.getElementById('pluggedValue').innerText = info.isPlugged;
  279. if (info.level > 5) {
  280. document.getElementById('criticalValue').innerText = 'false';
  281. }
  282. if (info.level > 20) {
  283. document.getElementById('lowValue').innerText = 'false';
  284. }
  285. }
  286. function batteryLow (info) {
  287. document.getElementById('lowValue').innerText = 'true';
  288. }
  289. function batteryCritical (info) {
  290. document.getElementById('criticalValue').innerText = 'true';
  291. }
  292. function addBattery () {
  293. window.addEventListener('batterystatus', updateInfo, false);
  294. }
  295. function removeBattery () {
  296. window.removeEventListener('batterystatus', updateInfo, false);
  297. }
  298. function addLow () {
  299. window.addEventListener('batterylow', batteryLow, false);
  300. }
  301. function removeLow () {
  302. window.removeEventListener('batterylow', batteryLow, false);
  303. }
  304. function addCritical () {
  305. window.addEventListener('batterycritical', batteryCritical, false);
  306. }
  307. function removeCritical () {
  308. window.removeEventListener('batterycritical', batteryCritical, false);
  309. }
  310. // Generate Dynamic Table
  311. function generateTable (tableId, rows, cells, elements) {
  312. var table = document.createElement('table');
  313. for (var r = 0; r < rows; r++) {
  314. var row = table.insertRow(r);
  315. for (var c = 0; c < cells; c++) {
  316. var cell = row.insertCell(c);
  317. cell.setAttribute('align', 'center');
  318. for (var e in elements) {
  319. if (elements[e].position.row === r && elements[e].position.cell === c) {
  320. var htmlElement = document.createElement(elements[e].tag);
  321. var content;
  322. if (elements[e].content !== '') {
  323. content = document.createTextNode(elements[e].content);
  324. htmlElement.appendChild(content);
  325. }
  326. if (elements[e].type) {
  327. htmlElement.type = elements[e].type;
  328. }
  329. htmlElement.setAttribute('id', elements[e].id);
  330. cell.appendChild(htmlElement);
  331. }
  332. }
  333. }
  334. }
  335. table.setAttribute('align', 'center');
  336. table.setAttribute('id', tableId);
  337. return table;
  338. }
  339. // Battery Elements
  340. var batteryElements =
  341. [{
  342. id: 'statusTag',
  343. content: 'Status:',
  344. tag: 'div',
  345. position: {
  346. row: 0,
  347. cell: 0
  348. }
  349. }, {
  350. id: 'statusValue',
  351. content: '',
  352. tag: 'div',
  353. position: {
  354. row: 0,
  355. cell: 1
  356. }
  357. }, {
  358. id: 'levelTag',
  359. content: 'Level:',
  360. tag: 'div',
  361. position: {
  362. row: 1,
  363. cell: 0
  364. }
  365. }, {
  366. id: 'levelValue',
  367. content: '',
  368. tag: 'div',
  369. position: {
  370. row: 1,
  371. cell: 1
  372. }
  373. }, {
  374. id: 'pluggedTag',
  375. content: 'Plugged:',
  376. tag: 'div',
  377. position: {
  378. row: 2,
  379. cell: 0
  380. }
  381. }, {
  382. id: 'pluggedValue',
  383. content: '',
  384. tag: 'div',
  385. position: {
  386. row: 2,
  387. cell: 1
  388. }
  389. }, {
  390. id: 'lowTag',
  391. content: 'Low:',
  392. tag: 'div',
  393. position: {
  394. row: 3,
  395. cell: 0
  396. }
  397. }, {
  398. id: 'lowValue',
  399. content: '',
  400. tag: 'div',
  401. position: {
  402. row: 3,
  403. cell: 1
  404. }
  405. }, {
  406. id: 'criticalTag',
  407. content: 'Critical:',
  408. tag: 'div',
  409. position: {
  410. row: 4,
  411. cell: 0
  412. }
  413. }, {
  414. id: 'criticalValue',
  415. content: '',
  416. tag: 'div',
  417. position: {
  418. row: 4,
  419. cell: 1
  420. }
  421. }
  422. ];
  423. // Title audio results
  424. var div = document.createElement('h2');
  425. div.appendChild(document.createTextNode('Battery Status'));
  426. div.setAttribute('align', 'center');
  427. contentEl.appendChild(div);
  428. var batteryTable = generateTable('info', 5, 3, batteryElements);
  429. contentEl.appendChild(batteryTable);
  430. div = document.createElement('h2');
  431. div.appendChild(document.createTextNode('Actions'));
  432. div.setAttribute('align', 'center');
  433. contentEl.appendChild(div);
  434. contentEl.innerHTML += '<h3>Battery Status Tests</h3>' +
  435. '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' +
  436. '<div id="addBS"></div><div id="remBs"></div>' +
  437. '<h3>Battery Low Tests</h3>' +
  438. '</p> Will update value for battery low to true when battery is below 20%' +
  439. '<div id="addBl"></div><div id="remBl"></div>' +
  440. '<h3>Battery Critical Tests</h3>' +
  441. '</p> Will update value for battery critical to true when battery is below 5%' +
  442. '<div id="addBc"></div><div id="remBc"></div>';
  443. createActionButton('Add "batterystatus" listener', function () {
  444. addBattery();
  445. }, 'addBS');
  446. createActionButton('Remove "batterystatus" listener', function () {
  447. removeBattery();
  448. }, 'remBs');
  449. createActionButton('Add "batterylow" listener', function () {
  450. addLow();
  451. }, 'addBl');
  452. createActionButton('Remove "batterylow" listener', function () {
  453. removeLow();
  454. }, 'remBl');
  455. createActionButton('Add "batterycritical" listener', function () {
  456. addCritical();
  457. }, 'addBc');
  458. createActionButton('Remove "batterycritical" listener', function () {
  459. removeCritical();
  460. }, 'remBc');
  461. };