This repository was archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Expand file tree
/
Copy pathInlineEditorProviders-test.js
More file actions
1384 lines (1118 loc) · 66.1 KB
/
InlineEditorProviders-test.js
File metadata and controls
1384 lines (1118 loc) · 66.1 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2012 Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/*jslint vars: true, plusplus: true, devel: true, browser: true, nomen: true, indent: 4, maxerr: 50 */
/*global define, describe, it, expect, beforeEach, afterEach, waits, waitsFor, waitsForDone, waitsForFail, runs, $, brackets, beforeFirst, afterLast */
define(function (require, exports, module) {
'use strict';
var Commands, // loaded from brackets.test
EditorManager, // loaded from brackets.test
FileSyncManager, // loaded from brackets.test
DocumentManager, // loaded from brackets.test
FileViewController, // loaded from brackets.test
InlineWidget = require("editor/InlineWidget").InlineWidget,
Dialogs = require("widgets/Dialogs"),
KeyEvent = require("utils/KeyEvent"),
FileUtils = require("file/FileUtils"),
SpecRunnerUtils = require("spec/SpecRunnerUtils");
describe("InlineEditorProviders", function () {
this.category = "integration";
var testPath = SpecRunnerUtils.getTestPath("/spec/InlineEditorProviders-test-files"),
tempPath = SpecRunnerUtils.getTempDirectory(),
testWindow,
infos = {};
function toRange(startLine, endLine) {
return {startLine: startLine, endLine: endLine};
}
function rewriteProject() {
var result = new $.Deferred(),
options = {
parseOffsets : true,
infos : infos,
removePrefix : true
};
SpecRunnerUtils.copyPath(testPath, tempPath, options).done(function () {
result.resolve();
}).fail(function () {
result.reject();
});
return result.promise();
}
/**
* Performs setup for an inline editor test. Parses offsets (saved to infos) for all files in
* the test project (testPath) and saves files back to disk without offset markup.
* When finished, open an editor for the specified project relative file path
* then attempts opens an inline editor at the given offset. Installs an after()
* function restore all file content back to original state with offset markup.
*
* @param {string} openFile Project relative file path to open in a main editor.
* @param {number} openOffset The offset index location within openFile to open an inline editor.
* @param {?boolean} expectInline Use false to verify that an inline editor should not be opened. Omit otherwise.
*/
function initInlineTest(openFile, openOffset, expectInline, workingSet) {
var allFiles,
editor,
hostOpened = false,
err = false;
workingSet = workingSet || [];
expectInline = (expectInline !== undefined) ? expectInline : true;
runs(function () {
workingSet.push(openFile);
waitsForDone(SpecRunnerUtils.openProjectFiles(workingSet), "FILE_OPEN timeout", 1000);
});
runs(function () {
editor = EditorManager.getCurrentFullEditor();
// open inline editor at specified offset index
var inlineEditorResult = SpecRunnerUtils.toggleQuickEditAtOffset(
editor,
infos[openFile].offsets[openOffset]
);
if (expectInline) {
waitsForDone(inlineEditorResult, "inline editor opened", 1000);
} else {
waitsForFail(inlineEditorResult, "inline editor not opened", 1000);
}
});
runs(function () {
if (expectInline) {
var inlineWidgets = editor.getInlineWidgets();
expect(inlineWidgets.length).toBe(1);
// By the time we're called, the content of the widget should be in the DOM and have a nontrivial height.
expect($.contains(testWindow.document.documentElement, inlineWidgets[0].htmlContent)).toBe(true);
expect(inlineWidgets[0].$htmlContent.height()).toBeGreaterThan(50);
}
editor = null;
});
}
// Utilities for testing Editor state
function expectText(editor) {
return expect(editor._codeMirror.getValue());
}
function expectTextToBeEqual(editor1, editor2) {
expect(editor1._codeMirror.getValue()).toBe(editor2._codeMirror.getValue());
}
function isLineHidden(cm, lineNum) {
var markers = cm.findMarksAt({line: lineNum, pos: 0}),
i;
for (i = 0; i < markers.length; i++) {
if (markers[i].collapsed) {
return true;
}
}
return false;
}
/*
* Note that the bulk of selector matching tests are in CSSutils-test.js.
* These tests are primarily focused on the InlineEditorProvider module.
*/
describe("htmlToCSSProvider", function () {
beforeFirst(function () {
SpecRunnerUtils.createTempDirectory();
// rewrite the project for each spec
runs(function () {
waitsForDone(rewriteProject(), "rewriteProject timeout", 1000);
});
// Create a new window that will be shared by ALL tests in this spec.
SpecRunnerUtils.createTestWindowAndRun(this, function (w) {
testWindow = w;
// Load module instances from brackets.test
Commands = testWindow.brackets.test.Commands;
EditorManager = testWindow.brackets.test.EditorManager;
FileSyncManager = testWindow.brackets.test.FileSyncManager;
DocumentManager = testWindow.brackets.test.DocumentManager;
FileViewController = testWindow.brackets.test.FileViewController;
});
});
afterLast(function () {
testWindow = null;
Commands = null;
EditorManager = null;
FileSyncManager = null;
DocumentManager = null;
FileViewController = null;
SpecRunnerUtils.closeTestWindow();
SpecRunnerUtils.removeTempDirectory();
});
beforeEach(function () {
this.addMatchers({
toHaveInlineEditorRange: function (range) {
var i = 0,
editor = this.actual,
hidden,
lineCount = editor.lineCount(),
shouldHide = [],
shouldShow = [],
startLine = range.startLine,
endLine = range.endLine,
visibleRangeCheck;
for (i = 0; i < lineCount; i++) {
hidden = isLineHidden(editor._codeMirror, i);
if (i < startLine) {
if (!hidden) {
shouldHide.push(i); // lines above start line should be hidden
}
} else if ((i >= startLine) && (i <= endLine)) {
if (hidden) {
shouldShow.push(i); // lines in the range should be visible
}
} else if (i > endLine) {
if (!hidden) {
shouldHide.push(i); // lines below end line should be hidden
}
}
}
visibleRangeCheck = (editor._visibleRange.startLine === startLine) &&
(editor._visibleRange.endLine === endLine);
this.message = function () {
var msg = "";
if (shouldHide.length > 0) {
msg += "Expected inline editor to hide [" + shouldHide.toString() + "].\n";
}
if (shouldShow.length > 0) {
msg += "Expected inline editor to show [" + shouldShow.toString() + "].\n";
}
if (!visibleRangeCheck) {
msg += "Editor._visibleRange [" +
editor._visibleRange.startLine + "," +
editor._visibleRange.endLine + "] should be [" +
startLine + "," + endLine + "].";
}
return msg;
};
return (shouldHide.length === 0) &&
(shouldShow.length === 0) &&
visibleRangeCheck;
}
});
SpecRunnerUtils.loadProjectInTestWindow(tempPath);
});
afterEach(function () {
//debug visual confirmation of inline editor
//waits(1000);
// revert files to original content with offset markup
testWindow.closeAllFiles();
});
it("should open a type selector and show correct range including the embedded php", function () {
initInlineTest("test1.php", 1);
runs(function () {
var inlineWidget = EditorManager.getCurrentFullEditor().getInlineWidgets()[0];
var inlinePos = inlineWidget.editor.getCursorPos();
// verify cursor position and displayed range in inline editor
expect(inlinePos).toEqual(infos["test1.php"].offsets[0]);
expect(inlineWidget.editor).toHaveInlineEditorRange(toRange(4, 8));
inlineWidget = null;
});
});
it("should open a type selector on opening tag", function () {
initInlineTest("test1.html", 0);
runs(function () {
var inlineWidget = EditorManager.getCurrentFullEditor().getInlineWidgets()[0];
var inlinePos = inlineWidget.editor.getCursorPos();
// verify cursor position in inline editor
expect(inlinePos).toEqual(infos["test1.css"].offsets[0]);
inlineWidget = null;
});
});
it("should open a type selector on closing tag", function () {
initInlineTest("test1.html", 9);
runs(function () {
var inlineWidget = EditorManager.getCurrentFullEditor().getInlineWidgets()[0];
var inlinePos = inlineWidget.editor.getCursorPos();
// verify cursor position in inline editor
expect(inlinePos).toEqual(infos["test1.css"].offsets[0]);
inlineWidget = null;
});
});
it("should open a class selector", function () {
initInlineTest("test1.html", 1);
runs(function () {
var inlineWidget = EditorManager.getCurrentFullEditor().getInlineWidgets()[0];
var inlinePos = inlineWidget.editor.getCursorPos();
// verify cursor position in inline editor
expect(inlinePos).toEqual(infos["test1.css"].offsets[1]);
inlineWidget = null;
});
});
it("should also open a class selector", function () {
initInlineTest("test1.html", 7);
runs(function () {
var inlineWidget = EditorManager.getCurrentFullEditor().getInlineWidgets()[0];
var inlinePos = inlineWidget.editor.getCursorPos();
// verify cursor position in inline editor
expect(inlinePos).toEqual(infos["test1.css"].offsets[1]);
inlineWidget = null;
});
});
it("should open an embedded class selector", function () {
initInlineTest("test1.html", 10);
runs(function () {
var inlineWidget = EditorManager.getCurrentFullEditor().getInlineWidgets()[0];
var inlinePos = inlineWidget.editor.getCursorPos();
// verify cursor position in inline editor
expect(inlinePos).toEqual(infos["test1.html"].offsets[11]);
inlineWidget = null;
});
});
it("should open an id selector", function () {
initInlineTest("test1.html", 2);
runs(function () {
var inlineWidget = EditorManager.getCurrentFullEditor().getInlineWidgets()[0];
var inlinePos = inlineWidget.editor.getCursorPos();
// verify cursor position in inline editor
expect(inlinePos).toEqual(infos["test1.css"].offsets[2]);
inlineWidget = null;
});
});
it("should close, then remove the inline widget and restore focus", function () {
initInlineTest("test1.html", 0);
var hostEditor, inlineWidget, inlinePos, savedPos;
runs(function () {
hostEditor = EditorManager.getCurrentFullEditor();
savedPos = hostEditor.getCursorPos();
inlineWidget = hostEditor.getInlineWidgets()[0];
inlinePos = inlineWidget.editor.getCursorPos();
// verify cursor position & focus in inline editor
expect(inlinePos).toEqual(infos["test1.css"].offsets[0]);
expect(inlineWidget.hasFocus()).toEqual(true);
expect(hostEditor.hasFocus()).toEqual(false);
// close the editor
waitsForDone(EditorManager.closeInlineWidget(hostEditor, inlineWidget), "closing inline widget");
});
runs(function () {
// verify no inline widgets in list
expect(hostEditor.getInlineWidgets().length).toBe(0);
// verify that the inline widget's content has been removed from the DOM
expect($.contains(testWindow.document.documentElement, inlineWidget.htmlContent)).toBe(false);
// verify full editor cursor & focus restored
expect(savedPos).toEqual(hostEditor.getCursorPos());
expect(hostEditor.hasFocus()).toEqual(true);
hostEditor = inlineWidget = null;
});
});
it("should close inline widget on Esc Key", function () {
initInlineTest("test1.html", 0);
runs(function () {
var hostEditor = EditorManager.getCurrentFullEditor(),
inlineWidget = hostEditor.getInlineWidgets()[0],
inlinePos = inlineWidget.editor.getCursorPos();
// verify inline widget
expect(hostEditor.getInlineWidgets().length).toBe(1);
// close the editor by simulating Esc key
var key = KeyEvent.DOM_VK_ESCAPE,
doc = testWindow.document,
element = doc.getElementsByClassName("inline-widget")[0];
SpecRunnerUtils.simulateKeyEvent(key, "keydown", element);
// verify no inline widgets
expect(hostEditor.getInlineWidgets().length).toBe(0);
doc = hostEditor = inlineWidget = null;
});
});
it("should not open an inline editor when positioned on textContent", function () {
initInlineTest("test1.html", 3, false);
runs(function () {
// verify no inline open
expect(EditorManager.getCurrentFullEditor().getInlineWidgets().length).toBe(0);
});
});
it("should not open an inline editor when positioned on a tag in a comment", function () {
initInlineTest("test1.html", 4, false);
runs(function () {
// verify no inline open
expect(EditorManager.getCurrentFullEditor().getInlineWidgets().length).toBe(0);
});
});
it("should increase size based on content", function () {
initInlineTest("test1.html", 1);
var inlineEditor, widgetHeight;
runs(function () {
inlineEditor = EditorManager.getCurrentFullEditor().getInlineWidgets()[0].editor;
widgetHeight = inlineEditor.totalHeight();
// verify original line count
expect(inlineEditor.lineCount()).toBe(12);
// change inline editor content
var newLines = ".bar {\ncolor: #f00;\n}\n.cat {\ncolor: #f00;\n}";
// insert new lines at current cursor position
// can't mutate document directly at this point
inlineEditor._codeMirror.replaceRange(
newLines,
inlineEditor.getCursorPos()
);
// verify widget resizes when contents is changed
expect(inlineEditor.lineCount()).toBe(17);
expect(inlineEditor.totalHeight()).toBeGreaterThan(widgetHeight);
inlineEditor = null;
});
});
it("should decrease size based on content", function () {
initInlineTest("test1.html", 1);
var inlineEditor, widgetHeight;
runs(function () {
inlineEditor = EditorManager.getCurrentFullEditor().getInlineWidgets()[0].editor;
widgetHeight = inlineEditor.totalHeight();
// verify original line count
expect(inlineEditor.lineCount()).toBe(12);
// replace the entire .foo rule with an empty string
// set text on the editor, can't mutate document directly at this point
inlineEditor._codeMirror.replaceRange(
"",
inlineEditor.getCursorPos(),
infos["test1.css"].offsets[3]
);
// verify widget resizes when contents is changed
expect(inlineEditor.lineCount()).toBe(10);
expect(inlineEditor.totalHeight()).toBeLessThan(widgetHeight);
inlineEditor = null;
});
});
it("should save changes in the inline editor", function () {
initInlineTest("test1.html", 1);
var saved = false,
err = false,
hostEditor,
inlineEditor,
newText = "\n/* jasmine was here */",
savedText;
this.after(function () {
// rewrite the project after saving changes
runs(function () {
waitsForDone(rewriteProject(), "rewriteProject timeout", 1000);
});
});
runs(function () {
hostEditor = EditorManager.getCurrentFullEditor();
inlineEditor = hostEditor.getInlineWidgets()[0].editor;
// insert text at the inline editor's cursor position
// can't mutate document directly at this point
inlineEditor._codeMirror.replaceRange(newText, inlineEditor.getCursorPos());
// we're going to compare this to disk later, so pass true to get non-normalized line endings
newText = inlineEditor.document.getText(true);
// verify isDirty flag
expect(inlineEditor.document.isDirty).toBeTruthy();
expect(hostEditor.document.isDirty).toBeFalsy();
// verify that the dirty dot is visible in the UI
expect(testWindow.$(".dirty-indicator", hostEditor.getInlineWidgets()[0].$htmlContent).width()).not.toEqual(0);
// verify focus is in inline editor
expect(inlineEditor.hasFocus()).toBeTruthy();
// execute file save command
waitsForDone(testWindow.executeCommand(Commands.FILE_SAVE), "save timeout", 1000);
});
runs(function () {
// verify focus is still in inline editor
expect(inlineEditor.hasFocus()).toBeTruthy();
// read saved file contents
FileUtils.readAsText(inlineEditor.document.file).done(function (text) {
savedText = text;
}).fail(function () {
err = true;
});
});
waitsFor(function () { return savedText !== undefined; }, "readAsText timeout", 1000);
runs(function () {
expect(savedText).toEqual(newText);
// verify isDirty flag
expect(inlineEditor.document.isDirty).toBeFalsy();
expect(hostEditor.document.isDirty).toBeFalsy();
// verify that the dirty dot is hidden in the UI
expect(testWindow.$(".dirty-indicator", hostEditor.getInlineWidgets()[0].$htmlContent).width()).toEqual(0);
inlineEditor = hostEditor = null;
});
});
it("should not save changes in the host editor", function () {
initInlineTest("test1.html", 1);
var err = false,
hostEditor,
inlineEditor,
newInlineText = "/* jasmine was inline */\n",
newHostText = "/* jasmine was here */\n",
savedInlineText,
savedHostText;
this.after(function () {
// rewrite the project after saving changes
runs(function () {
waitsForDone(rewriteProject(), "rewriteProject timeout", 1000);
});
});
runs(function () {
hostEditor = EditorManager.getCurrentFullEditor();
inlineEditor = hostEditor.getInlineWidgets()[0].editor;
// insert text at the host editor's cursor position
hostEditor._codeMirror.replaceRange(newHostText, hostEditor.getCursorPos());
// insert text at the inline editor's cursor position
// can't mutate document directly at this point
inlineEditor._codeMirror.replaceRange(newInlineText, inlineEditor.getCursorPos());
// we're going to compare this to disk later, so pass true to get non-normalized line endings
newInlineText = inlineEditor.document.getText(true);
// verify isDirty flag
expect(inlineEditor.document.isDirty).toBeTruthy();
expect(hostEditor.document.isDirty).toBeTruthy();
// verify focus is in inline editor
expect(inlineEditor.hasFocus()).toBeTruthy();
// execute file save command
waitsForDone(testWindow.executeCommand(Commands.FILE_SAVE), "save timeout", 1000);
});
runs(function () {
// read saved inline file contents
FileUtils.readAsText(inlineEditor.document.file).done(function (text) {
savedInlineText = text;
}).fail(function () {
err = true;
});
// read saved host editor file contents
FileUtils.readAsText(hostEditor.document.file).done(function (text) {
savedHostText = text;
}).fail(function () {
err = true;
});
});
waitsFor(function () { return savedInlineText !== undefined && savedHostText !== undefined; }, "readAsText timeout", 1000);
runs(function () {
expect(savedInlineText).toEqual(newInlineText);
expect(savedHostText).toEqual(infos["test1.html"].text); // i.e, should be unchanged
// verify isDirty flag
expect(inlineEditor.document.isDirty).toBeFalsy();
expect(hostEditor.document.isDirty).toBeTruthy();
inlineEditor = hostEditor = null;
});
});
describe("Inline Editor syncing from disk", function () {
it("should close inline editor when file deleted on disk", function () {
// Create an expendable CSS file
var fileToWrite,
savedTempCSSFile = false;
runs(function () {
// Important: must create file using test window's FS so that it sees the new file right away
var promise = SpecRunnerUtils.createTextFile(tempPath + "/tempCSS.css", "#anotherDiv {}", testWindow.brackets.test.FileSystem)
.done(function (entry) {
fileToWrite = entry;
})
.fail(function (err) {
console.log(err);
});
waitsForDone(promise, "writeText tempCSS.css", 1000);
});
// Open inline editor for that file
runs(function () {
initInlineTest("test1.html", 6, true);
});
// initInlineTest() inserts a waitsFor() automatically, so must end runs() block here
runs(function () {
var hostEditor = EditorManager.getCurrentFullEditor();
expect(hostEditor.getInlineWidgets().length).toBe(1);
});
// Delete the file
runs(function () {
waitsForDone(SpecRunnerUtils.deletePath(fileToWrite.fullPath));
});
// Ping FileSyncManager to recognize the deletion
runs(function () {
FileSyncManager.syncOpenDocuments();
});
// Verify inline is now closed
waitsFor(function () {
var hostEditor = EditorManager.getCurrentFullEditor();
return (hostEditor.getInlineWidgets().length === 0);
}, 1000);
// Cleanup: initInlineTest() saves contents of all files in the project and rewrites
// them back to disk at the end (to restore any stripped offset markers). But in this
// case we really want the temp file to stay gone.
runs(function () {
delete infos["tempCSS.css"];
});
});
// FUTURE: Eventually we'll instead want it to stay open and revert to the content on disk.
// Editor's syncing code can't yet handle blowing away the whole Document like that, though.
it("should close inline when file is closed without saving changes", function () {
initInlineTest("test1.html", 1);
var newText = "\n/* jasmine was here */",
hostEditor,
inlineEditor,
promise;
runs(function () {
hostEditor = EditorManager.getCurrentFullEditor();
inlineEditor = hostEditor.getInlineWidgets()[0].editor;
// verify inline is open
expect(hostEditor.getInlineWidgets().length).toBe(1);
// insert text at the inline editor's cursor position
inlineEditor._codeMirror.replaceRange(newText, inlineEditor.getCursorPos());
// verify isDirty flag
expect(inlineEditor.document.isDirty).toBe(true);
// close the main editor / working set entry for the inline's file
promise = testWindow.executeCommand(Commands.FILE_CLOSE, {file: inlineEditor.document.file});
// synchronously click the don't save button,
// asynchronously wait for the dialog to close and the Dialog's
// promise to resolve.
SpecRunnerUtils.clickDialogButton(Dialogs.DIALOG_BTN_DONTSAVE);
});
// Wait on the command's promise also since the command performs
// asynchronous tasks after the Dialog is resolved. If the command
// could complete synchronously, this wait would be unnecessary.
runs(function () {
waitsForDone(promise);
});
runs(function () {
// verify inline is closed
expect(hostEditor.getInlineWidgets().length).toBe(0);
inlineEditor = hostEditor = null;
});
});
});
describe("Bi-directional Editor Synchronizing", function () {
// For these tests we *deliberately* use Editor._codeMirror instead of Document to make edits,
// in order to test Editor->Document syncing (instead of Document->Editor).
it("should not add an inline document to the working set without being edited", function () {
initInlineTest("test1.html", 0);
runs(function () {
var i = DocumentManager.findInWorkingSet(infos["test1.css"].fileEntry.fullPath);
expect(i).toEqual(-1);
});
});
it("should add dirty documents to the working set", function () {
initInlineTest("test1.html", 1);
var inlineEditor, widgetHeight;
runs(function () {
inlineEditor = EditorManager.getCurrentFullEditor().getInlineWidgets()[0].editor;
widgetHeight = inlineEditor.totalHeight();
// change inline editor content
var newLines = ".bar {\ncolor: #f00;\n}\n.cat {\ncolor: #f00;\n}";
// insert new lines at current cursor position
inlineEditor._codeMirror.replaceRange(
newLines,
inlineEditor.getCursorPos()
);
var i = DocumentManager.findInWorkingSet(infos["test1.css"].fileEntry.fullPath);
expect(i).toEqual(1);
inlineEditor = null;
});
});
it("should sync edits between full and inline editors", function () {
var fullEditor,
inlineEditor,
newInlineText = "/* jasmine was inline */\n";
initInlineTest("test1.html", 1, true, ["test1.css"]);
runs(function () {
var cssPath = infos["test1.css"].fileEntry.fullPath;
var cssDoc = DocumentManager.getOpenDocumentForPath(cssPath);
// edit the inline editor
inlineEditor = EditorManager.getCurrentFullEditor().getInlineWidgets()[0].editor;
inlineEditor._codeMirror.replaceRange(
newInlineText,
inlineEditor.getCursorPos()
);
// activate the full editor
DocumentManager.setCurrentDocument(cssDoc);
fullEditor = EditorManager.getCurrentFullEditor();
// sanity check
expect(fullEditor).not.toBe(inlineEditor);
expect(fullEditor._codeMirror).not.toBe(inlineEditor._codeMirror);
// compare inline editor to full editor
expectTextToBeEqual(inlineEditor, fullEditor);
// make sure the text was inserted
expect(fullEditor._codeMirror.getValue().indexOf(newInlineText)).toBeGreaterThan(0);
// edit in the full editor and compare
fullEditor._codeMirror.replaceRange(
newInlineText,
inlineEditor.getCursorPos()
);
expectTextToBeEqual(inlineEditor, fullEditor);
inlineEditor = fullEditor = null;
});
});
});
describe("Inline Editor range updating", function () {
var fullEditor,
hostEditor,
inlineEditor,
newInlineText = "/* insert in full editor */\n",
before,
start,
middle,
middleOfMiddle,
end,
endOfEnd,
after,
wayafter;
beforeEach(function () {
initInlineTest("test1.html", 1, true, ["test1.css"]);
runs(function () {
var cssPath = infos["test1.css"].fileEntry.fullPath;
var cssDoc = DocumentManager.getOpenDocumentForPath(cssPath);
hostEditor = EditorManager.getCurrentFullEditor();
inlineEditor = hostEditor.getInlineWidgets()[0].editor;
// activate the full editor
DocumentManager.setCurrentDocument(cssDoc);
fullEditor = EditorManager.getCurrentFullEditor();
// alias offsets to nice names
before = infos["test1.css"].offsets[4];
start = infos["test1.css"].offsets[1];
middle = infos["test1.css"].offsets[5];
middleOfMiddle = {line: middle.line, ch: 3};
end = infos["test1.css"].offsets[6];
endOfEnd = infos["test1.css"].offsets[3];
after = infos["test1.css"].offsets[7];
wayafter = infos["test1.css"].offsets[2];
});
});
afterEach(function () {
fullEditor = null;
hostEditor = null;
inlineEditor = null;
});
it("should insert new line at start of range, and stay open on undo", function () {
// insert new line at start of inline range--the new line should be included in
// the inline
fullEditor._codeMirror.replaceRange(
newInlineText,
start
);
expect(inlineEditor).toHaveInlineEditorRange(toRange(start.line, end.line + 1));
// undo is equivalent to deleting the first line in the range: shouldn't close the editor
fullEditor._codeMirror.undo();
expect(hostEditor.getInlineWidgets().length).toBe(1);
expect(inlineEditor).toHaveInlineEditorRange(toRange(start.line, end.line));
});
it("should insert new line within first line of range, and stay open on undo", function () {
// insert new line in middle of first line of inline range--the new line should
// be included
fullEditor._codeMirror.replaceRange(
newInlineText,
{line: start.line, ch: start.ch + 1}
);
expect(inlineEditor).toHaveInlineEditorRange(toRange(start.line, end.line + 1));
// Even though the edit didn't start at the beginning of the line, the undo touches
// the whole line; but deleting the first line still shouldn't close the editor
fullEditor._codeMirror.undo();
expect(hostEditor.getInlineWidgets().length).toBe(1);
expect(inlineEditor).toHaveInlineEditorRange(toRange(start.line, end.line));
});
it("should not close inline when undoing changes to first line in range that did not include newlines", function () {
// undo is NOT deleting the entire first line in these cases, so the inline should be able to stay open
// insert new text at start of inline range, without newlines--the new text should be included in
// the inline
fullEditor._codeMirror.replaceRange(
".bar, ",
start
);
expect(inlineEditor).toHaveInlineEditorRange(toRange(start.line, end.line));
fullEditor._codeMirror.undo();
expect(hostEditor.getInlineWidgets().length).toBe(1);
expect(inlineEditor).toHaveInlineEditorRange(toRange(start.line, end.line));
// replace text at start of inline range with text without newlines
fullEditor._codeMirror.replaceRange(
".bar",
{line: start.line, ch: 0},
{line: start.line, ch: 4}
);
expect(inlineEditor).toHaveInlineEditorRange(toRange(start.line, end.line));
fullEditor._codeMirror.undo();
expect(hostEditor.getInlineWidgets().length).toBe(1);
expect(inlineEditor).toHaveInlineEditorRange(toRange(start.line, end.line));
// insert new text in middle of first line in range, without newlines
fullEditor._codeMirror.replaceRange(
"ABCD",
{line: start.line, ch: 3}
);
expect(inlineEditor).toHaveInlineEditorRange(toRange(start.line, end.line));
fullEditor._codeMirror.undo();
expect(hostEditor.getInlineWidgets().length).toBe(1);
expect(inlineEditor).toHaveInlineEditorRange(toRange(start.line, end.line));
});
it("should sync deletions (and their undos) on last line in range, with or without newlines", function () {
// undo is NOT deleting the entire last line in these cases, so the inline should be able to stay open
// insert new line in middle of last line of inline range--the new line should
// be included
fullEditor._codeMirror.replaceRange(
newInlineText,
{line: end.line, ch: end.ch + 1}
);
expect(inlineEditor).toHaveInlineEditorRange(toRange(start.line, end.line + 1));
fullEditor._codeMirror.undo();
expect(hostEditor.getInlineWidgets().length).toBe(1);
expect(inlineEditor).toHaveInlineEditorRange(toRange(start.line, end.line));
// insert new line at end of last line of inline range--the new line should
// be included
fullEditor._codeMirror.replaceRange(
newInlineText,
endOfEnd
);
expect(inlineEditor).toHaveInlineEditorRange(toRange(start.line, end.line + 1));
fullEditor._codeMirror.undo();
expect(hostEditor.getInlineWidgets().length).toBe(1);
expect(inlineEditor).toHaveInlineEditorRange(toRange(start.line, end.line));
// replace all text on last line (without replacing trailing newline)
fullEditor._codeMirror.replaceRange(
"ABCD",
end,
endOfEnd
);
expect(inlineEditor).toHaveInlineEditorRange(toRange(start.line, end.line));
fullEditor._codeMirror.undo();
expect(hostEditor.getInlineWidgets().length).toBe(1);
expect(inlineEditor).toHaveInlineEditorRange(toRange(start.line, end.line));
});
it("should sync insertions (and their undos) in most cases without closing", function () {
// insert line above inline range
fullEditor._codeMirror.replaceRange(
newInlineText,
before
);
expect(inlineEditor).toHaveInlineEditorRange(toRange(start.line + 1, end.line + 1));
fullEditor._codeMirror.undo();
expect(inlineEditor).toHaveInlineEditorRange(toRange(start.line, end.line));
// insert line within inline range
fullEditor._codeMirror.replaceRange(
newInlineText,
middle
);
expect(inlineEditor).toHaveInlineEditorRange(toRange(start.line, end.line + 1));
fullEditor._codeMirror.undo();
expect(inlineEditor).toHaveInlineEditorRange(toRange(start.line, end.line));
// insert line at end of inline range
fullEditor._codeMirror.replaceRange(
newInlineText,
end
);
expect(inlineEditor).toHaveInlineEditorRange(toRange(start.line, end.line + 1));
fullEditor._codeMirror.undo();
expect(inlineEditor).toHaveInlineEditorRange(toRange(start.line, end.line));