testing.es5.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /**
  2. * @license
  3. * Copyright Google LLC All Rights Reserved.
  4. *
  5. * Use of this source code is governed by an MIT-style license that can be
  6. * found in the LICENSE file at https://angular.io/license
  7. */
  8. /**
  9. * @fileoverview added by tsickle
  10. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  11. */
  12. /**
  13. * Creates a browser MouseEvent with the specified options.
  14. * \@docs-private
  15. * @param {?} type
  16. * @param {?=} x
  17. * @param {?=} y
  18. * @param {?=} button
  19. * @return {?}
  20. */
  21. function createMouseEvent(type, x, y, button) {
  22. if (x === void 0) { x = 0; }
  23. if (y === void 0) { y = 0; }
  24. if (button === void 0) { button = 0; }
  25. /** @type {?} */
  26. var event = document.createEvent('MouseEvent');
  27. /** @type {?} */
  28. var originalPreventDefault = event.preventDefault;
  29. event.initMouseEvent(type, true, /* canBubble */ true, /* cancelable */ window, /* view */ 0, /* detail */ x, /* screenX */ y, /* screenY */ x, /* clientX */ y, /* clientY */ false, /* ctrlKey */ false, /* altKey */ false, /* shiftKey */ false, /* metaKey */ button, /* button */ null /* relatedTarget */);
  30. // `initMouseEvent` doesn't allow us to pass the `buttons` and
  31. // defaults it to 0 which looks like a fake event.
  32. Object.defineProperty(event, 'buttons', { get: (/**
  33. * @return {?}
  34. */
  35. function () { return 1; }) });
  36. // IE won't set `defaultPrevented` on synthetic events so we need to do it manually.
  37. event.preventDefault = (/**
  38. * @return {?}
  39. */
  40. function () {
  41. Object.defineProperty(event, 'defaultPrevented', { get: (/**
  42. * @return {?}
  43. */
  44. function () { return true; }) });
  45. return originalPreventDefault.apply(this, arguments);
  46. });
  47. return event;
  48. }
  49. /**
  50. * Creates a browser TouchEvent with the specified pointer coordinates.
  51. * \@docs-private
  52. * @param {?} type
  53. * @param {?=} pageX
  54. * @param {?=} pageY
  55. * @return {?}
  56. */
  57. function createTouchEvent(type, pageX, pageY) {
  58. if (pageX === void 0) { pageX = 0; }
  59. if (pageY === void 0) { pageY = 0; }
  60. // In favor of creating events that work for most of the browsers, the event is created
  61. // as a basic UI Event. The necessary details for the event will be set manually.
  62. /** @type {?} */
  63. var event = document.createEvent('UIEvent');
  64. /** @type {?} */
  65. var touchDetails = { pageX: pageX, pageY: pageY };
  66. // TS3.6 removes the initUIEvent method and suggests porting to "new UIEvent()".
  67. ((/** @type {?} */ (event))).initUIEvent(type, true, true, window, 0);
  68. // Most of the browsers don't have a "initTouchEvent" method that can be used to define
  69. // the touch details.
  70. Object.defineProperties(event, {
  71. touches: { value: [touchDetails] },
  72. targetTouches: { value: [touchDetails] },
  73. changedTouches: { value: [touchDetails] }
  74. });
  75. return event;
  76. }
  77. /**
  78. * Dispatches a keydown event from an element.
  79. * \@docs-private
  80. * @param {?} type
  81. * @param {?=} keyCode
  82. * @param {?=} key
  83. * @param {?=} target
  84. * @param {?=} modifiers
  85. * @return {?}
  86. */
  87. function createKeyboardEvent(type, keyCode, key, target, modifiers) {
  88. if (keyCode === void 0) { keyCode = 0; }
  89. if (key === void 0) { key = ''; }
  90. if (modifiers === void 0) { modifiers = {}; }
  91. /** @type {?} */
  92. var event = (/** @type {?} */ (document.createEvent('KeyboardEvent')));
  93. /** @type {?} */
  94. var originalPreventDefault = event.preventDefault;
  95. // Firefox does not support `initKeyboardEvent`, but supports `initKeyEvent`.
  96. if (event.initKeyEvent) {
  97. event.initKeyEvent(type, true, true, window, modifiers.control, modifiers.alt, modifiers.shift, modifiers.meta, keyCode);
  98. }
  99. else {
  100. // `initKeyboardEvent` expects to receive modifiers as a whitespace-delimited string
  101. // See https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/initKeyboardEvent
  102. /** @type {?} */
  103. var modifiersStr = (modifiers.control ? 'Control ' : '' + modifiers.alt ? 'Alt ' : '' +
  104. modifiers.shift ? 'Shift ' : '' + modifiers.meta ? 'Meta' : '').trim();
  105. event.initKeyboardEvent(type, true, /* canBubble */ true, /* cancelable */ window, /* view */ 0, /* char */ key, /* key */ 0, /* location */ modifiersStr, /* modifiersList */ false /* repeat */);
  106. }
  107. // Webkit Browsers don't set the keyCode when calling the init function.
  108. // See related bug https://bugs.webkit.org/show_bug.cgi?id=16735
  109. Object.defineProperties(event, {
  110. keyCode: { get: (/**
  111. * @return {?}
  112. */
  113. function () { return keyCode; }) },
  114. key: { get: (/**
  115. * @return {?}
  116. */
  117. function () { return key; }) },
  118. target: { get: (/**
  119. * @return {?}
  120. */
  121. function () { return target; }) },
  122. ctrlKey: { get: (/**
  123. * @return {?}
  124. */
  125. function () { return !!modifiers.control; }) },
  126. altKey: { get: (/**
  127. * @return {?}
  128. */
  129. function () { return !!modifiers.alt; }) },
  130. shiftKey: { get: (/**
  131. * @return {?}
  132. */
  133. function () { return !!modifiers.shift; }) },
  134. metaKey: { get: (/**
  135. * @return {?}
  136. */
  137. function () { return !!modifiers.meta; }) }
  138. });
  139. // IE won't set `defaultPrevented` on synthetic events so we need to do it manually.
  140. event.preventDefault = (/**
  141. * @return {?}
  142. */
  143. function () {
  144. Object.defineProperty(event, 'defaultPrevented', { get: (/**
  145. * @return {?}
  146. */
  147. function () { return true; }) });
  148. return originalPreventDefault.apply(this, arguments);
  149. });
  150. return event;
  151. }
  152. /**
  153. * Creates a fake event object with any desired event type.
  154. * \@docs-private
  155. * @param {?} type
  156. * @param {?=} canBubble
  157. * @param {?=} cancelable
  158. * @return {?}
  159. */
  160. function createFakeEvent(type, canBubble, cancelable) {
  161. if (canBubble === void 0) { canBubble = false; }
  162. if (cancelable === void 0) { cancelable = true; }
  163. /** @type {?} */
  164. var event = document.createEvent('Event');
  165. event.initEvent(type, canBubble, cancelable);
  166. return event;
  167. }
  168. /**
  169. * @fileoverview added by tsickle
  170. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  171. */
  172. /**
  173. * Utility to dispatch any event on a Node.
  174. * \@docs-private
  175. * @param {?} node
  176. * @param {?} event
  177. * @return {?}
  178. */
  179. function dispatchEvent(node, event) {
  180. node.dispatchEvent(event);
  181. return event;
  182. }
  183. /**
  184. * Shorthand to dispatch a fake event on a specified node.
  185. * \@docs-private
  186. * @param {?} node
  187. * @param {?} type
  188. * @param {?=} canBubble
  189. * @return {?}
  190. */
  191. function dispatchFakeEvent(node, type, canBubble) {
  192. return dispatchEvent(node, createFakeEvent(type, canBubble));
  193. }
  194. /**
  195. * Shorthand to dispatch a keyboard event with a specified key code.
  196. * \@docs-private
  197. * @param {?} node
  198. * @param {?} type
  199. * @param {?=} keyCode
  200. * @param {?=} key
  201. * @param {?=} target
  202. * @param {?=} modifiers
  203. * @return {?}
  204. */
  205. function dispatchKeyboardEvent(node, type, keyCode, key, target, modifiers) {
  206. return (/** @type {?} */ (dispatchEvent(node, createKeyboardEvent(type, keyCode, key, target, modifiers))));
  207. }
  208. /**
  209. * Shorthand to dispatch a mouse event on the specified coordinates.
  210. * \@docs-private
  211. * @param {?} node
  212. * @param {?} type
  213. * @param {?=} x
  214. * @param {?=} y
  215. * @param {?=} event
  216. * @return {?}
  217. */
  218. function dispatchMouseEvent(node, type, x, y, event) {
  219. if (x === void 0) { x = 0; }
  220. if (y === void 0) { y = 0; }
  221. if (event === void 0) { event = createMouseEvent(type, x, y); }
  222. return (/** @type {?} */ (dispatchEvent(node, event)));
  223. }
  224. /**
  225. * Shorthand to dispatch a touch event on the specified coordinates.
  226. * \@docs-private
  227. * @param {?} node
  228. * @param {?} type
  229. * @param {?=} x
  230. * @param {?=} y
  231. * @return {?}
  232. */
  233. function dispatchTouchEvent(node, type, x, y) {
  234. if (x === void 0) { x = 0; }
  235. if (y === void 0) { y = 0; }
  236. return dispatchEvent(node, createTouchEvent(type, x, y));
  237. }
  238. /**
  239. * @fileoverview added by tsickle
  240. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  241. */
  242. /**
  243. * @param {?} element
  244. * @param {?} event
  245. * @return {?}
  246. */
  247. function triggerFocusChange(element, event) {
  248. /** @type {?} */
  249. var eventFired = false;
  250. /** @type {?} */
  251. var handler = (/**
  252. * @return {?}
  253. */
  254. function () { return eventFired = true; });
  255. element.addEventListener(event, handler);
  256. element[event]();
  257. element.removeEventListener(event, handler);
  258. if (!eventFired) {
  259. dispatchFakeEvent(element, event);
  260. }
  261. }
  262. /**
  263. * Patches an elements focus and blur methods to emit events consistently and predictably.
  264. * This is necessary, because some browsers, like IE11, will call the focus handlers asynchronously,
  265. * while others won't fire them at all if the browser window is not focused.
  266. * \@docs-private
  267. * @param {?} element
  268. * @return {?}
  269. */
  270. function patchElementFocus(element) {
  271. element.focus = (/**
  272. * @return {?}
  273. */
  274. function () { return dispatchFakeEvent(element, 'focus'); });
  275. element.blur = (/**
  276. * @return {?}
  277. */
  278. function () { return dispatchFakeEvent(element, 'blur'); });
  279. }
  280. /**
  281. * \@docs-private
  282. * @param {?} element
  283. * @return {?}
  284. */
  285. function triggerFocus(element) {
  286. triggerFocusChange(element, 'focus');
  287. }
  288. /**
  289. * \@docs-private
  290. * @param {?} element
  291. * @return {?}
  292. */
  293. function triggerBlur(element) {
  294. triggerFocusChange(element, 'blur');
  295. }
  296. /**
  297. * @fileoverview added by tsickle
  298. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  299. */
  300. /**
  301. * Checks whether the given Element is a text input element.
  302. * \@docs-private
  303. * @param {?} element
  304. * @return {?}
  305. */
  306. function isTextInput(element) {
  307. return element.nodeName.toLowerCase() === 'input' ||
  308. element.nodeName.toLowerCase() === 'textarea';
  309. }
  310. /**
  311. * @param {?} element
  312. * @param {...?} modifiersAndKeys
  313. * @return {?}
  314. */
  315. function typeInElement(element) {
  316. var modifiersAndKeys = [];
  317. for (var _i = 1; _i < arguments.length; _i++) {
  318. modifiersAndKeys[_i - 1] = arguments[_i];
  319. }
  320. /** @type {?} */
  321. var first = modifiersAndKeys[0];
  322. /** @type {?} */
  323. var modifiers;
  324. /** @type {?} */
  325. var rest;
  326. if (typeof first !== 'string' && first.keyCode === undefined && first.key === undefined) {
  327. modifiers = first;
  328. rest = modifiersAndKeys.slice(1);
  329. }
  330. else {
  331. modifiers = {};
  332. rest = modifiersAndKeys;
  333. }
  334. /** @type {?} */
  335. var keys = rest
  336. .map((/**
  337. * @param {?} k
  338. * @return {?}
  339. */
  340. function (k) { return typeof k === 'string' ?
  341. k.split('').map((/**
  342. * @param {?} c
  343. * @return {?}
  344. */
  345. function (c) { return ({ keyCode: c.toUpperCase().charCodeAt(0), key: c }); })) : [k]; }))
  346. .reduce((/**
  347. * @param {?} arr
  348. * @param {?} k
  349. * @return {?}
  350. */
  351. function (arr, k) { return arr.concat(k); }), []);
  352. triggerFocus(element);
  353. for (var _a = 0, keys_1 = keys; _a < keys_1.length; _a++) {
  354. var key = keys_1[_a];
  355. dispatchKeyboardEvent(element, 'keydown', key.keyCode, key.key, element, modifiers);
  356. dispatchKeyboardEvent(element, 'keypress', key.keyCode, key.key, element, modifiers);
  357. if (isTextInput(element) && key.key && key.key.length === 1) {
  358. element.value += key.key;
  359. dispatchFakeEvent(element, 'input');
  360. }
  361. dispatchKeyboardEvent(element, 'keyup', key.keyCode, key.key, element, modifiers);
  362. }
  363. }
  364. /**
  365. * Clears the text in an input or textarea element.
  366. * \@docs-private
  367. * @param {?} element
  368. * @return {?}
  369. */
  370. function clearElement(element) {
  371. triggerFocus((/** @type {?} */ (element)));
  372. element.value = '';
  373. dispatchFakeEvent(element, 'input');
  374. }
  375. /**
  376. * @fileoverview added by tsickle
  377. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  378. */
  379. /**
  380. * @fileoverview added by tsickle
  381. * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
  382. */
  383. export { dispatchEvent, dispatchFakeEvent, dispatchKeyboardEvent, dispatchMouseEvent, dispatchTouchEvent, createMouseEvent, createTouchEvent, createKeyboardEvent, createFakeEvent, isTextInput, typeInElement, clearElement, patchElementFocus, triggerFocus, triggerBlur };
  384. //# sourceMappingURL=testing.es5.js.map