toolkit/components/thumbnails/test/browser_thumbnails_storage.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/thumbnails/test/browser_thumbnails_storage.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,101 @@
     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/";
     1.8 +const URL_COPY = URL + "#copy";
     1.9 +
    1.10 +XPCOMUtils.defineLazyGetter(this, "Sanitizer", function () {
    1.11 +  let tmp = {};
    1.12 +  Cc["@mozilla.org/moz/jssubscript-loader;1"]
    1.13 +    .getService(Ci.mozIJSSubScriptLoader)
    1.14 +    .loadSubScript("chrome://browser/content/sanitize.js", tmp);
    1.15 +  return tmp.Sanitizer;
    1.16 +});
    1.17 +
    1.18 +/**
    1.19 + * These tests ensure that the thumbnail storage is working as intended.
    1.20 + * Newly captured thumbnails should be saved as files and they should as well
    1.21 + * be removed when the user sanitizes their history.
    1.22 + */
    1.23 +function runTests() {
    1.24 +  dontExpireThumbnailURLs([URL, URL_COPY]);
    1.25 +
    1.26 +  yield clearHistory();
    1.27 +  yield addVisitsAndRepopulateNewTabLinks(URL, next);
    1.28 +  yield createThumbnail();
    1.29 +
    1.30 +  // Make sure Storage.copy() updates an existing file.
    1.31 +  yield PageThumbsStorage.copy(URL, URL_COPY);
    1.32 +  let copy = new FileUtils.File(PageThumbsStorage.getFilePathForURL(URL_COPY));
    1.33 +  let mtime = copy.lastModifiedTime -= 60;
    1.34 +
    1.35 +  yield PageThumbsStorage.copy(URL, URL_COPY);
    1.36 +  isnot(new FileUtils.File(PageThumbsStorage.getFilePathForURL(URL_COPY)).lastModifiedTime, mtime,
    1.37 +        "thumbnail file was updated");
    1.38 +
    1.39 +  let file = new FileUtils.File(PageThumbsStorage.getFilePathForURL(URL));
    1.40 +  let fileCopy = new FileUtils.File(PageThumbsStorage.getFilePathForURL(URL_COPY));
    1.41 +
    1.42 +  // Clear the browser history. Retry until the files are gone because Windows
    1.43 +  // locks them sometimes.
    1.44 +  while (file.exists() || fileCopy.exists()) {
    1.45 +    yield clearHistory();
    1.46 +  }
    1.47 +
    1.48 +  yield addVisitsAndRepopulateNewTabLinks(URL, next);
    1.49 +  yield createThumbnail();
    1.50 +
    1.51 +  // Clear the last 10 minutes of browsing history.
    1.52 +  yield clearHistory(true);
    1.53 +
    1.54 +  // Retry until the file is gone because Windows locks it sometimes.
    1.55 +  clearFile(file, URL);
    1.56 +}
    1.57 +
    1.58 +function clearFile(aFile, aURL) {
    1.59 +  if (aFile.exists())
    1.60 +    // Re-add our URL to the history so that history observer's onDeleteURI()
    1.61 +    // is called again.
    1.62 +    addVisits(makeURI(aURL), function() {
    1.63 +      // Try again...
    1.64 +      yield clearHistory(true);
    1.65 +      clearFile(aFile, aURL);
    1.66 +    });
    1.67 +}
    1.68 +
    1.69 +function clearHistory(aUseRange) {
    1.70 +  let s = new Sanitizer();
    1.71 +  s.prefDomain = "privacy.cpd.";
    1.72 +
    1.73 +  let prefs = gPrefService.getBranch(s.prefDomain);
    1.74 +  prefs.setBoolPref("history", true);
    1.75 +  prefs.setBoolPref("downloads", false);
    1.76 +  prefs.setBoolPref("cache", false);
    1.77 +  prefs.setBoolPref("cookies", false);
    1.78 +  prefs.setBoolPref("formdata", false);
    1.79 +  prefs.setBoolPref("offlineApps", false);
    1.80 +  prefs.setBoolPref("passwords", false);
    1.81 +  prefs.setBoolPref("sessions", false);
    1.82 +  prefs.setBoolPref("siteSettings", false);
    1.83 +
    1.84 +  if (aUseRange) {
    1.85 +    let usec = Date.now() * 1000;
    1.86 +    s.range = [usec - 10 * 60 * 1000 * 1000, usec];
    1.87 +    s.ignoreTimespan = false;
    1.88 +  }
    1.89 +
    1.90 +  s.sanitize();
    1.91 +  s.range = null;
    1.92 +  s.ignoreTimespan = true;
    1.93 +
    1.94 +  executeSoon(next);
    1.95 +}
    1.96 +
    1.97 +function createThumbnail() {
    1.98 +  addTab(URL, function () {
    1.99 +    whenFileExists(URL, function () {
   1.100 +      gBrowser.removeTab(gBrowser.selectedTab);
   1.101 +      next();
   1.102 +    });
   1.103 +  });
   1.104 +}

mercurial