servicesTestUtils.ts 16.5 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8
/*---------------------------------------------------------------------------------------------
 *  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 {Promise, TPromise} from 'vs/base/common/winjs.base';
9
import {TestInstantiationService} from 'vs/test/utils/instantiationTestUtils';
E
Erich Gamma 已提交
10 11 12
import EventEmitter = require('vs/base/common/eventEmitter');
import Paths = require('vs/base/common/paths');
import URI from 'vs/base/common/uri';
13
import {ITelemetryService} from 'vs/platform/telemetry/common/telemetry';
14
import Storage = require('vs/workbench/common/storage');
15
import {EditorInputEvent, IEditorGroup} from 'vs/workbench/common/editor';
16
import Event, {Emitter} from 'vs/base/common/event';
E
Erich Gamma 已提交
17
import Severity from 'vs/base/common/severity';
B
Benjamin Pasero 已提交
18
import {IConfigurationService, getConfigurationValue, IConfigurationValue} from 'vs/platform/configuration/common/configuration';
E
Erich Gamma 已提交
19 20
import {IStorageService, StorageScope} from 'vs/platform/storage/common/storage';
import WorkbenchEditorService = require('vs/workbench/services/editor/common/editorService');
21
import QuickOpenService = require('vs/workbench/services/quickopen/common/quickOpenService');
E
Erich Gamma 已提交
22
import PartService = require('vs/workbench/services/part/common/partService');
23
import WorkspaceContextService = require('vs/platform/workspace/common/workspace');
24
import {IEditorInput, IEditorModel, Position, Direction, IEditor, IResourceInput, ITextEditorModel} from 'vs/platform/editor/common/editor';
E
Erich Gamma 已提交
25
import {IEventService} from 'vs/platform/event/common/event';
26
import {IUntitledEditorService} from 'vs/workbench/services/untitled/common/untitledEditorService';
E
Erich Gamma 已提交
27
import {IMessageService, IConfirmation} from 'vs/platform/message/common/message';
28
import {IWorkspace} from 'vs/platform/workspace/common/workspace';
29
import {ILifecycleService, ShutdownEvent} from 'vs/platform/lifecycle/common/lifecycle';
30
import {EditorStacksModel} from 'vs/workbench/common/editor/editorStacksModel';
B
Benjamin Pasero 已提交
31 32
import {ServiceCollection} from 'vs/platform/instantiation/common/serviceCollection';
import {InstantiationService} from 'vs/platform/instantiation/common/instantiationService';
33
import {IEditorGroupService, GroupArrangement} from 'vs/workbench/services/group/common/groupService';
34
import {TextFileService} from 'vs/workbench/parts/files/common/textFileServices';
A
Alex Dima 已提交
35
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
36
import {IFileService, IResolveContentOptions} from 'vs/platform/files/common/files';
A
Alex Dima 已提交
37
import {IModelService} from 'vs/editor/common/services/modelService';
38
import {ModelServiceImpl} from 'vs/editor/common/services/modelServiceImpl';
39 40
import {IRawTextContent} from 'vs/workbench/parts/files/common/files';
import {RawText} from 'vs/editor/common/model/textModel';
41 42
import {parseArgs} from 'vs/code/node/argv';
import {EnvironmentService} from 'vs/platform/environment/node/environmentService';
E
Erich Gamma 已提交
43 44 45 46

export const TestWorkspace: IWorkspace = {
	resource: URI.file('C:\\testWorkspace'),
	name: 'Test Workspace',
47
	uid: new Date().getTime()
E
Erich Gamma 已提交
48 49
};

J
Joao Moreno 已提交
50
export const TestEnvironmentService = new EnvironmentService(parseArgs(process.argv), process.execPath);
51

E
Erich Gamma 已提交
52
export class TestContextService implements WorkspaceContextService.IWorkspaceContextService {
53
	public _serviceBrand: any;
E
Erich Gamma 已提交
54 55 56 57

	private workspace: any;
	private options: any;

58
	constructor(workspace: any = TestWorkspace, options: any = null) {
E
Erich Gamma 已提交
59
		this.workspace = workspace;
60
		this.options = options || Object.create(null);
E
Erich Gamma 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
	}

	public getWorkspace(): IWorkspace {
		return this.workspace;
	}

	public getOptions() {
		return this.options;
	}

	public updateOptions() {

	}

	public isInsideWorkspace(resource: URI): boolean {
		if (resource && this.workspace) {
			return Paths.isEqualOrParent(resource.fsPath, this.workspace.resource.fsPath);
		}

		return false;
	}

	public toWorkspaceRelativePath(resource: URI): string {
84
		return Paths.makePosixAbsolute(Paths.normalize(resource.fsPath.substr('c:'.length)));
E
Erich Gamma 已提交
85 86 87 88 89 90 91
	}

	public toResource(workspaceRelativePath: string): URI {
		return URI.file(Paths.join('C:\\', workspaceRelativePath));
	}
}

A
Alex Dima 已提交
92 93 94 95 96 97 98 99 100 101 102
export abstract class TestTextFileService extends TextFileService {

	constructor(
		@WorkspaceContextService.IWorkspaceContextService contextService: WorkspaceContextService.IWorkspaceContextService,
		@IInstantiationService instantiationService: IInstantiationService,
		@IConfigurationService configurationService: IConfigurationService,
		@ITelemetryService telemetryService: ITelemetryService,
		@WorkbenchEditorService.IWorkbenchEditorService editorService: WorkbenchEditorService.IWorkbenchEditorService,
		@IEditorGroupService editorGroupService: IEditorGroupService,
		@IEventService eventService: IEventService,
		@IFileService fileService: IFileService,
B
Benjamin Pasero 已提交
103
		@IModelService modelService: IModelService
A
Alex Dima 已提交
104
	) {
105
		super(contextService, instantiationService, configurationService, telemetryService, editorService, eventService, fileService, modelService);
A
Alex Dima 已提交
106
	}
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123

	public resolveTextContent(resource: URI, options?: IResolveContentOptions): TPromise<IRawTextContent> {
		return this.fileService.resolveContent(resource, options).then((content) => {
			const raw = RawText.fromString(content.value, { defaultEOL: 1, detectIndentation: false, insertSpaces: false, tabSize: 4, trimAutoWhitespace: false });

			return <IRawTextContent> {
				resource: content.resource,
					name: content.name,
					mtime: content.mtime,
					etag: content.etag,
					mime: content.mime,
					encoding: content.encoding,
					value: raw,
					valueLogicalHash: null
			};
		});
	}
A
Alex Dima 已提交
124 125
}

E
Erich Gamma 已提交
126
export class TestMessageService implements IMessageService {
127
	public _serviceBrand: any;
E
Erich Gamma 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154

	private counter: number;

	constructor() {
		this.counter = 0;
	}

	public show(sev: Severity, message: any): () => void {
		this.counter++;

		return null;
	}

	public getCounter() {
		return this.counter;
	}

	public hideAll(): void {
		// No-op
	}

	public confirm(confirmation: IConfirmation): boolean {
		return false;
	}
}

export class TestPartService implements PartService.IPartService {
155
	public _serviceBrand: any;
E
Erich Gamma 已提交
156 157 158 159 160 161 162 163

	public layout(): void { }

	public isCreated(): boolean {
		return true;
	}

	public joinCreation(): Promise {
A
Alex Dima 已提交
164
		return TPromise.as(null);
E
Erich Gamma 已提交
165 166 167 168 169 170 171 172 173 174
	}

	public hasFocus(part): boolean {
		return false;
	}

	public isVisible(part): boolean {
		return true;
	}

175 176 177 178 179 180
	public isStatusBarHidden(): boolean {
		return false;
	}

	public setStatusBarHidden(hidden: boolean): void { }

E
Erich Gamma 已提交
181 182 183 184 185 186
	public isSideBarHidden(): boolean {
		return false;
	}

	public setSideBarHidden(hidden: boolean): void { }

I
isidor 已提交
187
	public isPanelHidden(): boolean {
I
isidor 已提交
188 189 190
		return false;
	}

I
isidor 已提交
191
	public setPanelHidden(hidden: boolean): void { }
I
isidor 已提交
192

E
Erich Gamma 已提交
193 194 195 196 197 198 199
	public getSideBarPosition() {
		return 0;
	}

	public setSideBarPosition(position): void { }
	public addClass(clazz: string): void { }
	public removeClass(clazz: string): void { }
200
	public getWorkbenchElementId(): string { return ''; }
E
Erich Gamma 已提交
201 202 203
}

export class TestEventService extends EventEmitter.EventEmitter implements IEventService {
204
	public _serviceBrand: any;
E
Erich Gamma 已提交
205 206 207
}

export class TestStorageService extends EventEmitter.EventEmitter implements IStorageService {
208
	public _serviceBrand: any;
E
Erich Gamma 已提交
209 210 211 212 213 214 215

	private storage: Storage.Storage;

	constructor() {
		super();

		let context = new TestContextService();
216
		this.storage = new Storage.Storage(new Storage.InMemoryLocalStorage(), null, context);
E
Erich Gamma 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
	}

	store(key: string, value: any, scope: StorageScope = StorageScope.GLOBAL): void {
		this.storage.store(key, value, scope);
	}

	swap(key: string, valueA: any, valueB: any, scope: StorageScope = StorageScope.GLOBAL, defaultValue?: any): void {
		this.storage.swap(key, valueA, valueB, scope, defaultValue);
	}

	remove(key: string, scope: StorageScope = StorageScope.GLOBAL): void {
		this.storage.remove(key, scope);
	}

	get(key: string, scope: StorageScope = StorageScope.GLOBAL, defaultValue?: string): string {
		return this.storage.get(key, scope, defaultValue);
	}

	getInteger(key: string, scope: StorageScope = StorageScope.GLOBAL, defaultValue?: number): number {
		return this.storage.getInteger(key, scope, defaultValue);
	}

	getBoolean(key: string, scope: StorageScope = StorageScope.GLOBAL, defaultValue?: boolean): boolean {
		return this.storage.getBoolean(key, scope, defaultValue);
	}
}

244
export class TestUntitledEditorService implements IUntitledEditorService {
245
	public _serviceBrand: any;
E
Erich Gamma 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258

	public get(resource: URI) {
		return null;
	}

	public getAll() {
		return [];
	}

	public getDirty() {
		return [];
	}

259 260 261 262
	public 	revertAll(resources?: URI[]): URI[] {
		return [];
	}

E
Erich Gamma 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275
	public isDirty() {
		return false;
	}

	public createOrGet(resource?: URI) {
		return null;
	}

	public hasAssociatedFilePath(resource: URI) {
		return false;
	}
}

276
export class TestEditorGroupService implements IEditorGroupService {
277
	public _serviceBrand: any;
278 279

	private stacksModel: EditorStacksModel;
280 281 282 283

	private _onEditorsChanged: Emitter<void>;
	private _onEditorOpening: Emitter<EditorInputEvent>;
	private _onEditorOpenFail: Emitter<IEditorInput>;
284
	private _onEditorsMoved: Emitter<void>;
285 286

	constructor(callback?: (method: string) => void) {
287
		this._onEditorsMoved = new Emitter<void>();
288 289 290 291
		this._onEditorsChanged = new Emitter<void>();
		this._onEditorOpening = new Emitter<EditorInputEvent>();
		this._onEditorOpenFail = new Emitter<IEditorInput>();

292 293 294 295 296 297 298 299 300
		let services = new ServiceCollection();

		services.set(IStorageService, new TestStorageService());
		services.set(WorkspaceContextService.IWorkspaceContextService, new TestContextService());
		const lifecycle = new TestLifecycleService();
		services.set(ILifecycleService, lifecycle);

		let inst = new InstantiationService(services);

301
		this.stacksModel = inst.createInstance(EditorStacksModel, true);
302 303
	}

304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
	public fireChange(): void {
		this._onEditorsChanged.fire();
	}

	public get onEditorsChanged(): Event<void> {
		return this._onEditorsChanged.event;
	}

	public get onEditorOpening(): Event<EditorInputEvent> {
		return this._onEditorOpening.event;
	}

	public get onEditorOpenFail(): Event<IEditorInput> {
		return this._onEditorOpenFail.event;
	}

320 321 322 323
	public get onEditorsMoved(): Event<void> {
		return this._onEditorsMoved.event;
	}

324 325 326
	public focusGroup(group: IEditorGroup): void;
	public focusGroup(position: Position): void;
	public focusGroup(arg1: any): void {
327 328 329

	}

330 331 332
	public activateGroup(group: IEditorGroup): void;
	public activateGroup(position: Position): void;
	public activateGroup(arg1: any): void {
333 334 335

	}

336 337 338
	public moveGroup(from: IEditorGroup, to: IEditorGroup): void;
	public moveGroup(from: Position, to: Position): void;
	public moveGroup(arg1: any, arg2: any): void {
339 340 341 342 343 344 345

	}

	public arrangeGroups(arrangement: GroupArrangement): void {

	}

346 347 348
	public pinEditor(group: IEditorGroup, input: IEditorInput): void;
	public pinEditor(position: Position, input: IEditorInput): void;
	public pinEditor(arg1: any, input: IEditorInput): void {
349 350
	}

351 352 353
	public unpinEditor(group: IEditorGroup, input: IEditorInput): void;
	public unpinEditor(position: Position, input: IEditorInput): void;
	public unpinEditor(arg1: any, input: IEditorInput): void {
354 355
	}

356 357 358
	public moveEditor(input: IEditorInput, from: IEditorGroup, to: IEditorGroup, index?: number): void;
	public moveEditor(input: IEditorInput, from: Position, to: Position, index?: number): void;
	public moveEditor(input: IEditorInput, from: any, to: any, index?: number): void {
359 360 361 362 363 364 365
	}

	public getStacksModel(): EditorStacksModel {
		return this.stacksModel;
	}
}

E
Erich Gamma 已提交
366
export class TestEditorService implements WorkbenchEditorService.IWorkbenchEditorService {
367
	public _serviceBrand: any;
E
Erich Gamma 已提交
368 369 370 371 372 373 374 375 376

	public activeEditorInput;
	public activeEditorOptions;
	public activeEditorPosition;

	private callback: (method: string) => void;

	constructor(callback?: (method: string) => void) {
		this.callback = callback || ((s: string) => { });
B
Benjamin Pasero 已提交
377 378
	}

B
Benjamin Pasero 已提交
379
	public openEditors(inputs): Promise {
A
Alex Dima 已提交
380
		return TPromise.as([]);
E
Erich Gamma 已提交
381 382
	}

383 384 385 386
	public replaceEditors(editors): TPromise<IEditor[]> {
		return TPromise.as([]);
	}

387 388 389 390
	public closeEditors(position: Position, except?: IEditorInput, direction?: Direction): TPromise<void> {
		return TPromise.as(null);
	}

391
	public closeAllEditors(except?: Position): TPromise<void> {
A
Alex Dima 已提交
392
		return TPromise.as(null);
E
Erich Gamma 已提交
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
	}

	public isVisible(input: IEditorInput, includeDiff: boolean): boolean {
		return false;
	}

	public getActiveEditor(): IEditor {
		this.callback('getActiveEditor');

		return null;
	}

	public getActiveEditorInput(): IEditorInput {
		this.callback('getActiveEditorInput');

B
Benjamin Pasero 已提交
408
		return this.activeEditorInput;
E
Erich Gamma 已提交
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
	}

	public getVisibleEditors(): IEditor[] {
		this.callback('getVisibleEditors');

		return [];
	}

	public openEditor(input: any, options?: any, position?: any): Promise {
		this.callback('openEditor');

		this.activeEditorInput = input;
		this.activeEditorOptions = options;
		this.activeEditorPosition = position;

A
Alex Dima 已提交
424
		return TPromise.as(null);
E
Erich Gamma 已提交
425 426 427 428 429 430 431 432 433 434
	}

	public resolveEditorModel(input: IEditorInput, refresh?: boolean): TPromise<IEditorModel>;
	public resolveEditorModel(input: IResourceInput, refresh?: boolean): TPromise<ITextEditorModel>;
	public resolveEditorModel(input: any, refresh?: boolean): Promise {
		this.callback('resolveEditorModel');

		return input.resolve(refresh);
	}

435
	public closeEditor(position: Position, input: IEditorInput): TPromise<void> {
E
Erich Gamma 已提交
436 437 438 439 440
		this.callback('closeEditor');

		return TPromise.as(null);
	}

441
	public createInput(input: IResourceInput): TPromise<IEditorInput> {
A
Alex Dima 已提交
442
		return TPromise.as(null);
E
Erich Gamma 已提交
443 444 445 446
	}
}

export class TestQuickOpenService implements QuickOpenService.IQuickOpenService {
447
	public _serviceBrand: any;
E
Erich Gamma 已提交
448 449 450

	private callback: (prefix: string) => void;

451
	constructor(callback?: (prefix: string) => void) {
E
Erich Gamma 已提交
452 453 454
		this.callback = callback;
	}

455
	pick(arg: any, options?: any, token?: any): Promise {
A
Alex Dima 已提交
456
		return TPromise.as(null);
E
Erich Gamma 已提交
457 458
	}

459
	input(options?: any, token?: any): Promise {
A
Alex Dima 已提交
460
		return TPromise.as(null);
E
Erich Gamma 已提交
461 462
	}

463 464
	accept(): void {
	}
J
Joao Moreno 已提交
465

466 467 468 469
	focus(): void {
	}

	close(): void {
470 471
	}

B
Benjamin Pasero 已提交
472
	show(prefix?: string, options?: any): Promise {
B
tslint  
Benjamin Pasero 已提交
473 474 475
		if (this.callback) {
			this.callback(prefix);
		}
E
Erich Gamma 已提交
476

A
Alex Dima 已提交
477
		return TPromise.as(true);
E
Erich Gamma 已提交
478 479
	}

480
	get onShow(): Event<void> {
E
Erich Gamma 已提交
481 482 483
		return null;
	}

484
	get onHide(): Event<void> {
E
Erich Gamma 已提交
485 486 487
		return null;
	}

B
Benjamin Pasero 已提交
488 489
	public dispose() { }
	public quickNavigate(): void { }
E
Erich Gamma 已提交
490 491 492
}

export const TestFileService = {
B
Benjamin Pasero 已提交
493
	resolveContent: function (resource) {
A
Alex Dima 已提交
494
		return TPromise.as({
E
Erich Gamma 已提交
495 496 497 498
			resource: resource,
			value: 'Hello Html',
			etag: 'index.txt',
			mime: 'text/plain',
B
Benjamin Pasero 已提交
499
			encoding: 'utf8',
E
Erich Gamma 已提交
500 501 502 503 504
			mtime: new Date().getTime(),
			name: Paths.basename(resource.fsPath)
		});
	},

A
Alex Dima 已提交
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
	resolveStreamContent: function (resource) {
		return TPromise.as({
			resource: resource,
			value: {
				on: (event:string, callback:Function): void => {
					if (event === 'data') {
						callback('Hello Html');
					}
					if (event === 'end') {
						callback();
					}
				}
			},
			etag: 'index.txt',
			mime: 'text/plain',
			encoding: 'utf8',
			mtime: new Date().getTime(),
			name: Paths.basename(resource.fsPath)
		});
	},

B
Benjamin Pasero 已提交
526
	updateContent: function (res) {
527
		return TPromise.timeout(1).then(() => {
E
Erich Gamma 已提交
528 529 530 531
			return {
				resource: res,
				etag: 'index.txt',
				mime: 'text/plain',
B
Benjamin Pasero 已提交
532
				encoding: 'utf8',
E
Erich Gamma 已提交
533 534 535 536 537
				mtime: new Date().getTime(),
				name: Paths.basename(res.fsPath)
			};
		});
	}
B
linting  
Benjamin Pasero 已提交
538
};
539 540

export class TestConfigurationService extends EventEmitter.EventEmitter implements IConfigurationService {
541
	public _serviceBrand: any;
542

543 544
	private configuration = Object.create(null);

545
	public reloadConfiguration<T>(section?: string): TPromise<T> {
546 547 548
		return TPromise.as(this.getConfiguration());
	}

549
	public getConfiguration(): any {
550
		return this.configuration;
551 552
	}

B
Benjamin Pasero 已提交
553
	public setUserConfiguration(key: any, value: any): Thenable<void> {
554
		this.configuration[key] = value;
B
Benjamin Pasero 已提交
555 556
		return TPromise.as(null);
	}
557 558 559 560

	public onDidUpdateConfiguration() {
		return { dispose() { } };
	}
B
Benjamin Pasero 已提交
561 562 563 564 565 566 567 568

	public lookup<C>(key: string): IConfigurationValue<C> {
		return {
			value: getConfigurationValue<C>(this.getConfiguration(), key),
			default: getConfigurationValue<C>(this.getConfiguration(), key),
			user: getConfigurationValue<C>(this.getConfiguration(), key)
		};
	}
569 570 571 572
}

export class TestLifecycleService implements ILifecycleService {

573
	public _serviceBrand: any;
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591

	private _onWillShutdown = new Emitter<ShutdownEvent>();
	private _onShutdown = new Emitter<void>();

	constructor() {
	}

	public fireShutdown(): void {
		this._onShutdown.fire();
	}

	public get onWillShutdown(): Event<ShutdownEvent> {
		return this._onWillShutdown.event;
	}

	public get onShutdown(): Event<void> {
		return this._onShutdown.event;
	}
592 593 594 595 596
}

export function createMockModelService(instantiationService: TestInstantiationService): IModelService {
	instantiationService.stub(IConfigurationService, new TestConfigurationService());
	return instantiationService.createInstance(ModelServiceImpl);
E
Erich Gamma 已提交
597
}