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.

     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 */
     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;
    12 // Reference to the Scratchpad object.
    13 let gScratchpad;
    15 // References to the temporary nsIFiles.
    16 let gFile01;
    17 let gFile02;
    18 let gFile03;
    19 let gFile04;
    21 // lists of recent files.
    22 var lists = {
    23   recentFiles01: null,
    24   recentFiles02: null,
    25   recentFiles03: null,
    26   recentFiles04: null,
    27 };
    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"
    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');";
    42 function startTest()
    43 {
    44   gScratchpad = gScratchpadWindow.Scratchpad;
    46   gFile01 = createAndLoadTemporaryFile(gFile01, gFileName01, gFileContent01);
    47   gFile02 = createAndLoadTemporaryFile(gFile02, gFileName02, gFileContent02);
    48   gFile03 = createAndLoadTemporaryFile(gFile03, gFileName03, gFileContent03);
    49 }
    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();
    57   is(lists.recentFiles01.length, 3,
    58      "Temporary files created successfully and added to list of recent files.");
    60   // Create a 4th file, this should clear the oldest file.
    61   gFile04 = createAndLoadTemporaryFile(gFile04, gFileName04, gFileContent04);
    62 }
    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();
    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.");
    77   // Open the oldest recent file.
    78   gScratchpad.openFile(0);
    79 }
    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();
    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.");
    94   Services.prefs.setIntPref("devtools.scratchpad.recentFilesMax", 0);
    95 }
    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.");
   105   Services.prefs.setIntPref("devtools.scratchpad.recentFilesMax", 2);
   106 }
   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/");
   116   lists.recentFiles04 = gScratchpad.getRecentFiles();
   118   is(lists.recentFiles04.length, 2,
   119      "Two recent files were successfully removed from the 'recent files'-list");
   121   let doc = gScratchpadWindow.document;
   122   let popup = doc.getElementById("sp-menu-open_recentPopup");
   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   }
   131   is(correctMenuItem, true,
   132      "Two recent files were successfully removed from the 'Open Recent'-menu");
   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);
   139   // Make sure the file has been deleted before continuing to avoid
   140   // intermittent oranges.
   141   waitForFileDeletion();
   142 }
   144 function waitForFileDeletion() {
   145   if (gFile04.exists()) {
   146     executeSoon(waitForFileDeletion);
   147     return;
   148   }
   150   gFile04 = null;
   151   gScratchpad.openFile(0);
   152 }
   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");
   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.");
   172   gScratchpad.clearRecentFiles();
   173 }
   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");
   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.");
   189   finishTest();
   190 }
   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);
   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);
   204   gScratchpad.setFilename(aFile.path);
   205   gScratchpad.importFromFile(aFile.QueryInterface(Ci.nsILocalFile),  true,
   206                             fileImported);
   207   gScratchpad.saveFile(fileSaved);
   209   return aFile;
   210 }
   212 function fileImported(aStatus)
   213 {
   214   ok(Components.isSuccessCode(aStatus),
   215      "the temporary file was imported successfully with Scratchpad");
   216 }
   218 function fileSaved(aStatus)
   219 {
   220   ok(Components.isSuccessCode(aStatus),
   221      "the temporary file was saved successfully with Scratchpad");
   223   checkIfMenuIsPopulated();
   224 }
   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;
   235   if (expectedMenuitemCount > 2) {
   236     is(expectedMenuitemCount, recentFilesPlusExtra,
   237        "the recent files menu was populated successfully.");
   238   }
   239 }
   241 /**
   242  * The PreferenceObserver listens for preference changes while Scratchpad is
   243  * running.
   244  */
   245 var PreferenceObserver = {
   246   _initialized: false,
   248   _timesFired: 0,
   249   set timesFired(aNewValue) {
   250     this._timesFired = aNewValue;
   251   },
   252   get timesFired() {
   253     return this._timesFired;
   254   },
   256   init: function PO_init()
   257   {
   258     if (this._initialized) {
   259       return;
   260     }
   262     this.branch = Services.prefs.getBranch("devtools.scratchpad.");
   263     this.branch.addObserver("", this, false);
   264     this._initialized = true;
   265   },
   267   observe: function PO_observe(aMessage, aTopic, aData)
   268   {
   269     if (aTopic != "nsPref:changed") {
   270       return;
   271     }
   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   },
   311   uninit: function PO_uninit () {
   312     this.branch.removeObserver("", this);
   313   }
   314 };
   316 function test()
   317 {
   318   waitForExplicitFinish();
   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;
   334     PreferenceObserver.uninit();
   335     Services.prefs.clearUserPref("devtools.scratchpad.recentFilesMax");
   336   });
   338   Services.prefs.setIntPref("devtools.scratchpad.recentFilesMax", 3);
   340   // Initiate the preference observer after we have set the temporary recent
   341   // files max for this test.
   342   PreferenceObserver.init();
   344   gBrowser.selectedTab = gBrowser.addTab();
   345   gBrowser.selectedBrowser.addEventListener("load", function onLoad() {
   346     gBrowser.selectedBrowser.removeEventListener("load", onLoad, true);
   347     openScratchpad(startTest);
   348   }, true);
   350   content.location = "data:text/html,<p>test recent files in Scratchpad";
   351 }
   353 function finishTest()
   354 {
   355   finish();
   356 }

mercurial