1 // 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.
7 #include "chrome/browser/history/android/visit_sql_handler.h"
9 #include "base/files/file_path.h"
10 #include "base/files/scoped_temp_dir.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/history/android/urls_sql_handler.h"
14 #include "chrome/browser/history/history_database.h"
15 #include "chrome/common/chrome_constants.h"
16 #include "testing/gtest/include/gtest/gtest.h"
19 using base::TimeDelta;
23 class VisitSQLHandlerTest : public testing::Test {
26 : urls_sql_handler_(&history_db_),
27 visit_sql_handler_(&history_db_) {
29 virtual ~VisitSQLHandlerTest() {}
32 virtual void SetUp() {
33 // Get a temporary directory for the test DB files.
34 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
35 base::FilePath history_db_name = temp_dir_.path().AppendASCII(
36 chrome::kHistoryFilename);
37 ASSERT_EQ(sql::INIT_OK, history_db_.Init(history_db_name));
40 virtual void TearDown() {
43 HistoryDatabase history_db_;
44 base::ScopedTempDir temp_dir_;
45 UrlsSQLHandler urls_sql_handler_;
46 VisitSQLHandler visit_sql_handler_;
49 DISALLOW_COPY_AND_ASSIGN(VisitSQLHandlerTest);
52 // Insert a url with only url set to verify no visit was inserted in visit
54 TEST_F(VisitSQLHandlerTest, InsertURL) {
55 HistoryAndBookmarkRow row;
56 row.set_raw_url("http://google.com");
57 row.set_url(GURL("http://google.com"));
59 ASSERT_TRUE(urls_sql_handler_.Insert(&row));
60 ASSERT_TRUE(visit_sql_handler_.Insert(&row));
63 ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
65 // Noting should be inserted to visit table.
67 ASSERT_TRUE(history_db_.GetVisitsForURL(row.url_id(), &visits));
68 EXPECT_EQ(0u, visits.size());
71 // Insert a url with last visit time set to verify a visit was inserted.
72 TEST_F(VisitSQLHandlerTest, InsertURLWithLastVisitTime) {
73 HistoryAndBookmarkRow row;
74 row.set_raw_url("http://google.com");
75 row.set_url(GURL("http://google.com"));
76 row.set_last_visit_time(Time::Now());
78 ASSERT_TRUE(urls_sql_handler_.Insert(&row));
79 ASSERT_TRUE(visit_sql_handler_.Insert(&row));
82 ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
85 ASSERT_TRUE(history_db_.GetVisitsForURL(row.url_id(), &visits));
86 EXPECT_EQ(1u, visits.size());
87 EXPECT_EQ(row.last_visit_time(), visits[0].visit_time);
90 // Insert a urls with created time to verify the a visit was inserted.
91 TEST_F(VisitSQLHandlerTest, InsertURLWithCreatedTime) {
92 HistoryAndBookmarkRow row;
93 row.set_raw_url("http://google.com");
94 row.set_url(GURL("http://google.com"));
95 row.set_title(UTF8ToUTF16("Google"));
96 row.set_created(Time::Now());
98 ASSERT_TRUE(urls_sql_handler_.Insert(&row));
99 ASSERT_TRUE(visit_sql_handler_.Insert(&row));
102 ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
105 ASSERT_TRUE(history_db_.GetVisitsForURL(row.url_id(), &visits));
106 EXPECT_EQ(1u, visits.size());
107 EXPECT_EQ(row.created(), visits[0].visit_time);
110 // Insert a URL with visit count as 1 to verify a visit was inserted.
111 TEST_F(VisitSQLHandlerTest, InsertURLWithVisitCount) {
112 HistoryAndBookmarkRow row;
113 row.set_raw_url("http://google.com");
114 row.set_url(GURL("http://google.com"));
115 row.set_visit_count(1);
117 ASSERT_TRUE(urls_sql_handler_.Insert(&row));
118 ASSERT_TRUE(visit_sql_handler_.Insert(&row));
121 ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
124 ASSERT_TRUE(history_db_.GetVisitsForURL(row.url_id(), &visits));
125 EXPECT_EQ(1u, visits.size());
126 EXPECT_NE(Time(), visits[0].visit_time);
129 // Insert a URL with all values set to verify the visit rows
130 // were inserted correctly.
131 TEST_F(VisitSQLHandlerTest, Insert) {
132 HistoryAndBookmarkRow row;
133 row.set_raw_url("http://google.com");
134 row.set_url(GURL("http://google.com"));
135 row.set_visit_count(10);
136 row.set_last_visit_time(Time::Now());
137 row.set_created(Time::Now() - TimeDelta::FromDays(1));
139 ASSERT_TRUE(urls_sql_handler_.Insert(&row));
140 ASSERT_TRUE(visit_sql_handler_.Insert(&row));
143 ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
146 ASSERT_TRUE(history_db_.GetVisitsForURL(row.url_id(), &visits));
147 // 10 row were inserted.
148 EXPECT_EQ(10u, visits.size());
149 // The earlies one has created time set.
150 EXPECT_EQ(row.created(), visits[0].visit_time);
151 // The latest one has last visit time set.
152 EXPECT_EQ(row.last_visit_time(), visits[9].visit_time);
155 // Test the case that both visit time and visit count updated.
156 TEST_F(VisitSQLHandlerTest, UpdateVisitTimeAndVisitCount) {
157 HistoryAndBookmarkRow row;
158 row.set_raw_url("http://google.com");
159 row.set_url(GURL("http://google.com"));
160 row.set_title(UTF8ToUTF16("Google"));
161 row.set_visit_count(10);
162 row.set_last_visit_time(Time::Now() - TimeDelta::FromDays(10));
164 ASSERT_TRUE(urls_sql_handler_.Insert(&row));
165 ASSERT_TRUE(visit_sql_handler_.Insert(&row));
168 ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
170 HistoryAndBookmarkRow update_row;
171 update_row.set_last_visit_time(Time::Now());
172 update_row.set_visit_count(1);
175 id.url_id = url_row.id();
178 ASSERT_TRUE(urls_sql_handler_.Update(update_row, ids));
179 ASSERT_TRUE(visit_sql_handler_.Update(update_row, ids));
182 ASSERT_TRUE(history_db_.GetVisitsForURL(row.url_id(), &visits));
183 EXPECT_EQ(1u, visits.size());
184 EXPECT_EQ(update_row.last_visit_time(), visits[0].visit_time);
187 // Update visit count to zero to verify the visit rows of this url
189 TEST_F(VisitSQLHandlerTest, UpdateVisitCountZero) {
190 HistoryAndBookmarkRow row;
191 row.set_raw_url("http://google.com");
192 row.set_url(GURL("http://google.com"));
193 row.set_visit_count(10);
194 row.set_last_visit_time(Time::Now() - TimeDelta::FromDays(10));
196 ASSERT_TRUE(urls_sql_handler_.Insert(&row));
197 ASSERT_TRUE(visit_sql_handler_.Insert(&row));
200 ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
202 HistoryAndBookmarkRow update_row;
203 update_row.set_visit_count(0);
205 id.url_id = url_row.id();
208 ASSERT_TRUE(urls_sql_handler_.Update(update_row, ids));
209 ASSERT_TRUE(visit_sql_handler_.Update(update_row, ids));
211 ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
212 EXPECT_EQ(0, url_row.visit_count());
213 // Last visit is reset.
214 EXPECT_EQ(Time(), url_row.last_visit());
216 ASSERT_TRUE(history_db_.GetVisitsForURL(row.url_id(), &visits));
217 EXPECT_EQ(0u, visits.size());
220 // Update both last visit time and created time to verify
221 // that visits row are updated correctly.
222 TEST_F(VisitSQLHandlerTest, UpdateBothTime) {
223 HistoryAndBookmarkRow row;
224 row.set_raw_url("http://google.com");
225 row.set_url(GURL("http://google.com"));
226 row.set_visit_count(10);
227 row.set_last_visit_time(Time::Now() - TimeDelta::FromDays(10));
229 ASSERT_TRUE(urls_sql_handler_.Insert(&row));
230 ASSERT_TRUE(visit_sql_handler_.Insert(&row));
232 ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
234 HistoryAndBookmarkRow update_row;
235 update_row.set_last_visit_time(Time::Now() - TimeDelta::FromDays(9));
236 update_row.set_created(Time::Now() - TimeDelta::FromDays(10));
238 id.url_id = url_row.id();
241 ASSERT_TRUE(urls_sql_handler_.Update(update_row, ids));
242 ASSERT_TRUE(visit_sql_handler_.Update(update_row, ids));
245 ASSERT_TRUE(history_db_.GetVisitsForURL(row.url_id(), &visits));
246 // Though both time are updated, visit count was increase by 1 because of
247 // last visit time's change.
248 EXPECT_EQ(11u, visits.size());
249 EXPECT_EQ(update_row.created(), visits[0].visit_time);
250 EXPECT_EQ(update_row.last_visit_time(), visits[10].visit_time);
253 // Update the visit count to verify the new visits are inserted.
254 TEST_F(VisitSQLHandlerTest, UpdateVisitCountIncreased) {
255 HistoryAndBookmarkRow row;
256 row.set_raw_url("http://google.com");
257 row.set_url(GURL("http://google.com"));
258 row.set_visit_count(10);
259 row.set_last_visit_time(Time::Now() - TimeDelta::FromDays(10));
261 ASSERT_TRUE(urls_sql_handler_.Insert(&row));
263 ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
264 EXPECT_EQ(row.url(), url_row.url());
265 EXPECT_EQ(10, url_row.visit_count());
266 EXPECT_EQ(row.last_visit_time(), url_row.last_visit());
268 HistoryAndBookmarkRow update_row;
269 update_row.set_visit_count(11);
271 id.url_id = url_row.id();
274 ASSERT_TRUE(urls_sql_handler_.Update(update_row, ids));
275 ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
276 EXPECT_EQ(row.url(), url_row.url());
277 EXPECT_EQ(11, url_row.visit_count());
278 EXPECT_LT(row.last_visit_time(), url_row.last_visit());
281 TEST_F(VisitSQLHandlerTest, Delete) {
282 HistoryAndBookmarkRow row;
283 row.set_raw_url("http://google.com");
284 row.set_url(GURL("http://google.com"));
285 row.set_visit_count(10);
286 row.set_last_visit_time(Time::Now() - TimeDelta::FromDays(10));
288 ASSERT_TRUE(urls_sql_handler_.Insert(&row));
290 ASSERT_TRUE(history_db_.GetURLRow(row.url_id(), &url_row));
291 EXPECT_EQ(row.url(), url_row.url());
292 EXPECT_EQ(10, url_row.visit_count());
293 EXPECT_EQ(row.last_visit_time(), url_row.last_visit());
296 id.url_id = url_row.id();
299 ASSERT_TRUE(urls_sql_handler_.Delete(ids));
300 EXPECT_FALSE(history_db_.GetURLRow(row.url_id(), &url_row));
303 } // namespace history