Html程序  |  180行  |  5.64 KB

<html>
<head>
<link rel="stylesheet" href="../../fast/js/resources/js-test-style.css">
<script src="../../fast/js/resources/js-test-pre.js"></script>
<script src="../../fast/js/resources/js-test-post-function.js"></script>
<script src="resources/shared.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>
<script>

description("Test IndexedDB's IDBObjectStore auto-increment feature.");
if (window.layoutTestController)
    layoutTestController.waitUntilDone();

function test()
{
    request = evalAndLog("webkitIndexedDB.open('objectstore-autoincrement')");
    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;
    trans.oncomplete = setVersionCompleted;

    deleteAllObjectStores(db);

    debug("createObjectStore():");
    window.store = evalAndLog("store = db.createObjectStore('StoreWithKeyPath', {keyPath: 'id', autoIncrement: true})");
    evalAndLog("db.createObjectStore('StoreWithAutoIncrement', {autoIncrement: true})");
    evalAndLog("db.createObjectStore('PlainOldStore', {autoIncrement: false})");
    var storeNames = evalAndLog("storeNames = db.objectStoreNames");

    shouldBeEqualToString("store.name", "StoreWithKeyPath");
    shouldBe("store.keyPath", "'id'");
    shouldBe("storeNames.contains('StoreWithKeyPath')", "true");
    shouldBe("storeNames.contains('StoreWithAutoIncrement')", "true");
    shouldBe("storeNames.contains('PlainOldStore')", "true");
    shouldBe("storeNames.length", "3");

    // Let the setVersion transaction complete.
}

function setVersionCompleted()
{
    debug("setVersionCompleted():");

    window.trans = evalAndLog("trans = db.transaction([], webkitIDBTransaction.READ_WRITE)");
    trans.onabort = unexpectedAbortCallback;
    trans.oncomplete = done;

    window.store = evalAndLog("store = trans.objectStore('StoreWithKeyPath')");

    debug("Insert into object store with auto increment and key path, with key in the object.");
    request = evalAndLog("store.add({name: 'Jeffersson', number: '7010', id: 3})");
    request.onsuccess = addJefferssonSuccess;
    request.onerror = unexpectedErrorCallback;
}

function addJefferssonSuccess()
{
    debug("addJefferssonSuccess():");
    shouldBe("event.target.result", "3");

    debug("Insert into object store with auto increment and key path, without key in the object.");
    request = evalAndLog("store.add({name: 'Lincoln', number: '7012'})");
    request.onsuccess = addLincolnWithInjectKeySuccess;
    request.onerror = unexpectedErrorCallback;
}

function addLincolnWithInjectKeySuccess()
{
    debug("addLincolnWithInjectKeySuccess():");
    shouldBe("event.target.result", "4");

    result = evalAndLog("store.get(4)");
    result.onsuccess = getLincolnAfterInjectedKeySuccess;
    result.onerror = unexpectedErrorCallback;
}

function getLincolnAfterInjectedKeySuccess()
{
    debug("getLincolnAfterInjectedKeySuccess():");
    shouldBeEqualToString("event.target.result.name", "Lincoln");
    shouldBeEqualToString("event.target.result.number", "7012");
    shouldBe("event.target.result.id", "4");

    window.store = evalAndLog("store = trans.objectStore('StoreWithAutoIncrement')");
    debug("Insert into object store with key gen using explicit key");
    request = evalAndLog("store.add({name: 'Lincoln', number: '7012'}, 5)");
    request.onsuccess = addLincolnWithExplicitKeySuccess;
    request.onerror = unexpectedErrorCallback;
}

function addLincolnWithExplicitKeySuccess()
{
    debug("addLincolnWithExplicitKeySuccess():");
    shouldBe("event.target.result", "5");

    request = evalAndLog("store.get(5)");
    request.onsuccess = getLincolnSuccess;
    request.onerror = unexpectedErrorCallback;
}

function getLincolnSuccess()
{
    debug("getLincolnSuccess():");
    shouldBeEqualToString("event.target.result.name", "Lincoln");
    shouldBeEqualToString("event.target.result.number", "7012");

    request = evalAndLog("store.put({name: 'Abraham', number: '2107'})");
    request.onsuccess = putAbrahamSuccess;
    request.onerror = unexpectedErrorCallback;
}

function putAbrahamSuccess()
{
    debug("putAbrahamSuccess():");
    shouldBe("event.target.result", "6");

    request = evalAndLog("store.get(6)");
    request.onsuccess = getAbrahamSuccess;
    request.onerror = unexpectedErrorCallback;
}

function getAbrahamSuccess()
{
    debug("getAbrahamSuccess():");
    shouldBeEqualToString("event.target.result.name", "Abraham");
    shouldBeEqualToString("event.target.result.number", "2107");

    window.store = evalAndLog("store = trans.objectStore('PlainOldStore')");
    debug("Try adding with no key to object store without auto increment.");
    request = evalAndLog("store.add({name: 'Adam'})");
    request.onsuccess = unexpectedSuccessCallback;
    request.onerror = addAdamError;
}

function addAdamError()
{
    debug("addAdamError():");
    shouldBe("event.target.errorCode", "webkitIDBDatabaseException.DATA_ERR");

    evalAndLog("event.preventDefault()");

    request = evalAndLog("store.add({name: 'Adam'}, 1)");
    request.onsuccess = addAdamSuccess;
    request.onerror = unexpectedErrorCallback;
}

function addAdamSuccess()
{
    debug("addAdamSuccess():");
    shouldBe("event.target.result", "1");
}

test();

var successfullyParsed = true;

</script>
</body>
</html>