1 if (this.importScripts) {
2 importScripts('../../../resources/js-test.js');
3 importScripts('shared.js');
6 description("Test features of IndexedDB's multiEntry indices.");
8 indexedDBTest(prepareDatabase, addData);
9 function prepareDatabase()
11 db = event.target.result;
13 store = evalAndLog("store = db.createObjectStore('store')");
14 evalAndLog("store.createIndex('index', 'x', {multiEntry: true})");
16 store2 = evalAndLog("store2 = db.createObjectStore('store-unique')");
17 evalAndLog("store2.createIndex('index-unique', 'x', {multiEntry: true, unique: true})");
23 debug("Populating stores (and indexes)");
24 transaction = evalAndLog("transaction = db.transaction(['store'], 'readwrite')");
25 transaction.onabort = unexpectedAbortCallback;
26 transaction.oncomplete = function() { verifyIndexes('index', verifyUniqueConstraint); };
28 debug("First try some keys that aren't what we're expecting");
29 request = evalAndLog("transaction.objectStore('store').put({x: [7, 8, 9], y: 'a'}, 'foo')");
30 request.onerror = unexpectedErrorCallback;
31 debug("Now overwrite them with what we're expecting");
32 request = evalAndLog("transaction.objectStore('store').put({x: [1, 2, 3], y: 'a'}, 'foo')");
33 request.onerror = unexpectedErrorCallback;
34 request = evalAndLog("transaction.objectStore('store').put({x: [4, 5, 6], y: 'b'}, 'bar')");
35 request.onerror = unexpectedErrorCallback;
36 request = evalAndLog("transaction.objectStore('store').put({x: [7, 7, 8, 7], y: 'c'}, 'baz')");
37 request.onerror = unexpectedErrorCallback;
38 request = evalAndLog("transaction.objectStore('store').put({x: [null, 9, 9], y: 'd'}, 'bloop')");
39 request.onerror = unexpectedErrorCallback;
42 function verifyIndexes(indexName, callback)
45 debug("Verifying index: " + indexName);
46 transaction = evalAndLog("transaction = db.transaction(['store'], 'readonly')");
47 transaction.onabort = unexpectedAbortCallback;
48 transaction.oncomplete = function() { verifyCount(callback); };
51 { key: 1, primaryKey: 'foo', y: 'a' },
52 { key: 2, primaryKey: 'foo', y: 'a' },
53 { key: 3, primaryKey: 'foo', y: 'a' },
54 { key: 4, primaryKey: 'bar', y: 'b' },
55 { key: 5, primaryKey: 'bar', y: 'b' },
56 { key: 6, primaryKey: 'bar', y: 'b' },
57 { key: 7, primaryKey: 'baz', y: 'c' },
58 { key: 8, primaryKey: 'baz', y: 'c' },
59 { key: 9, primaryKey: 'bloop', y: 'd' },
62 var request = evalAndLog("transaction.objectStore('store').index('" + indexName + "').openCursor()");
63 request.onerror = unexpectedErrorCallback;
64 request.onsuccess = function(event) {
65 cursor = evalAndLog("cursor = event.target.result");
67 ex = expected.shift();
68 shouldBeNonNull("ex");
69 shouldBe("cursor.key", String(ex.key));
70 shouldBeEqualToString("cursor.primaryKey", ex.primaryKey);
71 shouldBeEqualToString("cursor.value.y", ex.y);
72 evalAndLog("cursor.continue()");
74 shouldBe("expected.length", "0");
79 function verifyUniqueConstraint()
82 debug("Verifying unique constraint on multiEntry index");
83 transaction = evalAndLog("transaction = db.transaction(['store-unique'], 'readwrite')");
84 transaction.onabort = function () {
85 debug("Transaction aborted as expected");
86 createIndexOnStoreWithData();
88 transaction.oncomplete = unexpectedCompleteCallback;
90 request = evalAndLog("transaction.objectStore('store-unique').put({x: [1, 2, 3], y: 'a'}, 'foo')");
91 request.onerror = unexpectedErrorCallback;
92 request.onsuccess = function() {
94 debug("Replace an existing record - this should work");
95 request = evalAndLog("transaction.objectStore('store-unique').put({x: [1, 2, 7], y: 'a'}, 'foo')");
96 request.onerror = unexpectedErrorCallback;
97 request.onsuccess = function() {
99 debug("This should fail the uniqueness constraint on the index, and fail:");
100 request = evalAndLog("transaction.objectStore('store-unique').put({x: [5, 2], y: 'c'}, 'should fail')");
101 request.onsuccess = unexpectedSuccessCallback;
102 request.onerror = function() {
103 shouldBeUndefined("request.result");
104 shouldBeNonNull("request.error");
105 debug("Request failed, as expected (" + request.error.name + ")");
111 function verifyCount(callback) {
112 evalAndLog("transaction = db.transaction(['store'])");
114 transaction.onabort = unexpectedAbortCallback;
115 transaction.oncomplete = callback;
117 index = evalAndLog("transaction.objectStore('store').index('index')");
118 request = evalAndLog("index.count()");
119 request.onsuccess = function(event) {
121 shouldBe("event.target.result", "9");
123 request = evalAndLog("index.count(7)");
124 request.onsuccess = function(event) {
125 shouldBe("event.target.result", "1");
131 function createIndexOnStoreWithData()
134 debug("Create an index on a populated store");
135 evalAndLog("db.close()");
137 request = evalAndLog("indexedDB.open(dbname, 2)");
138 request.onerror = unexpectedErrorCallback;
139 request.onupgradeneeded = function() {
140 evalAndLog("db = event.target.result");
141 evalAndLog("trans = event.target.transaction");
142 trans.onabort = unexpectedAbortCallback;
144 store = evalAndLog("store = trans.objectStore('store')");
145 evalAndLog("store.createIndex('index-new', 'x', {multiEntry: true})");
147 request.onsuccess = function() { verifyIndexes('index-new', finishJSTest); };