Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(28)

Unified Diff: LayoutTests/fast/dom/DeviceMotion/script-tests/multiple-event-listeners.js

Issue 240993003: Fix the issue with adding/removing event listeners for Device Motion/Orientation. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: similarity=80 Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: LayoutTests/fast/dom/DeviceMotion/script-tests/multiple-event-listeners.js
diff --git a/LayoutTests/fast/dom/DeviceMotion/script-tests/multiple-event-listeners.js b/LayoutTests/fast/dom/DeviceMotion/script-tests/multiple-event-listeners.js
new file mode 100644
index 0000000000000000000000000000000000000000..7f0968f15061bdc192ac8235a54235e8b1d0f289
--- /dev/null
+++ b/LayoutTests/fast/dom/DeviceMotion/script-tests/multiple-event-listeners.js
@@ -0,0 +1,108 @@
+description('Tests using multiple event handlers for the Device Motion API.');
+
+var mockEvent;
+var expectedEvent;
+function setMockMotion(accelerationX, accelerationY, accelerationZ,
Peter Beverloo 2014/04/17 14:48:17 Can you try to increase focus of this test? It's v
timvolodine 2014/04/17 15:34:13 Done.
+ accelerationIncludingGravityX, accelerationIncludingGravityY, accelerationIncludingGravityZ,
+ rotationRateAlpha, rotationRateBeta, rotationRateGamma,
+ interval) {
+
+ mockEvent = {accelerationX: accelerationX, accelerationY: accelerationY, accelerationZ: accelerationZ,
+ accelerationIncludingGravityX: accelerationIncludingGravityX, accelerationIncludingGravityY: accelerationIncludingGravityY, accelerationIncludingGravityZ: accelerationIncludingGravityZ,
+ rotationRateAlpha: rotationRateAlpha, rotationRateBeta: rotationRateBeta, rotationRateGamma: rotationRateGamma,
+ interval: interval};
+
+ if (window.testRunner)
+ testRunner.setMockDeviceMotion(null != mockEvent.accelerationX, null == mockEvent.accelerationX ? 0 : mockEvent.accelerationX,
+ null != mockEvent.accelerationY, null == mockEvent.accelerationY ? 0 : mockEvent.accelerationY,
+ null != mockEvent.accelerationZ, null == mockEvent.accelerationZ ? 0 : mockEvent.accelerationZ,
+ null != mockEvent.accelerationIncludingGravityX, null == mockEvent.accelerationIncludingGravityX ? 0 : mockEvent.accelerationIncludingGravityX,
+ null != mockEvent.accelerationIncludingGravityY, null == mockEvent.accelerationIncludingGravityY ? 0 : mockEvent.accelerationIncludingGravityY,
+ null != mockEvent.accelerationIncludingGravityZ, null == mockEvent.accelerationIncludingGravityZ ? 0 : mockEvent.accelerationIncludingGravityZ,
+ null != mockEvent.rotationRateAlpha, null == mockEvent.rotationRateAlpha ? 0 : mockEvent.rotationRateAlpha,
+ null != mockEvent.rotationRateBeta, null == mockEvent.rotationRateBeta ? 0 : mockEvent.rotationRateBeta,
+ null != mockEvent.rotationRateGamma, null == mockEvent.rotationRateGamma ? 0 : mockEvent.rotationRateGamma,
+ interval);
+ else
+ debug('This test can not be run without the TestRunner');
+}
+
+var deviceMotionEvent;
+function checkMotion(event) {
+ deviceMotionEvent = event;
+ shouldBe('deviceMotionEvent.acceleration.x', 'expectedEvent.accelerationX');
+ shouldBe('deviceMotionEvent.acceleration.y', 'expectedEvent.accelerationY');
+ shouldBe('deviceMotionEvent.acceleration.z', 'expectedEvent.accelerationZ');
+
+ shouldBe('deviceMotionEvent.accelerationIncludingGravity.x', 'expectedEvent.accelerationIncludingGravityX');
+ shouldBe('deviceMotionEvent.accelerationIncludingGravity.y', 'expectedEvent.accelerationIncludingGravityY');
+ shouldBe('deviceMotionEvent.accelerationIncludingGravity.z', 'expectedEvent.accelerationIncludingGravityZ');
+
+ shouldBe('deviceMotionEvent.rotationRate.alpha', 'expectedEvent.rotationRateAlpha');
+ shouldBe('deviceMotionEvent.rotationRate.beta', 'expectedEvent.rotationRateBeta');
+ shouldBe('deviceMotionEvent.rotationRate.gamma', 'expectedEvent.rotationRateGamma');
+
+ shouldBe('deviceMotionEvent.interval', 'expectedEvent.interval');
+}
+
+var counter = 0;
+function firstListener(event) {
+ checkMotion(event);
+ counter++;
+ proceedIfNecessary();
+}
+
+function secondListener(event) {
+ checkMotion(event);
+ counter++;
+ proceedIfNecessary();
+}
+
+function proceedIfNecessary() {
+ if (counter == 2) {
+ setMockMotion(11, 12, 13,
+ 14, 15, 16,
+ 17, 18, 19,
+ 0);
+ // Note: this should not stop Device Motion updates,
+ // because there is still one listener active.
+ window.removeEventListener('devicemotion', secondListener);
+ setTimeout(function(){initThirdListener();}, 0);
+ }
+}
+
+var childFrame;
+function initThirdListener() {
+ childFrame = document.createElement('iframe');
+ document.body.appendChild(childFrame);
+ childFrame.contentWindow.addEventListener('devicemotion', thirdListener);
+}
+
+function thirdListener(event) {
+ // Expect the cached event because Device Motion was already active
+ // when third listener was added.
+ checkMotion(event);
+ window.removeEventListener('devicemotion', firstListener);
+ childFrame.contentWindow.removeEventListener('devicemotion', thirdListener);
+ setTimeout(function(){initFourthListener();}, 0);
+}
+
+function initFourthListener() {
+ expectedEvent = mockEvent;
+ window.addEventListener('devicemotion', fourthListener);
+}
+
+function fourthListener(event) {
+ checkMotion(event);
+ finishJSTest();
+}
+
+setMockMotion(1, 2, 3,
+ 4, 5, 6,
+ 7, 8, 9,
+ 0);
+expectedEvent = mockEvent;
+window.addEventListener('devicemotion', firstListener);
+window.addEventListener('devicemotion', secondListener);
+
+window.jsTestIsAsync = true;

Powered by Google App Engine
This is Rietveld 408576698