Home | History | Annotate | Download | only in indexeddb
      1 <html>
      2 <head>
      3 <link rel="stylesheet" href="../../fast/js/resources/js-test-style.css">
      4 <script src="../../fast/js/resources/js-test-pre.js"></script>
      5 <script src="../../fast/js/resources/js-test-post-function.js"></script>
      6 <script src="resources/shared.js"></script>
      7 </head>
      8 <body>
      9 <p id="description"></p>
     10 <div id="console"></div>
     11 <script>
     12 
     13 description("Test the basics of IndexedDB's IDBObjectStore.");
     14 if (window.layoutTestController) 
     15     layoutTestController.waitUntilDone();
     16 
     17 function test()
     18 {
     19     request = evalAndLog("webkitIndexedDB.open('objectstore-basics')");
     20     request.onsuccess = openSuccess;
     21     request.onerror = unexpectedErrorCallback;
     22 }
     23 
     24 function openSuccess()
     25 {
     26     debug("openSuccess():");
     27     window.db = evalAndLog("db = event.target.result");
     28 
     29     request = evalAndLog("db.setVersion('new version')");
     30     request.onsuccess = setVersionSuccess;
     31     request.onerror = unexpectedErrorCallback;
     32 }
     33 
     34 function setVersionSuccess()
     35 {
     36     debug("setVersionSuccess():");
     37     window.trans = evalAndLog("trans = event.target.result");
     38     shouldBeTrue("trans !== null");
     39     trans.onabort = unexpectedAbortCallback;
     40 
     41     deleteAllObjectStores(db);
     42 
     43     debug("createObjectStore():");
     44     window.store = evalAndLog("store = db.createObjectStore('storeName', null)");
     45     var storeNames = evalAndLog("storeNames = db.objectStoreNames");
     46 
     47     shouldBeEqualToString("store.name", "storeName");
     48     shouldBeNull("store.keyPath");
     49     shouldBe("storeNames.contains('storeName')", "true");
     50     shouldBe("storeNames.length", "1");
     51     // FIXME: test all of object store's methods.
     52 
     53     debug("Ask for an index that doesn't exist:");
     54     try {
     55         debug("index = store.index('asdf')");
     56         index = store.index('asdf');
     57         testFailed("Asking for a store that doesn't exist should have thrown.");
     58     } catch (err) {
     59         testPassed("Exception thrown.");
     60         code = err.code;
     61         shouldBe("code", "webkitIDBDatabaseException.NOT_FOUND_ERR");
     62     }
     63 
     64     createIndex();
     65 }
     66 
     67 function createIndex()
     68 {
     69     debug("createIndex():");
     70     var index = evalAndLog("index = store.createIndex('indexName', 'x', {unique: true})"); // true == unique requirement.
     71     shouldBeTrue("index !== null");
     72     shouldBeTrue("store.indexNames.contains('indexName')");
     73     index = evalAndLog("index = store.index('indexName')");
     74     shouldBeTrue("index !== null");
     75  
     76     debug("Ask for an index that doesn't exist:");
     77     try {
     78         debug("index = store.index('asdf')");
     79         index = store.index('asdf');
     80         testFailed("Asking for a store that doesn't exist should have thrown.");
     81     } catch (err) {
     82         testPassed("Exception thrown.");
     83         code = err.code
     84         shouldBe("code", "webkitIDBDatabaseException.NOT_FOUND_ERR");
     85     }
     86 
     87     commitAndContinue();    
     88 }
     89 
     90 function commitAndContinue()
     91 {
     92     window.setTimeout(testSetVersionAbort, 0);
     93 }
     94 
     95 function testSetVersionAbort()
     96 {
     97     request = evalAndLog('db.setVersion("version fail")');
     98     request.onsuccess = createAnotherIndex;
     99     request.onerror = unexpectedErrorCallback;
    100 }
    101 
    102 function createAnotherIndex()
    103 {
    104     shouldBeEqualToString("db.version", "version fail");
    105 
    106     var setVersionTrans = evalAndLog("setVersionTrans = event.target.result");
    107     shouldBeTrue("setVersionTrans !== null");
    108     setVersionTrans.oncomplete = unexpectedCompleteCallback;
    109     setVersionTrans.onabort = checkMetadata;
    110     window.store = evalAndLog("store = setVersionTrans.objectStore('storeName')");
    111     var index = evalAndLog("index = store.createIndex('indexFail', 'x')");
    112 
    113     setVersionTrans.abort();
    114 }
    115 
    116 function checkMetadata()
    117 {
    118     shouldBeEqualToString("db.version", "new version");
    119     shouldBe("store.indexNames", "['indexName']");
    120     shouldBe("store.indexNames.length", "1");
    121     shouldBe("store.indexNames.contains('')", "false");
    122     shouldBe("store.indexNames.contains('indexFail')", "false");
    123     shouldBe("store.indexNames.contains('indexName')", "true");
    124     shouldBeEqualToString("store.indexNames[0]", "indexName");
    125     shouldBeNull("store.indexNames[1]");
    126     shouldBeNull("store.indexNames[100]");
    127     shouldBeNull("store.indexNames.item(1)");
    128     shouldBeNull("store.indexNames.item(100)");
    129     addData();
    130 }
    131 
    132 var testDate = new Date("August 25, 1991 20:57:08");
    133 var testDateB = new Date("Wed Jan 05 2011 15:54:49");
    134 
    135 function addData()
    136 {
    137     var transaction = evalAndLog("transaction = db.transaction([], webkitIDBTransaction.READ_WRITE)");
    138     transaction.onabort = unexpectedAbortCallback;
    139     window.store = evalAndLog("store = transaction.objectStore('storeName')");
    140 
    141     debug("Try to insert data with a Date key:");
    142     request = evalAndLog("store.add({x: 'foo'}, testDate)");
    143     request.onsuccess = addDateSuccess;
    144     request.onerror = unexpectedErrorCallback;
    145 }
    146 
    147 function addDateSuccess()
    148 {
    149     debug("Try to insert a value not handled by structured clone:");
    150     try {
    151         debug("store.add({x: 'bar', y: document.getElementById('console')}, 'bar')");
    152         store.add({x: 'bar', y: document.getElementById('console')}, 'bar');
    153         testFailed("Passing a DOM node as value should have thrown.");
    154     } catch (err) {
    155         testPassed("Exception thrown");
    156         code = err.code;
    157         shouldBe("code", "DOMException.NOT_SUPPORTED_ERR");
    158     }
    159 
    160     debug("Try to insert data where key path yields a Date key:");
    161     request = evalAndLog("store.add({x: testDateB, y: 'value'}, 'key')");
    162     request.onsuccess = addSuccess;
    163     request.onerror = unexpectedErrorCallback;
    164 }
    165 
    166 function addSuccess()
    167 {
    168     debug("addSuccess():");
    169     shouldBeEqualToString("event.target.result", "key");
    170 
    171     request = evalAndLog("event.target.source.add({x: 'foo'}, 'zzz')");
    172     request.onsuccess = unexpectedSuccessCallback;
    173     request.addEventListener('error', addAgainFailure, false);
    174 }
    175 
    176 function addAgainFailure()
    177 {
    178     debug("addAgainFailure():");
    179     // FIXME: This error code needs to be specced.
    180     shouldBe("event.target.errorCode", "webkitIDBDatabaseException.UNKNOWN_ERR");
    181 
    182     evalAndLog("event.preventDefault()");
    183 
    184     transaction = evalAndLog("db.transaction([], webkitIDBTransaction.READ_WRITE)");
    185     transaction.onabort = unexpectedErrorCallback;
    186     var store = evalAndLog("store = transaction.objectStore('storeName')");
    187 
    188     request = evalAndLog("store.add({x: 'othervalue'}, null)");
    189     request.onsuccess = unexpectedSuccessCallback;
    190     request.addEventListener('error', addWithNullKeyFailure, true);
    191 }
    192 
    193 function addWithNullKeyFailure()
    194 {
    195     debug("addWithNullKeyFailre():");
    196     shouldBe("event.target.errorCode", "webkitIDBDatabaseException.DATA_ERR");
    197 
    198     evalAndLog("event.preventDefault()");
    199 
    200     transaction = evalAndLog("db.transaction([], webkitIDBTransaction.READ_WRITE)");
    201     transaction.onabort = unexpectedErrorCallback;
    202     var store = evalAndLog("store = transaction.objectStore('storeName')");
    203 
    204     request = evalAndLog("store.add({x: null}, 'validkey')");
    205     request.onsuccess = unexpectedSuccessCallback;
    206     request.onerror = addWithNullIndexFailure;
    207 }
    208 
    209 function addWithNullIndexFailure()
    210 {
    211     shouldBeTrue("event.cancelable");
    212     debug("addWithNullIndexFailure():");
    213     shouldBe("event.target.errorCode", "webkitIDBDatabaseException.DATA_ERR");
    214 
    215     evalAndLog("event.preventDefault()");
    216 
    217     transaction = evalAndLog("db.transaction([], webkitIDBTransaction.READ_WRITE)");
    218     transaction.onabort = unexpectedErrorCallback;
    219     var store = evalAndLog("store = transaction.objectStore('storeName')");
    220 
    221     request = evalAndLog("store.get('key')");
    222     request.addEventListener('success', getSuccess, true);
    223     request.onerror = unexpectedErrorCallback;
    224 }
    225 
    226 function getSuccess()
    227 {
    228     debug("getSuccess():");
    229     shouldBeEqualToString("event.target.result.y", "value");
    230 
    231     var store = evalAndLog("store = event.target.source");
    232     request = evalAndLog("store.get(testDate)");
    233     request.addEventListener('success', getSuccessDateKey, false);
    234     request.onerror = unexpectedErrorCallback;
    235 }
    236 
    237 function getSuccessDateKey()
    238 {
    239     debug("getSuccessDateKey():");
    240     shouldBeEqualToString("event.target.result.x", "foo");
    241 
    242     request = evalAndLog("store.delete('key')");
    243     request.onsuccess = removeSuccess;
    244     request.onerror = unexpectedErrorCallback;
    245 }
    246 
    247 function removeSuccess()
    248 {
    249     debug("removeSuccess():");
    250     shouldBeNull("event.target.result");
    251     var store = evalAndLog("store = event.target.source");
    252 
    253     try {
    254         debug("Passing an invalid key into store.get().");
    255         store.get([]);
    256         testFailed("No exception thrown");
    257     } catch (e) {
    258         testPassed("Caught exception: " + e.toString());
    259     }
    260 
    261     try {
    262         debug("Passing an invalid key into store.delete().");
    263         store.delete([]);
    264         testFailed("No exception thrown");
    265     } catch (e) {
    266         testPassed("Caught exception: " + e.toString());
    267     }
    268 
    269     try {
    270         debug("Passing an invalid key into store.add().");
    271         store.add(null, []);
    272         testFailed("No exception thrown");
    273     } catch (e) {
    274         testPassed("Caught exception: " + e.toString());
    275     }
    276 
    277     try {
    278         debug("Passing an invalid key into store.put().");
    279         store.put(null, []);
    280         testFailed("No exception thrown");
    281     } catch (e) {
    282         testPassed("Caught exception: " + e.toString());
    283     }
    284 
    285     done();
    286 }
    287 
    288 test();
    289 
    290 var successfullyParsed = true;
    291 
    292 </script>
    293 </body>
    294 </html>
    295