summaryrefslogtreecommitdiffstats
path: root/LayoutTests/storage/sql-error-codes.js
blob: 2d2c97c81fa85bc36d46df85fb7bd126a3a586d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
function finishTest()
{
    if (window.layoutTestController)
        layoutTestController.notifyDone();
}

var TOTAL_TESTS = 7;
var testsRun = 0;
function transactionErrorCallback(error, expectedErrorCodeName)
{
    if (error.code == error[expectedErrorCodeName]) {
        log("PASS: expected and got error code " + expectedErrorCodeName);
        if (++testsRun == TOTAL_TESTS)
            finishTest();
    } else {
        log("FAIL: expected error code " + expectedErrorCodeName + " (" + error[expectedErrorCodeName] + "); got " + error.code);
        finishTest();
    }
}

function transactionSuccessCallback()
{
    log("FAIL: a transaction has completed successfully.");
    finishTest();
}

function testTransaction(db, transactionCallback, expectedErrorCodeName)
{
    db.transaction(transactionCallback,
                   function(error) {
                       transactionErrorCallback(error, expectedErrorCodeName);
                   }, transactionSuccessCallback);
}

function testTransactionThrowsException(db)
{
    testTransaction(db, function(tx) { throw "Exception thrown in transaction callback."; }, "UNKNOWN_ERR");
}

function testTransactionFailureBecauseOfStatementFailure(db)
{
    testTransaction(db,
                    function(tx) {
                        tx.executeSql("BAD STATEMENT", [], null, function(tx, error) { return true; });
                    }, "UNKNOWN_ERR");
}

function testInvalidStatement(db)
{
    testTransaction(db, function(tx) { tx.executeSql("BAD STATEMENT"); }, "SYNTAX_ERR");
}

function testIncorrectNumberOfBindParameters(db)
{
    testTransaction(db,
                    function(tx) {
                        tx.executeSql("CREATE TABLE IF NOT EXISTS BadBindNumberTest (Foo INT, Bar INT)");
                        tx.executeSql("INSERT INTO BadBindNumberTest VALUES (?, ?)", [1]);
                    }, "SYNTAX_ERR");
}

function testBindParameterOfWrongType(db)
{
    var badString = { };
    badString.toString = function() { throw "Cannot call toString() on this object." };

    testTransaction(db, function(tx) {
        tx.executeSql("CREATE TABLE IF NOT EXISTS BadBindTypeTest (Foo TEXT)");
        tx.executeSql("INSERT INTO BadBindTypeTest VALUES (?)", [badString]);
    }, "UNKNOWN_ERR");
}

function testQuotaExceeded(db)
{
    testTransaction(db,
                    function(tx) {
                        tx.executeSql("CREATE TABLE IF NOT EXISTS QuotaTest (Foo BLOB)");
                        tx.executeSql("INSERT INTO QuotaTest VALUES (ZEROBLOB(10 * 1024 * 1024))");
                    }, "QUOTA_ERR");
}

function testVersionMismatch(db)
{
    // Use another DB handle to change the version. However, in order to make sure that the DB version is not
    // changed before the transactions in the other tests have run, we need to do it in a transaction on 'db'.
    db.transaction(function(tx) {
        var db2 = openDatabaseWithSuffix("SQLErrorCodesTest", "1.0", "Tests the error codes.", 1);
        db2.changeVersion("1.0", "2.0", function(tx) { },
                          function(error) {
                              log("FAIL: could not change the DB version.");
                              finishTest();
                          }, function() { });
        });

    testTransaction(db,
                    function(tx) {
                        tx.executeSql("THIS STATEMENT SHOULD NEVER GET EXECUTED");
                    }, "VERSION_ERR");
}

function runTest()
{
    if (window.layoutTestController)
        layoutTestController.clearAllDatabases();

    var db = openDatabaseWithSuffix("SQLErrorCodesTest", "1.0", "Tests the error codes.", 1);
    testTransactionThrowsException(db);
    testTransactionFailureBecauseOfStatementFailure(db);
    testInvalidStatement(db);
    testIncorrectNumberOfBindParameters(db);
    testBindParameterOfWrongType(db);
    testQuotaExceeded(db);
    testVersionMismatch(db);
}