1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/thumbnails/test/browser_thumbnails_storage_migrate3.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,106 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +const URL = "http://mochi.test:8888/migration3"; 1.8 +const URL2 = URL + "#2"; 1.9 +const URL3 = URL + "#3"; 1.10 +const THUMBNAIL_DIRECTORY = "thumbnails"; 1.11 +const PREF_STORAGE_VERSION = "browser.pagethumbnails.storage_version"; 1.12 + 1.13 +let tmp = {}; 1.14 +Cc["@mozilla.org/moz/jssubscript-loader;1"] 1.15 + .getService(Ci.mozIJSSubScriptLoader) 1.16 + .loadSubScript("resource://gre/modules/PageThumbs.jsm", tmp); 1.17 +let {PageThumbsStorageMigrator} = tmp; 1.18 + 1.19 +XPCOMUtils.defineLazyServiceGetter(this, "gDirSvc", 1.20 + "@mozilla.org/file/directory_service;1", "nsIProperties"); 1.21 + 1.22 +/** 1.23 + * This test makes sure we correctly migrate to thumbnail storage version 3. 1.24 + * This means copying existing thumbnails from the roaming to the local profile 1.25 + * directory and should just apply to Linux. 1.26 + */ 1.27 +function runTests() { 1.28 + let dirSvc = Cc["@mozilla.org/file/directory_service;1"] 1.29 + .getService(Ci.nsIProperties); 1.30 + 1.31 + // Prepare a local profile directory. 1.32 + let localProfile = FileUtils.getDir("ProfD", ["local-test"], true); 1.33 + changeLocation("ProfLD", localProfile); 1.34 + 1.35 + let local = FileUtils.getDir("ProfLD", [THUMBNAIL_DIRECTORY], true); 1.36 + let roaming = FileUtils.getDir("ProfD", [THUMBNAIL_DIRECTORY], true); 1.37 + 1.38 + // Set up some data in the roaming profile. 1.39 + let name = PageThumbsStorage.getLeafNameForURL(URL); 1.40 + let file = FileUtils.getFile("ProfD", [THUMBNAIL_DIRECTORY, name]); 1.41 + writeDummyFile(file); 1.42 + 1.43 + name = PageThumbsStorage.getLeafNameForURL(URL2); 1.44 + file = FileUtils.getFile("ProfD", [THUMBNAIL_DIRECTORY, name]); 1.45 + writeDummyFile(file); 1.46 + 1.47 + name = PageThumbsStorage.getLeafNameForURL(URL3); 1.48 + file = FileUtils.getFile("ProfD", [THUMBNAIL_DIRECTORY, name]); 1.49 + writeDummyFile(file); 1.50 + 1.51 + // Pretend to have one of the thumbnails 1.52 + // already in place at the new storage site. 1.53 + name = PageThumbsStorage.getLeafNameForURL(URL3); 1.54 + file = FileUtils.getFile("ProfLD", [THUMBNAIL_DIRECTORY, name]); 1.55 + writeDummyFile(file, "no-overwrite-plz"); 1.56 + 1.57 + // Kick off thumbnail storage migration. 1.58 + PageThumbsStorageMigrator.migrateToVersion3(localProfile.path); 1.59 + ok(true, "migration finished"); 1.60 + 1.61 + // Wait until the first thumbnail was moved to its new location. 1.62 + yield whenFileExists(URL); 1.63 + ok(true, "first thumbnail moved"); 1.64 + 1.65 + // Wait for the second thumbnail to be moved as well. 1.66 + yield whenFileExists(URL2); 1.67 + ok(true, "second thumbnail moved"); 1.68 + 1.69 + yield whenFileRemoved(roaming); 1.70 + ok(true, "roaming thumbnail directory removed"); 1.71 + 1.72 + // Check that our existing thumbnail wasn't overwritten. 1.73 + is(getFileContents(file), "no-overwrite-plz", 1.74 + "existing thumbnail was not overwritten"); 1.75 + 1.76 + // Sanity check: ensure that, until it is removed, deprecated 1.77 + // function |getFileForURL| points to the same path as 1.78 + // |getFilePathForURL|. 1.79 + if ("getFileForURL" in PageThumbsStorage) { 1.80 + let file = PageThumbsStorage.getFileForURL(URL); 1.81 + is(file.path, PageThumbsStorage.getFilePathForURL(URL), 1.82 + "Deprecated getFileForURL and getFilePathForURL return the same path"); 1.83 + } 1.84 +} 1.85 + 1.86 +function changeLocation(aLocation, aNewDir) { 1.87 + let oldDir = gDirSvc.get(aLocation, Ci.nsILocalFile); 1.88 + gDirSvc.undefine(aLocation); 1.89 + gDirSvc.set(aLocation, aNewDir); 1.90 + 1.91 + registerCleanupFunction(function () { 1.92 + gDirSvc.undefine(aLocation); 1.93 + gDirSvc.set(aLocation, oldDir); 1.94 + }); 1.95 +} 1.96 + 1.97 +function writeDummyFile(aFile, aContents) { 1.98 + let fos = FileUtils.openSafeFileOutputStream(aFile); 1.99 + let data = aContents || "dummy"; 1.100 + fos.write(data, data.length); 1.101 + FileUtils.closeSafeFileOutputStream(fos); 1.102 +} 1.103 + 1.104 +function getFileContents(aFile) { 1.105 + let istream = Cc["@mozilla.org/network/file-input-stream;1"] 1.106 + .createInstance(Ci.nsIFileInputStream); 1.107 + istream.init(aFile, FileUtils.MODE_RDONLY, FileUtils.PERMS_FILE, 0); 1.108 + return NetUtil.readInputStreamToString(istream, istream.available()); 1.109 +}