1//description("This test verifies that the javascript values returned by database queries are of same type as the values put into the database.");
2
3function writeMessageToLog(message)
4{
5    document.getElementById("console").innerText += message + "\n";
6}
7
8function notifyDone(str) {
9    writeMessageToLog(str);
10    if (window.layoutTestController)
11        layoutTestController.notifyDone();
12}
13
14var testValues = {
15    timestamp: new Date("Wed Feb 06 2008 12:16:52 GMT+0200 (EET)").valueOf(),
16    id: 1001,
17    real: 101.444,
18    text: "WebKit db TEXT",
19    blob: "supercalifragilistic"
20};
21
22function shouldBeSameTypeAndValue(propName, testValue, result) {
23    if (testValue == result && typeof testValue == typeof result) {
24        writeMessageToLog("PASS: property '" + propName + "' ok, type was " + typeof result);
25        return true;
26    }
27    writeMessageToLog("FAIL: property '" + propName + "' failed."
28        + " expected: " + typeof testValue + ":'" + testValue + "' "
29        + " got: " + typeof result + ":'" + result +"'");
30    return false;
31}
32
33function testDBValues(tx, result) {
34    var rs = result.rows.item(0);
35    // Avoid for .. in because (theretically) the order can change
36    i = "timestamp"; shouldBeSameTypeAndValue(i, testValues[i], rs[i]);
37    i = "id"; shouldBeSameTypeAndValue(i, testValues[i], rs[i]);
38    i = "real"; shouldBeSameTypeAndValue(i, testValues[i], rs[i]);
39    i = "text"; shouldBeSameTypeAndValue(i, testValues[i], rs[i]);
40    i = "blob"; shouldBeSameTypeAndValue(i, testValues[i], rs[i]);
41
42    tx.executeSql("DROP TABLE DataTypeTestTable", [],
43        function(tx, result) {
44            notifyDone("PASS: database clean up ok.");
45        },
46        function(tx, result) {
47            notifyDone("FAIL: Database clean up failed.");
48        });
49}
50
51function fetchDBValuesStmt(tx, result) {
52    tx.executeSql("SELECT * FROM DataTypeTestTable", [],
53        testDBValues,
54        function(tx,error) {
55            notifyDone("FAIL: Error fetching values from the db.")
56        });
57}
58
59function insertTestValuesStmt(tx, result) {
60    tx.executeSql("INSERT INTO DataTypeTestTable (id, real, timestamp, text, blob) VALUES (?,?,?,?,?)",
61        [testValues.id, testValues.real, testValues.timestamp, testValues.text, testValues.blob],
62        fetchDBValuesStmt,
63        function(tx, error) {
64            notifyDone("FAIL: Error inserting values to the db.");
65        });
66}
67
68function createTestDBStmt(tx)
69{
70    tx.executeSql("CREATE TABLE IF NOT EXISTS DataTypeTestTable (id INTEGER UNIQUE, real REAL, timestamp INTEGER, text TEXT, blob BLOB)", [],
71        insertTestValuesStmt,
72        function(tx, error) {
73            notifyDone("FAIL: Error creating the db.");
74        });
75}
76
77function runTest() {
78    if (window.layoutTestController) {
79        layoutTestController.dumpAsText();
80        layoutTestController.waitUntilDone();
81    }
82    var db = openDatabase("DataTypeTest", "1.0", "Database for sql data type test", 1);
83    if (db)
84        db.transaction(createTestDBStmt);
85    else
86        notifyDone("FAIL: Error opening the db");
87}
88