3 <script src="../../../resources/js-test.js"></script>
8 description('Tests using multiple event handlers for the Device Motion API.');
12 function setMockMotion(accelerationX, accelerationY, accelerationZ,
13 accelerationIncludingGravityX, accelerationIncludingGravityY, accelerationIncludingGravityZ,
14 rotationRateAlpha, rotationRateBeta, rotationRateGamma,
17 mockEvent = {accelerationX: accelerationX, accelerationY: accelerationY, accelerationZ: accelerationZ,
18 accelerationIncludingGravityX: accelerationIncludingGravityX, accelerationIncludingGravityY: accelerationIncludingGravityY, accelerationIncludingGravityZ: accelerationIncludingGravityZ,
19 rotationRateAlpha: rotationRateAlpha, rotationRateBeta: rotationRateBeta, rotationRateGamma: rotationRateGamma,
22 if (window.testRunner)
23 testRunner.setMockDeviceMotion(true, mockEvent.accelerationX, true, mockEvent.accelerationY, true, mockEvent.accelerationZ,
24 true, mockEvent.accelerationIncludingGravityX, true, mockEvent.accelerationIncludingGravityY, true, mockEvent.accelerationIncludingGravityZ,
25 true, mockEvent.rotationRateAlpha, true, mockEvent.rotationRateBeta, true, mockEvent.rotationRateGamma,
28 debug('This test can not be run without the TestRunner');
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');
38 shouldBe('deviceMotionEvent.accelerationIncludingGravity.x', 'expectedEvent.accelerationIncludingGravityX');
39 shouldBe('deviceMotionEvent.accelerationIncludingGravity.y', 'expectedEvent.accelerationIncludingGravityY');
40 shouldBe('deviceMotionEvent.accelerationIncludingGravity.z', 'expectedEvent.accelerationIncludingGravityZ');
42 shouldBe('deviceMotionEvent.rotationRate.alpha', 'expectedEvent.rotationRateAlpha');
43 shouldBe('deviceMotionEvent.rotationRate.beta', 'expectedEvent.rotationRateBeta');
44 shouldBe('deviceMotionEvent.rotationRate.gamma', 'expectedEvent.rotationRateGamma');
46 shouldBe('deviceMotionEvent.interval', 'expectedEvent.interval');
50 function firstListener(event) {
56 function secondListener(event) {
62 function proceedIfNecessary() {
64 setMockMotion(11, 12, 13,
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);
76 function initThirdListener() {
77 childFrame = document.createElement('iframe');
78 document.body.appendChild(childFrame);
79 childFrame.contentWindow.addEventListener('devicemotion', thirdListener);
82 function thirdListener(event) {
83 // Expect the cached event because Device Motion was already active
84 // when third listener was added.
86 window.removeEventListener('devicemotion', firstListener);
87 childFrame.contentWindow.removeEventListener('devicemotion', thirdListener);
88 setTimeout(function(){initFourthListener();}, 0);
91 function initFourthListener() {
92 expectedEvent = mockEvent;
93 window.addEventListener('devicemotion', fourthListener);
96 function fourthListener(event) {
101 setMockMotion(1, 2, 3,
105 expectedEvent = mockEvent;
106 window.addEventListener('devicemotion', firstListener);
107 window.addEventListener('devicemotion', secondListener);
109 window.jsTestIsAsync = true;