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 db1Name = "OpenDatabaseCreationCallback1" + (new Date()).getTime(); 30 var db1 = openDatabase(db1Name, "1.0", "", 1, 31 function(db) { 32 creationCallbackCalled1 = true; 33 if (db.version != "") { 34 log("Creation callback was called with a database with version " + 35 db.version + "; empty string expected."); 36 finishTest(); 37 } 38 }); 39 40 // Putting this code inside a transaction on 'db1' makes sure that it is executed after 41 // the creation callback is. 42 db1.transaction(function(tx) { 43 if (!creationCallbackCalled1) { 44 log("Creation callback for db1 was not called."); 45 finishTest(); 46 } 47 if (++transactionsRun == 2) 48 finishTest(); 49 }); 50 51 // Try to open another handle to the same database. 52 // Since the version of this database is "" (empty string), openDatabase() should return 53 // a null handle and throw a INVALID_STATE_ERR exception. 54 var db1Fail = null; 55 try { 56 db1Fail = openDatabase(db1Name, "1.0", "", 1); 57 log("This statement should not have been executed; an INVALID_STATE_ERR exception should've been thrown."); 58 finishTest(); 59 } catch(err) { 60 if (db1Fail) { 61 log("db1Fail should have been null."); 62 finishTest(); 63 } 64 } 65 66 // Open a handle to another database, first without a creation callback, then with one. 67 // Make sure the creation callback is not called. 68 var creationCallbackCalled2 = false; 69 var db2 = openDatabase("OpenDatabaseCreationCallback2", "1.0", "", 1); 70 db2 = openDatabase("OpenDatabaseCreationCallback2", "1.0", "", 1, 71 function(db) { creationCallbackCalled2 = true; }); 72 db2.transaction(function(tx) { 73 if (creationCallbackCalled2) { 74 log("Creation callback for db2 should not have been called."); 75 finishTest(); 76 } 77 if (++transactionsRun == 2) 78 finishTest(); 79 }); 80 } 81 82 </script> 83 </head> 84 85 <body onload="runTest()"> 86 This test tests openDatabase()'s creation callback. 87 <pre id="console"> 88 </pre> 89 </body> 90 91 </html> 92