-
Notifications
You must be signed in to change notification settings - Fork 27.9k
/
fileEditorInput.test.ts
178 lines (135 loc) · 7.18 KB
/
fileEditorInput.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { URI } from 'vs/base/common/uri';
import { toResource } from 'vs/base/test/common/utils';
import { FileEditorInput } from 'vs/workbench/contrib/files/common/editors/fileEditorInput';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { workbenchInstantiationService, TestTextFileService } from 'vs/workbench/test/workbenchTestServices';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { EncodingMode, Verbosity } from 'vs/workbench/common/editor';
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
import { FileOperationResult, FileOperationError } from 'vs/platform/files/common/files';
import { TextFileEditorModel } from 'vs/workbench/services/textfile/common/textFileEditorModel';
import { IModelService } from 'vs/editor/common/services/modelService';
import { timeout } from 'vs/base/common/async';
class ServiceAccessor {
constructor(
@IEditorService public editorService: IEditorService,
@ITextFileService public textFileService: TestTextFileService,
@IModelService public modelService: IModelService
) {
}
}
suite('Files - FileEditorInput', () => {
let instantiationService: IInstantiationService;
let accessor: ServiceAccessor;
setup(() => {
instantiationService = workbenchInstantiationService();
accessor = instantiationService.createInstance(ServiceAccessor);
});
test('Basics', function () {
let input = instantiationService.createInstance(FileEditorInput, toResource.call(this, '/foo/bar/file.js'), undefined);
const otherInput = instantiationService.createInstance(FileEditorInput, toResource.call(this, 'foo/bar/otherfile.js'), undefined);
const otherInputSame = instantiationService.createInstance(FileEditorInput, toResource.call(this, 'foo/bar/file.js'), undefined);
assert(input.matches(input));
assert(input.matches(otherInputSame));
assert(!input.matches(otherInput));
assert(!input.matches(null));
assert.ok(input.getName());
assert.ok(input.getDescription());
assert.ok(input.getTitle(Verbosity.SHORT));
assert.strictEqual('file.js', input.getName());
assert.strictEqual(toResource.call(this, '/foo/bar/file.js').fsPath, input.getResource().fsPath);
assert(input.getResource() instanceof URI);
input = instantiationService.createInstance(FileEditorInput, toResource.call(this, '/foo/bar.html'), undefined);
const inputToResolve: FileEditorInput = instantiationService.createInstance(FileEditorInput, toResource.call(this, '/foo/bar/file.js'), undefined);
const sameOtherInput: FileEditorInput = instantiationService.createInstance(FileEditorInput, toResource.call(this, '/foo/bar/file.js'), undefined);
return inputToResolve.resolve().then(resolved => {
assert.ok(inputToResolve.isResolved());
const resolvedModelA = resolved;
return inputToResolve.resolve().then(resolved => {
assert(resolvedModelA === resolved); // OK: Resolved Model cached globally per input
return sameOtherInput.resolve().then(otherResolved => {
assert(otherResolved === resolvedModelA); // OK: Resolved Model cached globally per input
inputToResolve.dispose();
return inputToResolve.resolve().then(resolved => {
assert(resolvedModelA === resolved); // Model is still the same because we had 2 clients
inputToResolve.dispose();
sameOtherInput.dispose();
resolvedModelA.dispose();
return inputToResolve.resolve().then(resolved => {
assert(resolvedModelA !== resolved); // Different instance, because input got disposed
let stat = (resolved as TextFileEditorModel).getStat();
return inputToResolve.resolve().then(resolved => {
return timeout(0).then(() => { // due to file editor input using `reload: { async: true }`
assert(stat !== (resolved as TextFileEditorModel).getStat()); // Different stat, because resolve always goes to the server for refresh
});
});
});
});
});
});
});
});
test('matches', function () {
const input1 = instantiationService.createInstance(FileEditorInput, toResource.call(this, '/foo/bar/updatefile.js'), undefined);
const input2 = instantiationService.createInstance(FileEditorInput, toResource.call(this, '/foo/bar/updatefile.js'), undefined);
const input3 = instantiationService.createInstance(FileEditorInput, toResource.call(this, '/foo/bar/other.js'), undefined);
const input2Upper = instantiationService.createInstance(FileEditorInput, toResource.call(this, '/foo/bar/UPDATEFILE.js'), undefined);
assert.strictEqual(input1.matches(null), false);
assert.strictEqual(input1.matches(input1), true);
assert.strictEqual(input1.matches(input2), true);
assert.strictEqual(input1.matches(input3), false);
assert.strictEqual(input1.matches(input2Upper), false);
});
test('getEncoding/setEncoding', function () {
const input = instantiationService.createInstance(FileEditorInput, toResource.call(this, '/foo/bar/updatefile.js'), undefined);
input.setEncoding('utf16', EncodingMode.Encode);
assert.equal(input.getEncoding(), 'utf16');
return input.resolve().then((resolved: TextFileEditorModel) => {
assert.equal(input.getEncoding(), resolved.getEncoding());
resolved.dispose();
});
});
test('save', function () {
const input = instantiationService.createInstance(FileEditorInput, toResource.call(this, '/foo/bar/updatefile.js'), undefined);
return input.resolve().then((resolved: TextFileEditorModel) => {
resolved.textEditorModel.setValue('changed');
assert.ok(input.isDirty());
return input.save().then(() => {
assert.ok(!input.isDirty());
resolved.dispose();
});
});
});
test('revert', function () {
const input = instantiationService.createInstance(FileEditorInput, toResource.call(this, '/foo/bar/updatefile.js'), undefined);
return input.resolve().then((resolved: TextFileEditorModel) => {
resolved.textEditorModel.setValue('changed');
assert.ok(input.isDirty());
return input.revert().then(() => {
assert.ok(!input.isDirty());
resolved.dispose();
});
});
});
test('resolve handles binary files', function () {
const input = instantiationService.createInstance(FileEditorInput, toResource.call(this, '/foo/bar/updatefile.js'), undefined);
accessor.textFileService.setResolveTextContentErrorOnce(new FileOperationError('error', FileOperationResult.FILE_IS_BINARY));
return input.resolve().then(resolved => {
assert.ok(resolved);
resolved.dispose();
});
});
test('resolve handles too large files', function () {
const input = instantiationService.createInstance(FileEditorInput, toResource.call(this, '/foo/bar/updatefile.js'), undefined);
accessor.textFileService.setResolveTextContentErrorOnce(new FileOperationError('error', FileOperationResult.FILE_TOO_LARGE));
return input.resolve().then(resolved => {
assert.ok(resolved);
resolved.dispose();
});
});
});