blob: 0917854f99c851fc91f59ef5377bd73ba583afd7 [file] [log] [blame]
<html>
<head>
<script src="../../fast/js/resources/js-test-pre.js"></script>
<script src="resources/shared.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>
<script>
description("Test the basics of IndexedDB's IDBObjectStore.");
if (window.layoutTestController)
layoutTestController.waitUntilDone();
function test()
{
request = evalAndLog("webkitIndexedDB.open('objectstore-basics')");
request.onsuccess = openSuccess;
request.onerror = unexpectedErrorCallback;
}
function openSuccess()
{
debug("openSuccess():");
window.db = evalAndLog("db = event.target.result");
request = evalAndLog("db.setVersion('new version')");
request.onsuccess = setVersionSuccess;
request.onerror = unexpectedErrorCallback;
}
function setVersionSuccess()
{
debug("setVersionSuccess():");
window.trans = evalAndLog("trans = event.target.result");
shouldBeTrue("trans !== null");
trans.onabort = unexpectedAbortCallback;
deleteAllObjectStores(db);
debug("createObjectStore():");
window.store = evalAndLog("store = db.createObjectStore('storeName', null)");
var storeNames = evalAndLog("storeNames = db.objectStoreNames");
shouldBeEqualToString("store.name", "storeName");
shouldBeNull("store.keyPath");
shouldBe("storeNames.contains('storeName')", "true");
shouldBe("storeNames.length", "1");
// FIXME: test all of object store's methods.
debug("Ask for an index that doesn't exist:");
try {
debug("index = store.index('asdf')");
index = store.index('asdf');
testFailed("Asking for a store that doesn't exist should have thrown.");
} catch (err) {
testPassed("Exception thrown.");
code = err.code;
shouldBe("code", "webkitIDBDatabaseException.NOT_FOUND_ERR");
}
createIndex();
}
function createIndex()
{
debug("createIndex():");
var index = evalAndLog("index = store.createIndex('indexName', 'x', {unique: true})"); // true == unique requirement.
shouldBeTrue("index !== null");
shouldBeTrue("store.indexNames.contains('indexName')");
index = evalAndLog("index = store.index('indexName')");
shouldBeTrue("index !== null");
debug("Ask for an index that doesn't exist:");
try {
debug("index = store.index('asdf')");
index = store.index('asdf');
testFailed("Asking for a store that doesn't exist should have thrown.");
} catch (err) {
testPassed("Exception thrown.");
code = err.code
shouldBe("code", "webkitIDBDatabaseException.NOT_FOUND_ERR");
}
commitAndContinue();
}
function commitAndContinue()
{
window.setTimeout(testSetVersionAbort, 0);
}
function testSetVersionAbort()
{
request = evalAndLog('db.setVersion("version fail")');
request.onsuccess = createAnotherIndex;
request.onerror = unexpectedErrorCallback;
}
function createAnotherIndex()
{
shouldBeEqualToString("db.version", "version fail");
var setVersionTrans = evalAndLog("setVersionTrans = event.target.result");
shouldBeTrue("setVersionTrans !== null");
setVersionTrans.oncomplete = unexpectedCompleteCallback;
setVersionTrans.onabort = checkMetadata;
window.store = evalAndLog("store = setVersionTrans.objectStore('storeName')");
var index = evalAndLog("index = store.createIndex('indexFail', 'x')");
setVersionTrans.abort();
}
function checkMetadata()
{
shouldBeEqualToString("db.version", "new version");
shouldBe("store.transaction", "setVersionTrans");
shouldBe("store.indexNames", "['indexName']");
shouldBe("store.indexNames.length", "1");
shouldBe("store.indexNames.contains('')", "false");
shouldBe("store.indexNames.contains('indexFail')", "false");
shouldBe("store.indexNames.contains('indexName')", "true");
shouldBeEqualToString("store.indexNames[0]", "indexName");
shouldBeNull("store.indexNames[1]");
shouldBeNull("store.indexNames[100]");
shouldBeNull("store.indexNames.item(1)");
shouldBeNull("store.indexNames.item(100)");
addData();
}
var testDate = new Date("August 25, 1991 20:57:08");
var testDateB = new Date("Wed Jan 05 2011 15:54:49");
function addData()
{
var transaction = evalAndLog("transaction = db.transaction(['storeName'], webkitIDBTransaction.READ_WRITE)");
transaction.onabort = unexpectedAbortCallback;
window.store = evalAndLog("store = transaction.objectStore('storeName')");
debug("Try to insert data with a Date key:");
request = evalAndLog("store.add({x: 'foo'}, testDate)");
request.onsuccess = addDateSuccess;
request.onerror = unexpectedErrorCallback;
}
function addDateSuccess()
{
debug("Try to insert a value not handled by structured clone:");
try {
debug("store.add({x: 'bar', y: document.getElementById('console')}, 'bar')");
store.add({x: 'bar', y: document.getElementById('console')}, 'bar');
testFailed("Passing a DOM node as value should have thrown.");
} catch (err) {
testPassed("Exception thrown");
code = err.code;
shouldBe("code", "DOMException.DATA_CLONE_ERR");
}
debug("Try to insert data where key path yields a Date key:");
request = evalAndLog("store.add({x: testDateB, y: 'value'}, 'key')");
request.onsuccess = addSuccess;
request.onerror = unexpectedErrorCallback;
}
function addSuccess()
{
debug("addSuccess():");
shouldBeEqualToString("event.target.result", "key");
request = evalAndLog("event.target.source.add({x: 'foo'}, 'zzz')");
request.onsuccess = unexpectedSuccessCallback;
request.addEventListener('error', addAgainFailure, false);
}
function addAgainFailure()
{
debug("addAgainFailure():");
shouldBe("event.target.errorCode", "webkitIDBDatabaseException.CONSTRAINT_ERR");
evalAndLog("event.preventDefault()");
transaction = evalAndLog("db.transaction(['storeName'], webkitIDBTransaction.READ_WRITE)");
transaction.onabort = unexpectedErrorCallback;
var store = evalAndLog("store = transaction.objectStore('storeName')");
evalAndLog("store.add({x: 'somevalue'}, 'somekey')");
evalAndExpectException("store.add({x: 'othervalue'}, null)", "webkitIDBDatabaseException.DATA_ERR");
transaction = evalAndLog("db.transaction(['storeName'], webkitIDBTransaction.READ_WRITE)");
transaction.onabort = unexpectedErrorCallback;
var store = evalAndLog("store = transaction.objectStore('storeName')");
request = evalAndLog("store.add({x: null}, 'validkey')");
request.onsuccess = unexpectedSuccessCallback;
request.onerror = addWithNullIndexFailure;
}
function addWithNullIndexFailure()
{
shouldBeTrue("event.cancelable");
debug("addWithNullIndexFailure():");
shouldBe("event.target.errorCode", "webkitIDBDatabaseException.DATA_ERR");
evalAndLog("event.preventDefault()");
transaction = evalAndLog("db.transaction(['storeName'], webkitIDBTransaction.READ_WRITE)");
transaction.onabort = unexpectedErrorCallback;
var store = evalAndLog("store = transaction.objectStore('storeName')");
request = evalAndLog("store.get('key')");
request.addEventListener('success', getSuccess, true);
request.onerror = unexpectedErrorCallback;
}
function getSuccess()
{
debug("getSuccess():");
shouldBeEqualToString("event.target.result.y", "value");
var store = evalAndLog("store = event.target.source");
request = evalAndLog("store.get(testDate)");
request.addEventListener('success', getSuccessDateKey, false);
request.onerror = unexpectedErrorCallback;
}
function getSuccessDateKey()
{
debug("getSuccessDateKey():");
shouldBeEqualToString("event.target.result.x", "foo");
request = evalAndLog("store.delete('key')");
request.onsuccess = removeSuccess;
request.onerror = unexpectedErrorCallback;
}
function removeSuccess()
{
debug("removeSuccess():");
shouldBe("event.target.result", "true");
request = evalAndLog("store.delete('key')");
request.onsuccess = removeSuccessButFalse;
request.onerror = unexpectedErrorCallback;
}
function removeSuccessButFalse()
{
debug("removeSuccessButFalse():");
shouldBe("event.target.result", "false");
var store = evalAndLog("store = event.target.source");
debug("Passing an invalid key into store.get().");
evalAndExpectException("store.get([])", "webkitIDBDatabaseException.DATA_ERR");
debug("Passing an invalid key into store.delete().");
evalAndExpectException("store.delete([])", "webkitIDBDatabaseException.DATA_ERR");
debug("Passing an invalid key into store.add().");
evalAndExpectException("store.add(null, [])", "webkitIDBDatabaseException.DATA_ERR");
debug("Passing an invalid key into store.put().");
evalAndExpectException("store.put(null, [])", "webkitIDBDatabaseException.DATA_ERR");
done();
}
test();
</script>
</body>
</html>