4 <script src=
"../resources/js-test.js"></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;
10 function addListener(eventName
, handler
) {
12 listeners
= new Array();
13 listeners
.push({name
: eventName
, handler
: handler
});
14 window
.addEventListener(eventName
, handler
);
16 function removeListeners() {
19 listeners
.forEach(function (l
) {
20 window
.removeEventListener(l
.name
, l
.handler
);
24 function finishSoon() {
25 setTimeout(finishJSTest
, 50);
30 { index
: 0, connected
: false, check
: null },
31 { index
: 1, connected
: false, check
:
33 shouldBeEqualToString('event.gamepad.id', "old");
36 { index
: 1, connected
: true, check
:
38 shouldBeEqualToString('event.gamepad.id', "new");
41 { index
: 2, connected
: true, check
: null }
45 function allTestsCompleted() {
46 return eventCounter
== expectations
.length
;
49 function testWithPageVisibleAgain() {
50 shouldBeEqualToString('document.visibilityState', 'hidden');
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');
60 expected
.check(event
);
62 if (allTestsCompleted())
63 finishSoon(); // Give some time to fail if unexpecteds events are coming.
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');
72 expected
.check(event
);
74 if (allTestsCompleted())
75 finishSoon(); // Give some time to fail if unexpecteds events are coming.
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');
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);
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);