<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<script src="../../resources/js-test-pre.js"></script>
<script src="resources/js-test-selection-shared.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>
<script>
description('Grammar checking for pasted text.');

jsTestIsAsync = true;

var testRoot = document.createElement("div");
document.body.insertBefore(testRoot, document.body.firstChild);

var testTextArea = document.createElement("textarea");
testRoot.appendChild(testTextArea);

var testInput = document.createElement("input");
testInput.setAttribute("type", "text");
testRoot.appendChild(testInput);

var testEditable = document.createElement("div");
testEditable.setAttribute("contentEditable", "true");
testRoot.appendChild(testEditable);

var testSourcePlain = document.createElement("div");
testSourcePlain.innerHTML = "You has the right.";
testRoot.appendChild(testSourcePlain);

var testSourceDecorated = document.createElement("div");
testSourceDecorated.innerHTML = "I have a<b>n ki</b>wi. I have no idea.";
testRoot.appendChild(testSourceDecorated);

var testSourceMulti = document.createElement("div");
testSourceMulti.innerHTML = "I have an grape. I have an muscat. I don't know.";
testRoot.appendChild(testSourceMulti);

var sel = window.getSelection();

var tests = [];

function done()
{
    var next = tests.shift();
    if (next)
        return window.setTimeout(next, 0);
    testRoot.style.display = "none";
    finishJSTest();
}

function findFirstTextNode(node)
{
    function iterToFindFirstTextNode(node)
    {
        if (node instanceof Text)
            return node;

        var childNodes = node.childNodes;
        for (var i = 0; i < childNodes.length; ++i) {
            var n = iterToFindFirstTextNode(childNodes[i]);
            if (n)
                return n;
        }

        return null;
    }


    if (node instanceof HTMLInputElement || node instanceof HTMLTextAreaElement)
        return iterToFindFirstTextNode(internals.shadowRoot(node));
    else
        return iterToFindFirstTextNode(node);
}

function verifyMarker(node, expectedMarked)
{
    if (node instanceof HTMLInputElement || node instanceof HTMLTextAreaElement)
        node.focus();
    else
        sel.selectAllChildren(node);

    var textNode = findFirstTextNode(node);
    var num = internals.markerCountForNode(textNode, "grammar");
    if (num != expectedMarked.length)
        return false;
    for (var i = 0; i < num; ++i) {
        var range = internals.markerRangeForNode(textNode, "grammar", i);
        if (range.toString() != expectedMarked[i])
            return false;
    }

    var nodeContent = node instanceof HTMLInputElement || node instanceof HTMLTextAreaElement ? node.value : node.innerHTML;
    testPassed(node.tagName + " ungrammatical phrase '" + expectedMarked + "' on '" + nodeContent + "'");

    return true;
}

var destination = null;
var expectedMarked = null;
function pasteAndVerify(source, dest, expectedMarked)
{
    sel.selectAllChildren(source);
    document.execCommand("Copy");
    if (dest instanceof HTMLInputElement || dest instanceof HTMLTextAreaElement) {
        dest.value = "";
        dest.focus();
    } else {
        dest.innerHTML = "";
        sel.selectAllChildren(dest);
    }
    document.execCommand("Paste");

    if (window.internals) {
        destination = dest;
        ungrammaticalPhrase = expectedMarked;
        shouldBecomeEqual('verifyMarker(destination, ungrammaticalPhrase)', 'true', done);
    }
};

if (window.internals)
    internals.settings.setAsynchronousSpellCheckingEnabled(true);

tests.push(function() { pasteAndVerify(testSourcePlain, testEditable, ["has"]); });
tests.push(function() { pasteAndVerify(testSourceDecorated, testEditable, ["a"]); }); // Checks only 'a'.
tests.push(function() { pasteAndVerify(testSourceMulti, testEditable, ["an", "an"]); });

tests.push(function() { pasteAndVerify(testSourcePlain, testInput, ["has"]); });
tests.push(function() { pasteAndVerify(testSourceDecorated, testInput, ["an"]); });
tests.push(function() { pasteAndVerify(testSourceMulti, testInput, ["an", "an"]); });

tests.push(function() { pasteAndVerify(testSourcePlain, testTextArea, ["has"]); });
tests.push(function() { pasteAndVerify(testSourceDecorated, testTextArea, ["an"]); });
tests.push(function() { pasteAndVerify(testSourceMulti, testTextArea, ["an", "an"]); });

done();

var successfullyParsed = true;
</script>
<script src="../../resources/js-test-post.js"></script>
</body>
</html>
