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 IndexedDB's IDBObjectStore auto-increment feature."); 14 if (window.layoutTestController) 15 layoutTestController.waitUntilDone(); 16 17 function test() 18 { 19 request = evalAndLog("webkitIndexedDB.open('objectstore-autoincrement')"); 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 trans.oncomplete = setVersionCompleted; 41 42 deleteAllObjectStores(db); 43 44 debug("createObjectStore():"); 45 window.store = evalAndLog("store = db.createObjectStore('StoreWithKeyPath', {keyPath: 'id', autoIncrement: true})"); 46 evalAndLog("db.createObjectStore('StoreWithAutoIncrement', {autoIncrement: true})"); 47 evalAndLog("db.createObjectStore('PlainOldStore', {autoIncrement: false})"); 48 var storeNames = evalAndLog("storeNames = db.objectStoreNames"); 49 50 shouldBeEqualToString("store.name", "StoreWithKeyPath"); 51 shouldBe("store.keyPath", "'id'"); 52 shouldBe("storeNames.contains('StoreWithKeyPath')", "true"); 53 shouldBe("storeNames.contains('StoreWithAutoIncrement')", "true"); 54 shouldBe("storeNames.contains('PlainOldStore')", "true"); 55 shouldBe("storeNames.length", "3"); 56 57 // Let the setVersion transaction complete. 58 } 59 60 function setVersionCompleted() 61 { 62 debug("setVersionCompleted():"); 63 64 window.trans = evalAndLog("trans = db.transaction([], webkitIDBTransaction.READ_WRITE)"); 65 trans.onabort = unexpectedAbortCallback; 66 trans.oncomplete = done; 67 68 window.store = evalAndLog("store = trans.objectStore('StoreWithKeyPath')"); 69 70 debug("Insert into object store with auto increment and key path, with key in the object."); 71 request = evalAndLog("store.add({name: 'Jeffersson', number: '7010', id: 3})"); 72 request.onsuccess = addJefferssonSuccess; 73 request.onerror = unexpectedErrorCallback; 74 } 75 76 function addJefferssonSuccess() 77 { 78 debug("addJefferssonSuccess():"); 79 shouldBe("event.target.result", "3"); 80 81 debug("Insert into object store with auto increment and key path, without key in the object."); 82 request = evalAndLog("store.add({name: 'Lincoln', number: '7012'})"); 83 request.onsuccess = addLincolnWithInjectKeySuccess; 84 request.onerror = unexpectedErrorCallback; 85 } 86 87 function addLincolnWithInjectKeySuccess() 88 { 89 debug("addLincolnWithInjectKeySuccess():"); 90 shouldBe("event.target.result", "4"); 91 92 result = evalAndLog("store.get(4)"); 93 result.onsuccess = getLincolnAfterInjectedKeySuccess; 94 result.onerror = unexpectedErrorCallback; 95 } 96 97 function getLincolnAfterInjectedKeySuccess() 98 { 99 debug("getLincolnAfterInjectedKeySuccess():"); 100 shouldBeEqualToString("event.target.result.name", "Lincoln"); 101 shouldBeEqualToString("event.target.result.number", "7012"); 102 shouldBe("event.target.result.id", "4"); 103 104 window.store = evalAndLog("store = trans.objectStore('StoreWithAutoIncrement')"); 105 debug("Insert into object store with key gen using explicit key"); 106 request = evalAndLog("store.add({name: 'Lincoln', number: '7012'}, 5)"); 107 request.onsuccess = addLincolnWithExplicitKeySuccess; 108 request.onerror = unexpectedErrorCallback; 109 } 110 111 function addLincolnWithExplicitKeySuccess() 112 { 113 debug("addLincolnWithExplicitKeySuccess():"); 114 shouldBe("event.target.result", "5"); 115 116 request = evalAndLog("store.get(5)"); 117 request.onsuccess = getLincolnSuccess; 118 request.onerror = unexpectedErrorCallback; 119 } 120 121 function getLincolnSuccess() 122 { 123 debug("getLincolnSuccess():"); 124 shouldBeEqualToString("event.target.result.name", "Lincoln"); 125 shouldBeEqualToString("event.target.result.number", "7012"); 126 127 request = evalAndLog("store.put({name: 'Abraham', number: '2107'})"); 128 request.onsuccess = putAbrahamSuccess; 129 request.onerror = unexpectedErrorCallback; 130 } 131 132 function putAbrahamSuccess() 133 { 134 debug("putAbrahamSuccess():"); 135 shouldBe("event.target.result", "6"); 136 137 request = evalAndLog("store.get(6)"); 138 request.onsuccess = getAbrahamSuccess; 139 request.onerror = unexpectedErrorCallback; 140 } 141 142 function getAbrahamSuccess() 143 { 144 debug("getAbrahamSuccess():"); 145 shouldBeEqualToString("event.target.result.name", "Abraham"); 146 shouldBeEqualToString("event.target.result.number", "2107"); 147 148 window.store = evalAndLog("store = trans.objectStore('PlainOldStore')"); 149 debug("Try adding with no key to object store without auto increment."); 150 request = evalAndLog("store.add({name: 'Adam'})"); 151 request.onsuccess = unexpectedSuccessCallback; 152 request.onerror = addAdamError; 153 } 154 155 function addAdamError() 156 { 157 debug("addAdamError():"); 158 shouldBe("event.target.errorCode", "webkitIDBDatabaseException.DATA_ERR"); 159 160 evalAndLog("event.preventDefault()"); 161 162 request = evalAndLog("store.add({name: 'Adam'}, 1)"); 163 request.onsuccess = addAdamSuccess; 164 request.onerror = unexpectedErrorCallback; 165 } 166 167 function addAdamSuccess() 168 { 169 debug("addAdamSuccess():"); 170 shouldBe("event.target.result", "1"); 171 } 172 173 test(); 174 175 var successfullyParsed = true; 176 177 </script> 178 </body> 179 </html> 180