Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / gamepad / page-visibility.html
blob6cce422375abd7fd13bd60cff103ded4fca53fc8
1 <!DOCTYPE html>
2 <html>
3 <body>
4 <script src="../resources/js-test.js"></script>
5 <script>
6 description("Tests that gamepad events are not fired when page is hidden but kept as pending and fired when the page becomes visible again.");
7 window.jsTestIsAsync = true;
9 var listeners;
10 function addListener(eventName, handler) {
11 if (!listeners)
12 listeners = new Array();
13 listeners.push({name: eventName, handler: handler});
14 window.addEventListener(eventName, handler);
16 function removeListeners() {
17 if (!listeners)
18 return;
19 listeners.forEach(function (l) {
20 window.removeEventListener(l.name, l.handler);
21 });
24 function finishSoon() {
25 setTimeout(finishJSTest, 50);
28 var eventCounter = 0;
29 var expectations = [
30 { index: 0, connected: false, check: null },
31 { index: 1, connected: false, check:
32 function(event) {
33 shouldBeEqualToString('event.gamepad.id', "old");
36 { index: 1, connected: true, check:
37 function(event) {
38 shouldBeEqualToString('event.gamepad.id', "new");
41 { index: 2, connected: true, check: null }
43 var expected;
45 function allTestsCompleted() {
46 return eventCounter == expectations.length;
49 function testWithPageVisibleAgain() {
50 shouldBeEqualToString('document.visibilityState', 'hidden');
51 removeListeners();
53 window.addEventListener('gamepadconnected', function(event) {
54 shouldBeTrue('eventCounter < expectations.length');
55 expected = expectations[eventCounter++];
56 shouldBeEqualToNumber('event.gamepad.index', expected.index);
57 shouldBeTrue('expected.connected');
58 shouldBeTrue('event.gamepad.connected');
59 if (expected.check)
60 expected.check(event);
62 if (allTestsCompleted())
63 finishSoon(); // Give some time to fail if unexpecteds events are coming.
64 });
65 window.addEventListener('gamepaddisconnected', function(event) {
66 shouldBeTrue('eventCounter < expectations.length');
67 expected = expectations[eventCounter++];
68 shouldBeEqualToNumber('event.gamepad.index', expected.index);
69 shouldBeFalse('expected.connected');
70 shouldBeFalse('event.gamepad.connected');
71 if (expected.check)
72 expected.check(event);
74 if (allTestsCompleted())
75 finishSoon(); // Give some time to fail if unexpecteds events are coming.
76 });
78 testRunner.setPageVisibility('visible');
81 function testWithPageHidden() {
82 testRunner.setPageVisibility('hidden');
83 var shouldNotReceive = function() {
84 testFailed('Should not have received gamepad events while the page was hidden');
85 finishJSTest();
87 addListener('gamepadconnected', shouldNotReceive);
88 addListener('gamepaddisconnected', shouldNotReceive);
90 gamepadController.disconnect(0);
92 // Both the disconnection and the connection should be dispatched.
93 gamepadController.disconnect(1);
94 gamepadController.setId(1, "new");
95 gamepadController.connect(1);
96 gamepadController.dispatchConnected(1);
98 // These should be suppressed.
99 gamepadController.connect(2);
100 gamepadController.dispatchConnected(2);
101 gamepadController.disconnect(2);
103 gamepadController.connect(2);
104 gamepadController.dispatchConnected(2);
106 setTimeout(testWithPageVisibleAgain, 0);
109 if (!window.testRunner || !window.gamepadController) {
110 debug("This test cannot work without testRunner and gamepadController.");
113 addListener('gamepadconnected', function() {
114 testPassed("Received a gamepadconnected event");
115 shouldBeEqualToNumber('event.gamepad.index', 0);
116 removeListeners();
117 setTimeout(testWithPageHidden, 0);
120 gamepadController.connect(0);
121 gamepadController.dispatchConnected(0);
122 gamepadController.setId(1, "old");
123 gamepadController.connect(1);
124 gamepadController.dispatchConnected(1);
126 </script>
127 </html>