browser/devtools/scratchpad/test/browser_scratchpad_recent_files.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:3545a43fbff7
1 /* vim: set ts=2 et sw=2 tw=80: */
2 /* Any copyright is dedicated to the Public Domain.
3 http://creativecommons.org/publicdomain/zero/1.0/ */
4 /* Bug 651942 */
5
6 let tempScope = {};
7 Cu.import("resource://gre/modules/NetUtil.jsm", tempScope);
8 Cu.import("resource://gre/modules/FileUtils.jsm", tempScope);
9 let NetUtil = tempScope.NetUtil;
10 let FileUtils = tempScope.FileUtils;
11
12 // Reference to the Scratchpad object.
13 let gScratchpad;
14
15 // References to the temporary nsIFiles.
16 let gFile01;
17 let gFile02;
18 let gFile03;
19 let gFile04;
20
21 // lists of recent files.
22 var lists = {
23 recentFiles01: null,
24 recentFiles02: null,
25 recentFiles03: null,
26 recentFiles04: null,
27 };
28
29 // Temporary file names.
30 let gFileName01 = "file01_ForBug651942.tmp"
31 let gFileName02 = "☕" // See bug 783858 for more information
32 let gFileName03 = "file03_ForBug651942.tmp"
33 let gFileName04 = "file04_ForBug651942.tmp"
34
35 // Content for the temporary files.
36 let gFileContent;
37 let gFileContent01 = "hello.world.01('bug651942');";
38 let gFileContent02 = "hello.world.02('bug651942');";
39 let gFileContent03 = "hello.world.03('bug651942');";
40 let gFileContent04 = "hello.world.04('bug651942');";
41
42 function startTest()
43 {
44 gScratchpad = gScratchpadWindow.Scratchpad;
45
46 gFile01 = createAndLoadTemporaryFile(gFile01, gFileName01, gFileContent01);
47 gFile02 = createAndLoadTemporaryFile(gFile02, gFileName02, gFileContent02);
48 gFile03 = createAndLoadTemporaryFile(gFile03, gFileName03, gFileContent03);
49 }
50
51 // Test to see if the three files we created in the 'startTest()'-method have
52 // been added to the list of recent files.
53 function testAddedToRecent()
54 {
55 lists.recentFiles01 = gScratchpad.getRecentFiles();
56
57 is(lists.recentFiles01.length, 3,
58 "Temporary files created successfully and added to list of recent files.");
59
60 // Create a 4th file, this should clear the oldest file.
61 gFile04 = createAndLoadTemporaryFile(gFile04, gFileName04, gFileContent04);
62 }
63
64 // We have opened a 4th file. Test to see if the oldest recent file was removed,
65 // and that the other files were reordered successfully.
66 function testOverwriteRecent()
67 {
68 lists.recentFiles02 = gScratchpad.getRecentFiles();
69
70 is(lists.recentFiles02[0], lists.recentFiles01[1],
71 "File02 was reordered successfully in the 'recent files'-list.");
72 is(lists.recentFiles02[1], lists.recentFiles01[2],
73 "File03 was reordered successfully in the 'recent files'-list.");
74 isnot(lists.recentFiles02[2], lists.recentFiles01[2],
75 "File04: was added successfully.");
76
77 // Open the oldest recent file.
78 gScratchpad.openFile(0);
79 }
80
81 // We have opened the "oldest"-recent file. Test to see if it is now the most
82 // recent file, and that the other files were reordered successfully.
83 function testOpenOldestRecent()
84 {
85 lists.recentFiles03 = gScratchpad.getRecentFiles();
86
87 is(lists.recentFiles02[0], lists.recentFiles03[2],
88 "File04 was reordered successfully in the 'recent files'-list.");
89 is(lists.recentFiles02[1], lists.recentFiles03[0],
90 "File03 was reordered successfully in the 'recent files'-list.");
91 is(lists.recentFiles02[2], lists.recentFiles03[1],
92 "File02 was reordered successfully in the 'recent files'-list.");
93
94 Services.prefs.setIntPref("devtools.scratchpad.recentFilesMax", 0);
95 }
96
97 // The "devtools.scratchpad.recentFilesMax"-preference was set to zero (0).
98 // This should disable the "Open Recent"-menu by hiding it (this should not
99 // remove any files from the list). Test to see if it's been hidden.
100 function testHideMenu()
101 {
102 let menu = gScratchpadWindow.document.getElementById("sp-open_recent-menu");
103 ok(menu.hasAttribute("hidden"), "The menu was hidden successfully.");
104
105 Services.prefs.setIntPref("devtools.scratchpad.recentFilesMax", 2);
106 }
107
108 // We have set the recentFilesMax-pref to one (1), this enables the feature,
109 // removes the two oldest files, rebuilds the menu and removes the
110 // "hidden"-attribute from it. Test to see if this works.
111 function testChangedMaxRecent()
112 {
113 let menu = gScratchpadWindow.document.getElementById("sp-open_recent-menu");
114 ok(!menu.hasAttribute("hidden"), "The menu is visible. \\o/");
115
116 lists.recentFiles04 = gScratchpad.getRecentFiles();
117
118 is(lists.recentFiles04.length, 2,
119 "Two recent files were successfully removed from the 'recent files'-list");
120
121 let doc = gScratchpadWindow.document;
122 let popup = doc.getElementById("sp-menu-open_recentPopup");
123
124 let menuitemLabel = popup.children[0].getAttribute("label");
125 let correctMenuItem = false;
126 if (menuitemLabel === lists.recentFiles03[2] &&
127 menuitemLabel === lists.recentFiles04[1]) {
128 correctMenuItem = true;
129 }
130
131 is(correctMenuItem, true,
132 "Two recent files were successfully removed from the 'Open Recent'-menu");
133
134 // We now remove one file from the harddrive and use the recent-menuitem for
135 // it to make sure the user is notified that the file no longer exists.
136 // This is tested in testOpenDeletedFile().
137 gFile04.remove(false);
138
139 // Make sure the file has been deleted before continuing to avoid
140 // intermittent oranges.
141 waitForFileDeletion();
142 }
143
144 function waitForFileDeletion() {
145 if (gFile04.exists()) {
146 executeSoon(waitForFileDeletion);
147 return;
148 }
149
150 gFile04 = null;
151 gScratchpad.openFile(0);
152 }
153
154 // By now we should have two recent files stored in the list but one of the
155 // files should be missing on the harddrive.
156 function testOpenDeletedFile() {
157 let doc = gScratchpadWindow.document;
158 let popup = doc.getElementById("sp-menu-open_recentPopup");
159
160 is(gScratchpad.getRecentFiles().length, 1,
161 "The missing file was successfully removed from the list.");
162 // The number of recent files stored, plus the separator and the
163 // clearRecentMenuItems-item.
164 is(popup.children.length, 3,
165 "The missing file was successfully removed from the menu.");
166 ok(gScratchpad.notificationBox.currentNotification,
167 "The notification was successfully displayed.");
168 is(gScratchpad.notificationBox.currentNotification.label,
169 gScratchpad.strings.GetStringFromName("fileNoLongerExists.notification"),
170 "The notification label is correct.");
171
172 gScratchpad.clearRecentFiles();
173 }
174
175 // We have cleared the last file. Test to see if the last file was removed,
176 // the menu is empty and was disabled successfully.
177 function testClearedAll()
178 {
179 let doc = gScratchpadWindow.document;
180 let menu = doc.getElementById("sp-open_recent-menu");
181 let popup = doc.getElementById("sp-menu-open_recentPopup");
182
183 is(gScratchpad.getRecentFiles().length, 0,
184 "All recent files removed successfully.");
185 is(popup.children.length, 0, "All menuitems removed successfully.");
186 ok(menu.hasAttribute("disabled"),
187 "No files in the menu, it was disabled successfully.");
188
189 finishTest();
190 }
191
192 function createAndLoadTemporaryFile(aFile, aFileName, aFileContent)
193 {
194 // Create a temporary file.
195 aFile = FileUtils.getFile("TmpD", [aFileName]);
196 aFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0o666);
197
198 // Write the temporary file.
199 let fout = Cc["@mozilla.org/network/file-output-stream;1"].
200 createInstance(Ci.nsIFileOutputStream);
201 fout.init(aFile.QueryInterface(Ci.nsILocalFile), 0x02 | 0x08 | 0x20,
202 0o644, fout.DEFER_OPEN);
203
204 gScratchpad.setFilename(aFile.path);
205 gScratchpad.importFromFile(aFile.QueryInterface(Ci.nsILocalFile), true,
206 fileImported);
207 gScratchpad.saveFile(fileSaved);
208
209 return aFile;
210 }
211
212 function fileImported(aStatus)
213 {
214 ok(Components.isSuccessCode(aStatus),
215 "the temporary file was imported successfully with Scratchpad");
216 }
217
218 function fileSaved(aStatus)
219 {
220 ok(Components.isSuccessCode(aStatus),
221 "the temporary file was saved successfully with Scratchpad");
222
223 checkIfMenuIsPopulated();
224 }
225
226 function checkIfMenuIsPopulated()
227 {
228 let doc = gScratchpadWindow.document;
229 let expectedMenuitemCount = doc.getElementById("sp-menu-open_recentPopup").
230 children.length;
231 // The number of recent files stored, plus the separator and the
232 // clearRecentMenuItems-item.
233 let recentFilesPlusExtra = gScratchpad.getRecentFiles().length + 2;
234
235 if (expectedMenuitemCount > 2) {
236 is(expectedMenuitemCount, recentFilesPlusExtra,
237 "the recent files menu was populated successfully.");
238 }
239 }
240
241 /**
242 * The PreferenceObserver listens for preference changes while Scratchpad is
243 * running.
244 */
245 var PreferenceObserver = {
246 _initialized: false,
247
248 _timesFired: 0,
249 set timesFired(aNewValue) {
250 this._timesFired = aNewValue;
251 },
252 get timesFired() {
253 return this._timesFired;
254 },
255
256 init: function PO_init()
257 {
258 if (this._initialized) {
259 return;
260 }
261
262 this.branch = Services.prefs.getBranch("devtools.scratchpad.");
263 this.branch.addObserver("", this, false);
264 this._initialized = true;
265 },
266
267 observe: function PO_observe(aMessage, aTopic, aData)
268 {
269 if (aTopic != "nsPref:changed") {
270 return;
271 }
272
273 switch (this.timesFired) {
274 case 0:
275 this.timesFired = 1;
276 break;
277 case 1:
278 this.timesFired = 2;
279 break;
280 case 2:
281 this.timesFired = 3;
282 testAddedToRecent();
283 break;
284 case 3:
285 this.timesFired = 4;
286 testOverwriteRecent();
287 break;
288 case 4:
289 this.timesFired = 5;
290 testOpenOldestRecent();
291 break;
292 case 5:
293 this.timesFired = 6;
294 testHideMenu();
295 break;
296 case 6:
297 this.timesFired = 7;
298 testChangedMaxRecent();
299 break;
300 case 7:
301 this.timesFired = 8;
302 testOpenDeletedFile();
303 break;
304 case 8:
305 this.timesFired = 9;
306 testClearedAll();
307 break;
308 }
309 },
310
311 uninit: function PO_uninit () {
312 this.branch.removeObserver("", this);
313 }
314 };
315
316 function test()
317 {
318 waitForExplicitFinish();
319
320 registerCleanupFunction(function () {
321 gFile01.remove(false);
322 gFile01 = null;
323 gFile02.remove(false);
324 gFile02 = null;
325 gFile03.remove(false);
326 gFile03 = null;
327 // gFile04 was removed earlier.
328 lists.recentFiles01 = null;
329 lists.recentFiles02 = null;
330 lists.recentFiles03 = null;
331 lists.recentFiles04 = null;
332 gScratchpad = null;
333
334 PreferenceObserver.uninit();
335 Services.prefs.clearUserPref("devtools.scratchpad.recentFilesMax");
336 });
337
338 Services.prefs.setIntPref("devtools.scratchpad.recentFilesMax", 3);
339
340 // Initiate the preference observer after we have set the temporary recent
341 // files max for this test.
342 PreferenceObserver.init();
343
344 gBrowser.selectedTab = gBrowser.addTab();
345 gBrowser.selectedBrowser.addEventListener("load", function onLoad() {
346 gBrowser.selectedBrowser.removeEventListener("load", onLoad, true);
347 openScratchpad(startTest);
348 }, true);
349
350 content.location = "data:text/html,<p>test recent files in Scratchpad";
351 }
352
353 function finishTest()
354 {
355 finish();
356 }

mercurial