Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / fast / workers / storage / resources / sql-exception-codes-sync.js
1 var dbName = "SQLExceptionCodesTest" + (new Date()).getTime();
2
3 function testTransaction(db, executeStatementsCallback, expectedError)
4 {
5     db.transaction(function(tx) {
6         try {
7             executeStatementsCallback(tx);
8             postMessage("FAIL: an exception (" + expectedError + ") should've been thrown.");
9         } catch (err) {
10             postMessage("LOG: " + err);
11             if (typeof err == "string" && err == expectedError)
12                 postMessage("PASS: expected and got error with message " + expectedError);
13             else if (err.name == expectedError)
14                 postMessage("PASS: expected and got error name " + expectedError);
15             else
16                 postMessage("FAIL: expected error name " + expectedError + ", got " + err);
17         }
18       });
19 }
20
21 function testTransactionThrowsException(db)
22 {
23     testTransaction(db, function(tx) { throw "Exception thrown in transaction callback."; }, "Exception thrown in transaction callback.");
24 }
25
26 function testInvalidStatement(db)
27 {
28     testTransaction(db, function(tx) { tx.executeSql("BAD STATEMENT"); }, "SyntaxError");
29 }
30
31 function testIncorrectNumberOfBindParameters(db)
32 {
33     testTransaction(db,
34                     function(tx) {
35                         tx.executeSql("CREATE TABLE IF NOT EXISTS BadBindNumberTest (Foo INT, Bar INT)");
36                         tx.executeSql("INSERT INTO BadBindNumberTest VALUES (?, ?)", [1]);
37                     }, "SyntaxError");
38 }
39
40 function testBindParameterOfWrongType(db)
41 {
42     var badString = { };
43     badString.toString = function() { throw "Cannot call toString() on this object." };
44
45     testTransaction(db, function(tx) {
46         tx.executeSql("CREATE TABLE IF NOT EXISTS BadBindTypeTest (Foo TEXT)");
47         tx.executeSql("INSERT INTO BadBindTypeTest VALUES (?)", [badString]);
48     }, "Cannot call toString() on this object.");
49 }
50
51 function testQuotaExceeded(db)
52 {
53     // Sometimes, SQLite automatically rolls back a transaction if executing a statement fails.
54     // This seems to be one of those cases.
55     try {
56         testTransaction(db,
57                         function(tx) {
58                             tx.executeSql("CREATE TABLE IF NOT EXISTS QuotaTest (Foo BLOB)");
59                             tx.executeSql("INSERT INTO QuotaTest VALUES (ZEROBLOB(10 * 1024 * 1024))");
60                         }, "QuotaExceededError");
61         postMessage("FAIL: Transaction should've been rolled back by SQLite.");
62     } catch (err) {
63         if (err.name == "DatabaseError")
64             postMessage("PASS: Transaction was rolled back by SQLite as expected.");
65         else
66             postMessage("FAIL: An unexpected exception was thrown: " + err);
67     }
68 }
69
70 function testVersionMismatch(db)
71 {
72     var db2 = openDatabaseSync(dbName, "1.0", "Tests the error codes.", 1);
73     db2.changeVersion("1.0", "2.0", function(tx) { });
74     testTransaction(db,
75                     function(tx) {
76                         tx.executeSql("THIS STATEMENT SHOULD NEVER GET EXECUTED");
77                     }, "VersionError");
78 }
79
80 var db = openDatabaseSync(dbName, "1.0", "Tests the exception codes.", 1);
81 testTransactionThrowsException(db);
82 testInvalidStatement(db);
83 testIncorrectNumberOfBindParameters(db);
84 testBindParameterOfWrongType(db);
85 testQuotaExceeded(db);
86 testVersionMismatch(db);
87
88 postMessage("done");