michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: const URL = "http://mochi.test:8888/migration3"; michael@0: const URL2 = URL + "#2"; michael@0: const URL3 = URL + "#3"; michael@0: const THUMBNAIL_DIRECTORY = "thumbnails"; michael@0: const PREF_STORAGE_VERSION = "browser.pagethumbnails.storage_version"; michael@0: michael@0: let tmp = {}; michael@0: Cc["@mozilla.org/moz/jssubscript-loader;1"] michael@0: .getService(Ci.mozIJSSubScriptLoader) michael@0: .loadSubScript("resource://gre/modules/PageThumbs.jsm", tmp); michael@0: let {PageThumbsStorageMigrator} = tmp; michael@0: michael@0: XPCOMUtils.defineLazyServiceGetter(this, "gDirSvc", michael@0: "@mozilla.org/file/directory_service;1", "nsIProperties"); michael@0: michael@0: /** michael@0: * This test makes sure we correctly migrate to thumbnail storage version 3. michael@0: * This means copying existing thumbnails from the roaming to the local profile michael@0: * directory and should just apply to Linux. michael@0: */ michael@0: function runTests() { michael@0: let dirSvc = Cc["@mozilla.org/file/directory_service;1"] michael@0: .getService(Ci.nsIProperties); michael@0: michael@0: // Prepare a local profile directory. michael@0: let localProfile = FileUtils.getDir("ProfD", ["local-test"], true); michael@0: changeLocation("ProfLD", localProfile); michael@0: michael@0: let local = FileUtils.getDir("ProfLD", [THUMBNAIL_DIRECTORY], true); michael@0: let roaming = FileUtils.getDir("ProfD", [THUMBNAIL_DIRECTORY], true); michael@0: michael@0: // Set up some data in the roaming profile. michael@0: let name = PageThumbsStorage.getLeafNameForURL(URL); michael@0: let file = FileUtils.getFile("ProfD", [THUMBNAIL_DIRECTORY, name]); michael@0: writeDummyFile(file); michael@0: michael@0: name = PageThumbsStorage.getLeafNameForURL(URL2); michael@0: file = FileUtils.getFile("ProfD", [THUMBNAIL_DIRECTORY, name]); michael@0: writeDummyFile(file); michael@0: michael@0: name = PageThumbsStorage.getLeafNameForURL(URL3); michael@0: file = FileUtils.getFile("ProfD", [THUMBNAIL_DIRECTORY, name]); michael@0: writeDummyFile(file); michael@0: michael@0: // Pretend to have one of the thumbnails michael@0: // already in place at the new storage site. michael@0: name = PageThumbsStorage.getLeafNameForURL(URL3); michael@0: file = FileUtils.getFile("ProfLD", [THUMBNAIL_DIRECTORY, name]); michael@0: writeDummyFile(file, "no-overwrite-plz"); michael@0: michael@0: // Kick off thumbnail storage migration. michael@0: PageThumbsStorageMigrator.migrateToVersion3(localProfile.path); michael@0: ok(true, "migration finished"); michael@0: michael@0: // Wait until the first thumbnail was moved to its new location. michael@0: yield whenFileExists(URL); michael@0: ok(true, "first thumbnail moved"); michael@0: michael@0: // Wait for the second thumbnail to be moved as well. michael@0: yield whenFileExists(URL2); michael@0: ok(true, "second thumbnail moved"); michael@0: michael@0: yield whenFileRemoved(roaming); michael@0: ok(true, "roaming thumbnail directory removed"); michael@0: michael@0: // Check that our existing thumbnail wasn't overwritten. michael@0: is(getFileContents(file), "no-overwrite-plz", michael@0: "existing thumbnail was not overwritten"); michael@0: michael@0: // Sanity check: ensure that, until it is removed, deprecated michael@0: // function |getFileForURL| points to the same path as michael@0: // |getFilePathForURL|. michael@0: if ("getFileForURL" in PageThumbsStorage) { michael@0: let file = PageThumbsStorage.getFileForURL(URL); michael@0: is(file.path, PageThumbsStorage.getFilePathForURL(URL), michael@0: "Deprecated getFileForURL and getFilePathForURL return the same path"); michael@0: } michael@0: } michael@0: michael@0: function changeLocation(aLocation, aNewDir) { michael@0: let oldDir = gDirSvc.get(aLocation, Ci.nsILocalFile); michael@0: gDirSvc.undefine(aLocation); michael@0: gDirSvc.set(aLocation, aNewDir); michael@0: michael@0: registerCleanupFunction(function () { michael@0: gDirSvc.undefine(aLocation); michael@0: gDirSvc.set(aLocation, oldDir); michael@0: }); michael@0: } michael@0: michael@0: function writeDummyFile(aFile, aContents) { michael@0: let fos = FileUtils.openSafeFileOutputStream(aFile); michael@0: let data = aContents || "dummy"; michael@0: fos.write(data, data.length); michael@0: FileUtils.closeSafeFileOutputStream(fos); michael@0: } michael@0: michael@0: function getFileContents(aFile) { michael@0: let istream = Cc["@mozilla.org/network/file-input-stream;1"] michael@0: .createInstance(Ci.nsIFileInputStream); michael@0: istream.init(aFile, FileUtils.MODE_RDONLY, FileUtils.PERMS_FILE, 0); michael@0: return NetUtil.readInputStreamToString(istream, istream.available()); michael@0: }