a8eb5fdcedd5d87c27429a7b0461621ee510b2e3
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / battery-status / multiple-promises.html
1 <!DOCTYPE html>
2 <html>
3 <body>
4 <script src="../resources/js-test.js"></script>
5 <script>
6 description("Test multiple promise resolution.");
7
8 if (!window.testRunner)
9     debug('This test cannot be run without the TestRunner');
10
11 // Clean-up any unused battery manager objects from previous tests.
12 gc();
13 jsTestIsAsync = true;
14 testRunner.waitUntilDone();
15 testRunner.setCanOpenWindows();
16 testRunner.setCloseRemainingWindowsWhenComplete(true);
17
18 var mockBatteryInfo;
19 function setAndFireMockBatteryInfo(charging, chargingTime, dischargingTime, level) {
20     mockBatteryInfo = { charging: charging,
21                         chargingTime: chargingTime,
22                         dischargingTime: dischargingTime,
23                         level: level };
24     testRunner.didChangeBatteryStatus(charging, chargingTime, dischargingTime, level);
25 }
26
27 // compare obtained battery values with the mock values
28 function checkBatteryInfo(batteryManager) {
29     batteryInfo = batteryManager;
30     shouldBeDefined("batteryInfo");
31     shouldBeDefined("mockBatteryInfo");
32     shouldBe('batteryInfo.charging', 'mockBatteryInfo.charging');
33     shouldBe('batteryInfo.chargingTime', 'mockBatteryInfo.chargingTime');
34     shouldBe('batteryInfo.dischargingTime', 'mockBatteryInfo.dischargingTime');
35     shouldBe('batteryInfo.level', 'mockBatteryInfo.level');
36 }
37
38 function batteryStatusFailure() {
39     testFailed('failed to successfully resolve the promise');
40     setTimeout(finishJSTest, 0);
41 }
42
43 var promise1Count = 0;
44 var promise2Count = 0;
45
46 function finishIfReady() {
47     if (promise1Count == 1 && promise2Count == 1)
48         setTimeout(finishJSTest, 0);
49 }
50
51 promise1 = navigator.getBattery().then(
52     function(battery) {
53         debug('first resolution');
54         checkBatteryInfo(battery);
55         promise1Count++;
56         finishIfReady();
57     }, batteryStatusFailure);
58
59 promise2 = navigator.getBattery().then(
60     function(battery) {
61         debug('second resolution');
62         checkBatteryInfo(battery);
63         promise2Count++;
64         finishIfReady();
65     }, batteryStatusFailure);
66
67 shouldBeFalse('promise1 === promise2');
68 setAndFireMockBatteryInfo(false, 10, 20, 0.5);
69 </script>
70 </body>
71 </html>