Upstream version 8.37.180.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / battery-status / multiple-promises-after-resolve.html
1 <!DOCTYPE html>
2 <html>
3 <body>
4 <script src="../resources/js-test.js"></script>
5 <script>
6 description("Test battery status API with multiple promises after resolve.");
7
8 if (!window.testRunner)
9     debug('This test cannot be run without the TestRunner');
10
11 testRunner.waitUntilDone();
12 jsTestIsAsync = true;
13
14 var mockBatteryInfo;
15 function setAndFireMockBatteryInfo(charging, chargingTime, dischargingTime, level) {
16     mockBatteryInfo = { charging: charging,
17                         chargingTime: chargingTime,
18                         dischargingTime: dischargingTime,
19                         level: level };
20     testRunner.didChangeBatteryStatus(charging, chargingTime, dischargingTime, level);
21 }
22
23 // compare obtained battery values with the mock values
24 function checkBatteryInfo(batteryManager) {
25     batteryInfo = batteryManager;
26     shouldBeDefined("batteryInfo");
27     shouldBeDefined("mockBatteryInfo");
28     shouldBe('batteryInfo.charging', 'mockBatteryInfo.charging');
29     shouldBe('batteryInfo.chargingTime', 'mockBatteryInfo.chargingTime');
30     shouldBe('batteryInfo.dischargingTime', 'mockBatteryInfo.dischargingTime');
31     shouldBe('batteryInfo.level', 'mockBatteryInfo.level');
32 }
33
34 function batteryStatusFailure() {
35     testFailed('failed to successfully resolve the promise');
36     finishJSTest();
37 }
38
39 function batteryStatusSuccess(battery) {
40     debug('resolution number 1');
41     checkBatteryInfo(battery);
42
43     navigator.getBattery().then(
44         function(battery) {
45             debug('resolution number 2');
46             checkBatteryInfo(battery);
47             setTimeout(cleanupAndFinish, 0);
48         }, batteryStatusFailure);
49 }
50
51 function cleanupAndFinish() {
52     // stops updating battery and clears current battery status.
53     window.testRunner.setPageVisibility("hidden");
54     finishJSTest();
55 }
56
57 navigator.getBattery().then(batteryStatusSuccess, batteryStatusFailure);
58 setAndFireMockBatteryInfo(false, 10, 20, 0.5);
59 </script>
60 </body>
61 </html>