-
-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathExtn-NavigationAndHistory-integ-test.js
More file actions
374 lines (328 loc) · 17.4 KB
/
Extn-NavigationAndHistory-integ-test.js
File metadata and controls
374 lines (328 loc) · 17.4 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
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
* Original work Copyright (c) 2012 - 2021 Adobe Systems Incorporated. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
/*global describe, it, expect, beforeAll, afterAll, awaitsForDone, beforeEach, awaits, awaitsFor, path */
define(function (require, exports, module) {
// Recommended to avoid reloading the integration test window Phoenix instance for each test.
const SpecRunnerUtils = require("spec/SpecRunnerUtils");
describe("LegacyInteg:FileRecovery integration tests", function () {
const testPathOriginal = SpecRunnerUtils.getTestPath("/spec/ProjectManager-test-files");
const testPath = SpecRunnerUtils.getTestRoot() + "/navigationTests/";
const tempRestorePath = SpecRunnerUtils.getTestRoot() + "/navigationTestsRestore/";
let FileViewController, // loaded from brackets.test
CommandManager,
Commands,
testWindow,
EditorManager,
MainViewManager,
brackets,
FileSystem,
$;
async function deletePath(pathToDel) {
if(!pathToDel.startsWith("/")) {
pathToDel = testPath + pathToDel;
}
let promise = SpecRunnerUtils.deletePath(pathToDel, true);
await awaitsForDone(promise, "Remove " + pathToDel, 5000);
}
async function loadTestWindow(force) {
testWindow = await SpecRunnerUtils.createTestWindowAndRun({forceReload: force});
brackets = testWindow.brackets;
$ = testWindow.$;
FileViewController = brackets.test.FileViewController;
CommandManager = brackets.test.CommandManager;
Commands = brackets.test.Commands;
EditorManager = brackets.test.EditorManager;
MainViewManager = brackets.test.MainViewManager;
FileSystem = brackets.test.FileSystem;
await awaitsForDone(SpecRunnerUtils.copyPath(testPathOriginal, testPath), "copy temp files");
await SpecRunnerUtils.loadProjectInTestWindow(testPath);
}
beforeEach(async function () {
await closeSession();
await deletePath(testPath);
await deletePath(tempRestorePath);
await loadTestWindow(true);
}, 30000);
afterAll(async function () {
FileViewController = null;
testWindow = null;
brackets = null;
await deletePath(testPath);
await deletePath(tempRestorePath);
await SpecRunnerUtils.closeTestWindow(true);
}, 30000);
async function closeSession() {
if(!CommandManager){
return;
}
await awaitsForDone(CommandManager.execute(Commands.FILE_CLOSE_ALL, { _forceClose: true }),
"closing all file");
}
async function openFile(relativePath) {
await awaitsForDone(
FileViewController.openAndSelectDocument(
testPath + relativePath,
FileViewController.PROJECT_MANAGER
));
}
function isFileOpen(relativePath) {
const fullPath = testPath + relativePath;
let allOpenFiles = MainViewManager.getAllOpenFiles();
for(let file of allOpenFiles){
if(file.fullPath === fullPath){
return true;
}
}
return false;
}
async function initFileRestorer(fileToOpen, scanInterval = 100) {
await deletePath(tempRestorePath);
await SpecRunnerUtils.waitTillPathNotExists(tempRestorePath);
await openFile(fileToOpen);
expect(isFileOpen(fileToOpen)).toBeTrue();
expect(testWindow._FileRecoveryExtensionForTests).exists;
expect(await SpecRunnerUtils.pathExists(tempRestorePath, true)).toBeFalse();
testWindow._FileRecoveryExtensionForTests.initWith(scanInterval,
FileSystem.getDirectoryForPath(tempRestorePath));
await SpecRunnerUtils.waitTillPathExists(tempRestorePath);
}
// multiple , in file tested as we have , for `size,"content"` integrity checks
const unsavedTextFragment = "hello,this is ,a test";
it("Should create restore folders and backup files", async function () {
await initFileRestorer("file.js");
let projectRestorePath = testWindow._FileRecoveryExtensionForTests.getProjectRestoreRoot(testPath);
// now edit a file so that its backup is created
let editor = EditorManager.getActiveEditor();
editor.document.setText(unsavedTextFragment);
await SpecRunnerUtils.waitTillPathExists(projectRestorePath.fullPath, true);
await SpecRunnerUtils.waitTillPathExists(projectRestorePath.fullPath + "file.js", false);
await closeSession();
});
it("Should saving files remove file restore folder", async function () {
await initFileRestorer("toDelete1/file.js");
let projectRestorePath = testWindow._FileRecoveryExtensionForTests.getProjectRestoreRoot(testPath);
// now edit a file so that its backup is created
let editor = EditorManager.getActiveEditor();
editor.document.setText(unsavedTextFragment);
await SpecRunnerUtils.waitTillPathExists(projectRestorePath.fullPath + "toDelete1/file.js", false);
await awaitsForDone(CommandManager.execute(Commands.FILE_SAVE_ALL), "saving all file");
await SpecRunnerUtils.waitTillPathNotExists(projectRestorePath.fullPath + "toDelete1/file.js", false);
await closeSession();
});
it("Should show restore notification and restore if there is anything to restore", async function () {
await initFileRestorer("toDelete1/file.js");
let projectRestorePath = testWindow._FileRecoveryExtensionForTests.getProjectRestoreRoot(testPath);
// now edit a file so that its backup is created
const unsavedText = unsavedTextFragment + Math.random();
let editor = EditorManager.getActiveEditor();
editor.document.setText(unsavedText);
await SpecRunnerUtils.waitTillPathExists(projectRestorePath.fullPath + "toDelete1/file.js", false);
// backup is now present, reload the project
testWindow.location.href = "about:blank";
await awaits(1000);
await SpecRunnerUtils.closeTestWindow(true, true);
await loadTestWindow(true);
testWindow._FileRecoveryExtensionForTests.initWith(100,
FileSystem.getDirectoryForPath(tempRestorePath));
await awaitsFor(()=>{
return $(".file-recovery-button").length === 1;
}, "waiting for restore notification", 5000);
// now press the recover button to start the recovery
$(".file-recovery-button").click();
// check if the file is recovered
await awaitsFor(()=>{
editor = EditorManager.getActiveEditor();
return editor && editor.document.getText() === unsavedText;
}, "waiting for restore notification", 5000);
await closeSession();
}, 30000);
it("Should show restore notification and discard if discard button clicked", async function () {
const fileNameToRestore = "toDelete1/file.js";
await initFileRestorer(fileNameToRestore);
let projectRestorePath = testWindow._FileRecoveryExtensionForTests.getProjectRestoreRoot(testPath);
// now edit a file so that its backup is created
const unsavedText = unsavedTextFragment + Math.random();
let editor = EditorManager.getActiveEditor();
editor.document.setText(unsavedText);
await SpecRunnerUtils.waitTillPathExists(projectRestorePath.fullPath + fileNameToRestore, false);
// backup is now present, reload the project
testWindow.location.href = "about:blank";
await awaits(1000);
await SpecRunnerUtils.closeTestWindow(true, true);
await loadTestWindow(true);
testWindow._FileRecoveryExtensionForTests.initWith(100,
FileSystem.getDirectoryForPath(tempRestorePath));
await awaitsFor(()=>{
return $(".file-recovery-button").length === 1;
}, "waiting for restore notification", 5000);
// now press the discard button to discard the recovery
$("#DISCARD_UNSAVED_FILES_RESTORE").click();
await SpecRunnerUtils.waitTillPathNotExists(projectRestorePath.fullPath + fileNameToRestore, false);
await openFile(fileNameToRestore);
editor = EditorManager.getActiveEditor();
expect(!!editor.isDirty).toBeFalse();
await closeSession();
}, 30000);
// below project switch test case is flakey. need to fix. disable for now.
// it("Should show restore on project switch", async function () {
// const readOnlyProject = SpecRunnerUtils.getTestPath("/spec/ProjectManager-test-files");
// await initFileRestorer("toDelete1/file.js", 1000);
// let projectRestorePath = testWindow._FileRecoveryExtensionForTests.getProjectRestoreRoot(testPath);
//
// // now edit a file so that its backup is created
// const unsavedText = "hello" + Math.random();
// let editor = EditorManager.getActiveEditor();
// editor.document.setText(unsavedText);
// await SpecRunnerUtils.waitTillPathExists(projectRestorePath.fullPath + "toDelete1/file.js", false);
//
// // backup is now present, switch to another project
// let loadPromise = SpecRunnerUtils.loadProjectInTestWindow(readOnlyProject);
// await awaitsFor(()=>{
// return $('button[data-button-id=dontsave]').length >= 1;
// }, "waiting for save changes dialogue", 5000);
// $('button[data-button-id=dontsave]')[0].click();
// await loadPromise;
// await SpecRunnerUtils.loadProjectInTestWindow(testPath);
// await awaits(3000);
// await awaitsFor(()=>{
// return $(".file-recovery-button").length === 1;
// }, "waiting for restore notification", 5000);
//
// // now press the recover button to start the recovery
// $(".file-recovery-button").click();
// // check if the file is recovered
// await awaitsFor(()=>{
// editor = EditorManager.getActiveEditor();
// return editor && editor.document.getText() === unsavedText;
// }, "waiting for restore notification", 5000);
// await closeSession();
// }, 1000000);
});
describe("integration:Navigation back/forward integration tests", function () {
const testProjectPath = SpecRunnerUtils.getTestPath("/spec/quickview-extn-test-files");
const NAVIGATION_JUMP_BACK = "navigation.jump.back",
NAVIGATION_JUMP_FWD = "navigation.jump.fwd",
_NAVIGATION_RESET_FOR_TESTS = "navigation.jump.reset.tests";
let FileViewController, // loaded from brackets.test
CommandManager,
Commands,
testWindow,
MainViewManager,
brackets,
$;
beforeAll(async function () {
testWindow = await SpecRunnerUtils.createTestWindowAndRun();
brackets = testWindow.brackets;
$ = testWindow.$;
FileViewController = brackets.test.FileViewController;
CommandManager = brackets.test.CommandManager;
Commands = brackets.test.Commands;
MainViewManager = brackets.test.MainViewManager;
}, 30000);
beforeEach(async ()=>{
await awaitsForDone(CommandManager.execute(Commands.FILE_CLOSE_ALL, { _forceClose: true }),
"closing all file");
await SpecRunnerUtils.closeTestWindow();
await SpecRunnerUtils.loadProjectInTestWindow(testProjectPath);
});
afterAll(async function () {
FileViewController = null;
testWindow = null;
brackets = null;
$ = null;
MainViewManager = null;
Commands = null;
CommandManager = null;
await SpecRunnerUtils.closeTestWindow();
}, 30000);
async function openFile(relativePath) {
await awaitsForDone(
FileViewController.openAndSelectDocument(
path.join(testProjectPath, relativePath),
FileViewController.PROJECT_MANAGER
));
}
async function _expectNavButton(backDisabled, fwdDisabled, message) {
await awaitsFor(()=>{
const navBackDisabledState = $("#navBackButton").hasClass("nav-back-btn-disabled");
const navFwdDisabledState = $("#navForwardButton").hasClass("nav-forward-btn-disabled");
return navBackDisabledState === backDisabled && navFwdDisabledState === fwdDisabled;
}, message || "For the nav buttons state to be updated");
expect($("#navBackButton").hasClass("nav-back-btn-disabled")).toBe(backDisabled);
expect($("#navForwardButton").hasClass("nav-forward-btn-disabled")).toBe(fwdDisabled);
}
async function _validateActiveFile(relativePath) {
await awaitsFor(()=>{
return MainViewManager.getCurrentlyViewedFile().fullPath === path.join(testProjectPath, relativePath);
}, "Active file to be " + relativePath);
}
async function navigateBack() {
await awaitsForDone(CommandManager.execute(NAVIGATION_JUMP_BACK), "To navigate back");
}
async function navigateForward() {
await awaitsForDone(CommandManager.execute(NAVIGATION_JUMP_FWD), "To navigate forward");
}
async function navigateResetStack() {
await awaitsForDone(CommandManager.execute(_NAVIGATION_RESET_FOR_TESTS), "reset nav stacks");
}
async function _validateNavForFiles(file1, file2, file3) {
await openFile("img/update_large_icon.svg"); // we open this just to have the first file back button
// on first time, because of a race condition, the back button may(enabled but does nothing) or
// may not be enabled. we cant test for this, so we just open a random file as control
await openFile(file1);
await openFile(file2);
await _expectNavButton(false, true, "nav back only enabled");
await navigateBack();
await _validateActiveFile(file1);
await _expectNavButton(false, false, "both enabled");
await navigateForward();
await _validateActiveFile(file2);
await _expectNavButton(false, true, "nav back only enabled");
await navigateBack();
await _validateActiveFile(file1);
await _expectNavButton(false, false, "both enabled");
await openFile(file3);
await _expectNavButton(false, true, "nav forward disabled due to new file open");
}
it("Should navigate back and forward between text files", async function () {
await navigateResetStack();
await _validateNavForFiles("test.css", "test.html", "test.js");
}, 15000);
it("Should navigate back and forward between image files", async function () {
await navigateResetStack();
await _validateNavForFiles("img/Color.png", "img/grabber_color-well.png", "img/throbber.gif");
}, 15000);
it("Should navigate back and forward between image and text files", async function () {
await navigateResetStack();
await _validateNavForFiles("test.css", "img/grabber_color-well.png", "test.js");
}, 15000);
it("Should navigate back and forward even after all files closed", async function () {
await navigateResetStack();
await _validateNavForFiles("test.css", "test.html", "test.js");
await awaitsForDone(CommandManager.execute(Commands.FILE_CLOSE_ALL, { _forceClose: true }),
"closing all files");
await _expectNavButton(false, true, "nav back only enabled");
await navigateBack();
await _validateActiveFile("test.js");
await _expectNavButton(false, true, "nav back only enabled");
}, 15000);
});
});