<!DOCTYPE html>
<html>
<head>
<script src="../../../resources/js-test.js"></script>
<script src="../../../resources/ui-helper.js"></script>
<style>
body.hide-everything-except-test-container > :not(#test-container) {
    display: none;
}
</style>
</head>
<body>
<div id="test-container">
    <p>Here's to the crazy ones.</p>
    <p>The misfits.</p>
    <p>The rebels.</p>
</div>
<script>
let testContainer = document.getElementById("test-container");
let paragraphs = testContainer.children;
let selection = window.getSelection();

function toggleOnlyShowTestContainer()
{
    document.body.classList.toggle("hide-everything-except-test-container");
}

async function testExtendSelectionToNextCharacter()
{
    await UIHelper.callFunctionAndWaitForEvent(() => selection.setBaseAndExtent(paragraphs[0].firstChild, 0, paragraphs[0].firstChild, 1), document, "selectionchange");

    debug("<br>Press Shift + right arrow to select the next character:");
    await UIHelper.callFunctionAndWaitForEvent(() => window.testRunner && UIHelper.keyDown("rightArrow", ["shiftKey"]) , document, "selectionchange");
    shouldBeEqualToString("window.getSelection().toString()", "He");
}

async function testExtendSelectionToPreviousCharacter()
{
    await UIHelper.callFunctionAndWaitForEvent(() => selection.setBaseAndExtent(paragraphs[0].firstChild, 3, paragraphs[0].firstChild, 1), document, "selectionchange");

    debug("<br>Press Shift + left arrow to select the previous character:");
    await UIHelper.callFunctionAndWaitForEvent(() => window.testRunner && UIHelper.keyDown("leftArrow", ["shiftKey"]) , document, "selectionchange");
    shouldBeEqualToString("window.getSelection().toString()", "Her");
}

async function testExtendSelectionToEndOfWord()
{
    await UIHelper.callFunctionAndWaitForEvent(() => selection.setBaseAndExtent(paragraphs[0].firstChild, 0, paragraphs[0].firstChild, 1), document, "selectionchange");

    debug("<br>Press Shift + Option + right arrow to select to the end of the word:");
    await UIHelper.callFunctionAndWaitForEvent(() => window.testRunner && UIHelper.keyDown("rightArrow", ["shiftKey", "altKey"]) , document, "selectionchange");
    shouldBeEqualToString("window.getSelection().toString()", "Here's ");
}

async function testExtendSelectionToBeginningOfWord()
{
    await UIHelper.callFunctionAndWaitForEvent(() => selection.setBaseAndExtent(paragraphs[1].firstChild, 3, paragraphs[1].firstChild, 1), document, "selectionchange");

    debug("<br>Press Shift + Option + left arrow to select to the beginning of the word:");
    await UIHelper.callFunctionAndWaitForEvent(() => window.testRunner && UIHelper.keyDown("leftArrow", ["shiftKey", "altKey"]) , document, "selectionchange");
    shouldBeEqualToString("window.getSelection().toString()", "The");
}

async function textExtendSelectionToEndOfLine()
{
    await UIHelper.callFunctionAndWaitForEvent(() => selection.setBaseAndExtent(paragraphs[0].firstChild, 0, paragraphs[0].firstChild, 1), document, "selectionchange");

    debug("<br>Press Shift + Control + right arrow to select to the end of the line:");
    await UIHelper.callFunctionAndWaitForEvent(() => window.testRunner && UIHelper.keyDown("rightArrow", ["shiftKey", "ctrlKey"]) , document, "selectionchange");
    shouldBeEqualToString("window.getSelection().toString()", "Here's to the crazy ones.");
}

async function testExtendSelectionToBeginningOfLine()
{
    await UIHelper.callFunctionAndWaitForEvent(() => selection.setBaseAndExtent(paragraphs[1].firstChild, paragraphs[1].firstChild.length, paragraphs[1].firstChild, paragraphs[1].firstChild.length - 1), document, "selectionchange");

    debug("<br>Press Shift + Control + left arrow to select to the beginning of the line:");
    await UIHelper.callFunctionAndWaitForEvent(() => window.testRunner && UIHelper.keyDown("leftArrow", ["shiftKey", "ctrlKey"]) , document, "selectionchange");
    shouldBeEqualToString("window.getSelection().toString()", "The misfits.");
}

async function testExtendSelectionUp()
{
    await UIHelper.callFunctionAndWaitForEvent(() => selection.setBaseAndExtent(paragraphs[1].firstChild, 1, paragraphs[1].firstChild, 0), document, "selectionchange");

    debug("<br>Press Shift + up arrow to select up:");
    await UIHelper.callFunctionAndWaitForEvent(() => window.testRunner && UIHelper.keyDown("upArrow", ["shiftKey"]) , document, "selectionchange");
    shouldBeEqualToString("window.getSelection().toString()", "Here's to the crazy ones.\n\nT");
}

