9e0df77336f6dd90828e48ac978ca3192ed221a7
[WebKit-https.git] / LayoutTests / storage / indexeddb / modern / resources / deleteobjectstore-1.js
1 description("This test creates an object store then populates it, then commits that transaction. \
2 It then deletes it, but aborts that transaction. \
3 Finally it checks to make sure everything from step 1 is there as expected.");
4
5 if (window.testRunner) {
6     testRunner.waitUntilDone();
7     testRunner.dumpAsText();
8 }
9
10 function done()
11 {
12     finishJSTest();
13 }
14
15 var createRequest = window.indexedDB.open("DeleteObjectStore1Database", 1);
16
17 createRequest.onupgradeneeded = function(event) {
18     debug("ALERT: " + "Initial upgrade needed: Old version - " + event.oldVersion + " New version - " + event.newVersion);
19
20     var versionTransaction = createRequest.transaction;
21     var database = event.target.result;
22     var objectStore = database.createObjectStore("TestObjectStore");
23     for (var i = 0; i < 10; ++i)
24         objectStore.put("AH AH AH AH AH", i + " puts");
25
26     versionTransaction.onabort = function(event) {
27         debug("ALERT: " + "Initial upgrade versionchange transaction unexpected aborted");
28         done();
29     }
30
31     versionTransaction.oncomplete = function(event) {
32         debug("ALERT: " + "Initial upgrade versionchange transaction complete");
33         database.close();
34         continueTest1();
35     }
36
37     versionTransaction.onerror = function(event) {
38         debug("ALERT: " + "Initial upgrade versionchange transaction unexpected error" + event);
39         done();
40     }
41 }
42
43 function getChecker(event) {
44     debug("ALERT: " + "Value gotten was " + event.target.result);
45 }
46
47 function continueTest1()
48 {
49     var openRequest = window.indexedDB.open("DeleteObjectStore1Database", 2);
50
51     openRequest.onerror = function(event) {
52         debug("ALERT: " + "Request error - " + event);
53     }
54     openRequest.onblocked = function(event) {
55         debug("ALERT: " + "Request unexpected blocked - " + event);
56         done();
57     }
58     openRequest.onsuccess = function(event) {
59         debug("ALERT: " + "Request unexpected success - " + event);
60         done();
61     }
62
63     openRequest.onupgradeneeded = function(event) {
64         debug("ALERT: " + "Second upgrade needed: Old version - " + event.oldVersion + " New version - " + event.newVersion);
65         var versionTransaction = openRequest.transaction;
66         var database = event.target.result;
67         var objectStore = versionTransaction.objectStore("TestObjectStore");
68         
69         debug("ALERT: " + "Deleting object store");        
70         database.deleteObjectStore("TestObjectStore");
71
72         versionTransaction.abort();
73
74         versionTransaction.onabort = function(event) {
75             debug("ALERT: " + "Second version change transaction abort");
76             continueTest2();
77             database.close();
78         }
79
80         versionTransaction.oncomplete = function(event) {
81             debug("ALERT: " + "Second version change transaction unexpected complete");
82             done();
83         }
84
85         versionTransaction.onerror = function(event) {
86             debug("ALERT: " + "Second version change transaction error - " + event);
87         }
88     }
89 }
90
91 function continueTest2()
92 {
93     var openRequest = window.indexedDB.open("DeleteObjectStore1Database", 1);
94
95     openRequest.onerror = function(event) {
96         debug("ALERT: " + "Request unexpected error - " + event);
97         done();
98     }
99     openRequest.onblocked = function(event) {
100         debug("ALERT: " + "Request unexpected blocked - " + event);
101         done();
102     }
103     openRequest.onupgradeneeded = function(event) {
104         debug("ALERT: " + "Request unexpected upgradeneeded - " + event);
105         done();
106     }
107
108     openRequest.onsuccess = function(event) {
109         debug("ALERT: " + "Success opening database connection - Starting final transaction");
110         var database = event.target.result;
111         var transaction = database.transaction("TestObjectStore", "readwrite");
112         var objectStore = transaction.objectStore("TestObjectStore");
113     
114         var request;
115         for (var i = 0; i < 10; ++i) {
116             request = objectStore.get(i + " puts");
117             request.onsuccess = getChecker;
118         }
119
120         transaction.onabort = function(event) {
121             debug("ALERT: " + "Final transaction unexpected abort");
122             done();
123         }
124
125         transaction.oncomplete = function(event) {
126             debug("ALERT: " + "Final transaction complete");
127             done();
128         }
129
130         transaction.onerror = function(event) {
131             debug("ALERT: " + "Final transaction unexpected error - " + event);
132             done();
133         }
134     }
135 }