1 if (this.importScripts) {
2 importScripts('../../../resources/js-test.js');
3 importScripts('shared.js');
6 description("Test IndexedDB's IDBObjectStore.count().");
8 indexedDBTest(prepareDatabase, verifyCount);
9 function prepareDatabase()
11 db = event.target.result;
12 event.target.transaction.onabort = unexpectedAbortCallback;
14 store = evalAndLog("store = db.createObjectStore('storeName', null)");
16 debug("adding 0 ... 99");
17 for (var i = 0; i < 100; ++i) {
18 request = store.add(i, i);
19 request.onerror = unexpectedErrorCallback;
23 function verifyCount()
26 debug("verifying count without range");
27 trans = evalAndLog("trans = db.transaction('storeName', 'readonly')");
28 shouldBeNonNull("trans");
29 trans.onabort = unexpectedAbortCallback;
30 trans.oncomplete = verifyCountWithRange;
32 store = evalAndLog("store = trans.objectStore('storeName')");
33 shouldBeNonNull("store");
35 request = evalAndLog("request = store.count()");
36 request.onerror = unexpectedErrorCallback;
37 request.onsuccess = function() {
38 shouldBeEqualToString("typeof request.result", "number");
39 shouldBe("request.result", "100");
40 // let the transaction complete
44 function verifyCountWithRange()
47 debug("verifying count with range");
48 trans = evalAndLog("trans = db.transaction('storeName', 'readonly')");
49 shouldBeNonNull("trans");
50 trans.onabort = unexpectedAbortCallback;
51 trans.oncomplete = verifyCountWithKey;
53 store = evalAndLog("store = trans.objectStore('storeName')");
54 shouldBeNonNull("store");
57 { lower: 0, lowerOpen: false, upper: 99, upperOpen: false, expected: 100 },
58 { lower: 0, lowerOpen: true, upper: 99, upperOpen: false, expected: 99 },
59 { lower: 0, lowerOpen: false, upper: 99, upperOpen: true, expected: 99 },
60 { lower: 0, lowerOpen: true, upper: 99, upperOpen: true, expected: 98 },
61 { lower: 0, lowerOpen: false, upper: 100, upperOpen: false, expected: 100 },
62 { lower: 0, lowerOpen: false, upper: 100, upperOpen: false, expected: 100 },
63 { lower: 10, lowerOpen: false, upper: 100, upperOpen: false, expected: 90 },
64 { lower: 0, lowerOpen: false, upper: 0, upperOpen: false, expected: 1 },
65 { lower: 500, lowerOpen: false, upper: 500, upperOpen: false, expected: 0 }
70 evalAndLog("test = " + JSON.stringify(tests.shift()));
71 request = evalAndLog("request = store.count(IDBKeyRange.bound(test.lower, test.upper, test.lowerOpen, test.upperOpen))");
72 request.onerror = unexpectedErrorCallback;
73 request.onsuccess = function() {
74 shouldBeEqualToString("typeof request.result", "number");
75 shouldBe("request.result", String(test.expected));
80 // otherwise let the transaction complete
87 function verifyCountWithKey()
90 debug("verifying count with key");
91 trans = evalAndLog("trans = db.transaction('storeName', 'readonly')");
92 shouldBeNonNull("trans");
93 trans.onabort = unexpectedAbortCallback;
94 trans.oncomplete = finishJSTest;
96 store = evalAndLog("store = trans.objectStore('storeName')");
97 shouldBeNonNull("store");
99 evalAndExpectException("store.count(NaN)", "0", "'DataError'");
100 evalAndExpectException("store.count({})", "0", "'DataError'");
101 evalAndExpectException("store.count(/regex/)", "0", "'DataError'");
104 { key: 0, expected: 1 },
105 { key: 100, expected: 0 },
106 { key: null, expected: 100 }
109 function nextTest() {
111 evalAndLog("test = " + JSON.stringify(tests.shift()));
112 request = evalAndLog("request = store.count(test.key)");
113 request.onerror = unexpectedErrorCallback;
114 request.onsuccess = function() {
115 shouldBeEqualToString("typeof request.result", "number");
116 shouldBe("request.result", String(test.expected));
120 // otherwise let the transaction complete