IndexedDB: Indexes should be secondarily sorted on primary key
[WebKit-https.git] / LayoutTests / storage / indexeddb / cursor-primary-key-order.html
1 <html>
2 <head>
3 <link rel="stylesheet" href="../../fast/js/resources/js-test-style.css">
4 <script src="../../fast/js/resources/js-test-pre.js"></script>
5 <script src="resources/shared.js"></script>
6 </head>
7 <body>
8 <p id="description"></p>
9 <div id="console"></div>
10 <script>
11
12 description("Test IndexedDB primary key ordering and readback from cursors.");
13 if (window.layoutTestController)
14     layoutTestController.waitUntilDone();
15
16 function test()
17 {
18     evalAndLog("indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB;");
19     shouldBeFalse("indexedDB == null");
20     evalAndLog("IDBCursor = window.IDBCursor || window.webkitIDBCursor;");
21     shouldBeFalse("IDBCursor == null");
22     evalAndLog("IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction");
23     shouldBeFalse("IDBTransaction == null");
24
25     prepareDatabase();
26 }
27
28 function prepareDatabase()
29 {
30     debug("");
31     evalAndLog("openRequest = indexedDB.open('cursor-primary-key-order')");
32     openRequest.onerror = unexpectedErrorCallback;
33     openRequest.onsuccess = function() {
34         evalAndLog("db = openRequest.result");
35         evalAndLog("versionChangeRequest = db.setVersion('1')");
36         versionChangeRequest.onerror = unexpectedErrorCallback;
37         versionChangeRequest.onsuccess = function() {
38             evalAndLog("store = db.createObjectStore('store')");
39             evalAndLog("index = store.createIndex('index', 'indexKey')");
40
41             versionChangeRequest.result.oncomplete = populateStore;
42         };
43     };
44 }
45
46 window.keys = [
47     "-Infinity",
48     "-2",
49     "-1",
50     "0",
51     "1",
52     "2",
53     "Infinity",
54
55     "'0'",
56     "'1'",
57     "'2'",
58     "'A'",
59     "'B'",
60     "'C'",
61     "'a'",
62     "'b'",
63     "'c'"
64 ];
65
66
67 function populateStore()
68 {
69     debug("");
70     debug("populating store...");
71     evalAndLog("trans = db.transaction('store', IDBTransaction.READ_WRITE)");
72     evalAndLog("store = trans.objectStore('store');");
73     trans.onerror = unexpectedErrorCallback;
74     trans.onabort = unexpectedAbortCallback;
75     var count = 0;
76     var indexKey = 0;
77     var keys = window.keys.slice();
78     keys.reverse();
79     keys.forEach(function(key) {
80         var value = { indexKey: indexKey, count: count++ };
81         evalAndLog("store.put(" + JSON.stringify(value) + ", " + key + ")");
82     });
83     trans.oncomplete = checkStore;
84 }
85
86 function checkStore()
87 {
88     debug("");
89     debug("iterating cursor...");
90     evalAndLog("trans = db.transaction('store', IDBTransaction.READ_ONLY)");
91     evalAndLog("store = trans.objectStore('store');");
92     evalAndLog("index = store.index('index');");
93     trans.onerror = unexpectedErrorCallback;
94     trans.onabort = unexpectedAbortCallback;
95     cursorRequest = evalAndLog("cursorRequest = index.openCursor()");
96     evalAndLog("count = 0");
97     var indexKey = 0;
98     cursorRequest.onerror = unexpectedErrorCallback;
99     cursorRequest.onsuccess = function() {
100         if (cursorRequest.result) {
101             evalAndLog("cursor = cursorRequest.result");
102             shouldBe("cursor.key", String(indexKey));
103             shouldBe("cursor.primaryKey", window.keys[count++]);
104             cursor.continue();
105         } else {
106             shouldBeTrue("count === keys.length");
107             done();
108         }
109     };
110 }
111
112 test();
113
114 </script>
115 </body>
116 </html>