7be934cfd9a14d070c0030735617177dd6d0785b
[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         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
59         // FIXME: The following call will be used once bugs 150147-150150 are resolved.
60         // thirdPhase();
61         done();
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             fourthPhase();
97         }
98         request.transaction.onabort = function()
99         {
100             alert("Version change transaction unexpected abort! (thirdPhase)");
101             done();
102         }
103         request.transaction.onerror = function()
104         {
105             alert("Version change transaction unexpected error! (thirdPhase)");
106             done();
107         }
108     } 
109 }
110
111 function fourthPhase()
112 {
113     try {
114         // We've upgraded to version 2, so version 1 should not be openable.
115         window.indexedDB.open("VersionTestDatabase", 1);
116     } catch (e) {
117         alert("Caught exception " + e);
118     }
119     done();
120 }
121
122 </script>