blob: 35160299b629aa6c1cf0b205e9af5e2d60c9a5e7 [file] [log] [blame]
[email protected]81702c82012-08-22 21:44:071// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]10994d132013-06-11 07:16:185#include "base/strings/string_number_conversions.h"
[email protected]74ebfb12013-06-07 20:48:006#include "base/strings/utf_string_conversions.h"
[email protected]81702c82012-08-22 21:44:077#include "content/public/browser/browser_context.h"
8#include "content/public/browser/download_manager.h"
9#include "content/public/browser/notification_service.h"
10#include "content/public/browser/notification_types.h"
11#include "content/public/browser/web_contents.h"
12#include "content/public/test/browser_test_utils.h"
[email protected]6e9def12014-03-27 20:23:2813#include "content/public/test/content_browser_test.h"
14#include "content/public/test/content_browser_test_utils.h"
[email protected]81702c82012-08-22 21:44:0715#include "content/public/test/test_utils.h"
[email protected]de7d61ff2013-08-20 11:30:4116#include "content/shell/browser/shell.h"
[email protected]81702c82012-08-22 21:44:0717#include "testing/gtest/include/gtest/gtest.h"
18
19namespace content {
20
21class DatabaseTest : public ContentBrowserTest {
22 public:
23 DatabaseTest() {}
24
25 void RunScriptAndCheckResult(Shell* shell,
26 const std::string& script,
27 const std::string& result) {
28 std::string data;
nickadef4a52016-06-09 18:45:5429 ASSERT_TRUE(ExecuteScriptAndExtractString(shell, script, &data));
[email protected]81702c82012-08-22 21:44:0730 ASSERT_EQ(data, result);
31 }
32
33 void Navigate(Shell* shell) {
34 NavigateToURL(shell, GetTestUrl("", "simple_database.html"));
35 }
36
37 void CreateTable(Shell* shell) {
38 RunScriptAndCheckResult(shell, "createTable()", "done");
39 }
40
41 void InsertRecord(Shell* shell, const std::string& data) {
42 RunScriptAndCheckResult(shell, "insertRecord('" + data + "')", "done");
43 }
44
45 void UpdateRecord(Shell* shell, int index, const std::string& data) {
46 RunScriptAndCheckResult(
[email protected]06bc5d92013-01-02 22:44:1347 shell,
48 "updateRecord(" + base::IntToString(index) + ", '" + data + "')",
49 "done");
[email protected]81702c82012-08-22 21:44:0750 }
51
52 void DeleteRecord(Shell* shell, int index) {
53 RunScriptAndCheckResult(
[email protected]06bc5d92013-01-02 22:44:1354 shell, "deleteRecord(" + base::IntToString(index) + ")", "done");
[email protected]81702c82012-08-22 21:44:0755 }
56
57 void CompareRecords(Shell* shell, const std::string& expected) {
58 RunScriptAndCheckResult(shell, "getRecords()", expected);
59 }
60
61 bool HasTable(Shell* shell) {
62 std::string data;
nickadef4a52016-06-09 18:45:5463 CHECK(ExecuteScriptAndExtractString(shell, "getRecords()", &data));
[email protected]81702c82012-08-22 21:44:0764 return data != "getRecords error: [object SQLError]";
65 }
66};
67
68// Insert records to the database.
69IN_PROC_BROWSER_TEST_F(DatabaseTest, InsertRecord) {
70 Navigate(shell());
71 CreateTable(shell());
72 InsertRecord(shell(), "text");
73 CompareRecords(shell(), "text");
74 InsertRecord(shell(), "text2");
75 CompareRecords(shell(), "text, text2");
76}
77
78// Update records in the database.
79IN_PROC_BROWSER_TEST_F(DatabaseTest, UpdateRecord) {
80 Navigate(shell());
81 CreateTable(shell());
82 InsertRecord(shell(), "text");
83 UpdateRecord(shell(), 0, "0");
84 CompareRecords(shell(), "0");
85
86 InsertRecord(shell(), "1");
87 InsertRecord(shell(), "2");
88 UpdateRecord(shell(), 1, "1000");
89 CompareRecords(shell(), "0, 1000, 2");
90}
91
92// Delete records in the database.
93IN_PROC_BROWSER_TEST_F(DatabaseTest, DeleteRecord) {
94 Navigate(shell());
95 CreateTable(shell());
96 InsertRecord(shell(), "text");
97 DeleteRecord(shell(), 0);
[email protected]007b3f82013-04-09 08:46:4598 CompareRecords(shell(), std::string());
[email protected]81702c82012-08-22 21:44:0799
100 InsertRecord(shell(), "0");
101 InsertRecord(shell(), "1");
102 InsertRecord(shell(), "2");
103 DeleteRecord(shell(), 1);
104 CompareRecords(shell(), "0, 2");
105}
106
107// Attempts to delete a nonexistent row in the table.
108IN_PROC_BROWSER_TEST_F(DatabaseTest, DeleteNonexistentRow) {
109 Navigate(shell());
110 CreateTable(shell());
111 InsertRecord(shell(), "text");
112
113 RunScriptAndCheckResult(
114 shell(), "deleteRecord(1)", "could not find row with index: 1");
115
116 CompareRecords(shell(), "text");
117}
118
119// Insert, update, and delete records in the database.
120IN_PROC_BROWSER_TEST_F(DatabaseTest, DatabaseOperations) {
121 Navigate(shell());
122 CreateTable(shell());
123
124 std::string expected;
125 for (int i = 0; i < 10; ++i) {
126 std::string item = base::IntToString(i);
127 InsertRecord(shell(), item);
128 if (!expected.empty())
129 expected += ", ";
130 expected += item;
131 }
132 CompareRecords(shell(), expected);
133
134 expected.clear();
135 for (int i = 0; i < 10; ++i) {
136 std::string item = base::IntToString(i * i);
137 UpdateRecord(shell(), i, item);
138 if (!expected.empty())
139 expected += ", ";
140 expected += item;
141 }
142 CompareRecords(shell(), expected);
143
144 for (int i = 0; i < 10; ++i)
145 DeleteRecord(shell(), 0);
146
[email protected]007b3f82013-04-09 08:46:45147 CompareRecords(shell(), std::string());
[email protected]81702c82012-08-22 21:44:07148
149 RunScriptAndCheckResult(
150 shell(), "deleteRecord(1)", "could not find row with index: 1");
151
[email protected]007b3f82013-04-09 08:46:45152 CompareRecords(shell(), std::string());
[email protected]81702c82012-08-22 21:44:07153}
154
155// Create records in the database and verify they persist after reload.
156IN_PROC_BROWSER_TEST_F(DatabaseTest, ReloadPage) {
157 Navigate(shell());
158 CreateTable(shell());
159 InsertRecord(shell(), "text");
160
161 WindowedNotificationObserver load_stop_observer(
162 NOTIFICATION_LOAD_STOP,
163 NotificationService::AllSources());
164 shell()->Reload();
165 load_stop_observer.Wait();
166
167 CompareRecords(shell(), "text");
168}
169
170// Attempt to read a database created in a regular browser from an off the
171// record browser.
172IN_PROC_BROWSER_TEST_F(DatabaseTest, OffTheRecordCannotReadRegularDatabase) {
173 Navigate(shell());
174 CreateTable(shell());
175 InsertRecord(shell(), "text");
176
177 Shell* otr = CreateOffTheRecordBrowser();
178 Navigate(otr);
179 ASSERT_FALSE(HasTable(otr));
180
181 CreateTable(otr);
[email protected]007b3f82013-04-09 08:46:45182 CompareRecords(otr, std::string());
[email protected]81702c82012-08-22 21:44:07183}
184
185// Attempt to read a database created in an off the record browser from a
186// regular browser.
187IN_PROC_BROWSER_TEST_F(DatabaseTest, RegularCannotReadOffTheRecordDatabase) {
188 Shell* otr = CreateOffTheRecordBrowser();
189 Navigate(otr);
190 CreateTable(otr);
191 InsertRecord(otr, "text");
192
193 Navigate(shell());
194 ASSERT_FALSE(HasTable(shell()));
195 CreateTable(shell());
[email protected]007b3f82013-04-09 08:46:45196 CompareRecords(shell(), std::string());
[email protected]81702c82012-08-22 21:44:07197}
198
199// Verify DB changes within first window are present in the second window.
200IN_PROC_BROWSER_TEST_F(DatabaseTest, ModificationPersistInSecondTab) {
201 Navigate(shell());
202 CreateTable(shell());
203 InsertRecord(shell(), "text");
204
205 Shell* shell2 = CreateBrowser();
206 Navigate(shell2);
207 UpdateRecord(shell2, 0, "0");
208
209 CompareRecords(shell(), "0");
210 CompareRecords(shell2, "0");
211}
212
213// Verify database modifications persist after restarting browser.
214IN_PROC_BROWSER_TEST_F(DatabaseTest, PRE_DatabasePersistsAfterRelaunch) {
215 Navigate(shell());
216 CreateTable(shell());
217 InsertRecord(shell(), "text");
218}
219
220IN_PROC_BROWSER_TEST_F(DatabaseTest, DatabasePersistsAfterRelaunch) {
221 Navigate(shell());
222 CompareRecords(shell(), "text");
223}
224
225// Verify OTR database is removed after OTR window closes.
226IN_PROC_BROWSER_TEST_F(DatabaseTest, PRE_OffTheRecordDatabaseNotPersistent) {
227 Shell* otr = CreateOffTheRecordBrowser();
228 Navigate(otr);
229 CreateTable(otr);
230 InsertRecord(otr, "text");
231}
232
233IN_PROC_BROWSER_TEST_F(DatabaseTest, OffTheRecordDatabaseNotPersistent) {
234 Shell* otr = CreateOffTheRecordBrowser();
235 Navigate(otr);
236 ASSERT_FALSE(HasTable(otr));
237}
238
239// Verify database modifications persist after crashing window.
240IN_PROC_BROWSER_TEST_F(DatabaseTest, ModificationsPersistAfterRendererCrash) {
241 Navigate(shell());
242 CreateTable(shell());
243 InsertRecord(shell(), "1");
244
245 CrashTab(shell()->web_contents());
246 Navigate(shell());
247 CompareRecords(shell(), "1");
248}
249
250// Test to check if database modifications are persistent across windows in
251// off the record window.
252IN_PROC_BROWSER_TEST_F(DatabaseTest, OffTheRecordDBPersistentAcrossWindows) {
253 Shell* otr1 = CreateOffTheRecordBrowser();
254 Navigate(otr1);
255 CreateTable(otr1);
256 InsertRecord(otr1, "text");
257
258 Shell* otr2 = CreateOffTheRecordBrowser();
259 Navigate(otr2);
260 CompareRecords(otr2, "text");
261}
262
263} // namespace content