Modern IDB: storage/indexeddb/delete-in-upgradeneeded-close-in-versionchange.html...
[WebKit-https.git] / LayoutTests / storage / indexeddb / modern / opendatabase-versions.html
1 This test creates a new database with the default version, commits that versionchange transaction, and then reopens it at different versions to make sure the IDBOpenDBRequests behave appropriately.<br>
2 <div id="logger"></div><br>
3 <script>
4
5 function log(msg)
6 {
7     document.getElementById("logger").innerHTML += msg + "<br>";
8 }
9
10 if (window.testRunner) {
11     testRunner.waitUntilDone();
12     testRunner.dumpAsText();
13 }
14
15 function done()
16 {
17     log("Done");
18     if (window.testRunner)
19         testRunner.notifyDone();
20 }
21
22 var request = window.indexedDB.open("VersionTestDatabase");
23 log(request + " (firstPhase)");
24
25 request.onsuccess = function()
26 {
27     log("First version change successful");
28 }
29 request.onerror = function(e)
30 {
31     log("Open request error (firstPhase) " + request.error.name);
32 }
33
34 request.onupgradeneeded = function(e)
35 {
36     var database = event.target.result;
37
38     log("upgradeneeded (firstPhase): old version - " + e.oldVersion + " new version - " + e.newVersion);
39     log(request.transaction);
40     request.transaction.oncomplete = function()
41     {
42         log("Version change complete (firstPhase). Database version is now - " + database.version);
43         database.close();
44         secondPhase();
45     }
46     request.transaction.onabort = function()
47     {
48         log("Version change transaction unexpected abort! (firstPhase)");
49         done();
50     }
51     request.transaction.onerror = function()
52     {
53         log("Version change transaction unexpected error! (firstPhase)");
54         done();
55     }
56 }
57
58 function secondPhase()
59 {
60     var request = window.indexedDB.open("VersionTestDatabase", 1);
61     log(request + " (secondPhase)");
62     request.onsuccess = function()
63     {
64         log("Successfully opened database at version 1 (secondPhase)");
65         request.result.close();
66         request.result.close(); // Close it twice just for the heck of it
67         thirdPhase();
68     }
69     request.onerror = function(e)
70     {
71         log("Unexpected error (secondPhase)" + e);
72         done();
73     }
74     request.onupgradeneeded = function(e)
75     {
76         log("Unexpected upgrade needed (secondPhase)" + e);
77         done();
78     }
79 }
80
81 function thirdPhase()
82 {
83     var request = window.indexedDB.open("VersionTestDatabase", 2);
84     log(request + " (thirdPhase)");
85     request.onsuccess = function()
86     {
87         log("Version change to version 2 successful");
88     }
89     request.onerror = function(e)
90     {
91         log("Open request error (thirdPhase) " + request.error.name);
92     }
93     request.onupgradeneeded = function(e)
94     {
95         var database = event.target.result;
96
97         log("upgradeneeded (thirdPhase): old version - " + e.oldVersion + " new version - " + e.newVersion);
98         log(request.transaction);
99         request.transaction.oncomplete = function()
100         {
101             log("Version change complete (thirdPhase). Database version is now - " + database.version);
102             database.close();
103             fourthPhase();
104         }
105         request.transaction.onabort = function()
106         {
107             log("Version change transaction unexpected abort! (thirdPhase)");
108             done();
109         }
110         request.transaction.onerror = function()
111         {
112             log("Version change transaction unexpected error! (thirdPhase)");
113             done();
114         }
115     } 
116 }
117
118 function fourthPhase()
119 {
120     // We've upgraded to version 2, so version 1 should not be openable.
121     var request = window.indexedDB.open("VersionTestDatabase", 1);
122     log(request + " (fourthPhase)");
123     request.onsuccess = function()
124     {
125         log("Unexpected success (fourthPhase)");
126         done();
127     }
128     request.onerror = function(e)
129     {
130         log("Expected error (fourthPhase) - " + request.error.name);
131         done();
132     }
133     request.onupgradeneeded = function(e)
134     {
135         log("Unexpected upgradeneeded (fourthPhase)");
136         done();
137     } 
138 }
139
140 </script>