<!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("Test that hidden files are not exposed via Entries API");
jsTestIsAsync = true;

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

function getDirectoryAsPromise(dirEntry, path)
{
    return new Promise((resolve, reject) => {
        dirEntry.getDirectory(path, {}, resolve, reject);
    });
}

function getEntriesAsPromise(dirEntry) {
    return new Promise((resolve, reject) => {
        let result = [];
        let reader = dirEntry.createReader();
        let doBatch = () => {
            reader.readEntries(entries => {
            if (entries.length > 0) {
                entries.forEach(e => result.push(e));
                doBatch();
            } else {
                resolve(result);
            }
        }, reject);
    };
    doBatch();
  });
}

function readEntriesTest()
{
    shouldBeEqualToString("directoryEntry.name", "testFiles");
    shouldBeEqualToString("directoryEntry.fullPath", "/testFiles");
    shouldBeTrue("directoryEntry.isDirectory");

    return getEntriesAsPromise(directoryEntry).then(entries => {
        foundEntries = entries;
        shouldBe("foundEntries.length", "4");
        for (let entry of entries) {
            if (entry.name.startsWith(".hidden")) {
                testFailed("Hidden file was returned by directoryEntry");
                break;
            }
        }
    }, e => {
        testFailed("Call to readEntries() failed unexpectedly: " + e);
        finishJSTest();
    });
}

function getFileTest()
{
    return getFileAsPromise(directoryEntry, ".hidden.txt").then(fileEntry => {
        testFailed("Hidden file was returned by getFile()");
        finishJSTest();
    }, e => {
        ex = e;
        shouldBeEqualToString("ex.name", "NotFoundError");
    });
}

function getDirectoryTest()
{
    return getDirectoryAsPromise(directoryEntry, ".hidden").then(fileEntry => {
        testFailed("Hidden directory was returned by getDirectory()");
        finishJSTest();
    }, e => {
        ex = e;
        shouldBeEqualToString("ex.name", "NotFoundError");
    });
}

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

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

    directoryEntry = dataTransfer.items[0].webkitGetAsEntry();

    readEntriesTest().then(getFileTest).then(getDirectoryTest).then(finishJSTest);
};

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

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