Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(164)

Unified Diff: LayoutTests/gamepad/page-visibility.html

Issue 306363002: Gamepad: honor page visibility (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: just a comment that it will be better with Oilpan Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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>

Powered by Google App Engine
This is Rietveld 408576698