Bug 794804 - part 2 - add tests for thumbnails expiration; r=yoric
authorTim Taubert <ttaubert@mozilla.com>
Fri, 28 Sep 2012 10:42:32 +0200
changeset 108501 dd150b8dc4dc864e504887d542be9e9543b1b29b
parent 108500 da72409eda0eb702c82e443abf6906e2b5024fb3
child 108502 4a87ede182ca81e35d0a39f59595a960a0975683
push id82
push usershu@rfrn.org
push dateFri, 05 Oct 2012 13:20:22 +0000
reviewersyoric
bugs794804
milestone18.0a1
Bug 794804 - part 2 - add tests for thumbnails expiration; r=yoric
browser/components/thumbnails/PageThumbs.jsm
browser/components/thumbnails/test/Makefile.in
browser/components/thumbnails/test/browser_thumbnails_expiration.js
browser/components/thumbnails/test/browser_thumbnails_storage.js
browser/components/thumbnails/test/head.js
--- a/browser/components/thumbnails/PageThumbs.jsm
+++ b/browser/components/thumbnails/PageThumbs.jsm
@@ -440,23 +440,23 @@ let PageThumbsExpiration = {
     for (let filter of this._filters) {
       if (typeof filter == "function")
         filter(filterCallback)
       else
         filter.filterForThumbnailExpiration(filterCallback);
     }
   },
 
