@@ -43,34 +43,55 @@ define(function (require, exports, module) {
4343 describe ( "DocumentCommandHandlers" , function ( ) {
4444 this . category = "integration" ;
4545
46- var testPath = SpecRunnerUtils . getTestPath ( "/spec/DocumentCommandHandlers-test-files" ) ,
47- testWindow ;
46+ var topLevelSuite = this ,
47+ testPath = SpecRunnerUtils . getTestPath ( "/spec/DocumentCommandHandlers-test-files" ) ,
48+ testWindow ,
49+ specCount ,
50+ promise ;
4851
4952 var TEST_JS_CONTENT = 'var myContent="This is awesome!";' ;
5053 var TEST_JS_NEW_CONTENT = "hello world" ;
5154 var TEST_JS_SECOND_NEW_CONTENT = "hello world 2" ;
5255
5356 beforeEach ( function ( ) {
54- SpecRunnerUtils . createTestWindowAndRun ( this , function ( w ) {
55- testWindow = w ;
56-
57- // Load module instances from brackets.test
58- CommandManager = testWindow . brackets . test . CommandManager ;
59- Commands = testWindow . brackets . test . Commands ;
60- DocumentCommandHandlers = testWindow . brackets . test . DocumentCommandHandlers ;
61- DocumentManager = testWindow . brackets . test . DocumentManager ;
62- Dialogs = testWindow . brackets . test . Dialogs ;
63- FileViewController = testWindow . brackets . test . FileViewController ;
64- } ) ;
57+ if ( specCount === undefined ) {
58+ specCount = SpecRunnerUtils . countSpecs ( topLevelSuite ) ;
59+ }
60+
61+ if ( ! testWindow ) {
62+ SpecRunnerUtils . createTestWindowAndRun ( this , function ( w ) {
63+ testWindow = w ;
64+
65+ // Load module instances from brackets.test
66+ CommandManager = testWindow . brackets . test . CommandManager ;
67+ Commands = testWindow . brackets . test . Commands ;
68+ DocumentCommandHandlers = testWindow . brackets . test . DocumentCommandHandlers ;
69+ DocumentManager = testWindow . brackets . test . DocumentManager ;
70+ Dialogs = testWindow . brackets . test . Dialogs ;
71+ FileViewController = testWindow . brackets . test . FileViewController ;
72+ } ) ;
73+ }
74+
75+ // Working set behavior is sensitive to whether file lives in the project or outside it, so make
76+ // the project root a known quantity.
77+ SpecRunnerUtils . loadProjectInTestWindow ( testPath ) ;
6578 } ) ;
6679
6780 afterEach ( function ( ) {
68- testWindow = null ;
69- CommandManager = null ;
70- Commands = null ;
71- DocumentCommandHandlers = null ;
72- DocumentManager = null ;
73- SpecRunnerUtils . closeTestWindow ( ) ;
81+ specCount -- ;
82+ promise = null ;
83+ testWindow . closeAllDocuments ( ) ;
84+
85+ runs ( function ( ) {
86+ if ( specCount === 0 ) {
87+ testWindow = null ;
88+ CommandManager = null ;
89+ Commands = null ;
90+ DocumentCommandHandlers = null ;
91+ DocumentManager = null ;
92+ SpecRunnerUtils . closeTestWindow ( ) ;
93+ }
94+ } ) ;
7495 } ) ;
7596
7697
@@ -88,12 +109,16 @@ define(function (require, exports, module) {
88109
89110
90111 describe ( "New Untitled File" , function ( ) {
112+ var filePath ,
113+ newFilename ,
114+ newFilePath ;
115+
91116 beforeEach ( function ( ) {
92- // Working set behavior is sensitive to whether file lives in the project or outside it, so make
93- // the project root a known quantity.
94- SpecRunnerUtils . loadProjectInTestWindow ( testPath ) ;
117+ filePath = testPath + "/test.js" ;
118+ newFilename = "testname.js" ;
119+ newFilePath = testPath + "/" + newFilename ;
95120 } ) ;
96-
121+
97122 /** @return {Array.<Document> } */
98123 function getWorkingSetDocs ( ) {
99124 return DocumentManager . getWorkingSet ( ) . map ( function ( file ) {
@@ -123,8 +148,6 @@ define(function (require, exports, module) {
123148 // Single untitled documents
124149
125150 it ( "should create a new untitled document in the Working Set" , function ( ) {
126- var promise ;
127-
128151 runs ( function ( ) {
129152 promise = CommandManager . execute ( Commands . FILE_NEW_UNTITLED ) ;
130153
@@ -149,10 +172,6 @@ define(function (require, exports, module) {
149172 } ) ;
150173
151174 it ( "should swap out untitled document in the Working Set after saving with new name" , function ( ) {
152- var newFilename = "testname.js" ,
153- newFilePath = testPath + "/" + newFilename ,
154- promise ;
155-
156175 runs ( function ( ) {
157176 promise = CommandManager . execute ( Commands . FILE_NEW_UNTITLED ) ;
158177
@@ -183,9 +202,6 @@ define(function (require, exports, module) {
183202 } ) ;
184203
185204 it ( "should swap out untitled document from working set even when not current" , function ( ) {
186- var newFilePath = testPath + "/testname.js" ,
187- promise ;
188-
189205 runs ( function ( ) {
190206 promise = CommandManager . execute ( Commands . FILE_NEW_UNTITLED ) ;
191207
@@ -223,9 +239,8 @@ define(function (require, exports, module) {
223239 } ) ;
224240
225241 it ( "should ask to save untitled document upon closing" , function ( ) {
226- var newFilename = "testname2.js" ,
227- newFilePath = testPath + "/" + newFilename ,
228- promise ;
242+ newFilename = "testname2.js" ;
243+ newFilePath = testPath + "/" + newFilename ;
229244
230245 runs ( function ( ) {
231246 promise = CommandManager . execute ( Commands . FILE_NEW_UNTITLED ) ;
@@ -260,8 +275,6 @@ define(function (require, exports, module) {
260275 } ) ;
261276
262277 it ( "should keep dirty untitled document in Working Set when close document is canceled" , function ( ) {
263- var promise ;
264-
265278 runs ( function ( ) {
266279 promise = CommandManager . execute ( Commands . FILE_NEW_UNTITLED ) ;
267280
@@ -292,8 +305,6 @@ define(function (require, exports, module) {
292305 } ) ;
293306
294307 it ( "should keep dirty untitled document in Working Set when saving during close is canceled" , function ( ) {
295- var promise ;
296-
297308 runs ( function ( ) {
298309 promise = CommandManager . execute ( Commands . FILE_NEW_UNTITLED ) ;
299310
@@ -328,8 +339,6 @@ define(function (require, exports, module) {
328339 } ) ;
329340
330341 it ( "should remove dirty untitled Document from Working Set when closing document is not saved" , function ( ) {
331- var promise ;
332-
333342 runs ( function ( ) {
334343 promise = CommandManager . execute ( Commands . FILE_NEW_UNTITLED ) ;
335344
@@ -356,8 +365,6 @@ define(function (require, exports, module) {
356365 } ) ;
357366
358367 it ( "should remove new untitled Document from Working Set upon closing" , function ( ) {
359- var promise ;
360-
361368 runs ( function ( ) {
362369 promise = CommandManager . execute ( Commands . FILE_NEW_UNTITLED ) ;
363370
@@ -590,8 +597,6 @@ define(function (require, exports, module) {
590597
591598 describe ( "Close File" , function ( ) {
592599 it ( "should complete without error if no files are open" , function ( ) {
593- var promise ;
594-
595600 runs ( function ( ) {
596601 promise = CommandManager . execute ( Commands . FILE_CLOSE ) ;
597602 waitsForDone ( promise , "FILE_CLOSE" ) ;
@@ -739,15 +744,58 @@ define(function (require, exports, module) {
739744 } ) ;
740745
741746 describe ( "Save As" , function ( ) {
742- it ( "should close the original file, reopen the saved file and add it to the Working Set" , function ( ) {
743- var filePath = testPath + "/test.js" ,
744- newFilename = "testname.js" ,
745- newFilePath = testPath + "/" + newFilename ,
746- promise ;
747-
747+ var filePath ,
748+ newFilename ,
749+ newFilePath ;
750+
751+ beforeEach ( function ( ) {
752+ filePath = testPath + "/test.js" ;
753+ newFilename = "testname.js" ;
754+ newFilePath = testPath + "/" + newFilename ;
755+ } ) ;
756+
757+ it ( "should close the original file, reopen the saved file and add select the new file in the project tree" , function ( ) {
748758 runs ( function ( ) {
759+ // Open the file, does not add to working set
749760 promise = CommandManager . execute ( Commands . FILE_OPEN , { fullPath : filePath } ) ;
761+ waitsForDone ( promise , "FILE_OPEN" ) ;
762+ } ) ;
763+
764+ runs ( function ( ) {
765+ var currentDocument = DocumentManager . getCurrentDocument ( ) ;
766+ expect ( currentDocument . file . fullPath ) . toEqual ( filePath ) ;
767+ } ) ;
768+
769+ runs ( function ( ) {
770+ spyOn ( testWindow . brackets . fs , 'showSaveDialog' ) . andCallFake ( function ( dialogTitle , initialPath , proposedNewName , callback ) {
771+ callback ( undefined , newFilePath ) ;
772+ } ) ;
773+
774+ promise = CommandManager . execute ( Commands . FILE_SAVE_AS ) ;
775+ waitsForDone ( promise , "Provide new filename" ) ;
776+ } ) ;
777+
778+ runs ( function ( ) {
779+ var currentDocument = DocumentManager . getCurrentDocument ( ) ;
780+ expect ( currentDocument . file . fullPath ) . toEqual ( newFilePath ) ;
781+ } ) ;
782+
783+ runs ( function ( ) {
784+ // New file should not appear in working set
785+ expect ( DocumentManager . findInWorkingSet ( newFilePath ) ) . toEqual ( - 1 ) ;
786+
787+ // Verify file exists & clean it up
788+ expectAndDelete ( newFilePath ) ;
789+ } ) ;
790+ } ) ;
750791
792+ it ( "should close the original file, reopen the saved file outside the project and add it to the Working Set" , function ( ) {
793+ newFilePath = SpecRunnerUtils . getTempDirectory ( ) + "/" + newFilename ;
794+
795+ SpecRunnerUtils . createTempDirectory ( ) ;
796+
797+ runs ( function ( ) {
798+ promise = CommandManager . execute ( Commands . FILE_OPEN , { fullPath : filePath } ) ;
751799 waitsForDone ( promise , "FILE_OPEN" ) ;
752800 } ) ;
753801
@@ -781,11 +829,6 @@ define(function (require, exports, module) {
781829 } ) ;
782830
783831 it ( "should leave Working Set untouched when operation is canceled" , function ( ) {
784- var filePath = testPath + "/test.js" ,
785- newFilename = "testname.js" ,
786- newFilePath = testPath + "/" + newFilename ,
787- promise ;
788-
789832 runs ( function ( ) {
790833 promise = CommandManager . execute ( Commands . FILE_OPEN , { fullPath : filePath } ) ;
791834
@@ -817,12 +860,8 @@ define(function (require, exports, module) {
817860 } ) ;
818861
819862 it ( "should maintain order within Working Set after Save As" , function ( ) {
820- var filePath = testPath + "/test.js" ,
821- newFilename = "testname.js" ,
822- newFilePath = testPath + "/" + newFilename ,
823- index ,
824- targetDoc ,
825- promise ;
863+ var index ,
864+ targetDoc ;
826865
827866 runs ( function ( ) {
828867 // open the target file
0 commit comments