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

Unified Diff: LayoutTests/fast/dom/DeviceOrientation/multiple-event-listeners.html

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: added fix and test for device orientation 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/DeviceOrientation/multiple-event-listeners.html
diff --git a/LayoutTests/fast/dom/DeviceOrientation/multiple-event-listeners.html b/LayoutTests/fast/dom/DeviceOrientation/multiple-event-listeners.html
new file mode 100644
index 0000000000000000000000000000000000000000..36b0e1b8d51c2f370477e008e2791aa84771d5cd
--- /dev/null
+++ b/LayoutTests/fast/dom/DeviceOrientation/multiple-event-listeners.html
@@ -0,0 +1,88 @@
+<html>
+<head>
+<script src="../../../resources/js-test.js"></script>
+</head>
+<body>
+<script>
+
+description('Tests using multiple event handlers for the Device Orientation API.');
+
+var mockEvent;
+var expectedEvent;
+function setMockOrientation(alpha, beta, gamma, absolute) {
+ mockEvent = {alpha: alpha, beta: beta, gamma: gamma, absolute: absolute};
+ if (window.testRunner)
+ testRunner.setMockDeviceOrientation(
+ true, mockEvent.alpha, true, mockEvent.beta, true, mockEvent.gamma, true, mockEvent.absolute);
+ else
+ debug('This test can not be run without the TestRunner');
+}
+
+var deviceOrientationEvent;
+function checkOrientation(event) {
+ deviceOrientationEvent = event;
+ shouldBe('deviceOrientationEvent.alpha', 'expectedEvent.alpha');
+ shouldBe('deviceOrientationEvent.beta', 'expectedEvent.beta');
+ shouldBe('deviceOrientationEvent.gamma', 'expectedEvent.gamma');
+ shouldBe('deviceOrientationEvent.absolute', 'expectedEvent.absolute');
+}
+
+var counter = 0;
+function firstListener(event) {
+ checkOrientation(event);
+ counter++;
+ proceedIfNecessary();
+}
+
+function secondListener(event) {
+ checkOrientation(event);
+ counter++;
+ proceedIfNecessary();
+}
+
+function proceedIfNecessary() {
+ if (counter == 2) {
+ setMockOrientation(11, 12, 13, true);
+ // Note: this should not stop Device Orientation updates,
+ // because there is still one listener active.
+ window.removeEventListener('deviceorientation', secondListener);
+ setTimeout(function(){initThirdListener();}, 0);
+ }
+}
+
+var childFrame;
+function initThirdListener() {
+ childFrame = document.createElement('iframe');
+ document.body.appendChild(childFrame);
+ childFrame.contentWindow.addEventListener('deviceorientation', thirdListener);
+}
+
+function thirdListener(event) {
+ // Expect the cached event because Device Orientation was already active
+ // when third listener was added.
+ checkOrientation(event);
+ window.removeEventListener('deviceorientation', firstListener);
+ childFrame.contentWindow.removeEventListener('deviceorientation', thirdListener);
+ setTimeout(function(){initFourthListener();}, 0);
+}
+
+function initFourthListener() {
+ expectedEvent = mockEvent;
+ window.addEventListener('deviceorientation', fourthListener);
+}
+
+function fourthListener(event) {
+ checkOrientation(event);
+ finishJSTest();
+}
+
+setMockOrientation(1, 2, 3, true);
+expectedEvent = mockEvent;
+window.addEventListener('deviceorientation', firstListener);
+window.addEventListener('deviceorientation', secondListener);
+
+window.jsTestIsAsync = true;
+
+</script>
+</body>
+</html>

Powered by Google App Engine
This is Rietveld 408576698