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