textFileEditorModel.test.ts 10.7 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 * as assert from 'assert';
9
import {TPromise} from 'vs/base/common/winjs.base';
10
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
E
Erich Gamma 已提交
11
import URI from 'vs/base/common/uri';
12
import paths = require('vs/base/common/paths');
B
Benjamin Pasero 已提交
13
import {EncodingMode} from 'vs/workbench/common/editor';
14
import {TextFileEditorModel} from 'vs/workbench/services/textfile/common/textFileEditorModel';
15
import {IEventService} from 'vs/platform/event/common/event';
16
import {ITextFileService, ModelState, StateChange} from 'vs/workbench/services/textfile/common/textfiles';
B
Benjamin Pasero 已提交
17
import {workbenchInstantiationService, TestTextFileService, createFileInput} from 'vs/test/utils/servicesTestUtils';
18
import {TextFileEditorModelManager} from 'vs/workbench/services/textfile/common/textFileEditorModelManager';
19
import {FileOperationResult, IFileOperationResult} from 'vs/platform/files/common/files';
B
Benjamin Pasero 已提交
20
import {IModelService} from 'vs/editor/common/services/modelService';
E
Erich Gamma 已提交
21 22 23 24 25

function toResource(path) {
	return URI.file(paths.join('C:\\', path));
}

26
class ServiceAccessor {
B
Benjamin Pasero 已提交
27
	constructor( @IEventService public eventService: IEventService, @ITextFileService public textFileService: TestTextFileService, @IModelService public modelService: IModelService) {
28 29 30
	}
}

