servicesTestUtils.ts 17.2 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';
A
Alex Dima 已提交
13
import {NullTelemetryService, 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 18 19
import Types = require('vs/base/common/types');
import Severity from 'vs/base/common/severity';
import http = require('vs/base/common/http');
20
import {IConfigurationService} from 'vs/platform/configuration/common/configuration';
21
import {IContent, IStat} from 'vs/platform/configuration/common/configurationService';
E
Erich Gamma 已提交
22 23
import {IStorageService, StorageScope} from 'vs/platform/storage/common/storage';
import WorkbenchEditorService = require('vs/workbench/services/editor/common/editorService');
24
import QuickOpenService = require('vs/workbench/services/quickopen/common/quickOpenService');
E
Erich Gamma 已提交
25 26
import PartService = require('vs/workbench/services/part/common/partService');
import WorkspaceContextService = require('vs/workbench/services/workspace/common/contextService');
27
import {IEditorInput, IEditorModel, Position, Direction, IEditor, IResourceInput, ITextEditorModel} from 'vs/platform/editor/common/editor';
E
Erich Gamma 已提交
28
import {IEventService} from 'vs/platform/event/common/event';
29
import {IUntitledEditorService} from 'vs/workbench/services/untitled/common/untitledEditorService';
E
Erich Gamma 已提交
30
import {IMessageService, IConfirmation} from 'vs/platform/message/common/message';
B
linting  
Benjamin Pasero 已提交
31
import {IWorkspace, IConfiguration} from 'vs/platform/workspace/common/workspace';
32
import {ILifecycleService, ShutdownEvent} from 'vs/platform/lifecycle/common/lifecycle';
33
import {EditorStacksModel} from 'vs/workbench/common/editor/editorStacksModel';
B
Benjamin Pasero 已提交
34 35
import {ServiceCollection} from 'vs/platform/instantiation/common/serviceCollection';
import {InstantiationService} from 'vs/platform/instantiation/common/instantiationService';
36
import {IEditorGroupService, GroupArrangement} from 'vs/workbench/services/group/common/groupService';
37
import {TextFileService} from 'vs/workbench/parts/files/common/textFileServices';
A
Alex Dima 已提交
38
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
39
import {IFileService, IResolveContentOptions} from 'vs/platform/files/common/files';
A
Alex Dima 已提交
40
import {IModelService} from 'vs/editor/common/services/modelService';
41
import {ModelServiceImpl} from 'vs/editor/common/services/modelServiceImpl';
42 43
import {IRawTextContent} from 'vs/workbench/parts/files/common/files';
import {RawText} from 'vs/editor/common/model/textModel';
44 45
import {parseArgs} from 'vs/code/node/argv';
import {EnvironmentService} from 'vs/platform/environment/node/environmentService';
E
Erich Gamma 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58

export const TestWorkspace: IWorkspace = {
	resource: URI.file('C:\\testWorkspace'),
	id: 'testWorkspace',
	name: 'Test Workspace',
	uid: new Date().getTime(),
	mtime: new Date().getTime()
};

export const TestConfiguration: IConfiguration = {
	env: Object.create(null)
};

59 60
export const TestEnvironmentService = new EnvironmentService(parseArgs(process.argv));

E
Erich Gamma 已提交
61
export class TestContextService implements WorkspaceContextService.IWorkspaceContextService {
62
	public _serviceBrand: any;
E
Erich Gamma 已提交
63 64 65 66 67 68 69 70

	private workspace: any;
	private configuration: any;
	private options: any;

	constructor(workspace: any = TestWorkspace, configuration: any = TestConfiguration, options: any = null) {
		this.workspace = workspace;
		this.configuration = configuration;
71 72 73 74 75
		this.options = options || {
			globalSettings: {
				settings: {}
			}
		};
E
Erich Gamma 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
	}

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

	public getConfiguration(): IConfiguration {
		return this.configuration;
	}

	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 {
103
		return Paths.makePosixAbsolute(Paths.normalize(resource.fsPath.substr('c:'.length)));
E
Erich Gamma 已提交
104 105 106 107 108 109 110
	}

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

A
Alex Dima 已提交
111 112 113 114 115 116 117 118 119 120 121
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 已提交
122
		@IModelService modelService: IModelService
A
Alex Dima 已提交
123
	) {
B
Benjamin Pasero 已提交
124
		super(contextService, instantiationService, configurationService, telemetryService, editorService, editorGroupService, eventService, fileService, modelService);
A
Alex Dima 已提交
125
	}
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

	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 已提交
143 144
}

