WebCore: Adding support for the optional creation callback that could be
[WebKit-https.git] / LayoutTests / storage / open-database-creation-callback.html
1 <html>
2 <head>
3 <script>
4 function log(message)
5 {
6     document.getElementById("console").innerHTML += message + "<br>";
7 }
8
9 function finishTest()
10 {
11     if (window.layoutTestController)
12         layoutTestController.notifyDone();
13 }
14
15 function runTest()
16 {
17     if (window.layoutTestController) {
18         layoutTestController.clearAllDatabases();
19         layoutTestController.setDatabaseQuota(32768);
20         layoutTestController.dumpDatabaseCallbacks();
21         layoutTestController.dumpAsText();
22         layoutTestController.waitUntilDone();
23     }
24
25     var transactionsRun = 0;
26
27     // Open a new database with a creation callback, and make sure the creation callback is queued
28     var creationCallbackCalled1 = false;
29     var db1 = openDatabase("OpenDatabaseCreationCallback1", "1.0", "", 1,
30                            function(db) {
31                                creationCallbackCalled1 = true;
32                                if (db.version != "") {
33                                    log("Creation callback was called with a database with version " +
34                                        db.version + "; empty string expected.");
35                                    finishTest();
36                                }
37                            });
38
39     // Putting this code inside a transaction on 'db1' makes sure that it is executed after
40     // the creation callback is.
41     db1.transaction(function(tx) {
42         if (!creationCallbackCalled1) {
43             log("Creation callback for db1 was not called.");
44             finishTest();
45         }
46         if (++transactionsRun == 2)
47             finishTest();
48     });
49
50     // Try to open another handle to the same database.
51     // Since the version of this database is "" (empty string), openDatabase() should return
52     // a null handle and throw a INVALID_STATE_ERR exception.
53     var db1Fail = null;
54     try {
55         db1Fail = openDatabase("OpenDatabaseCreationCallback1", "1.0", "", 1);
56         log("This statement should not have been executed; an INVALID_STATE_ERR exception should've been thrown.");
57         finishTest();
58     } catch(err) {
59         if (db1Fail) {
60             log("db1Fail should have been null.");
61             finishTest();
62         }
63     }
64
65     // Open a handle to another database, first without a creation callback, then with one.
66     // Make sure the creation callback is not called.
67     var creationCallbackCalled2 = false;
68     var db2 = openDatabase("OpenDatabaseCreationCallback2", "1.0", "", 1);
69     db2 = openDatabase("OpenDatabaseCreationCallback2", "1.0", "", 1,
70                        function(db) { creationCallbackCalled2 = true; });
71     db2.transaction(function(tx) {
72         if (creationCallbackCalled2) {
73             log("Creation callback for db2 should not have been called.");
74             finishTest();
75         }
76         if (++transactionsRun == 2)
77             finishTest();
78     });
79 }
80
81 </script>
82 </head>
83
84 <body onload="runTest()">
85 This test tests openDatabase()'s creation callback.
86 <pre id="console">
87 </pre>
88 </body>
89
90 </html>