<!DOCTYPE html>
<html>
<body>
<script src="../../resources/js-test-pre.js"></script>
<script>

description('Tests for replacing exactly one Text node. The optimization to update its data should not be observable.');

function createElementWithText(text)
{
    var iframe = document.createElement('iframe');
    document.body.appendChild(iframe);
    var doc = iframe.contentDocument;

    var container = doc.createElement("div");
    container.textContent = text;

    doc.body.appendChild(container);

    return container;
}

function eventCounter() { eventCount++; }
function countEvent(element, name) {
    eventCount = 0;
    element.addEventListener(name, eventCounter);
}

debug('Keeping a reference');
evalAndLog('container = createElementWithText("old"); textNode = container.firstChild');
shouldNotBe('container.innerHTML = "new"; container.firstChild', 'textNode');

debug('\nMutation observer');
var mutationObserver = new MutationObserver(function () {});
evalAndLog('container = createElementWithText("old"); mutationObserver.observe(container, {childList: true});');
shouldBe('container.innerHTML = "new"; records = mutationObserver.takeRecords(); records.length', '1');
shouldBe('records[0].type', '"childList"');
shouldBe('records[0].removedNodes.length', '1');
shouldBe('records[0].removedNodes[0].data', '"old"');
shouldBe('records[0].addedNodes.length', '1');
shouldBe('records[0].addedNodes[0].data', '"new"');

debug('\nDOMSubtreeModified event');
shouldBe('container = createElementWithText("old"); countEvent(container, "DOMSubtreeModified"); eventCount', '0');
shouldBeTrue('container.innerHTML = "new"; eventCount > 0');

debug('\nDOMNodeInserted event');
shouldBe('container = createElementWithText("old"); countEvent(container, "DOMNodeInserted"); eventCount', '0');
shouldBe('container.innerHTML = "new"; eventCount', '1');

debug('\nDOMNodeRemoved event');
shouldBe('container = createElementWithText("old"); countEvent(container, "DOMNodeRemoved"); eventCount', '0');
shouldBe('container.innerHTML = "new"; eventCount', '1');

debug('\nDOMCharacterDataModified event');
shouldBe('container = createElementWithText("old"); countEvent(container, "DOMCharacterDataModified"); eventCount', '0');
shouldBe('container.innerHTML = "new"; eventCount', '0');

// FIXME: Add a test for DOMNodeRemovedFromDocument event.

</script>
<script src="../../resources/js-test-post.js"></script>
</body>
</html>
