Home | History | Annotate | Download | only in storage
      1 function finishTest()
      2 {
      3     if (window.layoutTestController)
      4         layoutTestController.notifyDone();
      5 }
      6 
      7 var TOTAL_TESTS = 7;
      8 var testsRun = 0;
      9 function transactionErrorCallback(error, expectedErrorCodeName)
     10 {
     11     if (error.code == error[expectedErrorCodeName]) {
     12         log("PASS: expected and got error code " + expectedErrorCodeName);
     13         if (++testsRun == TOTAL_TESTS)
     14             finishTest();
     15     } else {
     16         log("FAIL: expected error code " + expectedErrorCodeName + " (" + error[expectedErrorCodeName] + "); got " + error.code);
     17         finishTest();
     18     }
     19 }
     20 
     21 function transactionSuccessCallback()
     22 {
     23     log("FAIL: a transaction has completed successfully.");
     24     finishTest();
     25 }
     26 
     27 function testTransaction(db, transactionCallback, expectedErrorCodeName)
     28 {
     29     db.transaction(transactionCallback,
     30                    function(error) {
     31                        transactionErrorCallback(error, expectedErrorCodeName);
     32                    }, transactionSuccessCallback);
     33 }
     34 
     35 function testTransactionThrowsException(db)
     36 {
     37     testTransaction(db, function(tx) { throw "Exception thrown in transaction callback."; }, "UNKNOWN_ERR");
     38 }
     39 
     40 function testTransactionFailureBecauseOfStatementFailure(db)
     41 {
     42     testTransaction(db,
     43                     function(tx) {
     44                         tx.executeSql("BAD STATEMENT", [], null, function(tx, error) { return true; });
     45                     }, "UNKNOWN_ERR");
     46 }
     47 
     48 function testInvalidStatement(db)
     49 {
     50     testTransaction(db, function(tx) { tx.executeSql("BAD STATEMENT"); }, "SYNTAX_ERR");
     51 }
     52 
     53 function testIncorrectNumberOfBindParameters(db)
     54 {
     55     testTransaction(db,
     56                     function(tx) {
     57                         tx.executeSql("CREATE TABLE IF NOT EXISTS BadBindNumberTest (Foo INT, Bar INT)");
     58                         tx.executeSql("INSERT INTO BadBindNumberTest VALUES (?, ?)", [1]);
     59                     }, "SYNTAX_ERR");
     60 }
     61 
     62 function testBindParameterOfWrongType(db)
     63 {
     64     var badString = { };
     65     badString.toString = function() { throw "Cannot call toString() on this object." };
     66 
     67     testTransaction(db, function(tx) {
     68         tx.executeSql("CREATE TABLE IF NOT EXISTS BadBindTypeTest (Foo TEXT)");
     69         tx.executeSql("INSERT INTO BadBindTypeTest VALUES (?)", [badString]);
     70     }, "UNKNOWN_ERR");
     71 }
     72 
     73 function testQuotaExceeded(db)
     74 {
     75     testTransaction(db,
     76                     function(tx) {
     77                         tx.executeSql("CREATE TABLE IF NOT EXISTS QuotaTest (Foo BLOB)");
     78                         tx.executeSql("INSERT INTO QuotaTest VALUES (ZEROBLOB(10 * 1024 * 1024))");
     79                     }, "QUOTA_ERR");
     80 }
     81 
     82 function testVersionMismatch(db)
     83 {
     84     // Use another DB handle to change the version. However, in order to make sure that the DB version is not
     85     // changed before the transactions in the other tests have run, we need to do it in a transaction on 'db'.
     86     db.transaction(function(tx) {
     87         var db2 = openDatabaseWithSuffix("SQLErrorCodesTest", "1.0", "Tests the error codes.", 1);
     88         db2.changeVersion("1.0", "2.0", function(tx) { },
     89                           function(error) {
     90                               log("FAIL: could not change the DB version.");
     91                               finishTest();
     92                           }, function() { });
     93         });
     94 
     95     testTransaction(db,
     96                     function(tx) {
     97                         tx.executeSql("THIS STATEMENT SHOULD NEVER GET EXECUTED");
     98                     }, "VERSION_ERR");
     99 }
    100 
    101 function runTest()
    102 {
    103     if (window.layoutTestController)
    104         layoutTestController.clearAllDatabases();
    105 
    106     var db = openDatabaseWithSuffix("SQLErrorCodesTest", "1.0", "Tests the error codes.", 1);
    107     testTransactionThrowsException(db);
    108     testTransactionFailureBecauseOfStatementFailure(db);
    109     testInvalidStatement(db);
    110     testIncorrectNumberOfBindParameters(db);
    111     testBindParameterOfWrongType(db);
    112     testQuotaExceeded(db);
    113     testVersionMismatch(db);
    114 }
    115