E
Erich Gamma 已提交
145
export class TestMessageService implements IMessageService {
146
	public _serviceBrand: any;
E
Erich Gamma 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173

	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 {
174
	public _serviceBrand: any;
E
Erich Gamma 已提交
175 176 177 178 179 180 181 182

	public layout(): void { }

	public isCreated(): boolean {
		return true;
	}

	public joinCreation(): Promise {
A
Alex Dima 已提交
183
		return TPromise.as(null);
E
Erich Gamma 已提交
184 185 186 187 188 189 190 191 192 193
	}

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

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

194 195 196 197 198 199
	public isStatusBarHidden(): boolean {
		return false;
	}

	public setStatusBarHidden(hidden: boolean): void { }

E
Erich Gamma 已提交
200 201 202 203 204 205
	public isSideBarHidden(): boolean {
		return false;
	}

	public setSideBarHidden(hidden: boolean): void { }

I
isidor 已提交
206
	public isPanelHidden(): boolean {
I
isidor 已提交
207 208 209
		return false;
	}

I
isidor 已提交
210
	public setPanelHidden(hidden: boolean): void { }
I
isidor 已提交
211

E
Erich Gamma 已提交
212 213 214 215 216 217 218 219 220 221
	public getSideBarPosition() {
		return 0;
	}

	public setSideBarPosition(position): void { }
	public addClass(clazz: string): void { }
	public removeClass(clazz: string): void { }
}

export class TestEventService extends EventEmitter.EventEmitter implements IEventService {
222
	public _serviceBrand: any;
E
Erich Gamma 已提交
223 224 225
}

export class TestStorageService extends EventEmitter.EventEmitter implements IStorageService {
226
	public _serviceBrand: any;
E
Erich Gamma 已提交
227 228 229 230 231 232 233

	private storage: Storage.Storage;

	constructor() {
		super();

		let context = new TestContextService();
234
		this.storage = new Storage.Storage(new Storage.InMemoryLocalStorage(), null, context);
E
Erich Gamma 已提交
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
	}

	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);
	}
}

262
export class TestUntitledEditorService implements IUntitledEditorService {
263
	public _serviceBrand: any;
E
Erich Gamma 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276

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

	public getAll() {
		return [];
	}

	public getDirty() {
		return [];
	}

277 278 279 280
	public 	revertAll(resources?: URI[]): URI[] {
		return [];
	}

E
Erich Gamma 已提交
281 282 283 284 285 286 287 288 289 290 291 292 293
	public isDirty() {
		return false;
	}

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

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

294
export class TestEditorGroupService implements IEditorGroupService {
295
	public _serviceBrand: any;
296 297

	private stacksModel: EditorStacksModel;
298 299 300 301

	private _onEditorsChanged: Emitter<void>;
	private _onEditorOpening: Emitter<EditorInputEvent>;
	private _onEditorOpenFail: Emitter<IEditorInput>;
302
	private _onEditorsMoved: Emitter<void>;
303 304

	constructor(callback?: (method: string) => void) {
305
		this._onEditorsMoved = new Emitter<void>();
306 307 308 309
		this._onEditorsChanged = new Emitter<void>();
		this._onEditorOpening = new Emitter<EditorInputEvent>();
		this._onEditorOpenFail = new Emitter<IEditorInput>();

310 311 312 313 314 315 316 317 318 319 320 321
		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);

		this.stacksModel = inst.createInstance(EditorStacksModel);
	}

322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
	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;
	}

338 339 340 341
	public get onEditorsMoved(): Event<void> {
		return this._onEditorsMoved.event;
	}

342 343 344
	public focusGroup(group: IEditorGroup): void;
	public focusGroup(position: Position): void;
	public focusGroup(arg1: any): void {
345 346 347

	}

348 349 350
	public activateGroup(group: IEditorGroup): void;
	public activateGroup(position: Position): void;
	public activateGroup(arg1: any): void {
351 352 353

	}

354 355 356
	public moveGroup(from: IEditorGroup, to: IEditorGroup): void;
	public moveGroup(from: Position, to: Position): void;
	public moveGroup(arg1: any, arg2: any): void {
357 358 359 360 361 362 363

	}

	public arrangeGroups(arrangement: GroupArrangement): void {

	}

364 365 366
	public pinEditor(group: IEditorGroup, input: IEditorInput): void;
	public pinEditor(position: Position, input: IEditorInput): void;
	public pinEditor(arg1: any, input: IEditorInput): void {
367 368
	}

369 370 371
	public unpinEditor(group: IEditorGroup, input: IEditorInput): void;
	public unpinEditor(position: Position, input: IEditorInput): void;
	public unpinEditor(arg1: any, input: IEditorInput): void {
372 373
	}

374 375 376
	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 {
377 378 379 380 381 382 383
	}

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

E
Erich Gamma 已提交
384
export class TestEditorService implements WorkbenchEditorService.IWorkbenchEditorService {
385
	public _serviceBrand: any;
E
Erich Gamma 已提交
386 387 388 389 390 391 392 393 394

	public activeEditorInput;
	public activeEditorOptions;
	public activeEditorPosition;

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

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

B
Benjamin Pasero 已提交
397
	public openEditors(inputs): Promise {
A
Alex Dima 已提交
398
		return TPromise.as([]);
E
Erich Gamma 已提交
399 400
	}

401 402 403 404
	public replaceEditors(editors): TPromise<IEditor[]> {
		return TPromise.as([]);
	}

405 406 407 408
	public closeEditors(position: Position, except?: IEditorInput, direction?: Direction): TPromise<void> {
		return TPromise.as(null);
	}

409
	public closeAllEditors(except?: Position): TPromise<void> {
A
Alex Dima 已提交
410
		return TPromise.as(null);
E
Erich Gamma 已提交
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
	}

	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 已提交
426
		return this.activeEditorInput;
E
Erich Gamma 已提交
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
	}

	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 已提交
442
		return TPromise.as(null);
E
Erich Gamma 已提交
443 444 445 446 447 448 449 450 451 452
	}

	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);
	}

453
	public closeEditor(position: Position, input: IEditorInput): TPromise<void> {
E
Erich Gamma 已提交
454 455 456 457 458
		this.callback('closeEditor');

		return TPromise.as(null);
	}

459
	public createInput(input: IResourceInput): TPromise<IEditorInput> {
A
Alex Dima 已提交
460
		return TPromise.as(null);
E
Erich Gamma 已提交
461 462 463 464
	}
}

export class TestQuickOpenService implements QuickOpenService.IQuickOpenService {
465
	public _serviceBrand: any;
E
Erich Gamma 已提交
466 467 468

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

469
	constructor(callback?: (prefix: string) => void) {
E
Erich Gamma 已提交
470 471 472
		this.callback = callback;
	}

473
	pick(arg: any, options?: any, token?: any): Promise {
A
Alex Dima 已提交
474
		return TPromise.as(null);
E
Erich Gamma 已提交
475 476
	}

477
	input(options?: any, token?: any): Promise {
A
Alex Dima 已提交
478
		return TPromise.as(null);
E
Erich Gamma 已提交
479 480
	}

481
	close(): void {
J
Joao Moreno 已提交
482

483 484
	}

B
Benjamin Pasero 已提交
485
	show(prefix?: string, options?: any): Promise {
B
tslint  
Benjamin Pasero 已提交
486 487 488
		if (this.callback) {
			this.callback(prefix);
		}
E
Erich Gamma 已提交
489

A
Alex Dima 已提交
490
		return TPromise.as(true);
E
Erich Gamma 已提交
491 492
	}

493
	get onShow(): Event<void> {
E
Erich Gamma 已提交
494 495 496
		return null;
	}

497
	get onHide(): Event<void> {
E
Erich Gamma 已提交
498 499 500
		return null;
	}

B
Benjamin Pasero 已提交
501 502
	public dispose() { }
	public quickNavigate(): void { }
E
Erich Gamma 已提交
503 504 505
}

export const TestFileService = {
B
Benjamin Pasero 已提交
506
	resolveContent: function (resource) {
A
Alex Dima 已提交
507
		return TPromise.as({
E
Erich Gamma 已提交
508 509 510 511
			resource: resource,
			value: 'Hello Html',
			etag: 'index.txt',
			mime: 'text/plain',
B
Benjamin Pasero 已提交
512
			encoding: 'utf8',
E
Erich Gamma 已提交
513 514 515 516 517
			mtime: new Date().getTime(),
			name: Paths.basename(resource.fsPath)
		});
	},

A
Alex Dima 已提交
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
	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 已提交
539
	updateContent: function (res) {
540
		return TPromise.timeout(1).then(() => {
E
Erich Gamma 已提交
541 542 543 544
			return {
				resource: res,
				etag: 'index.txt',
				mime: 'text/plain',
B
Benjamin Pasero 已提交
545
				encoding: 'utf8',
E
Erich Gamma 已提交
546 547 548 549 550
				mtime: new Date().getTime(),
				name: Paths.basename(res.fsPath)
			};
		});
	}
B
linting  
Benjamin Pasero 已提交
551
};
552 553

export class TestConfigurationService extends EventEmitter.EventEmitter implements IConfigurationService {
554
	public _serviceBrand: any;
555

556 557
	private configuration = Object.create(null);

558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
	protected resolveContents(resources: URI[]): TPromise<IContent[]> {
		return TPromise.as(resources.map((resource) => {
			return {
				resource: resource,
				value: ''
			};
		}));
	}

	protected resolveContent(resource: URI): TPromise<IContent> {
		return TPromise.as({
			resource: resource,
			value: ''
		});
	}

	protected resolveStat(resource: URI): TPromise<IStat> {
		return TPromise.as({
			resource: resource,
			isDirectory: false
		});
	}

581 582 583 584
	public loadConfiguration<T>(section?: string): TPromise<T> {
		return TPromise.as(this.getConfiguration());
	}

585
	public getConfiguration(): any {
586
		return this.configuration;
587 588
	}

B
Benjamin Pasero 已提交
589
	public hasWorkspaceConfiguration(): boolean {
590 591
		return false;
	}
592

B
Benjamin Pasero 已提交
593
	public setUserConfiguration(key: any, value: any): Thenable<void> {
594
		this.configuration[key] = value;
B
Benjamin Pasero 已提交
595 596
		return TPromise.as(null);
	}
597 598 599 600

	public onDidUpdateConfiguration() {
		return { dispose() { } };
	}
601 602 603 604
}

export class TestLifecycleService implements ILifecycleService {

605
	public _serviceBrand: any;
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623

	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;
	}
624 625 626 627 628
}

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