Modern IDB: After versionchange transactions complete, fire onsuccess on the original...
[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 <script>
3
4 if (window.testRunner) {
5     testRunner.waitUntilDone();
6     testRunner.dumpAsText();
7 }
8
9 function done()
10 {
11     alert("Done");
12     if (window.testRunner)
13         testRunner.notifyDone();
14 }
15
16 var request = window.indexedDB.open("VersionTestDatabase");
17 alert(request + " (firstPhase)");
18
19 request.onsuccess = function()
20 {
21     alert("First version change successful");
22 }
23 request.onerror = function(e)
24 {
25     alert("Unexpected error (firstPhase)");
26         done();
27 }
28
29 request.onupgradeneeded = function(e)
30 {
31     alert("upgradeneeded (firstPhase): old version - " + e.oldVersion + " new version - " + e.newVersion);
32     alert(request.transaction);
33     request.transaction.oncomplete = function()
34     {
35         alert("Version change complete (firstPhase). Database version is now - " + request.transaction.db.version);
36         request.transaction.db.close();
37         secondPhase();
38     }
39     request.transaction.onabort = function()
40     {
41         alert("Version change transaction unexpected abort! (firstPhase)");
42         done();
43     }
44     request.transaction.onerror = function()
45     {
46         alert("Version change transaction unexpected error! (firstPhase)");
47         done();
48     }
49 }
50
51 function secondPhase()
52 {
53     var request = window.indexedDB.open("VersionTestDatabase", 1);
54     alert(request + " (secondPhase)");
55     request.onsuccess = function()
56     {
57         alert("Successfully opened database at version 1 (secondPhase)");
58         request.result.close();
59         request.result.close(); // Close it twice just for the heck of it
60         thirdPhase();
61     }
62     request.onerror = function(e)
63     {
64         alert("Unexpected error (secondPhase)" + e);
65         done();
66     }
67     request.onupgradeneeded = function(e)
68     {
69         alert("Unexpected upgrade needed (secondPhase)" + e);
70         done();
71     }
72 }
73
74 function thirdPhase()
75 {
76     var request = window.indexedDB.open("VersionTestDatabase", 2);
77     alert(request + " (thirdPhase)");
78     request.onsuccess = function()
79     {
80         alert("Version change to version 2 successful");
81     }
82     request.onerror = function(e)
83     {
84         alert("Unexpected error (thirdPhase)");
85         done();
86     }
87     request.onupgradeneeded = function(e)
88     {
89         alert("upgradeneeded (thirdPhase): old version - " + e.oldVersion + " new version - " + e.newVersion);
90         alert(request.transaction);
91         request.transaction.oncomplete = function()
92         {
93             alert("Version change complete (thirdPhase). Database version is now - " + request.transaction.db.version);
94             request.transaction.db.close();
95             fourthPhase();
96         }
97         request.transaction.onabort = function()
98         {
99             alert("Version change transaction unexpected abort! (thirdPhase)");
100             done();
101         }
102         request.transaction.onerror = function()
103         {
104             alert("Version change transaction unexpected error! (thirdPhase)");
105             done();
106         }
107     } 
108 }
109
110 function fourthPhase()
111 {
112     // We've upgraded to version 2, so version 1 should not be openable.
113     var request = window.indexedDB.open("VersionTestDatabase", 1);
114     alert(request + " (fourthPhase)");
115     request.onsuccess = function()
116     {
117         alert("Unexpected success (fourthPhase)");
118         done();
119     }
120     request.onerror = function(e)
121     {
122         alert("Expected error (fourthPhase) - " + request.error.name);
123         done();
124     }
125     request.onupgradeneeded = function(e)
126     {
127         alert("Unexpected upgradeneeded (fourthPhase)");
128         done();
129     } 
130 }
131
132 </script>