-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
input_key_test.ts
127 lines (106 loc) · 4.66 KB
/
input_key_test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'chrome://shortcut-customization/strings.m.js';
import 'chrome://shortcut-customization/js/input_key.js';
import 'chrome://webui-test/mojo_webui_test_support.js';
import {IronIconElement} from '//resources/polymer/v3_0/iron-icon/iron-icon.js';
import {flush} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js';
import {AcceleratorLookupManager} from 'chrome://shortcut-customization/js/accelerator_lookup_manager.js';
import {InputKeyElement, keyToIconNameMap} from 'chrome://shortcut-customization/js/input_key.js';
import {assertEquals, assertFalse, assertTrue} from 'chrome://webui-test/chai_assert.js';
import {isVisible} from 'chrome://webui-test/test_util.js';
function initInputKeyElement(): InputKeyElement {
const element = document.createElement('input-key');
document.body.appendChild(element);
flush();
return element;
}
suite('inputKeyTest', function() {
let inputKeyElement: InputKeyElement|null = null;
let manager: AcceleratorLookupManager|null = null;
setup(() => {
manager = AcceleratorLookupManager.getInstance();
});
teardown(() => {
if (inputKeyElement) {
inputKeyElement.remove();
}
if (manager) {
manager.reset();
}
inputKeyElement = null;
});
test('BasicKeys', async () => {
inputKeyElement = initInputKeyElement();
inputKeyElement.key = 'a';
await flush();
const keyElement = inputKeyElement.shadowRoot!.querySelector('#key-text');
assertTrue(!!keyElement);
assertTrue(isVisible(keyElement));
assertEquals('a', keyElement.textContent);
const iconElement = inputKeyElement.shadowRoot!.querySelector('#key-icon');
assertFalse(isVisible(iconElement));
});
test('IconKeys', async () => {
inputKeyElement = initInputKeyElement();
inputKeyElement.key = 'PrintScreen';
await flush();
const iconElement = inputKeyElement.shadowRoot!.querySelector(
'#key-icon') as IronIconElement;
assertTrue(isVisible(iconElement));
assertEquals('shortcut-customization-keys:screenshot', iconElement.icon);
const keyElement = inputKeyElement.shadowRoot!.querySelector('#key-text');
assertFalse(isVisible(keyElement));
});
test('AllIconsHaveValidAriaLabelStringIds', async () => {
inputKeyElement = initInputKeyElement();
for (const keyCode of Object.keys(keyToIconNameMap)) {
const ariaLabelStringId =
InputKeyElement.getAriaLabelStringId(keyCode, true);
assertTrue(
inputKeyElement.i18nExists(ariaLabelStringId),
`String ID ${ariaLabelStringId} should exist, but it doesn't.`);
}
});
test('IconKeyHasAriaLabel', async () => {
inputKeyElement = initInputKeyElement();
inputKeyElement.key = 'PrintScreen';
await flush();
const iconWrapperElement = inputKeyElement.shadowRoot!.querySelector(
'#key > div') as HTMLDivElement;
assertTrue(isVisible(iconWrapperElement));
assertEquals('screenshot', iconWrapperElement.ariaLabel);
assertEquals('img', iconWrapperElement.getAttribute('role'));
});
test('MetaKeyShowLauncherIcon', async () => {
inputKeyElement = initInputKeyElement();
inputKeyElement.key = 'meta';
manager!.setHasLauncherButton(true);
await flush();
// Should show launcher icon when hasLauncherButton is true.
const iconElement = inputKeyElement.shadowRoot!.querySelector(
'#key-icon') as IronIconElement;
const iconWrapperElement = inputKeyElement.shadowRoot!.querySelector(
'#key > div') as HTMLDivElement;
assertTrue(isVisible(iconElement));
assertTrue(isVisible(iconWrapperElement));
assertEquals('shortcut-customization-keys:launcher', iconElement.icon);
assertEquals('launcher', iconWrapperElement.ariaLabel);
});
test('MetaKeyShowSearchIcon', async () => {
inputKeyElement = initInputKeyElement();
inputKeyElement.key = 'meta';
manager!.setHasLauncherButton(false);
await flush();
// Should show search icon when hasLauncherButton is false.
const iconElement2 = inputKeyElement.shadowRoot!.querySelector(
'#key-icon') as IronIconElement;
const iconWrapperElement2 = inputKeyElement.shadowRoot!.querySelector(
'#key > div') as HTMLDivElement;
assertTrue(isVisible(iconElement2));
assertTrue(isVisible(iconWrapperElement2));
assertEquals('shortcut-customization-keys:search', iconElement2.icon);
assertEquals('search', iconWrapperElement2.ariaLabel);
});
});