1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/thumbnails/test/browser_thumbnails_expiration.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,96 @@ 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/?t=" + Date.now(); 1.8 +const URL1 = URL + "#1"; 1.9 +const URL2 = URL + "#2"; 1.10 +const URL3 = URL + "#3"; 1.11 + 1.12 +let tmp = {}; 1.13 +Cc["@mozilla.org/moz/jssubscript-loader;1"] 1.14 + .getService(Ci.mozIJSSubScriptLoader) 1.15 + .loadSubScript("resource://gre/modules/PageThumbs.jsm", tmp); 1.16 + 1.17 +const {EXPIRATION_MIN_CHUNK_SIZE, PageThumbsExpiration} = tmp; 1.18 + 1.19 +function runTests() { 1.20 + // Create dummy URLs. 1.21 + let dummyURLs = []; 1.22 + for (let i = 0; i < EXPIRATION_MIN_CHUNK_SIZE + 10; i++) { 1.23 + dummyURLs.push(URL + "#dummy" + i); 1.24 + } 1.25 + 1.26 + // Make sure our thumbnails aren't expired too early. 1.27 + dontExpireThumbnailURLs([URL1, URL2, URL3].concat(dummyURLs)); 1.28 + 1.29 + // Create three thumbnails. 1.30 + yield createDummyThumbnail(URL1); 1.31 + ok(thumbnailExists(URL1), "first thumbnail created"); 1.32 + 1.33 + yield createDummyThumbnail(URL2); 1.34 + ok(thumbnailExists(URL2), "second thumbnail created"); 1.35 + 1.36 + yield createDummyThumbnail(URL3); 1.37 + ok(thumbnailExists(URL3), "third thumbnail created"); 1.38 + 1.39 + // Remove the third thumbnail. 1.40 + yield expireThumbnails([URL1, URL2]); 1.41 + ok(thumbnailExists(URL1), "first thumbnail still exists"); 1.42 + ok(thumbnailExists(URL2), "second thumbnail still exists"); 1.43 + ok(!thumbnailExists(URL3), "third thumbnail has been removed"); 1.44 + 1.45 + // Remove the second thumbnail. 1.46 + yield expireThumbnails([URL1]); 1.47 + ok(thumbnailExists(URL1), "first thumbnail still exists"); 1.48 + ok(!thumbnailExists(URL2), "second thumbnail has been removed"); 1.49 + 1.50 + // Remove all thumbnails. 1.51 + yield expireThumbnails([]); 1.52 + ok(!thumbnailExists(URL1), "all thumbnails have been removed"); 1.53 + 1.54 + // Create some more files than the min chunk size. 1.55 + for (let url of dummyURLs) { 1.56 + yield createDummyThumbnail(url); 1.57 + } 1.58 + 1.59 + ok(dummyURLs.every(thumbnailExists), "all dummy thumbnails created"); 1.60 + 1.61 + // Expire thumbnails and expect 10 remaining. 1.62 + yield expireThumbnails([]); 1.63 + let remainingURLs = [u for (u of dummyURLs) if (thumbnailExists(u))]; 1.64 + is(remainingURLs.length, 10, "10 dummy thumbnails remaining"); 1.65 + 1.66 + // Expire thumbnails again. All should be gone by now. 1.67 + yield expireThumbnails([]); 1.68 + remainingURLs = [u for (u of remainingURLs) if (thumbnailExists(u))]; 1.69 + is(remainingURLs.length, 0, "no dummy thumbnails remaining"); 1.70 +} 1.71 + 1.72 +function createDummyThumbnail(aURL) { 1.73 + info("Creating dummy thumbnail for " + aURL); 1.74 + let dummy = new Uint8Array(10); 1.75 + for (let i = 0; i < 10; ++i) { 1.76 + dummy[i] = i; 1.77 + } 1.78 + PageThumbsStorage.writeData(aURL, dummy).then( 1.79 + function onSuccess() { 1.80 + info("createDummyThumbnail succeeded"); 1.81 + executeSoon(next); 1.82 + }, 1.83 + function onFailure(error) { 1.84 + ok(false, "createDummyThumbnail failed " + error); 1.85 + } 1.86 + ); 1.87 +} 1.88 + 1.89 +function expireThumbnails(aKeep) { 1.90 + PageThumbsExpiration.expireThumbnails(aKeep).then( 1.91 + function onSuccess() { 1.92 + info("expireThumbnails succeeded"); 1.93 + executeSoon(next); 1.94 + }, 1.95 + function onFailure(error) { 1.96 + ok(false, "expireThumbnails failed " + error); 1.97 + } 1.98 + ); 1.99 +}