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..4b52d08aa3b129112418ca5c05b02f4945004513 |
--- /dev/null |
+++ b/LayoutTests/gamepad/page-visibility.html |
@@ -0,0 +1,99 @@ |
+<!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, 10); |
+} |
+ |
+function testWithPageVisibleAgain() { |
+ shouldBeEqualToString('document.visibilityState', 'hidden'); |
+ removeListeners(); |
+ |
+ var eventCount = 0; |
+ window.addEventListener('gamepadconnected', function(event) { |
+ testPassed('gamepadconnected dispatched after page become visible'); |
+ shouldBeEqualToString('document.visibilityState', 'visible'); |
+ shouldBeEqualToNumber('event.gamepad.index', 1); |
+ if (++eventCount < 2) |
+ return; |
+ if (eventCount > 2) |
+ testFailed("Only one gamepadconnected event should have been received. Redundant pending events (like connect and disconnect with the same gamepad) should be suppressed."); |
+ finishSoon(); // Give some time to fail if suppression doesn't work. |
+ }); |
+ window.addEventListener('gamepaddisconnected', function(event) { |
+ testPassed('gamepaddisconnected dispatched after visibility change'); |
+ shouldBeEqualToString('document.visibilityState', 'visible'); |
+ shouldBeEqualToNumber('event.gamepad.index', 0); |
+ if (++eventCount < 2) |
+ return; |
+ if (eventCount > 2) |
+ testFailed("Only one gamepaddisconnected event should have been received. Redundant pending events (like connect and disconnect with the same gamepad) should be suppressed."); |
+ finishSoon(); // Give some time to fail if suppression doesn't work. |
+ }); |
+ |
+ testRunner.setPageVisibility('visible'); |
+ |
+ setTimeout(function() { |
+ testFailed('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); |
+ |
+ // These should be suppressed. |
+ gamepadController.connect(1); |
+ gamepadController.dispatchConnected(1); |
+ gamepadController.disconnect(1); |
+ |
+ gamepadController.connect(1); |
+ gamepadController.dispatchConnected(1); |
+ |
+ 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); |
+ |
+</script> |
+</html> |