<!doctype html>
<meta charset=utf-8>
<script src="/resources/testharness.js"></script>
<script src="support.js"></script>
<!-- TODO(crbug.com/1218100): We should verify the full IDB surface area inside
an iframe, but for now a single test with an assortment of verifications is
sufficient to test third party storage partitioning didn't break anything. -->

<!-- This block is from delete-request-queue.htm -->
<script>
let saw;
indexeddb_test(
    (t, db) => {
        saw = expect(t, ['delete1', 'delete2']);
        let r = indexedDB.deleteDatabase(db.name);
        r.onerror = t.unreached_func('delete should succeed');
        r.onsuccess = t.step_func(e => saw('delete1'));
    },
    (t, db) => {
        let r = indexedDB.deleteDatabase(db.name);
        r.onerror = t.unreached_func('delete should succeed');
        r.onsuccess = t.step_func(e => saw('delete2'));

        db.close();
    },
    'Deletes are processed in order');
</script>

<!-- This block is from idbcursor-advance-continue-async.htm -->
<script>
function upgrade_func(t, db, tx) {
  var objStore = db.createObjectStore("test");
  objStore.createIndex("index", "");

  objStore.add("data",  1);
  objStore.add("data2", 2);
}

indexeddb_test(
  upgrade_func,
  function(t, db) {
    var count = 0;
    var rq = db.transaction("test", "readonly", {durability: 'relaxed'}).objectStore("test").openCursor();

    rq.onsuccess = t.step_func(function(e) {
      if (!e.target.result) {
        assert_equals(count, 2, 'count');
        t.done();
        return;
      }
      var cursor = e.target.result;

      switch(count) {
        case 0:
          assert_equals(cursor.value, "data")
          assert_equals(cursor.key, 1)
          cursor.advance(1)
          assert_equals(cursor.value, "data")
          assert_equals(cursor.key, 1)
          break

        case 1:
          assert_equals(cursor.value, "data2")
          assert_equals(cursor.key, 2)
          cursor.advance(1)
          assert_equals(cursor.value, "data2")
          assert_equals(cursor.key, 2)
          break

        default:
          assert_unreached("Unexpected count: " + count)
      }

      count++;
    });
    rq.onerror = t.unreached_func("unexpected error")
  },
  document.title + " - advance"
);

indexeddb_test(
  upgrade_func,
  function(t, db) {
    var count = 0;
    var rq = db.transaction("test", "readonly", {durability: 'relaxed'}).objectStore("test").index("index").openCursor();

    rq.onsuccess = t.step_func(function(e) {
      if (!e.target.result) {
        assert_equals(count, 2, 'count');
        t.done();
        return;
      }
      var cursor = e.target.result;

      switch(count) {
        case 0:
          assert_equals(cursor.value, "data")
          assert_equals(cursor.key,   "data")
          assert_equals(cursor.primaryKey, 1)
          cursor.continue("data2")
          assert_equals(cursor.value, "data")
          assert_equals(cursor.key,   "data")
          assert_equals(cursor.primaryKey, 1)
          break

        case 1:
          assert_equals(cursor.value, "data2")
          assert_equals(cursor.key,   "data2")
          assert_equals(cursor.primaryKey, 2)
          cursor.continue()
          assert_equals(cursor.value, "data2")
          assert_equals(cursor.key,   "data2")
          assert_equals(cursor.primaryKey, 2)
          break

        default:
          assert_unreached("Unexpected count: " + count)
      }

      count++;
    });
    rq.onerror = t.unreached_func("unexpected error")
  },
  document.title + " - continue"
);

indexeddb_test(
  upgrade_func,
  function(t, db) {
    var count = 0;
    var rq = db.transaction("test", "readonly", {durability: 'relaxed'}).objectStore("test").index("index").openCursor();

    rq.onsuccess = t.step_func(function(e) {
      if (!e.target.result) {
        assert_equals(count, 2, 'count');
        t.done();
        return;
      }
      var cursor = e.target.result;
      cursor.advance(1)

      switch(count) {
        case 0:
          assert_equals(cursor.value, "data")
          assert_equals(cursor.key,   "data")
          assert_equals(cursor.primaryKey, 1)
          break

        case 1:
          assert_equals(cursor.value, "data2")
          assert_equals(cursor.key,   "data2")
          assert_equals(cursor.primaryKey, 2)
          break

        default:
          assert_unreached("Unexpected count: " + count)
      }

      count++;
    });
    rq.onerror = t.unreached_func("unexpected error")
  },
  document.title + " - fresh advance still async"
);

