<!DOCTYPE html>

<html>
<head>
    <script src="../../../../resources/basic-gestures.js"></script>
    <script>
        if (window.testRunner) {
            testRunner.dumpAsText();
            testRunner.waitUntilDone();
        }
    
        async function runTest()
        {
            if (!testRunner.runUIScript)
                return;
        
            var output = '';
            var firstTargetRect = document.getElementById('firstSelection').getBoundingClientRect();
            var secondTargetRect = document.getElementById('secondSelection').getBoundingClientRect();
            var thirdTargetRect = document.getElementById('thirdSelection').getBoundingClientRect();
        
            var pressPointX = firstTargetRect.x+firstTargetRect.width / 2;
            var pressPointY = firstTargetRect.y+firstTargetRect.height / 2;
            var dragY = firstTargetRect.y;
            var dragX = firstTargetRect.x;
            var dragX2 = secondTargetRect.x;
            var dragX3 = thirdTargetRect.x;
            var dragX4 = firstTargetRect.x + firstTargetRect.width;
        
            await longPressAtPoint(pressPointX, pressPointY);
            if (document.getSelection().toString() == "aliqua")
                output += 'PASS: Correct Selection';
            else
                output += 'FAIL: failed to select a word as a result of a long press. Incorrect Selection: ' + document.getSelection().toString();
            output += '<br>';

            await touchAndDragFromPointToPoint(dragX, dragY, dragX2, dragY);
            if (document.getSelection().toString() == "magna aliqua")
                output += 'PASS: Correct Selection';
            else
                output += 'FAIL: failed to select additional word after a drag. Incorrect Selection: ' + document.getSelection().toString();
            output += '<br>';

            await touchAndDragFromPointToPoint(dragX2, dragY, dragX3, dragY);
            if (document.getSelection().toString() == "dolore magna aliqua")
                output += 'PASS: Correct Selection';
            else
                output += 'FAIL: failed to select additional word after a drag. Incorrect Selection: ' + document.getSelection().toString();
            output += '<br>';

            await touchAndDragFromPointToPoint(dragX3, dragY, dragX2, dragY);
            if (document.getSelection().toString() == "magna aliqua")
                output += 'PASS: Correct Selection';
            else
                output += 'FAIL: failed to deselect word after a drag. Incorrect Selection: ' + document.getSelection().toString();
            output += '<br>';

            await touchAndDragFromPointToPoint(dragX2, dragY, dragX, dragY);
            if (document.getSelection().toString() == "aliqua")
                 output += 'PASS: Correct Selection';
            else
                output += 'FAIL: failed to deselect word after a drag. Incorrect Selection: ' + document.getSelection().toString();
            output += '<br>';

            var result = await touchAndDragFromPointToPoint(dragX, dragY, dragX4, dragY);
            if (document.getSelection().toString() == "a")
                output += 'PASS: Correct Selection';
            else
                output += 'FAIL: failed to reduce selection to a single character by dragging right. Incorrect Selection: ' + document.getSelection().toString();
            output += '<br>';
            output += result;
            document.getElementById('target').innerHTML = output;
            testRunner.notifyDone();
        }
    
        window.addEventListener('load', runTest, false);
    </script>
    <style>
        #target {
            height: 400px;
            width: 300px;
            background-color: silver;
            font-family: monospace;
            font-size: 18px;
        }
    </style>
    <meta name="viewport" content="initial-scale=1">
</head>
<body>
    <div id="target">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et <br><span id="thirdSelection"></span>dolore <span id="secondSelection"></span>magna <span id="firstSelection">aliqua</span>. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
        
        This test requires UIScriptController to run.
    </div>
</body>
</html>
