Add API test to validate setting of service worker and cache storage directories
[WebKit-https.git] / LayoutTests / http / tests / cache-storage / cache-representation.https.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>Cache Storage: testing persistency</title>
5 <script src="/resources/testharness.js"></script>
6 <script src="/resources/testharnessreport.js"></script>
7 <script src="/js-test-resources/gc.js"></script>
8 </head>
9 <body>
10     <div id="check"></div>
11     <script>
12     if (window.testRunner)
13         testRunner.setPrivateBrowsingEnabled(true);
14
15     function checkCaches(hasPersistent, hasRemoved, name, value) {
16         test(() => {
17             var results = JSON.parse(value).origins;
18             assert_equals(results.length, 1);
19             var caches = results[0].caches;
20             assert_equals(!!caches["persistent"].length, hasPersistent, "persistent");
21             assert_equals(!!caches["removed"].length, hasRemoved, "removed");
22
23            if (!hasPersistent)
24                assert_equals(testRunner.domCacheSize(results.origin), 0, "non persistent cache storage size is zero");
25
26         }, name);
27     }
28
29     promise_test(test => {
30         return self.caches.keys().then(keys => {
31             var pending = [];
32             for (key of keys)
33                 pending.push(self.caches.delete(keys[0]));
34             return Promise.all(pending);
35         });
36     }, "Cleaning existing caches");
37
38     promise_test(test => {
39         if (!window.internals)
40             return Promise.reject("Test requires internals");
41
42         return new Promise((resolve, reject) => {
43             window.addEventListener("message", test.step_func((event) => {
44                 return internals.cacheStorageEngineRepresentation().then(value => {
45                     checkCaches(true, false, "A cache is created", value);
46                     resolve();
47                 });
48             }));
49             check.innerHTML = "<iframe src='resources/cache-persistency-iframe.html'></iframe>";
50         })
51     }, "Create a cache storage and look at the representation");
52
53     promise_test(test => {
54         if (!window.internals)
55             return Promise.reject("Test requires internals");
56
57         return new Promise((resolve, reject) => {
58             window.addEventListener("message", test.step_func((event) => {
59                 return internals.cacheStorageEngineRepresentation().then(value => {
60                     checkCaches(true, false, "A cache is persisting", value);
61                     resolve();
62                 });
63             }));
64             check.innerHTML = "<iframe src='resources/cache-persistency-iframe.html#check'></iframe>";
65         })
66     }, "Look at the representation a second time after removing the iframe");
67
68     promise_test(test => {
69         if (!window.internals)
70             return Promise.reject("Test requires internals");
71
72         return new Promise((resolve, reject) => {
73             window.addEventListener("message", test.step_func((event) => {
74                 return internals.cacheStorageEngineRepresentation().then(value => {
75                     checkCaches(false, true, "A cache is going in remove bucket", value);
76                     resolve();
77                 });
78             }));
79             check.innerHTML = "<iframe src='resources/cache-persistency-iframe.html#remove'></iframe>";
80         })
81     }, "Remove a cache and look at the representation a second time");
82
83     promise_test(test => {
84         if (!window.internals)
85             return Promise.reject("Test requires internals");
86
87         return new Promise((resolve, reject) => {
88             window.addEventListener("message", test.step_func((event) => {
89                 gc();
90                 setTimeout(() => {
91                     return internals.cacheStorageEngineRepresentation().then(value => {
92                         checkCaches(false, false, "A removed cache gets collected", value);
93                         resolve();
94                     });
95                 }, 0);
96             }));
97             check.innerHTML = "<iframe src='resources/cache-persistency-iframe.html#check'></iframe>";
98         });
99     }, "Look at the representation a second time after reloading the iframe");
100
101     </script>
102 </body>
103 </html>
104