-  expireThumbnails: function Expiration_expireThumbnails(aURLsToKeep) {
+  expireThumbnails: function Expiration_expireThumbnails(aURLsToKeep, aCallback) {
     PageThumbsWorker.postMessage({
       type: "expireFilesInDirectory",
       minChunkSize: EXPIRATION_MIN_CHUNK_SIZE,
       path: PageThumbsStorage.getDirectory().path,
       filesToKeep: [PageThumbsStorage.getLeafNameForURL(url) for (url of aURLsToKeep)]
-    });
+    }, aCallback);
   }
 };
 
 /**
  * Interface to a dedicated thread handling I/O
  */
 let PageThumbsWorker = {
   /**
--- a/browser/components/thumbnails/test/Makefile.in
+++ b/browser/components/thumbnails/test/Makefile.in
@@ -8,16 +8,17 @@ srcdir		= @srcdir@
 VPATH		= @srcdir@
 relativesrcdir  = @relativesrcdir@
 
 include $(DEPTH)/config/autoconf.mk
 include $(topsrcdir)/config/rules.mk
 
 _BROWSER_FILES = \
 	browser_thumbnails_capture.js \
+	browser_thumbnails_expiration.js \
 	browser_thumbnails_privacy.js \
 	browser_thumbnails_redirect.js \
 	browser_thumbnails_storage.js \
 	browser_thumbnails_bug726727.js \
 	head.js \
 	background_red.html \
 	background_red_redirect.sjs \
 	privacy_cache_control.sjs \
new file mode 100644
--- /dev/null
+++ b/browser/components/thumbnails/test/browser_thumbnails_expiration.js
@@ -0,0 +1,78 @@
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+const URL = "http://mochi.test:8888/?t=" + Date.now();
+const URL1 = URL + "#1";
+const URL2 = URL + "#2";
+const URL3 = URL + "#3";
+
+XPCOMUtils.defineLazyModuleGetter(this, "FileUtils",
+  "resource://gre/modules/FileUtils.jsm");
+
+let tmp = {};
+Cc["@mozilla.org/moz/jssubscript-loader;1"]
+  .getService(Ci.mozIJSSubScriptLoader)
+  .loadSubScript("resource:///modules/PageThumbs.jsm", tmp);
+
+const {EXPIRATION_MIN_CHUNK_SIZE, PageThumbsExpiration} = tmp;
+
+function runTests() {
+  // Create three thumbnails.
+  createDummyThumbnail(URL1);
+  ok(thumbnailExists(URL1), "first thumbnail created");
+
+  createDummyThumbnail(URL2);
+  ok(thumbnailExists(URL2), "second thumbnail created");
+
+  createDummyThumbnail(URL3);
+  ok(thumbnailExists(URL3), "third thumbnail created");
+
+  // Remove the third thumbnail.
+  yield expireThumbnails([URL1, URL2]);
+  ok(thumbnailExists(URL1), "first thumbnail still exists");
+  ok(thumbnailExists(URL2), "second thumbnail still exists");
+  ok(!thumbnailExists(URL3), "third thumbnail has been removed");
+
+  // Remove the second thumbnail.
+  yield expireThumbnails([URL1]);
+  ok(thumbnailExists(URL1), "first thumbnail still exists");
+  ok(!thumbnailExists(URL2), "second thumbnail has been removed");
+
+  // Remove all thumbnails.
+  yield expireThumbnails([]);
+  ok(!thumbnailExists(URL1), "all thumbnails have been removed");
+
+  // Create some more files than the min chunk size.
+  let urls = [];
+  for (let i = 0; i < EXPIRATION_MIN_CHUNK_SIZE + 10; i++) {
+    urls.push(URL + "#dummy" + i);
+  }
+
+  urls.forEach(createDummyThumbnail);
+  ok(urls.every(thumbnailExists), "all dummy thumbnails created");
+
+  // Expire thumbnails and expect 10 remaining.
+  yield expireThumbnails([]);
+  let remainingURLs = [u for (u of urls) if (thumbnailExists(u))];
+  is(remainingURLs.length, 10, "10 dummy thumbnails remaining");
+
+  // Expire thumbnails again. All should be gone by now.
+  yield expireThumbnails([]);
+  remainingURLs = [u for (u of remainingURLs) if (thumbnailExists(u))];
+  is(remainingURLs.length, 0, "no dummy thumbnails remaining");
+}
+
+function createDummyThumbnail(aURL) {
+  let file = PageThumbsStorage.getFileForURL(aURL);
+  let fos = FileUtils.openSafeFileOutputStream(file);
+
+  let data = "dummy";
+  fos.write(data, data.length);
+  FileUtils.closeSafeFileOutputStream(fos);
+}
+
+function expireThumbnails(aKeep) {
+  PageThumbsExpiration.expireThumbnails(aKeep, function () {
+    executeSoon(next);
+  });
+}
--- a/browser/components/thumbnails/test/browser_thumbnails_storage.js
+++ b/browser/components/thumbnails/test/browser_thumbnails_storage.js
@@ -91,17 +91,16 @@ function createThumbnail() {
       gBrowser.removeTab(gBrowser.selectedTab);
       next();
     });
   });
 }
 
 function whenFileExists(aCallback) {
   let callback;
-  let file = PageThumbsStorage.getFileForURL(URL);
-  if (file.exists() && file.fileSize) {
+  if (thumbnailExists(URL)) {
     callback = aCallback;
   } else {
     callback = function () whenFileExists(aCallback);
   }
 
   executeSoon(callback);
 }
--- a/browser/components/thumbnails/test/head.js
+++ b/browser/components/thumbnails/test/head.js
@@ -141,8 +141,17 @@ function checkThumbnailColor(aURL, aRed,
  * @param aGreen The green component's intensity.
  * @param aBlue The blue component's intensity.
  * @param aMessage The info message to print when comparing the pixel color.
  */
 function checkCanvasColor(aContext, aRed, aGreen, aBlue, aMessage) {
   let [r, g, b] = aContext.getImageData(0, 0, 1, 1).data;
   ok(r == aRed && g == aGreen && b == aBlue, aMessage);
 }
+
+/**
+ * Checks if a thumbnail for the given URL exists.
+ * @param aURL The url associated to the thumbnail.
+ */
+function thumbnailExists(aURL) {
+  let file = PageThumbsStorage.getFileForURL(aURL);
+  return file.exists() && file.fileSize;
+}