gecko/dom/indexedDB/test/test_transaction_abort.html

254 lines
7.3 KiB
HTML

<!--
Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/
-->
<html>
<head>
<title>Indexed Database Property Test</title>
<script type="text/javascript" src="/MochiKit/packed.js"></script>
<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 Ci = Components.interfaces;
const LOADING = Ci.nsIIDBTransaction.LOADING;
const DONE = Ci.nsIIDBTransaction.DONE;
const READ_ONLY = Ci.nsIIDBTransaction.READ_ONLY;
const READ_WRITE = Ci.nsIIDBTransaction.READ_WRITE;
const VERSION_CHANGE = Ci.nsIIDBTransaction.VERSION_CHANGE;
const name = window.location.pathname;
const description = "My Test Database";
let request = mozIndexedDB.open(name, description);
request.onerror = errorHandler;
request.onsuccess = grabEventAndContinueHandler;
let event = yield;
let db = event.result;
let transaction;
let objectStore;
request = db.setVersion("1");
request.onerror = errorHandler;
request.onsuccess = grabEventAndContinueHandler;
event = yield;
transaction = event.transaction;
objectStore = db.createObjectStore("foo", { autoIncrement: true });
is(transaction.db, db, "Correct database");
is(transaction.readyState, LOADING, "Correct readyState");
is(transaction.mode, VERSION_CHANGE, "Correct mode");
is(transaction.objectStoreNames.length, 1, "Correct names length");
is(transaction.objectStoreNames.item(0), "foo", "Correct name");
is(transaction.objectStore("foo").name, "foo", "Can get stores");
is(transaction.oncomplete, null, "No complete listener");
is(transaction.onabort, null, "No abort listener");
is(transaction.ontimeout, null, "No timeout listener");
is(objectStore.name, "foo", "Correct name");
is(objectStore.keyPath, "", "Correct keyPath");
is(objectStore.indexNames.length, 0, "Correct indexNames");
// Wait until it's complete!
transaction.oncomplete = grabEventAndContinueHandler;
event = yield;
is(transaction.db, db, "Correct database");
is(transaction.readyState, DONE, "Correct readyState");
is(transaction.mode, VERSION_CHANGE, "Correct mode");
is(transaction.objectStoreNames.length, 1, "Correct names length");
is(transaction.objectStoreNames.item(0), "foo", "Correct name");
is(transaction.onabort, null, "No abort listener");
is(transaction.ontimeout, null, "No timeout listener");
try {
is(transaction.objectStore("foo").name, "foo", "Can't get stores");
ok(false, "Should have thrown");
}
catch (e) {
ok(true, "Out of scope transaction can't make stores");
}
is(objectStore.name, "foo", "Correct name");
is(objectStore.keyPath, "", "Correct keyPath");
is(objectStore.indexNames.length, 0, "Correct indexNames");
try {
objectStore.add({});
ok(false, "Should have thrown");
}
catch (e) {
ok(true, "Add threw");
}
try {
objectStore.put({}, 1);
ok(false, "Should have thrown");
}
catch (e) {
ok(true, "Put threw");
}
try {
objectStore.put({}, 1);
ok(false, "Should have thrown");
}
catch (e) {
ok(true, "Put threw");
}
try {
objectStore.delete(1);
ok(false, "Should have thrown");
}
catch (e) {
ok(true, "Remove threw");
}
try {
objectStore.get(1);
ok(false, "Should have thrown");
}
catch (e) {
ok(true, "Get threw");
}
try {
objectStore.getAll(null);
ok(false, "Should have thrown");
}
catch (e) {
ok(true, "GetAll threw");
}
try {
objectStore.openCursor();
ok(false, "Should have thrown");
}
catch (e) {
ok(true, "OpenCursor threw");
}
try {
objectStore.createIndex("bar", "id");
ok(false, "Should have thrown");
}
catch (e) {
ok(true, "CreateIndex threw");
}
try {
objectStore.index("bar");
ok(false, "Should have thrown");
}
catch (e) {
ok(true, "Index threw");
}
try {
objectStore.deleteIndex("bar");
ok(false, "Should have thrown");
}
catch (e) {
ok(true, "RemoveIndex threw");
}
request = db.transaction("foo", READ_WRITE).objectStore("foo").add({});
request.onerror = errorHandler;
request.onsuccess = grabEventAndContinueHandler;
event = yield;
event.transaction.onabort = function(event) {
ok(false, "Shouldn't see an abort event!");
};
event.transaction.oncomplete = grabEventAndContinueHandler;
event = yield;
is(event.type, "complete", "Right kind of event");
let key;
request = db.transaction("foo", READ_WRITE).objectStore("foo").add({});
request.onerror = errorHandler;
request.onsuccess = grabEventAndContinueHandler;
event = yield;
key = event.result;
event.transaction.onabort = grabEventAndContinueHandler;
event.transaction.oncomplete = function(event) {
ok(false, "Shouldn't see a complete event here!");
};
is(event.transaction.readyState, LOADING, "Correct readyState");
event.transaction.abort();
is(event.transaction.readyState, DONE, "Correct readyState");
event = yield;
is(event.type, "abort", "Right kind of event");
request = db.transaction("foo").objectStore("foo").get(key);
request.onerror = errorHandler;
request.onsuccess = grabEventAndContinueHandler;
event = yield;
is(event.result, undefined, "Object was removed");
SimpleTest.executeSoon(function() { testGenerator.next(); });
yield;
let keys = [];
let abortEventCount = 0;
objectStore = db.transaction("foo", READ_WRITE).objectStore("foo");
for (let i = 0; i < 10; i++) {
request = objectStore.add({});
request.onerror = function(event) {
is(event.code, IDBDatabaseException.ABORT_ERR, "Good code");
abortEventCount++;
event.preventDefault();
};
request.onsuccess = function(event) {
keys.push(event.result);
if (keys.length == 5) {
event.transaction.onabort = grabEventAndContinueHandler;
event.transaction.abort();
}
};
}
event = yield;
is(event.type, "abort", "Got abort event");
is(keys.length, 5, "Added 5 items in this transaction");
is(abortEventCount, 5, "Got 5 abort error events");
for (let i in keys) {
request = db.transaction("foo").objectStore("foo").get(keys[i]);
request.onerror = errorHandler;
request.onsuccess = grabEventAndContinueHandler;
event = yield;
is(event.result, undefined, "Object was removed by abort");
}
finishTest();
yield;
}
</script>
<script type="text/javascript;version=1.7" src="helpers.js"></script>
</head>
<body onload="runTest();"></body>
</html>