Skip to content

Commit 50487cf

Browse files
author
Benjamin Pasero
committed
first cut textfile service tests
1 parent 77378e5 commit 50487cf

5 files changed

Lines changed: 209 additions & 68 deletions

File tree

src/vs/test/utils/servicesTestUtils.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import * as paths from 'vs/base/common/paths';
1212
import URI from 'vs/base/common/uri';
1313
import {ITelemetryService, NullTelemetryService} from 'vs/platform/telemetry/common/telemetry';
1414
import {Storage, InMemoryLocalStorage} from 'vs/workbench/common/storage';
15-
import {EditorInputEvent, IEditorGroup} from 'vs/workbench/common/editor';
15+
import {EditorInputEvent, IEditorGroup, ConfirmResult} from 'vs/workbench/common/editor';
1616
import Event, {Emitter} from 'vs/base/common/event';
1717
import Severity from 'vs/base/common/severity';
1818
import {IConfigurationService, getConfigurationValue, IConfigurationValue} from 'vs/platform/configuration/common/configuration';
@@ -24,7 +24,7 @@ import {IEventService} from 'vs/platform/event/common/event';
2424
import {IUntitledEditorService, UntitledEditorService} from 'vs/workbench/services/untitled/common/untitledEditorService';
2525
import {IMessageService, IConfirmation} from 'vs/platform/message/common/message';
2626
import {IWorkspace, IWorkspaceContextService} from 'vs/platform/workspace/common/workspace';
27-
import {ILifecycleService, ShutdownEvent, NullLifecycleService} from 'vs/platform/lifecycle/common/lifecycle';
27+
import {ILifecycleService, ShutdownEvent} from 'vs/platform/lifecycle/common/lifecycle';
2828
import {EditorStacksModel} from 'vs/workbench/common/editor/editorStacksModel';
2929
import {ServiceCollection} from 'vs/platform/instantiation/common/serviceCollection';
3030
import {InstantiationService} from 'vs/platform/instantiation/common/instantiationService';
@@ -90,7 +90,9 @@ export class TestContextService implements IWorkspaceContextService {
9090
}
9191
}
9292

