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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { IListRenderer, IListVirtualDelegate } from 'vs/base/browser/ui/list/list';
import { IListOptions, List } from 'vs/base/browser/ui/list/listWidget';
import { QuickInputController } from 'vs/base/parts/quickinput/browser/quickInput';
import { IQuickPick, IQuickPickItem } from 'vs/base/parts/quickinput/common/quickInput';
import { flakySuite } from 'vs/base/test/common/testUtils';
// Simple promisify of setTimeout
function wait(delayMS: number) {
return new Promise(function (resolve) {
setTimeout(resolve, delayMS);
});
}
flakySuite('QuickInput', () => { // https://github.com/microsoft/vscode/issues/147543
let fixture: HTMLElement, controller: QuickInputController, quickpick: IQuickPick<IQuickPickItem>;
function getScrollTop(): number {
return (quickpick as any).scrollTop;
}
setup(() => {
fixture = document.createElement('div');
document.body.appendChild(fixture);
controller = new QuickInputController({
container: fixture,
idPrefix: 'testQuickInput',
ignoreFocusOut() { return false; },
isScreenReaderOptimized() { return false; },
returnFocus() { },
backKeybindingLabel() { return undefined; },
setContextKey() { return undefined; },
createList: <T>(
user: string,
container: HTMLElement,
delegate: IListVirtualDelegate<T>,
renderers: IListRenderer<T, any>[],
options: IListOptions<T>,
) => new List<T>(user, container, delegate, renderers, options),
styles: {
button: {},
countBadge: {},
inputBox: {},
keybindingLabel: {},
list: {},
progressBar: {},
widget: {}
}
});
// initial layout
controller.layout({ height: 20, width: 40 }, 0);
});
teardown(() => {
quickpick?.dispose();
controller.dispose();
document.body.removeChild(fixture);
});
test('pick - basecase', async () => {
const item = { label: 'foo' };
const pickPromise = controller.pick([item, { label: 'bar' }]);
// wait a bit to let the pick get set up.
await wait(200);
controller.accept();
const pick = await pickPromise;
assert.strictEqual(pick, item);
});
test('pick - activeItem is honored', async () => {
const item = { label: 'foo' };
const pickPromise = controller.pick([{ label: 'bar' }, item], { activeItem: item });
// wait a bit to let the pick get set up.
await wait(200);
controller.accept();
const pick = await pickPromise;
assert.strictEqual(pick, item);
});
test('input - basecase', async () => {
const inputPromise = controller.input({ value: 'foo' });
// wait a bit to let the pick get set up.
await wait(200);
controller.accept();
const value = await inputPromise;
assert.strictEqual(value, 'foo');
});
test('onDidChangeValue - gets triggered when .value is set', async () => {
quickpick = controller.createQuickPick();
let value: string | undefined = undefined;
quickpick.onDidChangeValue((e) => value = e);
// Trigger a change
quickpick.value = 'changed';
try {
// wait a bit to let the event play out.
await wait(200);
assert.strictEqual(value, quickpick.value);
} finally {
quickpick.dispose();
}
});
test('keepScrollPosition - works with activeItems', async () => {
quickpick = controller.createQuickPick();
const items = [];
for (let i = 0; i < 1000; i++) {
items.push({ label: `item ${i}` });
}
quickpick.items = items;
// setting the active item should cause the quick pick to scroll to the bottom
quickpick.activeItems = [items[items.length - 1]];
quickpick.show();
let cursorTop = getScrollTop();
assert.notStrictEqual(cursorTop, 0);
quickpick.keepScrollPosition = true;
quickpick.activeItems = [items[0]];
assert.strictEqual(cursorTop, getScrollTop());
quickpick.keepScrollPosition = false;
quickpick.activeItems = [items[0]];
assert.strictEqual(getScrollTop(), 0);
});
test('keepScrollPosition - works with items', async () => {
quickpick = controller.createQuickPick();
const items = [];
for (let i = 0; i < 1000; i++) {
items.push({ label: `item ${i}` });
}
quickpick.items = items;
// setting the active item should cause the quick pick to scroll to the bottom
quickpick.activeItems = [items[items.length - 1]];
quickpick.show();
let cursorTop = getScrollTop();
assert.notStrictEqual(cursorTop, 0);
quickpick.keepScrollPosition = true;
quickpick.items = items;
assert.strictEqual(cursorTop, getScrollTop());
quickpick.keepScrollPosition = false;
quickpick.items = items;
assert.strictEqual(getScrollTop(), 0);
});
test('selectedItems - verify previous selectedItems does not hang over to next set of items', async () => {
quickpick = controller.createQuickPick();
quickpick.items = [{ label: 'step 1' }];
quickpick.show();
void (await new Promise<void>(resolve => {
quickpick.onDidAccept(() => {
quickpick.canSelectMany = true;
quickpick.items = [{ label: 'a' }, { label: 'b' }, { label: 'c' }];
resolve();
});
// accept 'step 1'
controller.accept();
}));
// accept in multi-select
controller.accept();
// Since we don't select any items, the selected items should be empty
assert.strictEqual(quickpick.selectedItems.length, 0);
});
});
|