cdk-testing.umd.js 13 KB

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