browser/devtools/scratchpad/test/browser_scratchpad_recent_files.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/devtools/scratchpad/test/browser_scratchpad_recent_files.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,356 @@
     1.4 +/* vim: set ts=2 et sw=2 tw=80: */
     1.5 +/* Any copyright is dedicated to the Public Domain.
     1.6 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.7 +/* Bug 651942 */
     1.8 +
     1.9 +let tempScope = {};
    1.10 +Cu.import("resource://gre/modules/NetUtil.jsm", tempScope);
    1.11 +Cu.import("resource://gre/modules/FileUtils.jsm", tempScope);
    1.12 +let NetUtil = tempScope.NetUtil;
    1.13 +let FileUtils = tempScope.FileUtils;
    1.14 +
    1.15 +// Reference to the Scratchpad object.
    1.16 +let gScratchpad;
    1.17 +
    1.18 +// References to the temporary nsIFiles.
    1.19 +let gFile01;
    1.20 +let gFile02;
    1.21 +let gFile03;
    1.22 +let gFile04;
    1.23 +
    1.24 +// lists of recent files.
    1.25 +var lists = {
    1.26 +  recentFiles01: null,
    1.27 +  recentFiles02: null,
    1.28 +  recentFiles03: null,
    1.29 +  recentFiles04: null,
    1.30 +};
    1.31 +
    1.32 +// Temporary file names.
    1.33 +let gFileName01 = "file01_ForBug651942.tmp"
    1.34 +let gFileName02 = "☕" // See bug 783858 for more information
    1.35 +let gFileName03 = "file03_ForBug651942.tmp"
    1.36 +let gFileName04 = "file04_ForBug651942.tmp"
    1.37 +
    1.38 +// Content for the temporary files.
    1.39 +let gFileContent;
    1.40 +let gFileContent01 = "hello.world.01('bug651942');";
    1.41 +let gFileContent02 = "hello.world.02('bug651942');";
    1.42 +let gFileContent03 = "hello.world.03('bug651942');";
    1.43 +let gFileContent04 = "hello.world.04('bug651942');";
    1.44 +
    1.45 +function startTest()
    1.46 +{
    1.47 +  gScratchpad = gScratchpadWindow.Scratchpad;
    1.48 +
    1.49 +  gFile01 = createAndLoadTemporaryFile(gFile01, gFileName01, gFileContent01);
    1.50 +  gFile02 = createAndLoadTemporaryFile(gFile02, gFileName02, gFileContent02);
    1.51 +  gFile03 = createAndLoadTemporaryFile(gFile03, gFileName03, gFileContent03);
    1.52 +}
    1.53 +
    1.54 +// Test to see if the three files we created in the 'startTest()'-method have
    1.55 +// been added to the list of recent files.
    1.56 +function testAddedToRecent()
    1.57 +{
    1.58 +  lists.recentFiles01 = gScratchpad.getRecentFiles();
    1.59 +
    1.60 +  is(lists.recentFiles01.length, 3,
    1.61 +     "Temporary files created successfully and added to list of recent files.");
    1.62 +
    1.63 +  // Create a 4th file, this should clear the oldest file.
    1.64 +  gFile04 = createAndLoadTemporaryFile(gFile04, gFileName04, gFileContent04);
    1.65 +}
    1.66 +
    1.67 +// We have opened a 4th file. Test to see if the oldest recent file was removed,
    1.68 +// and that the other files were reordered successfully.
    1.69 +function testOverwriteRecent()
    1.70 +{
    1.71 +  lists.recentFiles02 = gScratchpad.getRecentFiles();
    1.72 +
    1.73 +  is(lists.recentFiles02[0], lists.recentFiles01[1],
    1.74 +     "File02 was reordered successfully in the 'recent files'-list.");
    1.75 +  is(lists.recentFiles02[1], lists.recentFiles01[2],
    1.76 +     "File03 was reordered successfully in the 'recent files'-list.");
    1.77 +  isnot(lists.recentFiles02[2], lists.recentFiles01[2],
    1.78 +        "File04: was added successfully.");
    1.79 +
    1.80 +  // Open the oldest recent file.
    1.81 +  gScratchpad.openFile(0);
    1.82 +}
    1.83 +
    1.84 +// We have opened the "oldest"-recent file. Test to see if it is now the most
    1.85 +// recent file, and that the other files were reordered successfully.
    1.86 +function testOpenOldestRecent()
    1.87 +{
    1.88 +  lists.recentFiles03 = gScratchpad.getRecentFiles();
    1.89 +
    1.90 +  is(lists.recentFiles02[0], lists.recentFiles03[2],
    1.91 +     "File04 was reordered successfully in the 'recent files'-list.");
    1.92 +  is(lists.recentFiles02[1], lists.recentFiles03[0],
    1.93 +     "File03 was reordered successfully in the 'recent files'-list.");
    1.94 +  is(lists.recentFiles02[2], lists.recentFiles03[1],
    1.95 +     "File02 was reordered successfully in the 'recent files'-list.");
    1.96 +
    1.97 +  Services.prefs.setIntPref("devtools.scratchpad.recentFilesMax", 0);
    1.98 +}
    1.99 +
   1.100 +// The "devtools.scratchpad.recentFilesMax"-preference was set to zero (0).
   1.101 +// This should disable the "Open Recent"-menu by hiding it (this should not
   1.102 +// remove any files from the list). Test to see if it's been hidden.
   1.103 +function testHideMenu()
   1.104 +{
   1.105 +  let menu = gScratchpadWindow.document.getElementById("sp-open_recent-menu");
   1.106 +  ok(menu.hasAttribute("hidden"), "The menu was hidden successfully.");
   1.107 +
   1.108 +  Services.prefs.setIntPref("devtools.scratchpad.recentFilesMax", 2);
   1.109 +}
   1.110 +
   1.111 +// We have set the recentFilesMax-pref to one (1), this enables the feature,
   1.112 +// removes the two oldest files, rebuilds the menu and removes the
   1.113 +// "hidden"-attribute from it. Test to see if this works.
   1.114 +function testChangedMaxRecent()
   1.115 +{
   1.116 +  let menu = gScratchpadWindow.document.getElementById("sp-open_recent-menu");
   1.117 +  ok(!menu.hasAttribute("hidden"), "The menu is visible. \\o/");
   1.118 +
   1.119 +  lists.recentFiles04 = gScratchpad.getRecentFiles();
   1.120 +
   1.121 +  is(lists.recentFiles04.length, 2,
   1.122 +     "Two recent files were successfully removed from the 'recent files'-list");
   1.123 +
   1.124 +  let doc = gScratchpadWindow.document;
   1.125 +  let popup = doc.getElementById("sp-menu-open_recentPopup");
   1.126 +
   1.127 +  let menuitemLabel = popup.children[0].getAttribute("label");
   1.128 +  let correctMenuItem = false;
   1.129 +  if (menuitemLabel === lists.recentFiles03[2] &&
   1.130 +      menuitemLabel === lists.recentFiles04[1]) {
   1.131 +    correctMenuItem = true;
   1.132 +  }
   1.133 +
   1.134 +  is(correctMenuItem, true,
   1.135 +     "Two recent files were successfully removed from the 'Open Recent'-menu");
   1.136 +
   1.137 +  // We now remove one file from the harddrive and use the recent-menuitem for
   1.138 +  // it to make sure the user is notified that the file no longer exists.
   1.139 +  // This is tested in testOpenDeletedFile().
   1.140 +  gFile04.remove(false);
   1.141 +
   1.142 +  // Make sure the file has been deleted before continuing to avoid
   1.143 +  // intermittent oranges.
   1.144 +  waitForFileDeletion();
   1.145 +}
   1.146 +
   1.147 +function waitForFileDeletion() {
   1.148 +  if (gFile04.exists()) {
   1.149 +    executeSoon(waitForFileDeletion);
   1.150 +    return;
   1.151 +  }
   1.152 +
   1.153 +  gFile04 = null;
   1.154 +  gScratchpad.openFile(0);
   1.155 +}
   1.156 +
   1.157 +// By now we should have two recent files stored in the list but one of the
   1.158 +// files should be missing on the harddrive.
   1.159 +function testOpenDeletedFile() {
   1.160 +  let doc = gScratchpadWindow.document;
   1.161 +  let popup = doc.getElementById("sp-menu-open_recentPopup");
   1.162 +
   1.163 +  is(gScratchpad.getRecentFiles().length, 1,
   1.164 +     "The missing file was successfully removed from the list.");
   1.165 +  // The number of recent files stored, plus the separator and the
   1.166 +  // clearRecentMenuItems-item.
   1.167 +  is(popup.children.length, 3,
   1.168 +     "The missing file was successfully removed from the menu.");
   1.169 +  ok(gScratchpad.notificationBox.currentNotification,
   1.170 +     "The notification was successfully displayed.");
   1.171 +  is(gScratchpad.notificationBox.currentNotification.label,
   1.172 +     gScratchpad.strings.GetStringFromName("fileNoLongerExists.notification"),
   1.173 +     "The notification label is correct.");
   1.174 +
   1.175 +  gScratchpad.clearRecentFiles();
   1.176 +}
   1.177 +
   1.178 +// We have cleared the last file. Test to see if the last file was removed,
   1.179 +// the menu is empty and was disabled successfully.
   1.180 +function testClearedAll()
   1.181 +{
   1.182 +  let doc = gScratchpadWindow.document;
   1.183 +  let menu = doc.getElementById("sp-open_recent-menu");
   1.184 +  let popup = doc.getElementById("sp-menu-open_recentPopup");
   1.185 +
   1.186 +  is(gScratchpad.getRecentFiles().length, 0,
   1.187 +     "All recent files removed successfully.");
   1.188 +  is(popup.children.length, 0, "All menuitems removed successfully.");
   1.189 +  ok(menu.hasAttribute("disabled"),
   1.190 +     "No files in the menu, it was disabled successfully.");
   1.191 +
   1.192 +  finishTest();
   1.193 +}
   1.194 +
   1.195 +function createAndLoadTemporaryFile(aFile, aFileName, aFileContent)
   1.196 +{
   1.197 +  // Create a temporary file.
   1.198 +  aFile = FileUtils.getFile("TmpD", [aFileName]);
   1.199 +  aFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0o666);
   1.200 +
   1.201 +  // Write the temporary file.
   1.202 +  let fout = Cc["@mozilla.org/network/file-output-stream;1"].
   1.203 +             createInstance(Ci.nsIFileOutputStream);
   1.204 +  fout.init(aFile.QueryInterface(Ci.nsILocalFile), 0x02 | 0x08 | 0x20,
   1.205 +            0o644, fout.DEFER_OPEN);
   1.206 +
   1.207 +  gScratchpad.setFilename(aFile.path);
   1.208 +  gScratchpad.importFromFile(aFile.QueryInterface(Ci.nsILocalFile),  true,
   1.209 +                            fileImported);
   1.210 +  gScratchpad.saveFile(fileSaved);
   1.211 +
   1.212 +  return aFile;
   1.213 +}
   1.214 +
   1.215 +function fileImported(aStatus)
   1.216 +{
   1.217 +  ok(Components.isSuccessCode(aStatus),
   1.218 +     "the temporary file was imported successfully with Scratchpad");
   1.219 +}
   1.220 +
   1.221 +function fileSaved(aStatus)
   1.222 +{
   1.223 +  ok(Components.isSuccessCode(aStatus),
   1.224 +     "the temporary file was saved successfully with Scratchpad");
   1.225 +
   1.226 +  checkIfMenuIsPopulated();
   1.227 +}
   1.228 +
   1.229 +function checkIfMenuIsPopulated()
   1.230 +{
   1.231 +  let doc = gScratchpadWindow.document;
   1.232 +  let expectedMenuitemCount = doc.getElementById("sp-menu-open_recentPopup").
   1.233 +                              children.length;
   1.234 +  // The number of recent files stored, plus the separator and the
   1.235 +  // clearRecentMenuItems-item.
   1.236 +  let recentFilesPlusExtra = gScratchpad.getRecentFiles().length + 2;
   1.237 +
   1.238 +  if (expectedMenuitemCount > 2) {
   1.239 +    is(expectedMenuitemCount, recentFilesPlusExtra,
   1.240 +       "the recent files menu was populated successfully.");
   1.241 +  }
   1.242 +}
   1.243 +
   1.244 +/**
   1.245 + * The PreferenceObserver listens for preference changes while Scratchpad is
   1.246 + * running.
   1.247 + */
   1.248 +var PreferenceObserver = {
   1.249 +  _initialized: false,
   1.250 +
   1.251 +  _timesFired: 0,
   1.252 +  set timesFired(aNewValue) {
   1.253 +    this._timesFired = aNewValue;
   1.254 +  },
   1.255 +  get timesFired() {
   1.256 +    return this._timesFired;
   1.257 +  },
   1.258 +
   1.259 +  init: function PO_init()
   1.260 +  {
   1.261 +    if (this._initialized) {
   1.262 +      return;
   1.263 +    }
   1.264 +
   1.265 +    this.branch = Services.prefs.getBranch("devtools.scratchpad.");
   1.266 +    this.branch.addObserver("", this, false);
   1.267 +    this._initialized = true;
   1.268 +  },
   1.269 +
   1.270 +  observe: function PO_observe(aMessage, aTopic, aData)
   1.271 +  {
   1.272 +    if (aTopic != "nsPref:changed") {
   1.273 +      return;
   1.274 +    }
   1.275 +
   1.276 +    switch (this.timesFired) {
   1.277 +      case 0:
   1.278 +        this.timesFired = 1;
   1.279 +        break;
   1.280 +      case 1:
   1.281 +        this.timesFired = 2;
   1.282 +        break;
   1.283 +      case 2:
   1.284 +        this.timesFired = 3;
   1.285 +        testAddedToRecent();
   1.286 +        break;
   1.287 +      case 3:
   1.288 +        this.timesFired = 4;
   1.289 +        testOverwriteRecent();
   1.290 +        break;
   1.291 +      case 4:
   1.292 +        this.timesFired = 5;
   1.293 +        testOpenOldestRecent();
   1.294 +        break;
   1.295 +      case 5:
   1.296 +        this.timesFired = 6;
   1.297 +        testHideMenu();
   1.298 +        break;
   1.299 +      case 6:
   1.300 +        this.timesFired = 7;
   1.301 +        testChangedMaxRecent();
   1.302 +        break;
   1.303 +      case 7:
   1.304 +        this.timesFired = 8;
   1.305 +        testOpenDeletedFile();
   1.306 +        break;
   1.307 +      case 8:
   1.308 +        this.timesFired = 9;
   1.309 +        testClearedAll();
   1.310 +        break;
   1.311 +    }
   1.312 +  },
   1.313 +
   1.314 +  uninit: function PO_uninit () {
   1.315 +    this.branch.removeObserver("", this);
   1.316 +  }
   1.317 +};
   1.318 +
   1.319 +function test()
   1.320 +{
   1.321 +  waitForExplicitFinish();
   1.322 +
   1.323 +  registerCleanupFunction(function () {
   1.324 +    gFile01.remove(false);
   1.325 +    gFile01 = null;
   1.326 +    gFile02.remove(false);
   1.327 +    gFile02 = null;
   1.328 +    gFile03.remove(false);
   1.329 +    gFile03 = null;
   1.330 +    // gFile04 was removed earlier.
   1.331 +    lists.recentFiles01 = null;
   1.332 +    lists.recentFiles02 = null;
   1.333 +    lists.recentFiles03 = null;
   1.334 +    lists.recentFiles04 = null;
   1.335 +    gScratchpad = null;
   1.336 +
   1.337 +    PreferenceObserver.uninit();
   1.338 +    Services.prefs.clearUserPref("devtools.scratchpad.recentFilesMax");
   1.339 +  });
   1.340 +
   1.341 +  Services.prefs.setIntPref("devtools.scratchpad.recentFilesMax", 3);
   1.342 +
   1.343 +  // Initiate the preference observer after we have set the temporary recent
   1.344 +  // files max for this test.
   1.345 +  PreferenceObserver.init();
   1.346 +
   1.347 +  gBrowser.selectedTab = gBrowser.addTab();
   1.348 +  gBrowser.selectedBrowser.addEventListener("load", function onLoad() {
   1.349 +    gBrowser.selectedBrowser.removeEventListener("load", onLoad, true);
   1.350 +    openScratchpad(startTest);
   1.351 +  }, true);
   1.352 +
   1.353 +  content.location = "data:text/html,<p>test recent files in Scratchpad";
   1.354 +}
   1.355 +
   1.356 +function finishTest()
   1.357 +{
   1.358 +  finish();
   1.359 +}

mercurial