<html>
<head>
<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
<script>

var drtURL = "https://127.0.0.1:8443";

// Helpers.

function log(message) {
    document.getElementById("console").innerHTML += message + "<br>";
}

// Start and end.

function startTest() {
    if (window.testRunner) {
        testRunner.setAllowsAnySSLCertificate(true);
        testRunner.clearAllApplicationCaches();
        log("Cleared application cache");
        testRunner.dumpApplicationCacheDelegateCallbacks();
        testRunner.dumpAsText();
        testRunner.waitUntilDone();
    }

    addFirstIFrame();
}

function finishTest() {
    if (window.testRunner)
        testRunner.notifyDone();
}

// Stages.

function addIFrameWithContinuation(src, continuation) {
    window.onmessage = continuation;
    var iframe = document.createElement("iframe");
    iframe.src = src;
    document.body.appendChild(iframe);
}

function addFirstIFrame() {
    // Expected to succeed.
    addIFrameWithContinuation("http://127.0.0.1:8443/ssl/resources/origin-usage-iframe-1.html", function(event) {
        log(event.data);
        var usage = testRunner.applicationCacheDiskUsageForOrigin(drtURL);
        log("Usage for " + drtURL + " after first frame is between 15000 and 16000 bytes: " + ((usage > 15000) && (usage < 16000)));
        addSecondIFrame();
    });
}

function addSecondIFrame() {
    addIFrameWithContinuation("http://127.0.0.1:8443/ssl/resources/origin-usage-iframe-2.html", function(event) {
        log(event.data);
        var usage = testRunner.applicationCacheDiskUsageForOrigin(drtURL);
        log("Usage for " + drtURL + " after second frame is between 30000 and 32000 bytes: " + ((usage > 30000) && (usage < 32000)));

        log("Removing Application Cache for " + drtURL);
        testRunner.clearApplicationCacheForOrigin(drtURL);

        log("Usage for " + drtURL + ": " + testRunner.applicationCacheDiskUsageForOrigin(drtURL));

        finishTest();
    });
}

</script>
</head>
<body onload="startTest()">
<p>This test checks that per-origin usage is reported correctly when upgrading insecure load requests by loading two iframes which use ~15K each and querying origin usage.</p>
</p>
<pre id="console"></pre>
</body>
</html>
