1 if (this.importScripts) {
2 importScripts('../../../resources/js-test.js');
3 importScripts('shared.js');
6 description("Test features of IndexedDB's unique indices.");
8 indexedDBTest(prepareDatabase, setVersionCompleted);
9 function prepareDatabase()
11 db = event.target.result;
12 event.target.transaction.onabort = unexpectedAbortCallback;
14 self.store = evalAndLog("db.createObjectStore('store')");
15 self.indexObject = evalAndLog("store.createIndex('index', 'x', {unique: true})");
18 function setVersionCompleted()
20 debug("setVersionCompleted():");
21 self.transaction = evalAndLog("transaction = db.transaction(['store'], 'readwrite')");
23 request = evalAndLog("transaction.objectStore('store').put({x: 1}, 'foo')");
24 request.onerror = unexpectedErrorCallback;
25 request.onsuccess = addMoreData;
28 function addMoreData()
30 debug("addMoreData():");
32 // The x value violates the uniqueness constraint of the index.
33 request = evalAndLog("transaction.objectStore('store').put({x: 1}, 'bar')");
34 request.onerror = addMoreDataFailed;
35 request.onsuccess = unexpectedSuccessCallback;
38 function addMoreDataFailed()
40 debug("addMoreDataFailed():");
42 // Don't abort the transaction.
43 evalAndLog("event.preventDefault()");
45 shouldBe("event.target.error.name", "'ConstraintError'");
47 // Update the 'foo' entry in object store, changing the value of x.
48 request = evalAndLog("transaction.objectStore('store').put({x: 0}, 'foo')");
49 request.onerror = unexpectedErrorCallback;
50 request.onsuccess = changeDataSuccess;
53 function changeDataSuccess()
55 debug("changeDataSuccess():");
57 // An index cursor starting at 1 should not find anything.
58 var request = evalAndLog("transaction.objectStore('store').index('index').openCursor(IDBKeyRange.lowerBound(1))");
59 request.onsuccess = cursorSuccess;
60 request.onerror = unexpectedErrorCallback;
63 function cursorSuccess()
65 debug("cursorSuccess():");
66 shouldBeNull("event.target.result");
68 // A key cursor starting at 1 should not find anything.
69 var request = evalAndLog("transaction.objectStore('store').index('index').openKeyCursor(IDBKeyRange.lowerBound(1))");
70 request.onsuccess = keyCursorSuccess;
71 request.onerror = unexpectedErrorCallback;
74 function keyCursorSuccess()
76 debug("keyCursorSuccess():");
77 shouldBeNull("event.target.result");
79 // Now we should be able to add a value with x: 1.
80 request = evalAndLog("transaction.objectStore('store').put({x: 1}, 'bar')");
81 request.onerror = unexpectedErrorCallback;
82 request.onsuccess = addMoreDataSucces;
85 function addMoreDataSucces()
87 debug("addMoreDataSucces():");
89 request = evalAndLog("transaction.objectStore('store').delete('bar')");
90 request.onerror = unexpectedErrorCallback;
91 request.onsuccess = deleteSuccess;
94 function deleteSuccess()
96 debug("deleteSuccess():");
98 // Now we should be able to add a value with x: 1 again.
99 request = evalAndLog("transaction.objectStore('store').put({x: 1}, 'baz')");
100 request.onerror = unexpectedErrorCallback;
101 request.onsuccess = finalAddSuccess;
104 function finalAddSuccess() {
105 debug("finalAddSuccess():");
107 // An overwrite should be ok.
108 request = evalAndLog("transaction.objectStore('store').put({x: 1}, 'baz')");
109 request.onerror = unexpectedErrorCallback;
110 request.onsuccess = finishJSTest;