tizen beta release
[framework/web/webkit-efl.git] / LayoutTests / storage / indexeddb / key-type-array.html
1 <!DOCTYPE html>
2 <html>
3 <head>
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 key types");
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
21     evalAndLog("IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction;");
22     shouldBeFalse("IDBTransaction == null");
23
24     evalAndLog("IDBDatabaseException = window.IDBDatabaseException || window.webkitIDBDatabaseException;");
25     shouldBeFalse("IDBDatabaseException == null");
26
27
28     name = window.location.pathname;
29     description = "My Test Database";
30     openreq = evalAndLog("indexedDB.open(name, description)");
31     openreq.onsuccess = openSuccess;
32     openreq.onerror = unexpectedErrorCallback;
33 }
34
35 function openSuccess()
36 {
37     evalAndLog("db = openreq.result");
38     setverreq = evalAndLog("request = db.setVersion('1')");
39     setverreq.onsuccess = setVersionSuccess;
40     setverreq.onerror = unexpectedErrorCallback;
41 }
42
43 function setVersionSuccess()
44 {
45     debug("preparing database");
46     trans = setverreq.result;
47     trans.oncomplete = testValidArrayKeys;
48     deleteAllObjectStores(db);
49     objectStore = evalAndLog("db.createObjectStore('store');");
50     debug("");
51 }
52
53 function testValidArrayKeys()
54 {
55     evalAndLog("trans = db.transaction('store', IDBTransaction.READ_WRITE)");
56     evalAndLog("store = trans.objectStore('store')");
57     debug("");
58
59     evalAndLog("long_array = []; for (i = 0; i < 1000; ++i) { long_array.push('abc', 123, new Date(0), []); }");
60     debug("");
61
62     debug("array that contains non-numeric self-reference");
63     evalAndLog("self_referrential_array = []; self_referrential_array.self = self_referrential_array;");
64     debug("");
65
66     var n = 0, cases = [
67         "[]",
68
69         "[-Infinity]",
70         "[-Number.MAX_VALUE]",
71         "[-1]",
72         "[-Number.MIN_VALUE]",
73         "[0]",
74         "[Number.MIN_VALUE]",
75         "[1]",
76         "[Number.MAX_VALUE]",
77         "[Infinity]",
78
79         "[1,2,3]",
80
81         "[new Date(0)]",
82         "[new Date('2525-01-01T00:00:00Z')]",
83
84         "[new Date(0), new Date('2525-01-01T00:00:00Z')]",
85
86         "['']",
87         "['\x00']",
88         "['abc123']",
89
90         "['abc', 123]",
91
92         "[[]]",
93
94         "[[], []]",
95         "[[], [], []]",
96
97         "[[[]]]",
98         "[[[[]]]]",
99
100         "[123, 'abc', new Date(0), []]",
101         "[[123, 'abc', new Date(0), []], [456, 'def', new Date(999), [[]]]]",
102
103         "long_array",
104         "self_referrential_array"
105     ];
106
107     function testArrayPutGet(value, key, callback)
108     {
109         debug("testing array key: " + key);
110         putreq = evalAndLog("store.put('" + value + "', " + key + ");");
111         putreq.onerror = unexpectedErrorCallback;
112         putreq.onsuccess = function() {
113             getreq = evalAndLog("store.get(" + key + ");");
114             getreq.onerror = unexpectedErrorCallback;
115             getreq.onsuccess = function() {
116                 shouldBeEqualToString("getreq.result", value);
117                 debug("");
118                 callback();
119             };
120         };
121     }
122
123     function nextTest()
124     {
125         var testcase = cases.shift();
126         if (testcase) {
127             testArrayPutGet("value" + (++n), testcase, nextTest);
128         }
129     }
130
131     nextTest();
132
133     trans.oncomplete = testInvalidArrayKeys;
134 }
135
136
137 function testInvalidArrayKeys()
138 {
139     evalAndLog("trans = db.transaction('store', IDBTransaction.READ_WRITE)");
140     evalAndLog("store = trans.objectStore('store')");
141     debug("");
142
143     debug("array that contains itself: array = [ array ]");
144     evalAndLog("cyclic_array = []; cyclic_array.push(cyclic_array)");
145     shouldThrow("JSON.stringify(cyclic_array)");
146
147     debug("array that contains itself, one level down: array = [ [ array ] ]");
148     evalAndLog("cyclic_array2 = []; cyclic_array2.push([cyclic_array2])");
149     shouldThrow("JSON.stringify(cyclic_array2)");
150
151     debug("array that contains itself, not as first element: array = [1, 'b', [], array]");
152     evalAndLog("cyclic_array3 = [1, 'b', []]; cyclic_array3.push(cyclic_array3)");
153     shouldThrow("JSON.stringify(cyclic_array3)");
154     debug("");
155
156     debug("array that contains array that contains itself");
157     evalAndLog("cyclic_array4 = [cyclic_array];");
158     shouldThrow("JSON.stringify(cyclic_array4)");
159     debug("");
160
161     var invalidKeys = [
162         "[ void 0 ]", // undefined
163         "[ true ]",
164         "[ false ]",
165         "[ NaN ]",
166         "[ null ]",
167         "[ {} ]",
168         "[ function () {} ]",
169         "[ /regex/ ]",
170         "[ window ]",
171         "[ window.document ]",
172         "[ window.document.body ]",
173         "cyclic_array",
174         "cyclic_array2",
175         "cyclic_array3",
176         "cyclic_array4",
177         "Array(1000)" // sparse
178     ];
179
180     invalidKeys.forEach(function (key) {
181         debug("testing invalid array key: " + key);
182         evalAndExpectException("store.put('value', " + key + ");", "IDBDatabaseException.DATA_ERR");
183         debug("");
184     });
185
186     testDepthLimits();
187 }
188
189 function makeArrayOfDepth(n)
190 {
191     var array = [];
192     while (--n) {
193         array = [array];
194     }
195     return array;
196 }
197
198 function testDepthLimits()
199 {
200     shouldBe("indexedDB.cmp(makeArrayOfDepth(25), 0)", "1");
201     shouldBe("indexedDB.cmp(makeArrayOfDepth(250), 0)", "1");
202     evalAndExpectException("indexedDB.cmp(makeArrayOfDepth(2500), 0)", "IDBDatabaseException.DATA_ERR");
203     debug("");
204
205     done();
206 }
207
208 test();
209
210 </script>
211 </body>
212 </html>