93-
export abstract class TestTextFileService extends TextFileService {
93+
export class TestTextFileService extends TextFileService {
94+
private promptPath: string;
95+
private confirmResult: ConfirmResult;
9496

9597
constructor(
9698
@ILifecycleService lifecycleService: ILifecycleService,
@@ -105,6 +107,14 @@ export abstract class TestTextFileService extends TextFileService {
105107
super(lifecycleService, contextService, configurationService, telemetryService, editorGroupService, editorService, fileService, untitledEditorService);
106108
}
107109

110+
public setPromptPath(path: string): void {
111+
this.promptPath = path;
112+
}
113+
114+
public setConfirmResult(result: ConfirmResult): void {
115+
this.confirmResult = result;
116+
}
117+
108118
public resolveTextContent(resource: URI, options?: IResolveContentOptions): TPromise<IRawTextContent> {
109119
return this.fileService.resolveContent(resource, options).then((content) => {
110120
const raw = RawText.fromString(content.value, { defaultEOL: 1, detectIndentation: false, insertSpaces: false, tabSize: 4, trimAutoWhitespace: false });
@@ -121,10 +131,18 @@ export abstract class TestTextFileService extends TextFileService {
121131
};
122132
});
123133
}
134+
135+
public promptForPath(defaultPath?: string): string {
136+
return this.promptPath;
137+
}
138+
139+
public confirmSave(resources?: URI[]): ConfirmResult {
140+
return this.confirmResult;
141+
}
124142
}
125143

126144
export function textFileServiceInstantiationService(): TestInstantiationService {
127-
let instantiationService = new TestInstantiationService();
145+
let instantiationService = new TestInstantiationService(new ServiceCollection([ILifecycleService, new TestLifecycleService()]));
128146
instantiationService.stub(IEventService, new TestEventService());
129147
instantiationService.stub(IWorkspaceContextService, new TestContextService(TestWorkspace));
130148
instantiationService.stub(IConfigurationService, new TestConfigurationService());
@@ -136,11 +154,10 @@ export function textFileServiceInstantiationService(): TestInstantiationService
136154
instantiationService.stub(IModeService);
137155
instantiationService.stub(IHistoryService, 'getHistory', []);
138156
instantiationService.stub(IModelService, createMockModelService(instantiationService));
139-
instantiationService.stub(ILifecycleService, NullLifecycleService);
140157
instantiationService.stub(IFileService, TestFileService);
141158
instantiationService.stub(ITelemetryService, NullTelemetryService);
142159
instantiationService.stub(IMessageService, new TestMessageService());
143-
instantiationService.stub(ITextFileService, <ITextFileService>instantiationService.createInstance(<any>TestTextFileService));
160+
instantiationService.stub(ITextFileService, <ITextFileService>instantiationService.createInstance(TestTextFileService));
144161

145162
return instantiationService;
146163
}
@@ -604,6 +621,10 @@ export class TestLifecycleService implements ILifecycleService {
604621
this._onShutdown.fire();
605622
}
606623

624+
public fireWillShutdown(event: ShutdownEvent): void {
625+
this._onWillShutdown.fire(event);
626+
}
627+
607628
public get onWillShutdown(): Event<ShutdownEvent> {
608629
return this._onWillShutdown.event;
609630
}

src/vs/workbench/parts/files/test/browser/viewModel.test.ts renamed to src/vs/workbench/parts/files/test/browser/explorerViewModel.test.ts

File renamed without changes.

src/vs/workbench/parts/files/test/browser/fileEditorModel.test.ts

Lines changed: 62 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ suite('Files - TextFileEditorModel', () => {
4141
});
4242

4343
test('Load does not trigger save', function (done) {
44-
const m1 = instantiationService.createInstance(TextFileEditorModel, toResource('/path/index.txt'), 'utf8');
44+
const model = instantiationService.createInstance(TextFileEditorModel, toResource('/path/index.txt'), 'utf8');
4545

4646
accessor.eventService.addListener2('files:internalFileChanged', () => {
4747
assert.ok(false);
@@ -55,26 +55,26 @@ suite('Files - TextFileEditorModel', () => {
5555
assert.ok(false);
5656
});
5757

58-
m1.load().then(() => {
59-
assert.ok(m1.isResolved());
58+
model.load().then(() => {
59+
assert.ok(model.isResolved());
6060

61-
m1.dispose();
61+
model.dispose();
6262

6363
done();
6464
});
6565
});
6666

6767
test('Load returns dirty model as long as model is dirty', function (done) {
68-
const m1 = instantiationService.createInstance(TextFileEditorModel, toResource('/path/index_async.txt'), 'utf8');
68+
const model = instantiationService.createInstance(TextFileEditorModel, toResource('/path/index_async.txt'), 'utf8');
6969

70-
m1.load().then(() => {
71-
m1.textEditorModel.setValue('foo');
70+
model.load().then(() => {
71+
model.textEditorModel.setValue('foo');
7272

73-
assert.ok(m1.isDirty());
74-
m1.load().then(() => {
75-
assert.ok(m1.isDirty());
73+
assert.ok(model.isDirty());
74+
model.load().then(() => {
75+
assert.ok(model.isDirty());
7676

77-
m1.dispose();
77+
model.dispose();
7878

7979
done();
8080
});
@@ -88,43 +88,43 @@ suite('Files - TextFileEditorModel', () => {
8888
eventCounter++;
8989
});
9090

91-
const m1 = instantiationService.createInstance(TextFileEditorModel, toResource('/path/index_async.txt'), 'utf8');
91+
const model = instantiationService.createInstance(TextFileEditorModel, toResource('/path/index_async.txt'), 'utf8');
9292

93-
m1.load().then(() => {
94-
m1.textEditorModel.setValue('foo');
93+
model.load().then(() => {
94+
model.textEditorModel.setValue('foo');
9595

96-
assert.ok(m1.isDirty());
96+
assert.ok(model.isDirty());
9797

98-
m1.revert().then(() => {
99-
assert.ok(!m1.isDirty());
100-
assert.equal(m1.textEditorModel.getValue(), 'Hello Html');
98+
model.revert().then(() => {
99+
assert.ok(!model.isDirty());
100+
assert.equal(model.textEditorModel.getValue(), 'Hello Html');
101101
assert.equal(eventCounter, 1);
102102

103-
m1.dispose();
103+
model.dispose();
104104

105105
done();
106106
});
107107
});
108108
});
109109

110110
test('Conflict Resolution Mode', function (done) {
111-
const m1 = instantiationService.createInstance(TextFileEditorModel, toResource('/path/index_async.txt'), 'utf8');
111+
const model = instantiationService.createInstance(TextFileEditorModel, toResource('/path/index_async.txt'), 'utf8');
112112

113-
m1.load().then(() => {
114-
m1.setConflictResolutionMode();
115-
m1.textEditorModel.setValue('foo');
113+
model.load().then(() => {
114+
model.setConflictResolutionMode();
115+
model.textEditorModel.setValue('foo');
116116

117-
assert.ok(m1.isDirty());
118-
assert.ok(m1.isInConflictResolutionMode());
117+
assert.ok(model.isDirty());
118+
assert.ok(model.isInConflictResolutionMode());
119119

120-
m1.revert().then(() => {
121-
m1.textEditorModel.setValue('bar');
122-
assert.ok(m1.isDirty());
120+
model.revert().then(() => {
121+
model.textEditorModel.setValue('bar');
122+
assert.ok(model.isDirty());
123123

124-
return m1.save().then(() => {
125-
assert.ok(!m1.isDirty());
124+
return model.save().then(() => {
125+
assert.ok(!model.isDirty());
126126

127-
m1.dispose();
127+
model.dispose();
128128

129129
done();
130130
});
@@ -134,10 +134,10 @@ suite('Files - TextFileEditorModel', () => {
134134

135135
test('Auto Save triggered when model changes', function (done) {
136136
let eventCounter = 0;
137-
const m1 = instantiationService.createInstance(TextFileEditorModel, toResource('/path/index.txt'), 'utf8');
137+
const model = instantiationService.createInstance(TextFileEditorModel, toResource('/path/index.txt'), 'utf8');
138138

139-
(<any>m1).autoSaveAfterMillies = 10;
140-
(<any>m1).autoSaveAfterMilliesEnabled = true;
139+
(<any>model).autoSaveAfterMillies = 10;
140+
(<any>model).autoSaveAfterMilliesEnabled = true;
141141

142142
accessor.eventService.addListener2(EventType.FILE_DIRTY, () => {
143143
eventCounter++;
@@ -147,51 +147,51 @@ suite('Files - TextFileEditorModel', () => {
147147
eventCounter++;
148148
});
149149

150-
m1.load().then(() => {
151-
m1.textEditorModel.setValue('foo');
150+
model.load().then(() => {
151+
model.textEditorModel.setValue('foo');
152152

153153
return TPromise.timeout(50).then(() => {
154-
assert.ok(!m1.isDirty());
154+
assert.ok(!model.isDirty());
155155
assert.equal(eventCounter, 2);
156156

157-
m1.dispose();
157+
model.dispose();
158158

159159
done();
160160
});
161161
});
162162
});
163163

164164
test('save() and isDirty() - proper with check for mtimes', function (done) {
165-
const c1 = instantiationService.createInstance(FileEditorInput, toResource('/path/index_async2.txt'), 'text/plain', 'utf8');
166-
const c2 = instantiationService.createInstance(FileEditorInput, toResource('/path/index_async.txt'), 'text/plain', 'utf8');
165+
const input1 = instantiationService.createInstance(FileEditorInput, toResource('/path/index_async2.txt'), 'text/plain', 'utf8');
166+
const input2 = instantiationService.createInstance(FileEditorInput, toResource('/path/index_async.txt'), 'text/plain', 'utf8');
167167

168-
c1.resolve().then((m1: TextFileEditorModel) => {
169-
c2.resolve().then((m2: TextFileEditorModel) => {
170-
m1.textEditorModel.setValue('foo');
168+
input1.resolve().then((model1: TextFileEditorModel) => {
169+
input2.resolve().then((model2: TextFileEditorModel) => {
170+
model1.textEditorModel.setValue('foo');
171171

172-
const m1Mtime = m1.getLastModifiedTime();
173-
const m2Mtime = m2.getLastModifiedTime();
172+
const m1Mtime = model1.getLastModifiedTime();
173+
const m2Mtime = model2.getLastModifiedTime();
174174
assert.ok(m1Mtime > 0);
175175
assert.ok(m2Mtime > 0);
176176

177177
assert.ok(accessor.textFileService.isDirty());
178178
assert.ok(accessor.textFileService.isDirty(toResource('/path/index_async2.txt')));
179179
assert.ok(!accessor.textFileService.isDirty(toResource('/path/index_async.txt')));
180180

181-
m2.textEditorModel.setValue('foo');
181+
model2.textEditorModel.setValue('foo');
182182
assert.ok(accessor.textFileService.isDirty(toResource('/path/index_async.txt')));
183183

184184
return TPromise.timeout(10).then(() => {
185185
accessor.textFileService.saveAll().then(() => {
186186
assert.ok(!accessor.textFileService.isDirty(toResource('/path/index_async.txt')));
187187
assert.ok(!accessor.textFileService.isDirty(toResource('/path/index_async2.txt')));
188-
assert.ok(m1.getLastModifiedTime() > m1Mtime);
189-
assert.ok(m2.getLastModifiedTime() > m2Mtime);
190-
assert.ok(m1.getLastSaveAttemptTime() > m1Mtime);
191-
assert.ok(m2.getLastSaveAttemptTime() > m2Mtime);
188+
assert.ok(model1.getLastModifiedTime() > m1Mtime);
189+
assert.ok(model2.getLastModifiedTime() > m2Mtime);
190+
assert.ok(model1.getLastSaveAttemptTime() > m1Mtime);
191+
assert.ok(model2.getLastSaveAttemptTime() > m2Mtime);
192192

193-
m1.dispose();
194-
m2.dispose();
193+
model1.dispose();
194+
model2.dispose();
195195

196196
done();
197197
});
@@ -202,26 +202,26 @@ suite('Files - TextFileEditorModel', () => {
202202

203203
test('Save Participant', function (done) {
204204
let eventCounter = 0;
205-
const m1 = instantiationService.createInstance(TextFileEditorModel, toResource('/path/index_async.txt'), 'utf8');
205+
const model = instantiationService.createInstance(TextFileEditorModel, toResource('/path/index_async.txt'), 'utf8');
206206

207207
accessor.eventService.addListener2(EventType.FILE_SAVED, (e) => {
208-
assert.equal(m1.getValue(), 'bar');
209-
assert.ok(!m1.isDirty());
208+
assert.equal(model.getValue(), 'bar');
209+
assert.ok(!model.isDirty());
210210
eventCounter++;
211211
});
212212

213213
accessor.eventService.addListener2(EventType.FILE_SAVING, (e) => {
214-
assert.ok(m1.isDirty());
215-
m1.textEditorModel.setValue('bar');
216-
assert.ok(m1.isDirty());
214+
assert.ok(model.isDirty());
215+
model.textEditorModel.setValue('bar');
216+
assert.ok(model.isDirty());
217217
eventCounter++;
218218
});
219219

220-
m1.load().then(() => {
221-
m1.textEditorModel.setValue('foo');
220+
model.load().then(() => {
221+
model.textEditorModel.setValue('foo');
222222

223-
m1.save().then(() => {
224-
m1.dispose();
223+
model.save().then(() => {
224+
model.dispose();
225225

226226
assert.equal(eventCounter, 2);
227227

src/vs/workbench/parts/files/test/browser/events.test.ts renamed to src/vs/workbench/parts/files/test/browser/fileEvents.test.ts

File renamed without changes.

0 commit comments

Comments
 (0)