Add Cache API support of records persistency
[WebKit-https.git] / LayoutTests / http / tests / cache-storage / cache-persistency.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 </head>
8 <body>
9     <div id="check"></div>
10     <script>
11     promise_test(test => {
12         return self.caches.keys().then(keys => {
13             var pending = [];
14             for (key of keys)
15                 pending.push(self.caches.delete(keys[0]));
16             return Promise.all(pending);
17         });
18     }, "Cleaning existing caches");
19
20     promise_test(test => {
21         if (!window.internals || !window.testRunner)
22             return Promise.reject("Test requires internals");
23
24         testRunner.setPrivateBrowsingEnabled(true);
25
26         return new Promise((resolve, reject) => {
27             window.addEventListener("message", test.step_func((event) => {
28                 if (event.data === "ready") {
29                     internals.clearCacheStorageMemoryRepresentation().then(() => {
30                        check.innerHTML = "<iframe src='resources/cache-persistency-iframe.html#check'></iframe>";
31                     });
32                     return;
33                 }
34                 assert_true(event.data === false, "No cache object should be found");
35                 testRunner.setPrivateBrowsingEnabled(false);
36                 resolve();
37             }));
38             check.innerHTML = "<iframe src='resources/cache-persistency-iframe.html'></iframe>";
39         })
40     }, "Clear memory representation and disable disk persistency");
41
42     promise_test(test => {
43         if (window.testRunner)
44             testRunner.setPrivateBrowsingEnabled(false);
45
46         return Promise.resolve().then(() => {
47             if (window.internals)
48                 return internals.clearCacheStorageMemoryRepresentation();
49         }).then(() => {
50             return self.caches.keys();
51         }).then(keys => {
52             var pending = [];
53             for (key of keys)
54                 pending.push(self.caches.delete(keys[0]));
55             return Promise.all(pending);
56         });
57     }, "Cleaning added caches from previous test 1");
58
59     promise_test(test => {
60         if (!window.internals)
61             return Promise.reject("Test requires internals");
62
63         return new Promise((resolve, reject) => {
64             window.addEventListener("message", test.step_func((event) => {
65                 if (event.data === "ready") {
66                     internals.clearCacheStorageMemoryRepresentation().then(() => {
67                         check.innerHTML = "<iframe src='resources/cache-persistency-iframe.html#check'></iframe>";
68                     });
69                     return;
70                 }
71                 assert_true(event.data === true, "A cache object should be found");
72                 resolve();
73             }));
74             check.innerHTML = "<iframe src='resources/cache-persistency-iframe.html'></iframe>";
75         })
76     }, "Clear memory representation but keep disk persistency");
77
78     promise_test(test => {
79         return self.caches.keys().then(keys => {
80             var pending = [];
81             for (key of keys)
82                 pending.push(self.caches.delete(keys[0]));
83             return Promise.all(pending);
84         });
85     }, "Cleaning added caches from previous test 2");
86
87     promise_test(test => {
88         if (!window.internals)
89             return Promise.reject("Test requires internals");
90
91         return Promise.all([self.caches.open("test2"), self.caches.open("test1")]).then(() => {
92             return self.caches.keys()
93         }).then(keys => {
94             assert_array_equals(keys, ["test2", "test1"]);
95         }).then(() => {
96             return internals.clearCacheStorageMemoryRepresentation();
97         }).then(() => {
98             return self.caches.keys();
99         }).then(keys => {
100             assert_array_equals(keys, ["test2", "test1"]);
101             return self.caches.delete("test2");
102         }).then(() => {
103             return internals.clearCacheStorageMemoryRepresentation();
104         }).then(() => {
105             return self.caches.keys();
106         }).then(keys => {
107             assert_array_equals(keys, ["test1"]);
108         }).then(() => {
109             return self.caches.open("test2");
110         }).then(() => {
111             return internals.clearCacheStorageMemoryRepresentation();
112         }).then(() => {
113             return self.caches.keys();
114         }).then(keys => {
115             assert_array_equals(keys, ["test1", "test2"]);
116         }).then(() => {
117             return Promise.all([self.caches.delete("test2"), self.caches.delete("test1")]);
118         }).then(() => {
119             return internals.clearCacheStorageMemoryRepresentation();
120         }).then(() => {
121             return self.caches.keys();
122         }).then(keys => {
123             assert_array_equals(keys, []);
124         });
125     }, "Adding/Removing caches and clearing in memory representation at various times");
126
127     promise_test(test => {
128         return self.caches.keys().then(keys => {
129             var pending = [];
130             for (key of keys)
131                 pending.push(self.caches.delete(keys[0]));
132             return Promise.all(pending);
133         });
134     }, "Cleaning added caches");
135     </script>
136 </body>
137 </html>
138