e429e1ad84df4dbe8492d81d5c4c2377f744da42
[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     // FIXME: once IDBObjectStore.deleteIndex() is implemented, try these
53     // https://bugs.webkit.org/show_bug.cgi?id=150911
54     // objectStore.deleteIndex("TestIndex");
55     // 
56     // try {
57     //     index.get("test");
58     // } catch(e) {
59     //     log("Failed to get with deleted IDBIndex");
60     // }
61     // 
62     // try {
63     //     index.getKey("test");
64     // } catch(e) {
65     //     log("Failed to getKey with deleted IDBIndex");
66     // }
67     // 
68     // try {
69     //     index.count();
70     // } catch(e) {
71     //     log("Failed to count with deleted IDBIndex");
72     // }
73
74     database.deleteObjectStore("TestObjectStore");
75     try {
76         index.get(null);
77     } catch(e) {
78          log("Failed to get with deleted IDBObjectStore");
79     }
80
81     try {
82         index.getKey(null);
83     } catch(e) {
84          log("Failed to getKey with deleted IDBObjectStore");
85     }
86
87     try {
88         index.count(null);
89     } catch(e) {
90          log("Failed to count with deleted IDBObjectStore");
91     }
92
93     var objectStore = database.createObjectStore("TestObjectStore2");
94     objectStore.createIndex("TestIndex", "foo");
95         
96     versionTransaction.onabort = function(event) {
97         log("Initial upgrade versionchange transaction unexpected aborted");
98         done();
99     }
100
101     versionTransaction.oncomplete = function(event) {
102         log("Initial upgrade versionchange transaction complete");
103         continueTest1();
104     }
105
106     versionTransaction.onerror = function(event) {
107         log("Initial upgrade versionchange transaction unexpected error" + event);
108         done();
109     }
110 }
111
112 function continueTest1()
113 {
114     var transaction = database.transaction("TestObjectStore2", "readonly");
115     var objectStore = transaction.objectStore("TestObjectStore2");
116     index = objectStore.index("TestIndex");
117
118     // Spin the transaction with get requests to keep it alive long enough for the setTimeout to fire.
119     var canFinish = false;
120     var spinGet = function() { 
121         objectStore.get("foo").onsuccess = function() {
122             if (!canFinish)
123                 spinGet();
124         }
125     }
126     spinGet();
127
128     var testWhileInactive = function() {
129         try {
130             index.get(null);
131         } catch(e) {
132              log("Failed to get while transaction inactive");
133         }
134
135         try {
136             index.getKey(null);
137         } catch(e) {
138              log("Failed to getKey while transaction inactive");
139         }
140
141         try {
142             index.count(null);
143         } catch(e) {
144              log("Failed to count while transaction inactive");
145         }
146         canFinish = true;
147     }
148     
149     setTimeout(testWhileInactive, 0);
150     
151     transaction.onabort = function(event) {
152         log("readonly transaction unexpected abort" + event);
153         done();
154     }
155
156     transaction.oncomplete = function(event) {
157         log("readonly transaction complete");
158         done();
159     }
160
161     transaction.onerror = function(event) {
162         log("readonly transaction unexpected error" + event);
163         done();
164     }
165 }
166
167 </script>