Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(135)

Side by Side Diff: LayoutTests/battery-status/promise-with-eventlisteners.html

Issue 329723005: Battery Status API: blink promise implementation and layout tests. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: revert to a single resolver at a time Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 <!DOCTYPE html>
2 <html>
3 <body>
4 <script src="../resources/js-test.js"></script>
5 <script>
6 description("Test promise resolution and event listeners callbacks.");
7
8 if (!window.testRunner)
9 debug('This test cannot be run without the TestRunner');
10
11 jsTestIsAsync = true;
12
13 var mockBatteryInfo;
14 function setAndFireMockBatteryInfo(charging, chargingTime, dischargingTime, leve l) {
15 mockBatteryInfo = { charging: charging,
16 chargingTime: chargingTime,
17 dischargingTime: dischargingTime,
18 level: level };
19 testRunner.didChangeBatteryStatus(charging, chargingTime, dischargingTime, l evel);
20 }
21
22 // compare obtained battery values with the mock values
23 function checkBatteryInfo(batteryManager) {
24 batteryInfo = batteryManager;
25 shouldBeDefined("batteryInfo");
26 shouldBeDefined("mockBatteryInfo");
27 shouldBe('batteryInfo.charging', 'mockBatteryInfo.charging');
28 shouldBe('batteryInfo.chargingTime', 'mockBatteryInfo.chargingTime');
29 shouldBe('batteryInfo.dischargingTime', 'mockBatteryInfo.dischargingTime');
30 shouldBe('batteryInfo.level', 'mockBatteryInfo.level');
31 }
32
33 var battery;
34 function batteryStatusSuccess(batteryManager) {
35 debug('batteryStatusSuccess invoked');
36 battery = batteryManager;
37 checkBatteryInfo(battery);
38
39 battery.addEventListener('chargingchange', onChargingChange);
40 battery.addEventListener('dischargingtimechange', onDischargingTimeChange);
41 battery.addEventListener('chargingtimechange', onChargingTimeChange);
42 battery.addEventListener('levelchange', onLevelChange);
43
44 setAndFireMockBatteryInfo(true, 11, 22, 0.6);
45 }
46
47 var chargingChanged = 0;
48 var chargingTimeChanged = 0;
49 var dischargingTimeChanged = 0;
50 var levelChanged = 0;
51
52 function onChargingChange() {
53 debug('chargingchange invoked');
54 if (this !== battery) { testFailed('this !== battery'); }
55 checkBatteryInfo(this);
56 chargingChanged++;
57 finishIfReady();
58 }
59
60 function onChargingTimeChange() {
61 debug('chargingtimechange invoked');
62 if (this !== battery) { testFailed('this !== battery'); }
63 checkBatteryInfo(this);
64 chargingTimeChanged++;
65 finishIfReady();
66 }
67
68 function onDischargingTimeChange() {
69 debug('dischargingtimechange invoked');
70 if (this !== battery) { testFailed('this !== battery'); }
71 checkBatteryInfo(this);
72 dischargingTimeChanged++;
73 finishIfReady();
74 }
75
76 function onLevelChange() {
77 debug('levelchange invoked');
78 if (this !== battery) { testFailed('this !== battery'); }
79 checkBatteryInfo(this);
80 levelChanged++;
81 finishIfReady();
82 }
83
84 function cleanupAndFinish() {
85 battery.removeEventListener('chargingchange', onChargingChange);
86 battery.removeEventListener('dischargingtimechange', onDischargingTimeChange );
87 battery.removeEventListener('chargingtimechange', onChargingTimeChange);
88 battery.removeEventListener('levelchange', onLevelChange);
89
90 // stops updating battery and clears current battery status.
91 window.testRunner.setPageVisibility("hidden");
92 finishJSTest();
93 }
94
95 function finishIfReady() {
96 if (chargingChanged == 1 && chargingTimeChanged == 1 && dischargingTimeChang ed == 1 && levelChanged == 1) {
97 setTimeout(cleanupAndFinish, 0);
98 }
99 }
100
101 function batteryStatusFailure() {
102 testFailed('failed to successfully resolve the promise');
103 setTimeout(cleanupAndFinish, 0);
104 }
105
106 navigator.getBattery().then(batteryStatusSuccess, batteryStatusFailure);
107 setAndFireMockBatteryInfo(false, 10, 20, 0.5);
108 </script>
109 </body>
110 </html>
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698