Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / fast / dom / DeviceMotion / multiple-event-listeners.html
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, accelerationIncludingGravityY, accelerationIncludingGravityZ,
14                        rotationRateAlpha, rotationRateBeta, rotationRateGamma,
15                        interval) {
16
17     mockEvent = {accelerationX: accelerationX, accelerationY: accelerationY, accelerationZ: accelerationZ,
18                  accelerationIncludingGravityX: accelerationIncludingGravityX, accelerationIncludingGravityY: accelerationIncludingGravityY, accelerationIncludingGravityZ: accelerationIncludingGravityZ,
19                  rotationRateAlpha: rotationRateAlpha, rotationRateBeta: rotationRateBeta, rotationRateGamma: rotationRateGamma,
20                  interval: interval};
21
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,
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.rotationRateAlpha');
43     shouldBe('deviceMotionEvent.rotationRate.beta', 'expectedEvent.rotationRateBeta');
44     shouldBe('deviceMotionEvent.rotationRate.gamma', 'expectedEvent.rotationRateGamma');
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>