Index: LayoutTests/gamepad/page-visibility.html |
diff --git a/LayoutTests/gamepad/page-visibility.html b/LayoutTests/gamepad/page-visibility.html |
new file mode 100644 |
index 0000000000000000000000000000000000000000..bc3c69375bfa8856406947110f3e287485006a4b |
--- /dev/null |
+++ b/LayoutTests/gamepad/page-visibility.html |
@@ -0,0 +1,128 @@ |
+<!DOCTYPE html> |
+<html> |
+<body> |
+<script src="../resources/js-test.js"></script> |
+<script> |
+description("Tests that gamepad events are not fired when page is hidden but kept as pending and fired when the page becomes visible again."); |
+window.jsTestIsAsync = true; |
+ |
+var listeners; |
+function addListener(eventName, handler) { |
+ if (!listeners) |
+ listeners = new Array(); |
+ listeners.push({name: eventName, handler: handler}); |
+ window.addEventListener(eventName, handler); |
+} |
+function removeListeners() { |
+ if (!listeners) |
+ return; |
+ listeners.forEach(function (l) { |
+ window.removeEventListener(l.name, l.handler); |
+ }); |
+} |
+ |
+function finishSoon() { |
+ setTimeout(finishJSTest, 50); |
+} |
+ |
+var eventCounter = 0; |
+var expectations = [ |
+ { index: 0, connected: false, check: null }, |
+ { index: 1, connected: false, check: |
+ function(event) { |
+ shouldBeEqualToString('event.gamepad.id', "old"); |
+ } |
+ }, |
+ { index: 1, connected: true, check: |
+ function(event) { |
+ shouldBeEqualToString('event.gamepad.id', "new"); |
+ } |
+ }, |
+ { index: 2, connected: true, check: null } |
+]; |
+var expected; |
+ |
+function testWithPageVisibleAgain() { |
+ shouldBeEqualToString('document.visibilityState', 'hidden'); |
+ removeListeners(); |
+ |
+ window.addEventListener('gamepadconnected', function(event) { |
+ shouldBeTrue('eventCounter < expectations.length'); |
+ expected = expectations[eventCounter++]; |
+ shouldBeEqualToNumber('event.gamepad.index', expected.index); |
+ shouldBeTrue('expected.connected'); |
+ shouldBeTrue('event.gamepad.connected'); |
+ if (expected.check) |
+ expected.check(event); |
+ |
+ if (eventCounter == expectations.length) |
+ finishSoon(); // Give some time to fail if unexpecteds events are coming. |
+ }); |
+ window.addEventListener('gamepaddisconnected', function(event) { |
+ shouldBeTrue('eventCounter < expectations.length'); |
+ expected = expectations[eventCounter++]; |
+ shouldBeEqualToNumber('event.gamepad.index', expected.index); |
+ shouldBeFalse('expected.connected'); |
+ shouldBeFalse('event.gamepad.connected'); |
+ if (expected.check) |
+ expected.check(event); |
+ |
+ if (eventCounter == expectations.length) |
+ finishSoon(); // Give some time to fail if unexpecteds events are coming. |
+ }); |
+ |
+ testRunner.setPageVisibility('visible'); |
+ |
+ setTimeout(function() { |
+ testFailed('More gamepad events should have been received'); |
+ finishJSTest(); |
+ }, 100); |
+} |
+ |
+function testWithPageHidden() { |
+ testRunner.setPageVisibility('hidden'); |
+ var shouldNotReceive = function() { |
+ testFailed('Should not have received gamepad events while the page was hidden'); |
+ finishJSTest(); |
+ }; |
+ addListener('gamepadconnected', shouldNotReceive); |
+ addListener('gamepaddisconnected', shouldNotReceive); |
+ |
+ gamepadController.disconnect(0); |
+ |
+ // Both the disconnection and the connection should be dispatched. |
+ gamepadController.disconnect(1); |
+ gamepadController.setId(1, "new"); |
+ gamepadController.connect(1); |
+ gamepadController.dispatchConnected(1); |
+ |
+ // These should be suppressed. |
+ gamepadController.connect(2); |
+ gamepadController.dispatchConnected(2); |
+ gamepadController.disconnect(2); |
+ |
+ gamepadController.connect(2); |
+ gamepadController.dispatchConnected(2); |
+ |
+ setTimeout(testWithPageVisibleAgain, 0); |
+} |
+ |
+if (!window.testRunner || !window.gamepadController) { |
+ debug("This test cannot work without testRunner and gamepadController."); |
+} |
+ |
+addListener('gamepadconnected', function() { |
+ testPassed("Received a gamepadconnected event"); |
+ shouldBeEqualToNumber('event.gamepad.index', 0); |
+ removeListeners(); |
+ setTimeout(testWithPageHidden, 0); |
+}); |
+ |
+gamepadController.connect(0); |
+gamepadController.dispatchConnected(0); |
+gamepadController.setId(1, "old"); |
+gamepadController.connect(1); |
+gamepadController.dispatchConnected(1); |
+ |
+</script> |
+</html> |