977d47c78392933e950dc7dbadcb0b38a3a7576b
[WebKit-https.git] / LayoutTests / storage / indexeddb / modern / idbdatabase-deleteobjectstore-failures.html
1 This tests some obvious failures that can happen while calling IDBDatabase.deleteObjectStore()
2 <script>
3
4 if (window.testRunner) {
5     testRunner.waitUntilDone();
6     testRunner.dumpAsText();
7 }
8
9 function done()
10 {
11     alert("Done");
12     if (window.testRunner)
13         testRunner.notifyDone();
14 }
15
16 var createRequest = window.indexedDB.open("IDBDatabaseDeleteObjectStoreFailuresDatabase", 1);
17 var database;
18
19 createRequest.onupgradeneeded = function(event) {
20     alert("Initial upgrade needed: Old version - " + event.oldVersion + " New version - " + event.newVersion);
21
22     var versionTransaction = createRequest.transaction;
23     database = event.target.result;
24     var objectStore = database.createObjectStore("TestObjectStore");
25     var request = objectStore.put("bar", "foo");
26
27     versionTransaction.onabort = function(event) {
28         alert("Initial upgrade versionchange transaction unexpected aborted");
29         done();
30     }
31
32     versionTransaction.oncomplete = function(event) {
33         alert("Initial upgrade versionchange transaction complete");
34         continueTest1();
35     }
36
37     versionTransaction.onerror = function(event) {
38         alert("Initial upgrade versionchange transaction unexpected error" + event);
39         done();
40     }
41 }
42
43 function continueTest1()
44 {
45     var transaction = database.transaction("TestObjectStore", "readwrite");
46     var objectStore = transaction.objectStore("TestObjectStore");
47     var request = objectStore.put("baz", "foo");
48
49     request.onsuccess = function() {
50         alert("readwrite put success - about to try to delete an objectstore");
51         try {
52             database.deleteObjectStore("TestObjectStore");
53         } catch(e) {
54             alert("Failed to deleteObjectStore without a versionchange transaction - " + e);
55         }
56     }
57     
58     transaction.onabort = function(event) {
59         alert("readwrite transaction unexpected aborted");
60         done();
61     }
62
63     transaction.oncomplete = function(event) {
64         alert("readwrite transaction complete");
65         continueTest2();
66         database.close();
67     }
68
69     transaction.onerror = function(event) {
70         alert("readwrite transaction unexpected error" + event);
71         done();
72     }
73 }
74
75 function continueTest2()
76 {
77     var openRequest = window.indexedDB.open("IDBDatabaseDeleteObjectStoreFailuresDatabase", 2);
78
79     openRequest.onerror = function(event) {
80         alert("Request unexpected error - " + event);
81         done();
82     }
83     openRequest.onblocked = function(event) {
84         alert("Request unexpected blocked - " + event);
85         done();
86     }
87     openRequest.onsuccess = function(event) {
88         alert("Request unexpected success - " + event);
89         done();
90     }
91
92     openRequest.onupgradeneeded = function(event) {
93         alert("Second upgrade needed: Old version - " + event.oldVersion + " New version - " + event.newVersion);
94     
95         var versionTransaction = openRequest.transaction;
96         database = event.target.result;
97
98         try {
99             database.deleteObjectStore("NonexistentObjectStore");
100         } catch(e) {
101             alert("Failed to deleteObjectStore with a non-existent objectstore - " + e);
102         }
103
104         // Spin the transaction with get requests to keep it alive long enough for the setTimeout to fire.
105         var objectStore = versionTransaction.objectStore("TestObjectStore");
106         var canFinish = false;
107         var spinGet = function() { 
108             objectStore.get("foo").onsuccess = function() {
109                 if (!canFinish)
110                     spinGet();
111             }
112         }
113         spinGet();
114         
115         // After the versionChange transaction becomes inactive, but while it's still in-progress, try to delete the objectstore
116         var tryInactiveDelete = function() 
117         {
118             try {
119                 database.deleteObjectStore("TestObjectStore");
120             } catch(e) {
121                 alert("Failed to deleteObjectStore with an in-progress versionchange transaction that is inactive - " + e);
122             }
123             canFinish = true;
124         }
125         
126         setTimeout(tryInactiveDelete, 0);
127
128         versionTransaction.onabort = function(event) {
129             alert("Second version change transaction unexpected abort");
130             done();
131         }
132
133         versionTransaction.oncomplete = function(event) {
134             alert("Second version change transaction complete");
135             done();
136         }
137
138         versionTransaction.onerror = function(event) {
139             alert("Second version change transaction unexpected error - " + event);
140             done();
141         }
142     }
143 }
144 </script>