michael@0: /* vim: set ts=2 et sw=2 tw=80: */ michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: /* Bug 651942 */ michael@0: michael@0: let tempScope = {}; michael@0: Cu.import("resource://gre/modules/NetUtil.jsm", tempScope); michael@0: Cu.import("resource://gre/modules/FileUtils.jsm", tempScope); michael@0: let NetUtil = tempScope.NetUtil; michael@0: let FileUtils = tempScope.FileUtils; michael@0: michael@0: // Reference to the Scratchpad object. michael@0: let gScratchpad; michael@0: michael@0: // References to the temporary nsIFiles. michael@0: let gFile01; michael@0: let gFile02; michael@0: let gFile03; michael@0: let gFile04; michael@0: michael@0: // lists of recent files. michael@0: var lists = { michael@0: recentFiles01: null, michael@0: recentFiles02: null, michael@0: recentFiles03: null, michael@0: recentFiles04: null, michael@0: }; michael@0: michael@0: // Temporary file names. michael@0: let gFileName01 = "file01_ForBug651942.tmp" michael@0: let gFileName02 = "☕" // See bug 783858 for more information michael@0: let gFileName03 = "file03_ForBug651942.tmp" michael@0: let gFileName04 = "file04_ForBug651942.tmp" michael@0: michael@0: // Content for the temporary files. michael@0: let gFileContent; michael@0: let gFileContent01 = "hello.world.01('bug651942');"; michael@0: let gFileContent02 = "hello.world.02('bug651942');"; michael@0: let gFileContent03 = "hello.world.03('bug651942');"; michael@0: let gFileContent04 = "hello.world.04('bug651942');"; michael@0: michael@0: function startTest() michael@0: { michael@0: gScratchpad = gScratchpadWindow.Scratchpad; michael@0: michael@0: gFile01 = createAndLoadTemporaryFile(gFile01, gFileName01, gFileContent01); michael@0: gFile02 = createAndLoadTemporaryFile(gFile02, gFileName02, gFileContent02); michael@0: gFile03 = createAndLoadTemporaryFile(gFile03, gFileName03, gFileContent03); michael@0: } michael@0: michael@0: // Test to see if the three files we created in the 'startTest()'-method have michael@0: // been added to the list of recent files. michael@0: function testAddedToRecent() michael@0: { michael@0: lists.recentFiles01 = gScratchpad.getRecentFiles(); michael@0: michael@0: is(lists.recentFiles01.length, 3, michael@0: "Temporary files created successfully and added to list of recent files."); michael@0: michael@0: // Create a 4th file, this should clear the oldest file. michael@0: gFile04 = createAndLoadTemporaryFile(gFile04, gFileName04, gFileContent04); michael@0: } michael@0: michael@0: // We have opened a 4th file. Test to see if the oldest recent file was removed, michael@0: // and that the other files were reordered successfully. michael@0: function testOverwriteRecent() michael@0: { michael@0: lists.recentFiles02 = gScratchpad.getRecentFiles(); michael@0: michael@0: is(lists.recentFiles02[0], lists.recentFiles01[1], michael@0: "File02 was reordered successfully in the 'recent files'-list."); michael@0: is(lists.recentFiles02[1], lists.recentFiles01[2], michael@0: "File03 was reordered successfully in the 'recent files'-list."); michael@0: isnot(lists.recentFiles02[2], lists.recentFiles01[2], michael@0: "File04: was added successfully."); michael@0: michael@0: // Open the oldest recent file. michael@0: gScratchpad.openFile(0); michael@0: } michael@0: michael@0: // We have opened the "oldest"-recent file. Test to see if it is now the most michael@0: // recent file, and that the other files were reordered successfully. michael@0: function testOpenOldestRecent() michael@0: { michael@0: lists.recentFiles03 = gScratchpad.getRecentFiles(); michael@0: michael@0: is(lists.recentFiles02[0], lists.recentFiles03[2], michael@0: "File04 was reordered successfully in the 'recent files'-list."); michael@0: is(lists.recentFiles02[1], lists.recentFiles03[0], michael@0: "File03 was reordered successfully in the 'recent files'-list."); michael@0: is(lists.recentFiles02[2], lists.recentFiles03[1], michael@0: "File02 was reordered successfully in the 'recent files'-list."); michael@0: michael@0: Services.prefs.setIntPref("devtools.scratchpad.recentFilesMax", 0); michael@0: } michael@0: michael@0: // The "devtools.scratchpad.recentFilesMax"-preference was set to zero (0). michael@0: // This should disable the "Open Recent"-menu by hiding it (this should not michael@0: // remove any files from the list). Test to see if it's been hidden. michael@0: function testHideMenu() michael@0: { michael@0: let menu = gScratchpadWindow.document.getElementById("sp-open_recent-menu"); michael@0: ok(menu.hasAttribute("hidden"), "The menu was hidden successfully."); michael@0: michael@0: Services.prefs.setIntPref("devtools.scratchpad.recentFilesMax", 2); michael@0: } michael@0: michael@0: // We have set the recentFilesMax-pref to one (1), this enables the feature, michael@0: // removes the two oldest files, rebuilds the menu and removes the michael@0: // "hidden"-attribute from it. Test to see if this works. michael@0: function testChangedMaxRecent() michael@0: { michael@0: let menu = gScratchpadWindow.document.getElementById("sp-open_recent-menu"); michael@0: ok(!menu.hasAttribute("hidden"), "The menu is visible. \\o/"); michael@0: michael@0: lists.recentFiles04 = gScratchpad.getRecentFiles(); michael@0: michael@0: is(lists.recentFiles04.length, 2, michael@0: "Two recent files were successfully removed from the 'recent files'-list"); michael@0: michael@0: let doc = gScratchpadWindow.document; michael@0: let popup = doc.getElementById("sp-menu-open_recentPopup"); michael@0: michael@0: let menuitemLabel = popup.children[0].getAttribute("label"); michael@0: let correctMenuItem = false; michael@0: if (menuitemLabel === lists.recentFiles03[2] && michael@0: menuitemLabel === lists.recentFiles04[1]) { michael@0: correctMenuItem = true; michael@0: } michael@0: michael@0: is(correctMenuItem, true, michael@0: "Two recent files were successfully removed from the 'Open Recent'-menu"); michael@0: michael@0: // We now remove one file from the harddrive and use the recent-menuitem for michael@0: // it to make sure the user is notified that the file no longer exists. michael@0: // This is tested in testOpenDeletedFile(). michael@0: gFile04.remove(false); michael@0: michael@0: // Make sure the file has been deleted before continuing to avoid michael@0: // intermittent oranges. michael@0: waitForFileDeletion(); michael@0: } michael@0: michael@0: function waitForFileDeletion() { michael@0: if (gFile04.exists()) { michael@0: executeSoon(waitForFileDeletion); michael@0: return; michael@0: } michael@0: michael@0: gFile04 = null; michael@0: gScratchpad.openFile(0); michael@0: } michael@0: michael@0: // By now we should have two recent files stored in the list but one of the michael@0: // files should be missing on the harddrive. michael@0: function testOpenDeletedFile() { michael@0: let doc = gScratchpadWindow.document; michael@0: let popup = doc.getElementById("sp-menu-open_recentPopup"); michael@0: michael@0: is(gScratchpad.getRecentFiles().length, 1, michael@0: "The missing file was successfully removed from the list."); michael@0: // The number of recent files stored, plus the separator and the michael@0: // clearRecentMenuItems-item. michael@0: is(popup.children.length, 3, michael@0: "The missing file was successfully removed from the menu."); michael@0: ok(gScratchpad.notificationBox.currentNotification, michael@0: "The notification was successfully displayed."); michael@0: is(gScratchpad.notificationBox.currentNotification.label, michael@0: gScratchpad.strings.GetStringFromName("fileNoLongerExists.notification"), michael@0: "The notification label is correct."); michael@0: michael@0: gScratchpad.clearRecentFiles(); michael@0: } michael@0: michael@0: // We have cleared the last file. Test to see if the last file was removed, michael@0: // the menu is empty and was disabled successfully. michael@0: function testClearedAll() michael@0: { michael@0: let doc = gScratchpadWindow.document; michael@0: let menu = doc.getElementById("sp-open_recent-menu"); michael@0: let popup = doc.getElementById("sp-menu-open_recentPopup"); michael@0: michael@0: is(gScratchpad.getRecentFiles().length, 0, michael@0: "All recent files removed successfully."); michael@0: is(popup.children.length, 0, "All menuitems removed successfully."); michael@0: ok(menu.hasAttribute("disabled"), michael@0: "No files in the menu, it was disabled successfully."); michael@0: michael@0: finishTest(); michael@0: } michael@0: michael@0: function createAndLoadTemporaryFile(aFile, aFileName, aFileContent) michael@0: { michael@0: // Create a temporary file. michael@0: aFile = FileUtils.getFile("TmpD", [aFileName]); michael@0: aFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0o666); michael@0: michael@0: // Write the temporary file. michael@0: let fout = Cc["@mozilla.org/network/file-output-stream;1"]. michael@0: createInstance(Ci.nsIFileOutputStream); michael@0: fout.init(aFile.QueryInterface(Ci.nsILocalFile), 0x02 | 0x08 | 0x20, michael@0: 0o644, fout.DEFER_OPEN); michael@0: michael@0: gScratchpad.setFilename(aFile.path); michael@0: gScratchpad.importFromFile(aFile.QueryInterface(Ci.nsILocalFile), true, michael@0: fileImported); michael@0: gScratchpad.saveFile(fileSaved); michael@0: michael@0: return aFile; michael@0: } michael@0: michael@0: function fileImported(aStatus) michael@0: { michael@0: ok(Components.isSuccessCode(aStatus), michael@0: "the temporary file was imported successfully with Scratchpad"); michael@0: } michael@0: michael@0: function fileSaved(aStatus) michael@0: { michael@0: ok(Components.isSuccessCode(aStatus), michael@0: "the temporary file was saved successfully with Scratchpad"); michael@0: michael@0: checkIfMenuIsPopulated(); michael@0: } michael@0: michael@0: function checkIfMenuIsPopulated() michael@0: { michael@0: let doc = gScratchpadWindow.document; michael@0: let expectedMenuitemCount = doc.getElementById("sp-menu-open_recentPopup"). michael@0: children.length; michael@0: // The number of recent files stored, plus the separator and the michael@0: // clearRecentMenuItems-item. michael@0: let recentFilesPlusExtra = gScratchpad.getRecentFiles().length + 2; michael@0: michael@0: if (expectedMenuitemCount > 2) { michael@0: is(expectedMenuitemCount, recentFilesPlusExtra, michael@0: "the recent files menu was populated successfully."); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * The PreferenceObserver listens for preference changes while Scratchpad is michael@0: * running. michael@0: */ michael@0: var PreferenceObserver = { michael@0: _initialized: false, michael@0: michael@0: _timesFired: 0, michael@0: set timesFired(aNewValue) { michael@0: this._timesFired = aNewValue; michael@0: }, michael@0: get timesFired() { michael@0: return this._timesFired; michael@0: }, michael@0: michael@0: init: function PO_init() michael@0: { michael@0: if (this._initialized) { michael@0: return; michael@0: } michael@0: michael@0: this.branch = Services.prefs.getBranch("devtools.scratchpad."); michael@0: this.branch.addObserver("", this, false); michael@0: this._initialized = true; michael@0: }, michael@0: michael@0: observe: function PO_observe(aMessage, aTopic, aData) michael@0: { michael@0: if (aTopic != "nsPref:changed") { michael@0: return; michael@0: } michael@0: michael@0: switch (this.timesFired) { michael@0: case 0: michael@0: this.timesFired = 1; michael@0: break; michael@0: case 1: michael@0: this.timesFired = 2; michael@0: break; michael@0: case 2: michael@0: this.timesFired = 3; michael@0: testAddedToRecent(); michael@0: break; michael@0: case 3: michael@0: this.timesFired = 4; michael@0: testOverwriteRecent(); michael@0: break; michael@0: case 4: michael@0: this.timesFired = 5; michael@0: testOpenOldestRecent(); michael@0: break; michael@0: case 5: michael@0: this.timesFired = 6; michael@0: testHideMenu(); michael@0: break; michael@0: case 6: michael@0: this.timesFired = 7; michael@0: testChangedMaxRecent(); michael@0: break; michael@0: case 7: michael@0: this.timesFired = 8; michael@0: testOpenDeletedFile(); michael@0: break; michael@0: case 8: michael@0: this.timesFired = 9; michael@0: testClearedAll(); michael@0: break; michael@0: } michael@0: }, michael@0: michael@0: uninit: function PO_uninit () { michael@0: this.branch.removeObserver("", this); michael@0: } michael@0: }; michael@0: michael@0: function test() michael@0: { michael@0: waitForExplicitFinish(); michael@0: michael@0: registerCleanupFunction(function () { michael@0: gFile01.remove(false); michael@0: gFile01 = null; michael@0: gFile02.remove(false); michael@0: gFile02 = null; michael@0: gFile03.remove(false); michael@0: gFile03 = null; michael@0: // gFile04 was removed earlier. michael@0: lists.recentFiles01 = null; michael@0: lists.recentFiles02 = null; michael@0: lists.recentFiles03 = null; michael@0: lists.recentFiles04 = null; michael@0: gScratchpad = null; michael@0: michael@0: PreferenceObserver.uninit(); michael@0: Services.prefs.clearUserPref("devtools.scratchpad.recentFilesMax"); michael@0: }); michael@0: michael@0: Services.prefs.setIntPref("devtools.scratchpad.recentFilesMax", 3); michael@0: michael@0: // Initiate the preference observer after we have set the temporary recent michael@0: // files max for this test. michael@0: PreferenceObserver.init(); michael@0: michael@0: gBrowser.selectedTab = gBrowser.addTab(); michael@0: gBrowser.selectedBrowser.addEventListener("load", function onLoad() { michael@0: gBrowser.selectedBrowser.removeEventListener("load", onLoad, true); michael@0: openScratchpad(startTest); michael@0: }, true); michael@0: michael@0: content.location = "data:text/html,

test recent files in Scratchpad"; michael@0: } michael@0: michael@0: function finishTest() michael@0: { michael@0: finish(); michael@0: }