servicesTestUtils.ts 11.8 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8 9 10 11
/*---------------------------------------------------------------------------------------------
 *  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';
import EventEmitter = require('vs/base/common/eventEmitter');
import Paths = require('vs/base/common/paths');
import URI from 'vs/base/common/uri';
12
import {NullTelemetryService} from 'vs/platform/telemetry/common/telemetry';
13
import Storage = require('vs/workbench/common/storage');
E
Erich Gamma 已提交
14
import WorkbenchEditorCommon = require('vs/workbench/common/editor');
B
linting  
Benjamin Pasero 已提交
15
import Event from 'vs/base/common/event';
E
Erich Gamma 已提交
16 17 18
import Types = require('vs/base/common/types');
import Severity from 'vs/base/common/severity';
import http = require('vs/base/common/http');
19
import {IConfigurationService} from 'vs/platform/configuration/common/configuration';
E
Erich Gamma 已提交
20 21
import {IStorageService, StorageScope} from 'vs/platform/storage/common/storage';
import WorkbenchEditorService = require('vs/workbench/services/editor/common/editorService');
22
import QuickOpenService = require('vs/workbench/services/quickopen/common/quickOpenService');
E
Erich Gamma 已提交
23 24
import PartService = require('vs/workbench/services/part/common/partService');
import WorkspaceContextService = require('vs/workbench/services/workspace/common/contextService');
B
linting  
Benjamin Pasero 已提交
25
import {IEditorInput, IEditorModel, Position, IEditor, IResourceInput, ITextEditorModel} from 'vs/platform/editor/common/editor';
E
Erich Gamma 已提交
26
import {IEventService} from 'vs/platform/event/common/event';
27
import {IUntitledEditorService} from 'vs/workbench/services/untitled/common/untitledEditorService';
E
Erich Gamma 已提交
28 29
import {IMessageService, IConfirmation} from 'vs/platform/message/common/message';
import {BaseRequestService} from 'vs/platform/request/common/baseRequestService';
B
linting  
Benjamin Pasero 已提交
30
import {IWorkspace, IConfiguration} from 'vs/platform/workspace/common/workspace';
E
Erich Gamma 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

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

export class TestContextService implements WorkspaceContextService.IWorkspaceContextService {
	public serviceId = WorkspaceContextService.IWorkspaceContextService;

	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;
54 55 56 57 58
		this.options = options || {
			globalSettings: {
				settings: {}
			}
		};
E
Erich Gamma 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
	}

	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 {
		return Paths.makeAbsolute(Paths.normalize(resource.fsPath.substr('c:'.length)));
	}

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

export class TestMessageService implements IMessageService {
	public serviceId = IMessageService;

	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 {
	public serviceId = PartService.IPartService;

	public layout(): void { }

	public isCreated(): boolean {
		return true;
	}

	public joinCreation(): Promise {
A
Alex Dima 已提交
132
		return TPromise.as(null);
E
Erich Gamma 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
	}

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

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

	public isSideBarHidden(): boolean {
		return false;
	}

	public setSideBarHidden(hidden: boolean): void { }

I
isidor 已提交
149
	public isPanelHidden(): boolean {
I
isidor 已提交
150 151 152
		return false;
	}

I
isidor 已提交
153
	public setPanelHidden(hidden: boolean): void { }
I
isidor 已提交
154

E
Erich Gamma 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
	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 {
	public serviceId = IEventService;
}

export class TestStorageService extends EventEmitter.EventEmitter implements IStorageService {
	public serviceId = IStorageService;

	private storage: Storage.Storage;

	constructor() {
		super();

		let context = new TestContextService();
		this.storage = new Storage.Storage(context, new Storage.InMemoryLocalStorage());
	}

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

export class TestRequestService extends BaseRequestService {

	constructor(workspace = TestWorkspace) {
208
		super(new TestContextService(), NullTelemetryService);
E
Erich Gamma 已提交
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
	}
}

export interface ICustomResponse {
	responseText: string;
	getResponseHeader: (key: string) => string;
}

export interface IMockRequestHandler {
	(url: string): string | ICustomResponse;
}

export class MockRequestService extends BaseRequestService {

	constructor(workspace: any, private handler: IMockRequestHandler) {
224
		super(new TestContextService(), NullTelemetryService);
E
Erich Gamma 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
	}

	public makeRequest(options: http.IXHROptions): TPromise<http.IXHRResponse> {
		let data = this.handler(options.url);

		if (!data) {
			return super.makeRequest(options);
		}

		let isString = Types.isString(data);
		let responseText = isString ? <string>data : (<ICustomResponse>data).responseText;
		let getResponseHeader = isString ? () => '' : (<ICustomResponse>data).getResponseHeader;

		return TPromise.as<http.IXHRResponse>({
			responseText: responseText,
			status: 200,
			readyState: 4,
			getResponseHeader: getResponseHeader
		});
	}
}

247 248
export class TestUntitledEditorService implements IUntitledEditorService {
	public serviceId = IUntitledEditorService;
E
Erich Gamma 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288

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

	public getAll() {
		return [];
	}

	public getDirty() {
		return [];
	}

	public isDirty() {
		return false;
	}

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

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

export class TestEditorService implements WorkbenchEditorService.IWorkbenchEditorService {
	public serviceId = WorkbenchEditorService.IWorkbenchEditorService;

	public activeEditorInput;
	public activeEditorOptions;
	public activeEditorPosition;

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

	constructor(callback?: (method: string) => void) {
		this.callback = callback || ((s: string) => { });
	}

	public setEditors(inputs): Promise {
A
Alex Dima 已提交
289
		return TPromise.as([]);
E
Erich Gamma 已提交
290 291 292
	}

	public closeEditors(othersOnly?: boolean): Promise {
A
Alex Dima 已提交
293
		return TPromise.as(null);
E
Erich Gamma 已提交
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
	}

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

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

		return null;
	}

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

		return null;
	}

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

		return [];
	}

318 319 320 321 322 323
	public activateEditor(position: Position): void;
	public activateEditor(editor: IEditor): void;
	public activateEditor(arg: any): void {
		this.callback('activateEditor');
	}

E
Erich Gamma 已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
	public moveEditor(from: Position, to: Position): void {
		this.callback('moveEditor');
	}

	public arrangeEditors(arrangement: WorkbenchEditorService.EditorArrangement): void {
		this.callback('arrangeEditors');
	}

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

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

A
Alex Dima 已提交
339
		return TPromise.as(null);
E
Erich Gamma 已提交
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
	}

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


	public closeEditor(editor?: IEditor): TPromise<IEditor>;
	public closeEditor(position?: Position): TPromise<IEditor>;
	public closeEditor(arg?: any): TPromise<IEditor> {
		this.callback('closeEditor');

		return TPromise.as(null);
	}

	public focusEditor(editor?: IEditor): TPromise<IEditor>;
	public focusEditor(position?: Position): TPromise<IEditor>;
	public focusEditor(arg?: any): TPromise<IEditor> {
		this.callback('focusEditor');

		return TPromise.as(null);
	}

367
	public inputToType(input: IResourceInput): TPromise<IEditorInput> {
A
Alex Dima 已提交
368
		return TPromise.as(null);
E
Erich Gamma 已提交
369 370 371 372 373 374 375 376
	}
}

export class TestQuickOpenService implements QuickOpenService.IQuickOpenService {
	public serviceId = QuickOpenService.IQuickOpenService;

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

377
	constructor(callback?: (prefix: string) => void) {
E
Erich Gamma 已提交
378 379 380 381
		this.callback = callback;
	}

	pick(arg: any, placeHolder?: string, autoFocusFirst?: boolean): Promise {
A
Alex Dima 已提交
382
		return TPromise.as(null);
E
Erich Gamma 已提交
383 384 385
	}

	input(options?: any): Promise {
A
Alex Dima 已提交
386
		return TPromise.as(null);
E
Erich Gamma 已提交
387 388 389
	}

	refresh(): Promise {
A
Alex Dima 已提交
390
		return TPromise.as(true);
E
Erich Gamma 已提交
391 392 393
	}

	show(prefix?: string, quickNavigateConfiguration?: any): Promise {
B
tslint  
Benjamin Pasero 已提交
394 395 396
		if (this.callback) {
			this.callback(prefix);
		}
E
Erich Gamma 已提交
397

A
Alex Dima 已提交
398
		return TPromise.as(true);
E
Erich Gamma 已提交
399 400 401 402 403 404
	}

	getEditorHistory(): WorkbenchEditorCommon.EditorInput[] {
		return [];
	}

405
	get onShow(): Event<void> {
E
Erich Gamma 已提交
406 407 408
		return null;
	}

409
	get onHide(): Event<void> {
E
Erich Gamma 已提交
410 411 412
		return null;
	}

B
Benjamin Pasero 已提交
413 414 415
	public removeEditorHistoryEntry(input: WorkbenchEditorCommon.EditorInput): void { }
	public dispose() { }
	public quickNavigate(): void { }
E
Erich Gamma 已提交
416 417 418
}

export const TestFileService = {
B
Benjamin Pasero 已提交
419
	resolveContent: function (resource) {
A
Alex Dima 已提交
420
		return TPromise.as({
E
Erich Gamma 已提交
421 422 423 424
			resource: resource,
			value: 'Hello Html',
			etag: 'index.txt',
			mime: 'text/plain',
B
Benjamin Pasero 已提交
425
			encoding: 'utf8',
E
Erich Gamma 已提交
426 427 428 429 430
			mtime: new Date().getTime(),
			name: Paths.basename(resource.fsPath)
		});
	},

B
Benjamin Pasero 已提交
431
	updateContent: function (res) {
432
		return TPromise.timeout(1).then(() => {
E
Erich Gamma 已提交
433 434 435 436
			return {
				resource: res,
				etag: 'index.txt',
				mime: 'text/plain',
B
Benjamin Pasero 已提交
437
				encoding: 'utf8',
E
Erich Gamma 已提交
438 439 440 441 442
				mtime: new Date().getTime(),
				name: Paths.basename(res.fsPath)
			};
		});
	}
B
linting  
Benjamin Pasero 已提交
443
};
444 445 446 447

export class TestConfigurationService extends EventEmitter.EventEmitter implements IConfigurationService {
	public serviceId = IConfigurationService;

448 449 450 451
	public loadConfiguration<T>(section?: string): TPromise<T> {
		return TPromise.as(this.getConfiguration());
	}

452 453
	public getConfiguration(): any {
		return {};
454 455
	}

B
Benjamin Pasero 已提交
456
	public hasWorkspaceConfiguration(): boolean {
457 458
		return false;
	}
459 460 461 462

	public onDidUpdateConfiguration() {
		return { dispose() { } };
	}
E
Erich Gamma 已提交
463
}