<!-- webkit-test-runner [ useEphemeralSession=true ] -->
<!DOCTYPE html>
<html>
<head>
<title>Cache Storage: testing persistency</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/js-test-resources/gc.js"></script>
</head>
<body>
    <div id="check"></div>
    <script>
    function checkCaches(hasPersistent, hasRemoved, name, value) {
        test(() => {
            var results = JSON.parse(value).origins;
            if (!hasPersistent && !hasRemoved) {
                assert_equals(results.length, 0);
                return;
            }
            assert_equals(results.length, 1);
            var caches = results[0].caches;
            assert_equals(!!caches["persistent"].length, hasPersistent, "persistent");
            assert_equals(!!caches["removed"].length, hasRemoved, "removed");

           if (!hasPersistent)
               assert_equals(testRunner.domCacheSize(results.origin), 0, "non persistent cache storage size is zero");

        }, name);
    }

    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) => {
                return internals.cacheStorageEngineRepresentation().then(value => {
                    checkCaches(true, false, "A cache is created", value);
                    resolve();
                });
            }));
            check.innerHTML = "<iframe src='resources/cache-persistency-iframe.html'></iframe>";
        })
    }, "Create a cache storage and look at the representation");

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

        return new Promise((resolve, reject) => {
            window.addEventListener("message", test.step_func((event) => {
                return internals.cacheStorageEngineRepresentation().then(value => {
                    checkCaches(true, false, "A cache is persisting", value);
                    resolve();
                });
            }));
            check.innerHTML = "<iframe src='resources/cache-persistency-iframe.html#check'></iframe>";
        })
    }, "Look at the representation a second time after removing the iframe");

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

        return new Promise((resolve, reject) => {
            window.addEventListener("message", test.step_func((event) => {
                return internals.cacheStorageEngineRepresentation().then(value => {
                    checkCaches(false, true, "A cache is going in remove bucket", value);
                    resolve();
                });
            }));
            check.innerHTML = "<iframe src='resources/cache-persistency-iframe.html#remove'></iframe>";
        })
    }, "Remove a cache and look at the representation a second time");

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

        return new Promise((resolve, reject) => {
            window.addEventListener("message", test.step_func((event) => {
                gc();
                setTimeout(() => {
                    return internals.cacheStorageEngineRepresentation().then(value => {
                        checkCaches(false, false, "A removed cache gets collected", value);
                        resolve();
                    });
                }, 0);
            }));
            check.innerHTML = "<iframe src='resources/cache-persistency-iframe.html#check'></iframe>";
        });
    }, "Look at the representation a second time after reloading the iframe");

    </script>
</body>
</html>

