textFileService.test.ts 8.9 KB
Newer Older
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

J
Johannes Rieken 已提交
7
import { TPromise } from 'vs/base/common/winjs.base';
8
import * as assert from 'assert';
J
Johannes Rieken 已提交
9
import { ILifecycleService, ShutdownEvent } from 'vs/platform/lifecycle/common/lifecycle';
B
Benjamin Pasero 已提交
10
import { workbenchInstantiationService, TestLifecycleService, TestTextFileService, onError, toResource } from 'vs/test/utils/servicesTestUtils';
J
Johannes Rieken 已提交
11 12 13 14 15 16 17
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { TextFileEditorModel } from 'vs/workbench/services/textfile/common/textFileEditorModel';
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
import { ConfirmResult } from 'vs/workbench/common/editor';
import { IUntitledEditorService } from 'vs/workbench/services/untitled/common/untitledEditorService';
import { UntitledEditorModel } from 'vs/workbench/common/editor/untitledEditorModel';
import { TextFileEditorModelManager } from 'vs/workbench/services/textfile/common/textFileEditorModelManager';
18 19 20 21

class ServiceAccessor {
	constructor(
		@ILifecycleService public lifecycleService: TestLifecycleService,
B
Benjamin Pasero 已提交
22 23
		@ITextFileService public textFileService: TestTextFileService,
		@IUntitledEditorService public untitledEditorService: IUntitledEditorService
24 25 26 27 28 29 30
	) {
	}
}

class ShutdownEventImpl implements ShutdownEvent {

	public value: boolean | TPromise<boolean>;
D
Daniel Imms 已提交
31
	public quitRequested: boolean = false;
32 33 34 35 36 37

	veto(value: boolean | TPromise<boolean>): void {
		this.value = value;
	}
}

