toolkit/components/thumbnails/test/browser_thumbnails_expiration.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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/?t=" + Date.now();
michael@0 5 const URL1 = URL + "#1";
michael@0 6 const URL2 = URL + "#2";
michael@0 7 const URL3 = URL + "#3";
michael@0 8
michael@0 9 let tmp = {};
michael@0 10 Cc["@mozilla.org/moz/jssubscript-loader;1"]
michael@0 11 .getService(Ci.mozIJSSubScriptLoader)
michael@0 12 .loadSubScript("resource://gre/modules/PageThumbs.jsm", tmp);
michael@0 13
michael@0 14 const {EXPIRATION_MIN_CHUNK_SIZE, PageThumbsExpiration} = tmp;
michael@0 15
michael@0 16 function runTests() {
michael@0 17 // Create dummy URLs.
michael@0 18 let dummyURLs = [];
michael@0 19 for (let i = 0; i < EXPIRATION_MIN_CHUNK_SIZE + 10; i++) {
michael@0 20 dummyURLs.push(URL + "#dummy" + i);
michael@0 21 }
michael@0 22
michael@0 23 // Make sure our thumbnails aren't expired too early.
michael@0 24 dontExpireThumbnailURLs([URL1, URL2, URL3].concat(dummyURLs));
michael@0 25
michael@0 26 // Create three thumbnails.
michael@0 27 yield createDummyThumbnail(URL1);
michael@0 28 ok(thumbnailExists(URL1), "first thumbnail created");
michael@0 29
michael@0 30 yield createDummyThumbnail(URL2);
michael@0 31 ok(thumbnailExists(URL2), "second thumbnail created");
michael@0 32
michael@0 33 yield createDummyThumbnail(URL3);
michael@0 34 ok(thumbnailExists(URL3), "third thumbnail created");
michael@0 35
michael@0 36 // Remove the third thumbnail.
michael@0 37 yield expireThumbnails([URL1, URL2]);
michael@0 38 ok(thumbnailExists(URL1), "first thumbnail still exists");
michael@0 39 ok(thumbnailExists(URL2), "second thumbnail still exists");
michael@0 40 ok(!thumbnailExists(URL3), "third thumbnail has been removed");
michael@0 41
michael@0 42 // Remove the second thumbnail.
michael@0 43 yield expireThumbnails([URL1]);
michael@0 44 ok(thumbnailExists(URL1), "first thumbnail still exists");
michael@0 45 ok(!thumbnailExists(URL2), "second thumbnail has been removed");
michael@0 46
michael@0 47 // Remove all thumbnails.
michael@0 48 yield expireThumbnails([]);
michael@0 49 ok(!thumbnailExists(URL1), "all thumbnails have been removed");
michael@0 50
michael@0 51 // Create some more files than the min chunk size.
michael@0 52 for (let url of dummyURLs) {
michael@0 53 yield createDummyThumbnail(url);
michael@0 54 }
michael@0 55
michael@0 56 ok(dummyURLs.every(thumbnailExists), "all dummy thumbnails created");
michael@0 57
michael@0 58 // Expire thumbnails and expect 10 remaining.
michael@0 59 yield expireThumbnails([]);
michael@0 60 let remainingURLs = [u for (u of dummyURLs) if (thumbnailExists(u))];
michael@0 61 is(remainingURLs.length, 10, "10 dummy thumbnails remaining");
michael@0 62
michael@0 63 // Expire thumbnails again. All should be gone by now.
michael@0 64 yield expireThumbnails([]);
michael@0 65 remainingURLs = [u for (u of remainingURLs) if (thumbnailExists(u))];
michael@0 66 is(remainingURLs.length, 0, "no dummy thumbnails remaining");
michael@0 67 }
michael@0 68
michael@0 69 function createDummyThumbnail(aURL) {
michael@0 70 info("Creating dummy thumbnail for " + aURL);
michael@0 71 let dummy = new Uint8Array(10);
michael@0 72 for (let i = 0; i < 10; ++i) {
michael@0 73 dummy[i] = i;
michael@0 74 }
michael@0 75 PageThumbsStorage.writeData(aURL, dummy).then(
michael@0 76 function onSuccess() {
michael@0 77 info("createDummyThumbnail succeeded");
michael@0 78 executeSoon(next);
michael@0 79 },
michael@0 80 function onFailure(error) {
michael@0 81 ok(false, "createDummyThumbnail failed " + error);
michael@0 82 }
michael@0 83 );
michael@0 84 }
michael@0 85
michael@0 86 function expireThumbnails(aKeep) {
michael@0 87 PageThumbsExpiration.expireThumbnails(aKeep).then(
michael@0 88 function onSuccess() {
michael@0 89 info("expireThumbnails succeeded");
michael@0 90 executeSoon(next);
michael@0 91 },
michael@0 92 function onFailure(error) {
michael@0 93 ok(false, "expireThumbnails failed " + error);
michael@0 94 }
michael@0 95 );
michael@0 96 }

mercurial