Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / battery-status / multiple-windows-page-visibility.html
1 <!DOCTYPE html>
2 <html>
3 <body>
4 <script src="../resources/js-test.js"></script>
5 <script>
6 description("Test multiple windows with page visibility.");
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 battery;
44 function batteryStatusSuccess(batteryManager) {
45     battery = batteryManager;
46     debug('resolution in window');
47     checkBatteryInfo(battery);
48     battery.addEventListener('levelchange', failAndFinish);
49
50     firstWindow = window;
51     secondWindow = window.open("");
52     secondWindow.navigator.getBattery().then(
53         function(battery2) {
54             debug('resolution in secondWindow');
55             checkBatteryInfo(battery2);
56             setTimeout(fireNewMockLevel, 0);
57         }, batteryStatusFailure);
58
59     window.testRunner.setPageVisibility("hidden");
60     debug("first window: page is hidden");
61 }
62
63 function fireNewMockLevel() {
64     setAndFireMockBatteryInfo(false, 10, 20, 0.6);
65     proceedToVisible();
66 }
67
68 function proceedToVisible() {
69     battery.removeEventListener('levelchange', failAndFinish);
70     battery.addEventListener('levelchange', onLevelChange);
71     testRunner.setPageVisibility("visible");
72     debug("first window: page is visible");
73 }
74
75 function onLevelChange() {
76     checkBatteryInfo(battery);
77     battery.removeEventListener('levelchange', onLevelChange);
78     setTimeout(finishJSTest, 0);
79 }
80
81 function failAndFinish() {
82     testFailed('received event while the page was hidden');
83     setTimeout(finishJSTest, 0);
84 }
85
86 debug("first window: page is visible");
87 navigator.getBattery().then(batteryStatusSuccess, batteryStatusFailure);
88 setAndFireMockBatteryInfo(false, 10, 20, 0.5);
89 </script>
90 </body>
91 </html>