Modern IDB: Pipe through cursor functions from client to server.
[WebKit-https.git] / LayoutTests / storage / indexeddb / modern / cursor-1.html
1 This tests basic IDBCursor functionality<br>
2 <div id="logger"></div>
3 <script>
4
5 if (window.testRunner) {
6     testRunner.waitUntilDone();
7     testRunner.dumpAsText();
8 }
9
10 function done()
11 {
12     log("Done");
13     if (window.testRunner)
14         testRunner.notifyDone();
15 }
16
17 function log(message)
18 {
19     document.getElementById("logger").innerHTML += message + "<br>";
20 }
21
22 var createRequest = window.indexedDB.open("Cursor1Database", 1);
23
24 function logCursor(cursor)
25 {
26     log("Cursor is: " + cursor);
27     log("Cursor direction is: " + cursor.direction);
28     log("Cursor source is: " + cursor.source + " (" + cursor.source.name + ")");    
29     log("Cursor key is: " + cursor.key);    
30     log("Cursor primary key is: " + cursor.primaryKey);    
31 }
32
33 function setupRequest(request)
34 {
35     // FIXME: Right now (until https://bugs.webkit.org/show_bug.cgi?id=151196 is resolved) what should be successful cursor operations will actually always fail.
36     request.onsuccess = function() {
37         log("Success opening cursor");
38         logCursor(request.result);  
39     }
40     request.onerror = function(e) {
41         log("Error opening cursor (expected for now)");
42         logCursor(request.result);
43         e.stopPropagation();
44     } 
45 }
46
47 var objectStore;
48 var index;
49
50 function testCursorDirection(direction)
51 {
52     var range = IDBKeyRange.lowerBound(-Infinity);
53     var request = objectStore.openCursor(range, direction);
54     setupRequest(request);
55     request = index.openCursor(range, direction);
56     setupRequest(request);
57     request = index.openKeyCursor(range, direction);
58     setupRequest(request);
59 }
60
61 createRequest.onupgradeneeded = function(event) {
62     log("Initial upgrade needed: Old version - " + event.oldVersion + " New version - " + event.newVersion);
63
64     var versionTransaction = createRequest.transaction;
65     var database = event.target.result;
66     objectStore = database.createObjectStore("TestObjectStore");
67     index = objectStore.createIndex("TestIndex1", "bar");
68
69     for (var i = 0; i < 10; ++i) {
70         objectStore.put("Record " + i, i);
71     }
72     objectStore.put({ bar: "Hello" }, "foo");
73
74     testCursorDirection("next");
75     testCursorDirection("nextunique");
76     testCursorDirection("prev");
77     testCursorDirection("prevunique");
78       
79     versionTransaction.onabort = function(event) {
80         log("Initial upgrade versionchange transaction unexpected aborted");
81         done();
82     }
83
84     versionTransaction.oncomplete = function(event) {
85         log("Initial upgrade versionchange transaction complete");
86         done();
87     }
88
89     versionTransaction.onerror = function(event) {
90         log("Initial upgrade versionchange transaction unexpected error" + event);
91         done();
92     }
93 }
94
95 </script>