indexeddb_test(
  upgrade_func,
  function(t, db) {
    var count = 0;
    var rq = db.transaction("test", "readonly", {durability: 'relaxed'}).objectStore("test").openCursor();

    rq.onsuccess = t.step_func(function(e) {
      if (!e.target.result) {
        assert_equals(count, 2, 'count');
        t.done();
        return;
      }
      var cursor = e.target.result;
      cursor.continue()

      switch(count) {
        case 0:
          assert_equals(cursor.value, "data")
          assert_equals(cursor.key, 1)
          break

        case 1:
          assert_equals(cursor.value, "data2")
          assert_equals(cursor.key, 2)
          break

        default:
          assert_unreached("Unexpected count: " + count)
      }

      count++;
    });
    rq.onerror = t.unreached_func("unexpected error")
  },
  document.title + " - fresh continue still async"
);
</script>

<!-- This block is from idbindex_getAll.htm -->
<script>
var alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('');
var ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');

function getall_test(func, name) {
  indexeddb_test(
    function(t, connection, tx) {
      var store = connection.createObjectStore('generated',
            {autoIncrement: true, keyPath: 'id'});
      var index = store.createIndex('test_idx', 'upper');
      alphabet.forEach(function(letter) {
        store.put({ch: letter, upper: letter.toUpperCase()});
      });

      store = connection.createObjectStore('out-of-line', null);
      index = store.createIndex('test_idx', 'upper');
      alphabet.forEach(function(letter) {
        store.put({ch: letter, upper: letter.toUpperCase()}, letter);
      });

      store = connection.createObjectStore('out-of-line-not-unique', null);
      index = store.createIndex('test_idx', 'half');
      alphabet.forEach(function(letter) {
        if (letter <= 'm')
          store.put({ch: letter, half: 'first'}, letter);
        else
          store.put({ch: letter, half: 'second'}, letter);
      });

      store = connection.createObjectStore('out-of-line-multi', null);
      index = store.createIndex('test_idx', 'attribs', {multiEntry: true});
      alphabet.forEach(function(letter) {
        attrs = [];
        if (['a', 'e', 'i', 'o', 'u'].indexOf(letter) != -1)
          attrs.push('vowel');
        else
          attrs.push('consonant');
        if (letter == 'a')
          attrs.push('first');
        if (letter == 'z')
          attrs.push('last');
        store.put({ch: letter, attribs: attrs}, letter);
      });

      store = connection.createObjectStore('empty', null);
      index = store.createIndex('test_idx', 'upper');
    },
    func,
    name
  );
}

function createGetAllRequest(t, storeName, connection, range, maxCount) {
    var transaction = connection.transaction(storeName, 'readonly');
    var store = transaction.objectStore(storeName);
    var index = store.index('test_idx');
    var req = index.getAll(range, maxCount);
    req.onerror = t.unreached_func('getAll request should succeed');
    return req;
}

getall_test(function(t, connection) {
      var req = createGetAllRequest(t, 'out-of-line', connection, 'C');
      req.onsuccess = t.step_func(function(evt) {
          var data = evt.target.result;
          assert_class_string(data, 'Array', 'result should be an array');
          assert_array_equals(data.map(function(e) { return e.ch; }), ['c']);
          assert_array_equals(data.map(function(e) { return e.upper; }), ['C']);
          t.done();
      });
    }, 'Single item get');

getall_test(function(t, connection) {
      var req = createGetAllRequest(t, 'empty', connection);
      req.onsuccess = t.step_func(function(evt) {
          assert_array_equals(evt.target.result, [],
              'getAll() on empty object store should return an empty array');
          t.done();
      });
    }, 'Empty object store');

