1 if (this.importScripts) {
2 importScripts('../../../resources/js-test.js');
3 importScripts('shared.js');
6 description("Test IndexedDB's IDBIndex.count().");
8 indexedDBTest(prepareDatabase, verifyCount);
9 function prepareDatabase()
11 db = event.target.result;
12 event.target.transaction.onabort = unexpectedAbortCallback;
13 store = evalAndLog("store = db.createObjectStore('storeName', null)");
15 self.index = evalAndLog("store.createIndex('indexName', '')");
16 shouldBeTrue("store.indexNames.contains('indexName')");
18 debug("adding 0 ... 99");
19 for (var i = 0; i < 100; ++i) {
20 request = store.add(i, i);
21 request.onerror = unexpectedErrorCallback;
25 function verifyCount()
28 debug("verifying count without range");
29 trans = evalAndLog("trans = db.transaction('storeName', 'readonly')");
30 shouldBeNonNull("trans");
31 trans.onabort = unexpectedAbortCallback;
32 trans.oncomplete = verifyCountWithRange;
34 store = evalAndLog("store = trans.objectStore('storeName')");
35 shouldBeNonNull("store");
36 index = evalAndLog("index = store.index('indexName')");
37 shouldBeNonNull("index");
39 request = evalAndLog("request = index.count()");
40 request.onerror = unexpectedErrorCallback;
41 request.onsuccess = function() {
42 shouldBeEqualToString("typeof request.result", "number");
43 shouldBe("request.result", "100");
44 // let the transaction complete
48 function verifyCountWithRange()
51 debug("verifying count with range");
52 trans = evalAndLog("trans = db.transaction('storeName', 'readonly')");
53 shouldBeNonNull("trans");
54 trans.onabort = unexpectedAbortCallback;
55 trans.oncomplete = verifyCountWithKey;
57 store = evalAndLog("store = trans.objectStore('storeName')");
58 shouldBeNonNull("store");
59 store = evalAndLog("index = trans.objectStore('storeName').index('indexName')");
60 shouldBeNonNull("index");
63 { lower: 0, lowerOpen: false, upper: 99, upperOpen: false, expected: 100 },
64 { lower: 0, lowerOpen: true, upper: 99, upperOpen: false, expected: 99 },
65 { lower: 0, lowerOpen: false, upper: 99, upperOpen: true, expected: 99 },
66 { lower: 0, lowerOpen: true, upper: 99, upperOpen: true, expected: 98 },
67 { lower: 0, lowerOpen: false, upper: 100, upperOpen: false, expected: 100 },
68 { lower: 0, lowerOpen: false, upper: 100, upperOpen: false, expected: 100 },
69 { lower: 10, lowerOpen: false, upper: 100, upperOpen: false, expected: 90 },
70 { lower: 0, lowerOpen: false, upper: 0, upperOpen: false, expected: 1 },
71 { lower: 500, lowerOpen: false, upper: 500, upperOpen: false, expected: 0 }
76 evalAndLog("test = " + JSON.stringify(tests.shift()));
77 request = evalAndLog("request = index.count(IDBKeyRange.bound(test.lower, test.upper, test.lowerOpen, test.upperOpen))");
78 request.onerror = unexpectedErrorCallback;
79 request.onsuccess = function() {
80 shouldBeEqualToString("typeof request.result", "number");
81 shouldBe("request.result", String(test.expected));
86 // otherwise let the transaction complete
93 function verifyCountWithKey()
96 debug("verifying count with key");
97 trans = evalAndLog("trans = db.transaction('storeName', 'readonly')");
98 shouldBeNonNull("trans");
99 trans.onabort = unexpectedAbortCallback;
100 trans.oncomplete = finishJSTest;
102 store = evalAndLog("store = trans.objectStore('storeName')");
103 shouldBeNonNull("store");
104 store = evalAndLog("index = trans.objectStore('storeName').index('indexName')");
105 shouldBeNonNull("index");
107 evalAndExpectException("index.count(NaN)", "0", "'DataError'");
108 evalAndExpectException("index.count({})", "0", "'DataError'");
109 evalAndExpectException("index.count(/regex/)", "0", "'DataError'");
112 { key: 0, expected: 1 },
113 { key: 100, expected: 0 },
114 { key: null, expected: 100 }
117 function nextTest() {
119 evalAndLog("test = " + JSON.stringify(tests.shift()));
120 request = evalAndLog("request = index.count(test.key)");
121 request.onerror = unexpectedErrorCallback;
122 request.onsuccess = function() {
123 shouldBeEqualToString("typeof request.result", "number");
124 shouldBe("request.result", String(test.expected));
129 // otherwise let the transaction complete