<html>
<head></head>
<body>
<div id="description"></div>
<div id="console"></div>
<input type="file" id="fileInput"></input>
<script>
if (window.testRunner) {
    testRunner.dumpAsText();
    testRunner.waitUntilDone();
}

var console = document.getElementById("console");

var messages = [];

function equal(actual, expected)
{
    if (typeof actual !== typeof expected)
        return false;
    if (actual === expected)
        return true;
    if ((actual instanceof Date) || (expected instanceof Date)) {
        if ((actual instanceof Date) && (expected instanceof Date))
            return (expected instanceof Date) && actual.getTime() == expected.getTime();
        return false;
    }
    if (Array.isArray(actual) || Array.isArray(expected)) {
        if (!Array.isArray(actual) || !Array.isArray(expected))
            return false;
        if (actual.length != expected.length)
            return false;
        for (var i = 0; i < actual.length; i++) {
            if ((i in actual) ^ (i in expected))
                return false;
            if (!equal(actual[i], expected[i]))
                return false;
        }
        return true;
    }
    if (actual.constructor !== expected.constructor)
        return false;
    var keys = Object.keys(actual);
    if (!equal(keys, Object.keys(expected)))
        return false;
    for (var i = 0; i < keys.length; i++) {
        if (!equal(actual[keys[i]], expected[keys[i]]))
            return false;
    }
    return true;
}

function safeToString(o) {
    if (o instanceof Date)
        return o.toISOString();
    if (typeof o !== "object" || !o)
        return o;
    try {
        return o.toString();
    } catch (e) {
        return Object.prototype.toString.call(o) + "(default toString threw "+e+")";
    }
}

function shouldBe(actual, expected)
{
    var actualValue = eval(actual);
    var expectedValue = eval(expected);
    if (equal(actualValue, expectedValue))
        console.innerHTML += "PASS: " + actual + " is " + safeToString(expectedValue) + " of type " + typeof actualValue + "<br>";
    else
        console.innerHTML += "FAIL: " + actual + " is " + actualValue + " should be " + expectedValue + " of type " + typeof expectedValue + "<br>";
}

function onmessage(evt) {
    evt.foo = "bar"; // add a property.
    eventData = evt.data;
    shouldBe("eventData", messages[0]);
    evt.func = function(){}; // add a function property.
    eventData = evt.data;
    shouldBe("eventData", messages[0]);
    evt.func = ""; // despecialise a function property.
    eventData = evt.data;
    shouldBe("eventData", messages[0]);
    for (var i = 0; i < 1000; i++) // To dictionary transition
        evt[i] = i;
    eventData = evt.data;
    shouldBe("eventData", messages[0]);
    delete evt.foo; // To uncachable dictionary
    eventData = evt.data;
    shouldBe("eventData", messages[0]);
    messages.shift();
    if (safeToString(evt.data) == 'done' && window.testRunner)
        testRunner.notifyDone();
}

window.addEventListener('message', onmessage, false);

function tryPostMessage(message, shouldThrow, expected) {
    try {
        var value = eval(message);
        postMessage(value, "*");
        if (shouldThrow)
            console.innerHTML += "FAIL: 'postMessage("+message+")' should throw but didn't<br>";
        messages.push(expected || message);
    } catch(e) {
        if (shouldThrow)
            console.innerHTML += "PASS: 'postMessage("+message+")' threw " + e + "<br>";
        else
            console.innerHTML += "FAIL: 'postMessage("+message+")' should not throw but threw " + e + "<br>";
    }
}

document.getElementById("description").innerHTML = "Tests that we clone object hierarchies";

tryPostMessage('null');
tryPostMessage('undefined');
tryPostMessage('1');
tryPostMessage('true');
tryPostMessage('"1"');
tryPostMessage('({})');
tryPostMessage('({a:1})');
tryPostMessage('[]');
tryPostMessage('[1,2,3]');
tryPostMessage('[,,1]');
tryPostMessage('(function(){})', true);
tryPostMessage('new Date(1234567890000)');
tryPostMessage('"done"');
</script>
</body>
</html>
