Skip to content

Commit d8ff449

Browse files
authored
chore: remove experimental test packages for patch branch (#17192)
Because we moved so much around on master, it was easier to delete some code on the patch branch than to get it all functioning.
1 parent fcf9f8f commit d8ff449

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+314
-2139
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
import {
10+
createFakeEvent,
11+
createKeyboardEvent,
12+
createMouseEvent,
13+
createTouchEvent,
14+
ModifierKeys
15+
} from './event-objects';
16+
17+
/**
18+
* Utility to dispatch any event on a Node.
19+
* @docs-private
20+
*/
21+
export function dispatchEvent(node: Node | Window, event: Event): Event {
22+
node.dispatchEvent(event);
23+
return event;
24+
}
25+
26+
/**
27+
* Shorthand to dispatch a fake event on a specified node.
28+
* @docs-private
29+
*/
30+
export function dispatchFakeEvent(node: Node | Window, type: string, canBubble?: boolean): Event {
31+
return dispatchEvent(node, createFakeEvent(type, canBubble));
32+
}
33+
34+
/**
35+
* Shorthand to dispatch a keyboard event with a specified key code.
36+
* @docs-private
37+
*/
38+
export function dispatchKeyboardEvent(node: Node, type: string, keyCode?: number, key?: string,
39+
target?: Element, modifiers?: ModifierKeys): KeyboardEvent {
40+
return dispatchEvent(node,
41+
createKeyboardEvent(type, keyCode, key, target, modifiers)) as KeyboardEvent;
42+
}
43+
44+
/**
45+
* Shorthand to dispatch a mouse event on the specified coordinates.
46+
* @docs-private
47+
*/
48+
export function dispatchMouseEvent(node: Node, type: string, x = 0, y = 0,
49+
event = createMouseEvent(type, x, y)): MouseEvent {
50+
return dispatchEvent(node, event) as MouseEvent;
51+
}
52+
53+
/**
54+
* Shorthand to dispatch a touch event on the specified coordinates.
55+
* @docs-private
56+
*/
57+
export function dispatchTouchEvent(node: Node, type: string, x = 0, y = 0) {
58+
return dispatchEvent(node, createTouchEvent(type, x, y));
59+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
import {dispatchFakeEvent} from './dispatch-events';
10+
11+
function triggerFocusChange(element: HTMLElement, event: 'focus' | 'blur') {
12+
let eventFired = false;
13+
const handler = () => eventFired = true;
14+
element.addEventListener(event, handler);
15+
element[event]();
16+
element.removeEventListener(event, handler);
17+
if (!eventFired) {
18+
dispatchFakeEvent(element, event);
19+
}
20+
}
21+
22+
/**
23+
* Patches an elements focus and blur methods to emit events consistently and predictably.
24+
* This is necessary, because some browsers, like IE11, will call the focus handlers asynchronously,
25+
* while others won't fire them at all if the browser window is not focused.
26+
* @docs-private
27+
*/
28+
export function patchElementFocus(element: HTMLElement) {
29+
element.focus = () => dispatchFakeEvent(element, 'focus');
30+
element.blur = () => dispatchFakeEvent(element, 'blur');
31+
}
32+
33+
/** @docs-private */
34+
export function triggerFocus(element: HTMLElement) {
35+
triggerFocusChange(element, 'focus');
36+
}
37+
38+
/** @docs-private */
39+
export function triggerBlur(element: HTMLElement) {
40+
triggerFocusChange(element, 'blur');
41+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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+
/** Modifier keys that may be held while typing. */
10+
export interface ModifierKeys {
11+
control?: boolean;
12+
alt?: boolean;
13+
shift?: boolean;
14+
meta?: boolean;
15+
}
16+
17+
/**
18+
* Creates a browser MouseEvent with the specified options.
19+
* @docs-private
20+
*/
21+
export function createMouseEvent(type: string, x = 0, y = 0, button = 0) {
22+
const event = document.createEvent('MouseEvent');
23+
const originalPreventDefault = event.preventDefault;
24+
25+
event.initMouseEvent(type,
26+
true, /* canBubble */
27+
true, /* cancelable */
28+
window, /* view */
29+
0, /* detail */
30+
x, /* screenX */
31+
y, /* screenY */
32+
x, /* clientX */
33+
y, /* clientY */
34+
false, /* ctrlKey */
35+
false, /* altKey */
36+
false, /* shiftKey */
37+
false, /* metaKey */
38+
button, /* button */
39+
null /* relatedTarget */);
40+
41+
// `initMouseEvent` doesn't allow us to pass the `buttons` and
42+
// defaults it to 0 which looks like a fake event.
43+
Object.defineProperty(event, 'buttons', {get: () => 1});
44+
45+
// IE won't set `defaultPrevented` on synthetic events so we need to do it manually.
46+
event.preventDefault = function() {
47+
Object.defineProperty(event, 'defaultPrevented', { get: () => true });
48+
return originalPreventDefault.apply(this, arguments);
49+
};
50+
51+
return event;
52+
}
53+
54+
/**
55+
* Creates a browser TouchEvent with the specified pointer coordinates.
56+
* @docs-private
57+
*/
58+
export function createTouchEvent(type: string, pageX = 0, pageY = 0) {
59+
// In favor of creating events that work for most of the browsers, the event is created
60+
// as a basic UI Event. The necessary details for the event will be set manually.
61+
const event = document.createEvent('UIEvent');
62+
const touchDetails = {pageX, pageY};
63+
64+
event.initUIEvent(type, true, true, window, 0);
65+
66+
// Most of the browsers don't have a "initTouchEvent" method that can be used to define
67+
// the touch details.
68+
Object.defineProperties(event, {
69+
touches: {value: [touchDetails]},
70+
targetTouches: {value: [touchDetails]},
71+
changedTouches: {value: [touchDetails]}
72+
});
73+
74+
return event;
75+
}
76+
77+
/**
78+
* Dispatches a keydown event from an element.
79+
* @docs-private
80+
*/
81+
export function createKeyboardEvent(type: string, keyCode: number = 0, key: string = '',
82+
target?: Element, modifiers: ModifierKeys = {}) {
83+
const event = document.createEvent('KeyboardEvent') as any;
84+
const originalPreventDefault = event.preventDefault;
85+
86+
// Firefox does not support `initKeyboardEvent`, but supports `initKeyEvent`.
87+
if (event.initKeyEvent) {
88+
event.initKeyEvent(type, true, true, window, modifiers.control, modifiers.alt, modifiers.shift,
89+
modifiers.meta, keyCode);
90+
} else {
91+
// `initKeyboardEvent` expects to receive modifiers as a whitespace-delimited string
92+
// See https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/initKeyboardEvent
93+
const modifiersStr = (modifiers.control ? 'Control ' : '' + modifiers.alt ? 'Alt ' : '' +
94+
modifiers.shift ? 'Shift ' : '' + modifiers.meta ? 'Meta' : '').trim();
95+
event.initKeyboardEvent(type,
96+
true, /* canBubble */
97+
true, /* cancelable */
98+
window, /* view */
99+
0, /* char */
100+
key, /* key */
101+
0, /* location */
102+
modifiersStr, /* modifiersList */
103+
false /* repeat */);
104+
}
105+
106+
// Webkit Browsers don't set the keyCode when calling the init function.
107+
// See related bug https://bugs.webkit.org/show_bug.cgi?id=16735
108+
Object.defineProperties(event, {
109+
keyCode: { get: () => keyCode },
110+
key: { get: () => key },
111+
target: { get: () => target },
112+
ctrlKey: { get: () => !!modifiers.control },
113+
altKey: { get: () => !!modifiers.alt },
114+
shiftKey: { get: () => !!modifiers.shift },
115+
metaKey: { get: () => !!modifiers.meta }
116+
});
117+
118+
// IE won't set `defaultPrevented` on synthetic events so we need to do it manually.
119+
event.preventDefault = function() {
120+
Object.defineProperty(event, 'defaultPrevented', { get: () => true });
121+
return originalPreventDefault.apply(this, arguments);
122+
};
123+
124+
return event;
125+
}
126+
127+
/**
128+
* Creates a fake event object with any desired event type.
129+
* @docs-private
130+
*/
131+
export function createFakeEvent(type: string, canBubble = false, cancelable = true) {
132+
const event = document.createEvent('Event');
133+
event.initEvent(type, canBubble, cancelable);
134+
return event;
135+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
import {dispatchFakeEvent, dispatchKeyboardEvent} from './dispatch-events';
10+
import {triggerFocus} from './element-focus';
11+
import {ModifierKeys} from './event-objects';
12+
13+
/**
14+
* Checks whether the given Element is a text input element.
15+
* @docs-private
16+
*/
17+
export function isTextInput(element: Element): element is HTMLInputElement | HTMLTextAreaElement {
18+
return element.nodeName.toLowerCase() === 'input' ||
19+
element.nodeName.toLowerCase() === 'textarea' ;
20+
}
21+
22+
/**
23+
* Focuses an input, sets its value and dispatches
24+
* the `input` event, simulating the user typing.
25+
* @param element Element onto which to set the value.
26+
* @param keys The keys to send to the element.
27+
* @docs-private
28+
*/
29+
export function typeInElement(
30+
element: HTMLElement, ...keys: (string | {keyCode?: number, key?: string})[]): void;
31+
32+
/**
33+
* Focuses an input, sets its value and dispatches
34+
* the `input` event, simulating the user typing.
35+
* @param element Element onto which to set the value.
36+
* @param modifiers Modifier keys that are held while typing.
37+
* @param keys The keys to send to the element.
38+
* @docs-private
39+
*/
40+
export function typeInElement(element: HTMLElement, modifiers: ModifierKeys,
41+
...keys: (string | {keyCode?: number, key?: string})[]): void;
42+
43+
export function typeInElement(element: HTMLElement, ...modifiersAndKeys: any) {
44+
const first = modifiersAndKeys[0];
45+
let modifiers: ModifierKeys;
46+
let rest: (string | {keyCode?: number, key?: string})[];
47+
if (typeof first !== 'string' && first.keyCode === undefined && first.key === undefined) {
48+
modifiers = first;
49+
rest = modifiersAndKeys.slice(1);
50+
} else {
51+
modifiers = {};
52+
rest = modifiersAndKeys;
53+
}
54+
const keys: {keyCode?: number, key?: string}[] = rest
55+
.map(k => typeof k === 'string' ?
56+
k.split('').map(c => ({keyCode: c.toUpperCase().charCodeAt(0), key: c})) : [k])
57+
.reduce((arr, k) => arr.concat(k), []);
58+
59+
triggerFocus(element);
60+
for (const key of keys) {
61+
dispatchKeyboardEvent(element, 'keydown', key.keyCode, key.key, element, modifiers);
62+
dispatchKeyboardEvent(element, 'keypress', key.keyCode, key.key, element, modifiers);
63+
if (isTextInput(element) && key.key && key.key.length === 1) {
64+
element.value += key.key;
65+
dispatchFakeEvent(element, 'input');
66+
}
67+
dispatchKeyboardEvent(element, 'keyup', key.keyCode, key.key, element, modifiers);
68+
}
69+
}
70+
71+
/**
72+
* Clears the text in an input or textarea element.
73+
* @docs-private
74+
*/
75+
export function clearElement(element: HTMLInputElement | HTMLTextAreaElement) {
76+
triggerFocus(element as HTMLElement);
77+
element.value = '';
78+
dispatchFakeEvent(element, 'input');
79+
}

src/material-experimental/form-field/testing/BUILD.bazel

Lines changed: 0 additions & 53 deletions
This file was deleted.

src/material-experimental/form-field/testing/control/BUILD.bazel

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/material-experimental/form-field/testing/control/form-field-control-harness.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)