async function testExtendSelectionDown()
{
    await UIHelper.callFunctionAndWaitForEvent(() => selection.setBaseAndExtent(paragraphs[1].firstChild, 0, paragraphs[1].firstChild, 1), document, "selectionchange");

    debug("<br>Press Shift + down arrow to select down:");
    await UIHelper.callFunctionAndWaitForEvent(() => window.testRunner && UIHelper.keyDown("downArrow", ["shiftKey"]) , document, "selectionchange");
    shouldBeEqualToString("window.getSelection().toString()", "The misfits.\n\nT");
}

async function testExtendSelectionToEndOfParagraph()
{
    toggleOnlyShowTestContainer();

    await UIHelper.callFunctionAndWaitForEvent(() => selection.setBaseAndExtent(paragraphs[0].firstChild, 0, paragraphs[0].firstChild, 1), document, "selectionchange");

    debug("<br>Press Shift + Option + down arrow to select to the end of the paragraph:");
    await UIHelper.callFunctionAndWaitForEvent(() => window.testRunner && UIHelper.keyDown("downArrow", ["shiftKey", "altKey"]) , document, "selectionchange");
    shouldBeEqualToString("window.getSelection().toString()", "Here's to the crazy ones.");

    toggleOnlyShowTestContainer();
}

async function testExtendSelectionToBeginningOfParagraph()
{
    toggleOnlyShowTestContainer();

    await UIHelper.callFunctionAndWaitForEvent(() => selection.setBaseAndExtent(paragraphs[2].firstChild, paragraphs[2].firstChild.length, paragraphs[2].firstChild, paragraphs[2].firstChild.length - 1), document, "selectionchange");

    debug("<br>Press Shift + Option + up arrow to select to the beginning of the paragraph:");
    await UIHelper.callFunctionAndWaitForEvent(() => window.testRunner && UIHelper.keyDown("upArrow", ["shiftKey", "altKey"]) , document, "selectionchange");
    shouldBeEqualToString("window.getSelection().toString()", "The rebels.");

    toggleOnlyShowTestContainer();
}

async function testExtendSelectionToEndOfDocument()
{
    toggleOnlyShowTestContainer();

    await UIHelper.callFunctionAndWaitForEvent(() => selection.setBaseAndExtent(paragraphs[0].firstChild, 0, paragraphs[0].firstChild, 1), document, "selectionchange");

    debug("<br>Press Shift + Control + down arrow to select to the end of the document:");
    await UIHelper.callFunctionAndWaitForEvent(() => window.testRunner && UIHelper.keyDown("downArrow", ["shiftKey", "ctrlKey"]) , document, "selectionchange");
    shouldBeEqualToString("window.getSelection().toString()", "Here's to the crazy ones.\n\nThe misfits.\n\nThe rebels.");

    toggleOnlyShowTestContainer();
}

async function testExtendSelectionToBeginningOfDocument()
{
    toggleOnlyShowTestContainer();

    await UIHelper.callFunctionAndWaitForEvent(() => selection.setBaseAndExtent(paragraphs[1].firstChild, paragraphs[1].firstChild.length, paragraphs[1].firstChild, paragraphs[1].firstChild.length - 1), document, "selectionchange");

    debug("<br>Press Shift + Control + up arrow to select to the beginning of the document:");
    await UIHelper.callFunctionAndWaitForEvent(() => window.testRunner && UIHelper.keyDown("upArrow", ["shiftKey", "ctrlKey"]) , document, "selectionchange");
    shouldBeEqualToString("window.getSelection().toString()", "Here's to the crazy ones.\n\nThe misfits.");

    toggleOnlyShowTestContainer();
}

async function runTests()
{
    await testExtendSelectionToNextCharacter();
    await testExtendSelectionToPreviousCharacter();
    await testExtendSelectionToEndOfWord();
    await testExtendSelectionToBeginningOfWord();
    await textExtendSelectionToEndOfLine();
    await testExtendSelectionToBeginningOfLine();
    await testExtendSelectionUp();
    await testExtendSelectionDown();
    await testExtendSelectionToEndOfParagraph();
    await testExtendSelectionToBeginningOfParagraph();
    await testExtendSelectionToEndOfDocument();
    await testExtendSelectionToBeginningOfDocument();

    document.body.removeChild(document.getElementById("test-container"));
    finishJSTest();
}

window.jsTestIsAsync = true;
description("Test selecting non-editable text using the keyboard.");
runTests();
</script>
</body>
</html>
