summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webstorage/eventTestHarness.js
blob: 7d9ed01870ea1b21daa4687bc019f2dd9df9de9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
iframe = document.createElement("IFRAME");
iframe.src = "about:blank";
document.body.appendChild(iframe);
iframe.contentWindow.document.body.textContent = "Nothing to see here.";

storageEventList = new Array();
iframe.contentWindow.onstorage = function(e) {
    window.parent.storageEventList.push(e);
};

function runAfterNStorageEvents(callback, expectedNumEvents)
{
    countStorageEvents(callback, expectedNumEvents, 0)
}

function countStorageEvents(callback, expectedNumEvents, times)
{
    function onTimeout()
    {
        var currentCount = storageEventList.length;
        if (currentCount == expectedNumEvents) {
            callback();
        } else if (currentCount > expectedNumEvents) {
            msg = "got at least " + currentCount + ", expected only " + expectedNumEvents + " events";
            callback(msg);
        } else if (times > 50) {
            msg = "Timeout: only got " + currentCount + ", expected " + expectedNumEvents + " events";
            callback(msg);
        } else {
            countStorageEvents(callback, expectedNumEvents, times+1);
        }
    }
    setTimeout(onTimeout, 20);
}

function clearStorage(storageName, callback)
{
    if (window[storageName].length === 0) {
        storageEventList = [];
        setTimeout(callback, 0);
    } else {
        window[storageName].clear();
        runAfterNStorageEvents(function() {
            storageEventList = [];
            callback();
        }, 1);
    }
}

function testStorages(testCallback)
{
    testCallback("sessionStorage");
    var hit = false;
    add_result_callback(function() {
        if (!hit) {
            hit = true;
            testCallback("localStorage");
        }
    });
}