Get foreground tab on Android
[chromium-blink-merge.git] / content / test / data / simple_database.html
blob6fe544602c7bfbd6ebb7c5e18f361b3359c49f9e
1 <html>
2 <script>
4 // Open a Web SQL database.
5 var g_db = null;
6 if (typeof window.openDatabase == "undefined") {
7 document.write("Error: Web SQL databases are not supported.");
9 try {
10 g_db = openDatabase("test", "1.0", "test database", 1024 * 1024);
11 } catch(err) {
12 document.write("Error: cannot open database.");
15 // Creates a table named "table1" with one text column named "data".
16 function createTable() {
17 if (!g_db)
18 return;
19 g_db.transaction(
20 function(tx) {
21 tx.executeSql("CREATE TABLE table1 (data TEXT)");
23 function(error) {
24 sendValueToTest(error);
26 function() {
27 sendValueToTest("done");
28 });
31 // Inserts a record into the database.
32 function insertRecord(text) {
33 g_db.transaction(
34 function(tx) {
35 tx.executeSql("INSERT INTO table1 VALUES (?)", [text]);
37 function(error) {
38 sendValueToTest(error);
40 function() {
41 sendValueToTest("done");
42 });
46 // Updates a record at the given index with the given text. The indices are
47 // 0-based and are ordered from oldest record, to newest record.
48 function updateRecord(index, text) {
49 var didUpdate = false;
50 findId(index, function(rowId) {
51 g_db.transaction(
52 function(tx) {
53 tx.executeSql(
54 "UPDATE table1 SET data=? WHERE ROWID=?",
55 [text, rowId],
56 function(tx, result) {
57 if (result.rowsAffected == 1)
58 didUpdate = true;
59 else if (result.rowsAffected == 0)
60 sendValueToTest("could not update index: " + index);
61 else
62 sendValueToTest("multiple rows with index: " + index);
63 });
65 function(error) {
66 sendValueToTest("update error: " + error);
68 function() {
69 if (didUpdate)
70 sendValueToTest("done");
71 });
72 });
75 // Deletes a record at the given index.
76 function deleteRecord(index) {
77 findId(index, function(rowId) {
78 g_db.transaction(
79 function(tx) {
80 tx.executeSql("DELETE FROM table1 WHERE ROWID=?", [rowId]);
82 function(error) {
83 sendValueToTest("delete error: " + error);
85 function() {
86 sendValueToTest("done");
87 });
88 });
91 // Gets all the records in the database, ordered by their age.
92 function getRecords() {
93 g_db.readTransaction(function(tx) {
94 tx.executeSql(
95 "SELECT data FROM table1 ORDER BY ROWID",
96 [],
97 function(tx, result) {
98 items = "";
99 for (var i = 0; i < result.rows.length; i++) {
100 if (items != "")
101 items += ", ";
102 items += result.rows.item(i).data;
104 sendValueToTest(items);
106 function(tx, error) {
107 sendValueToTest("getRecords error: " + error);
112 // Helper function that finds the ID for a record based on a given index.
113 function findId(index, callback) {
114 g_db.readTransaction(function(tx) {
115 // |ROWID| is a special sqlite column. It is unique and is incremented
116 // automatically when a new record is created.
117 // |LIMIT| is a nonstandard clause supported by sqlite that lets us pick
118 // rows from the database by index. E.g., LIMIT 2,10 will give us 10 records
119 // starting at offset 2.
120 tx.executeSql(
121 "SELECT ROWID AS id FROM table1 ORDER BY ROWID LIMIT ?,1",
122 [index],
123 function(tx, result) {
124 if (result.rows.length >= 1)
125 callback(result.rows.item(0).id);
126 else
127 sendValueToTest("could not find row with index: " + index);
129 function(tx, error) {
130 sendValueToTest("findId error: " + error);
135 function sendValueToTest(value) {
136 //alert(value);
137 window.domAutomationController.setAutomationId(0);
138 window.domAutomationController.send(value);
141 </script>
143 <body>
144 This page is used for testing Web SQL databases.
145 </body>
146 </html>