toolkit/components/thumbnails/test/browser_thumbnails_storage_migrate3.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:958b64254ff9
1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
3
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";
9
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;
15
16 XPCOMUtils.defineLazyServiceGetter(this, "gDirSvc",
17 "@mozilla.org/file/directory_service;1", "nsIProperties");
18
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);
27
28 // Prepare a local profile directory.
29 let localProfile = FileUtils.getDir("ProfD", ["local-test"], true);
30 changeLocation("ProfLD", localProfile);
31
32 let local = FileUtils.getDir("ProfLD", [THUMBNAIL_DIRECTORY], true);
33 let roaming = FileUtils.getDir("ProfD", [THUMBNAIL_DIRECTORY], true);
34
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);
39
40 name = PageThumbsStorage.getLeafNameForURL(URL2);
41 file = FileUtils.getFile("ProfD", [THUMBNAIL_DIRECTORY, name]);
42 writeDummyFile(file);
43
44 name = PageThumbsStorage.getLeafNameForURL(URL3);
45 file = FileUtils.getFile("ProfD", [THUMBNAIL_DIRECTORY, name]);
46 writeDummyFile(file);
47
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");
53
54 // Kick off thumbnail storage migration.
55 PageThumbsStorageMigrator.migrateToVersion3(localProfile.path);
56 ok(true, "migration finished");
57
58 // Wait until the first thumbnail was moved to its new location.
59 yield whenFileExists(URL);
60 ok(true, "first thumbnail moved");
61
62 // Wait for the second thumbnail to be moved as well.
63 yield whenFileExists(URL2);
64 ok(true, "second thumbnail moved");
65
66 yield whenFileRemoved(roaming);
67 ok(true, "roaming thumbnail directory removed");
68
69 // Check that our existing thumbnail wasn't overwritten.
70 is(getFileContents(file), "no-overwrite-plz",
71 "existing thumbnail was not overwritten");
72
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 }
82
83 function changeLocation(aLocation, aNewDir) {
84 let oldDir = gDirSvc.get(aLocation, Ci.nsILocalFile);
85 gDirSvc.undefine(aLocation);
86 gDirSvc.set(aLocation, aNewDir);
87
88 registerCleanupFunction(function () {
89 gDirSvc.undefine(aLocation);
90 gDirSvc.set(aLocation, oldDir);
91 });
92 }
93
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 }
100
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