<!DOCTYPE html>
<html>
<head>
<title>Cache Storage: testing persistency</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
    <div id="check"></div>
    <script>
    promise_test(test => {
        return self.caches.keys().then(keys => {
            var pending = [];
            for (key of keys)
                pending.push(self.caches.delete(keys[0]));
            return Promise.all(pending);
        });
    }, "Cleaning existing caches");

    promise_test(test => {
        if (!window.internals)
            return Promise.reject("Test requires internals");

        return new Promise((resolve, reject) => {
            window.addEventListener("message", test.step_func((event) => {
                if (event.data === "ready") {
                    internals.clearCacheStorageMemoryRepresentation().then(() => {
                        check.innerHTML = "<iframe src='resources/cache-persistency-iframe.html#check'></iframe>";
                    });
                    return;
                }
                assert_true(event.data === true, "A cache object should be found");
                resolve();
            }));
            check.innerHTML = "<iframe src='resources/cache-persistency-iframe.html'></iframe>";
        })
    }, "Clear memory representation but keep disk persistency");

    promise_test(test => {
        return self.caches.keys().then(keys => {
            var pending = [];
            for (key of keys)
                pending.push(self.caches.delete(keys[0]));
            return Promise.all(pending);
        });
    }, "Cleaning added caches from previous test");

    promise_test(test => {
        if (!window.internals)
            return Promise.reject("Test requires internals");

        return Promise.all([self.caches.open("test2"), self.caches.open("test1")]).then(() => {
            return self.caches.keys()
        }).then(keys => {
            assert_array_equals(keys, ["test2", "test1"]);
        }).then(() => {
            return internals.clearCacheStorageMemoryRepresentation();
        }).then(() => {
            return self.caches.keys();
        }).then(keys => {
            assert_array_equals(keys, ["test2", "test1"]);
            return self.caches.delete("test2");
        }).then(() => {
            return internals.clearCacheStorageMemoryRepresentation();
        }).then(() => {
            return self.caches.keys();
        }).then(keys => {
            assert_array_equals(keys, ["test1"]);
        }).then(() => {
            return self.caches.open("test2");
        }).then(() => {
            return internals.clearCacheStorageMemoryRepresentation();
        }).then(() => {
            return self.caches.keys();
        }).then(keys => {
            assert_array_equals(keys, ["test1", "test2"]);
        }).then(() => {
            return Promise.all([self.caches.delete("test2"), self.caches.delete("test1")]);
        }).then(() => {
            return internals.clearCacheStorageMemoryRepresentation();
        }).then(() => {
            return self.caches.keys();
        }).then(keys => {
            assert_array_equals(keys, []);
        });
    }, "Adding/Removing caches and clearing in memory representation at various times");

    promise_test(test => {
        return self.caches.keys().then(keys => {
            var pending = [];
            for (key of keys)
                pending.push(self.caches.delete(keys[0]));
            return Promise.all(pending);
        });
    }, "Cleaning added caches");
    </script>
</body>
</html>

