Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/contextkey/test/browser/contextkey.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 { DeferredPromise } from '../../../../base/common/async.js';
8
import { URI } from '../../../../base/common/uri.js';
9
import { mock } from '../../../../base/test/common/mock.js';
10
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../base/test/common/utils.js';
11
import { IConfigurationService } from '../../../configuration/common/configuration.js';
12
import { TestConfigurationService } from '../../../configuration/test/common/testConfigurationService.js';
13
import { ContextKeyService, setContext } from '../../browser/contextKeyService.js';
14
import { ContextKeyExpr, IContextKeyService } from '../../common/contextkey.js';
15
import { ServiceCollection } from '../../../instantiation/common/serviceCollection.js';
16
import { TestInstantiationService } from '../../../instantiation/test/common/instantiationServiceMock.js';
17
import { ITelemetryService } from '../../../telemetry/common/telemetry.js';
18
19
suite('ContextKeyService', () => {
20
const testDisposables = ensureNoDisposablesAreLeakedInTestSuite();
21
22
test('updateParent', () => {
23
const root = testDisposables.add(new ContextKeyService(new TestConfigurationService()));
24
const parent1 = testDisposables.add(root.createScoped(document.createElement('div')));
25
const parent2 = testDisposables.add(root.createScoped(document.createElement('div')));
26
27
const child = testDisposables.add(parent1.createScoped(document.createElement('div')));
28
parent1.createKey('testA', 1);
29
parent1.createKey('testB', 2);
30
parent1.createKey('testD', 0);
31
32
parent2.createKey('testA', 3);
33
parent2.createKey('testC', 4);
34
parent2.createKey('testD', 0);
35
36
let complete: () => void;
37
let reject: (err: Error) => void;
38
const p = new Promise<void>((_complete, _reject) => {
39
complete = _complete;
40
reject = _reject;
41
});
42
testDisposables.add(child.onDidChangeContext(e => {
43
try {
44
assert.ok(e.affectsSome(new Set(['testA'])), 'testA changed');
45
assert.ok(e.affectsSome(new Set(['testB'])), 'testB changed');
46
assert.ok(e.affectsSome(new Set(['testC'])), 'testC changed');
47
assert.ok(!e.affectsSome(new Set(['testD'])), 'testD did not change');
48
49
assert.strictEqual(child.getContextKeyValue('testA'), 3);
50
assert.strictEqual(child.getContextKeyValue('testB'), undefined);
51
assert.strictEqual(child.getContextKeyValue('testC'), 4);
52
assert.strictEqual(child.getContextKeyValue('testD'), 0);
53
} catch (err) {
54
reject(err);
55
return;
56
}
57
58
complete();
59
}));
60
61
child.updateParent(parent2);
62
63
return p;
64
});
65
66
test('updateParent to same service', () => {
67
const root = testDisposables.add(new ContextKeyService(new TestConfigurationService()));
68
const parent1 = testDisposables.add(root.createScoped(document.createElement('div')));
69
70
const child = testDisposables.add(parent1.createScoped(document.createElement('div')));
71
parent1.createKey('testA', 1);
72
parent1.createKey('testB', 2);
73
parent1.createKey('testD', 0);
74
75
let eventFired = false;
76
testDisposables.add(child.onDidChangeContext(e => {
77
eventFired = true;
78
}));
79
80
child.updateParent(parent1);
81
82
assert.strictEqual(eventFired, false);
83
});
84
85
test('issue #147732: URIs as context values', () => {
86
const configurationService: IConfigurationService = new TestConfigurationService();
87
const contextKeyService: IContextKeyService = testDisposables.add(new ContextKeyService(configurationService));
88
const instantiationService = testDisposables.add(new TestInstantiationService(new ServiceCollection(
89
[IConfigurationService, configurationService],
90
[IContextKeyService, contextKeyService],
91
[ITelemetryService, new class extends mock<ITelemetryService>() {
92
override async publicLog2() {
93
//
94
}
95
}]
96
)));
97
98
const uri = URI.parse('test://abc');
99
contextKeyService.createKey<string>('notebookCellResource', undefined).set(uri.toString());
100
instantiationService.invokeFunction(setContext, 'jupyter.runByLineCells', JSON.parse(JSON.stringify([uri])));
101
102
const expr = ContextKeyExpr.in('notebookCellResource', 'jupyter.runByLineCells');
103
assert.deepStrictEqual(contextKeyService.contextMatchesRules(expr), true);
104
});
105
106
test('suppress update event from parent when one key is overridden by child', () => {
107
const root = testDisposables.add(new ContextKeyService(new TestConfigurationService()));
108
const child = testDisposables.add(root.createScoped(document.createElement('div')));
109
110
root.createKey('testA', 1);
111
child.createKey('testA', 4);
112
113
let fired = false;
114
const event = testDisposables.add(child.onDidChangeContext(e => fired = true));
115
root.setContext('testA', 10);
116
assert.strictEqual(fired, false, 'Should not fire event when overridden key is updated in parent');
117
event.dispose();
118
});
119
120
test('suppress update event from parent when all keys are overridden by child', () => {
121
const root = testDisposables.add(new ContextKeyService(new TestConfigurationService()));
122
const child = testDisposables.add(root.createScoped(document.createElement('div')));
123
124
root.createKey('testA', 1);
125
root.createKey('testB', 2);
126
root.createKey('testC', 3);
127
128
child.createKey('testA', 4);
129
child.createKey('testB', 5);
130
child.createKey('testD', 6);
131
132
let fired = false;
133
const event = testDisposables.add(child.onDidChangeContext(e => fired = true));
134
root.bufferChangeEvents(() => {
135
root.setContext('testA', 10);
136
root.setContext('testB', 20);
137
root.setContext('testD', 30);
138
});
139
140
assert.strictEqual(fired, false, 'Should not fire event when overridden key is updated in parent');
141
event.dispose();
142
});
143
144
test('pass through update event from parent when one key is not overridden by child', () => {
145
const root = testDisposables.add(new ContextKeyService(new TestConfigurationService()));
146
const child = testDisposables.add(root.createScoped(document.createElement('div')));
147
148
root.createKey('testA', 1);
149
root.createKey('testB', 2);
150
root.createKey('testC', 3);
151
152
child.createKey('testA', 4);
153
child.createKey('testB', 5);
154
child.createKey('testD', 6);
155
156
const def = new DeferredPromise();
157
testDisposables.add(child.onDidChangeContext(e => {
158
try {
159
assert.ok(e.affectsSome(new Set(['testA'])), 'testA changed');
160
assert.ok(e.affectsSome(new Set(['testB'])), 'testB changed');
161
assert.ok(e.affectsSome(new Set(['testC'])), 'testC changed');
162
} catch (err) {
163
def.error(err);
164
return;
165
}
166
167
def.complete(undefined);
168
}));
169
170
root.bufferChangeEvents(() => {
171
root.setContext('testA', 10);
172
root.setContext('testB', 20);
173
root.setContext('testC', 30);
174
});
175
176
return def.p;
177
});
178
});
179
180