OLD | NEW |
(Empty) | |
| 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 kep
t as pending and fired when the page becomes visible again."); |
| 7 window.jsTestIsAsync = true; |
| 8 |
| 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); |
| 15 } |
| 16 function removeListeners() { |
| 17 if (!listeners) |
| 18 return; |
| 19 listeners.forEach(function (l) { |
| 20 window.removeEventListener(l.name, l.handler); |
| 21 }); |
| 22 } |
| 23 |
| 24 function finishSoon() { |
| 25 setTimeout(finishJSTest, 50); |
| 26 } |
| 27 |
| 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"); |
| 34 } |
| 35 }, |
| 36 { index: 1, connected: true, check: |
| 37 function(event) { |
| 38 shouldBeEqualToString('event.gamepad.id', "new"); |
| 39 } |
| 40 }, |
| 41 { index: 2, connected: true, check: null } |
| 42 ]; |
| 43 var expected; |
| 44 |
| 45 function testWithPageVisibleAgain() { |
| 46 shouldBeEqualToString('document.visibilityState', 'hidden'); |
| 47 removeListeners(); |
| 48 |
| 49 window.addEventListener('gamepadconnected', function(event) { |
| 50 shouldBeTrue('eventCounter < expectations.length'); |
| 51 expected = expectations[eventCounter++]; |
| 52 shouldBeEqualToNumber('event.gamepad.index', expected.index); |
| 53 shouldBeTrue('expected.connected'); |
| 54 shouldBeTrue('event.gamepad.connected'); |
| 55 if (expected.check) |
| 56 expected.check(event); |
| 57 |
| 58 if (eventCounter == expectations.length) |
| 59 finishSoon(); // Give some time to fail if unexpecteds events are co
ming. |
| 60 }); |
| 61 window.addEventListener('gamepaddisconnected', function(event) { |
| 62 shouldBeTrue('eventCounter < expectations.length'); |
| 63 expected = expectations[eventCounter++]; |
| 64 shouldBeEqualToNumber('event.gamepad.index', expected.index); |
| 65 shouldBeFalse('expected.connected'); |
| 66 shouldBeFalse('event.gamepad.connected'); |
| 67 if (expected.check) |
| 68 expected.check(event); |
| 69 |
| 70 if (eventCounter == expectations.length) |
| 71 finishSoon(); // Give some time to fail if unexpecteds events are co
ming. |
| 72 }); |
| 73 |
| 74 testRunner.setPageVisibility('visible'); |
| 75 |
| 76 setTimeout(function() { |
| 77 testFailed('More gamepad events should have been received'); |
| 78 finishJSTest(); |
| 79 }, 100); |
| 80 } |
| 81 |
| 82 function testWithPageHidden() { |
| 83 testRunner.setPageVisibility('hidden'); |
| 84 var shouldNotReceive = function() { |
| 85 testFailed('Should not have received gamepad events while the page was h
idden'); |
| 86 finishJSTest(); |
| 87 }; |
| 88 addListener('gamepadconnected', shouldNotReceive); |
| 89 addListener('gamepaddisconnected', shouldNotReceive); |
| 90 |
| 91 gamepadController.disconnect(0); |
| 92 |
| 93 // Both the disconnection and the connection should be dispatched. |
| 94 gamepadController.disconnect(1); |
| 95 gamepadController.setId(1, "new"); |
| 96 gamepadController.connect(1); |
| 97 gamepadController.dispatchConnected(1); |
| 98 |
| 99 // These should be suppressed. |
| 100 gamepadController.connect(2); |
| 101 gamepadController.dispatchConnected(2); |
| 102 gamepadController.disconnect(2); |
| 103 |
| 104 gamepadController.connect(2); |
| 105 gamepadController.dispatchConnected(2); |
| 106 |
| 107 setTimeout(testWithPageVisibleAgain, 0); |
| 108 } |
| 109 |
| 110 if (!window.testRunner || !window.gamepadController) { |
| 111 debug("This test cannot work without testRunner and gamepadController."); |
| 112 } |
| 113 |
| 114 addListener('gamepadconnected', function() { |
| 115 testPassed("Received a gamepadconnected event"); |
| 116 shouldBeEqualToNumber('event.gamepad.index', 0); |
| 117 removeListeners(); |
| 118 setTimeout(testWithPageHidden, 0); |
| 119 }); |
| 120 |
| 121 gamepadController.connect(0); |
| 122 gamepadController.dispatchConnected(0); |
| 123 gamepadController.setId(1, "old"); |
| 124 gamepadController.connect(1); |
| 125 gamepadController.dispatchConnected(1); |
| 126 |
| 127 </script> |
| 128 </html> |
OLD | NEW |