getall_test(function(t, connection) {
      var req = createGetAllRequest(t, 'out-of-line', connection);
      req.onsuccess = t.step_func(function(evt) {
          var data = evt.target.result;
          assert_class_string(data, 'Array', 'result should be an array');
          assert_array_equals(data.map(function(e) { return e.ch; }), alphabet);
          assert_array_equals(data.map(function(e) { return e.upper; }), ALPHABET);
          t.done();
      });
    }, 'Get all keys');

getall_test(function(t, connection) {
      var req = createGetAllRequest(t, 'out-of-line', connection, undefined,
                                    10);
      req.onsuccess = t.step_func(function(evt) {
          var data = evt.target.result;
          assert_class_string(data, 'Array', 'result should be an array');
          assert_array_equals(data.map(function(e) { return e.ch; }), 'abcdefghij'.split(''));
          assert_array_equals(data.map(function(e) { return e.upper; }), 'ABCDEFGHIJ'.split(''));
          t.done();
      });
    }, 'maxCount=10');

getall_test(function(t, connection) {
      var req = createGetAllRequest(t, 'out-of-line', connection,
                                    IDBKeyRange.bound('G', 'M'));
      req.onsuccess = t.step_func(function(evt) {
          var data = evt.target.result;
          assert_array_equals(data.map(function(e) { return e.ch; }), 'ghijklm'.split(''));
          assert_array_equals(data.map(function(e) { return e.upper; }), 'GHIJKLM'.split(''));
          t.done();
      });
    }, 'Get bound range');

getall_test(function(t, connection) {
      var req = createGetAllRequest(t, 'out-of-line', connection,
                                    IDBKeyRange.bound('G', 'M'), 3);
      req.onsuccess = t.step_func(function(evt) {
          var data = evt.target.result;
          assert_class_string(data, 'Array', 'result should be an array');
          assert_array_equals(data.map(function(e) { return e.ch; }), 'ghi'.split(''));
          assert_array_equals(data.map(function(e) { return e.upper; }), 'GHI'.split(''));
          t.done();
      });
    }, 'Get bound range with maxCount');

getall_test(function(t, connection) {
      var req = createGetAllRequest(t, 'out-of-line', connection,
          IDBKeyRange.bound('G', 'K', false, true));
      req.onsuccess = t.step_func(function(evt) {
          var data = evt.target.result;
          assert_class_string(data, 'Array', 'result should be an array');
          assert_array_equals(data.map(function(e) { return e.ch; }), 'ghij'.split(''));
          assert_array_equals(data.map(function(e) { return e.upper; }), 'GHIJ'.split(''));
          t.done();
      });
    }, 'Get upper excluded');

getall_test(function(t, connection) {
      var req = createGetAllRequest(t, 'out-of-line', connection,
          IDBKeyRange.bound('G', 'K', true, false));
      req.onsuccess = t.step_func(function(evt) {
          var data = evt.target.result;
          assert_class_string(data, 'Array', 'result should be an array');
          assert_array_equals(data.map(function(e) { return e.ch; }), 'hijk'.split(''));
          assert_array_equals(data.map(function(e) { return e.upper; }), 'HIJK'.split(''));
          t.done();
      });
    }, 'Get lower excluded');

getall_test(function(t, connection) {
      var req = createGetAllRequest(t, 'generated',
          connection, IDBKeyRange.bound(4, 15), 3);
      req.onsuccess = t.step_func(function(evt) {
          var data = evt.target.result;
          assert_true(Array.isArray(data));
          assert_equals(data.length, 0);
          t.done();
      });
    }, 'Get bound range (generated) with maxCount');

getall_test(function(t, connection) {
      var req = createGetAllRequest(t, 'out-of-line',
          connection, "Doesn't exist");
      req.onsuccess = t.step_func(function(evt) {
          assert_array_equals(evt.target.result, [],
              'getAll() using a nonexistent key should return an empty array');
          t.done();
      req.onerror = t.unreached_func('getAll request should succeed');
      });
    }, 'Non existent key');

