Html程序  |  295行  |  9.1 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 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.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([], 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.NOT_SUPPORTED_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():");
    // FIXME: This error code needs to be specced.
    shouldBe("event.target.errorCode", "webkitIDBDatabaseException.UNKNOWN_ERR");

    evalAndLog("event.preventDefault()");

    transaction = evalAndLog("db.transaction([], webkitIDBTransaction.READ_WRITE)");
    transaction.onabort = unexpectedErrorCallback;
    var store = evalAndLog("store = transaction.objectStore('storeName')");

    request = evalAndLog("store.add({x: 'othervalue'}, null)");
    request.onsuccess = unexpectedSuccessCallback;
    request.addEventListener('error', addWithNullKeyFailure, true);
}

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

    evalAndLog("event.preventDefault()");

    transaction = evalAndLog("db.transaction([], 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([], 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():");
    shouldBeNull("event.target.result");
    var store = evalAndLog("store = event.target.source");

    try {
        debug("Passing an invalid key into store.get().");
        store.get([]);
        testFailed("No exception thrown");
    } catch (e) {
        testPassed("Caught exception: " + e.toString());
    }

    try {
        debug("Passing an invalid key into store.delete().");
        store.delete([]);
        testFailed("No exception thrown");
    } catch (e) {
        testPassed("Caught exception: " + e.toString());
    }

    try {
        debug("Passing an invalid key into store.add().");
        store.add(null, []);
        testFailed("No exception thrown");
    } catch (e) {
        testPassed("Caught exception: " + e.toString());
    }

    try {
        debug("Passing an invalid key into store.put().");
        store.put(null, []);
        testFailed("No exception thrown");
    } catch (e) {
        testPassed("Caught exception: " + e.toString());
    }

    done();
}

test();

var successfullyParsed = true;

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