Path: blob/main/src/vs/platform/contextkey/test/browser/contextkey.test.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import assert from 'assert';6import { DeferredPromise } from '../../../../base/common/async.js';7import { URI } from '../../../../base/common/uri.js';8import { mock } from '../../../../base/test/common/mock.js';9import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../base/test/common/utils.js';10import { IConfigurationService } from '../../../configuration/common/configuration.js';11import { TestConfigurationService } from '../../../configuration/test/common/testConfigurationService.js';12import { ContextKeyService, setContext } from '../../browser/contextKeyService.js';13import { ContextKeyExpr, IContextKeyService } from '../../common/contextkey.js';14import { ServiceCollection } from '../../../instantiation/common/serviceCollection.js';15import { TestInstantiationService } from '../../../instantiation/test/common/instantiationServiceMock.js';16import { ITelemetryService } from '../../../telemetry/common/telemetry.js';1718suite('ContextKeyService', () => {19const testDisposables = ensureNoDisposablesAreLeakedInTestSuite();2021test('updateParent', () => {22const root = testDisposables.add(new ContextKeyService(new TestConfigurationService()));23const parent1 = testDisposables.add(root.createScoped(document.createElement('div')));24const parent2 = testDisposables.add(root.createScoped(document.createElement('div')));2526const child = testDisposables.add(parent1.createScoped(document.createElement('div')));27parent1.createKey('testA', 1);28parent1.createKey('testB', 2);29parent1.createKey('testD', 0);3031parent2.createKey('testA', 3);32parent2.createKey('testC', 4);33parent2.createKey('testD', 0);3435let complete: () => void;36let reject: (err: Error) => void;37const p = new Promise<void>((_complete, _reject) => {38complete = _complete;39reject = _reject;40});41testDisposables.add(child.onDidChangeContext(e => {42try {43assert.ok(e.affectsSome(new Set(['testA'])), 'testA changed');44assert.ok(e.affectsSome(new Set(['testB'])), 'testB changed');45assert.ok(e.affectsSome(new Set(['testC'])), 'testC changed');46assert.ok(!e.affectsSome(new Set(['testD'])), 'testD did not change');4748assert.strictEqual(child.getContextKeyValue('testA'), 3);49assert.strictEqual(child.getContextKeyValue('testB'), undefined);50assert.strictEqual(child.getContextKeyValue('testC'), 4);51assert.strictEqual(child.getContextKeyValue('testD'), 0);52} catch (err) {53reject(err);54return;55}5657complete();58}));5960child.updateParent(parent2);6162return p;63});6465test('updateParent to same service', () => {66const root = testDisposables.add(new ContextKeyService(new TestConfigurationService()));67const parent1 = testDisposables.add(root.createScoped(document.createElement('div')));6869const child = testDisposables.add(parent1.createScoped(document.createElement('div')));70parent1.createKey('testA', 1);71parent1.createKey('testB', 2);72parent1.createKey('testD', 0);7374let eventFired = false;75testDisposables.add(child.onDidChangeContext(e => {76eventFired = true;77}));7879child.updateParent(parent1);8081assert.strictEqual(eventFired, false);82});8384test('issue #147732: URIs as context values', () => {85const configurationService: IConfigurationService = new TestConfigurationService();86const contextKeyService: IContextKeyService = testDisposables.add(new ContextKeyService(configurationService));87const instantiationService = testDisposables.add(new TestInstantiationService(new ServiceCollection(88[IConfigurationService, configurationService],89[IContextKeyService, contextKeyService],90[ITelemetryService, new class extends mock<ITelemetryService>() {91override async publicLog2() {92//93}94}]95)));9697const uri = URI.parse('test://abc');98contextKeyService.createKey<string>('notebookCellResource', undefined).set(uri.toString());99instantiationService.invokeFunction(setContext, 'jupyter.runByLineCells', JSON.parse(JSON.stringify([uri])));100101const expr = ContextKeyExpr.in('notebookCellResource', 'jupyter.runByLineCells');102assert.deepStrictEqual(contextKeyService.contextMatchesRules(expr), true);103});104105test('suppress update event from parent when one key is overridden by child', () => {106const root = testDisposables.add(new ContextKeyService(new TestConfigurationService()));107const child = testDisposables.add(root.createScoped(document.createElement('div')));108109root.createKey('testA', 1);110child.createKey('testA', 4);111112let fired = false;113const event = testDisposables.add(child.onDidChangeContext(e => fired = true));114root.setContext('testA', 10);115assert.strictEqual(fired, false, 'Should not fire event when overridden key is updated in parent');116event.dispose();117});118119test('suppress update event from parent when all keys are overridden by child', () => {120const root = testDisposables.add(new ContextKeyService(new TestConfigurationService()));121const child = testDisposables.add(root.createScoped(document.createElement('div')));122123root.createKey('testA', 1);124root.createKey('testB', 2);125root.createKey('testC', 3);126127child.createKey('testA', 4);128child.createKey('testB', 5);129child.createKey('testD', 6);130131let fired = false;132const event = testDisposables.add(child.onDidChangeContext(e => fired = true));133root.bufferChangeEvents(() => {134root.setContext('testA', 10);135root.setContext('testB', 20);136root.setContext('testD', 30);137});138139assert.strictEqual(fired, false, 'Should not fire event when overridden key is updated in parent');140event.dispose();141});142143test('pass through update event from parent when one key is not overridden by child', () => {144const root = testDisposables.add(new ContextKeyService(new TestConfigurationService()));145const child = testDisposables.add(root.createScoped(document.createElement('div')));146147root.createKey('testA', 1);148root.createKey('testB', 2);149root.createKey('testC', 3);150151child.createKey('testA', 4);152child.createKey('testB', 5);153child.createKey('testD', 6);154155const def = new DeferredPromise();156testDisposables.add(child.onDidChangeContext(e => {157try {158assert.ok(e.affectsSome(new Set(['testA'])), 'testA changed');159assert.ok(e.affectsSome(new Set(['testB'])), 'testB changed');160assert.ok(e.affectsSome(new Set(['testC'])), 'testC changed');161} catch (err) {162def.error(err);163return;164}165166def.complete(undefined);167}));168169root.bufferChangeEvents(() => {170root.setContext('testA', 10);171root.setContext('testB', 20);172root.setContext('testC', 30);173});174175return def.p;176});177});178179180