getall_test(function(t, connection) {
      var req = createGetAllRequest(t, 'out-of-line', connection,
          undefined, 0);
      req.onsuccess = t.step_func(function(evt) {
          var data = evt.target.result;
          assert_class_string(data, 'Array', 'result should be an array');
          assert_array_equals(data.map(function(e) { return e.ch; }), alphabet);
          assert_array_equals(data.map(function(e) { return e.upper; }), ALPHABET);
          t.done();
      });
    }, 'maxCount=0');

getall_test(function(t, connection) {
      var req = createGetAllRequest(t, 'out-of-line-not-unique', connection,
                                    'first');
      req.onsuccess = t.step_func(function(evt) {
          var data = evt.target.result;
          assert_class_string(data, 'Array', 'result should be an array');
          assert_array_equals(data.map(function(e) { return e.ch; }), 'abcdefghijklm'.split(''));
          assert_true(data.every(function(e) { return e.half === 'first'; }));
          t.done();
      });
    }, 'Retrieve multiEntry key');

getall_test(function(t, connection) {
      var req = createGetAllRequest(t, 'out-of-line-multi', connection,
                                    'vowel');
      req.onsuccess = t.step_func(function(evt) {
          var data = evt.target.result;
          assert_class_string(data, 'Array', 'result should be an array');
          assert_array_equals(data.map(function(e) { return e.ch; }), ['a', 'e', 'i', 'o', 'u']);
          assert_array_equals(data[0].attribs, ['vowel', 'first']);
          assert_true(data.every(function(e) { return e.attribs[0] === 'vowel'; }));
          t.done();
      });
    }, 'Retrieve one key multiple values');
</script>

<!-- This block is from idbobjectstore_openKeyCursor.htm -->
<script>
function store_test(func, name) {
  indexeddb_test(
    function(t, db, tx) {
      var store = db.createObjectStore("store");
      for (var i = 0; i < 10; ++i) {
        store.put("value: " + i, i);
      }
    },
    function(t, db) {
      var tx = db.transaction("store", "readonly", {durability: 'relaxed'});
      var store = tx.objectStore("store");
      func(t, db, tx, store);
    }, name);
}

store_test(function(t, db, tx, store) {
    var expected = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
    var actual = [];
    var request = store.openKeyCursor();
    request.onsuccess = t.step_func(function() {
        var cursor = request.result;
        if (!cursor)
            return;
        assert_equals(cursor.direction, "next");
        assert_false("value" in cursor);
        assert_equals(indexedDB.cmp(cursor.key, cursor.primaryKey), 0);
        actual.push(cursor.key);
        cursor.continue();
    });

    tx.onabort = t.unreached_func("transaction aborted");
    tx.oncomplete = t.step_func(function() {
        assert_array_equals(expected, actual, "keys should match");
        t.done();
    });

}, "IDBObjectStore.openKeyCursor() - forward iteration");

store_test(function(t, db, tx, store) {
    var expected = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0];
    var actual = [];
    var request = store.openKeyCursor(null, "prev");
    request.onsuccess = t.step_func(function() {
        var cursor = request.result;
        if (!cursor)
            return;
        assert_equals(cursor.direction, "prev");
        assert_false("value" in cursor);
        assert_equals(indexedDB.cmp(cursor.key, cursor.primaryKey), 0);
        actual.push(cursor.key);
        cursor.continue();
    });

    tx.onabort = t.unreached_func("transaction aborted");
    tx.oncomplete = t.step_func(function() {
        assert_array_equals(expected, actual, "keys should match");
        t.done();
    });

}, "IDBObjectStore.openKeyCursor() - reverse iteration");

store_test(function(t, db, tx, store) {
    var expected = [4, 5, 6];
    var actual = [];
    var request = store.openKeyCursor(IDBKeyRange.bound(4, 6));
    request.onsuccess = t.step_func(function() {
        var cursor = request.result;
        if (!cursor)
            return;
        assert_equals(cursor.direction, "next");
        assert_false("value" in cursor);
        assert_equals(indexedDB.cmp(cursor.key, cursor.primaryKey), 0);
        actual.push(cursor.key);
        cursor.continue();
    });

    tx.onabort = t.unreached_func("transaction aborted");
    tx.oncomplete = t.step_func(function() {
        assert_array_equals(expected, actual, "keys should match");
        t.done();
    });

}, "IDBObjectStore.openKeyCursor() - forward iteration with range");

