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

description('This tests copying URL string using dataTransfer.items. To manually test, click on "Copy text" and paste (Command+V on Mac Control+V elsewhere).');

function copyText()
{
    document.getElementById('source').focus();
    document.execCommand('SelectAll', false, null);
    document.execCommand('Copy', false, null);
}

function copy(event)
{
    clipboardData = event.clipboardData;
    shouldBe('clipboardData.items.length', '0');
    shouldBe('clipboardData.setData("text/uri-list", "rock"); clipboardData.items.length', '1');
    shouldBeEqualToString('initialItem = clipboardData.items[0]; initialItem.kind', 'string');
    shouldBeEqualToString('initialItem.type', 'text/uri-list');
    shouldBe('initialItem.getAsFile()', 'null');
    shouldBe('initialItem.getAsString(checkContent(1, "rock"))', 'undefined');

    shouldThrowErrorName('clipboardData.items.add("paper", "text/uri-list")', 'NotSupportedError');
    shouldBe('clipboardData.items[0]', 'initialItem');
    shouldBe('clipboardData.items.clear(); clipboardData.items.length', '0');

    shouldBe('firstItem = clipboardData.items.add("https://webkit.org/", "text/uri-list"); clipboardData.items.length', '1');
    shouldBe('clipboardData.items[0]', 'firstItem');
    shouldNotBe('clipboardData.items[0]', 'initialItem');
    shouldBeEqualToString('firstItem.kind', 'string');
    shouldBeEqualToString('firstItem.type', 'text/uri-list');
    shouldBe('firstItem.getAsFile()', 'null');
    shouldBe('firstItem.getAsString(checkContent(2, "https://webkit.org/"))', 'undefined');
    shouldBeEqualToString('clipboardData.getData("url")', 'https://webkit.org/');
    shouldBeEqualToString('clipboardData.getData("text/plain")', '');

    shouldBe('secondItem = clipboardData.items.add("WebKit", "text/PLAIN"); clipboardData.items.length', '2');
    shouldBe('clipboardData.items[1]', 'secondItem');
    shouldBeEqualToString('secondItem.kind', 'string');
    shouldBeEqualToString('secondItem.type', 'text/plain');
    shouldBe('secondItem.getAsFile()', 'null');
    shouldBe('secondItem.getAsString(checkContent(3, "WebKit"))', 'undefined');
    shouldBeEqualToString('clipboardData.getData("url")', 'https://webkit.org/');
    shouldBeEqualToString('clipboardData.getData("TEXT/plain")', 'WebKit');

    shouldBe('clipboardData.setData("text/plain", "some text"); clipboardData.items.length', '2');
    shouldBe('clipboardData.items[0]', 'firstItem');
    shouldBeEqualToString('firstItem.kind', 'string');
    shouldBeEqualToString('firstItem.type', 'text/uri-list');
    shouldBe('firstItem.getAsString(checkContent(4, "https://webkit.org/"))', 'undefined');
    shouldNotBe('newSecondItem = clipboardData.items[1]; newSecondItem', 'secondItem');
    shouldBeEqualToString('newSecondItem.kind', 'string');
    shouldBeEqualToString('newSecondItem.type', 'text/plain');
    shouldBe('newSecondItem.getAsString(checkContent(5, "some text"))', 'undefined');
}

let count = 0;
function checkContent(number, expectedContent)
{
    count++;
    return (content) => {
        const variableName = 'actualContent' + number;
        window[variableName] = content;
        shouldBeEqualToString(variableName, expectedContent);
        count--;
        if (!count) {
            document.getElementById('container').style.display = 'none';
            finishJSTest();
        }
    }
}

if (window.testRunner)
    window.onload = copyText;
jsTestIsAsync = true;
successfullyParsed = true;

</script>
<div id="container">
    <button onclick="copyText();">Copy text</button>
    <div id="source" oncopy="copy(event)" contenteditable="true">hello, world</div>
</div>
<script src="../../resources/js-test-post.js"></script>
</body>
</html>
