<html>
<head>
<script src="../editing.js"></script>
<title>Editing multiple words with markers test</title>
</head>
<body>
<textarea id="testElement"></textarea>
<script src="../../resources/js-test-pre.js"></script>
<script>
description('The test verifies if the spelling markers disappear when '
    + 'the multiple misspelled words are concatenated by delete command. '
    + 'To test manually, type "it\'s a meagesga meagesga", then '
    + 'select and delete "esga meag". The test succeeds '
    + 'if the remaining text does not have any underline.');

jsTestIsAsync = true;

if (window.internals) {
    internals.settings.setUnifiedTextCheckerEnabled(true);
    internals.settings.setAsynchronousSpellCheckingEnabled(true);
}

function resetText() {
    var textArea = document.getElementById('testElement');
    textArea.value = "";
    textArea.focus();
    typeCharacterCommand('i');
    typeCharacterCommand('t');
    typeCharacterCommand('\'');
    typeCharacterCommand('s');
    typeCharacterCommand(' ');
    typeCharacterCommand('a');
    typeCharacterCommand(' ');
    typeCharacterCommand('m');
    typeCharacterCommand('e');
    typeCharacterCommand('a');
    typeCharacterCommand('g');
    typeCharacterCommand('e');
    typeCharacterCommand('s');
    typeCharacterCommand('g');
    typeCharacterCommand('a');
    typeCharacterCommand(' ');
    typeCharacterCommand('m');
    typeCharacterCommand('e');
    typeCharacterCommand('a');
    typeCharacterCommand('g');
    typeCharacterCommand('e');
    typeCharacterCommand('s');
    typeCharacterCommand('g');
    typeCharacterCommand('a');
    typeCharacterCommand(' ');
}

var testCases = [
    { selectionRange: null, spellingMarkerPositions: [ 0, 7 ], spellingMarkerLengths: [ 4, 8 ], shouldBeMarked: [ false, true ] },
    { selectionRange: [ 11, 20 ], spellingMarkerPositions: [ 7, 11 ], spellingMarkerLengths: [ 4, 4 ], shouldBeMarked: [ false, false] },
    { selectionRange: [ 11, 16 ], spellingMarkerPositions: [ 7, 11 ], spellingMarkerLengths: [ 4, 8 ], shouldBeMarked: [ false, false] },
    { selectionRange: [ 15, 20 ], spellingMarkerPositions: [ 7, 15 ], spellingMarkerLengths: [ 8, 4 ], shouldBeMarked: [ false, false] }
];

var spellingPositions;
var spellingLengths;
var markersExpectation;

function checkSpellingMarkerAfterDeleteingSelection(selectionRange, spellingMarkerPositions, spellingMarkerLengths, shouldBeMarked)
{
    resetText();
    if (selectionRange) {
        document.getElementById('testElement').setSelectionRange(selectionRange[0], selectionRange[1]);
        execDeleteCommand();
    }

    spellingPositions = spellingMarkerPositions;
    spellingLengths = spellingMarkerLengths;
    markersExpectation = shouldBeMarked;

    if (window.internals) {
        shouldBecomeEqual('internals.hasSpellingMarker(spellingPositions[0], spellingLengths[0])',
            markersExpectation[0] ? 'true' : 'false', function() {
                shouldBecomeEqual('internals.hasSpellingMarker(spellingPositions[1], spellingLengths[1])',
                    markersExpectation[1] ? 'true' : 'false', function() {
                        debug("");
                        done();
                    });
            });
    }
}

function done()
{
    var nextTestCase = testCases.shift();
    if (nextTestCase)
        return setTimeout(checkSpellingMarkerAfterDeleteingSelection(
            nextTestCase.selectionRange,
            nextTestCase.spellingMarkerPositions,
            nextTestCase.spellingMarkerLengths,
            nextTestCase.shouldBeMarked,
            0));

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