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

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

mercurial