E
Erich Gamma 已提交
31 32
suite('Files - TextFileEditorModel', () => {

33
	let instantiationService: IInstantiationService;
B
Benjamin Pasero 已提交
34
	let accessor: ServiceAccessor;
35

E
Erich Gamma 已提交
36
	setup(() => {
B
Benjamin Pasero 已提交
37
		instantiationService = workbenchInstantiationService();
38
		accessor = instantiationService.createInstance(ServiceAccessor);
E
Erich Gamma 已提交
39 40 41
	});

	teardown(() => {
42
		(<TextFileEditorModelManager>accessor.textFileService.models).clear();
B
Benjamin Pasero 已提交
43
		TextFileEditorModel.setSaveParticipant(null); // reset any set participant
E
Erich Gamma 已提交
44 45
	});

B
Benjamin Pasero 已提交
46 47 48 49 50 51 52 53 54 55 56 57
	test('Save', function (done) {
		const model: TextFileEditorModel = instantiationService.createInstance(TextFileEditorModel, toResource('/path/index_async.txt'), 'utf8');

		model.load().then(() => {
			model.textEditorModel.setValue('bar');
			assert.ok(model.getLastModifiedTime() <= Date.now());

			return model.save().then(() => {
				assert.ok(model.getLastSaveAttemptTime() <= Date.now());
				assert.ok(!model.isDirty());

				model.dispose();
B
Benjamin Pasero 已提交
58
				assert.ok(!accessor.modelService.getModel(model.getResource()));
B
Benjamin Pasero 已提交
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

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

	test('setEncoding - encode', function () {
		const model: TextFileEditorModel = instantiationService.createInstance(TextFileEditorModel, toResource('/path/index_async.txt'), 'utf8');

		model.setEncoding('utf8', EncodingMode.Encode); // no-op
		assert.equal(model.getLastModifiedTime(), -1);

		model.setEncoding('utf16', EncodingMode.Encode);

		assert.ok(model.getLastModifiedTime() <= Date.now()); // indicates model was saved due to encoding change

		model.dispose();
	});

	test('setEncoding - decode', function () {
		const model: TextFileEditorModel = instantiationService.createInstance(TextFileEditorModel, toResource('/path/index_async.txt'), 'utf8');

		model.setEncoding('utf16', EncodingMode.Decode);

		assert.ok(model.isResolved()); // model got loaded due to decoding

		model.dispose();
	});

88 89 90 91 92 93 94 95 96 97 98 99
	test('disposes when underlying model is destroyed', function (done) {
		const model: TextFileEditorModel = instantiationService.createInstance(TextFileEditorModel, toResource('/path/index_async.txt'), 'utf8');

		model.load().then(() => {
			model.textEditorModel.destroy();

			assert.ok(model.isDisposed());

			done();
		});
	});

B
Benjamin Pasero 已提交
100
	test('Load does not trigger save', function (done) {
101
		const model = instantiationService.createInstance(TextFileEditorModel, toResource('/path/index.txt'), 'utf8');
B
Benjamin Pasero 已提交
102
		assert.equal(model.getState(), ModelState.SAVED);
E
Erich Gamma 已提交
103

104
		accessor.eventService.addListener2('files:internalFileChanged', () => {
E
Erich Gamma 已提交
105 106 107
			assert.ok(false);
		});

108 109
		model.onDidStateChange(e => {
			assert.ok(e !== StateChange.DIRTY && e !== StateChange.SAVED);
E
Erich Gamma 已提交
110 111
		});

112 113
		model.load().then(() => {
			assert.ok(model.isResolved());
E
Erich Gamma 已提交
114

115
			model.dispose();
E
Erich Gamma 已提交
116

B
Benjamin Pasero 已提交
117 118
			assert.ok(!accessor.modelService.getModel(model.getResource()));

E
Erich Gamma 已提交
119 120 121 122
			done();
		});
	});

B
Benjamin Pasero 已提交
123
	test('Load returns dirty model as long as model is dirty', function (done) {
124
		const model = instantiationService.createInstance(TextFileEditorModel, toResource('/path/index_async.txt'), 'utf8');
E
Erich Gamma 已提交
125

126 127
		model.load().then(() => {
			model.textEditorModel.setValue('foo');
E
Erich Gamma 已提交
128

129
			assert.ok(model.isDirty());
B
Benjamin Pasero 已提交
130
			assert.equal(model.getState(), ModelState.DIRTY);
131 132
			model.load().then(() => {
				assert.ok(model.isDirty());
E
Erich Gamma 已提交
133

134
				model.dispose();
E
Erich Gamma 已提交
135 136 137 138 139 140

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

B
Benjamin Pasero 已提交
141
	test('Revert', function (done) {
E
Erich Gamma 已提交
142 143 144
		let eventCounter = 0;


145
		const model = instantiationService.createInstance(TextFileEditorModel, toResource('/path/index_async.txt'), 'utf8');
E
Erich Gamma 已提交
146

147 148 149 150 151 152
		model.onDidStateChange(e => {
			if (e === StateChange.REVERTED) {
				eventCounter++;
			}
		});

153 154
		model.load().then(() => {
			model.textEditorModel.setValue('foo');
E
Erich Gamma 已提交
155

156
			assert.ok(model.isDirty());
E
Erich Gamma 已提交
157

158 159 160
			model.revert().then(() => {
				assert.ok(!model.isDirty());
				assert.equal(model.textEditorModel.getValue(), 'Hello Html');
E
Erich Gamma 已提交
161 162
				assert.equal(eventCounter, 1);

163
				model.dispose();
E
Erich Gamma 已提交
164 165 166 167 168 169

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

170
	test('File not modified error is handled gracefully', function (done) {
B
Benjamin Pasero 已提交
171
		const model: TextFileEditorModel = instantiationService.createInstance(TextFileEditorModel, toResource('/path/index_async.txt'), 'utf8');
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

		model.load().then(() => {
			const mtime = model.getLastModifiedTime();
			accessor.textFileService.setResolveTextContentErrorOnce(<IFileOperationResult>{
				message: 'error',
				fileOperationResult: FileOperationResult.FILE_NOT_MODIFIED_SINCE
			});

			model.load().then((model: TextFileEditorModel) => {
				assert.ok(model);
				assert.equal(model.getLastModifiedTime(), mtime);
				model.dispose();

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

B
Benjamin Pasero 已提交
190
	test('Conflict Resolution Mode', function (done) {
B
Benjamin Pasero 已提交
191
		const model: TextFileEditorModel = instantiationService.createInstance(TextFileEditorModel, toResource('/path/index_async.txt'), 'utf8');
E
Erich Gamma 已提交
192

193 194 195
		model.load().then(() => {
			model.setConflictResolutionMode();
			model.textEditorModel.setValue('foo');
E
Erich Gamma 已提交
196

197
			assert.ok(model.isDirty());
B
Benjamin Pasero 已提交
198
			assert.equal(model.getState(), ModelState.CONFLICT);
199
			assert.ok(model.isInConflictResolutionMode());
E
Erich Gamma 已提交
200

201 202 203
			model.revert().then(() => {
				model.textEditorModel.setValue('bar');
				assert.ok(model.isDirty());
E
Erich Gamma 已提交
204

205 206
				return model.save().then(() => {
					assert.ok(!model.isDirty());
E
Erich Gamma 已提交
207

208
					model.dispose();
E
Erich Gamma 已提交
209 210 211 212 213 214 215

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

B
Benjamin Pasero 已提交
216
	test('Auto Save triggered when model changes', function (done) {
E
Erich Gamma 已提交
217
		let eventCounter = 0;
B
Benjamin Pasero 已提交
218
		const model: TextFileEditorModel = instantiationService.createInstance(TextFileEditorModel, toResource('/path/index.txt'), 'utf8');
E
Erich Gamma 已提交
219

220 221
		(<any>model).autoSaveAfterMillies = 10;
		(<any>model).autoSaveAfterMilliesEnabled = true;
E
Erich Gamma 已提交
222

223 224 225 226
		model.onDidStateChange(e => {
			if (e === StateChange.DIRTY || e === StateChange.SAVED) {
				eventCounter++;
			}
E
Erich Gamma 已提交
227 228
		});

229 230
		model.load().then(() => {
			model.textEditorModel.setValue('foo');
E
Erich Gamma 已提交
231

232
			return TPromise.timeout(50).then(() => {
233
				assert.ok(!model.isDirty());
E
Erich Gamma 已提交
234 235
				assert.equal(eventCounter, 2);

236
				model.dispose();
E
Erich Gamma 已提交
237

B
Benjamin Pasero 已提交
238 239
				assert.ok(!accessor.modelService.getModel(model.getResource()));

E
Erich Gamma 已提交
240 241 242 243 244
				done();
			});
		});
	});

B
Benjamin Pasero 已提交
245
	test('save() and isDirty() - proper with check for mtimes', function (done) {
B
Benjamin Pasero 已提交
246 247
		const input1 = createFileInput(instantiationService, toResource('/path/index_async2.txt'));
		const input2 = createFileInput(instantiationService, toResource('/path/index_async.txt'));
E
Erich Gamma 已提交
248

249 250 251
		input1.resolve().then((model1: TextFileEditorModel) => {
			input2.resolve().then((model2: TextFileEditorModel) => {
				model1.textEditorModel.setValue('foo');
E
Erich Gamma 已提交
252

253 254
				const m1Mtime = model1.getLastModifiedTime();
				const m2Mtime = model2.getLastModifiedTime();
E
Erich Gamma 已提交
255 256 257
				assert.ok(m1Mtime > 0);
				assert.ok(m2Mtime > 0);

258 259 260
				assert.ok(accessor.textFileService.isDirty());
				assert.ok(accessor.textFileService.isDirty(toResource('/path/index_async2.txt')));
				assert.ok(!accessor.textFileService.isDirty(toResource('/path/index_async.txt')));
E
Erich Gamma 已提交
261

262
				model2.textEditorModel.setValue('foo');
263
				assert.ok(accessor.textFileService.isDirty(toResource('/path/index_async.txt')));
E
Erich Gamma 已提交
264

265
				return TPromise.timeout(10).then(() => {
266 267 268
					accessor.textFileService.saveAll().then(() => {
						assert.ok(!accessor.textFileService.isDirty(toResource('/path/index_async.txt')));
						assert.ok(!accessor.textFileService.isDirty(toResource('/path/index_async2.txt')));
269 270 271 272
						assert.ok(model1.getLastModifiedTime() > m1Mtime);
						assert.ok(model2.getLastModifiedTime() > m2Mtime);
						assert.ok(model1.getLastSaveAttemptTime() > m1Mtime);
						assert.ok(model2.getLastSaveAttemptTime() > m2Mtime);
E
Erich Gamma 已提交
273

274 275
						model1.dispose();
						model2.dispose();
E
Erich Gamma 已提交
276 277 278 279 280 281 282 283

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

B
Benjamin Pasero 已提交
284
	test('Save Participant', function (done) {
E
Erich Gamma 已提交
285
		let eventCounter = 0;
B
Benjamin Pasero 已提交
286
		const model: TextFileEditorModel = instantiationService.createInstance(TextFileEditorModel, toResource('/path/index_async.txt'), 'utf8');
E
Erich Gamma 已提交
287

288 289 290 291 292 293
		model.onDidStateChange(e => {
			if (e === StateChange.SAVED) {
				assert.equal(model.getValue(), 'bar');
				assert.ok(!model.isDirty());
				eventCounter++;
			}
E
Erich Gamma 已提交
294 295
		});

B
Benjamin Pasero 已提交
296 297 298 299 300 301
		TextFileEditorModel.setSaveParticipant({
			participate: (model) => {
				assert.ok(model.isDirty());
				model.textEditorModel.setValue('bar');
				assert.ok(model.isDirty());
				eventCounter++;
J
Johannes Rieken 已提交
302
				return undefined;
B
Benjamin Pasero 已提交
303
			}
E
Erich Gamma 已提交
304 305
		});

306 307
		model.load().then(() => {
			model.textEditorModel.setValue('foo');
E
Erich Gamma 已提交
308

309 310
			model.save().then(() => {
				model.dispose();
E
Erich Gamma 已提交
311 312 313 314 315 316 317

				assert.equal(eventCounter, 2);

				done();
			});
		});
	});
J
Johannes Rieken 已提交
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

	test('Save Participant, async participant', function () {

		const model: TextFileEditorModel = instantiationService.createInstance(TextFileEditorModel, toResource('/path/index_async.txt'), 'utf8');

		TextFileEditorModel.setSaveParticipant({
			participate: (model) => {
				return TPromise.timeout(10);
			}
		});

		return model.load().then(() => {
			model.textEditorModel.setValue('foo');
			const now = Date.now();
			return model.save().then(() => {
				assert.ok(Date.now() - now >= 10);
				model.dispose();
			});
		});
	});

	test('Save Participant, bad participant', function () {

		const model: TextFileEditorModel = instantiationService.createInstance(TextFileEditorModel, toResource('/path/index_async.txt'), 'utf8');

		TextFileEditorModel.setSaveParticipant({
			participate: (model) => {
				return TPromise.wrapError('boom');
			}
		});

		return model.load().then(() => {
			model.textEditorModel.setValue('foo');
			return model.save().then(() => {
352
				assert.ok(true);
J
Johannes Rieken 已提交
353 354
				model.dispose();
			}, err => {
355
				assert.ok(false);
J
Johannes Rieken 已提交
356 357 358
			});
		});
	});
B
Benjamin Pasero 已提交
359
});