Skip to content

Commit 5dc8004

Browse files
committed
more editor service tests
1 parent 8f7851d commit 5dc8004

File tree

2 files changed

+175
-19
lines changed

2 files changed

+175
-19
lines changed

src/vs/workbench/services/editor/test/browser/editorService.test.ts

Lines changed: 154 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { EditorActivation, EditorOverride, IResourceEditorInput } from 'vs/platf
88
import { URI } from 'vs/base/common/uri';
99
import { Event } from 'vs/base/common/event';
1010
import { DEFAULT_EDITOR_ASSOCIATION, EditorsOrder, GroupIdentifier, IEditorInputWithOptions, IEditorPane, IResourceDiffEditorInput, isEditorInputWithOptions, isResourceDiffEditorInput, isUntitledResourceEditorInput, IUntitledTextResourceEditorInput, IUntypedEditorInput, UntypedEditorContext } from 'vs/workbench/common/editor';
11-
import { workbenchInstantiationService, TestServiceAccessor, registerTestEditor, TestFileEditorInput, ITestInstantiationService, registerTestResourceEditor, registerTestSideBySideEditor, createEditorPart, registerTestFileEditor } from 'vs/workbench/test/browser/workbenchTestServices';
11+
import { workbenchInstantiationService, TestServiceAccessor, registerTestEditor, TestFileEditorInput, ITestInstantiationService, registerTestResourceEditor, registerTestSideBySideEditor, createEditorPart, registerTestFileEditor, TestEditorWithOptions, TestTextFileEditor } from 'vs/workbench/test/browser/workbenchTestServices';
1212
import { TextResourceEditorInput } from 'vs/workbench/common/editor/textResourceEditorInput';
1313
import { EditorService } from 'vs/workbench/services/editor/browser/editorService';
1414
import { IEditorGroup, IEditorGroupsService, GroupDirection, GroupsArrangement } from 'vs/workbench/services/editor/common/editorGroupsService';
@@ -71,7 +71,7 @@ suite('EditorService', () => {
7171
return [part, editorService, instantiationService.createInstance(TestServiceAccessor)];
7272
}
7373

74-
test('openEditor', async () => {
74+
test('basics', async () => {
7575
const [, service] = await createEditorService();
7676

7777
let input = new TestFileEditorInput(URI.parse('my://resource-basics'), TEST_EDITOR_INPUT_ID);
@@ -178,11 +178,11 @@ suite('EditorService', () => {
178178
didCloseEditorListener.dispose();
179179
});
180180

181-
test('openEditor - override handling', () => {
181+
test('openEditor()', () => {
182182
return testOpenEditors(false);
183183
});
184184

185-
test('openEditors - override handling', () => {
185+
test('openEditors()', () => {
186186
return testOpenEditors(true);
187187
});
188188

@@ -265,10 +265,11 @@ suite('EditorService', () => {
265265
{
266266
let untypedEditor: IResourceEditorInput = { resource: URI.file('file.editor-service-override-tests') };
267267
let pane = await openEditor(untypedEditor);
268+
let typedEditor = pane?.input;
268269

269270
assert.strictEqual(pane?.group, rootGroup);
270-
assert.ok(pane.input instanceof TestFileEditorInput);
271-
assert.strictEqual(pane.input.resource.toString(), untypedEditor.resource.toString());
271+
assert.ok(typedEditor instanceof TestFileEditorInput);
272+
assert.strictEqual(typedEditor.resource.toString(), untypedEditor.resource.toString());
272273

273274
assert.strictEqual(editorFactoryCalled, 1);
274275
assert.strictEqual(untitledEditorFactoryCalled, 0);
@@ -278,17 +279,43 @@ suite('EditorService', () => {
278279
assert.ok(!lastUntitledEditorFactoryEditor);
279280
assert.ok(!lastDiffEditorFactoryEditor);
280281

282+
// opening the same editor should not create
283+
// a new editor input
284+
pane = await openEditor(untypedEditor);
285+
assert.strictEqual(pane?.input, typedEditor);
286+
287+
// replaceEditors should work too
288+
let untypedEditorReplacement: IResourceEditorInput = { resource: URI.file('file-replaced.editor-service-override-tests') };
289+
await service.replaceEditors([{
290+
editor: typedEditor,
291+
replacement: untypedEditorReplacement
292+
}], rootGroup);
293+
294+
typedEditor = rootGroup.activeEditor!;
295+
296+
assert.ok(typedEditor instanceof TestFileEditorInput);
297+
assert.strictEqual(typedEditor.resource.toString(), untypedEditorReplacement.resource.toString());
298+
299+
assert.strictEqual(editorFactoryCalled, 3);
300+
assert.strictEqual(untitledEditorFactoryCalled, 0);
301+
assert.strictEqual(diffEditorFactoryCalled, 0);
302+
303+
assert.strictEqual(lastEditorFactoryEditor, untypedEditorReplacement);
304+
assert.ok(!lastUntitledEditorFactoryEditor);
305+
assert.ok(!lastDiffEditorFactoryEditor);
306+
281307
await resetTestState();
282308
}
283309

284310
// untyped resource editor, options (override disabled), no group
285311
{
286312
let untypedEditor: IResourceEditorInput = { resource: URI.file('file.editor-service-override-tests'), options: { override: EditorOverride.DISABLED } };
287313
let pane = await openEditor(untypedEditor);
314+
let typedEditor = pane?.input;
288315

289316
assert.strictEqual(pane?.group, rootGroup);
290-
assert.ok(pane.input instanceof FileEditorInput);
291-
assert.strictEqual(pane.input.resource.toString(), untypedEditor.resource.toString());
317+
assert.ok(typedEditor instanceof FileEditorInput);
318+
assert.strictEqual(typedEditor.resource.toString(), untypedEditor.resource.toString());
292319

293320
assert.strictEqual(editorFactoryCalled, 0);
294321
assert.strictEqual(untitledEditorFactoryCalled, 0);
@@ -298,6 +325,11 @@ suite('EditorService', () => {
298325
assert.ok(!lastUntitledEditorFactoryEditor);
299326
assert.ok(!lastDiffEditorFactoryEditor);
300327

328+
// opening the same editor should not create
329+
// a new editor input
330+
pane = await openEditor(untypedEditor);
331+
assert.strictEqual(pane?.input, typedEditor);
332+
301333
await resetTestState();
302334
}
303335

@@ -458,10 +490,11 @@ suite('EditorService', () => {
458490
{
459491
let typedEditor = new TestFileEditorInput(URI.file('file.editor-service-override-tests'), TEST_EDITOR_INPUT_ID);
460492
let pane = await openEditor({ editor: typedEditor });
493+
let typedInput = pane?.input;
461494

462495
assert.strictEqual(pane?.group, rootGroup);
463-
assert.ok(pane.input instanceof TestFileEditorInput);
464-
assert.strictEqual(pane.input.resource.toString(), typedEditor.resource.toString());
496+
assert.ok(typedInput instanceof TestFileEditorInput);
497+
assert.strictEqual(typedInput.resource.toString(), typedEditor.resource.toString());
465498

466499
assert.strictEqual(editorFactoryCalled, 1);
467500
assert.strictEqual(untitledEditorFactoryCalled, 0);
@@ -471,17 +504,43 @@ suite('EditorService', () => {
471504
assert.ok(!lastUntitledEditorFactoryEditor);
472505
assert.ok(!lastDiffEditorFactoryEditor);
473506

507+
// opening the same editor should not create
508+
// a new editor input
509+
pane = await openEditor(typedEditor);
510+
assert.strictEqual(pane?.input, typedInput);
511+
512+
// replaceEditors should work too
513+
let typedEditorReplacement = new TestFileEditorInput(URI.file('file-replaced.editor-service-override-tests'), TEST_EDITOR_INPUT_ID);
514+
await service.replaceEditors([{
515+
editor: typedEditor,
516+
replacement: typedEditorReplacement
517+
}], rootGroup);
518+
519+
typedInput = rootGroup.activeEditor!;
520+
521+
assert.ok(typedInput instanceof TestFileEditorInput);
522+
assert.strictEqual(typedInput.resource.toString(), typedEditorReplacement.resource.toString());
523+
524+
assert.strictEqual(editorFactoryCalled, 3);
525+
assert.strictEqual(untitledEditorFactoryCalled, 0);
526+
assert.strictEqual(diffEditorFactoryCalled, 0);
527+
528+
assert.strictEqual((lastEditorFactoryEditor as IResourceEditorInput).resource.toString(), typedInput.resource.toString());
529+
assert.ok(!lastUntitledEditorFactoryEditor);
530+
assert.ok(!lastDiffEditorFactoryEditor);
531+
474532
await resetTestState();
475533
}
476534

477535
// typed editor, options (override disabled), no group
478536
{
479537
let typedEditor = new TestFileEditorInput(URI.file('file.editor-service-override-tests'), TEST_EDITOR_INPUT_ID);
480538
let pane = await openEditor({ editor: typedEditor, options: { override: EditorOverride.DISABLED } });
539+
let typedInput = pane?.input;
481540

482541
assert.strictEqual(pane?.group, rootGroup);
483-
assert.ok(pane.input instanceof TestFileEditorInput);
484-
assert.strictEqual(pane.input.resource.toString(), typedEditor.resource.toString());
542+
assert.ok(typedInput instanceof TestFileEditorInput);
543+
assert.strictEqual(typedInput.resource.toString(), typedEditor.resource.toString());
485544

486545
assert.strictEqual(editorFactoryCalled, 0);
487546
assert.strictEqual(untitledEditorFactoryCalled, 0);
@@ -491,6 +550,11 @@ suite('EditorService', () => {
491550
assert.ok(!lastUntitledEditorFactoryEditor);
492551
assert.ok(!lastDiffEditorFactoryEditor);
493552

553+
// opening the same editor should not create
554+
// a new editor input
555+
pane = await openEditor(typedEditor);
556+
assert.strictEqual(pane?.input, typedInput);
557+
494558
await resetTestState();
495559
}
496560

@@ -692,10 +756,11 @@ suite('EditorService', () => {
692756
{
693757
let untypedEditor: IUntitledTextResourceEditorInput = { resource: URI.file('file-original.editor-service-override-tests').with({ scheme: 'untitled' }) };
694758
let pane = await openEditor(untypedEditor);
759+
let typedEditor = pane?.input;
695760

696761
assert.strictEqual(pane?.group, rootGroup);
697-
assert.ok(pane.input instanceof TestFileEditorInput);
698-
assert.strictEqual(pane.input.resource.scheme, 'untitled');
762+
assert.ok(typedEditor instanceof TestFileEditorInput);
763+
assert.strictEqual(typedEditor.resource.scheme, 'untitled');
699764

700765
assert.strictEqual(editorFactoryCalled, 0);
701766
assert.strictEqual(untitledEditorFactoryCalled, 1);
@@ -705,6 +770,11 @@ suite('EditorService', () => {
705770
assert.strictEqual(lastUntitledEditorFactoryEditor, untypedEditor);
706771
assert.ok(!lastDiffEditorFactoryEditor);
707772

773+
// opening the same editor should not create
774+
// a new editor input
775+
pane = await openEditor(untypedEditor);
776+
assert.strictEqual(pane?.input, typedEditor);
777+
708778
await resetTestState();
709779
}
710780

@@ -736,11 +806,16 @@ suite('EditorService', () => {
736806
{
737807
// untyped diff editor, no options, no group
738808
{
739-
let untypedEditor: IResourceDiffEditorInput = { originalInput: { resource: URI.file('file-original.editor-service-override-tests') }, modifiedInput: { resource: URI.file('file-modified.editor-service-override-tests') }, options: { override: 'editorServiceOverrideTests' } };
809+
let untypedEditor: IResourceDiffEditorInput = {
810+
originalInput: { resource: URI.file('file-original.editor-service-override-tests') },
811+
modifiedInput: { resource: URI.file('file-modified.editor-service-override-tests') },
812+
options: { override: 'editorServiceOverrideTests' }
813+
};
740814
let pane = await openEditor(untypedEditor);
815+
let typedEditor = pane?.input;
741816

742817
assert.strictEqual(pane?.group, rootGroup);
743-
assert.ok(pane.input instanceof TestFileEditorInput);
818+
assert.ok(typedEditor instanceof TestFileEditorInput);
744819

745820
assert.strictEqual(editorFactoryCalled, 0);
746821
assert.strictEqual(untitledEditorFactoryCalled, 0);
@@ -755,7 +830,11 @@ suite('EditorService', () => {
755830

756831
// untyped diff editor, no options, SIDE_GROUP
757832
{
758-
let untypedEditor: IResourceDiffEditorInput = { originalInput: { resource: URI.file('file-original.editor-service-override-tests') }, modifiedInput: { resource: URI.file('file-modified.editor-service-override-tests') }, options: { override: 'editorServiceOverrideTests' } };
833+
let untypedEditor: IResourceDiffEditorInput = {
834+
originalInput: { resource: URI.file('file-original.editor-service-override-tests') },
835+
modifiedInput: { resource: URI.file('file-modified.editor-service-override-tests') },
836+
options: { override: 'editorServiceOverrideTests' }
837+
};
759838
let pane = await openEditor(untypedEditor, SIDE_GROUP);
760839

761840
assert.strictEqual(accessor.editorGroupService.groups.length, 2);
@@ -893,8 +972,66 @@ suite('EditorService', () => {
893972
await resetTestState();
894973
}
895974
}
975+
976+
// openEditors with >1 editor
977+
if (useOpenEditors) {
978+
979+
// mix of untyped and typed editors
980+
{
981+
let untypedEditor1: IResourceEditorInput = { resource: URI.file('file1.editor-service-override-tests') };
982+
let untypedEditor2: IResourceEditorInput = { resource: URI.file('file2.editor-service-override-tests'), options: { override: EditorOverride.DISABLED } };
983+
let untypedEditor3: IEditorInputWithOptions = { editor: new TestFileEditorInput(URI.file('file3.editor-service-override-tests'), TEST_EDITOR_INPUT_ID) };
984+
let untypedEditor4: IEditorInputWithOptions = { editor: new TestFileEditorInput(URI.file('file4.editor-service-override-tests'), TEST_EDITOR_INPUT_ID), options: { override: EditorOverride.DISABLED } };
985+
let untypedEditor5: IResourceEditorInput = { resource: URI.file('file5.editor-service-override-tests') };
986+
let pane = (await service.openEditors([untypedEditor1, untypedEditor2, untypedEditor3, untypedEditor4, untypedEditor5]))[0];
987+
988+
assert.strictEqual(pane?.group, rootGroup);
989+
assert.strictEqual(pane?.group.count, 5);
990+
991+
assert.strictEqual(editorFactoryCalled, 3);
992+
assert.strictEqual(untitledEditorFactoryCalled, 0);
993+
assert.strictEqual(diffEditorFactoryCalled, 0);
994+
995+
assert.ok(lastEditorFactoryEditor);
996+
assert.ok(!lastUntitledEditorFactoryEditor);
997+
assert.ok(!lastDiffEditorFactoryEditor);
998+
999+
await resetTestState();
1000+
}
1001+
}
8961002
}
8971003

1004+
test('openEditor() applies options if editor already opened', async () => {
1005+
disposables.add(registerTestFileEditor());
1006+
1007+
const [, service, accessor] = await createEditorService();
1008+
1009+
disposables.add(accessor.editorOverrideService.registerEditor(
1010+
'*.editor-service-override-tests',
1011+
{ id: 'editorServiceOverrideTests', label: 'Label', priority: RegisteredEditorPriority.exclusive },
1012+
{},
1013+
editor => ({ editor: new TestFileEditorInput(editor.resource, 'editorServiceOverrideTests') })
1014+
));
1015+
1016+
// Typed editor
1017+
let pane = await service.openEditor(new TestFileEditorInput(URI.parse('my://resource-openEditors'), 'editorServiceOverrideTests'));
1018+
pane = await service.openEditor(new TestFileEditorInput(URI.parse('my://resource-openEditors'), 'editorServiceOverrideTests'), { sticky: true, preserveFocus: true });
1019+
1020+
assert.ok(pane instanceof TestEditorWithOptions);
1021+
assert.strictEqual(pane.lastSetOptions?.sticky, true);
1022+
assert.strictEqual(pane.lastSetOptions?.preserveFocus, true);
1023+
1024+
await pane.group?.closeAllEditors();
1025+
1026+
// Untyped editor (without registered editor)
1027+
pane = await service.openEditor({ resource: URI.file('resource-openEditors') });
1028+
pane = await service.openEditor({ resource: URI.file('resource-openEditors'), options: { sticky: true, preserveFocus: true } });
1029+
1030+
assert.ok(pane instanceof TestTextFileEditor);
1031+
assert.strictEqual(pane.lastSetOptions?.sticky, true);
1032+
assert.strictEqual(pane.lastSetOptions?.preserveFocus, true);
1033+
});
1034+
8981035
test('isOpen() with side by side editor', async () => {
8991036
const [part, service] = await createEditorService();
9001037

src/vs/workbench/test/browser/workbenchTestServices.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { IConfigurationService, ConfigurationTarget } from 'vs/platform/configur
1818
import { IWorkbenchLayoutService, Parts, Position as PartPosition } from 'vs/workbench/services/layout/browser/layoutService';
1919
import { TextModelResolverService } from 'vs/workbench/services/textmodelResolver/common/textModelResolverService';
2020
import { ITextModelService } from 'vs/editor/common/services/resolverService';
21-
import { IEditorOptions, IResourceEditorInput, IEditorModel, IResourceEditorInputIdentifier, ITextResourceEditorInput } from 'vs/platform/editor/common/editor';
21+
import { IEditorOptions, IResourceEditorInput, IEditorModel, IResourceEditorInputIdentifier, ITextResourceEditorInput, ITextEditorOptions } from 'vs/platform/editor/common/editor';
2222
import { IUntitledTextEditorService, UntitledTextEditorService } from 'vs/workbench/services/untitled/common/untitledTextEditorService';
2323
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
2424
import { ILifecycleService, BeforeShutdownEvent, ShutdownReason, StartupKind, LifecyclePhase, WillShutdownEvent } from 'vs/workbench/services/lifecycle/common/lifecycle';
@@ -168,6 +168,14 @@ export class TestTextResourceEditor extends TextResourceEditor {
168168

169169
export class TestTextFileEditor extends TextFileEditor {
170170

171+
lastSetOptions: ITextEditorOptions | undefined = undefined;
172+
173+
override setOptions(options: ITextEditorOptions | undefined): void {
174+
this.lastSetOptions = options;
175+
176+
super.setOptions(options);
177+
}
178+
171179
protected override createEditorControl(parent: HTMLElement, configuration: any): IEditor {
172180
return this.instantiationService.createInstance(TestCodeEditor, parent, configuration, {});
173181
}
@@ -1310,8 +1318,19 @@ export class TestEditorInput extends EditorInput {
13101318
}
13111319
}
13121320

1321+
export abstract class TestEditorWithOptions extends EditorPane {
1322+
1323+
lastSetOptions: ITextEditorOptions | undefined = undefined;
1324+
1325+
override setOptions(options: ITextEditorOptions | undefined): void {
1326+
this.lastSetOptions = options;
1327+
1328+
super.setOptions(options);
1329+
}
1330+
}
1331+
13131332
export function registerTestEditor(id: string, inputs: SyncDescriptor<EditorInput>[], serializerInputId?: string): IDisposable {
1314-
class TestEditor extends EditorPane {
1333+
class TestEditor extends TestEditorWithOptions {
13151334

13161335
private _scopedContextKeyService: IContextKeyService;
13171336

0 commit comments

Comments
 (0)