Html程序  |  237行  |  7.86 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 cursor update.");
if (window.layoutTestController)
    layoutTestController.waitUntilDone();

test();

function test()
{
    request = evalAndLog("webkitIndexedDB.open('cursor-update')");
    request.onsuccess = openSuccess;
    request.onerror = unexpectedErrorCallback;
}

function openSuccess()
{
    var 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 = openBasicCursor;

    deleteAllObjectStores(db);

    var objectStore = evalAndLog("objectStore = db.createObjectStore('basicStore')");
    evalAndLog("objectStore.add('myValue1', 'myKey1').onerror = unexpectedErrorCallback");
    evalAndLog("objectStore.add('myValue2', 'myKey2').onerror = unexpectedErrorCallback");
    evalAndLog("objectStore.add('myValue3', 'myKey3').onerror = unexpectedErrorCallback");
    evalAndLog("objectStore.add('myValue4', 'myKey4').onerror = unexpectedErrorCallback");

    var objectStore = evalAndLog("objectStore = db.createObjectStore('autoIncrementStore', {autoIncrement: true})");
    evalAndLog("objectStore.add('foo1').onerror = unexpectedErrorCallback");
    evalAndLog("objectStore.add('foo2').onerror = unexpectedErrorCallback");
    evalAndLog("objectStore.add('foo3').onerror = unexpectedErrorCallback");
    evalAndLog("objectStore.add('foo4').onerror = unexpectedErrorCallback");

    var objectStore = evalAndLog("objectStore = db.createObjectStore('keyPathStore', {keyPath: 'id'})");
    evalAndLog("objectStore.createIndex('numberIndex', 'number')");
    evalAndLog("objectStore.add({number: 1, id: 1}).onerror = unexpectedErrorCallback");
    evalAndLog("objectStore.add({number: 2, id: 2}).onerror = unexpectedErrorCallback");
    evalAndLog("objectStore.add({number: 3, id: 3}).onerror = unexpectedErrorCallback");
    evalAndLog("objectStore.add({number: 4, id: 4}).onerror = unexpectedErrorCallback");
}

function openBasicCursor()
{
    debug("openBasicCursor()");
    evalAndLog("trans = db.transaction([], webkitIDBTransaction.READ_WRITE)");
    trans.onabort = unexpectedAbortCallback;
    trans.oncomplete = transactionComplete;

    keyRange = webkitIDBKeyRange.lowerBound("myKey1");
    window.objectStore = evalAndLog("trans.objectStore('basicStore')");
    request = evalAndLog("objectStore.openCursor(keyRange)");
    request.onsuccess = basicUpdateCursor;
    request.onerror = unexpectedErrorCallback;
    counter = 1;
}

function basicUpdateCursor()
{
    debug("basicUpdateCursor()");
    shouldBe("event.target.source", "objectStore");
    if (event.target.result == null) {
        shouldBe("counter", "5");
        counter = 1;

        request = evalAndLog("trans.objectStore('basicStore').openCursor(keyRange)");
        request.onsuccess = basicCheckCursor;
        request.onerror = unexpectedErrorCallback;
        return;
    }

    request = evalAndLog("event.target.result.update('myUpdatedValue' + counter++)");
    request.onsuccess = function() { evalAndLog("event.target.source.continue()"); }
    request.onerror = unexpectedErrorCallback;
}

function basicCheckCursor()
{
    debug("basicCheckCursor()");
    if (event.target.result == null) {
        shouldBe("counter", "5");
        counter = 1;

        keyRange = webkitIDBKeyRange.lowerBound(1);
        request = evalAndLog("trans.objectStore('autoIncrementStore').openCursor(keyRange)");
        request.onsuccess = autoIncrementUpdateCursor;
        request.onerror = unexpectedErrorCallback;
        return;
    }

    shouldBeEqualToString("event.target.result.key", "myKey" + counter);
    shouldBeEqualToString("event.target.result.value", "myUpdatedValue" + counter++);
    evalAndLog("event.target.result.continue()");
}

function autoIncrementUpdateCursor()
{
    debug("autoIncrementUpdateCursor()");
    if (event.target.result == null) {
        shouldBe("counter", "5");
        counter = 1;

        request = evalAndLog("trans.objectStore('autoIncrementStore').openCursor(keyRange)");
        request.onsuccess = autoIncrementCheckCursor;
        request.onerror = unexpectedErrorCallback;
        return;
    }

    request = evalAndLog("event.target.result.update('myUpdatedFoo' + counter++)");
    request.onsuccess = function() { evalAndLog("event.target.source.continue()"); }
    request.onerror = unexpectedErrorCallback;
}

function autoIncrementCheckCursor()
{
    debug("autoIncrementCheckCursor()");
    if (event.target.result == null) {
        shouldBe("counter", "5");
        counter = 1;

        keyRange = webkitIDBKeyRange.lowerBound(1);
        request = evalAndLog("trans.objectStore('keyPathStore').openCursor(keyRange)");
        request.onsuccess = keyPathUpdateCursor;
        request.onerror = unexpectedErrorCallback;
        return;
    }

    shouldBe("event.target.result.key", "counter");
    shouldBeEqualToString("event.target.result.value", "myUpdatedFoo" + counter++);
    evalAndLog("event.target.result.continue()");
}

function keyPathUpdateCursor()
{
    debug("keyPathUpdateCursor()");
    if (event.target.result == null) {
        shouldBe("counter", "5");
        counter = 1;

        request = evalAndLog("trans.objectStore('keyPathStore').openCursor(keyRange)");
        request.onsuccess = keyPathCheckCursor;
        request.onerror = unexpectedErrorCallback;
        return;
    }

    request = evalAndLog("event.target.result.update({id: 100 + counter, number: 100 + counter})");
    request.onsuccess = unexpectedSuccessCallback;
    request.onerror = function() {
        shouldBe("event.target.errorCode", "webkitIDBDatabaseException.DATA_ERR");

        evalAndLog("event.preventDefault()");

        request = evalAndLog("event.target.source.update({id: counter, number: 100 + counter++})");
        request.onsuccess = function() { evalAndLog("event.target.source.continue()") };
        request.onerror = unexpectedErrorCallback;
    }
}

function keyPathCheckCursor()
{
    debug("keyPathCheckCursor()");
    if (event.target.result == null) {
        shouldBe("counter", "5");
        counter = 1;

        keyRange = webkitIDBKeyRange.lowerBound(101);
        request = evalAndLog("trans.objectStore('keyPathStore').index('numberIndex').openKeyCursor(keyRange)");
        request.onsuccess = keyCursor;
        request.onerror = unexpectedErrorCallback;
        return;
    }

    shouldBe("event.target.result.key", "counter");
    shouldBe("event.target.result.value.id", "counter");
    shouldBe("event.target.result.value.number", (counter + 100).toString());
    counter++;
    evalAndLog("event.target.result.continue()");
}

function keyCursor()
{
    debug("keyCursor()");
    if (event.target.result == null) {
        shouldBe("counter", "5");
        return;
    }

    shouldBe("event.target.result.key", "counter + 100");
    shouldBe("event.target.result.primaryKey", "counter");

    try {
        debug("event.target.result.update({id: counter, number: counter + 200})");
        event.target.result.update({id: counter, number: counter + 200});
        testFailed("Expected exception.");
    } catch (e) {
        code = e.code;
        shouldBe("code", "webkitIDBDatabaseException.NOT_ALLOWED_ERR");
    }

    counter++;
    event.target.result.continue();
}

function transactionComplete()
{
    debug("transactionComplete()");
    done();
}

var successfullyParsed = true;

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