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