<!DOCTYPE html>
<script src="../../../../resources/js-test.js"></script>
<script src="resources/shared.js"></script>
<input type="file" id="fileInput" multiple></input>
<script>

description("Confirm basic Blob/File/FileList functionality.");

fileInput = document.getElementById("fileInput");
if (window.eventSender) {
    var fileRect = fileInput.getClientRects()[0];
    var targetX = fileRect.left + fileRect.width / 2;
    var targetY = fileRect.top + fileRect.height / 2;
    eventSender.beginDragWithFiles(['resources/test-data.html', 'resources/test-data.txt']);
    eventSender.mouseMoveTo(targetX, targetY);
    eventSender.mouseUp();
}

function prepareDatabase()
{
    db = event.target.result;
    var trans = event.target.transaction;
    evalAndLog("store = db.createObjectStore('storeName')");
    evalAndLog("store.put('value', 'key')");
    trans.onerror = unexpectedErrorCallback;
    trans.onabort = unexpectedAbortCallback;
}

var blobValidation = ".size == test_content.length";
function testBlob()
{
    preamble();

    shouldBeTrue("FileReader != null");
    evalAndLog("test_content = 'This is a test. This is only a test.'");
    evalAndLog("blob = new Blob([test_content])");
    validateResult("blob", blobValidation, testFile);
}

var fileValidation = ".name == fileInput.files[0].name";
function testFile()
{
    preamble();
    evalAndLog("file = fileInput.files[0]");
    validateResult("file", fileValidation, testNewFile);
}

var newFileValidation = ".name == newFile.name";
function testNewFile()
{
    debug("");
    debug("testNewFile():");
    evalAndLog("newFile = new File([test_content], 'filename', {type:'text/plain'})");
    validateResult("newFile", newFileValidation, testFileList);
}

var fileListValidation = "[1].name == fileInput.files[1].name";
function testFileList()
{
    preamble();
    evalAndLog("filelist = fileInput.files");
    validateResult("filelist",
        fileListValidation, testCursor);
}

function testCursor()
{
    preamble();
    evalAndLog("transaction = db.transaction('storeName', 'readonly')");
    transaction.onerror = unexpectedErrorCallback;
    transaction.onabort = unexpectedAbortCallback;
    evalAndLog("store = transaction.objectStore('storeName')");
    evalAndLog("request = store.openCursor()");

    var count = 0;
    request.onsuccess = continueToTest;

    function continueToTest(event)
    {
        cursor = event.target.result;
        switch (count++) {
          case 0:
            shouldBeTrue("cursor.value" + blobValidation);
            break;
          case 1:
            shouldBeTrue("cursor.value" + fileValidation);
            break;
          case 2:
            shouldBeTrue("cursor.value" + fileListValidation);
            break;
          case 3:
            shouldBeTrue("cursor.value == 'value'");
            break;
          case 4:
            shouldBeTrue("cursor.value" + newFileValidation);
            break;
          case 5:
            shouldBeNull("cursor");
            finishJSTest();
            return;
          default:
            testFailed("count was unexpectedly " + count);
        }
        debug("cursor.continue();");
        cursor.continue();
//        evalAndLog("cursor.continue();");
    }
}

function validateResult(variable, validation, onSuccess)
{
    var keyName = variable + "key";
    debug("");
    debug("validateResult(" + variable + "):");
    shouldBeTrue(variable + validation);
    evalAndLog("transaction = db.transaction('storeName', 'readwrite')");
    evalAndLog("store = transaction.objectStore('storeName')");
    evalAndLog("store.put(" + variable + ", '" + keyName + "')");
    transaction.onerror = unexpectedErrorCallback;
    transaction.onabort = unexpectedAbortCallback;
    var onGetSuccess = function (e) {
      shouldBeTrue("event.target.result" + validation);
      onSuccess();
    }
    transaction.oncomplete = function () {
      doRead(keyName, onGetSuccess);
    }
}

function doRead(keyName, onSuccess)
{
    evalAndLog("transaction = db.transaction('storeName', 'readwrite')");
    evalAndLog("store = transaction.objectStore('storeName')");
    evalAndLog("request = store.get('" + keyName + "')");
    request.onsuccess = onSuccess;
    transaction.onerror = unexpectedErrorCallback;
    transaction.onabort = unexpectedAbortCallback;
}

if (window.eventSender) {
    indexedDBTest(prepareDatabase, testBlob);
} else {
    alert("Select at least 2 files using the input control above to initiate the test");
    document.getElementById("fileInput").onchange = function() { indexedDBTest(prepareDatabase, testBlob); };
}

</script>
