Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | // Simple gestures tests |
michael@0 | 6 | // |
michael@0 | 7 | // These tests require the ability to disable the fact that the |
michael@0 | 8 | // Firefox chrome intentionally prevents "simple gesture" events from |
michael@0 | 9 | // reaching web content. |
michael@0 | 10 | |
michael@0 | 11 | let test_utils; |
michael@0 | 12 | let test_commandset; |
michael@0 | 13 | let test_prefBranch = "browser.gesture."; |
michael@0 | 14 | |
michael@0 | 15 | function test() |
michael@0 | 16 | { |
michael@0 | 17 | waitForExplicitFinish(); |
michael@0 | 18 | |
michael@0 | 19 | // Disable the default gestures support during the test |
michael@0 | 20 | gGestureSupport.init(false); |
michael@0 | 21 | |
michael@0 | 22 | test_utils = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor). |
michael@0 | 23 | getInterface(Components.interfaces.nsIDOMWindowUtils); |
michael@0 | 24 | |
michael@0 | 25 | // Run the tests of "simple gesture" events generally |
michael@0 | 26 | test_EnsureConstantsAreDisjoint(); |
michael@0 | 27 | test_TestEventListeners(); |
michael@0 | 28 | test_TestEventCreation(); |
michael@0 | 29 | |
michael@0 | 30 | // Reenable the default gestures support. The remaining tests target |
michael@0 | 31 | // the Firefox gesture functionality. |
michael@0 | 32 | gGestureSupport.init(true); |
michael@0 | 33 | |
michael@0 | 34 | // Test Firefox's gestures support. |
michael@0 | 35 | test_commandset = document.getElementById("mainCommandSet"); |
michael@0 | 36 | test_swipeGestures(); |
michael@0 | 37 | test_latchedGesture("pinch", "out", "in", "MozMagnifyGesture"); |
michael@0 | 38 | test_thresholdGesture("pinch", "out", "in", "MozMagnifyGesture"); |
michael@0 | 39 | test_rotateGestures(); |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | let test_eventCount = 0; |
michael@0 | 43 | let test_expectedType; |
michael@0 | 44 | let test_expectedDirection; |
michael@0 | 45 | let test_expectedDelta; |
michael@0 | 46 | let test_expectedModifiers; |
michael@0 | 47 | let test_expectedClickCount; |
michael@0 | 48 | let test_imageTab; |
michael@0 | 49 | |
michael@0 | 50 | function test_gestureListener(evt) |
michael@0 | 51 | { |
michael@0 | 52 | is(evt.type, test_expectedType, |
michael@0 | 53 | "evt.type (" + evt.type + ") does not match expected value"); |
michael@0 | 54 | is(evt.target, test_utils.elementFromPoint(20, 20, false, false), |
michael@0 | 55 | "evt.target (" + evt.target + ") does not match expected value"); |
michael@0 | 56 | is(evt.clientX, 20, |
michael@0 | 57 | "evt.clientX (" + evt.clientX + ") does not match expected value"); |
michael@0 | 58 | is(evt.clientY, 20, |
michael@0 | 59 | "evt.clientY (" + evt.clientY + ") does not match expected value"); |
michael@0 | 60 | isnot(evt.screenX, 0, |
michael@0 | 61 | "evt.screenX (" + evt.screenX + ") does not match expected value"); |
michael@0 | 62 | isnot(evt.screenY, 0, |
michael@0 | 63 | "evt.screenY (" + evt.screenY + ") does not match expected value"); |
michael@0 | 64 | |
michael@0 | 65 | is(evt.direction, test_expectedDirection, |
michael@0 | 66 | "evt.direction (" + evt.direction + ") does not match expected value"); |
michael@0 | 67 | is(evt.delta, test_expectedDelta, |
michael@0 | 68 | "evt.delta (" + evt.delta + ") does not match expected value"); |
michael@0 | 69 | |
michael@0 | 70 | is(evt.shiftKey, (test_expectedModifiers & Components.interfaces.nsIDOMEvent.SHIFT_MASK) != 0, |
michael@0 | 71 | "evt.shiftKey did not match expected value"); |
michael@0 | 72 | is(evt.ctrlKey, (test_expectedModifiers & Components.interfaces.nsIDOMEvent.CONTROL_MASK) != 0, |
michael@0 | 73 | "evt.ctrlKey did not match expected value"); |
michael@0 | 74 | is(evt.altKey, (test_expectedModifiers & Components.interfaces.nsIDOMEvent.ALT_MASK) != 0, |
michael@0 | 75 | "evt.altKey did not match expected value"); |
michael@0 | 76 | is(evt.metaKey, (test_expectedModifiers & Components.interfaces.nsIDOMEvent.META_MASK) != 0, |
michael@0 | 77 | "evt.metaKey did not match expected value"); |
michael@0 | 78 | |
michael@0 | 79 | if (evt.type == "MozTapGesture") { |
michael@0 | 80 | is(evt.clickCount, test_expectedClickCount, "evt.clickCount does not match"); |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | test_eventCount++; |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | function test_helper1(type, direction, delta, modifiers) |
michael@0 | 87 | { |
michael@0 | 88 | // Setup the expected values |
michael@0 | 89 | test_expectedType = type; |
michael@0 | 90 | test_expectedDirection = direction; |
michael@0 | 91 | test_expectedDelta = delta; |
michael@0 | 92 | test_expectedModifiers = modifiers; |
michael@0 | 93 | |
michael@0 | 94 | let expectedEventCount = test_eventCount + 1; |
michael@0 | 95 | |
michael@0 | 96 | document.addEventListener(type, test_gestureListener, true); |
michael@0 | 97 | test_utils.sendSimpleGestureEvent(type, 20, 20, direction, delta, modifiers); |
michael@0 | 98 | document.removeEventListener(type, test_gestureListener, true); |
michael@0 | 99 | |
michael@0 | 100 | is(expectedEventCount, test_eventCount, "Event (" + type + ") was never received by event listener"); |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | function test_clicks(type, clicks) |
michael@0 | 104 | { |
michael@0 | 105 | // Setup the expected values |
michael@0 | 106 | test_expectedType = type; |
michael@0 | 107 | test_expectedDirection = 0; |
michael@0 | 108 | test_expectedDelta = 0; |
michael@0 | 109 | test_expectedModifiers = 0; |
michael@0 | 110 | test_expectedClickCount = clicks; |
michael@0 | 111 | |
michael@0 | 112 | let expectedEventCount = test_eventCount + 1; |
michael@0 | 113 | |
michael@0 | 114 | document.addEventListener(type, test_gestureListener, true); |
michael@0 | 115 | test_utils.sendSimpleGestureEvent(type, 20, 20, 0, 0, 0, clicks); |
michael@0 | 116 | document.removeEventListener(type, test_gestureListener, true); |
michael@0 | 117 | |
michael@0 | 118 | is(expectedEventCount, test_eventCount, "Event (" + type + ") was never received by event listener"); |
michael@0 | 119 | } |
michael@0 | 120 | |
michael@0 | 121 | function test_TestEventListeners() |
michael@0 | 122 | { |
michael@0 | 123 | let e = test_helper1; // easier to type this name |
michael@0 | 124 | |
michael@0 | 125 | // Swipe gesture animation events |
michael@0 | 126 | e("MozSwipeGestureStart", 0, -0.7, 0); |
michael@0 | 127 | e("MozSwipeGestureUpdate", 0, -0.4, 0); |
michael@0 | 128 | e("MozSwipeGestureEnd", 0, 0, 0); |
michael@0 | 129 | e("MozSwipeGestureStart", 0, 0.6, 0); |
michael@0 | 130 | e("MozSwipeGestureUpdate", 0, 0.3, 0); |
michael@0 | 131 | e("MozSwipeGestureEnd", 0, 1, 0); |
michael@0 | 132 | |
michael@0 | 133 | // Swipe gesture event |
michael@0 | 134 | e("MozSwipeGesture", SimpleGestureEvent.DIRECTION_LEFT, 0.0, 0); |
michael@0 | 135 | e("MozSwipeGesture", SimpleGestureEvent.DIRECTION_RIGHT, 0.0, 0); |
michael@0 | 136 | e("MozSwipeGesture", SimpleGestureEvent.DIRECTION_UP, 0.0, 0); |
michael@0 | 137 | e("MozSwipeGesture", SimpleGestureEvent.DIRECTION_DOWN, 0.0, 0); |
michael@0 | 138 | e("MozSwipeGesture", |
michael@0 | 139 | SimpleGestureEvent.DIRECTION_UP | SimpleGestureEvent.DIRECTION_LEFT, 0.0, 0); |
michael@0 | 140 | e("MozSwipeGesture", |
michael@0 | 141 | SimpleGestureEvent.DIRECTION_DOWN | SimpleGestureEvent.DIRECTION_RIGHT, 0.0, 0); |
michael@0 | 142 | e("MozSwipeGesture", |
michael@0 | 143 | SimpleGestureEvent.DIRECTION_UP | SimpleGestureEvent.DIRECTION_RIGHT, 0.0, 0); |
michael@0 | 144 | e("MozSwipeGesture", |
michael@0 | 145 | SimpleGestureEvent.DIRECTION_DOWN | SimpleGestureEvent.DIRECTION_LEFT, 0.0, 0); |
michael@0 | 146 | |
michael@0 | 147 | // magnify gesture events |
michael@0 | 148 | e("MozMagnifyGestureStart", 0, 50.0, 0); |
michael@0 | 149 | e("MozMagnifyGestureUpdate", 0, -25.0, 0); |
michael@0 | 150 | e("MozMagnifyGestureUpdate", 0, 5.0, 0); |
michael@0 | 151 | e("MozMagnifyGesture", 0, 30.0, 0); |
michael@0 | 152 | |
michael@0 | 153 | // rotate gesture events |
michael@0 | 154 | e("MozRotateGestureStart", SimpleGestureEvent.ROTATION_CLOCKWISE, 33.0, 0); |
michael@0 | 155 | e("MozRotateGestureUpdate", SimpleGestureEvent.ROTATION_COUNTERCLOCKWISE, -13.0, 0); |
michael@0 | 156 | e("MozRotateGestureUpdate", SimpleGestureEvent.ROTATION_CLOCKWISE, 13.0, 0); |
michael@0 | 157 | e("MozRotateGesture", SimpleGestureEvent.ROTATION_CLOCKWISE, 33.0, 0); |
michael@0 | 158 | |
michael@0 | 159 | // Tap and presstap gesture events |
michael@0 | 160 | test_clicks("MozTapGesture", 1); |
michael@0 | 161 | test_clicks("MozTapGesture", 2); |
michael@0 | 162 | test_clicks("MozTapGesture", 3); |
michael@0 | 163 | test_clicks("MozPressTapGesture", 1); |
michael@0 | 164 | |
michael@0 | 165 | // simple delivery test for edgeui gestures |
michael@0 | 166 | e("MozEdgeUIStarted", 0, 0, 0); |
michael@0 | 167 | e("MozEdgeUICanceled", 0, 0, 0); |
michael@0 | 168 | e("MozEdgeUICompleted", 0, 0, 0); |
michael@0 | 169 | |
michael@0 | 170 | // event.shiftKey |
michael@0 | 171 | let modifier = Components.interfaces.nsIDOMEvent.SHIFT_MASK; |
michael@0 | 172 | e("MozSwipeGesture", SimpleGestureEvent.DIRECTION_RIGHT, 0, modifier); |
michael@0 | 173 | |
michael@0 | 174 | // event.metaKey |
michael@0 | 175 | modifier = Components.interfaces.nsIDOMEvent.META_MASK; |
michael@0 | 176 | e("MozSwipeGesture", SimpleGestureEvent.DIRECTION_RIGHT, 0, modifier); |
michael@0 | 177 | |
michael@0 | 178 | // event.altKey |
michael@0 | 179 | modifier = Components.interfaces.nsIDOMEvent.ALT_MASK; |
michael@0 | 180 | e("MozSwipeGesture", SimpleGestureEvent.DIRECTION_RIGHT, 0, modifier); |
michael@0 | 181 | |
michael@0 | 182 | // event.ctrlKey |
michael@0 | 183 | modifier = Components.interfaces.nsIDOMEvent.CONTROL_MASK; |
michael@0 | 184 | e("MozSwipeGesture", SimpleGestureEvent.DIRECTION_RIGHT, 0, modifier); |
michael@0 | 185 | } |
michael@0 | 186 | |
michael@0 | 187 | function test_eventDispatchListener(evt) |
michael@0 | 188 | { |
michael@0 | 189 | test_eventCount++; |
michael@0 | 190 | evt.stopPropagation(); |
michael@0 | 191 | } |
michael@0 | 192 | |
michael@0 | 193 | function test_helper2(type, direction, delta, altKey, ctrlKey, shiftKey, metaKey) |
michael@0 | 194 | { |
michael@0 | 195 | let event = null; |
michael@0 | 196 | let successful; |
michael@0 | 197 | |
michael@0 | 198 | try { |
michael@0 | 199 | event = document.createEvent("SimpleGestureEvent"); |
michael@0 | 200 | successful = true; |
michael@0 | 201 | } |
michael@0 | 202 | catch (ex) { |
michael@0 | 203 | successful = false; |
michael@0 | 204 | } |
michael@0 | 205 | ok(successful, "Unable to create SimpleGestureEvent"); |
michael@0 | 206 | |
michael@0 | 207 | try { |
michael@0 | 208 | event.initSimpleGestureEvent(type, true, true, window, 1, |
michael@0 | 209 | 10, 10, 10, 10, |
michael@0 | 210 | ctrlKey, altKey, shiftKey, metaKey, |
michael@0 | 211 | 1, window, |
michael@0 | 212 | 0, direction, delta, 0); |
michael@0 | 213 | successful = true; |
michael@0 | 214 | } |
michael@0 | 215 | catch (ex) { |
michael@0 | 216 | successful = false; |
michael@0 | 217 | } |
michael@0 | 218 | ok(successful, "event.initSimpleGestureEvent should not fail"); |
michael@0 | 219 | |
michael@0 | 220 | // Make sure the event fields match the expected values |
michael@0 | 221 | is(event.type, type, "Mismatch on evt.type"); |
michael@0 | 222 | is(event.direction, direction, "Mismatch on evt.direction"); |
michael@0 | 223 | is(event.delta, delta, "Mismatch on evt.delta"); |
michael@0 | 224 | is(event.altKey, altKey, "Mismatch on evt.altKey"); |
michael@0 | 225 | is(event.ctrlKey, ctrlKey, "Mismatch on evt.ctrlKey"); |
michael@0 | 226 | is(event.shiftKey, shiftKey, "Mismatch on evt.shiftKey"); |
michael@0 | 227 | is(event.metaKey, metaKey, "Mismatch on evt.metaKey"); |
michael@0 | 228 | is(event.view, window, "Mismatch on evt.view"); |
michael@0 | 229 | is(event.detail, 1, "Mismatch on evt.detail"); |
michael@0 | 230 | is(event.clientX, 10, "Mismatch on evt.clientX"); |
michael@0 | 231 | is(event.clientY, 10, "Mismatch on evt.clientY"); |
michael@0 | 232 | is(event.screenX, 10, "Mismatch on evt.screenX"); |
michael@0 | 233 | is(event.screenY, 10, "Mismatch on evt.screenY"); |
michael@0 | 234 | is(event.button, 1, "Mismatch on evt.button"); |
michael@0 | 235 | is(event.relatedTarget, window, "Mismatch on evt.relatedTarget"); |
michael@0 | 236 | |
michael@0 | 237 | // Test event dispatch |
michael@0 | 238 | let expectedEventCount = test_eventCount + 1; |
michael@0 | 239 | document.addEventListener(type, test_eventDispatchListener, true); |
michael@0 | 240 | document.dispatchEvent(event); |
michael@0 | 241 | document.removeEventListener(type, test_eventDispatchListener, true); |
michael@0 | 242 | is(expectedEventCount, test_eventCount, "Dispatched event was never received by listener"); |
michael@0 | 243 | } |
michael@0 | 244 | |
michael@0 | 245 | function test_TestEventCreation() |
michael@0 | 246 | { |
michael@0 | 247 | // Event creation |
michael@0 | 248 | test_helper2("MozMagnifyGesture", SimpleGestureEvent.DIRECTION_RIGHT, 20.0, |
michael@0 | 249 | true, false, true, false); |
michael@0 | 250 | test_helper2("MozMagnifyGesture", SimpleGestureEvent.DIRECTION_LEFT, -20.0, |
michael@0 | 251 | false, true, false, true); |
michael@0 | 252 | } |
michael@0 | 253 | |
michael@0 | 254 | function test_EnsureConstantsAreDisjoint() |
michael@0 | 255 | { |
michael@0 | 256 | let up = SimpleGestureEvent.DIRECTION_UP; |
michael@0 | 257 | let down = SimpleGestureEvent.DIRECTION_DOWN; |
michael@0 | 258 | let left = SimpleGestureEvent.DIRECTION_LEFT; |
michael@0 | 259 | let right = SimpleGestureEvent.DIRECTION_RIGHT; |
michael@0 | 260 | |
michael@0 | 261 | let clockwise = SimpleGestureEvent.ROTATION_CLOCKWISE; |
michael@0 | 262 | let cclockwise = SimpleGestureEvent.ROTATION_COUNTERCLOCKWISE; |
michael@0 | 263 | |
michael@0 | 264 | ok(up ^ down, "DIRECTION_UP and DIRECTION_DOWN are not bitwise disjoint"); |
michael@0 | 265 | ok(up ^ left, "DIRECTION_UP and DIRECTION_LEFT are not bitwise disjoint"); |
michael@0 | 266 | ok(up ^ right, "DIRECTION_UP and DIRECTION_RIGHT are not bitwise disjoint"); |
michael@0 | 267 | ok(down ^ left, "DIRECTION_DOWN and DIRECTION_LEFT are not bitwise disjoint"); |
michael@0 | 268 | ok(down ^ right, "DIRECTION_DOWN and DIRECTION_RIGHT are not bitwise disjoint"); |
michael@0 | 269 | ok(left ^ right, "DIRECTION_LEFT and DIRECTION_RIGHT are not bitwise disjoint"); |
michael@0 | 270 | ok(clockwise ^ cclockwise, "ROTATION_CLOCKWISE and ROTATION_COUNTERCLOCKWISE are not bitwise disjoint"); |
michael@0 | 271 | } |
michael@0 | 272 | |
michael@0 | 273 | // Helper for test of latched event processing. Emits the actual |
michael@0 | 274 | // gesture events to test whether the commands associated with the |
michael@0 | 275 | // gesture will only trigger once for each direction of movement. |
michael@0 | 276 | function test_emitLatchedEvents(eventPrefix, initialDelta, cmd) |
michael@0 | 277 | { |
michael@0 | 278 | let cumulativeDelta = 0; |
michael@0 | 279 | let isIncreasing = initialDelta > 0; |
michael@0 | 280 | |
michael@0 | 281 | let expect = {}; |
michael@0 | 282 | // Reset the call counters and initialize expected values |
michael@0 | 283 | for (let dir in cmd) |
michael@0 | 284 | cmd[dir].callCount = expect[dir] = 0; |
michael@0 | 285 | |
michael@0 | 286 | let check = function(aDir, aMsg) ok(cmd[aDir].callCount == expect[aDir], aMsg); |
michael@0 | 287 | let checkBoth = function(aNum, aInc, aDec) { |
michael@0 | 288 | let prefix = "Step " + aNum + ": "; |
michael@0 | 289 | check("inc", prefix + aInc); |
michael@0 | 290 | check("dec", prefix + aDec); |
michael@0 | 291 | }; |
michael@0 | 292 | |
michael@0 | 293 | // Send the "Start" event. |
michael@0 | 294 | test_utils.sendSimpleGestureEvent(eventPrefix + "Start", 0, 0, 0, initialDelta, 0); |
michael@0 | 295 | cumulativeDelta += initialDelta; |
michael@0 | 296 | if (isIncreasing) { |
michael@0 | 297 | expect.inc++; |
michael@0 | 298 | checkBoth(1, "Increasing command was not triggered", "Decreasing command was triggered"); |
michael@0 | 299 | } else { |
michael@0 | 300 | expect.dec++; |
michael@0 | 301 | checkBoth(1, "Increasing command was triggered", "Decreasing command was not triggered"); |
michael@0 | 302 | } |
michael@0 | 303 | |
michael@0 | 304 | // Send random values in the same direction and ensure neither |
michael@0 | 305 | // command triggers. |
michael@0 | 306 | for (let i = 0; i < 5; i++) { |
michael@0 | 307 | let delta = Math.random() * (isIncreasing ? 100 : -100); |
michael@0 | 308 | test_utils.sendSimpleGestureEvent(eventPrefix + "Update", 0, 0, 0, delta, 0); |
michael@0 | 309 | cumulativeDelta += delta; |
michael@0 | 310 | checkBoth(2, "Increasing command was triggered", "Decreasing command was triggered"); |
michael@0 | 311 | } |
michael@0 | 312 | |
michael@0 | 313 | // Now go back in the opposite direction. |
michael@0 | 314 | test_utils.sendSimpleGestureEvent(eventPrefix + "Update", 0, 0, 0, |
michael@0 | 315 | - initialDelta, 0); |
michael@0 | 316 | cumulativeDelta += - initialDelta; |
michael@0 | 317 | if (isIncreasing) { |
michael@0 | 318 | expect.dec++; |
michael@0 | 319 | checkBoth(3, "Increasing command was triggered", "Decreasing command was not triggered"); |
michael@0 | 320 | } else { |
michael@0 | 321 | expect.inc++; |
michael@0 | 322 | checkBoth(3, "Increasing command was not triggered", "Decreasing command was triggered"); |
michael@0 | 323 | } |
michael@0 | 324 | |
michael@0 | 325 | // Send random values in the opposite direction and ensure neither |
michael@0 | 326 | // command triggers. |
michael@0 | 327 | for (let i = 0; i < 5; i++) { |
michael@0 | 328 | let delta = Math.random() * (isIncreasing ? -100 : 100); |
michael@0 | 329 | test_utils.sendSimpleGestureEvent(eventPrefix + "Update", 0, 0, 0, delta, 0); |
michael@0 | 330 | cumulativeDelta += delta; |
michael@0 | 331 | checkBoth(4, "Increasing command was triggered", "Decreasing command was triggered"); |
michael@0 | 332 | } |
michael@0 | 333 | |
michael@0 | 334 | // Go back to the original direction. The original command should trigger. |
michael@0 | 335 | test_utils.sendSimpleGestureEvent(eventPrefix + "Update", 0, 0, 0, |
michael@0 | 336 | initialDelta, 0); |
michael@0 | 337 | cumulativeDelta += initialDelta; |
michael@0 | 338 | if (isIncreasing) { |
michael@0 | 339 | expect.inc++; |
michael@0 | 340 | checkBoth(5, "Increasing command was not triggered", "Decreasing command was triggered"); |
michael@0 | 341 | } else { |
michael@0 | 342 | expect.dec++; |
michael@0 | 343 | checkBoth(5, "Increasing command was triggered", "Decreasing command was not triggered"); |
michael@0 | 344 | } |
michael@0 | 345 | |
michael@0 | 346 | // Send the wrap-up event. No commands should be triggered. |
michael@0 | 347 | test_utils.sendSimpleGestureEvent(eventPrefix, 0, 0, 0, cumulativeDelta, 0); |
michael@0 | 348 | checkBoth(6, "Increasing command was triggered", "Decreasing command was triggered"); |
michael@0 | 349 | } |
michael@0 | 350 | |
michael@0 | 351 | function test_addCommand(prefName, id) |
michael@0 | 352 | { |
michael@0 | 353 | let cmd = test_commandset.appendChild(document.createElement("command")); |
michael@0 | 354 | cmd.setAttribute("id", id); |
michael@0 | 355 | cmd.setAttribute("oncommand", "this.callCount++;"); |
michael@0 | 356 | |
michael@0 | 357 | cmd.origPrefName = prefName; |
michael@0 | 358 | cmd.origPrefValue = gPrefService.getCharPref(prefName); |
michael@0 | 359 | gPrefService.setCharPref(prefName, id); |
michael@0 | 360 | |
michael@0 | 361 | return cmd; |
michael@0 | 362 | } |
michael@0 | 363 | |
michael@0 | 364 | function test_removeCommand(cmd) |
michael@0 | 365 | { |
michael@0 | 366 | gPrefService.setCharPref(cmd.origPrefName, cmd.origPrefValue); |
michael@0 | 367 | test_commandset.removeChild(cmd); |
michael@0 | 368 | } |
michael@0 | 369 | |
michael@0 | 370 | // Test whether latched events are only called once per direction of motion. |
michael@0 | 371 | function test_latchedGesture(gesture, inc, dec, eventPrefix) |
michael@0 | 372 | { |
michael@0 | 373 | let branch = test_prefBranch + gesture + "."; |
michael@0 | 374 | |
michael@0 | 375 | // Put the gesture into latched mode. |
michael@0 | 376 | let oldLatchedValue = gPrefService.getBoolPref(branch + "latched"); |
michael@0 | 377 | gPrefService.setBoolPref(branch + "latched", true); |
michael@0 | 378 | |
michael@0 | 379 | // Install the test commands for increasing and decreasing motion. |
michael@0 | 380 | let cmd = { |
michael@0 | 381 | inc: test_addCommand(branch + inc, "test:incMotion"), |
michael@0 | 382 | dec: test_addCommand(branch + dec, "test:decMotion"), |
michael@0 | 383 | }; |
michael@0 | 384 | |
michael@0 | 385 | // Test the gestures in each direction. |
michael@0 | 386 | test_emitLatchedEvents(eventPrefix, 500, cmd); |
michael@0 | 387 | test_emitLatchedEvents(eventPrefix, -500, cmd); |
michael@0 | 388 | |
michael@0 | 389 | // Restore the gesture to its original configuration. |
michael@0 | 390 | gPrefService.setBoolPref(branch + "latched", oldLatchedValue); |
michael@0 | 391 | for (let dir in cmd) |
michael@0 | 392 | test_removeCommand(cmd[dir]); |
michael@0 | 393 | } |
michael@0 | 394 | |
michael@0 | 395 | // Test whether non-latched events are triggered upon sufficient motion. |
michael@0 | 396 | function test_thresholdGesture(gesture, inc, dec, eventPrefix) |
michael@0 | 397 | { |
michael@0 | 398 | let branch = test_prefBranch + gesture + "."; |
michael@0 | 399 | |
michael@0 | 400 | // Disable latched mode for this gesture. |
michael@0 | 401 | let oldLatchedValue = gPrefService.getBoolPref(branch + "latched"); |
michael@0 | 402 | gPrefService.setBoolPref(branch + "latched", false); |
michael@0 | 403 | |
michael@0 | 404 | // Set the triggering threshold value to 50. |
michael@0 | 405 | let oldThresholdValue = gPrefService.getIntPref(branch + "threshold"); |
michael@0 | 406 | gPrefService.setIntPref(branch + "threshold", 50); |
michael@0 | 407 | |
michael@0 | 408 | // Install the test commands for increasing and decreasing motion. |
michael@0 | 409 | let cmdInc = test_addCommand(branch + inc, "test:incMotion"); |
michael@0 | 410 | let cmdDec = test_addCommand(branch + dec, "test:decMotion"); |
michael@0 | 411 | |
michael@0 | 412 | // Send the start event but stop short of triggering threshold. |
michael@0 | 413 | cmdInc.callCount = cmdDec.callCount = 0; |
michael@0 | 414 | test_utils.sendSimpleGestureEvent(eventPrefix + "Start", 0, 0, 0, 49.5, 0); |
michael@0 | 415 | ok(cmdInc.callCount == 0, "Increasing command was triggered"); |
michael@0 | 416 | ok(cmdDec.callCount == 0, "Decreasing command was triggered"); |
michael@0 | 417 | |
michael@0 | 418 | // Now trigger the threshold. |
michael@0 | 419 | cmdInc.callCount = cmdDec.callCount = 0; |
michael@0 | 420 | test_utils.sendSimpleGestureEvent(eventPrefix + "Update", 0, 0, 0, 1, 0); |
michael@0 | 421 | ok(cmdInc.callCount == 1, "Increasing command was not triggered"); |
michael@0 | 422 | ok(cmdDec.callCount == 0, "Decreasing command was triggered"); |
michael@0 | 423 | |
michael@0 | 424 | // The tracking counter should go to zero. Go back the other way and |
michael@0 | 425 | // stop short of triggering the threshold. |
michael@0 | 426 | cmdInc.callCount = cmdDec.callCount = 0; |
michael@0 | 427 | test_utils.sendSimpleGestureEvent(eventPrefix + "Update", 0, 0, 0, -49.5, 0); |
michael@0 | 428 | ok(cmdInc.callCount == 0, "Increasing command was triggered"); |
michael@0 | 429 | ok(cmdDec.callCount == 0, "Decreasing command was triggered"); |
michael@0 | 430 | |
michael@0 | 431 | // Now cross the threshold and trigger the decreasing command. |
michael@0 | 432 | cmdInc.callCount = cmdDec.callCount = 0; |
michael@0 | 433 | test_utils.sendSimpleGestureEvent(eventPrefix + "Update", 0, 0, 0, -1.5, 0); |
michael@0 | 434 | ok(cmdInc.callCount == 0, "Increasing command was triggered"); |
michael@0 | 435 | ok(cmdDec.callCount == 1, "Decreasing command was not triggered"); |
michael@0 | 436 | |
michael@0 | 437 | // Send the wrap-up event. No commands should trigger. |
michael@0 | 438 | cmdInc.callCount = cmdDec.callCount = 0; |
michael@0 | 439 | test_utils.sendSimpleGestureEvent(eventPrefix, 0, 0, 0, -0.5, 0); |
michael@0 | 440 | ok(cmdInc.callCount == 0, "Increasing command was triggered"); |
michael@0 | 441 | ok(cmdDec.callCount == 0, "Decreasing command was triggered"); |
michael@0 | 442 | |
michael@0 | 443 | // Restore the gesture to its original configuration. |
michael@0 | 444 | gPrefService.setBoolPref(branch + "latched", oldLatchedValue); |
michael@0 | 445 | gPrefService.setIntPref(branch + "threshold", oldThresholdValue); |
michael@0 | 446 | test_removeCommand(cmdInc); |
michael@0 | 447 | test_removeCommand(cmdDec); |
michael@0 | 448 | } |
michael@0 | 449 | |
michael@0 | 450 | function test_swipeGestures() |
michael@0 | 451 | { |
michael@0 | 452 | // easier to type names for the direction constants |
michael@0 | 453 | let up = SimpleGestureEvent.DIRECTION_UP; |
michael@0 | 454 | let down = SimpleGestureEvent.DIRECTION_DOWN; |
michael@0 | 455 | let left = SimpleGestureEvent.DIRECTION_LEFT; |
michael@0 | 456 | let right = SimpleGestureEvent.DIRECTION_RIGHT; |
michael@0 | 457 | |
michael@0 | 458 | let branch = test_prefBranch + "swipe."; |
michael@0 | 459 | |
michael@0 | 460 | // Install the test commands for the swipe gestures. |
michael@0 | 461 | let cmdUp = test_addCommand(branch + "up", "test:swipeUp"); |
michael@0 | 462 | let cmdDown = test_addCommand(branch + "down", "test:swipeDown"); |
michael@0 | 463 | let cmdLeft = test_addCommand(branch + "left", "test:swipeLeft"); |
michael@0 | 464 | let cmdRight = test_addCommand(branch + "right", "test:swipeRight"); |
michael@0 | 465 | |
michael@0 | 466 | function resetCounts() { |
michael@0 | 467 | cmdUp.callCount = 0; |
michael@0 | 468 | cmdDown.callCount = 0; |
michael@0 | 469 | cmdLeft.callCount = 0; |
michael@0 | 470 | cmdRight.callCount = 0; |
michael@0 | 471 | } |
michael@0 | 472 | |
michael@0 | 473 | // UP |
michael@0 | 474 | resetCounts(); |
michael@0 | 475 | test_utils.sendSimpleGestureEvent("MozSwipeGesture", 0, 0, up, 0, 0); |
michael@0 | 476 | ok(cmdUp.callCount == 1, "Step 1: Up command was not triggered"); |
michael@0 | 477 | ok(cmdDown.callCount == 0, "Step 1: Down command was triggered"); |
michael@0 | 478 | ok(cmdLeft.callCount == 0, "Step 1: Left command was triggered"); |
michael@0 | 479 | ok(cmdRight.callCount == 0, "Step 1: Right command was triggered"); |
michael@0 | 480 | |
michael@0 | 481 | // DOWN |
michael@0 | 482 | resetCounts(); |
michael@0 | 483 | test_utils.sendSimpleGestureEvent("MozSwipeGesture", 0, 0, down, 0, 0); |
michael@0 | 484 | ok(cmdUp.callCount == 0, "Step 2: Up command was triggered"); |
michael@0 | 485 | ok(cmdDown.callCount == 1, "Step 2: Down command was not triggered"); |
michael@0 | 486 | ok(cmdLeft.callCount == 0, "Step 2: Left command was triggered"); |
michael@0 | 487 | ok(cmdRight.callCount == 0, "Step 2: Right command was triggered"); |
michael@0 | 488 | |
michael@0 | 489 | // LEFT |
michael@0 | 490 | resetCounts(); |
michael@0 | 491 | test_utils.sendSimpleGestureEvent("MozSwipeGesture", 0, 0, left, 0, 0); |
michael@0 | 492 | ok(cmdUp.callCount == 0, "Step 3: Up command was triggered"); |
michael@0 | 493 | ok(cmdDown.callCount == 0, "Step 3: Down command was triggered"); |
michael@0 | 494 | ok(cmdLeft.callCount == 1, "Step 3: Left command was not triggered"); |
michael@0 | 495 | ok(cmdRight.callCount == 0, "Step 3: Right command was triggered"); |
michael@0 | 496 | |
michael@0 | 497 | // RIGHT |
michael@0 | 498 | resetCounts(); |
michael@0 | 499 | test_utils.sendSimpleGestureEvent("MozSwipeGesture", 0, 0, right, 0, 0); |
michael@0 | 500 | ok(cmdUp.callCount == 0, "Step 4: Up command was triggered"); |
michael@0 | 501 | ok(cmdDown.callCount == 0, "Step 4: Down command was triggered"); |
michael@0 | 502 | ok(cmdLeft.callCount == 0, "Step 4: Left command was triggered"); |
michael@0 | 503 | ok(cmdRight.callCount == 1, "Step 4: Right command was not triggered"); |
michael@0 | 504 | |
michael@0 | 505 | // Make sure combinations do not trigger events. |
michael@0 | 506 | let combos = [ up | left, up | right, down | left, down | right]; |
michael@0 | 507 | for (let i = 0; i < combos.length; i++) { |
michael@0 | 508 | resetCounts(); |
michael@0 | 509 | test_utils.sendSimpleGestureEvent("MozSwipeGesture", 0, 0, combos[i], 0, 0); |
michael@0 | 510 | ok(cmdUp.callCount == 0, "Step 5-"+i+": Up command was triggered"); |
michael@0 | 511 | ok(cmdDown.callCount == 0, "Step 5-"+i+": Down command was triggered"); |
michael@0 | 512 | ok(cmdLeft.callCount == 0, "Step 5-"+i+": Left command was triggered"); |
michael@0 | 513 | ok(cmdRight.callCount == 0, "Step 5-"+i+": Right command was triggered"); |
michael@0 | 514 | } |
michael@0 | 515 | |
michael@0 | 516 | // Remove the test commands. |
michael@0 | 517 | test_removeCommand(cmdUp); |
michael@0 | 518 | test_removeCommand(cmdDown); |
michael@0 | 519 | test_removeCommand(cmdLeft); |
michael@0 | 520 | test_removeCommand(cmdRight); |
michael@0 | 521 | } |
michael@0 | 522 | |
michael@0 | 523 | |
michael@0 | 524 | function test_rotateHelperGetImageRotation(aImageElement) |
michael@0 | 525 | { |
michael@0 | 526 | // Get the true image rotation from the transform matrix, bounded |
michael@0 | 527 | // to 0 <= result < 360 |
michael@0 | 528 | let transformValue = content.window.getComputedStyle(aImageElement, null) |
michael@0 | 529 | .transform; |
michael@0 | 530 | if (transformValue == "none") |
michael@0 | 531 | return 0; |
michael@0 | 532 | |
michael@0 | 533 | transformValue = transformValue.split("(")[1] |
michael@0 | 534 | .split(")")[0] |
michael@0 | 535 | .split(","); |
michael@0 | 536 | var rotation = Math.round(Math.atan2(transformValue[1], transformValue[0]) * |
michael@0 | 537 | (180 / Math.PI)); |
michael@0 | 538 | return (rotation < 0 ? rotation + 360 : rotation); |
michael@0 | 539 | } |
michael@0 | 540 | |
michael@0 | 541 | function test_rotateHelperOneGesture(aImageElement, aCurrentRotation, |
michael@0 | 542 | aDirection, aAmount, aStop) |
michael@0 | 543 | { |
michael@0 | 544 | if (aAmount <= 0 || aAmount > 90) // Bound to 0 < aAmount <= 90 |
michael@0 | 545 | return; |
michael@0 | 546 | |
michael@0 | 547 | // easier to type names for the direction constants |
michael@0 | 548 | let clockwise = SimpleGestureEvent.ROTATION_CLOCKWISE; |
michael@0 | 549 | let cclockwise = SimpleGestureEvent.ROTATION_COUNTERCLOCKWISE; |
michael@0 | 550 | |
michael@0 | 551 | let delta = aAmount * (aDirection == clockwise ? 1 : -1); |
michael@0 | 552 | |
michael@0 | 553 | // Kill transition time on image so test isn't wrong and doesn't take 10 seconds |
michael@0 | 554 | aImageElement.style.transitionDuration = "0s"; |
michael@0 | 555 | |
michael@0 | 556 | // Start the gesture, perform an update, and force flush |
michael@0 | 557 | test_utils.sendSimpleGestureEvent("MozRotateGestureStart", 0, 0, aDirection, .001, 0); |
michael@0 | 558 | test_utils.sendSimpleGestureEvent("MozRotateGestureUpdate", 0, 0, aDirection, delta, 0); |
michael@0 | 559 | aImageElement.clientTop; |
michael@0 | 560 | |
michael@0 | 561 | // If stop, check intermediate |
michael@0 | 562 | if (aStop) { |
michael@0 | 563 | // Send near-zero-delta to stop, and force flush |
michael@0 | 564 | test_utils.sendSimpleGestureEvent("MozRotateGestureUpdate", 0, 0, aDirection, .001, 0); |
michael@0 | 565 | aImageElement.clientTop; |
michael@0 | 566 | |
michael@0 | 567 | let stopExpectedRotation = (aCurrentRotation + delta) % 360; |
michael@0 | 568 | if (stopExpectedRotation < 0) |
michael@0 | 569 | stopExpectedRotation += 360; |
michael@0 | 570 | |
michael@0 | 571 | is(stopExpectedRotation, test_rotateHelperGetImageRotation(aImageElement), |
michael@0 | 572 | "Image rotation at gesture stop/hold: expected=" + stopExpectedRotation + |
michael@0 | 573 | ", observed=" + test_rotateHelperGetImageRotation(aImageElement) + |
michael@0 | 574 | ", init=" + aCurrentRotation + |
michael@0 | 575 | ", amt=" + aAmount + |
michael@0 | 576 | ", dir=" + (aDirection == clockwise ? "cl" : "ccl")); |
michael@0 | 577 | } |
michael@0 | 578 | // End it and force flush |
michael@0 | 579 | test_utils.sendSimpleGestureEvent("MozRotateGesture", 0, 0, aDirection, 0, 0); |
michael@0 | 580 | aImageElement.clientTop; |
michael@0 | 581 | |
michael@0 | 582 | let finalExpectedRotation; |
michael@0 | 583 | |
michael@0 | 584 | if (aAmount < 45 && aStop) { |
michael@0 | 585 | // Rotate a bit, then stop. Expect no change at end of gesture. |
michael@0 | 586 | finalExpectedRotation = aCurrentRotation; |
michael@0 | 587 | } |
michael@0 | 588 | else { |
michael@0 | 589 | // Either not stopping (expect 90 degree change in aDirection), OR |
michael@0 | 590 | // stopping but after 45, (expect 90 degree change in aDirection) |
michael@0 | 591 | finalExpectedRotation = (aCurrentRotation + |
michael@0 | 592 | (aDirection == clockwise ? 1 : -1) * 90) % 360; |
michael@0 | 593 | if (finalExpectedRotation < 0) |
michael@0 | 594 | finalExpectedRotation += 360; |
michael@0 | 595 | } |
michael@0 | 596 | |
michael@0 | 597 | is(finalExpectedRotation, test_rotateHelperGetImageRotation(aImageElement), |
michael@0 | 598 | "Image rotation gesture end: expected=" + finalExpectedRotation + |
michael@0 | 599 | ", observed=" + test_rotateHelperGetImageRotation(aImageElement) + |
michael@0 | 600 | ", init=" + aCurrentRotation + |
michael@0 | 601 | ", amt=" + aAmount + |
michael@0 | 602 | ", dir=" + (aDirection == clockwise ? "cl" : "ccl")); |
michael@0 | 603 | } |
michael@0 | 604 | |
michael@0 | 605 | function test_rotateGesturesOnTab() |
michael@0 | 606 | { |
michael@0 | 607 | gBrowser.selectedBrowser.removeEventListener("load", test_rotateGesturesOnTab, true); |
michael@0 | 608 | |
michael@0 | 609 | if (!(content.document instanceof ImageDocument)) { |
michael@0 | 610 | ok(false, "Image document failed to open for rotation testing"); |
michael@0 | 611 | gBrowser.removeTab(test_imageTab); |
michael@0 | 612 | finish(); |
michael@0 | 613 | return; |
michael@0 | 614 | } |
michael@0 | 615 | |
michael@0 | 616 | // easier to type names for the direction constants |
michael@0 | 617 | let cl = SimpleGestureEvent.ROTATION_CLOCKWISE; |
michael@0 | 618 | let ccl = SimpleGestureEvent.ROTATION_COUNTERCLOCKWISE; |
michael@0 | 619 | |
michael@0 | 620 | let imgElem = content.document.body && |
michael@0 | 621 | content.document.body.firstElementChild; |
michael@0 | 622 | |
michael@0 | 623 | if (!imgElem) { |
michael@0 | 624 | ok(false, "Could not get image element on ImageDocument for rotation!"); |
michael@0 | 625 | gBrowser.removeTab(test_imageTab); |
michael@0 | 626 | finish(); |
michael@0 | 627 | return; |
michael@0 | 628 | } |
michael@0 | 629 | |
michael@0 | 630 | // Quick function to normalize rotation to 0 <= r < 360 |
michael@0 | 631 | var normRot = function(rotation) { |
michael@0 | 632 | rotation = rotation % 360; |
michael@0 | 633 | if (rotation < 0) |
michael@0 | 634 | rotation += 360; |
michael@0 | 635 | return rotation; |
michael@0 | 636 | } |
michael@0 | 637 | |
michael@0 | 638 | for (var initRot = 0; initRot < 360; initRot += 90) { |
michael@0 | 639 | // Test each case: at each 90 degree snap; cl/ccl; |
michael@0 | 640 | // amount more or less than 45; stop and hold or don't (32 total tests) |
michael@0 | 641 | // The amount added to the initRot is where it is expected to be |
michael@0 | 642 | test_rotateHelperOneGesture(imgElem, normRot(initRot + 0), cl, 35, true ); |
michael@0 | 643 | test_rotateHelperOneGesture(imgElem, normRot(initRot + 0), cl, 35, false); |
michael@0 | 644 | test_rotateHelperOneGesture(imgElem, normRot(initRot + 90), cl, 55, true ); |
michael@0 | 645 | test_rotateHelperOneGesture(imgElem, normRot(initRot + 180), cl, 55, false); |
michael@0 | 646 | test_rotateHelperOneGesture(imgElem, normRot(initRot + 270), ccl, 35, true ); |
michael@0 | 647 | test_rotateHelperOneGesture(imgElem, normRot(initRot + 270), ccl, 35, false); |
michael@0 | 648 | test_rotateHelperOneGesture(imgElem, normRot(initRot + 180), ccl, 55, true ); |
michael@0 | 649 | test_rotateHelperOneGesture(imgElem, normRot(initRot + 90), ccl, 55, false); |
michael@0 | 650 | |
michael@0 | 651 | // Manually rotate it 90 degrees clockwise to prepare for next iteration, |
michael@0 | 652 | // and force flush |
michael@0 | 653 | test_utils.sendSimpleGestureEvent("MozRotateGestureStart", 0, 0, cl, .001, 0); |
michael@0 | 654 | test_utils.sendSimpleGestureEvent("MozRotateGestureUpdate", 0, 0, cl, 90, 0); |
michael@0 | 655 | test_utils.sendSimpleGestureEvent("MozRotateGestureUpdate", 0, 0, cl, .001, 0); |
michael@0 | 656 | test_utils.sendSimpleGestureEvent("MozRotateGesture", 0, 0, cl, 0, 0); |
michael@0 | 657 | imgElem.clientTop; |
michael@0 | 658 | } |
michael@0 | 659 | |
michael@0 | 660 | gBrowser.removeTab(test_imageTab); |
michael@0 | 661 | test_imageTab = null; |
michael@0 | 662 | finish(); |
michael@0 | 663 | } |
michael@0 | 664 | |
michael@0 | 665 | function test_rotateGestures() |
michael@0 | 666 | { |
michael@0 | 667 | test_imageTab = gBrowser.addTab("chrome://branding/content/about-logo.png"); |
michael@0 | 668 | gBrowser.selectedTab = test_imageTab; |
michael@0 | 669 | |
michael@0 | 670 | gBrowser.selectedBrowser.addEventListener("load", test_rotateGesturesOnTab, true); |
michael@0 | 671 | } |