<!DOCTYPE html>
<html>
<head>
<title>Cache Storage: testing Cache persistency</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
    <script>
function compareClientOrigins(a, b)
{
    if (a.origin.clientOrigin < b.clientOrigin)
        return 1;
    if (a.origin.clientOrigin > b.origin.clientOrigin)
        return -1;
    return 0;
}

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

    return new Promise((resolve, reject) => {
        var counter = 0;
        window.addEventListener("message", async (event) => {
            var representation = JSON.parse(await internals.cacheStorageEngineRepresentation()).origins;
            test(() => {
                representation.sort(compareClientOrigins);
                var cache = representation[0].origin.clientOrigin === "https://localhost:8443"
                assert_equals(representation[0].origin.topOrigin, "https://127.0.0.1:8443", "top origin of cache 1");
                assert_equals(representation[0].origin.clientOrigin, "https://localhost:8443", "client origin of cache 1");
                assert_array_equals(representation[0].caches.persistent, ["cache1"]);
            }, "Validating cache representation before clearing");

            testRunner.clearDOMCache('https://localhost:8443');

            representation = JSON.parse(await internals.cacheStorageEngineRepresentation()).origins;
            test(() => {
                assert_equals(representation.length, 0);
            }, "Validating cache representation after clearing");
            resolve();
        });
    })
}, "Create a cache storage from localhost and clear it");

promise_test(async test => {
    var cache = await self.caches.open("test-cache-records-persistency");
    await cache.put("https://example.com/foo", new Response("body", { statusText: "status" }));

    if (!window.testRunner)
        return Promise.reject("test runner needed");

    assert_false(testRunner.hasDOMCache('https://localhost:80'), 'hasDOMCache with fake origin');
    assert_true(testRunner.hasDOMCache(window.location.origin), "hasDOMCache with actual origin");

    testRunner.clearDOMCache('https://localhost:80');

    var keys = await self.caches.keys();
    assert_not_equals(keys.length, 0, "keys should not be empty");
    assert_true(testRunner.domCacheSize(window.location.origin) > 0, "Actual origin cache size is not zero");

    testRunner.clearDOMCache(window.location.origin);
    assert_false(testRunner.hasDOMCache(window.location.origin), "Actual origin cache is cleared");
    assert_equals(testRunner.domCacheSize(window.location.origin), 0, "Actual origin cache size is zero");

    keys = await self.caches.keys();
    assert_equals(keys.length, 0, "keys should be empty");

    cache = await self.caches.open("test-cache-records-persistency");
    keys = await cache.keys();
    assert_equals(keys.length, 0, "records should be empty");
}, 'Clearing disk cache of a given origin');
    </script>
    <iframe src="https://localhost:8443/cache-storage/resources/cache-persistency-iframe.html#name=cache1"></iframe>
</body>
</html>