store_test(function(t, db, tx, store) {
    var expected = [6, 5, 4];
    var actual = [];
    var request = store.openKeyCursor(IDBKeyRange.bound(4, 6), "prev");
    request.onsuccess = t.step_func(function() {
        var cursor = request.result;
        if (!cursor)
            return;
        assert_equals(cursor.direction, "prev");
        assert_false("value" in cursor);
        assert_equals(indexedDB.cmp(cursor.key, cursor.primaryKey), 0);
        actual.push(cursor.key);
        cursor.continue();
    });

    tx.onabort = t.unreached_func("transaction aborted");
    tx.oncomplete = t.step_func(function() {
        assert_array_equals(expected, actual, "keys should match");
        t.done();
    });

}, "IDBObjectStore.openKeyCursor() - reverse iteration with range");

store_test(function(t, db, tx, store) {
    assert_throws_dom("DataError", function() { store.openKeyCursor(NaN); },
        "openKeyCursor should throw on invalid number key");
    assert_throws_dom("DataError", function() { store.openKeyCursor(new Date(NaN)); },
        "openKeyCursor should throw on invalid date key");
    assert_throws_dom("DataError", function() {
        var cycle = [];
        cycle.push(cycle);
        store.openKeyCursor(cycle);
    }, "openKeyCursor should throw on invalid array key");
    assert_throws_dom("DataError", function() { store.openKeyCursor({}); },
        "openKeyCursor should throw on invalid key type");
    setTimeout(t.step_func(function() {
        assert_throws_dom("TransactionInactiveError", function() { store.openKeyCursor(); },
            "openKeyCursor should throw if transaction is inactive");
        t.done();
    }), 0);

}, "IDBObjectStore.openKeyCursor() - invalid inputs");
</script>

<!-- This block is from idbtransaction.htm -->
<script>
async_test(function(t) {
    var dbname = "idbtransaction-" + document.location + t.name;
    indexedDB.deleteDatabase(dbname);
    var open_rq = indexedDB.open(dbname);

    open_rq.onblocked = t.unreached_func('open_rq.onblocked');
    open_rq.onerror = t.unreached_func('open_rq.onerror');

    open_rq.onupgradeneeded = t.step_func(function(e) {
    t.add_cleanup(function() {
        open_rq.onerror = function(e) {
        e.preventDefault();
        };
        open_rq.result.close();
        indexedDB.deleteDatabase(open_rq.result.name);
    });

    assert_equals(e.target, open_rq, "e.target is reusing the same IDBOpenDBRequest");
    assert_equals(e.target.transaction, open_rq.transaction, "IDBOpenDBRequest.transaction");

    assert_true(e.target.transaction instanceof IDBTransaction, "transaction instanceof IDBTransaction");
    t.done();
    });

}, document.title + " - request gotten by the handler");

async_test(function(t) {
    var dbname = "idbtransaction-" + document.location + t.name;
    indexedDB.deleteDatabase(dbname);
    var open_rq = indexedDB.open(dbname);

    assert_equals(open_rq.transaction, null, "IDBOpenDBRequest.transaction");
    assert_equals(open_rq.source, null, "IDBOpenDBRequest.source");
    assert_equals(open_rq.readyState, "pending", "IDBOpenDBRequest.readyState");

    assert_true(open_rq instanceof IDBOpenDBRequest, "open_rq instanceof IDBOpenDBRequest");
    assert_equals(open_rq + "", "[object IDBOpenDBRequest]", "IDBOpenDBRequest (open_rq)");

    open_rq.onblocked = t.unreached_func('open_rq.onblocked');
    open_rq.onerror = t.unreached_func('open_rq.onerror');

    open_rq.onupgradeneeded = t.step_func(function() {
    t.add_cleanup(function() {
        open_rq.onerror = function(e) {
        e.preventDefault();
        };
        open_rq.result.close();
        indexedDB.deleteDatabase(open_rq.result.name);
    });
    t.done();
    });

}, document.title + " - request returned by open()");
</script>
