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