servicesTestUtils.ts 13.8 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*---------------------------------------------------------------------------------------------
 *  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 'vs/workbench/browser/parts/editor/editor.contribution'; // make sure to load all contributed editor things into tests
import {Promise, TPromise} from 'vs/base/common/winjs.base';
import Objects = require('vs/base/common/objects');
import EventEmitter = require('vs/base/common/eventEmitter');
import Strings = require('vs/base/common/strings');
import Paths = require('vs/base/common/paths');
import Env = require('vs/base/common/flags');
import URI from 'vs/base/common/uri';
import MainTelemetryService = require('vs/platform/telemetry/browser/mainTelemetryService');
17
import Storage = require('vs/workbench/common/storage');
E
Erich Gamma 已提交
18 19 20
import WorkbenchEditorCommon = require('vs/workbench/common/editor');
import Viewlet = require('vs/workbench/browser/viewlet');
import InstantiationService = require('vs/platform/instantiation/common/instantiationService');
21
import Event, {Emitter} from 'vs/base/common/event';
E
Erich Gamma 已提交
22 23 24 25 26 27 28 29
import LifecycleService = require('vs/platform/lifecycle/common/baseLifecycleService');
import Types = require('vs/base/common/types');
import Mime = require('vs/base/common/mime');
import Assert = require('vs/base/common/assert');
import Severity from 'vs/base/common/severity';
import Arrays = require('vs/base/common/arrays');
import Errors = require('vs/base/common/errors');
import http = require('vs/base/common/http');
30
import {IConfigurationService} from 'vs/platform/configuration/common/configuration';
E
Erich Gamma 已提交
31
import {IStorageService, StorageScope} from 'vs/platform/storage/common/storage';
32
import UntitledEditorService = require('vs/workbench/services/untitled/common/untitledEditorService');
E
Erich Gamma 已提交
33
import WorkbenchEditorService = require('vs/workbench/services/editor/common/editorService');
34
import QuickOpenService = require('vs/workbench/services/quickopen/common/quickOpenService');
E
Erich Gamma 已提交
35 36 37 38 39 40
import ViewletService = require('vs/workbench/services/viewlet/common/viewletService');
import PartService = require('vs/workbench/services/part/common/partService');
import WorkspaceContextService = require('vs/workbench/services/workspace/common/contextService');
import ViewletCommon = require('vs/workbench/common/viewlet');
import Files = require('vs/platform/files/common/files');
import {BaseWorkspaceContextService} from 'vs/platform/workspace/common/baseWorkspaceContextService';
41
import {IEditorInput, IEditorModel, IEditorOptions, Position, IEditor, IResourceInput, ITextEditorModel} from 'vs/platform/editor/common/editor';
E
Erich Gamma 已提交
42 43
import {IEventService} from 'vs/platform/event/common/event';
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
44
import {IUntitledEditorService} from 'vs/workbench/services/untitled/common/untitledEditorService';
E
Erich Gamma 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
import {IMessageService, IConfirmation} from 'vs/platform/message/common/message';
import Lifecycle = require('vs/base/common/lifecycle');
import {IRequestService} from 'vs/platform/request/common/request';
import {BaseRequestService} from 'vs/platform/request/common/baseRequestService';
import {ITelemetryService, ITelemetryInfo} from 'vs/platform/telemetry/common/telemetry';
import {IWorkspaceContextService, IWorkspace, IConfiguration} from 'vs/platform/workspace/common/workspace';

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;
74 75 76 77 78
		this.options = options || {
			globalSettings: {
				settings: {}
			}
		};
E
Erich Gamma 已提交
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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 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 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 208 209 210 211 212 213 214 215 216 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 244 245 246 247 248 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 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
	}

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

	public setStatusMessage(message: string, autoDisposeAfter: number = -1): Lifecycle.IDisposable {
		return {
			dispose: () => { /* Nothing to do here */ }
		};
	}
}

