Modern IDB: IDBObjectStore.deleteIndex() support.
[WebKit-https.git] / LayoutTests / storage / indexeddb / modern / index-get-count-failures.html
1 This tests some obvious failures that can happen while calling the IDBIndex methods get(), getKey(), and count().<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("IndexGetCountFailuresDatabase", 1);
23 var database;
24 var index;
25
26 createRequest.onupgradeneeded = function(event) {
27     log("Initial upgrade needed: Old version - " + event.oldVersion + " New version - " + event.newVersion);
28
29     var versionTransaction = createRequest.transaction;
30     database = event.target.result;
31     var objectStore = database.createObjectStore("TestObjectStore");
32     index = objectStore.createIndex("TestIndex", "foo");
33     
34     try {
35         index.get(null);
36     } catch(e) {
37         log("Failed to get with a null key");
38     }
39
40     try {
41         index.getKey(null);
42     } catch(e) {
43         log("Failed to getKey with a null key");
44     }
45
46     try {
47         index.count(null);
48     } catch(e) {
49         log("Failed to count with a null range");
50     }
51     
52     objectStore.deleteIndex("TestIndex");
53     
54     try {
55         index.get("test");
56     } catch(e) {
57         log("Failed to get with deleted IDBIndex");
58     }
59     
60     try {
61         index.getKey("test");
62     } catch(e) {
63         log("Failed to getKey with deleted IDBIndex");
64     }
65     
66     try {
67         index.count();
68     } catch(e) {
69         log("Failed to count with deleted IDBIndex");
70     }
71     
72     try {
73         objectStore.deleteIndex("TestIndex");
74     } catch(e) {
75         log("Failed to delete a nonexistent IDBIndex");
76     }
77
78     database.deleteObjectStore("TestObjectStore");
79     try {
80         index.get(null);
81     } catch(e) {
82          log("Failed to get with deleted IDBObjectStore");
83     }
84
85     try {
86         index.getKey(null);
87     } catch(e) {
88          log("Failed to getKey with deleted IDBObjectStore");
89     }
90
91     try {
92         index.count(null);
93     } catch(e) {
94          log("Failed to count with deleted IDBObjectStore");
95     }
96
97     var objectStore = database.createObjectStore("TestObjectStore2");
98     objectStore.createIndex("TestIndex", "foo");
99         
100     versionTransaction.onabort = function(event) {
101         log("Initial upgrade versionchange transaction unexpected aborted");
102         done();
103     }
104
105     versionTransaction.oncomplete = function(event) {
106         log("Initial upgrade versionchange transaction complete");
107         continueTest1();
108     }
109
110     versionTransaction.onerror = function(event) {
111         log("Initial upgrade versionchange transaction unexpected error" + event);
112         done();
113     }
114 }
115
116 function continueTest1()
117 {
118     var transaction = database.transaction("TestObjectStore2", "readonly");
119     var objectStore = transaction.objectStore("TestObjectStore2");
120     index = objectStore.index("TestIndex");
121
122     // Spin the transaction with get requests to keep it alive long enough for the setTimeout to fire.
123     var canFinish = false;
124     var spinGet = function() { 
125         objectStore.get("foo").onsuccess = function() {
126             if (!canFinish)
127                 spinGet();
128         }
129     }
130     spinGet();
131
132     var testWhileInactive = function() {
133         try {
134             index.get(null);
135         } catch(e) {
136              log("Failed to get while transaction inactive");
137         }
138
139         try {
140             index.getKey(null);
141         } catch(e) {
142              log("Failed to getKey while transaction inactive");
143         }
144
145         try {
146             index.count(null);
147         } catch(e) {
148              log("Failed to count while transaction inactive");
149         }
150         canFinish = true;
151     }
152     
153     setTimeout(testWhileInactive, 0);
154     
155     transaction.onabort = function(event) {
156         log("readonly transaction unexpected abort" + event);
157         done();
158     }
159
160     transaction.oncomplete = function(event) {
161         log("readonly transaction complete");
162         done();
163     }
164
165     transaction.onerror = function(event) {
166         log("readonly transaction unexpected error" + event);
167         done();
168     }
169 }
170
171 </script>