IndexedDB: Support Array-type key paths
[WebKit-https.git] / LayoutTests / storage / indexeddb / resources / keypath-arrays.js
1 if (this.importScripts) {
2     importScripts('../../../fast/js/resources/js-test-pre.js');
3     importScripts('shared.js');
4 }
5
6 description("Test IndexedDB Array-type keyPaths");
7
8 function test()
9 {
10     removeVendorPrefixes();
11
12     request = evalAndLog("indexedDB.deleteDatabase('keypath-arrays')");
13     request.onerror = unexpectedErrorCallback;
14     request.onsuccess = function () {
15         request = evalAndLog("indexedDB.open('keypath-arrays')");
16         request.onerror = unexpectedErrorCallback;
17         request.onsuccess = openSuccess;
18     };
19 }
20
21 function openSuccess()
22 {
23     debug("");
24     debug("openSuccess():");
25     db = evalAndLog("db = event.target.result");
26     request = evalAndLog("request = db.setVersion('1')");
27     request.onerror = unexpectedErrorCallback;
28     request.onsuccess = function () {
29         transaction = request.result;
30         transaction.onabort = unexpectedAbortCallback;
31         evalAndLog("store = db.createObjectStore('store', {keyPath: ['a', 'b']})");
32         evalAndLog("store.createIndex('index', ['c', 'd'])");
33
34         evalAndExpectException("db.createObjectStore('store-with-generator', {keyPath: ['a', 'b'], autoIncrement: true})", "DOMException.INVALID_ACCESS_ERR");
35         evalAndExpectException("store.createIndex('index-multientry', ['e', 'f'], {multiEntry: true})", "DOMException.NOT_SUPPORTED_ERR");
36
37         transaction.oncomplete = testKeyPaths;
38     };
39 }
40
41 function testKeyPaths()
42 {
43     debug("");
44     debug("testKeyPaths():");
45
46     transaction = evalAndLog("transaction = db.transaction(['store'], 'readwrite')");
47     transaction.onabort = unexpectedAbortCallback;
48     evalAndLog("store = transaction.objectStore('store')");
49     evalAndLog("index = store.index('index')");
50
51     debug("");
52     evalAndLog("request = store.put({a: 1, b: 2, c: 3, d: 4})");
53     request.onerror = unexpectedErrorCallback;
54     checkStore();
55
56     function checkStore() {
57         evalAndLog("request = store.openCursor()");
58         request.onerror = unexpectedErrorCallback;
59         request.onsuccess = function () {
60             evalAndLog("cursor = request.result");
61             shouldBeNonNull("cursor");
62             shouldBeEqualToString("JSON.stringify(cursor.key)", "[1,2]");
63             checkIndex();
64         };
65     };
66
67     function checkIndex() {
68         evalAndLog("request = index.openCursor()");
69         request.onerror = unexpectedErrorCallback;
70         request.onsuccess = function () {
71             evalAndLog("cursor = request.result");
72             shouldBeNonNull("cursor");
73             shouldBeEqualToString("JSON.stringify(cursor.primaryKey)", "[1,2]");
74             shouldBeEqualToString("JSON.stringify(cursor.key)", "[3,4]");
75         };
76     };
77
78     transaction.oncomplete = finishJSTest;
79 }
80
81 test();