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 pathEditorOptionHandlers-test.js
More file actions
462 lines (367 loc) · 19.7 KB
/
EditorOptionHandlers-test.js
File metadata and controls
462 lines (367 loc) · 19.7 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
/*
* Copyright (c) 2013 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, beforeEach, afterEach, it, runs, waitsFor, expect, brackets, waitsForDone, beforeFirst, afterLast */
define(function (require, exports, module) {
"use strict";
// Load dependent modules
var CommandManager, // loaded from brackets.test
Commands, // loaded from brackets.test
EditorManager, // loaded from brackets.test
DocumentManager, // loaded from brackets.test
FileViewController,
SpecRunnerUtils = require("spec/SpecRunnerUtils");
describe("EditorOptionHandlers", function () {
this.category = "integration";
var testPath = SpecRunnerUtils.getTestPath("/spec/EditorOptionHandlers-test-files"),
testWindow;
var CSS_FILE = testPath + "/test.css",
HTML_FILE = testPath + "/test.html",
JS_FILE = testPath + "/test.js";
var OPEN_BRACKET = 91,
CLOSE_BRACKET = 93,
SINGLE_QUOTE = 39,
BACKSPACE = 8;
beforeFirst(function () {
// 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
CommandManager = testWindow.brackets.test.CommandManager;
Commands = testWindow.brackets.test.Commands;
EditorManager = testWindow.brackets.test.EditorManager;
DocumentManager = testWindow.brackets.test.DocumentManager;
FileViewController = testWindow.brackets.test.FileViewController;
SpecRunnerUtils.loadProjectInTestWindow(testPath);
});
});
afterLast(function () {
testWindow = null;
CommandManager = null;
Commands = null;
EditorManager = null;
DocumentManager = null;
FileViewController = null;
SpecRunnerUtils.closeTestWindow();
});
afterEach(function () {
testWindow.closeAllFiles();
});
function checkLineWrapping(editor, firstPos, secondPos, shouldWrap) {
runs(function () {
var firstLineBottom, nextLineBottom;
expect(editor).toBeTruthy();
editor.setCursorPos(firstPos);
firstLineBottom = editor._codeMirror.cursorCoords(null, "local").bottom;
editor.setCursorPos(secondPos);
nextLineBottom = editor._codeMirror.cursorCoords(null, "local").bottom;
if (shouldWrap) {
expect(firstLineBottom).toBeLessThan(nextLineBottom);
} else {
expect(firstLineBottom).toEqual(nextLineBottom);
}
});
}
function checkActiveLine(editor, line, shouldShow) {
runs(function () {
var lineInfo;
expect(editor).toBeTruthy();
editor.setCursorPos({line: line, ch: 0});
lineInfo = editor._codeMirror.lineInfo(line);
if (shouldShow) {
expect(lineInfo.wrapClass).toBe("CodeMirror-activeline");
} else {
expect(lineInfo.wrapClass).toBeUndefined();
}
});
}
function checkActiveLineOption(editor, shouldBe) {
runs(function () {
expect(editor).toBeTruthy();
expect(editor._codeMirror.getOption("styleActiveLine")).toBe(shouldBe);
});
}
function checkLineNumbers(editor, shouldShow) {
runs(function () {
var gutterElement, $lineNumbers;
expect(editor).toBeTruthy();
gutterElement = editor._codeMirror.getGutterElement();
$lineNumbers = $(gutterElement).find(".CodeMirror-linenumbers");
if (shouldShow) {
expect($lineNumbers.length).toNotBe(0);
} else {
expect($lineNumbers.length).toBe(0);
}
});
}
function checkCloseBraces(editor, startSel, endSel, keyCode, expectedText) {
runs(function () {
var input, line;
expect(editor).toBeTruthy();
input = editor._codeMirror.getInputField();
if (endSel) {
editor.setSelection(startSel, endSel);
} else {
editor.setCursorPos(startSel);
}
SpecRunnerUtils.simulateKeyEvent(keyCode, keyCode === BACKSPACE ? "keydown" : "keypress", input);
line = editor._codeMirror.getLine(startSel.line);
expect(line).toBe(expectedText);
});
}
// Helper functions to open editors / toggle options
function openEditor(fullPath) {
runs(function () {
var promise = CommandManager.execute(Commands.FILE_ADD_TO_WORKING_SET, {fullPath: fullPath});
waitsForDone(promise, "Open into working set");
});
}
function openAnotherEditor(fullpath) {
runs(function () {
// Open another document and bring it to the front
waitsForDone(FileViewController.openAndSelectDocument(fullpath, FileViewController.PROJECT_MANAGER),
"FILE_OPEN on file timeout", 1000);
});
}
function openInlineEditor(toggleEditorAt) {
toggleEditorAt = toggleEditorAt || {line: 8, ch: 11};
openEditor(HTML_FILE);
runs(function () {
// Open inline editor onto test.css's ".testClass" rule
var promise = SpecRunnerUtils.toggleQuickEditAtOffset(EditorManager.getCurrentFullEditor(), toggleEditorAt);
waitsForDone(promise, "Open inline editor");
});
}
function toggleOption(commandID, text) {
runs(function () {
var promise = CommandManager.execute(commandID);
waitsForDone(promise, text);
});
}
describe("Toggle Word Wrap", function () {
it("should wrap long lines in main editor by default", function () {
openEditor(HTML_FILE);
runs(function () {
var editor = EditorManager.getCurrentFullEditor();
// Use two cursor positions to detect line wrapping. First position at
// the beginning of a long line and the second position to be
// somewhere on the long line that will be part of an extra line
// created by word-wrap and get its bottom coordinate.
checkLineWrapping(editor, {line: 8, ch: 0}, {line: 8, ch: 210}, true);
});
});
it("should also wrap long lines in inline editor by default", function () {
openInlineEditor();
runs(function () {
var editor = EditorManager.getCurrentFullEditor().getInlineWidgets()[0].editor;
checkLineWrapping(editor, {line: 0, ch: 0}, {line: 0, ch: 320}, true);
});
});
it("should NOT wrap the long lines after turning off word-wrap", function () {
// Turn off word-wrap
toggleOption(Commands.TOGGLE_WORD_WRAP, "Toggle word-wrap");
openEditor(CSS_FILE);
runs(function () {
var editor = EditorManager.getCurrentFullEditor();
checkLineWrapping(editor, {line: 0, ch: 1}, {line: 0, ch: 180}, false);
});
});
it("should NOT wrap the long lines in another document when word-wrap off", function () {
openEditor(CSS_FILE);
openAnotherEditor(HTML_FILE);
runs(function () {
var editor = EditorManager.getCurrentFullEditor();
checkLineWrapping(editor, {line: 8, ch: 0}, {line: 8, ch: 210}, false);
});
});
});
describe("Toggle Active Line", function () {
it("should NOT show active line in main editor by default", function () {
openEditor(HTML_FILE);
runs(function () {
var editor = EditorManager.getCurrentFullEditor();
checkActiveLine(editor, 5, false);
});
});
it("should NOT show active line in inline editor by default", function () {
openInlineEditor();
runs(function () {
var editor = EditorManager.getCurrentFullEditor().getInlineWidgets()[0].editor;
checkActiveLine(editor, 0, false);
});
});
it("should style active line after turning it on", function () {
// Turn on show active line
toggleOption(Commands.TOGGLE_ACTIVE_LINE, "Toggle active line");
openEditor(CSS_FILE);
runs(function () {
var editor = EditorManager.getCurrentFullEditor();
checkActiveLine(editor, 0, true);
});
});
it("should have the active line option be FALSE when the editor has a selection", function () {
openEditor(CSS_FILE);
runs(function () {
var editor = EditorManager.getCurrentFullEditor();
editor.setSelection({line: 0, ch: 0}, {line: 0, ch: 1});
checkActiveLineOption(editor, false);
});
});
it("should style the active line when opening another document with show active line on", function () {
openEditor(CSS_FILE);
openAnotherEditor(HTML_FILE);
runs(function () {
var editor = EditorManager.getCurrentFullEditor();
checkActiveLine(editor, 3, true);
});
});
});
describe("Toggle Line Numbers", function () {
it("should show line numbers in main editor by default", function () {
openEditor(HTML_FILE);
runs(function () {
var editor = EditorManager.getCurrentFullEditor();
checkLineNumbers(editor, true);
});
});
it("should also show line numbers in inline editor by default", function () {
openInlineEditor();
runs(function () {
var editor = EditorManager.getCurrentFullEditor().getInlineWidgets()[0].editor;
checkLineNumbers(editor, true);
});
});
it("should NOT show line numbers in main editor after turning it off", function () {
// Turn off show line numbers
toggleOption(Commands.TOGGLE_LINE_NUMBERS, "Toggle line numbers");
openEditor(CSS_FILE);
runs(function () {
var editor = EditorManager.getCurrentFullEditor();
checkLineNumbers(editor, false);
});
});
it("should NOT show line numbers in inline editor after turning it off", function () {
openInlineEditor();
runs(function () {
var editor = EditorManager.getCurrentFullEditor().getInlineWidgets()[0].editor;
checkLineNumbers(editor, false);
});
});
it("should NOT show line numbers when opening another document with show line numbers off", function () {
openEditor(CSS_FILE);
openAnotherEditor(HTML_FILE);
runs(function () {
var editor = EditorManager.getCurrentFullEditor();
checkLineNumbers(editor, false);
});
});
});
describe("Toggle Auto Close Braces", function () {
it("should NOT auto close braces in main editor by default", function () {
openEditor(JS_FILE);
runs(function () {
var editor = EditorManager.getCurrentFullEditor();
checkCloseBraces(editor, {line: 0, ch: 35}, null, OPEN_BRACKET, "var myContent = \"This is awesome!\";");
});
});
it("should NOT auto close braces in inline editor by default", function () {
openInlineEditor({line: 9, ch: 11});
runs(function () {
var editor = EditorManager.getCurrentFullEditor().getInlineWidgets()[0].editor;
checkCloseBraces(editor, {line: 1, ch: 15}, null, OPEN_BRACKET, ".shortLineClass { color: red; }");
});
});
it("should auto close braces in the main editor after turning it on", function () {
// Turn on auto close braces
toggleOption(Commands.TOGGLE_CLOSE_BRACKETS, "Toggle auto close braces");
openEditor(JS_FILE);
runs(function () {
var editor = EditorManager.getCurrentFullEditor();
checkCloseBraces(editor, {line: 0, ch: 35}, null, OPEN_BRACKET, "var myContent = \"This is awesome!\";[]");
});
});
it("should auto close braces in inline editor after turning it on", function () {
openInlineEditor({line: 9, ch: 11});
runs(function () {
var editor = EditorManager.getCurrentFullEditor().getInlineWidgets()[0].editor;
checkCloseBraces(editor, {line: 1, ch: 32}, null, OPEN_BRACKET, ".shortLineClass { color: red; }[]");
});
});
it("should auto close braces when opening another document with auto close braces on", function () {
openEditor(CSS_FILE);
openAnotherEditor(JS_FILE);
runs(function () {
var editor = EditorManager.getCurrentFullEditor();
checkCloseBraces(editor, {line: 0, ch: 35}, null, OPEN_BRACKET, "var myContent = \"This is awesome!\";[]");
});
});
it("should only auto close braces before spaces, closing braces or end of lines", function () {
openEditor(JS_FILE);
runs(function () {
var editor = EditorManager.getCurrentFullEditor();
checkCloseBraces(editor, {line: 0, ch: 0}, null, OPEN_BRACKET, "var myContent = \"This is awesome!\";");
checkCloseBraces(editor, {line: 0, ch: 15}, null, OPEN_BRACKET, "var myContent =[] \"This is awesome!\";");
checkCloseBraces(editor, {line: 0, ch: 16}, null, OPEN_BRACKET, "var myContent =[[]] \"This is awesome!\";");
checkCloseBraces(editor, {line: 0, ch: 39}, null, OPEN_BRACKET, "var myContent =[[]] \"This is awesome!\";[]");
});
});
it("should overwrite a close brace when writing a close brace before the same close brace", function () {
openEditor(JS_FILE);
runs(function () {
var editor = EditorManager.getCurrentFullEditor();
checkCloseBraces(editor, {line: 0, ch: 15}, null, OPEN_BRACKET, "var myContent =[] \"This is awesome!\";");
checkCloseBraces(editor, {line: 0, ch: 16}, null, CLOSE_BRACKET, "var myContent =[] \"This is awesome!\";");
runs(function () {
expect(editor.getCursorPos()).toEqual({line: 0, ch: 17});
});
});
});
it("should wrap a selection between braces", function () {
openEditor(JS_FILE);
runs(function () {
var editor = EditorManager.getCurrentFullEditor();
checkCloseBraces(editor, {line: 0, ch: 16}, {line: 0, ch: 34}, OPEN_BRACKET, "var myContent = [\"This is awesome!\"];");
runs(function () {
expect(editor.getSelection()).toEqual({start: {line: 0, ch: 16}, end: {line: 0, ch: 36}, reversed: false});
});
});
});
it("should delete both open and close braces when both are together and backspacing", function () {
openEditor(JS_FILE);
runs(function () {
var editor = EditorManager.getCurrentFullEditor();
checkCloseBraces(editor, {line: 0, ch: 15}, null, OPEN_BRACKET, "var myContent =[] \"This is awesome!\";");
checkCloseBraces(editor, {line: 0, ch: 16}, null, BACKSPACE, "var myContent = \"This is awesome!\";");
});
});
it("should NOT auto close single quotes inside comments", function () {
openEditor(JS_FILE);
runs(function () {
var editor = EditorManager.getCurrentFullEditor();
checkCloseBraces(editor, {line: 0, ch: 15}, null, SINGLE_QUOTE, "var myContent ='' \"This is awesome!\";");
checkCloseBraces(editor, {line: 1, ch: 7}, null, SINGLE_QUOTE, "// Yes, it is!");
});
});
});
});
});