<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="../../../resources/js-test-pre.js"></script>
<title></title>
</head>
<body>
<p id=description></p>
<div id="console"></div>
<script>

window.jsTestIsAsync = true;
var mutations;

function testBasic() {
    var observer;

    function start() {
        debug('Testing takeRecords.');

        mutations = null;
        div = document.createElement('div');
        subDiv = div.appendChild(document.createElement('div'));
        subDiv.innerHTML = 'hello, world';
        observer = new MutationObserver(function(mutations) {
            window.mutations = mutations;
        });

        observer.observe(div, {attributes: true, characterData: true, subtree: true});
        subDiv.setAttribute('foo', 'bar');
        subDiv.firstChild.textContent = 'goodbye!';
        div.removeChild(subDiv);

        mutations = observer.takeRecords();

        debug('...records are taken synchronously.');

        shouldBe('mutations.length', '2');
        shouldBe('mutations[0].type', '"attributes"');
        shouldBe('mutations[0].target', 'subDiv');
        shouldBe('mutations[0].attributeName', '"foo"');
        shouldBe('mutations[0].attributeNamespace', 'null');
        shouldBe('mutations[1].type', '"characterData"');
        shouldBe('mutations[1].target', 'subDiv.firstChild');

        subDiv.setAttribute('foo', 'baz');
        setTimeout(finish, 0);
    }

    function finish() {
        debug('...takeRecord took records, but did not clear transient observers');

        shouldBe('mutations.length', '1');
        shouldBe('mutations[0].type', '"attributes"');
        shouldBe('mutations[0].target', 'subDiv');
        shouldBe('mutations[0].attributeName', '"foo"');
        observer.disconnect();
        debug('');
        runNextTest();
    }

    start();
}

var tests = [testBasic];
var testIndex = 0;

function runNextTest() {
    if (testIndex < tests.length)
        tests[testIndex++]();
    else
        finishJSTest();
}

description('Testing WebKitMutationObserver.takeRecords');

runNextTest();
</script>
<script src="../../../resources/js-test-post.js"></script>
</body>
</html>
