<!DOCTYPE html>
<html>
<head>
<script src="../../../resources/js-test.js"></script>
<script src="../../editing.js"></script>
</head>
<body>
<div id="dropzone" style="width: 200px; height: 200px; background-color: grey;"></div>
<script>
description("Basic test coverage for fileSystemFileEntry.file()");
jsTestIsAsync = true;

function fileAsPromise(fileEntry)
{
    return new Promise((resolve, reject) => {
        fileEntry.file(resolve, reject);
    });
}

function readTextFileAsPromise(file)
{
    return new Promise((resolve, reject) => {
        let fileReader = new FileReader();
        fileReader.onload = function(e) {
            resolve(fileReader.result);
        }
        fileReader.readAsText(file);
    });
}

function getFileAsPromise(directoryEntry, path)
{
    return new Promise((resolve, reject) => {
        directoryEntry.getFile(path, {}, resolve, reject);
    });
}

function test1()
{
    shouldBeEqualToString("firstEntry.name", "test.txt");
    shouldBeEqualToString("firstEntry.fullPath", "/test.txt");
    shouldBeTrue("firstEntry.isFile");
    return fileAsPromise(firstEntry).then(file => {
        fileResult = file;
        shouldBe("fileResult.__proto__", "File.prototype");
        shouldBeEqualToString("fileResult.name", "test.txt");
        shouldBeEqualToString("fileResult.webkitRelativePath", "");
        shouldBe("fileResult.size", "5");
        shouldBeEqualToString("fileResult.type", "text/plain");
    }, e => {
        testFailed("fileEntry.file() call failed unexpectedly: " + e);
    }).then(function() {
       return readTextFileAsPromise(fileResult).then(content => {
           contentResult = content;
           shouldBeEqualToString("contentResult", "Hello");
       });
    });
}

function test2()
{
    shouldBeEqualToString("secondEntry.name", "testFiles");
    shouldBeEqualToString("secondEntry.fullPath", "/testFiles");
    shouldBeTrue("secondEntry.isDirectory");

    return getFileAsPromise(secondEntry, "subfolder1/file3.txt").then(fileEntry => {
        fileEntryResult = fileEntry;
        shouldBeEqualToString("fileEntryResult.name", "file3.txt");
        shouldBeEqualToString("fileEntryResult.fullPath", "/testFiles/subfolder1/file3.txt");
        shouldBeTrue("fileEntryResult.isFile");
    }, e => {
        testFailed("directoryEntry.getFile() call failed unexpectedly: " + e);
    }).then(function() {
        return fileAsPromise(fileEntryResult).then(file => {
            file3Result = file;
            shouldBe("file3Result.__proto__", "File.prototype");
            shouldBeEqualToString("file3Result.name", "file3.txt");
            shouldBeEqualToString("file3Result.webkitRelativePath", "");
            shouldBe("file3Result.size", "2");
            shouldBeEqualToString("file3Result.type", "text/plain");
        }, e => {
            testFailed("fileEntry.file() call failed unexpectedly: " + e);
        }).then(function() {
            return readTextFileAsPromise(file3Result).then(content => {
                contentResult = content;
                shouldBeEqualToString("contentResult", "3\n");
            });
        });
    });
}

var dropzone = document.getElementById('dropzone');
dropzone.ondrop = function(e) {
    e.preventDefault();
    dataTransfer = e.dataTransfer;

    shouldBe("dataTransfer.items.length", "2");

    firstEntry = dataTransfer.items[0].webkitGetAsEntry();
    secondEntry = dataTransfer.items[1].webkitGetAsEntry();

    test1().then(test2).then(finishJSTest);
};

dropzone.ondragover = function(ev) {
    ev.preventDefault();
}

onload = function() {
    dragFilesOntoElement(dropzone, ['../../../fast/forms/resources/test.txt', '../../../fast/forms/file/entries-api/resources/testFiles']);
}
</script>
</body>
</html>