B
renames  
Benjamin Pasero 已提交
38
suite('Files - TextFileService', () => {
39

40
	let instantiationService: IInstantiationService;
41
	let model: TextFileEditorModel;
B
Benjamin Pasero 已提交
42
	let accessor: ServiceAccessor;
43 44

	setup(() => {
B
Benjamin Pasero 已提交
45
		instantiationService = workbenchInstantiationService();
46 47 48 49 50
		accessor = instantiationService.createInstance(ServiceAccessor);
	});

	teardown(() => {
		model.dispose();
51
		(<TextFileEditorModelManager>accessor.textFileService.models).clear();
52
		(<TextFileEditorModelManager>accessor.textFileService.models).dispose();
B
Benjamin Pasero 已提交
53
		accessor.untitledEditorService.revertAll();
54 55
	});

D
Daniel Imms 已提交
56
	test('confirm onWillShutdown - no veto', function () {
B
Benjamin Pasero 已提交
57 58 59
		model = instantiationService.createInstance(TextFileEditorModel, toResource.call(this, '/path/file.txt'), 'utf8');
		(<TextFileEditorModelManager>accessor.textFileService.models).add(model.getResource(), model);

60 61 62
		const event = new ShutdownEventImpl();
		accessor.lifecycleService.fireWillShutdown(event);

B
Benjamin Pasero 已提交
63 64 65 66 67 68 69 70
		const veto = event.value;
		if (typeof veto === 'boolean') {
			assert.ok(!veto);
		} else {
			veto.then(veto => {
				assert.ok(!veto);
			});
		}
71 72 73
	});

	test('confirm onWillShutdown - veto if user cancels', function (done) {
B
Benjamin Pasero 已提交
74 75 76
		model = instantiationService.createInstance(TextFileEditorModel, toResource.call(this, '/path/file.txt'), 'utf8');
		(<TextFileEditorModelManager>accessor.textFileService.models).add(model.getResource(), model);

B
Benjamin Pasero 已提交
77 78
		const service = accessor.textFileService;
		service.setConfirmResult(ConfirmResult.CANCEL);
79

B
Benjamin Pasero 已提交
80
		model.load().done(() => {
81 82
			model.textEditorModel.setValue('foo');

B
Benjamin Pasero 已提交
83
			assert.equal(service.getDirty().length, 1);
84 85 86 87 88 89 90

			const event = new ShutdownEventImpl();
			accessor.lifecycleService.fireWillShutdown(event);

			assert.ok(event.value);

			done();
B
Benjamin Pasero 已提交
91
		}, error => onError(error, done));
92 93 94
	});

	test('confirm onWillShutdown - no veto if user does not want to save', function (done) {
B
Benjamin Pasero 已提交
95 96 97
		model = instantiationService.createInstance(TextFileEditorModel, toResource.call(this, '/path/file.txt'), 'utf8');
		(<TextFileEditorModelManager>accessor.textFileService.models).add(model.getResource(), model);

B
Benjamin Pasero 已提交
98 99
		const service = accessor.textFileService;
		service.setConfirmResult(ConfirmResult.DONT_SAVE);
100

B
Benjamin Pasero 已提交
101
		model.load().done(() => {
102 103
			model.textEditorModel.setValue('foo');

B
Benjamin Pasero 已提交
104
			assert.equal(service.getDirty().length, 1);
105 106 107 108

			const event = new ShutdownEventImpl();
			accessor.lifecycleService.fireWillShutdown(event);

B
Benjamin Pasero 已提交
109 110 111
			const veto = event.value;
			if (typeof veto === 'boolean') {
				assert.ok(!veto);
D
Daniel Imms 已提交
112

B
Benjamin Pasero 已提交
113 114 115 116 117 118 119 120
				done();
			} else {
				veto.then(veto => {
					assert.ok(!veto);

					done();
				});
			}
B
Benjamin Pasero 已提交
121
		}, error => onError(error, done));
122 123 124
	});

	test('confirm onWillShutdown - save', function (done) {
B
Benjamin Pasero 已提交
125 126 127
		model = instantiationService.createInstance(TextFileEditorModel, toResource.call(this, '/path/file.txt'), 'utf8');
		(<TextFileEditorModelManager>accessor.textFileService.models).add(model.getResource(), model);

B
Benjamin Pasero 已提交
128 129
		const service = accessor.textFileService;
		service.setConfirmResult(ConfirmResult.SAVE);
130

B
Benjamin Pasero 已提交
131
		model.load().done(() => {
132 133
			model.textEditorModel.setValue('foo');

B
Benjamin Pasero 已提交
134
			assert.equal(service.getDirty().length, 1);
135 136 137 138 139 140 141 142 143 144

			const event = new ShutdownEventImpl();
			accessor.lifecycleService.fireWillShutdown(event);

			return (<TPromise<boolean>>event.value).then(veto => {
				assert.ok(!veto);
				assert.ok(!model.isDirty());

				done();
			});
B
Benjamin Pasero 已提交
145
		}, error => onError(error, done));
146
	});
B
Benjamin Pasero 已提交
147 148

	test('isDirty/getDirty - files and untitled', function (done) {
B
Benjamin Pasero 已提交
149 150 151
		model = instantiationService.createInstance(TextFileEditorModel, toResource.call(this, '/path/file.txt'), 'utf8');
		(<TextFileEditorModelManager>accessor.textFileService.models).add(model.getResource(), model);

B
Benjamin Pasero 已提交
152
		const service = accessor.textFileService;
B
Benjamin Pasero 已提交
153
		model.load().done(() => {
B
Benjamin Pasero 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
			assert.ok(!service.isDirty(model.getResource()));
			model.textEditorModel.setValue('foo');

			assert.ok(service.isDirty(model.getResource()));
			assert.equal(service.getDirty().length, 1);
			assert.equal(service.getDirty([model.getResource()])[0].toString(), model.getResource().toString());

			const untitled = accessor.untitledEditorService.createOrGet();
			return untitled.resolve().then((model: UntitledEditorModel) => {
				assert.ok(!service.isDirty(untitled.getResource()));
				assert.equal(service.getDirty().length, 1);
				model.setValue('changed');

				assert.ok(service.isDirty(untitled.getResource()));
				assert.equal(service.getDirty().length, 2);
				assert.equal(service.getDirty([untitled.getResource()])[0].toString(), untitled.getResource().toString());

				done();
			});
B
Benjamin Pasero 已提交
173
		}, error => onError(error, done));
B
Benjamin Pasero 已提交
174 175 176
	});

	test('save - file', function (done) {
B
Benjamin Pasero 已提交
177 178 179
		model = instantiationService.createInstance(TextFileEditorModel, toResource.call(this, '/path/file.txt'), 'utf8');
		(<TextFileEditorModelManager>accessor.textFileService.models).add(model.getResource(), model);

B
Benjamin Pasero 已提交
180 181
		const service = accessor.textFileService;

B
Benjamin Pasero 已提交
182
		model.load().done(() => {
B
Benjamin Pasero 已提交
183 184 185 186 187 188 189 190 191 192
			model.textEditorModel.setValue('foo');

			assert.ok(service.isDirty(model.getResource()));

			return service.save(model.getResource()).then(res => {
				assert.ok(res);
				assert.ok(!service.isDirty(model.getResource()));

				done();
			});
B
Benjamin Pasero 已提交
193
		}, error => onError(error, done));
B
Benjamin Pasero 已提交
194 195 196
	});

	test('saveAll - file', function (done) {
B
Benjamin Pasero 已提交
197 198 199
		model = instantiationService.createInstance(TextFileEditorModel, toResource.call(this, '/path/file.txt'), 'utf8');
		(<TextFileEditorModelManager>accessor.textFileService.models).add(model.getResource(), model);

B
Benjamin Pasero 已提交
200 201
		const service = accessor.textFileService;

B
Benjamin Pasero 已提交
202
		model.load().done(() => {
B
Benjamin Pasero 已提交
203 204 205 206 207 208 209 210 211 212 213 214
			model.textEditorModel.setValue('foo');

			assert.ok(service.isDirty(model.getResource()));

			return service.saveAll([model.getResource()]).then(res => {
				assert.ok(res);
				assert.ok(!service.isDirty(model.getResource()));
				assert.equal(res.results.length, 1);
				assert.equal(res.results[0].source.toString(), model.getResource().toString());

				done();
			});
B
Benjamin Pasero 已提交
215
		}, error => onError(error, done));
B
Benjamin Pasero 已提交
216 217 218
	});

	test('saveAs - file', function (done) {
B
Benjamin Pasero 已提交
219 220 221
		model = instantiationService.createInstance(TextFileEditorModel, toResource.call(this, '/path/file.txt'), 'utf8');
		(<TextFileEditorModelManager>accessor.textFileService.models).add(model.getResource(), model);

B
Benjamin Pasero 已提交
222 223 224
		const service = accessor.textFileService;
		service.setPromptPath(model.getResource().fsPath);

B
Benjamin Pasero 已提交
225
		model.load().done(() => {
B
Benjamin Pasero 已提交
226 227 228 229 230 231 232 233 234 235
			model.textEditorModel.setValue('foo');

			assert.ok(service.isDirty(model.getResource()));

			return service.saveAs(model.getResource()).then(res => {
				assert.equal(res.toString(), model.getResource().toString());
				assert.ok(!service.isDirty(model.getResource()));

				done();
			});
B
Benjamin Pasero 已提交
236
		}, error => onError(error, done));
B
Benjamin Pasero 已提交
237 238 239
	});

	test('revert - file', function (done) {
B
Benjamin Pasero 已提交
240 241 242
		model = instantiationService.createInstance(TextFileEditorModel, toResource.call(this, '/path/file.txt'), 'utf8');
		(<TextFileEditorModelManager>accessor.textFileService.models).add(model.getResource(), model);

B
Benjamin Pasero 已提交
243 244 245
		const service = accessor.textFileService;
		service.setPromptPath(model.getResource().fsPath);

B
Benjamin Pasero 已提交
246
		model.load().done(() => {
B
Benjamin Pasero 已提交
247 248 249 250 251 252 253 254 255 256
			model.textEditorModel.setValue('foo');

			assert.ok(service.isDirty(model.getResource()));

			return service.revert(model.getResource()).then(res => {
				assert.ok(res);
				assert.ok(!service.isDirty(model.getResource()));

				done();
			});
B
Benjamin Pasero 已提交
257
		}, error => onError(error, done));
B
Benjamin Pasero 已提交
258
	});
259
});