browser/devtools/scratchpad/test/browser_scratchpad_recent_files.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial