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 pathWorkingSetView-test.js
More file actions
408 lines (329 loc) · 17.3 KB
/
WorkingSetView-test.js
File metadata and controls
408 lines (329 loc) · 17.3 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
/*
* Copyright (c) 2014 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, xit, expect, beforeEach, afterEach, waitsFor, waitsForDone, runs, beforeFirst, afterLast, waitsForTime */
define(function (require, exports, module) {
"use strict";
var CommandManager, // Load from brackets.test
Commands, // Load from brackets.test
DocumentManager, // Load from brackets.test
FileViewController, // Load from brackets.test
MainViewManager, // Load from brackets.test
ProjectManager, // Load from brackets.test
WorkingSetView,
SpecRunnerUtils = require("spec/SpecRunnerUtils");
describe("WorkingSetView", function () {
this.category = "integration";
var testPath = SpecRunnerUtils.getTestPath("/spec/WorkingSetView-test-files"),
testWindow,
workingSetListItemCount;
function openAndMakeDirty(path) {
var doc, didOpen = false, gotError = false;
// open file
runs(function () {
FileViewController.openAndSelectDocument(path, FileViewController.PROJECT_MANAGER)
.done(function () { didOpen = true; })
.fail(function () { gotError = true; });
});
waitsFor(function () { return didOpen && !gotError; }, "FILE_OPEN on file timeout", 1000);
// change editor content to make doc dirty which adds it to the working set
runs(function () {
doc = DocumentManager.getCurrentDocument();
doc.setText("dirty document");
});
}
function createTestWindow(spec, loadProject) {
SpecRunnerUtils.createTestWindowAndRun(spec, function (w) {
testWindow = w;
// Load module instances from brackets.test
CommandManager = testWindow.brackets.test.CommandManager;
Commands = testWindow.brackets.test.Commands;
DocumentManager = testWindow.brackets.test.DocumentManager;
FileViewController = testWindow.brackets.test.FileViewController;
MainViewManager = testWindow.brackets.test.MainViewManager;
WorkingSetView = testWindow.brackets.test.WorkingSetView;
ProjectManager = testWindow.brackets.test.ProjectManager;
// Open a directory
if (loadProject) {
SpecRunnerUtils.loadProjectInTestWindow(testPath);
}
});
runs(function () {
// Initialize: register listeners
MainViewManager.on("workingSetAdd", function (event, addedFile) {
workingSetListItemCount++;
});
});
}
function closeTestWindow() {
testWindow = null;
CommandManager = null;
Commands = null;
DocumentManager = null;
FileViewController = null;
MainViewManager = null;
SpecRunnerUtils.closeTestWindow();
}
beforeFirst(function () {
createTestWindow(this, true);
});
afterLast(closeTestWindow);
beforeEach(function () {
workingSetListItemCount = 0;
openAndMakeDirty(testPath + "/file_one.js");
openAndMakeDirty(testPath + "/file_two.js");
// Wait for both files to be added to the working set
waitsFor(function () { return workingSetListItemCount === 2; }, "workingSetListItemCount to equal 2", 1000);
});
afterEach(function () {
testWindow.closeAllFiles();
});
it("should add a list item when a file is dirtied", function () {
// check if files are added to work set and dirty icons are present
runs(function () {
var $listItems = testWindow.$(".open-files-container > ul").children();
expect($listItems.length).toBe(2);
expect($listItems.find("a").get(0).text === "file_one.js").toBeTruthy();
expect($listItems.find(".file-status-icon").length).toBe(2);
});
});
it("should remove a list item when a file is closed", function () {
DocumentManager.getCurrentDocument()._markClean(); // so we can close without a save dialog
// close the document
var didClose = false, gotError = false;
runs(function () {
CommandManager.execute(Commands.FILE_CLOSE)
.done(function () { didClose = true; })
.fail(function () { gotError = true; });
});
waitsFor(function () { return didClose && !gotError; }, "FILE_OPEN on file timeout", 1000);
// check there are no list items
runs(function () {
var listItems = testWindow.$(".open-files-container > ul").children();
expect(listItems.length).toBe(1);
});
});
it("should make a file that is clicked the current one in the editor", function () {
runs(function () {
var $ = testWindow.$;
var secondItem = $($(".open-files-container > ul").children()[1]);
secondItem.trigger("click");
var $listItems = $(".open-files-container > ul").children();
expect($($listItems[0]).hasClass("selected")).not.toBeTruthy();
expect($($listItems[1]).hasClass("selected")).toBeTruthy();
});
});
xit("should rebuild the ui from the model correctly", function () {
// force the test window to initialize to unit test preferences
// for just this test
runs(function () {
localStorage.setItem("doLoadPreferences", true);
});
// remove temporary unit test preferences with a single-spec after()
this.after(function () {
localStorage.removeItem("doLoadPreferences");
});
// close test window while working set has 2 files (see beforeEach())
closeTestWindow();
// reopen brackets test window to initialize unit test working set
createTestWindow(this, false);
var $listItems;
// wait for working set to populate
waitsFor(
function () {
// check working set UI list content
$listItems = testWindow.$(".open-files-container > ul").children();
return ($listItems.length === 2) && $($listItems[1]).hasClass("selected");
},
1000
);
// files should be in the working set
runs(function () {
expect($listItems.find("a").get(0).text === "file_one.js").toBeTruthy();
expect($listItems.find("a").get(1).text === "file_two.js").toBeTruthy();
// files should be clean
expect($listItems.find(".file-status-icon dirty").length).toBe(0);
// file_two.js should be active
expect($($listItems[1]).hasClass("selected")).toBeTruthy();
});
});
it("should close a file when the user clicks the close button", function () {
var $ = testWindow.$;
var didClose = false;
// make 2nd doc clean
var fileList = MainViewManager.getWorkingSet(MainViewManager.ACTIVE_PANE);
runs(function () {
var doc0 = DocumentManager.getOpenDocumentForPath(fileList[0].fullPath);
var doc1 = DocumentManager.getOpenDocumentForPath(fileList[1].fullPath);
doc1._markClean();
// make the first one active
MainViewManager._edit(MainViewManager.ACTIVE_PANE, doc0);
// hover over and click on close icon of 2nd list item
var secondItem = $($(".open-files-container > ul").children()[1]);
secondItem.trigger("mouseover");
var closeIcon = secondItem.find(".file-status-icon");
expect(closeIcon.length).toBe(1);
// simulate click
MainViewManager.on("workingSetRemove", function (event, removedFile) {
didClose = true;
});
closeIcon.trigger("mousedown");
});
waitsFor(function () { return didClose; }, "click on working set close icon timeout", 1000);
runs(function () {
var $listItems = $(".open-files-container > ul").children();
expect($listItems.length).toBe(1);
expect($listItems.find("a").get(0).text === "file_one.js").toBeTruthy();
});
});
it("should remove dirty icon when file becomes clean", function () {
runs(function () {
// check that dirty icon is removed when docs are cleaned
var fileList = MainViewManager.getWorkingSet(MainViewManager.ACTIVE_PANE);
var doc0 = DocumentManager.getOpenDocumentForPath(fileList[0].fullPath);
doc0._markClean();
var listItems = testWindow.$(".open-files-container > ul").children();
expect(listItems.find(".file-status-icon dirty").length).toBe(0);
});
});
it("should show the file in project tree when a file is being renamed", function () {
var $ = testWindow.$;
var secondItem = $(".open-files-container > ul").children().eq(1);
var fileName = secondItem.text();
runs(function () {
secondItem.trigger("click");
// Calling FILE_RENAME synchronously works fine here since the item is already visible in project file tree.
// However, if the selected item is not already visible in the tree, this command will complete asynchronously.
// In that case, waitsFor will be needed before continuing with the rest of the test.
CommandManager.execute(Commands.FILE_RENAME);
});
waitsForTime(ProjectManager._RENDER_DEBOUNCE_TIME);
runs(function () {
expect($("#project-files-container ul input").val()).toBe(fileName);
});
});
it("should show a directory name next to the file name when two files with same names are opened", function () {
runs(function () {
// Count currently opened files
var workingSetListItemCountBeforeTest = workingSetListItemCount;
// First we need to open another file
openAndMakeDirty(testPath + "/directory/file_one.js");
// Wait for file to be added to the working set
waitsFor(function () { return workingSetListItemCount === workingSetListItemCountBeforeTest + 1; }, 1000);
runs(function () {
// Two files with the same name file_one.js should be now opened
var $list = testWindow.$(".open-files-container > ul");
expect($list.find(".directory").length).toBe(2);
// Now close last opened file to hide the directories again
DocumentManager.getCurrentDocument()._markClean(); // so we can close without a save dialog
waitsForDone(CommandManager.execute(Commands.FILE_CLOSE), "timeout on FILE_CLOSE", 1000);
// there should be no more directories shown
runs(function () {
expect($list.find(".directory").length).toBe(0);
});
});
});
});
it("should show different directory names, when two files of the same name are opened, located in folders with same name", function () {
runs(function () {
// Count currently opened files
var workingSetListItemCountBeforeTest = workingSetListItemCount;
// Open both files
openAndMakeDirty(testPath + "/directory/file_one.js");
openAndMakeDirty(testPath + "/directory/directory/file_one.js");
// Wait for them to load
waitsFor(function () { return workingSetListItemCount === workingSetListItemCountBeforeTest + 2; }, "Open file count to be increased by 2", 1000);
runs(function () {
// Collect all directory names displayed
var $list = testWindow.$(".open-files-container > ul");
var names = $list.find(".directory").map(function () {
return $(this).text();
}).toArray();
// All directory names should be unique
var uniq = 0, map = {};
names.forEach(function (name) {
if (!map[name]) {
map[name] = true;
uniq++;
}
});
expect(uniq).toBe(names.length);
});
});
});
it("should callback for icons", function () {
runs(function () {
function iconProvider(file) {
return "<img src='" + file.name + ".jpg' class='icon' />";
}
WorkingSetView.addIconProvider(iconProvider);
runs(function () {
// Collect all icon filenames used
var $list = testWindow.$(".open-files-container > ul");
var icons = $list.find(".icon").map(function () {
return $(this).attr("src");
}).toArray();
// All directory names should be unique
expect(icons.length).toBe(2);
expect(icons[0]).toBe("file_one.js.jpg");
expect(icons[1]).toBe("file_two.js.jpg");
});
});
});
it("should callback for class", function () {
runs(function () {
var master = ["one", "two"],
classes = master.slice(0);
function classProvider(file) {
return classes.pop();
}
WorkingSetView.addClassProvider(classProvider);
runs(function () {
var $list = testWindow.$(".open-files-container > li"),
test = master.slice(0);
$list.each(function (number, el) {
expect($(el).hasClass(test.pop())).toBeTruthy();
});
});
});
});
it("should allow refresh to be used to update the class list", function () {
runs(function () {
function classProvider(file) {
return "one";
}
WorkingSetView.addClassProvider(classProvider);
var master = ["three", "four"];
WorkingSetView.refresh();
runs(function () {
var $list = testWindow.$(".open-files-container > li"),
test = master.slice(0);
$list.each(function (number, el) {
expect($(el).hasClass(test.pop())).toBeTruthy();
expect($(el).hasClass("one")).toBeFalsy();
});
});
});
});
});
});