<!--
  Any copyright is dedicated to the Public Domain.
  http://creativecommons.org/publicdomain/zero/1.0/
-->
<html>
<head>
  <title>File Handle Test</title>

  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />

  <script type="text/javascript;version=1.7">
  function testSteps()
  {
    const name = window.location.pathname;

    let testBuffer = getRandomBuffer(100000);

    let request = indexedDB.open(name, 1);
    request.onerror = errorHandler;
    request.onupgradeneeded = grabEventAndContinueHandler;
    request.onsuccess = errorHandler;
    let event = yield undefined;

    is(event.type, "upgradeneeded", "Got correct event type");

    let db = event.target.result;
    db.onerror = errorHandler;

    db.createObjectStore("Foo", { });

    request.onupgradeneeded = errorHandler;
    request.onsuccess = grabEventAndContinueHandler;
    event = yield undefined;

    is(event.type, "success", "Got correct event type");

    request = db.createMutableFile("test.txt");
    request.onerror = errorHandler;
    request.onsuccess = grabEventAndContinueHandler;
    event = yield undefined;

    let mutableFile = event.target.result;

    function dummyWorkerScript() {
      onmessage = function(event) {
        throw("Shouldn't be called!");
      }
    }

    let url =
      URL.createObjectURL(new Blob(["(", dummyWorkerScript.toSource(), ")()"]));

    let worker1 = new Worker(url);
    try {
      worker1.postMessage(mutableFile);
      ok(false, "Should have thrown!");
    }
    catch (e) {
      ok(e instanceof DOMException, "Got exception.");
      is(e.name, "DataCloneError", "Good error.");
      is(e.code, DOMException.DATA_CLONE_ERR, "Good error code.")
    }

    mutableFile.onerror = errorHandler;

    let fileHandle = mutableFile.open("readwrite");

    request = fileHandle.write(testBuffer);
    request.onsuccess = grabEventAndContinueHandler;
    event = yield undefined;

    request = mutableFile.getFile();
    request.onsuccess = grabEventAndContinueHandler;
    event = yield undefined;

    let file = event.target.result;

    let worker2 = new Worker(url);
    URL.revokeObjectURL(url);
    try {
      worker2.postMessage(file);
      ok(false, "Should have thrown!");
    }
    catch (e) {
      ok(e instanceof DOMException, "Got exception.");
      is(e.name, "DataCloneError", "Good error.");
      is(e.code, DOMException.DATA_CLONE_ERR, "Good error code.")
    }

    let objectStore =
      db.transaction("Foo", "readwrite").objectStore("Foo");

    request = objectStore.add(mutableFile, 42);
    request.onsuccess = grabEventAndContinueHandler;
    event = yield undefined;

    function workerScript() {
      onmessage = function(event) {
        var name = event.data;
        var request = indexedDB.open(name, 1);
        request.onsuccess = function(event) {
          var db = event.target.result;
          let objectStore = db.transaction("Foo").objectStore("Foo");
          request = objectStore.get(42);
          request.onsuccess = function(event) {
            try {
              let result = request.result;
              postMessage("error");
            }
            catch (e) {
              postMessage("success");
            }
          }
          request.onerror = function(event) {
            postMessage("error");
          }
        }
        request.onerror = function(event) {
          postMessage("error");
        }
      }
    }

    url = URL.createObjectURL(new Blob(["(", workerScript.toSource(), ")()"]));

    let worker3 = new Worker(url);
    URL.revokeObjectURL(url);
    worker3.postMessage(name);
    worker3.onmessage = grabEventAndContinueHandler;
    event = yield undefined;

    is(event.data, "success", "Good response.");

    todo(false, "Terminate all workers at the end of the test to work around bug 1340941.");
    worker1.terminate();
    worker2.terminate();
    worker3.terminate();

    finishTest();
    yield undefined;
  }
  </script>
  <script type="text/javascript;version=1.7" src="file.js"></script>
  <script type="text/javascript;version=1.7" src="helpers.js"></script>

</head>

<body onload="runTest();"></body>

</html>