stringEditorInput.test.ts 6.5 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 * as assert from 'assert';
import {Promise } from 'vs/base/common/winjs.base';
import * as Strings from 'vs/base/common/strings';
import URI from 'vs/base/common/uri';
12
import {StringEditorInput} from 'vs/workbench/common/editor/stringEditorInput';
E
Erich Gamma 已提交
13
import {LogEditorInput} from 'vs/workbench/browser/parts/editor/logEditorInput';
14 15
import {ResourceEditorInput} from 'vs/workbench/common/editor/resourceEditorInput';
import {ResourceEditorModel} from 'vs/workbench/common/editor/resourceEditorModel';
E
Erich Gamma 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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 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
import {TestWorkspace, TestEditorService, MockRequestService} from 'vs/workbench/test/browser/servicesTestUtils';
import * as InstantiationService from 'vs/platform/instantiation/common/instantiationService';
import {createMockModelService, createMockModeService} from 'vs/editor/test/common/servicesTestUtils';

suite("Workbench - StringEditorInput", () => {

	test("StringEditorInput", function(done) {
		let editorService = new TestEditorService(function() { });
		let inst = InstantiationService.create({
			modeService: createMockModeService(),
			modelService: createMockModelService()
		});

		let input = inst.createInstance(StringEditorInput, "name", 'description', "value", "mime", false);
		let otherInput = inst.createInstance(StringEditorInput, "name", 'description', "othervalue", "mime", false);
		let otherInputSame = inst.createInstance(StringEditorInput, "name", 'description', "value", "mime", false);

		let inputSingleton = inst.createInstance(StringEditorInput, "name", 'description', "value", "mime", true);
		let otherInputSingleton = inst.createInstance(StringEditorInput, "name", 'description', "othervalue", "mime", true);
		assert(inputSingleton.matches(otherInputSingleton));
		(<any>otherInputSingleton).singleton = false;
		assert(!inputSingleton.matches(otherInputSingleton));

		assert(input.matches(input));
		assert(input.matches(otherInputSame));
		assert(!input.matches(otherInput));
		assert(!input.matches(null));
		assert(input.getName());

		input = inst.createInstance(StringEditorInput, "name", 'description', "value", "mime", false);

		input = inst.createInstance(StringEditorInput, "name", 'description', "value", "mime", false);
		editorService.resolveEditorModel(input, true).then(function(resolved) {
			let resolvedModelA = resolved;
			return editorService.resolveEditorModel(input, true).then(function(resolved) {
				assert(resolvedModelA === resolved); // assert: Resolved Model cached per instance

				let otherInput = inst.createInstance(StringEditorInput, "name", 'description', "value", "mime", false);
				return editorService.resolveEditorModel(otherInput, true).then(function(resolved) {
					assert(resolvedModelA !== resolved); // NOT assert: Different instance, different model

					input.dispose();

					return editorService.resolveEditorModel(input, true).then(function(resolved) {
						assert(resolvedModelA !== resolved); // Different instance, because input got disposed

						let model = (<any>resolved).textEditorModel;
						return editorService.resolveEditorModel(input, true).then(function(againResolved) {
							assert(model === (<any>againResolved).textEditorModel); // Models should not differ because string input is constant

							input.dispose();
						});
					});
				});
			});
		}).done(() => done());
	});

	test("StringEditorInput - setValue, clearValue, append", function() {
		let inst = InstantiationService.create({
			modeService: createMockModeService(),
			modelService: createMockModelService()
		});

		let input = inst.createInstance(StringEditorInput, "name", 'description', "value", "mime", false);

		assert.strictEqual(input.getValue(), 'value');
		input.setValue("foo");
		assert.strictEqual(input.getValue(), 'foo');
		input.clearValue();
		assert(!input.getValue());
		input.append("1");
		assert.strictEqual(input.getValue(), '1');
		input.append("2");
		assert.strictEqual(input.getValue(), '12');
	});

	test("Input.matches() - StringEditorInput", function() {
		let inst = InstantiationService.create({});

		let promise = Promise.as("value");

		let stringEditorInput = inst.createInstance(StringEditorInput, "name", 'description', "value", "mime", false);
99
		let promiseEditorInput = inst.createInstance(ResourceEditorInput, "name", "description", URI.create('inMemory', null, 'thePath'));
E
Erich Gamma 已提交
100 101

		let stringEditorInput2 = inst.createInstance(StringEditorInput, "name", 'description', "value", "mime", false);
102
		let promiseEditorInput2 = inst.createInstance(ResourceEditorInput, "name", "description", URI.create('inMemory', null, 'thePath'));
E
Erich Gamma 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

		assert.strictEqual(stringEditorInput.matches(null), false);
		assert.strictEqual(promiseEditorInput.matches(null), false);

		assert.strictEqual(promiseEditorInput.matches(promiseEditorInput), true);
		assert.strictEqual(stringEditorInput.matches(stringEditorInput), true);

		assert.strictEqual(promiseEditorInput.matches(promiseEditorInput2), true);
		assert.strictEqual(stringEditorInput.matches(stringEditorInput2), true);
	});

	test("LogEditorInput", function() {
		let inst = InstantiationService.create({});

		let logEditorInput = inst.createInstance(LogEditorInput, "name", 'description', "value\nvalue\nvalue", "mime", false);
		let logEditorInput2 = inst.createInstance(LogEditorInput, "name", 'description', "value\nvalue\nvalue", "mime", false);
		let stringEditorInput = inst.createInstance(StringEditorInput, "name", 'description', "value", "mime", false);

		assert.strictEqual(logEditorInput.matches(stringEditorInput), false);
		assert.strictEqual(logEditorInput.matches(logEditorInput2), true);
	});

125
	test("ResourceEditorInput", function(done) {
E
Erich Gamma 已提交
126 127 128 129 130 131 132 133
		let modelService = createMockModelService();
		let modeService = createMockModeService();
		let inst = InstantiationService.create({
			modeService: modeService,
			modelService: modelService
		});

		let resource = URI.create('inMemory', null, 'thePath');
J
Johannes Rieken 已提交
134
		let model = modelService.createModel('function test() {}', modeService.getOrCreateMode('text'), resource);
135
		let input:ResourceEditorInput = inst.createInstance(ResourceEditorInput, 'The Name', 'The Description', resource);
E
Erich Gamma 已提交
136

137
		input.resolve().then((model:ResourceEditorModel) => {
E
Erich Gamma 已提交
138 139 140 141 142 143 144
			assert.ok(model);
			assert.equal(model.getValue(), 'function test() {}');

			done();
		});
	});
});