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