browserevent_test.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 2009 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.events.BrowserEventTest');
  15. goog.setTestOnly('goog.events.BrowserEventTest');
  16. goog.require('goog.events.BrowserEvent');
  17. goog.require('goog.events.BrowserFeature');
  18. goog.require('goog.math.Coordinate');
  19. goog.require('goog.testing.PropertyReplacer');
  20. goog.require('goog.testing.jsunit');
  21. goog.require('goog.userAgent');
  22. var stubs = new goog.testing.PropertyReplacer();
  23. var Button = goog.events.BrowserEvent.MouseButton;
  24. function setUp() {
  25. stubs.reset();
  26. }
  27. /**
  28. * @see https://bugzilla.mozilla.org/show_bug.cgi?id=497780
  29. */
  30. function testInvalidNodeBug() {
  31. if (!goog.userAgent.GECKO) return;
  32. var event = {};
  33. event.relatedTarget = {};
  34. event.relatedTarget.__defineGetter__('nodeName', function() {
  35. throw Error('https://bugzilla.mozilla.org/show_bug.cgi?id=497780');
  36. });
  37. assertThrows(function() { return event.relatedTarget.nodeName; });
  38. var bEvent = new goog.events.BrowserEvent(event);
  39. assertEquals(event, bEvent.event_);
  40. assertNull(bEvent.relatedTarget);
  41. }
  42. function testPreventDefault() {
  43. var event = {};
  44. event.defaultPrevented = false;
  45. var bEvent = new goog.events.BrowserEvent(event);
  46. assertFalse(bEvent.defaultPrevented);
  47. bEvent.preventDefault();
  48. assertTrue(bEvent.defaultPrevented);
  49. }
  50. function testDefaultPrevented() {
  51. var event = {};
  52. event.defaultPrevented = true;
  53. var bEvent = new goog.events.BrowserEvent(event);
  54. assertTrue(bEvent.defaultPrevented);
  55. }
  56. function testIsButtonIe() {
  57. stubs.set(goog.events.BrowserFeature, 'HAS_W3C_BUTTON', false);
  58. assertIsButton(createMouseEvent('mousedown', 1), Button.LEFT, true);
  59. assertIsButton(createMouseEvent('click', 0), Button.LEFT, true);
  60. assertIsButton(createMouseEvent('mousedown', 2), Button.RIGHT, false);
  61. assertIsButton(createMouseEvent('mousedown', 4), Button.MIDDLE, false);
  62. }
  63. function testIsButtonWebkitMac() {
  64. stubs.set(goog.events.BrowserFeature, 'HAS_W3C_BUTTON', true);
  65. stubs.set(goog.userAgent, 'WEBKIT', true);
  66. stubs.set(goog.userAgent, 'MAC', true);
  67. assertIsButton(createMouseEvent('mousedown', 0), Button.LEFT, true);
  68. assertIsButton(createMouseEvent('mousedown', 0, true), Button.LEFT, false);
  69. assertIsButton(createMouseEvent('mousedown', 2), Button.RIGHT, false);
  70. assertIsButton(createMouseEvent('mousedown', 2, true), Button.RIGHT, false);
  71. assertIsButton(createMouseEvent('mousedown', 1), Button.MIDDLE, false);
  72. assertIsButton(createMouseEvent('mousedown', 1, true), Button.MIDDLE, false);
  73. }
  74. function testIsButtonGecko() {
  75. stubs.set(goog.events.BrowserFeature, 'HAS_W3C_BUTTON', true);
  76. stubs.set(goog.userAgent, 'GECKO', true);
  77. stubs.set(goog.userAgent, 'MAC', true);
  78. assertIsButton(createMouseEvent('mousedown', 0), Button.LEFT, true);
  79. assertIsButton(createMouseEvent('mousedown', 2, true), Button.RIGHT, false);
  80. }
  81. function testTouchEventHandling() {
  82. var clientCoords = new goog.math.Coordinate(5, 5);
  83. var screenCoords = new goog.math.Coordinate(10, 10);
  84. var target = document.body;
  85. var touchStart =
  86. createTouchEvent('touchstart', target, clientCoords, screenCoords);
  87. var touchMove =
  88. createTouchEvent('touchmove', target, clientCoords, screenCoords);
  89. var touchEnd =
  90. createTouchEvent('touchend', target, clientCoords, screenCoords);
  91. var touchCancel =
  92. createTouchEvent('touchcancel', target, clientCoords, screenCoords);
  93. assertEquals(clientCoords.x, touchStart.clientX);
  94. assertEquals(clientCoords.y, touchStart.clientY);
  95. assertEquals(target, touchStart.target);
  96. assertEquals(screenCoords.x, touchMove.screenX);
  97. assertEquals(screenCoords.y, touchMove.screenY);
  98. assertEquals(clientCoords.x, touchEnd.clientX);
  99. assertEquals(clientCoords.y, touchEnd.clientY);
  100. assertEquals(screenCoords.x, touchCancel.screenX);
  101. assertEquals(screenCoords.y, touchCancel.screenY);
  102. assertEquals(target, touchCancel.target);
  103. }
  104. function createMouseEvent(type, button, opt_ctrlKey) {
  105. return new goog.events.BrowserEvent(
  106. {type: type, button: button, ctrlKey: !!opt_ctrlKey});
  107. }
  108. function createTouchEvent(type, target, clientCoords, screenCoords) {
  109. return new goog.events.BrowserEvent({
  110. type: type,
  111. target: target,
  112. changedTouches: [{
  113. clientX: clientCoords.x,
  114. clientY: clientCoords.y,
  115. screenX: screenCoords.x,
  116. screenY: screenCoords.y
  117. }]
  118. });
  119. }
  120. function assertIsButton(event, button, isActionButton) {
  121. for (var key in Button) {
  122. assertEquals(
  123. 'Testing isButton(' + key + ') against ' + button + ' and type ' +
  124. event.type,
  125. Button[key] == button, event.isButton(Button[key]));
  126. }
  127. assertEquals(isActionButton, event.isMouseActionButton());
  128. }