提交 cd127d38 编写于 作者: B Benjamin Pasero

grid - introduce some next editor tests

上级 b171096f
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as assert from 'assert';
import { TPromise } from 'vs/base/common/winjs.base';
import * as paths from 'vs/base/common/paths';
import { IEditorInput, IEditorModel } from 'vs/platform/editor/common/editor';
import URI from 'vs/base/common/uri';
import { BaseEditor } from 'vs/workbench/browser/parts/editor/baseEditor';
import { EditorInput, EditorOptions, IFileEditorInput } from 'vs/workbench/common/editor';
import { workbenchInstantiationService } from 'vs/workbench/test/workbenchTestServices';
import { ResourceEditorInput } from 'vs/workbench/common/editor/resourceEditorInput';
import { TestThemeService } from 'vs/platform/theme/test/common/testThemeService';
import { NextEditorService, DelegatingWorkbenchEditorService } from 'vs/workbench/services/editor/browser/nextEditorService';
import { INextEditorGroup, INextEditorGroupsService } from 'vs/workbench/services/group/common/nextEditorGroupsService';
import { NextEditorPart } from 'vs/workbench/browser/parts/editor2/nextEditorPart';
import { Dimension } from 'vs/base/browser/dom';
import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection';
import { INextEditorService } from 'vs/workbench/services/editor/common/nextEditorService';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { NullTelemetryService } from 'vs/platform/telemetry/common/telemetryUtils';
import { IEditorRegistry, EditorDescriptor, Extensions } from 'vs/workbench/browser/editor';
import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors';
import { Registry } from 'vs/platform/registry/common/platform';
export class TestEditorControl extends BaseEditor {
constructor(@ITelemetryService telemetryService: ITelemetryService) { super('MyTestFileEditorForNextEditorService', NullTelemetryService, new TestThemeService()); }
getId(): string { return 'myTestFileEditorForNextEditorService'; }
layout(): void { }
createEditor(): any { }
}
export class TestEditorInput extends EditorInput implements IFileEditorInput {
constructor(private resource: URI) { super(); }
getTypeId() { return 'testFileEditorInputForNextEditorService'; }
resolve(): TPromise<IEditorModel> { return null; }
matches(other: TestEditorInput): boolean { return other && this.resource.toString() === other.resource.toString() && other instanceof TestEditorInput; }
setEncoding(encoding: string) { }
getEncoding(): string { return null; }
setPreferredEncoding(encoding: string) { }
getResource(): URI { return this.resource; }
setForceOpenAsBinary(): void { }
}
suite('NextEditorService editor2', () => {
test('basics', function () {
const partInstantiator = workbenchInstantiationService();
const part = partInstantiator.createInstance(NextEditorPart, 'id', false);
part.create(document.createElement('div'));
part.layout(new Dimension(400, 300));
const testInstantiationService = partInstantiator.createChild(new ServiceCollection([INextEditorGroupsService, part]));
const service: INextEditorService = testInstantiationService.createInstance(NextEditorService);
const input = testInstantiationService.createInstance(TestEditorInput, URI.parse('my://resource'));
let willOpenEditorEventCounter = 0;
const willOpenEditorListener = service.onWillOpenEditor(() => {
willOpenEditorEventCounter++;
});
let activeEditorChangeEventCounter = 0;
const activeEditorChangeListener = service.onDidActiveEditorChange(() => {
activeEditorChangeEventCounter++;
});
let visibleEditorChangeEventCounter = 0;
const visibleEditorChangeListener = service.onDidVisibleEditorsChange(() => {
visibleEditorChangeEventCounter++;
});
let didCloseEditorListenerCounter = 0;
const didCloseEditorListener = service.onDidCloseEditor(editor => {
didCloseEditorListenerCounter++;
});
let willCloseEditorListenerCounter = 0;
const willCloseEditorListener = service.onWillCloseEditor(editor => {
willCloseEditorListenerCounter++;
});
// Open EditorInput
return service.openEditor(input).then(editor => {
assert.ok(editor instanceof TestEditorControl);
assert.equal(editor, service.activeControl);
assert.equal(input, service.activeEditor);
assert.equal(service.visibleControls.length, 1);
assert.equal(service.visibleControls[0], editor);
assert.ok(!service.activeTextEditorControl);
assert.equal(service.visibleTextEditorControls.length, 0);
assert.equal(service.isOpen(input), true);
assert.equal(activeEditorChangeEventCounter, 1);
assert.equal(visibleEditorChangeEventCounter, 1);
assert.equal(willOpenEditorEventCounter, 1);
service.closeEditor(input, editor.group);
assert.equal(willCloseEditorListenerCounter, 1);
assert.equal(didCloseEditorListenerCounter, 1);
activeEditorChangeListener.dispose();
visibleEditorChangeListener.dispose();
willCloseEditorListener.dispose();
didCloseEditorListener.dispose();
willOpenEditorListener.dispose();
});
});
test('caching', function () {
const instantiationService = workbenchInstantiationService();
const service: NextEditorService = <any>instantiationService.createInstance(NextEditorService);
// Cached Input (Files)
const fileResource1 = toFileResource(this, '/foo/bar/cache1.js');
const fileInput1 = service.createInput({ resource: fileResource1 });
assert.ok(fileInput1);
const fileResource2 = toFileResource(this, '/foo/bar/cache2.js');
const fileInput2 = service.createInput({ resource: fileResource2 });
assert.ok(fileInput2);
assert.notEqual(fileInput1, fileInput2);
const fileInput1Again = service.createInput({ resource: fileResource1 });
assert.equal(fileInput1Again, fileInput1);
fileInput1Again.dispose();
assert.ok(fileInput1.isDisposed());
const fileInput1AgainAndAgain = service.createInput({ resource: fileResource1 });
assert.notEqual(fileInput1AgainAndAgain, fileInput1);
assert.ok(!fileInput1AgainAndAgain.isDisposed());
// Cached Input (Resource)
const resource1 = toResource.call(this, '/foo/bar/cache1.js');
const input1 = service.createInput({ resource: resource1 });
assert.ok(input1);
const resource2 = toResource.call(this, '/foo/bar/cache2.js');
const input2 = service.createInput({ resource: resource2 });
assert.ok(input2);
assert.notEqual(input1, input2);
const input1Again = service.createInput({ resource: resource1 });
assert.equal(input1Again, input1);
input1Again.dispose();
assert.ok(input1.isDisposed());
const input1AgainAndAgain = service.createInput({ resource: resource1 });
assert.notEqual(input1AgainAndAgain, input1);
assert.ok(!input1AgainAndAgain.isDisposed());
});
test('delegate', function (done) {
const instantiationService = workbenchInstantiationService();
class MyEditor extends BaseEditor {
constructor(id: string) {
super(id, null, new TestThemeService());
}
getId(): string {
return 'myEditor';
}
public layout(): void {
}
public createEditor(): any {
}
}
const ed = instantiationService.createInstance(MyEditor, 'my.editor');
const inp = instantiationService.createInstance(ResourceEditorInput, 'name', 'description', URI.parse('my://resource'));
const delegate = instantiationService.createInstance(DelegatingWorkbenchEditorService);
delegate.setEditorOpenHandler((group: INextEditorGroup, input: IEditorInput, options?: EditorOptions) => {
assert.strictEqual(input, inp);
done();
return TPromise.as(ed);
});
delegate.openEditor(inp);
});
});
function toResource(path: string) {
return URI.from({ scheme: 'custom', path });
}
function toFileResource(self: any, path: string) {
return URI.file(paths.join('C:\\', Buffer.from(self.test.fullTitle()).toString('base64'), path));
}
Registry.as<IEditorRegistry>(Extensions.Editors).registerEditor(new EditorDescriptor(TestEditorControl, 'MyTestFileEditorForNextEditorService', 'My Test File Editor For Next Editor Service'), new SyncDescriptor(TestEditorInput));
......@@ -24,22 +24,22 @@ import { NullTelemetryService } from 'vs/platform/telemetry/common/telemetryUtil
import { TestThemeService } from 'vs/platform/theme/test/common/testThemeService';
import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors';
export class TestFileEditor extends BaseEditor {
export class TestEditorControl extends BaseEditor {
constructor(@ITelemetryService telemetryService: ITelemetryService) { super('MyTestFileEditor', NullTelemetryService, new TestThemeService()); }
constructor(@ITelemetryService telemetryService: ITelemetryService) { super('MyTestFileEditorForNextEditorGroupService', NullTelemetryService, new TestThemeService()); }
getId(): string { return 'myTestFileEditor'; }
getId(): string { return 'myTestFileEditorForNextEditorGroupService'; }
layout(): void { }
createEditor(): any { }
}
export class TestFileEditorInput extends EditorInput implements IFileEditorInput {
export class TestEditorInput extends EditorInput implements IFileEditorInput {
constructor(private resource: URI) { super(); }
getTypeId() { return 'testFileEditorInput'; }
getTypeId() { return 'testFileEditorInputForNextEditorGroupService'; }
resolve(): TPromise<IEditorModel> { return null; }
matches(other: TestFileEditorInput): boolean { return other && this.resource.toString() === other.resource.toString() && other instanceof TestFileEditorInput; }
matches(other: TestEditorInput): boolean { return other && this.resource.toString() === other.resource.toString() && other instanceof TestEditorInput; }
setEncoding(encoding: string) { }
getEncoding(): string { return null; }
setPreferredEncoding(encoding: string) { }
......@@ -60,7 +60,7 @@ suite('Next editor2 part tests', () => {
constructor() { }
serialize(editorInput: EditorInput): string {
const testEditorInput = <TestFileEditorInput>editorInput;
const testEditorInput = <TestEditorInput>editorInput;
const testInput: ISerializedTestFileEditorInput = {
resource: testEditorInput.getResource().toString()
};
......@@ -71,12 +71,12 @@ suite('Next editor2 part tests', () => {
deserialize(instantiationService: IInstantiationService, serializedEditorInput: string): EditorInput {
const testInput: ISerializedTestFileEditorInput = JSON.parse(serializedEditorInput);
return new TestFileEditorInput(URI.parse(testInput.resource));
return new TestEditorInput(URI.parse(testInput.resource));
}
}
(Registry.as<IEditorInputFactoryRegistry>(EditorExtensions.EditorInputFactories)).registerEditorInputFactory('testFileEditorInput', TestFileEditorInputFactory);
(Registry.as<IEditorRegistry>(Extensions.Editors)).registerEditor(new EditorDescriptor(TestFileEditor, 'MyTestFileEditor', 'My Test File Editor'), new SyncDescriptor(TestFileEditorInput));
(Registry.as<IEditorRegistry>(Extensions.Editors)).registerEditor(new EditorDescriptor(TestEditorControl, 'MyTestFileEditor', 'My Test File Editor'), new SyncDescriptor(TestEditorInput));
}
registerTestFileEditorInput();
......@@ -280,8 +280,8 @@ suite('Next editor2 part tests', () => {
editorCloseCounter++;
});
const input = new TestFileEditorInput(URI.file('foo/bar'));
const inputInactive = new TestFileEditorInput(URI.file('foo/bar/inactive'));
const input = new TestEditorInput(URI.file('foo/bar'));
const inputInactive = new TestEditorInput(URI.file('foo/bar/inactive'));
return group.openEditor(input, EditorOptions.create({ pinned: true })).then(() => {
return group.openEditor(inputInactive, EditorOptions.create({ inactive: true })).then(() => {
......@@ -289,7 +289,7 @@ suite('Next editor2 part tests', () => {
assert.equal(activeEditorChangeCounter, 1);
assert.equal(group.activeEditor, input);
assert.ok(group.activeControl instanceof TestFileEditor);
assert.ok(group.activeControl instanceof TestEditorControl);
assert.equal(group.editors.length, 2);
return group.openEditor(inputInactive).then(() => {
......@@ -310,4 +310,4 @@ suite('Next editor2 part tests', () => {
});
});
});
});
});
\ No newline at end of file
......@@ -14,7 +14,7 @@ import URI from 'vs/base/common/uri';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { NullTelemetryService } from 'vs/platform/telemetry/common/telemetryUtils';
import { StorageService, InMemoryLocalStorage } from 'vs/platform/storage/common/storageService';
import { IEditorGroup, ConfirmResult, IEditorOpeningEvent, IEditorInputWithOptions, CloseDirection } from 'vs/workbench/common/editor';
import { IEditorGroup, ConfirmResult, IEditorOpeningEvent, IEditorInputWithOptions, CloseDirection, IEditorIdentifier } from 'vs/workbench/common/editor';
import { Event, Emitter } from 'vs/base/common/event';
import Severity from 'vs/base/common/severity';
import { IBackupFileService } from 'vs/workbench/services/backup/common/backup';
......@@ -23,7 +23,7 @@ import { IStorageService, StorageScope } from 'vs/platform/storage/common/storag
import { IPartService, Parts, Position as PartPosition, IDimension } from 'vs/workbench/services/part/common/partService';
import { TextModelResolverService } from 'vs/workbench/services/textmodelResolver/common/textModelResolverService';
import { ITextModelService } from 'vs/editor/common/services/resolverService';
import { IEditorInput, IEditorOptions, Position, IEditor, IResourceInput } from 'vs/platform/editor/common/editor';
import { IEditorInput, IEditorOptions, Position, IEditor, IResourceInput, IUntitledResourceInput, IResourceDiffInput, IResourceSideBySideInput } from 'vs/platform/editor/common/editor';
import { IUntitledEditorService, UntitledEditorService } from 'vs/workbench/services/untitled/common/untitledEditorService';
import { IWorkspaceContextService, IWorkspace as IWorkbenchWorkspace, WorkbenchState, IWorkspaceFolder, IWorkspaceFoldersChangeEvent } from 'vs/platform/workspace/common/workspace';
import { ILifecycleService, ShutdownEvent, ShutdownReason, StartupKind, LifecyclePhase } from 'vs/platform/lifecycle/common/lifecycle';
......@@ -71,6 +71,7 @@ import { IKeybindingService } from '../../platform/keybinding/common/keybinding'
import { IDecorationsService, IResourceDecorationChangeEvent, IDecoration, IDecorationData, IDecorationsProvider } from 'vs/workbench/services/decorations/browser/decorations';
import { IDisposable, toDisposable } from 'vs/base/common/lifecycle';
import { INextEditorGroupsService, INextEditorGroup, GroupsOrder, GroupsArrangement, GroupDirection, IAddGroupOptions, IMergeGroupOptions, IMoveEditorOptions, ICopyEditorOptions, IEditorReplacement } from 'vs/workbench/services/group/common/nextEditorGroupsService';
import { INextEditorService } from 'vs/workbench/services/editor/common/nextEditorService';
export function createFileInput(instantiationService: IInstantiationService, resource: URI): FileEditorInput {
return instantiationService.createInstance(FileEditorInput, resource, void 0);
......@@ -276,6 +277,7 @@ export function workbenchInstantiationService(): IInstantiationService {
instantiationService.stub(IThemeService, new TestThemeService());
instantiationService.stub(IHashService, new TestHashService());
instantiationService.stub(INextEditorGroupsService, new TestNextEditorGroupsService([new TestNextEditorGroup(0)]));
instantiationService.stub(INextEditorService, new TestNextEditorService());
return instantiationService;
}
......@@ -846,6 +848,49 @@ export class TestNextEditorGroup implements INextEditorGroup {
}
}
export class TestNextEditorService implements INextEditorService {
_serviceBrand: ServiceIdentifier<any>;
onDidActiveEditorChange: Event<void> = Event.None;
onDidVisibleEditorsChange: Event<void> = Event.None;
onWillCloseEditor: Event<IEditorIdentifier> = Event.None;
onDidCloseEditor: Event<IEditorIdentifier> = Event.None;
onWillOpenEditor: Event<IEditorOpeningEvent> = Event.None;
onDidOpenEditorFail: Event<IEditorIdentifier> = Event.None;
activeControl: IEditor;
activeTextEditorControl: any;
activeEditor: IEditorInput;
visibleControls: ReadonlyArray<IEditor>;
visibleTextEditorControls = [];
visibleEditors: ReadonlyArray<IEditorInput>;
openEditor(editor: any, options?: any, group?: any) {
return TPromise.as(null);
}
openEditors(editors: any, group?: any) {
return TPromise.as(null);
}
isOpen(editor: IEditorInput | IResourceInput | IUntitledResourceInput): boolean {
return false;
}
closeEditor(editor: IEditorInput, group: number | INextEditorGroup): Thenable<void> {
return TPromise.as(null);
}
invokeWithinEditorContext<T>(fn: (accessor: ServicesAccessor) => T): T {
return fn(null);
}
createInput(input: IResourceInput | IUntitledResourceInput | IResourceDiffInput | IResourceSideBySideInput): IEditorInput {
return null;
}
}
export class TestFileService implements IFileService {
public _serviceBrand: any;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册