Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/actions/test/common/menuService.test.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import assert from 'assert';
7
import { DisposableStore } from '../../../../base/common/lifecycle.js';
8
import { generateUuid } from '../../../../base/common/uuid.js';
9
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../base/test/common/utils.js';
10
import { isIMenuItem, MenuId, MenuRegistry } from '../../common/actions.js';
11
import { MenuService } from '../../common/menuService.js';
12
import { NullCommandService } from '../../../commands/test/common/nullCommandService.js';
13
import { MockContextKeyService, MockKeybindingService } from '../../../keybinding/test/common/mockKeybindingService.js';
14
import { InMemoryStorageService } from '../../../storage/common/storage.js';
15
16
// --- service instances
17
18
const contextKeyService = new class extends MockContextKeyService {
19
override contextMatchesRules() {
20
return true;
21
}
22
};
23
24
// --- tests
25
26
suite('MenuService', function () {
27
28
let menuService: MenuService;
29
const disposables = new DisposableStore();
30
let testMenuId: MenuId;
31
32
setup(function () {
33
menuService = new MenuService(NullCommandService, new MockKeybindingService(), new InMemoryStorageService());
34
testMenuId = new MenuId(`testo/${generateUuid()}`);
35
disposables.clear();
36
});
37
38
teardown(function () {
39
disposables.clear();
40
});
41
42
ensureNoDisposablesAreLeakedInTestSuite();
43
44
test('group sorting', function () {
45
46
disposables.add(MenuRegistry.appendMenuItem(testMenuId, {
47
command: { id: 'one', title: 'FOO' },
48
group: '0_hello'
49
}));
50
51
disposables.add(MenuRegistry.appendMenuItem(testMenuId, {
52
command: { id: 'two', title: 'FOO' },
53
group: 'hello'
54
}));
55
56
disposables.add(MenuRegistry.appendMenuItem(testMenuId, {
57
command: { id: 'three', title: 'FOO' },
58
group: 'Hello'
59
}));
60
61
disposables.add(MenuRegistry.appendMenuItem(testMenuId, {
62
command: { id: 'four', title: 'FOO' },
63
group: ''
64
}));
65
66
disposables.add(MenuRegistry.appendMenuItem(testMenuId, {
67
command: { id: 'five', title: 'FOO' },
68
group: 'navigation'
69
}));
70
71
const groups = disposables.add(menuService.createMenu(testMenuId, contextKeyService)).getActions();
72
73
assert.strictEqual(groups.length, 5);
74
const [one, two, three, four, five] = groups;
75
76
assert.strictEqual(one[0], 'navigation');
77
assert.strictEqual(two[0], '0_hello');
78
assert.strictEqual(three[0], 'hello');
79
assert.strictEqual(four[0], 'Hello');
80
assert.strictEqual(five[0], '');
81
});
82
83
test('in group sorting, by title', function () {
84
85
disposables.add(MenuRegistry.appendMenuItem(testMenuId, {
86
command: { id: 'a', title: 'aaa' },
87
group: 'Hello'
88
}));
89
90
disposables.add(MenuRegistry.appendMenuItem(testMenuId, {
91
command: { id: 'b', title: 'fff' },
92
group: 'Hello'
93
}));
94
95
disposables.add(MenuRegistry.appendMenuItem(testMenuId, {
96
command: { id: 'c', title: 'zzz' },
97
group: 'Hello'
98
}));
99
100
const groups = disposables.add(menuService.createMenu(testMenuId, contextKeyService)).getActions();
101
102
assert.strictEqual(groups.length, 1);
103
const [, actions] = groups[0];
104
105
assert.strictEqual(actions.length, 3);
106
const [one, two, three] = actions;
107
assert.strictEqual(one.id, 'a');
108
assert.strictEqual(two.id, 'b');
109
assert.strictEqual(three.id, 'c');
110
});
111
112
test('in group sorting, by title and order', function () {
113
114
disposables.add(MenuRegistry.appendMenuItem(testMenuId, {
115
command: { id: 'a', title: 'aaa' },
116
group: 'Hello',
117
order: 10
118
}));
119
120
disposables.add(MenuRegistry.appendMenuItem(testMenuId, {
121
command: { id: 'b', title: 'fff' },
122
group: 'Hello'
123
}));
124
125
disposables.add(MenuRegistry.appendMenuItem(testMenuId, {
126
command: { id: 'c', title: 'zzz' },
127
group: 'Hello',
128
order: -1
129
}));
130
131
disposables.add(MenuRegistry.appendMenuItem(testMenuId, {
132
command: { id: 'd', title: 'yyy' },
133
group: 'Hello',
134
order: -1
135
}));
136
137
const groups = disposables.add(menuService.createMenu(testMenuId, contextKeyService)).getActions();
138
139
assert.strictEqual(groups.length, 1);
140
const [, actions] = groups[0];
141
142
assert.strictEqual(actions.length, 4);
143
const [one, two, three, four] = actions;
144
assert.strictEqual(one.id, 'd');
145
assert.strictEqual(two.id, 'c');
146
assert.strictEqual(three.id, 'b');
147
assert.strictEqual(four.id, 'a');
148
});
149
150
151
test('in group sorting, special: navigation', function () {
152
153
disposables.add(MenuRegistry.appendMenuItem(testMenuId, {
154
command: { id: 'a', title: 'aaa' },
155
group: 'navigation',
156
order: 1.3
157
}));
158
159
disposables.add(MenuRegistry.appendMenuItem(testMenuId, {
160
command: { id: 'b', title: 'fff' },
161
group: 'navigation',
162
order: 1.2
163
}));
164
165
disposables.add(MenuRegistry.appendMenuItem(testMenuId, {
166
command: { id: 'c', title: 'zzz' },
167
group: 'navigation',
168
order: 1.1
169
}));
170
171
const groups = disposables.add(menuService.createMenu(testMenuId, contextKeyService)).getActions();
172
173
assert.strictEqual(groups.length, 1);
174
const [[, actions]] = groups;
175
176
assert.strictEqual(actions.length, 3);
177
const [one, two, three] = actions;
178
assert.strictEqual(one.id, 'c');
179
assert.strictEqual(two.id, 'b');
180
assert.strictEqual(three.id, 'a');
181
});
182
183
test('special MenuId palette', function () {
184
185
disposables.add(MenuRegistry.appendMenuItem(MenuId.CommandPalette, {
186
command: { id: 'a', title: 'Explicit' }
187
}));
188
189
disposables.add(MenuRegistry.addCommand({ id: 'b', title: 'Implicit' }));
190
191
let foundA = false;
192
let foundB = false;
193
for (const item of MenuRegistry.getMenuItems(MenuId.CommandPalette)) {
194
if (isIMenuItem(item)) {
195
if (item.command.id === 'a') {
196
assert.strictEqual(item.command.title, 'Explicit');
197
foundA = true;
198
}
199
if (item.command.id === 'b') {
200
assert.strictEqual(item.command.title, 'Implicit');
201
foundB = true;
202
}
203
}
204
}
205
assert.strictEqual(foundA, true);
206
assert.strictEqual(foundB, true);
207
});
208
209
test('Extension contributed submenus missing with errors in output #155030', function () {
210
211
const id = generateUuid();
212
const menu = new MenuId(id);
213
214
assert.throws(() => new MenuId(id));
215
assert.ok(menu === MenuId.for(id));
216
});
217
});
218
219