Modern IDB: Basic createObjectStore implementation.
[WebKit-https.git] / LayoutTests / storage / indexeddb / modern / createobjectstore-basic.html
1 This test starts some version change transactions, creates some object stores, and variably commits or aborts the version change transactions.<br>
2 At various stages it verifies the object stores in the database are as-expected.<br>
3 <script>
4
5 if (window.testRunner) {
6     testRunner.waitUntilDone();
7     testRunner.dumpAsText();
8 }
9
10 function done()
11 {
12     alert("Done");
13     if (window.testRunner)
14         testRunner.notifyDone();
15 }
16
17 function dumpObjectStores(database) {
18     var list = database.objectStoreNames;
19     alert("Object store names:");
20     for (var i = 0; i < list.length; ++i) { 
21         alert(list[i]);
22     }
23 }
24
25 var createRequest = window.indexedDB.open("CreateObjectStoreTestDatabase", 1);
26
27 createRequest.onupgradeneeded = function(event) {
28     alert("Initial upgrade needed: Old version - " + event.oldVersion + " New version - " + event.newVersion);
29
30     var versionTransaction = createRequest.transaction;
31     var database = event.target.result;
32     var objectStore = database.createObjectStore("FirstAbortedObjectStore");
33
34     var f = function() {
35         versionTransaction.abort();
36     };
37
38     dumpObjectStores(database);    
39     setTimeout(f, 0);
40     
41     versionTransaction.onabort = function(event) {
42         alert("Initial upgrade versionchange transaction aborted");
43         dumpObjectStores(database);
44         continueTest1();
45         database.close();
46     }
47
48     versionTransaction.oncomplete = function(event) {
49         alert("Initial upgrade versionchange transaction unexpected complete");
50         done();
51     }
52
53     versionTransaction.onerror = function(event) {
54         alert("Initial upgrade versionchange transaction unexpected error" + event);
55         done();
56     }
57 }
58
59 function continueTest1()
60 {
61     createRequest = window.indexedDB.open("CreateObjectStoreTestDatabase", 1);
62
63     createRequest.onupgradeneeded = function(event) {
64         alert("Second upgrade needed: Old version - " + event.oldVersion + " New version - " + event.newVersion);
65
66         var versionTransaction = createRequest.transaction;
67         var database = event.target.result;
68         dumpObjectStores(database);
69         var objectStore = database.createObjectStore("FirstCommittedObjectStore");
70
71         versionTransaction.onabort = function(event) {
72             alert("Second upgrade versionchange transaction unexpected abort");
73             done();
74         }
75
76         versionTransaction.oncomplete = function(event) {
77             alert("Second upgrade versionchange transaction complete");
78             dumpObjectStores(database);
79             continueTest2();
80             database.close();
81         }
82
83         versionTransaction.onerror = function(event) {
84             alert("Second upgrade versionchange transaction unexpected error" + event);
85             done();
86         }
87     }
88 }
89
90 function continueTest2()
91 {
92     createRequest = window.indexedDB.open("CreateObjectStoreTestDatabase", 2);
93
94     createRequest.onupgradeneeded = function(event) {
95         alert("Third upgrade needed: Old version - " + event.oldVersion + " New version - " + event.newVersion);
96
97         var versionTransaction = createRequest.transaction;
98         var database = event.target.result;
99         var objectStore = database.createObjectStore("SecondCommittedObjectStore");
100
101         dumpObjectStores(database);    
102     
103         versionTransaction.onabort = function(event) {
104             alert("Third upgrade versionchange transaction unexpected abort");
105             done();
106         }
107
108         versionTransaction.oncomplete = function(event) {
109             alert("Third upgrade versionchange transaction complete");
110             dumpObjectStores(database);
111             database.close(); 
112             continueTest3();
113         }
114
115         versionTransaction.onerror = function(event) {
116             alert("Third upgrade versionchange transaction unexpected error" + event);
117             done();
118         }
119     }
120 }
121
122 function continueTest3()
123 {
124     createRequest = window.indexedDB.open("CreateObjectStoreTestDatabase", 3);
125
126     createRequest.onupgradeneeded = function(event) {
127         alert("Fourth upgrade needed: Old version - " + event.oldVersion + " New version - " + event.newVersion);
128         var database = event.target.result;
129         dumpObjectStores(database);
130         done();
131     }
132 }
133
134 </script>