1 // Copyright (c) 2011 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.
8 #include "base/file_util.h"
9 #include "base/path_service.h"
10 #include "base/string_number_conversions.h"
11 #include "base/time.h"
12 #include "base/utf_string_conversions.h"
13 #include "chrome/common/chrome_paths.h"
14 #include "chrome/browser/search_engines/template_url.h"
15 #include "chrome/browser/webdata/keyword_table.h"
16 #include "chrome/browser/webdata/web_database.h"
17 #include "sql/statement.h"
18 #include "testing/gtest/include/gtest/gtest.h"
21 using base::TimeDelta
;
23 class KeywordTableTest
: public testing::Test
{
26 virtual ~KeywordTableTest() {}
29 virtual void SetUp() {
30 PathService::Get(chrome::DIR_TEST_DATA
, &file_
);
31 const std::string test_db
= "TestWebDatabase" +
32 base::Int64ToString(Time::Now().ToTimeT()) +
34 file_
= file_
.AppendASCII(test_db
);
35 file_util::Delete(file_
, false);
38 virtual void TearDown() {
39 file_util::Delete(file_
, false);
42 static int64
GetID(const TemplateURL
* url
) {
46 static void SetID(int64 new_id
, TemplateURL
* url
) {
50 static void set_prepopulate_id(TemplateURL
* url
, int id
) {
51 url
->SetPrepopulateId(id
);
54 static void set_logo_id(TemplateURL
* url
, int id
) {
61 DISALLOW_COPY_AND_ASSIGN(KeywordTableTest
);
65 TEST_F(KeywordTableTest
, Keywords
) {
68 ASSERT_EQ(sql::INIT_OK
, db
.Init(file_
));
70 TemplateURL template_url
;
71 template_url
.set_short_name(ASCIIToUTF16("short_name"));
72 template_url
.set_keyword(ASCIIToUTF16("keyword"));
73 GURL
favicon_url("http://favicon.url/");
74 GURL
originating_url("http://google.com/");
75 template_url
.SetFaviconURL(favicon_url
);
76 template_url
.SetURL("http://url/", 0, 0);
77 template_url
.set_safe_for_autoreplace(true);
78 Time created_time
= Time::Now();
79 template_url
.set_date_created(created_time
);
80 Time last_modified_time
= created_time
+ TimeDelta::FromSeconds(10);
81 template_url
.set_last_modified(last_modified_time
);
82 template_url
.set_show_in_default_list(true);
83 template_url
.set_originating_url(originating_url
);
84 template_url
.set_usage_count(32);
85 template_url
.add_input_encoding("UTF-8");
86 template_url
.add_input_encoding("UTF-16");
87 set_prepopulate_id(&template_url
, 10);
88 set_logo_id(&template_url
, 1000);
89 template_url
.set_created_by_policy(true);
90 template_url
.SetInstantURL("http://instant/", 0, 0);
91 SetID(1, &template_url
);
93 EXPECT_TRUE(db
.GetKeywordTable()->AddKeyword(template_url
));
95 std::vector
<TemplateURL
*> template_urls
;
96 EXPECT_TRUE(db
.GetKeywordTable()->GetKeywords(&template_urls
));
98 EXPECT_EQ(1U, template_urls
.size());
99 const TemplateURL
* restored_url
= template_urls
.front();
101 EXPECT_EQ(template_url
.short_name(), restored_url
->short_name());
103 EXPECT_EQ(template_url
.keyword(), restored_url
->keyword());
105 EXPECT_FALSE(restored_url
->autogenerate_keyword());
107 EXPECT_TRUE(favicon_url
== restored_url
->GetFaviconURL());
109 EXPECT_TRUE(restored_url
->safe_for_autoreplace());
111 // The database stores time only at the resolution of a second.
112 EXPECT_EQ(created_time
.ToTimeT(), restored_url
->date_created().ToTimeT());
114 EXPECT_EQ(last_modified_time
.ToTimeT(),
115 restored_url
->last_modified().ToTimeT());
117 EXPECT_TRUE(restored_url
->show_in_default_list());
119 EXPECT_EQ(GetID(&template_url
), GetID(restored_url
));
121 EXPECT_TRUE(originating_url
== restored_url
->originating_url());
123 EXPECT_EQ(32, restored_url
->usage_count());
125 ASSERT_EQ(2U, restored_url
->input_encodings().size());
126 EXPECT_EQ("UTF-8", restored_url
->input_encodings()[0]);
127 EXPECT_EQ("UTF-16", restored_url
->input_encodings()[1]);
129 EXPECT_EQ(10, restored_url
->prepopulate_id());
131 EXPECT_EQ(1000, restored_url
->logo_id());
133 EXPECT_TRUE(restored_url
->created_by_policy());
135 ASSERT_TRUE(restored_url
->instant_url());
136 EXPECT_EQ("http://instant/", restored_url
->instant_url()->url());
138 EXPECT_TRUE(db
.GetKeywordTable()->RemoveKeyword(restored_url
->id()));
140 template_urls
.clear();
141 EXPECT_TRUE(db
.GetKeywordTable()->GetKeywords(&template_urls
));
143 EXPECT_EQ(0U, template_urls
.size());
148 TEST_F(KeywordTableTest
, KeywordMisc
) {
151 ASSERT_EQ(sql::INIT_OK
, db
.Init(file_
));
153 ASSERT_EQ(0, db
.GetKeywordTable()->GetDefaultSearchProviderID());
154 ASSERT_EQ(0, db
.GetKeywordTable()->GetBuiltinKeywordVersion());
156 TemplateURL template_url
;
157 template_url
.set_short_name(ASCIIToUTF16("short_name"));
158 template_url
.set_keyword(ASCIIToUTF16("keyword"));
159 GURL
favicon_url("http://favicon.url/");
160 GURL
originating_url("http://google.com/");
161 template_url
.SetFaviconURL(favicon_url
);
162 template_url
.SetURL("http://url/", 0, 0);
163 template_url
.set_safe_for_autoreplace(true);
164 Time created_time
= Time::Now();
165 template_url
.set_date_created(created_time
);
166 Time last_modified_time
= created_time
+ TimeDelta::FromSeconds(10);
167 template_url
.set_last_modified(last_modified_time
);
168 template_url
.set_show_in_default_list(true);
169 template_url
.set_originating_url(originating_url
);
170 template_url
.set_usage_count(32);
171 template_url
.add_input_encoding("UTF-8");
172 template_url
.add_input_encoding("UTF-16");
173 set_prepopulate_id(&template_url
, 10);
174 set_logo_id(&template_url
, 1000);
175 template_url
.set_created_by_policy(true);
176 template_url
.SetInstantURL("http://instant/", 0, 0);
177 SetID(10, &template_url
);
178 ASSERT_TRUE(db
.GetKeywordTable()->AddKeyword(template_url
));
180 ASSERT_TRUE(db
.GetKeywordTable()->SetDefaultSearchProviderID(10));
181 ASSERT_TRUE(db
.GetKeywordTable()->SetBuiltinKeywordVersion(11));
183 ASSERT_EQ(10, db
.GetKeywordTable()->GetDefaultSearchProviderID());
184 ASSERT_EQ(11, db
.GetKeywordTable()->GetBuiltinKeywordVersion());
187 TEST_F(KeywordTableTest
, DefaultSearchProviderBackup
) {
190 ASSERT_EQ(sql::INIT_OK
, db
.Init(file_
));
192 EXPECT_EQ(0, db
.GetKeywordTable()->GetDefaultSearchProviderID());
194 TemplateURL template_url
;
195 template_url
.set_short_name(ASCIIToUTF16("short_name"));
196 template_url
.set_keyword(ASCIIToUTF16("keyword"));
197 GURL
favicon_url("http://favicon.url/");
198 template_url
.SetFaviconURL(favicon_url
);
199 template_url
.SetURL("http://url/", 0, 0);
200 template_url
.set_safe_for_autoreplace(true);
201 template_url
.set_show_in_default_list(true);
202 template_url
.SetSuggestionsURL("url2", 0, 0);
203 SetID(1, &template_url
);
205 EXPECT_TRUE(db
.GetKeywordTable()->AddKeyword(template_url
));
207 ASSERT_TRUE(db
.GetKeywordTable()->SetDefaultSearchProviderID(1));
208 EXPECT_TRUE(db
.GetKeywordTable()->IsBackupSignatureValid());
209 EXPECT_EQ(1, db
.GetKeywordTable()->GetDefaultSearchProviderID());
211 scoped_ptr
<TemplateURL
> backup_url(
212 db
.GetKeywordTable()->GetDefaultSearchProviderBackup());
213 EXPECT_EQ(1, backup_url
->id());
214 EXPECT_EQ(ASCIIToUTF16("short_name"), backup_url
->short_name());
215 EXPECT_EQ(ASCIIToUTF16("keyword"), backup_url
->keyword());
216 EXPECT_TRUE(favicon_url
== backup_url
->GetFaviconURL());
217 EXPECT_EQ("http://url/", backup_url
->url()->url());
218 EXPECT_TRUE(backup_url
->safe_for_autoreplace());
219 EXPECT_TRUE(backup_url
->show_in_default_list());
220 EXPECT_EQ("url2", backup_url
->suggestions_url()->url());
221 EXPECT_FALSE(db
.GetKeywordTable()->DidDefaultSearchProviderChange());
223 // Change the actual setting.
224 ASSERT_TRUE(db
.GetKeywordTable()->meta_table_
->SetValue(
225 "Default Search Provider ID", 2));
226 EXPECT_TRUE(db
.GetKeywordTable()->IsBackupSignatureValid());
227 EXPECT_EQ(2, db
.GetKeywordTable()->GetDefaultSearchProviderID());
229 backup_url
.reset(db
.GetKeywordTable()->GetDefaultSearchProviderBackup());
230 EXPECT_EQ(1, backup_url
->id());
231 EXPECT_EQ(ASCIIToUTF16("short_name"), backup_url
->short_name());
232 EXPECT_EQ(ASCIIToUTF16("keyword"), backup_url
->keyword());
233 EXPECT_TRUE(favicon_url
== backup_url
->GetFaviconURL());
234 EXPECT_EQ("http://url/", backup_url
->url()->url());
235 EXPECT_TRUE(backup_url
->safe_for_autoreplace());
236 EXPECT_TRUE(backup_url
->show_in_default_list());
237 EXPECT_EQ("url2", backup_url
->suggestions_url()->url());
238 EXPECT_TRUE(db
.GetKeywordTable()->DidDefaultSearchProviderChange());
240 // Change the backup.
241 ASSERT_TRUE(db
.GetKeywordTable()->meta_table_
->SetValue(
242 "Default Search Provider ID", 1));
243 ASSERT_TRUE(db
.GetKeywordTable()->meta_table_
->SetValue(
244 "Default Search Provider ID Backup", 2));
245 EXPECT_FALSE(db
.GetKeywordTable()->IsBackupSignatureValid());
246 EXPECT_EQ(1, db
.GetKeywordTable()->GetDefaultSearchProviderID());
247 EXPECT_EQ(NULL
, db
.GetKeywordTable()->GetDefaultSearchProviderBackup());
248 EXPECT_TRUE(db
.GetKeywordTable()->DidDefaultSearchProviderChange());
250 // Change the signature.
251 ASSERT_TRUE(db
.GetKeywordTable()->meta_table_
->SetValue(
252 "Default Search Provider ID Backup", 1));
253 ASSERT_TRUE(db
.GetKeywordTable()->meta_table_
->SetValue(
254 "Default Search Provider ID Backup Signature", ""));
255 EXPECT_FALSE(db
.GetKeywordTable()->IsBackupSignatureValid());
256 EXPECT_EQ(1, db
.GetKeywordTable()->GetDefaultSearchProviderID());
257 EXPECT_EQ(NULL
, db
.GetKeywordTable()->GetDefaultSearchProviderBackup());
258 EXPECT_TRUE(db
.GetKeywordTable()->DidDefaultSearchProviderChange());
261 ASSERT_TRUE(db
.GetKeywordTable()->UpdateBackupSignature());
262 sql::Statement
remove_keyword(db
.GetKeywordTable()->db_
->GetUniqueStatement(
263 "DELETE FROM keywords WHERE id=1"));
264 ASSERT_TRUE(remove_keyword
.Run());
265 EXPECT_TRUE(db
.GetKeywordTable()->IsBackupSignatureValid());
266 EXPECT_EQ(1, db
.GetKeywordTable()->GetDefaultSearchProviderID());
268 backup_url
.reset(db
.GetKeywordTable()->GetDefaultSearchProviderBackup());
269 EXPECT_EQ(1, backup_url
->id());
270 EXPECT_EQ(ASCIIToUTF16("short_name"), backup_url
->short_name());
271 EXPECT_EQ(ASCIIToUTF16("keyword"), backup_url
->keyword());
272 EXPECT_TRUE(favicon_url
== backup_url
->GetFaviconURL());
273 EXPECT_EQ("http://url/", backup_url
->url()->url());
274 EXPECT_TRUE(backup_url
->safe_for_autoreplace());
275 EXPECT_TRUE(backup_url
->show_in_default_list());
276 EXPECT_EQ("url2", backup_url
->suggestions_url()->url());
277 EXPECT_TRUE(db
.GetKeywordTable()->DidDefaultSearchProviderChange());
279 // Change keywords backup.
280 sql::Statement
remove_keyword_backup(
281 db
.GetKeywordTable()->db_
->GetUniqueStatement(
282 "DELETE FROM keywords_backup WHERE id=1"));
283 ASSERT_TRUE(remove_keyword_backup
.Run());
284 EXPECT_FALSE(db
.GetKeywordTable()->IsBackupSignatureValid());
285 EXPECT_EQ(1, db
.GetKeywordTable()->GetDefaultSearchProviderID());
286 EXPECT_EQ(NULL
, db
.GetKeywordTable()->GetDefaultSearchProviderBackup());
287 EXPECT_TRUE(db
.GetKeywordTable()->DidDefaultSearchProviderChange());
290 TEST_F(KeywordTableTest
, GetTableContents
) {
293 ASSERT_EQ(sql::INIT_OK
, db
.Init(file_
));
295 TemplateURL template_url
;
296 template_url
.set_short_name(ASCIIToUTF16("short_name"));
297 template_url
.set_keyword(ASCIIToUTF16("keyword"));
298 GURL
favicon_url("http://favicon.url/");
299 GURL
originating_url("http://originating.url/");
300 template_url
.SetFaviconURL(favicon_url
);
301 template_url
.SetURL("http://url/", 0, 0);
302 template_url
.set_safe_for_autoreplace(true);
303 template_url
.set_show_in_default_list(true);
304 template_url
.SetSuggestionsURL("url2", 0, 0);
305 template_url
.set_sync_guid("1234-5678-90AB-CDEF");
306 template_url
.set_date_created(base::Time::UnixEpoch());
307 template_url
.set_last_modified(base::Time::UnixEpoch());
308 SetID(1, &template_url
);
310 ASSERT_TRUE(db
.GetKeywordTable()->AddKeyword(template_url
));
312 GURL
originating_url2("http://originating.url/");
313 template_url
.set_originating_url(originating_url2
);
314 template_url
.set_autogenerate_keyword(true);
315 EXPECT_EQ(ASCIIToUTF16("url"), template_url
.keyword());
316 template_url
.add_input_encoding("Shift_JIS");
317 set_prepopulate_id(&template_url
, 5);
318 set_logo_id(&template_url
, 2000);
319 template_url
.SetInstantURL("http://instant2/", 0, 0);
320 SetID(2, &template_url
);
321 template_url
.set_sync_guid("FEDC-BA09-8765-4321");
322 ASSERT_TRUE(db
.GetKeywordTable()->AddKeyword(template_url
));
324 const char kTestContents
[] =
325 "1short_namekeywordhttp://favicon.url/http://url/1001"
327 "1234-5678-90AB-CDEF"
328 "2short_nameurlhttp://favicon.url/http://url/1http://originating.url/"
329 "00Shift_JIS1url25120000http://instant2/0"
330 "FEDC-BA09-8765-4321";
332 std::string contents
;
333 ASSERT_TRUE(db
.GetKeywordTable()->GetTableContents("keywords", &contents
));
334 EXPECT_EQ(kTestContents
, contents
);
336 ASSERT_TRUE(db
.GetKeywordTable()->GetTableContents("keywords_backup",
338 EXPECT_EQ(kTestContents
, contents
);
341 TEST_F(KeywordTableTest
, GetTableContentsOrdering
) {
344 ASSERT_EQ(sql::INIT_OK
, db
.Init(file_
));
346 TemplateURL template_url
;
347 template_url
.set_short_name(ASCIIToUTF16("short_name"));
348 template_url
.set_keyword(ASCIIToUTF16("keyword"));
349 GURL
favicon_url("http://favicon.url/");
350 GURL
originating_url("http://originating.url/");
351 template_url
.SetFaviconURL(favicon_url
);
352 template_url
.SetURL("http://url/", 0, 0);
353 template_url
.set_safe_for_autoreplace(true);
354 template_url
.set_show_in_default_list(true);
355 template_url
.SetSuggestionsURL("url2", 0, 0);
356 template_url
.set_sync_guid("1234-5678-90AB-CDEF");
357 template_url
.set_date_created(base::Time::UnixEpoch());
358 template_url
.set_last_modified(base::Time::UnixEpoch());
359 SetID(2, &template_url
);
361 ASSERT_TRUE(db
.GetKeywordTable()->AddKeyword(template_url
));
363 GURL
originating_url2("http://originating.url/");
364 template_url
.set_originating_url(originating_url2
);
365 template_url
.set_autogenerate_keyword(true);
366 EXPECT_EQ(ASCIIToUTF16("url"), template_url
.keyword());
367 template_url
.add_input_encoding("Shift_JIS");
368 set_prepopulate_id(&template_url
, 5);
369 set_logo_id(&template_url
, 2000);
370 template_url
.SetInstantURL("http://instant2/", 0, 0);
371 SetID(1, &template_url
);
372 template_url
.set_sync_guid("FEDC-BA09-8765-4321");
373 ASSERT_TRUE(db
.GetKeywordTable()->AddKeyword(template_url
));
375 const char kTestContents
[] =
376 "1short_nameurlhttp://favicon.url/http://url/1http://originating.url/"
377 "00Shift_JIS1url25120000http://instant2/0"
378 "FEDC-BA09-8765-4321"
379 "2short_namekeywordhttp://favicon.url/http://url/1001"
381 "1234-5678-90AB-CDEF";
383 std::string contents
;
384 ASSERT_TRUE(db
.GetKeywordTable()->GetTableContents("keywords", &contents
));
385 EXPECT_EQ(kTestContents
, contents
);
387 ASSERT_TRUE(db
.GetKeywordTable()->GetTableContents("keywords_backup",
389 EXPECT_EQ(kTestContents
, contents
);
392 TEST_F(KeywordTableTest
, UpdateKeyword
) {
395 ASSERT_EQ(sql::INIT_OK
, db
.Init(file_
));
397 TemplateURL template_url
;
398 template_url
.set_short_name(ASCIIToUTF16("short_name"));
399 template_url
.set_keyword(ASCIIToUTF16("keyword"));
400 GURL
favicon_url("http://favicon.url/");
401 GURL
originating_url("http://originating.url/");
402 template_url
.SetFaviconURL(favicon_url
);
403 template_url
.SetURL("http://url/", 0, 0);
404 template_url
.set_safe_for_autoreplace(true);
405 template_url
.set_show_in_default_list(true);
406 template_url
.SetSuggestionsURL("url2", 0, 0);
407 SetID(1, &template_url
);
409 EXPECT_TRUE(db
.GetKeywordTable()->AddKeyword(template_url
));
411 GURL
originating_url2("http://originating.url/");
412 template_url
.set_originating_url(originating_url2
);
413 template_url
.set_autogenerate_keyword(true);
414 EXPECT_EQ(ASCIIToUTF16("url"), template_url
.keyword());
415 template_url
.add_input_encoding("Shift_JIS");
416 set_prepopulate_id(&template_url
, 5);
417 set_logo_id(&template_url
, 2000);
418 template_url
.SetInstantURL("http://instant2/", 0, 0);
419 EXPECT_TRUE(db
.GetKeywordTable()->UpdateKeyword(template_url
));
421 std::vector
<TemplateURL
*> template_urls
;
422 EXPECT_TRUE(db
.GetKeywordTable()->GetKeywords(&template_urls
));
424 EXPECT_EQ(1U, template_urls
.size());
425 const TemplateURL
* restored_url
= template_urls
.front();
427 EXPECT_EQ(template_url
.short_name(), restored_url
->short_name());
429 EXPECT_EQ(template_url
.keyword(), restored_url
->keyword());
431 EXPECT_TRUE(restored_url
->autogenerate_keyword());
433 EXPECT_TRUE(favicon_url
== restored_url
->GetFaviconURL());
435 EXPECT_TRUE(restored_url
->safe_for_autoreplace());
437 EXPECT_TRUE(restored_url
->show_in_default_list());
439 EXPECT_EQ(GetID(&template_url
), GetID(restored_url
));
441 EXPECT_TRUE(originating_url2
== restored_url
->originating_url());
443 ASSERT_EQ(1U, restored_url
->input_encodings().size());
444 ASSERT_EQ("Shift_JIS", restored_url
->input_encodings()[0]);
446 EXPECT_EQ(template_url
.suggestions_url()->url(),
447 restored_url
->suggestions_url()->url());
449 EXPECT_EQ(template_url
.id(), restored_url
->id());
451 EXPECT_EQ(template_url
.prepopulate_id(), restored_url
->prepopulate_id());
453 EXPECT_EQ(template_url
.logo_id(), restored_url
->logo_id());
455 EXPECT_TRUE(restored_url
->instant_url());
456 EXPECT_EQ(template_url
.instant_url()->url(),
457 restored_url
->instant_url()->url());
462 TEST_F(KeywordTableTest
, KeywordWithNoFavicon
) {
465 ASSERT_EQ(sql::INIT_OK
, db
.Init(file_
));
467 TemplateURL template_url
;
468 template_url
.set_short_name(ASCIIToUTF16("short_name"));
469 template_url
.set_keyword(ASCIIToUTF16("keyword"));
470 template_url
.SetURL("http://url/", 0, 0);
471 template_url
.set_safe_for_autoreplace(true);
472 SetID(-100, &template_url
);
474 EXPECT_TRUE(db
.GetKeywordTable()->AddKeyword(template_url
));
476 std::vector
<TemplateURL
*> template_urls
;
477 EXPECT_TRUE(db
.GetKeywordTable()->GetKeywords(&template_urls
));
478 EXPECT_EQ(1U, template_urls
.size());
479 const TemplateURL
* restored_url
= template_urls
.front();
481 EXPECT_EQ(template_url
.short_name(), restored_url
->short_name());
482 EXPECT_EQ(template_url
.keyword(), restored_url
->keyword());
483 EXPECT_TRUE(!restored_url
->GetFaviconURL().is_valid());
484 EXPECT_TRUE(restored_url
->safe_for_autoreplace());
485 EXPECT_EQ(GetID(&template_url
), GetID(restored_url
));