Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / storage / indexeddb / resources / cursor-key-order.js
blob489df448b0a7e9d915f5caadc63e1bde9ad3671f
1 if (this.importScripts) {
2     importScripts('../../../resources/js-test.js');
3     importScripts('shared.js');
6 description("Test IndexedDB keys ordering and readback from cursors.");
8 indexedDBTest(prepareDatabase, populateStore);
9 function prepareDatabase()
11     db = event.target.result;
12     evalAndLog("db.createObjectStore('store')");
15 self.keys = [
16     "-Infinity",
17     "-Number.MAX_VALUE",
18     "-1",
19     "-Number.MIN_VALUE",
20     "0",
21     "Number.MIN_VALUE",
22     "1",
23     "Number.MAX_VALUE",
24     "Infinity",
26     "new Date(0)",
27     "new Date(1000)",
28     "new Date(1317399931023)",
30     "''",
31     "'\x00'",
32     "'a'",
33     "'aa'",
34     "'b'",
35     "'ba'",
37     "'\xA2'", // U+00A2 CENT SIGN
38     "'\u6C34'", // U+6C34 CJK UNIFIED IDEOGRAPH (water)
39     "'\uD834\uDD1E'", // U+1D11E MUSICAL SYMBOL G-CLEF (UTF-16 surrogate pair)
40     "'\uFFFD'", // U+FFFD REPLACEMENT CHARACTER
42     "[]",
44     "[-Infinity]",
45     "[-Number.MAX_VALUE]",
46     "[-1]",
47     "[-Number.MIN_VALUE]",
48     "[0]",
49     "[Number.MIN_VALUE]",
50     "[1]",
51     "[Number.MAX_VALUE]",
52     "[Infinity]",
54     "[new Date(0)]",
55     "[new Date(1000)]",
56     "[new Date(1317399931023)]",
58     "['']",
59     "['\x00']",
60     "['a']",
61     "['aa']",
62     "['b']",
63     "['ba']",
65     "['\xA2']", // U+00A2 CENT SIGN
66     "['\u6C34']", // U+6C34 CJK UNIFIED IDEOGRAPH (water)
67     "['\uD834\uDD1E']", // U+1D11E MUSICAL SYMBOL G-CLEF (UTF-16 surrogate pair)
68     "['\uFFFD']", // U+FFFD REPLACEMENT CHARACTER
70     "[[]]",
72     "[[], []]",
73     "[[], [], []]",
75     "[[[]]]",
76     "[[[[]]]]"
79 function compare(a, b)
81     if (typeof a !== typeof b) {
82         return false;
83     }
85     switch (typeof a) {
86         case 'undefined': // Not valid keys, but compare anyway.
87         case 'boolean': // Not valid keys, but compare anyway.
88         case 'string':
89             return a === b;
90         case 'number':
91             if (a === b) {
92                 return (a !== 0) || (1 / a === 1 / b); // 0 isn't -0
93             } else {
94                 return a !== a && b !== b; // NaN is NaN
95             }
96         case 'object':
97             if (a instanceof Date && b instanceof Date) {
98                 return +a === +b;
99             } else if (a instanceof Array && b instanceof Array) {
100                 if (a.length !== b.length) {
101                     return false;
102                 }
103                 for (var i = 0; i < a.length; i++) {
104                     if (!compare(a[i], b[i])) {
105                         return false;
106                     }
107                 }
108                 return true;
109             }
110             // For the purposes of this test, other objects don't count
111             return false;
112         default:
113             return false;
114     }
117 function populateStore()
119     debug("");
120     debug("populating store...");
121     evalAndLog("trans = db.transaction('store', 'readwrite')");
122     evalAndLog("store = trans.objectStore('store');");
123     trans.onerror = unexpectedErrorCallback;
124     trans.onabort = unexpectedAbortCallback;
125     var count = 0;
126     keys.forEach(function(key) {
127         evalAndLog("store.put(" + (count++) + ", " + key + ")");
128     });
129     trans.oncomplete = checkStore;
132 function checkStore()
134     debug("");
135     debug("iterating cursor...");
136     evalAndLog("trans = db.transaction('store', 'readonly')");
137     evalAndLog("store = trans.objectStore('store');");
138     trans.onerror = unexpectedErrorCallback;
139     trans.onabort = unexpectedAbortCallback;
140     evalAndLog("count = 0");
141     curreq = evalAndLog("curreq = store.openCursor()");
142     curreq.onerror = unexpectedErrorCallback;
143     curreq.onsuccess = function() {
144         if (curreq.result) {
145             evalAndLog("cursor = curreq.result");
146             shouldBeTrue("compare(cursor.key, " + keys[count] + ")");
147             evalAndLog("getreq = store.get(cursor.key)");
148             getreq.onerror = unexpectedErrorCallback;
149             getreq.onsuccess = function() {
150                 shouldBe("getreq.result", "count++");
151                 cursor.continue();
152             };
153         } else {
154             shouldBe("count", "keys.length");
155             finishUp();
156         }
157     };
160 function finishUp()
162     testKeyCompare();
163     finishJSTest();
166 function testKeyCompare()
168     debug("");
169     debug("validate compare function");
170     var cases = [
171         "undefined",
172         "true",
173         "false",
174         "0",
175         "-0",
176         "123",
177         "Infinity",
178         "-Infinity",
179         "NaN",
180         "''",
181         "'abc'",
182         "'xyz'",
183         "new Date(0)",
184         "new Date(1e3)",
185         "new Date(1e9)",
186         "[]",
187         "[123]",
188         "['abc']",
189         "[123, 'abc']",
190         "['abc', 123]",
191         "[[]]",
192         "[[123]]",
193         "[['abc']]",
194         "[[123], 'abc']",
195         "[[123], 123]"
196     ];
198     for (var i = 0; i < cases.length; ++i) {
199         for (var j = 0; j < cases.length; ++j) {
200             if (i === j) {
201                 shouldBeTrue("compare(" + cases[i] + ", " + cases[j] + ")");
202             } else {
203                 shouldBeFalse("compare(" + cases[i] + ", " + cases[j] + ")");
204             }
205         }
206     }