toolkit/components/thumbnails/test/browser_thumbnails_storage_migrate3.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* Any copyright is dedicated to the Public Domain.
     2    http://creativecommons.org/publicdomain/zero/1.0/ */
     4 const URL = "http://mochi.test:8888/migration3";
     5 const URL2 = URL + "#2";
     6 const URL3 = URL + "#3";
     7 const THUMBNAIL_DIRECTORY = "thumbnails";
     8 const PREF_STORAGE_VERSION = "browser.pagethumbnails.storage_version";
    10 let tmp = {};
    11 Cc["@mozilla.org/moz/jssubscript-loader;1"]
    12   .getService(Ci.mozIJSSubScriptLoader)
    13   .loadSubScript("resource://gre/modules/PageThumbs.jsm", tmp);
    14 let {PageThumbsStorageMigrator} = tmp;
    16 XPCOMUtils.defineLazyServiceGetter(this, "gDirSvc",
    17   "@mozilla.org/file/directory_service;1", "nsIProperties");
    19 /**
    20  * This test makes sure we correctly migrate to thumbnail storage version 3.
    21  * This means copying existing thumbnails from the roaming to the local profile
    22  * directory and should just apply to Linux.
    23  */
    24 function runTests() {
    25   let dirSvc = Cc["@mozilla.org/file/directory_service;1"]
    26                  .getService(Ci.nsIProperties);
    28   // Prepare a local profile directory.
    29   let localProfile = FileUtils.getDir("ProfD", ["local-test"], true);
    30   changeLocation("ProfLD", localProfile);
    32   let local = FileUtils.getDir("ProfLD", [THUMBNAIL_DIRECTORY], true);
    33   let roaming = FileUtils.getDir("ProfD", [THUMBNAIL_DIRECTORY], true);
    35   // Set up some data in the roaming profile.
    36   let name = PageThumbsStorage.getLeafNameForURL(URL);
    37   let file = FileUtils.getFile("ProfD", [THUMBNAIL_DIRECTORY, name]);
    38   writeDummyFile(file);
    40   name = PageThumbsStorage.getLeafNameForURL(URL2);
    41   file = FileUtils.getFile("ProfD", [THUMBNAIL_DIRECTORY, name]);
    42   writeDummyFile(file);
    44   name = PageThumbsStorage.getLeafNameForURL(URL3);
    45   file = FileUtils.getFile("ProfD", [THUMBNAIL_DIRECTORY, name]);
    46   writeDummyFile(file);
    48   // Pretend to have one of the thumbnails
    49   // already in place at the new storage site.
    50   name = PageThumbsStorage.getLeafNameForURL(URL3);
    51   file = FileUtils.getFile("ProfLD", [THUMBNAIL_DIRECTORY, name]);
    52   writeDummyFile(file, "no-overwrite-plz");
    54   // Kick off thumbnail storage migration.
    55   PageThumbsStorageMigrator.migrateToVersion3(localProfile.path);
    56   ok(true, "migration finished");
    58   // Wait until the first thumbnail was moved to its new location.
    59   yield whenFileExists(URL);
    60   ok(true, "first thumbnail moved");
    62   // Wait for the second thumbnail to be moved as well.
    63   yield whenFileExists(URL2);
    64   ok(true, "second thumbnail moved");
    66   yield whenFileRemoved(roaming);
    67   ok(true, "roaming thumbnail directory removed");
    69   // Check that our existing thumbnail wasn't overwritten.
    70   is(getFileContents(file), "no-overwrite-plz",
    71     "existing thumbnail was not overwritten");
    73   // Sanity check: ensure that, until it is removed, deprecated
    74   // function |getFileForURL| points to the same path as
    75   // |getFilePathForURL|.
    76   if ("getFileForURL" in PageThumbsStorage) {
    77     let file = PageThumbsStorage.getFileForURL(URL);
    78     is(file.path, PageThumbsStorage.getFilePathForURL(URL),
    79        "Deprecated getFileForURL and getFilePathForURL return the same path");
    80   }
    81 }
    83 function changeLocation(aLocation, aNewDir) {
    84   let oldDir = gDirSvc.get(aLocation, Ci.nsILocalFile);
    85   gDirSvc.undefine(aLocation);
    86   gDirSvc.set(aLocation, aNewDir);
    88   registerCleanupFunction(function () {
    89     gDirSvc.undefine(aLocation);
    90     gDirSvc.set(aLocation, oldDir);
    91   });
    92 }
    94 function writeDummyFile(aFile, aContents) {
    95   let fos = FileUtils.openSafeFileOutputStream(aFile);
    96   let data = aContents || "dummy";
    97   fos.write(data, data.length);
    98   FileUtils.closeSafeFileOutputStream(fos);
    99 }
   101 function getFileContents(aFile) {
   102   let istream = Cc["@mozilla.org/network/file-input-stream;1"]
   103                   .createInstance(Ci.nsIFileInputStream);
   104   istream.init(aFile, FileUtils.MODE_RDONLY, FileUtils.PERMS_FILE, 0);
   105   return NetUtil.readInputStreamToString(istream, istream.available());
   106 }

mercurial