@@ -61,8 +61,13 @@ suite('Files - TextFileEditorModel', () => {
6161 test ( 'basic events' , async function ( ) {
6262 const model = instantiationService . createInstance ( TextFileEditorModel , toResource . call ( this , '/path/index_async.txt' ) , 'utf8' , undefined ) ;
6363
64+ let onDidLoadCounter = 0 ;
65+ model . onDidLoad ( ( ) => onDidLoadCounter ++ ) ;
66+
6467 await model . load ( ) ;
6568
69+ assert . equal ( onDidLoadCounter , 1 ) ;
70+
6671 let onDidChangeContentCounter = 0 ;
6772 model . onDidChangeContent ( ( ) => onDidChangeContentCounter ++ ) ;
6873
@@ -101,9 +106,7 @@ suite('Files - TextFileEditorModel', () => {
101106 assert . equal ( accessor . workingCopyService . isDirty ( model . resource ) , true ) ;
102107
103108 let savedEvent = false ;
104- model . onDidSave ( e => {
105- savedEvent = true ;
106- } ) ;
109+ model . onDidSave ( e => savedEvent = true ) ;
107110
108111 let workingCopyEvent = false ;
109112 accessor . workingCopyService . onDidChangeDirty ( e => {
@@ -136,9 +139,7 @@ suite('Files - TextFileEditorModel', () => {
136139 await model . load ( ) ;
137140
138141 let savedEvent = false ;
139- model . onDidSave ( e => {
140- savedEvent = true ;
141- } ) ;
142+ model . onDidSave ( e => savedEvent = true ) ;
142143
143144 let workingCopyEvent = false ;
144145 accessor . workingCopyService . onDidChangeDirty ( e => {
@@ -156,14 +157,81 @@ suite('Files - TextFileEditorModel', () => {
156157 assert . ok ( ! accessor . modelService . getModel ( model . resource ) ) ;
157158 } ) ;
158159
160+ test ( 'save error (generic)' , async function ( ) {
161+ const model : TextFileEditorModel = instantiationService . createInstance ( TextFileEditorModel , toResource . call ( this , '/path/index_async.txt' ) , 'utf8' , undefined ) ;
162+
163+ await model . load ( ) ;
164+
165+ model . textEditorModel ! . setValue ( 'bar' ) ;
166+
167+ let saveErrorEvent = false ;
168+ model . onDidSaveError ( e => saveErrorEvent = true ) ;
169+
170+ accessor . fileService . writeShouldThrowError = new Error ( 'failed to write' ) ;
171+ try {
172+ const pendingSave = model . save ( ) ;
173+ assert . ok ( model . hasState ( ModelState . PENDING_SAVE ) ) ;
174+
175+ await pendingSave ;
176+
177+ assert . ok ( model . hasState ( ModelState . ERROR ) ) ;
178+ assert . ok ( model . isDirty ( ) ) ;
179+ assert . ok ( saveErrorEvent ) ;
180+
181+ assert . equal ( accessor . workingCopyService . dirtyCount , 1 ) ;
182+ assert . equal ( accessor . workingCopyService . isDirty ( model . resource ) , true ) ;
183+
184+ model . dispose ( ) ;
185+ } finally {
186+ accessor . fileService . writeShouldThrowError = undefined ;
187+ }
188+ } ) ;
189+
190+ test ( 'save error (conflict)' , async function ( ) {
191+ const model : TextFileEditorModel = instantiationService . createInstance ( TextFileEditorModel , toResource . call ( this , '/path/index_async.txt' ) , 'utf8' , undefined ) ;
192+
193+ await model . load ( ) ;
194+
195+ model . textEditorModel ! . setValue ( 'bar' ) ;
196+
197+ let saveErrorEvent = false ;
198+ model . onDidSaveError ( e => saveErrorEvent = true ) ;
199+
200+ accessor . fileService . writeShouldThrowError = new FileOperationError ( 'save conflict' , FileOperationResult . FILE_MODIFIED_SINCE ) ;
201+ try {
202+ const pendingSave = model . save ( ) ;
203+ assert . ok ( model . hasState ( ModelState . PENDING_SAVE ) ) ;
204+
205+ await pendingSave ;
206+
207+ assert . ok ( model . hasState ( ModelState . CONFLICT ) ) ;
208+ assert . ok ( model . isDirty ( ) ) ;
209+ assert . ok ( saveErrorEvent ) ;
210+
211+ assert . equal ( accessor . workingCopyService . dirtyCount , 1 ) ;
212+ assert . equal ( accessor . workingCopyService . isDirty ( model . resource ) , true ) ;
213+
214+ model . dispose ( ) ;
215+ } finally {
216+ accessor . fileService . writeShouldThrowError = undefined ;
217+ }
218+ } ) ;
219+
159220 test ( 'setEncoding - encode' , function ( ) {
160221 const model : TextFileEditorModel = instantiationService . createInstance ( TextFileEditorModel , toResource . call ( this , '/path/index_async.txt' ) , 'utf8' , undefined ) ;
161222
223+ let encodingEvent = false ;
224+ model . onDidChangeEncoding ( e => encodingEvent = true ) ;
225+
162226 model . setEncoding ( 'utf8' , EncodingMode . Encode ) ; // no-op
163227 assert . equal ( getLastModifiedTime ( model ) , - 1 ) ;
164228
229+ assert . ok ( ! encodingEvent ) ;
230+
165231 model . setEncoding ( 'utf16' , EncodingMode . Encode ) ;
166232
233+ assert . ok ( encodingEvent ) ;
234+
167235 assert . ok ( getLastModifiedTime ( model ) <= Date . now ( ) ) ; // indicates model was saved due to encoding change
168236
169237 model . dispose ( ) ;
@@ -208,13 +276,8 @@ suite('Files - TextFileEditorModel', () => {
208276 const model = instantiationService . createInstance ( TextFileEditorModel , toResource . call ( this , '/path/index.txt' ) , 'utf8' , undefined ) ;
209277 assert . ok ( model . hasState ( ModelState . SAVED ) ) ;
210278
211- model . onDidSave ( e => {
212- assert . fail ( ) ;
213- } ) ;
214-
215- model . onDidChangeDirty ( e => {
216- assert . fail ( ) ;
217- } ) ;
279+ model . onDidSave ( e => assert . fail ( ) ) ;
280+ model . onDidChangeDirty ( e => assert . fail ( ) ) ;
218281
219282 await model . load ( ) ;
220283 assert . ok ( model . isResolved ( ) ) ;
@@ -240,9 +303,7 @@ suite('Files - TextFileEditorModel', () => {
240303
241304 const model = instantiationService . createInstance ( TextFileEditorModel , toResource . call ( this , '/path/index_async.txt' ) , 'utf8' , undefined ) ;
242305
243- model . onDidRevert ( e => {
244- eventCounter ++ ;
245- } ) ;
306+ model . onDidRevert ( e => eventCounter ++ ) ;
246307
247308 let workingCopyEvent = false ;
248309 accessor . workingCopyService . onDidChangeDirty ( e => {
@@ -275,9 +336,7 @@ suite('Files - TextFileEditorModel', () => {
275336
276337 const model = instantiationService . createInstance ( TextFileEditorModel , toResource . call ( this , '/path/index_async.txt' ) , 'utf8' , undefined ) ;
277338
278- model . onDidRevert ( e => {
279- eventCounter ++ ;
280- } ) ;
339+ model . onDidRevert ( e => eventCounter ++ ) ;
281340
282341 let workingCopyEvent = false ;
283342 accessor . workingCopyService . onDidChangeDirty ( e => {
@@ -333,9 +392,7 @@ suite('Files - TextFileEditorModel', () => {
333392 await model . revert ( { soft : true } ) ;
334393 assert . ok ( ! model . isDirty ( ) ) ;
335394
336- model . onDidChangeDirty ( e => {
337- eventCounter ++ ;
338- } ) ;
395+ model . onDidChangeDirty ( e => eventCounter ++ ) ;
339396
340397 let workingCopyEvent = false ;
341398 accessor . workingCopyService . onDidChangeDirty ( e => {
0 commit comments