blob: 3dcb7330a8a25a2ef8f1d895a2c4bcf23784533e [file] [log] [blame]
<!DOCTYPE html>
<meta charset=utf-8>
<title>IDBObjectStore.createIndex() - event order when unique constraint is triggered</title>
<link rel="author" href="" title="Odin Hørthe Omdal">
<meta rel=help href=>
<meta rel=assert title="The index that is requested to be created can contain constraints on the data allowed in the index's referenced object store, such as requiring uniqueness of the values referenced by the index's keyPath. If the referenced object store already contains data which violates these constraints, this must not cause the implementation of createIndex to throw an exception or affect what it returns. The implementation must still create and return an IDBIndex object. Instead the implementation must queue up an operation to abort the 'versionchange' transaction which was used for the createIndex call.">
<meta rel=assert title="In some implementations it's possible for the implementation to asynchronously run into problems creating the index after the createIndex function has returned. For example in implementations where metadata about the newly created index is queued up to be inserted into the database asynchronously, or where the implementation might need to ask the user for permission for quota reasons. Such implementations must still create and return an IDBIndex object. Instead, once the implementation realizes that creating the index has failed, it must abort the transaction using the steps for aborting a transaction using the appropriate error as error parameter.">
<meta rel=assert title="if the index can't be created due to unique constraints, ConstraintError must be used as error">
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=support.js></script>
// Transaction may fire window.onerror in some implementations.
var db,
events = [],
t = async_test(document.title, {timeout: 10000})
var open_rq = createdb(t);
open_rq.onupgradeneeded = function(e) {
db =;
db.onerror = log("db.error");
db.onabort = log("db.abort"); = log("transaction.abort") = log("transaction.error") = log("transaction.complete")
var txn =,
objStore = db.createObjectStore("store");
var rq_add1 = objStore.add({ animal: "Unicorn" }, 1);
rq_add1.onsuccess = log("rq_add1.success");
rq_add1.onerror = log("rq_add1.error");
var rq_add2 = objStore.add({ animal: "Unicorn" }, 2);
rq_add2.onsuccess = log("rq_add2.success");
rq_add2.onerror = log("rq_add2.error");
objStore.createIndex("index", "animal", { unique: true })
var rq_add3 = objStore.add({ animal: "Unicorn" }, 3);
rq_add3.onsuccess = log("rq_add3.success");
rq_add3.onerror = log("rq_add3.error");
open_rq.onerror = function(e) {
assert_array_equals(events, [ "rq_add1.success",
"rq_add3.error: AbortError",
"transaction.error: AbortError",
"db.error: AbortError",
"transaction.abort: ConstraintError",
"db.abort: ConstraintError",
"open_rq.error: AbortError" ],
function log(msg) {
return function(e) {
if(e && &&
events.push(msg + ": " +;
<div id=log></div>