OLD | NEW |
(Empty) | |
| 1 <html> |
| 2 <head> |
| 3 <script src="../../../resources/js-test.js"></script> |
| 4 </head> |
| 5 <body> |
| 6 <script> |
| 7 |
| 8 description('Tests using multiple event handlers for the Device Motion API.'); |
| 9 |
| 10 var mockEvent; |
| 11 var expectedEvent; |
| 12 function setMockMotion(accelerationX, accelerationY, accelerationZ, |
| 13 accelerationIncludingGravityX, accelerationIncludingGravi
tyY, accelerationIncludingGravityZ, |
| 14 rotationRateAlpha, rotationRateBeta, rotationRateGamma, |
| 15 interval) { |
| 16 |
| 17 mockEvent = {accelerationX: accelerationX, accelerationY: accelerationY, acc
elerationZ: accelerationZ, |
| 18 accelerationIncludingGravityX: accelerationIncludingGravityX, a
ccelerationIncludingGravityY: accelerationIncludingGravityY, accelerationIncludi
ngGravityZ: accelerationIncludingGravityZ, |
| 19 rotationRateAlpha: rotationRateAlpha, rotationRateBeta: rotatio
nRateBeta, rotationRateGamma: rotationRateGamma, |
| 20 interval: interval}; |
| 21 |
| 22 if (window.testRunner) |
| 23 testRunner.setMockDeviceMotion(true, mockEvent.accelerationX, true, mock
Event.accelerationY, true, mockEvent.accelerationZ, |
| 24 true, mockEvent.accelerationIncludingGrav
ityX, true, mockEvent.accelerationIncludingGravityY, true, mockEvent.acceleratio
nIncludingGravityZ, |
| 25 true, mockEvent.rotationRateAlpha, true,
mockEvent.rotationRateBeta, true, mockEvent.rotationRateGamma, |
| 26 interval); |
| 27 else |
| 28 debug('This test can not be run without the TestRunner'); |
| 29 } |
| 30 |
| 31 var deviceMotionEvent; |
| 32 function checkMotion(event) { |
| 33 deviceMotionEvent = event; |
| 34 shouldBe('deviceMotionEvent.acceleration.x', 'expectedEvent.accelerationX'); |
| 35 shouldBe('deviceMotionEvent.acceleration.y', 'expectedEvent.accelerationY'); |
| 36 shouldBe('deviceMotionEvent.acceleration.z', 'expectedEvent.accelerationZ'); |
| 37 |
| 38 shouldBe('deviceMotionEvent.accelerationIncludingGravity.x', 'expectedEvent.
accelerationIncludingGravityX'); |
| 39 shouldBe('deviceMotionEvent.accelerationIncludingGravity.y', 'expectedEvent.
accelerationIncludingGravityY'); |
| 40 shouldBe('deviceMotionEvent.accelerationIncludingGravity.z', 'expectedEvent.
accelerationIncludingGravityZ'); |
| 41 |
| 42 shouldBe('deviceMotionEvent.rotationRate.alpha', 'expectedEvent.rotationRate
Alpha'); |
| 43 shouldBe('deviceMotionEvent.rotationRate.beta', 'expectedEvent.rotationRateB
eta'); |
| 44 shouldBe('deviceMotionEvent.rotationRate.gamma', 'expectedEvent.rotationRate
Gamma'); |
| 45 |
| 46 shouldBe('deviceMotionEvent.interval', 'expectedEvent.interval'); |
| 47 } |
| 48 |
| 49 var counter = 0; |
| 50 function firstListener(event) { |
| 51 checkMotion(event); |
| 52 counter++; |
| 53 proceedIfNecessary(); |
| 54 } |
| 55 |
| 56 function secondListener(event) { |
| 57 checkMotion(event); |
| 58 counter++; |
| 59 proceedIfNecessary(); |
| 60 } |
| 61 |
| 62 function proceedIfNecessary() { |
| 63 if (counter == 2) { |
| 64 setMockMotion(11, 12, 13, |
| 65 14, 15, 16, |
| 66 17, 18, 19, |
| 67 0); |
| 68 // Note: this should not stop Device Motion updates, |
| 69 // because there is still one listener active. |
| 70 window.removeEventListener('devicemotion', secondListener); |
| 71 setTimeout(function(){initThirdListener();}, 0); |
| 72 } |
| 73 } |
| 74 |
| 75 var childFrame; |
| 76 function initThirdListener() { |
| 77 childFrame = document.createElement('iframe'); |
| 78 document.body.appendChild(childFrame); |
| 79 childFrame.contentWindow.addEventListener('devicemotion', thirdListener); |
| 80 } |
| 81 |
| 82 function thirdListener(event) { |
| 83 // Expect the cached event because Device Motion was already active |
| 84 // when third listener was added. |
| 85 checkMotion(event); |
| 86 window.removeEventListener('devicemotion', firstListener); |
| 87 childFrame.contentWindow.removeEventListener('devicemotion', thirdListener); |
| 88 setTimeout(function(){initFourthListener();}, 0); |
| 89 } |
| 90 |
| 91 function initFourthListener() { |
| 92 expectedEvent = mockEvent; |
| 93 window.addEventListener('devicemotion', fourthListener); |
| 94 } |
| 95 |
| 96 function fourthListener(event) { |
| 97 checkMotion(event); |
| 98 finishJSTest(); |
| 99 } |
| 100 |
| 101 setMockMotion(1, 2, 3, |
| 102 4, 5, 6, |
| 103 7, 8, 9, |
| 104 0); |
| 105 expectedEvent = mockEvent; |
| 106 window.addEventListener('devicemotion', firstListener); |
| 107 window.addEventListener('devicemotion', secondListener); |
| 108 |
| 109 window.jsTestIsAsync = true; |
| 110 |
| 111 </script> |
| 112 </body> |
| 113 </html> |
OLD | NEW |