Modern IDB: IDBObjectStore.deleteIndex() support.
[WebKit-https.git] / LayoutTests / storage / indexeddb / modern / deleteindex-1.html
1 This tests deleting an index and then committing the transaction.<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 index;
23 var objectStore;
24 var database;
25
26 function checkIndexValues()
27 {
28     var countRequest = index.count();
29     countRequest.onsuccess = function() {
30         log("Count is: " + countRequest.result);
31     }
32
33     var cursorRequest = index.openCursor();
34     cursorRequest.onsuccess = function() {
35         var cursor = cursorRequest.result;
36         log("Cursor at record: " + cursor.key + " / " + cursor.primaryKey);
37         
38         if (cursor.key != undefined)
39             cursor.continue();
40         else {
41             objectStore.deleteIndex("TestIndex1");
42             log("Deleted the index");
43         }
44     }
45     cursorRequest.onerror = function(e) {
46         log("Unexpected error opening or iterating cursor");
47         logCursor(cursorRequest.result);
48         done();
49     } 
50 }
51
52 var createRequest = window.indexedDB.open("DeleteIndex1Database", 1);
53 createRequest.onupgradeneeded = function(event) {
54     log("Initial upgrade needed: Old version - " + event.oldVersion + " New version - " + event.newVersion);
55
56     var versionTransaction = createRequest.transaction;
57     database = event.target.result;
58     objectStore = database.createObjectStore("TestObjectStore");
59     objectStore.put({ bar: "A" }, 1);
60     objectStore.put({ bar: "A" }, 2);
61
62     index = objectStore.createIndex("TestIndex1", "bar");
63     
64     checkIndexValues();
65     
66     versionTransaction.onabort = function(event) {
67         log("Initial upgrade versionchange transaction unexpected abort");
68         done();
69     }
70
71     versionTransaction.oncomplete = function(event) {
72         log("Initial upgrade versionchange transaction complete");
73         continueTest();
74     }
75
76     versionTransaction.onerror = function(event) {
77         log("Initial upgrade versionchange transaction unexpected error" + event);
78         done();
79     }
80 }
81
82 function continueTest() {
83     var transaction = database.transaction("TestObjectStore");
84     objectStore = transaction.objectStore("TestObjectStore");
85
86     var names = objectStore.indexNames;
87     log("Object store has indexes:")
88     for (var i = 0; i < names.length; ++i)
89         log(names[i]);
90     
91     try {
92         objectStore.index("TestIndex1");
93     } catch(e) {
94         log("Unable to get index from object store (it shouldn't exist)");
95     }
96
97     transaction.onabort = function(event) {
98         log("Transaction unexpected abort");
99         done();
100     }
101
102     transaction.oncomplete = function(event) {
103         log("Transaction complete");
104         done();
105     }
106
107     transaction.onerror = function(event) {
108         log("Transaction unexpected error" + event);
109         done();
110     }
111 }
112
113 </script>