Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / storage / indexeddb / resources / cursor-prev-no-duplicate.js
blobe754ae36bda85e6849cab761c64d3ada2afa5e30
1 if (this.importScripts) {
2     importScripts('../../../resources/js-test.js');
3     importScripts('shared.js');
6 description("Test IndexedDB behavior when iterating backwards with and without NO_DUPLICATE");
8 indexedDBTest(prepareDatabase, populateStore);
9 function prepareDatabase()
11     db = event.target.result;
12     store = evalAndLog("store = db.createObjectStore('store')");
13     evalAndLog("store.createIndex('index', 'sorted')");
16 function populateStore()
18     debug("");
19     debug("populating store...");
20     evalAndLog("trans = db.transaction('store', 'readwrite')");
21     evalAndLog("store = trans.objectStore('store');");
22     trans.onerror = unexpectedErrorCallback;
23     trans.onabort = unexpectedAbortCallback;
25     evalAndLog("store.put({sorted: 3, value: 111}, 1)");
26     evalAndLog("store.put({sorted: 2, value: 222}, 2)");
27     evalAndLog("store.put({sorted: 1, value: 333}, 3)");
28     evalAndLog("store.put({sorted: 10, value: 444}, 17)");
29     evalAndLog("store.put({sorted: 10, value: 555}, 16)");
30     evalAndLog("store.put({sorted: 10, value: 666}, 15)");
31     trans.oncomplete = testFarRangeCursor_closed;
35 function testFarRangeCursor_closed()
37     debug("");
38     debug("testFarRangeCursor: upper bound is well out of range, results always the same, whether open or closed");
40     runTest(makeOpenCursor("store", 7, false, "'prev'"),
41             { expectedValue: 333, expectedKey: 3},
42             testFarRangeCursor_open);
45 function testFarRangeCursor_open()
47     runTest(makeOpenCursor("store", 7, true, "'prev'"),
48             { expectedValue: 333, expectedKey: 3},
49             testFarRangeCursor_indexClosed);
52 function testFarRangeCursor_indexClosed()
54     // here '7' refers to the 'sorted' value
55     runTest(makeOpenCursor("index", 7, false, "'prev'"),
56             { expectedValue: 111, expectedKey: 3, expectedPrimaryKey: 1},
57             testFarRangeCursor_indexOpen);
59 function testFarRangeCursor_indexOpen()
61     runTest(makeOpenCursor("index", 7, true, "'prev'"),
62             { expectedValue: 111, expectedKey: 3, expectedPrimaryKey: 1},
63             testFarRangeCursor_indexKeyOpen);
66 function testFarRangeCursor_indexKeyOpen()
68     // here '7' refers to the sorted value
69     runTest(makeOpenKeyCursor("index", 7, false, "'prev'"),
70             { expectedKey: 3, expectedPrimaryKey: 1},
71             testFarRangeCursor_indexKeyClosed);
74 function testFarRangeCursor_indexKeyClosed()
76     runTest(makeOpenKeyCursor("index", 7, true, "'prev'"),
77             { expectedKey: 3, expectedPrimaryKey: 1},
78             testBoundaryCursor_closed);
81 function testBoundaryCursor_closed()
83     runTest(makeOpenCursor("store", 3, false, "'prev'"),
84             { expectedValue: 333, expectedKey: 3},
85             testBoundaryCursor_open);
88 function testBoundaryCursor_open()
90     runTest(makeOpenCursor("store", 3, true, "'prev'"),
91             { expectedValue: 222, expectedKey: 2},
92             testBoundaryCursor_indexClosed);
95 function testBoundaryCursor_indexClosed()
97     // by index sort order, we should return them in a different order
98     runTest(makeOpenCursor("index", 3, false, "'prev'"),
99             { expectedValue: 111, expectedKey: 3, expectedPrimaryKey: 1},
100             testBoundaryCursor_indexOpen);
103 function testBoundaryCursor_indexOpen()
105     runTest(makeOpenCursor("index", 3, true, "'prev'"),
106             { expectedValue: 222, expectedKey: 2, expectedPrimaryKey: 2},
107             testBoundaryCursor_indexKeyClosed);
110 function testBoundaryCursor_indexKeyClosed()
113     // now the value doesn't matter, just the primary key
114     runTest(makeOpenKeyCursor("index", 3, false, "'prev'"),
115             { expectedKey: 3, expectedPrimaryKey: 1},
116             testBoundaryCursor_indexKeyOpen);
119 function testBoundaryCursor_indexKeyOpen()
121     runTest(makeOpenKeyCursor("index", 3, true, "'prev'"),
122             { expectedKey: 2, expectedPrimaryKey: 2},
123             testNoDuplicate_closed);
126 function testNoDuplicate_closed()
128     debug("testNoDuplicate: there are 3 values, but we should return always the first one");
130     // PREV_NO_DUPLICATE doesn't really affect non-indexed
131     // cursors, but we should make sure we get the right one
132     // anyway
133     runTest(makeOpenCursor("store", 15, false, "'prevunique'"),
134             { expectedValue: 666, expectedKey: 15, expectedPrimaryKey: 15 },
135             testNoDuplicate_open);
138 function testNoDuplicate_open()
140     // still three values, but now the index says we should return the
141     // second one
142     runTest(makeOpenCursor("index", 15, false, "'prevunique'"),
143             { expectedValue: 666, expectedKey: 10, expectedPrimaryKey: 15},
144             testNoDuplicate_indexKeyClosed);
148 function testNoDuplicate_indexKeyClosed()
150     // same behavior as above, without a value
151     runTest(makeOpenKeyCursor("index", 15, false, "'prevunique'"),
152             { expectedKey: 10, expectedPrimaryKey: 15},
153             finishJSTest);
157 function makeOpenCursor(obj, upperBound, open, direction)
159     return obj + ".openCursor(IDBKeyRange.upperBound(" + upperBound + ", " +
160         open + "), " +
161             direction + ")";
164 function makeOpenKeyCursor(obj, upperBound, open, direction)
166     return obj + ".openKeyCursor(IDBKeyRange.upperBound(" + upperBound + ", " +
167         open + "), " +
168             direction + ")";
171 function runTest(openCursor, expectation, callback)
173     trans = db.transaction('store', 'readonly');
175     // expose these for code in openCursor
176     store = trans.objectStore('store');
177     index = store.index('index');
178     trans.onerror = unexpectedErrorCallback;
179     trans.onabort = unexpectedAbortCallback;
180     trans.oncomplete = function() {
181         debug("DONE");
182         debug("");
183         callback();
184     };
186     storeReq = evalAndLog("storeReq = " + openCursor);
187     storeReq.onsuccess = function() {
188         cursor = event.target.result;
189         if (cursor === null) {
190             testFailed("null cursor");
191             return;
192         }
194         shouldBe("cursor.key", JSON.stringify(expectation.expectedKey));
195         if ("value" in cursor) {
196             shouldBe("cursor.value.value", JSON.stringify(expectation.expectedValue));
197         } else if ("expectedValue" in expectation)
198             testFailed("Test broken: shouldn't have expectedValue");
200         if ("expectedPrimaryKey" in expectation) {
201             shouldBe("cursor.primaryKey", JSON.stringify(expectation.expectedPrimaryKey));
202         }
203     };