Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / storage / indexeddb / resources / transaction-basics.js
blob3834165fd1289cb63c70aa8e1d32ed238848e7ab
1 if (this.importScripts) {
2     importScripts('../../../resources/js-test.js');
3     importScripts('shared.js');
6 description("Test IndexedDB transaction basics.");
8 indexedDBTest(prepareDatabase, testSetVersionAbort1);
9 function prepareDatabase()
11     db = event.target.result;
14 function testSetVersionAbort1()
16     checkMetadataEmpty();
17     evalAndLog("request = newConnection()");
18     request.onupgradeneeded = addRemoveIDBObjects;
19     request.onsuccess = unexpectedSuccessCallback;
20     request.onerror = testSetVersionAbort2;
23 function addRemoveIDBObjects()
25     debug("addRemoveIDBObjects():");
26     db = event.target.result;
27     evalAndLog("trans = event.target.transaction");
28     shouldBeNonNull("trans");
29     trans.oncomplete = unexpectedCompleteCallback;
31     var store = evalAndLog("store = db.createObjectStore('storeFail', null)");
32     var index = evalAndLog("index = store.createIndex('indexFail', 'x')");
34     evalAndLog("db.deleteObjectStore('storeFail')");
35     evalAndExpectException("store.deleteIndex('indexFail')", "DOMException.INVALID_STATE_ERR", "'InvalidStateError'");
37     trans.abort();
40 function testSetVersionAbort2()
42     debug("");
43     debug("testSetVersionAbort2():");
44     checkMetadataEmpty();
45     evalAndLog("request = newConnection()");
46     request.onupgradeneeded = addRemoveAddIDBObjects;
47     request.onerror = null;
50 function addRemoveAddIDBObjects()
52     debug("addRemoveAddIDBObjects():");
53     db = event.target.result;
54     var trans = evalAndLog("trans = event.target.transaction");
55     shouldBeNonNull("trans");
56     trans.addEventListener('abort', testSetVersionAbort3, false);
57     trans.oncomplete = unexpectedCompleteCallback;
59     var store = evalAndLog("store = db.createObjectStore('storeFail', null)");
60     var index = evalAndLog("index = store.createIndex('indexFail', 'x')");
62     evalAndLog("db.deleteObjectStore('storeFail')");
63     evalAndExpectException("store.deleteIndex('indexFail')", "DOMException.INVALID_STATE_ERR", "'InvalidStateError'");
65     var store = evalAndLog("store = db.createObjectStore('storeFail', null)");
66     var index = evalAndLog("index = store.createIndex('indexFail', 'x')");
68     trans.abort();
71 function testSetVersionAbort3()
73     debug("");
74     debug("testSetVersionAbort3():");
75     shouldBeFalse("event.cancelable");
76     checkMetadataEmpty();
77     evalAndLog("request = newConnection()");
78     request.onupgradeneeded = addIDBObjects;
79     request.onsuccess = unexpectedSuccessCallback;
80     request.onerror = testSetVersionAbort4;
83 function addIDBObjects()
85     debug("addIDBObjects():");
86     db = event.target.result;
87     shouldBeFalse("event.cancelable");
88     var trans = evalAndLog("trans = event.target.transaction");
89     shouldBeNonNull("trans");
90     trans.onabort = testInactiveAbortedTransaction;
91     trans.oncomplete = unexpectedCompleteCallback;
93     store = evalAndLog("store = db.createObjectStore('storeFail', null)");
94     index = evalAndLog("index = store.createIndex('indexFail', 'x')");
96     trans.abort();
99 function testInactiveAbortedTransaction()
101     debug("");
102     debug("testInactiveAbortedTransaction():");
103     evalAndExpectException("index.openCursor()", "0", "'TransactionInactiveError'");
104     evalAndExpectException("index.openKeyCursor()", "0", "'TransactionInactiveError'");
105     evalAndExpectException("index.get(0)", "0", "'TransactionInactiveError'");
106     evalAndExpectException("index.getKey(0)", "0", "'TransactionInactiveError'");
107     evalAndExpectException("index.count()", "0", "'TransactionInactiveError'");
109     evalAndExpectException("store.put(0, 0)", "0", "'TransactionInactiveError'");
110     evalAndExpectException("store.add(0, 0)", "0", "'TransactionInactiveError'");
111     evalAndExpectException("store.delete(0)", "0", "'TransactionInactiveError'");
112     evalAndExpectException("store.clear()", "0", "'TransactionInactiveError'");
113     evalAndExpectException("store.get(0)", "0", "'TransactionInactiveError'");
114     evalAndExpectException("store.openCursor()", "0", "'TransactionInactiveError'");
117 function testSetVersionAbort4()
119     debug("");
120     debug("testSetVersionAbort4():");
121     checkMetadataEmpty();
122     evalAndLog("request = newConnection()");
123     request.onupgradeneeded = addIDBObjectsAndCommit;
124     request.onerror = unexpectedErrorCallback;
125     request.onsuccess = testSetVersionAbort5;
128 function addIDBObjectsAndCommit()
130     db = event.target.result;
131     debug("addIDBObjectsAndCommit():");
132     var trans = evalAndLog("trans = event.target.transaction");
133     shouldBeNonNull("trans");
134     trans.onabort = unexpectedAbortCallback;
136     store = evalAndLog("store = db.createObjectStore('storeFail', null)");
137     index = evalAndLog("index = store.createIndex('indexFail', 'x')");
139     trans.oncomplete = testInactiveCompletedTransaction;
142 function testInactiveCompletedTransaction()
144     debug("");
145     debug("testInactiveCompletedTransaction():");
146     evalAndExpectException("index.openCursor()", "0", "'TransactionInactiveError'");
147     evalAndExpectException("index.openKeyCursor()", "0", "'TransactionInactiveError'");
148     evalAndExpectException("index.get(0)", "0", "'TransactionInactiveError'");
149     evalAndExpectException("index.getKey(0)", "0", "'TransactionInactiveError'");
150     evalAndExpectException("index.count()", "0", "'TransactionInactiveError'");
152     evalAndExpectException("store.put(0, 0)", "0", "'TransactionInactiveError'");
153     evalAndExpectException("store.add(0, 0)", "0", "'TransactionInactiveError'");
154     evalAndExpectException("store.delete(0)", "0", "'TransactionInactiveError'");
155     evalAndExpectException("store.clear()", "0", "'TransactionInactiveError'");
156     evalAndExpectException("store.get(0)", "0", "'TransactionInactiveError'");
157     evalAndExpectException("store.openCursor()", "0", "'TransactionInactiveError'");
160 function testSetVersionAbort5()
162     debug("");
163     debug("testSetVersionAbort5():");
164     checkMetadataExistingObjectStore();
165     evalAndLog("request = newConnection()");
166     request.onupgradeneeded = removeIDBObjects;
167     request.onsuccess = unexpectedSuccessCallback;
168     request.onerror = testSetVersionAbort6;
171 function removeIDBObjects()
173     db = event.target.result;
174     debug("removeIDBObjects():");
175     var trans = evalAndLog("trans = event.target.transaction");
176     shouldBeNonNull("trans");
177     trans.oncomplete = unexpectedCompleteCallback;
179     var store = evalAndLog("store = trans.objectStore('storeFail')");
180     evalAndLog("store.deleteIndex('indexFail')");
181     evalAndLog("db.deleteObjectStore('storeFail')");
183     trans.abort();
186 function testSetVersionAbort6()
188     debug("");
189     debug("testSetVersionAbort6():");
190     checkMetadataExistingObjectStore();
191     evalAndLog("request = newConnection()");
192     request.onupgradeneeded = setVersionSuccess;
193     request.onsuccess = completeCallback;
196 function checkMetadataEmpty()
198     shouldBe("self.db.objectStoreNames", "[]");
199     shouldBe("self.db.objectStoreNames.length", "0");
200     shouldBe("self.db.objectStoreNames.contains('storeFail')", "false");
203 function checkMetadataExistingObjectStore()
205     shouldBe("db.objectStoreNames", "['storeFail']");
206     shouldBe("db.objectStoreNames.length", "1");
207     shouldBe("db.objectStoreNames.contains('storeFail')", "true");
210 var version = 1;
211 function newConnection()
213     db.close();
214     var request = evalAndLog("indexedDB.open(dbname, " + (++version) + ")");
215     request.onerror = unexpectedErrorCallback;
216     request.onblocked = unexpectedBlockedCallback;
217     return request;
220 function setVersionSuccess()
222     db = event.target.result;
223     debug("");
224     debug("setVersionSuccess():");
225     evalAndLog("trans = event.target.transaction");
226     shouldBeNonNull("trans");
227     trans.onabort = unexpectedAbortCallback;
229     deleteAllObjectStores(db);
231     evalAndLog("db.createObjectStore('storeName', null)");
234 function completeCallback()
236     preamble();
237     shouldBeFalse("event.cancelable");
238     testPassed("complete event fired");
239     transaction = evalAndLog("db.transaction(['storeName'])");
240     transaction.oncomplete = emptyCompleteCallback;
241     var store = evalAndLog("store = transaction.objectStore('storeName')");
242     shouldBeEqualToString("store.name", "storeName");
245 function emptyCompleteCallback()
247     testPassed("complete event fired");
248     testDOMStringList();
251 function testDOMStringList()
253     debug("");
254     debug("Verifying DOMStringList works as argument for IDBDatabase.transaction()");
255     debug("db.objectStoreNames is " + db.objectStoreNames);
256     debug("... which contains: " + JSON.stringify(Array.prototype.slice.call(db.objectStoreNames)));
257     evalAndLog("transaction = db.transaction(db.objectStoreNames)");
258     testPassed("no exception thrown");
259     for (var i = 0; i < db.objectStoreNames.length; ++i) {
260       shouldBeNonNull("transaction.objectStore(" + JSON.stringify(db.objectStoreNames[i]) + ")");
261     }
262     testPassed("all stores present in transaction");
263     transaction.oncomplete = testInvalidMode;
266 function testInvalidMode()
268     debug("");
269     debug("Verify that specifying an invalid mode raises an exception");
270     evalAndExpectExceptionClass("db.transaction(['storeName'], 'lsakjdf')", "TypeError");
271     testDegenerateNames();
274 function testDegenerateNames()
276     debug("");
277     debug("Test that null and undefined are treated as strings");
279     evalAndExpectException("db.transaction(null)", "DOMException.NOT_FOUND_ERR", "'NotFoundError'");
280     evalAndExpectException("db.transaction(undefined)", "DOMException.NOT_FOUND_ERR", "'NotFoundError'");
282     evalAndLog("request = newConnection()");
283     request.onerror = unexpectedErrorCallback;
284     request.onupgradeneeded = function () {
285         var trans = request.transaction;
286         db = event.target.result;
287         evalAndLog("db.createObjectStore('null')");
288         evalAndLog("db.createObjectStore('undefined')");
289         trans.oncomplete = verifyDegenerateNames;
290     };
291     function verifyDegenerateNames() {
292         shouldNotThrow("transaction = db.transaction(null)");
293         shouldBeNonNull("transaction.objectStore('null')");
294         shouldNotThrow("transaction = db.transaction(undefined)");
295         shouldBeNonNull("transaction.objectStore('undefined')");
296         finishJSTest();
297     }