export class TestTelemetryService implements ITelemetryService {
	public serviceId = ITelemetryService;

	getSessionId(): string {
		return null;
	}

	getMachineId(): string {
		return null;
	}

	getInstanceId(): string {
		return null;
	}

	getTelemetryInfo(): TPromise<ITelemetryInfo> {
		return TPromise.as(null);
	}

	log(eventName: string, data?: any): void { }
	publicLog(eventName: string, data?: any): void { }

	start(name: string, data?: any, isPublic?: boolean): any {
		return null;
	}

	getAppendersCount(): number {
		return -1;
	}

	getAppenders(): any[] {
		return [];
	}

	addTelemetryAppender(appender): void { }
	removeTelemetryAppender(appender): void { }
	dispose(): void { }
	setInstantiationService(instantiationService: IInstantiationService) { }
}

export class TestPartService implements PartService.IPartService {
	public serviceId = PartService.IPartService;

	public layout(): void { }

	public isCreated(): boolean {
		return true;
	}

	public joinCreation(): Promise {
		return Promise.as(null);
	}

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

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

	public isSideBarHidden(): boolean {
		return false;
	}

	public setSideBarHidden(hidden: boolean): void { }

	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 TestLifecycleService extends LifecycleService.BaseLifecycleService { }

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) {
		super(new TestContextService(), new MainTelemetryService.MainTelemetryService());
	}
}

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) {
		super(new TestContextService(), new MainTelemetryService.MainTelemetryService());
	}

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

309 310
export class TestUntitledEditorService implements IUntitledEditorService {
	public serviceId = IUntitledEditorService;
E
Erich Gamma 已提交
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 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 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422

	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 {
		return Promise.as([]);
	}

	public closeEditors(othersOnly?: boolean): Promise {
		return Promise.as(null);
	}

	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 [];
	}

	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;

		return Promise.as(null);
	}

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

423
	public inputToType(input: IResourceInput): TPromise<IEditorInput> {
E
Erich Gamma 已提交
424 425 426 427 428 429 430 431 432
		return Promise.as(null);
	}
}

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

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

433
	constructor(callback?: (prefix: string) => void) {
E
Erich Gamma 已提交
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
		this.callback = callback;
	}

	pick(arg: any, placeHolder?: string, autoFocusFirst?: boolean): Promise {
		return Promise.as(null);
	}

	input(options?: any): Promise {
		return Promise.as(null);
	}

	refresh(): Promise {
		return Promise.as(true);
	}

	show(prefix?: string, quickNavigateConfiguration?: any): Promise {
450
		this.callback && this.callback(prefix);
E
Erich Gamma 已提交
451 452 453 454 455 456 457 458

		return Promise.as(true);
	}

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

459
	get onShow(): Event<void> {
E
Erich Gamma 已提交
460 461 462
		return null;
	}

463
	get onHide(): Event<void> {
E
Erich Gamma 已提交
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
		return null;
	}

	public removeEditorHistoryEntry(input: WorkbenchEditorCommon.EditorInput): void {}
	public dispose() {}
	public quickNavigate(): void {}
}

export const TestFileService = {
	resolveContent: function(resource) {
		return Promise.as({
			resource: resource,
			value: 'Hello Html',
			etag: 'index.txt',
			mime: 'text/plain',
			charset: 'utf8',
			mtime: new Date().getTime(),
			name: Paths.basename(resource.fsPath)
		});
	},

	updateContent: function(res) {
		return Promise.timeout(1).then(() => {
			return {
				resource: res,
				etag: 'index.txt',
				mime: 'text/plain',
				charset: 'utf8',
				mtime: new Date().getTime(),
				name: Paths.basename(res.fsPath)
			};
		});
	}
497 498 499 500 501 502 503 504 505 506 507 508
}

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

	public loadConfiguration(section?:string):TPromise<any> {
		return TPromise.as({});
	}

	public hasWorkspaceConfiguration():boolean {
		return false;
	}
E
Erich Gamma 已提交
509
}