toolkit/components/thumbnails/test/browser_thumbnails_expiration.js

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

mercurial