Update app list background color.
[chromium-blink-merge.git] / components / search_engines / template_url_service_unittest.cc
blob368496cc7fc22499981af99c439a6504282efccc
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.
5 #include "components/search_engines/template_url_service.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/callback.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/run_loop.h"
14 #include "base/strings/string_split.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "base/task/cancelable_task_tracker.h"
18 #include "base/test/simple_test_clock.h"
19 #include "base/threading/thread.h"
20 #include "base/time/time.h"
21 #include "chrome/browser/history/history_service.h"
22 #include "chrome/browser/history/history_service_factory.h"
23 #include "chrome/browser/search_engines/template_url_service_test_util.h"
24 #include "chrome/test/base/testing_profile.h"
25 #include "components/search_engines/keyword_web_data_service.h"
26 #include "components/search_engines/search_host_to_urls_map.h"
27 #include "components/search_engines/search_terms_data.h"
28 #include "components/search_engines/template_url.h"
29 #include "components/search_engines/template_url_prepopulate_data.h"
30 #include "content/public/test/test_browser_thread_bundle.h"
31 #include "testing/gtest/include/gtest/gtest.h"
33 using base::ASCIIToUTF16;
34 using base::Time;
35 using base::TimeDelta;
37 namespace {
39 // QueryHistoryCallbackImpl ---------------------------------------------------
41 struct QueryHistoryCallbackImpl {
42 QueryHistoryCallbackImpl() : success(false) {}
44 void Callback(bool success,
45 const history::URLRow& row,
46 const history::VisitVector& visits) {
47 this->success = success;
48 if (success) {
49 this->row = row;
50 this->visits = visits;
54 bool success;
55 history::URLRow row;
56 history::VisitVector visits;
59 TemplateURL* CreateKeywordWithDate(
60 TemplateURLService* model,
61 const std::string& short_name,
62 const std::string& keyword,
63 const std::string& url,
64 const std::string& suggest_url,
65 const std::string& alternate_url,
66 const std::string& favicon_url,
67 bool safe_for_autoreplace,
68 bool show_in_default_list,
69 const std::string& encodings,
70 Time date_created,
71 Time last_modified) {
72 TemplateURLData data;
73 data.short_name = base::UTF8ToUTF16(short_name);
74 data.SetKeyword(base::UTF8ToUTF16(keyword));
75 data.SetURL(url);
76 data.suggestions_url = suggest_url;
77 if (!alternate_url.empty())
78 data.alternate_urls.push_back(alternate_url);
79 data.favicon_url = GURL(favicon_url);
80 data.safe_for_autoreplace = safe_for_autoreplace;
81 data.show_in_default_list = show_in_default_list;
82 base::SplitString(encodings, ';', &data.input_encodings);
83 data.date_created = date_created;
84 data.last_modified = last_modified;
85 return new TemplateURL(data);
88 TemplateURL* AddKeywordWithDate(
89 TemplateURLService* model,
90 const std::string& short_name,
91 const std::string& keyword,
92 const std::string& url,
93 const std::string& suggest_url,
94 const std::string& alternate_url,
95 const std::string& favicon_url,
96 bool safe_for_autoreplace,
97 const std::string& encodings,
98 Time date_created,
99 Time last_modified) {
100 TemplateURL* t_url = CreateKeywordWithDate(
101 model, short_name, keyword, url, suggest_url, alternate_url,favicon_url,
102 safe_for_autoreplace, false, encodings, date_created, last_modified);
103 model->Add(t_url);
104 EXPECT_NE(0, t_url->id());
105 return t_url;
108 // Checks that the two TemplateURLs are similar. It does not check the id, the
109 // date_created or the last_modified time. Neither pointer should be NULL.
110 void ExpectSimilar(const TemplateURL* expected, const TemplateURL* actual) {
111 ASSERT_TRUE(expected != NULL);
112 ASSERT_TRUE(actual != NULL);
113 EXPECT_EQ(expected->short_name(), actual->short_name());
114 EXPECT_EQ(expected->keyword(), actual->keyword());
115 EXPECT_EQ(expected->url(), actual->url());
116 EXPECT_EQ(expected->suggestions_url(), actual->suggestions_url());
117 EXPECT_EQ(expected->favicon_url(), actual->favicon_url());
118 EXPECT_EQ(expected->alternate_urls(), actual->alternate_urls());
119 EXPECT_EQ(expected->show_in_default_list(), actual->show_in_default_list());
120 EXPECT_EQ(expected->safe_for_autoreplace(), actual->safe_for_autoreplace());
121 EXPECT_EQ(expected->input_encodings(), actual->input_encodings());
122 EXPECT_EQ(expected->search_terms_replacement_key(),
123 actual->search_terms_replacement_key());
126 } // namespace
129 // TemplateURLServiceTest -----------------------------------------------------
131 class TemplateURLServiceTest : public testing::Test {
132 public:
133 TemplateURLServiceTest();
135 // testing::Test:
136 void SetUp() override;
137 void TearDown() override;
139 TemplateURL* AddKeywordWithDate(const std::string& short_name,
140 const std::string& keyword,
141 const std::string& url,
142 const std::string& suggest_url,
143 const std::string& alternate_url,
144 const std::string& favicon_url,
145 bool safe_for_autoreplace,
146 const std::string& encodings,
147 Time date_created,
148 Time last_modified);
150 // Verifies the two TemplateURLs are equal.
151 void AssertEquals(const TemplateURL& expected, const TemplateURL& actual);
153 // Verifies the two timestamps are equal, within the expected degree of
154 // precision.
155 void AssertTimesEqual(const base::Time& expected, const base::Time& actual);
157 // Create an URL that appears to have been prepopulated, but won't be in the
158 // current data. The caller owns the returned TemplateURL*.
159 TemplateURL* CreatePreloadedTemplateURL(bool safe_for_autoreplace,
160 int prepopulate_id);
162 // Helper methods to make calling TemplateURLServiceTestUtil methods less
163 // visually noisy in the test code.
164 void VerifyObserverCount(int expected_changed_count);
165 void VerifyObserverFired();
166 TemplateURLServiceTestUtil* test_util() { return test_util_.get(); }
167 TemplateURLService* model() { return test_util_->model(); }
168 const SearchTermsData& search_terms_data() {
169 return model()->search_terms_data();
172 private:
173 content::TestBrowserThreadBundle thread_bundle_; // To set up BrowserThreads.
174 scoped_ptr<TemplateURLServiceTestUtil> test_util_;
176 DISALLOW_COPY_AND_ASSIGN(TemplateURLServiceTest);
179 class TemplateURLServiceWithoutFallbackTest : public TemplateURLServiceTest {
180 public:
181 TemplateURLServiceWithoutFallbackTest() : TemplateURLServiceTest() {}
183 void SetUp() override {
184 DefaultSearchManager::SetFallbackSearchEnginesDisabledForTesting(true);
185 TemplateURLServiceTest::SetUp();
188 void TearDown() override {
189 TemplateURLServiceTest::TearDown();
190 DefaultSearchManager::SetFallbackSearchEnginesDisabledForTesting(false);
194 TemplateURLServiceTest::TemplateURLServiceTest() {
197 void TemplateURLServiceTest::SetUp() {
198 test_util_.reset(new TemplateURLServiceTestUtil);
201 void TemplateURLServiceTest::TearDown() {
202 test_util_.reset();
205 TemplateURL* TemplateURLServiceTest::AddKeywordWithDate(
206 const std::string& short_name,
207 const std::string& keyword,
208 const std::string& url,
209 const std::string& suggest_url,
210 const std::string& alternate_url,
211 const std::string& favicon_url,
212 bool safe_for_autoreplace,
213 const std::string& encodings,
214 Time date_created,
215 Time last_modified) {
216 return ::AddKeywordWithDate(model(), short_name, keyword, url, suggest_url,
217 alternate_url, favicon_url, safe_for_autoreplace,
218 encodings, date_created, last_modified);
221 void TemplateURLServiceTest::AssertEquals(const TemplateURL& expected,
222 const TemplateURL& actual) {
223 ASSERT_EQ(expected.short_name(), actual.short_name());
224 ASSERT_EQ(expected.keyword(), actual.keyword());
225 ASSERT_EQ(expected.url(), actual.url());
226 ASSERT_EQ(expected.suggestions_url(), actual.suggestions_url());
227 ASSERT_EQ(expected.favicon_url(), actual.favicon_url());
228 ASSERT_EQ(expected.alternate_urls(), actual.alternate_urls());
229 ASSERT_EQ(expected.show_in_default_list(), actual.show_in_default_list());
230 ASSERT_EQ(expected.safe_for_autoreplace(), actual.safe_for_autoreplace());
231 ASSERT_EQ(expected.input_encodings(), actual.input_encodings());
232 ASSERT_EQ(expected.id(), actual.id());
233 ASSERT_EQ(expected.date_created(), actual.date_created());
234 AssertTimesEqual(expected.last_modified(), actual.last_modified());
235 ASSERT_EQ(expected.sync_guid(), actual.sync_guid());
236 ASSERT_EQ(expected.search_terms_replacement_key(),
237 actual.search_terms_replacement_key());
240 void TemplateURLServiceTest::AssertTimesEqual(const base::Time& expected,
241 const base::Time& actual) {
242 // Because times are stored with a granularity of one second, there is a loss
243 // of precision when serializing and deserializing the timestamps. Hence, only
244 // expect timestamps to be equal to within one second of one another.
245 ASSERT_LT((expected - actual).magnitude(), base::TimeDelta::FromSeconds(1));
248 TemplateURL* TemplateURLServiceTest::CreatePreloadedTemplateURL(
249 bool safe_for_autoreplace,
250 int prepopulate_id) {
251 TemplateURLData data;
252 data.short_name = ASCIIToUTF16("unittest");
253 data.SetKeyword(ASCIIToUTF16("unittest"));
254 data.SetURL("http://www.unittest.com/{searchTerms}");
255 data.favicon_url = GURL("http://favicon.url");
256 data.show_in_default_list = true;
257 data.safe_for_autoreplace = safe_for_autoreplace;
258 data.input_encodings.push_back("UTF-8");
259 data.date_created = Time::FromTimeT(100);
260 data.last_modified = Time::FromTimeT(100);
261 data.prepopulate_id = prepopulate_id;
262 return new TemplateURL(data);
265 void TemplateURLServiceTest::VerifyObserverCount(int expected_changed_count) {
266 EXPECT_EQ(expected_changed_count, test_util_->GetObserverCount());
267 test_util_->ResetObserverCount();
270 void TemplateURLServiceTest::VerifyObserverFired() {
271 EXPECT_LE(1, test_util_->GetObserverCount());
272 test_util_->ResetObserverCount();
276 // Actual tests ---------------------------------------------------------------
278 TEST_F(TemplateURLServiceTest, Load) {
279 test_util()->VerifyLoad();
282 TEST_F(TemplateURLServiceTest, AddUpdateRemove) {
283 // Add a new TemplateURL.
284 test_util()->VerifyLoad();
285 const size_t initial_count = model()->GetTemplateURLs().size();
287 TemplateURLData data;
288 data.short_name = ASCIIToUTF16("google");
289 data.SetKeyword(ASCIIToUTF16("keyword"));
290 data.SetURL("http://www.google.com/foo/bar");
291 data.favicon_url = GURL("http://favicon.url");
292 data.safe_for_autoreplace = true;
293 data.date_created = Time::FromTimeT(100);
294 data.last_modified = Time::FromTimeT(100);
295 data.sync_guid = "00000000-0000-0000-0000-000000000001";
296 TemplateURL* t_url = new TemplateURL(data);
297 model()->Add(t_url);
298 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("keyword"), GURL(),
299 NULL));
300 VerifyObserverCount(1);
301 base::RunLoop().RunUntilIdle();
302 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
303 ASSERT_EQ(t_url, model()->GetTemplateURLForKeyword(t_url->keyword()));
304 // We need to make a second copy as the model takes ownership of |t_url| and
305 // will delete it. We have to do this after calling Add() since that gives
306 // |t_url| its ID.
307 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data()));
309 // Reload the model to verify it was actually saved to the database.
310 test_util()->ResetModel(true);
311 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
312 TemplateURL* loaded_url =
313 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"));
314 ASSERT_TRUE(loaded_url != NULL);
315 AssertEquals(*cloned_url, *loaded_url);
316 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("keyword"), GURL(),
317 NULL));
319 // We expect the last_modified time to be updated to the present time on an
320 // explicit reset.
321 base::Time now = base::Time::Now();
322 scoped_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock);
323 clock->SetNow(now);
324 model()->set_clock(clock.Pass());
326 // Mutate an element and verify it succeeded.
327 model()->ResetTemplateURL(loaded_url, ASCIIToUTF16("a"), ASCIIToUTF16("b"),
328 "c");
329 ASSERT_EQ(ASCIIToUTF16("a"), loaded_url->short_name());
330 ASSERT_EQ(ASCIIToUTF16("b"), loaded_url->keyword());
331 ASSERT_EQ("c", loaded_url->url());
332 ASSERT_FALSE(loaded_url->safe_for_autoreplace());
333 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("keyword"), GURL(),
334 NULL));
335 ASSERT_FALSE(model()->CanReplaceKeyword(ASCIIToUTF16("b"), GURL(), NULL));
336 cloned_url.reset(new TemplateURL(loaded_url->data()));
337 base::RunLoop().RunUntilIdle();
338 test_util()->ResetModel(true);
339 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
340 loaded_url = model()->GetTemplateURLForKeyword(ASCIIToUTF16("b"));
341 ASSERT_TRUE(loaded_url != NULL);
342 AssertEquals(*cloned_url, *loaded_url);
343 // We changed a TemplateURL in the service, so ensure that the time was
344 // updated.
345 AssertTimesEqual(now, loaded_url->last_modified());
347 // Remove an element and verify it succeeded.
348 model()->Remove(loaded_url);
349 VerifyObserverCount(1);
350 test_util()->ResetModel(true);
351 ASSERT_EQ(initial_count, model()->GetTemplateURLs().size());
352 EXPECT_TRUE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("b")) == NULL);
355 TEST_F(TemplateURLServiceTest, AddSameKeyword) {
356 test_util()->VerifyLoad();
358 AddKeywordWithDate(
359 "first", "keyword", "http://test1", std::string(), std::string(),
360 std::string(), true, "UTF-8", Time(), Time());
361 VerifyObserverCount(1);
363 // Test what happens when we try to add a TemplateURL with the same keyword as
364 // one in the model.
365 TemplateURLData data;
366 data.short_name = ASCIIToUTF16("second");
367 data.SetKeyword(ASCIIToUTF16("keyword"));
368 data.SetURL("http://test2");
369 data.safe_for_autoreplace = false;
370 TemplateURL* t_url = new TemplateURL(data);
371 model()->Add(t_url);
373 // Because the old TemplateURL was replaceable and the new one wasn't, the new
374 // one should have replaced the old.
375 VerifyObserverCount(1);
376 EXPECT_EQ(t_url, model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")));
377 EXPECT_EQ(ASCIIToUTF16("second"), t_url->short_name());
378 EXPECT_EQ(ASCIIToUTF16("keyword"), t_url->keyword());
379 EXPECT_FALSE(t_url->safe_for_autoreplace());
381 // Now try adding a replaceable TemplateURL. This should just delete the
382 // passed-in URL.
383 data.short_name = ASCIIToUTF16("third");
384 data.SetURL("http://test3");
385 data.safe_for_autoreplace = true;
386 model()->Add(new TemplateURL(data));
387 VerifyObserverCount(0);
388 EXPECT_EQ(t_url, model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")));
389 EXPECT_EQ(ASCIIToUTF16("second"), t_url->short_name());
390 EXPECT_EQ(ASCIIToUTF16("keyword"), t_url->keyword());
391 EXPECT_FALSE(t_url->safe_for_autoreplace());
393 // Now try adding a non-replaceable TemplateURL again. This should uniquify
394 // the existing entry's keyword.
395 data.short_name = ASCIIToUTF16("fourth");
396 data.SetURL("http://test4");
397 data.safe_for_autoreplace = false;
398 TemplateURL* t_url2 = new TemplateURL(data);
399 model()->Add(t_url2);
400 VerifyObserverCount(1);
401 EXPECT_EQ(t_url2, model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")));
402 EXPECT_EQ(ASCIIToUTF16("fourth"), t_url2->short_name());
403 EXPECT_EQ(ASCIIToUTF16("keyword"), t_url2->keyword());
404 EXPECT_EQ(ASCIIToUTF16("second"), t_url->short_name());
405 EXPECT_EQ(ASCIIToUTF16("test2"), t_url->keyword());
408 TEST_F(TemplateURLServiceTest, AddExtensionKeyword) {
409 test_util()->VerifyLoad();
411 AddKeywordWithDate(
412 "replaceable", "keyword1", "http://test1", std::string(), std::string(),
413 std::string(), true, "UTF-8", Time(), Time());
414 TemplateURL* original2 = AddKeywordWithDate(
415 "nonreplaceable", "keyword2", "http://test2", std::string(),
416 std::string(), std::string(), false, "UTF-8", Time(), Time());
417 model()->RegisterOmniboxKeyword("test3", "extension", "keyword3",
418 "http://test3");
419 TemplateURL* original3 =
420 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword3"));
421 ASSERT_TRUE(original3);
423 // Extension keywords should override replaceable keywords.
424 model()->RegisterOmniboxKeyword("id1", "test", "keyword1", "http://test4");
425 TemplateURL* extension1 = model()->FindTemplateURLForExtension(
426 "id1", TemplateURL::OMNIBOX_API_EXTENSION);
427 EXPECT_TRUE(extension1);
428 EXPECT_EQ(extension1,
429 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword1")));
431 // They should not override non-replaceable keywords.
432 model()->RegisterOmniboxKeyword("id2", "test", "keyword2", "http://test5");
433 TemplateURL* extension2 = model()->FindTemplateURLForExtension(
434 "id2", TemplateURL::OMNIBOX_API_EXTENSION);
435 ASSERT_TRUE(extension2);
436 EXPECT_EQ(original2,
437 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword2")));
439 // They should override extension keywords added earlier.
440 model()->RegisterOmniboxKeyword("id3", "test", "keyword3", "http://test6");
441 TemplateURL* extension3 = model()->FindTemplateURLForExtension(
442 "id3", TemplateURL::OMNIBOX_API_EXTENSION);
443 ASSERT_TRUE(extension3);
444 EXPECT_EQ(extension3,
445 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword3")));
448 TEST_F(TemplateURLServiceTest, AddSameKeywordWithExtensionPresent) {
449 test_util()->VerifyLoad();
451 // Similar to the AddSameKeyword test, but with an extension keyword masking a
452 // replaceable TemplateURL. We should still do correct conflict resolution
453 // between the non-template URLs.
454 model()->RegisterOmniboxKeyword("test2", "extension", "keyword",
455 "http://test2");
456 TemplateURL* extension =
457 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"));
458 ASSERT_TRUE(extension);
459 // Adding a keyword that matches the extension.
460 AddKeywordWithDate(
461 "replaceable", "keyword", "http://test1", std::string(), std::string(),
462 std::string(), true, "UTF-8", Time(), Time());
464 // Adding another replaceable keyword should remove the existing one, but
465 // leave the extension as is.
466 TemplateURLData data;
467 data.short_name = ASCIIToUTF16("name1");
468 data.SetKeyword(ASCIIToUTF16("keyword"));
469 data.SetURL("http://test3");
470 data.safe_for_autoreplace = true;
471 TemplateURL* t_url = new TemplateURL(data);
472 model()->Add(t_url);
473 EXPECT_EQ(extension,
474 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")));
475 EXPECT_EQ(t_url, model()->GetTemplateURLForHost("test3"));
477 // Adding a nonreplaceable keyword should remove the existing replaceable
478 // keyword and replace the extension as the associated URL for this keyword,
479 // but not evict the extension from the service entirely.
480 data.short_name = ASCIIToUTF16("name2");
481 data.SetURL("http://test4");
482 data.safe_for_autoreplace = false;
483 TemplateURL* t_url2 = new TemplateURL(data);
484 model()->Add(t_url2);
485 EXPECT_EQ(t_url2,
486 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")));
489 TEST_F(TemplateURLServiceTest, NotPersistOmniboxExtensionKeyword) {
490 test_util()->VerifyLoad();
492 // Register an omnibox keyword.
493 model()->RegisterOmniboxKeyword("test", "extension", "keyword",
494 "chrome-extension://test");
495 ASSERT_TRUE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")));
497 // Reload the data.
498 test_util()->ResetModel(true);
500 // Ensure the omnibox keyword is not persisted.
501 ASSERT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")));
504 TEST_F(TemplateURLServiceTest, ClearBrowsingData_Keywords) {
505 Time now = Time::Now();
506 TimeDelta one_day = TimeDelta::FromDays(1);
507 Time month_ago = now - TimeDelta::FromDays(30);
509 // Nothing has been added.
510 EXPECT_EQ(0U, model()->GetTemplateURLs().size());
512 // Create one with a 0 time.
513 AddKeywordWithDate("name1", "key1", "http://foo1", "http://suggest1",
514 std::string(), "http://icon1", true, "UTF-8;UTF-16",
515 Time(), Time());
516 // Create one for now and +/- 1 day.
517 AddKeywordWithDate("name2", "key2", "http://foo2", "http://suggest2",
518 std::string(), "http://icon2", true, "UTF-8;UTF-16",
519 now - one_day, Time());
520 AddKeywordWithDate("name3", "key3", "http://foo3", std::string(),
521 std::string(), std::string(), true, std::string(), now,
522 Time());
523 AddKeywordWithDate("name4", "key4", "http://foo4", std::string(),
524 std::string(), std::string(), true, std::string(),
525 now + one_day, Time());
526 // Try the other three states.
527 AddKeywordWithDate("name5", "key5", "http://foo5", "http://suggest5",
528 std::string(), "http://icon5", false, "UTF-8;UTF-16", now,
529 Time());
530 AddKeywordWithDate("name6", "key6", "http://foo6", "http://suggest6",
531 std::string(), "http://icon6", false, "UTF-8;UTF-16",
532 month_ago, Time());
534 // We just added a few items, validate them.
535 EXPECT_EQ(6U, model()->GetTemplateURLs().size());
537 // Try removing from current timestamp. This should delete the one in the
538 // future and one very recent one.
539 model()->RemoveAutoGeneratedSince(now);
540 EXPECT_EQ(4U, model()->GetTemplateURLs().size());
542 // Try removing from two months ago. This should only delete items that are
543 // auto-generated.
544 model()->RemoveAutoGeneratedBetween(now - TimeDelta::FromDays(60), now);
545 EXPECT_EQ(3U, model()->GetTemplateURLs().size());
547 // Make sure the right values remain.
548 EXPECT_EQ(ASCIIToUTF16("key1"), model()->GetTemplateURLs()[0]->keyword());
549 EXPECT_TRUE(model()->GetTemplateURLs()[0]->safe_for_autoreplace());
550 EXPECT_EQ(0U,
551 model()->GetTemplateURLs()[0]->date_created().ToInternalValue());
553 EXPECT_EQ(ASCIIToUTF16("key5"), model()->GetTemplateURLs()[1]->keyword());
554 EXPECT_FALSE(model()->GetTemplateURLs()[1]->safe_for_autoreplace());
555 EXPECT_EQ(now.ToInternalValue(),
556 model()->GetTemplateURLs()[1]->date_created().ToInternalValue());
558 EXPECT_EQ(ASCIIToUTF16("key6"), model()->GetTemplateURLs()[2]->keyword());
559 EXPECT_FALSE(model()->GetTemplateURLs()[2]->safe_for_autoreplace());
560 EXPECT_EQ(month_ago.ToInternalValue(),
561 model()->GetTemplateURLs()[2]->date_created().ToInternalValue());
563 // Try removing from Time=0. This should delete one more.
564 model()->RemoveAutoGeneratedSince(Time());
565 EXPECT_EQ(2U, model()->GetTemplateURLs().size());
568 TEST_F(TemplateURLServiceTest, ClearBrowsingData_KeywordsForOrigin) {
569 Time now = Time::Now();
570 TimeDelta one_day = TimeDelta::FromDays(1);
571 Time month_ago = now - TimeDelta::FromDays(30);
573 // Nothing has been added.
574 EXPECT_EQ(0U, model()->GetTemplateURLs().size());
576 // Create one for now and +/- 1 day.
577 AddKeywordWithDate("name1", "key1", "http://foo1", "http://suggest1",
578 std::string(), "http://icon2", true, "UTF-8;UTF-16",
579 now - one_day, Time());
580 AddKeywordWithDate("name2", "key2", "http://foo2", std::string(),
581 std::string(), std::string(), true, std::string(), now,
582 Time());
583 AddKeywordWithDate("name3", "key3", "http://foo3", std::string(),
584 std::string(), std::string(), true, std::string(),
585 now + one_day, Time());
587 // We just added a few items, validate them.
588 EXPECT_EQ(3U, model()->GetTemplateURLs().size());
590 // Try removing foo2. This should delete foo2, but leave foo1 and 3 untouched.
591 model()->RemoveAutoGeneratedForOriginBetween(GURL("http://foo2"), month_ago,
592 now + one_day);
593 EXPECT_EQ(2U, model()->GetTemplateURLs().size());
594 EXPECT_EQ(ASCIIToUTF16("key1"), model()->GetTemplateURLs()[0]->keyword());
595 EXPECT_TRUE(model()->GetTemplateURLs()[0]->safe_for_autoreplace());
596 EXPECT_EQ(ASCIIToUTF16("key3"), model()->GetTemplateURLs()[1]->keyword());
597 EXPECT_TRUE(model()->GetTemplateURLs()[1]->safe_for_autoreplace());
599 // Try removing foo1, but outside the range in which it was modified. It
600 // should remain untouched.
601 model()->RemoveAutoGeneratedForOriginBetween(GURL("http://foo1"), now,
602 now + one_day);
603 EXPECT_EQ(2U, model()->GetTemplateURLs().size());
604 EXPECT_EQ(ASCIIToUTF16("key1"), model()->GetTemplateURLs()[0]->keyword());
605 EXPECT_TRUE(model()->GetTemplateURLs()[0]->safe_for_autoreplace());
606 EXPECT_EQ(ASCIIToUTF16("key3"), model()->GetTemplateURLs()[1]->keyword());
607 EXPECT_TRUE(model()->GetTemplateURLs()[1]->safe_for_autoreplace());
610 // Try removing foo3. This should delete foo3, but leave foo1 untouched.
611 model()->RemoveAutoGeneratedForOriginBetween(GURL("http://foo3"), month_ago,
612 now + one_day + one_day);
613 EXPECT_EQ(1U, model()->GetTemplateURLs().size());
614 EXPECT_EQ(ASCIIToUTF16("key1"), model()->GetTemplateURLs()[0]->keyword());
615 EXPECT_TRUE(model()->GetTemplateURLs()[0]->safe_for_autoreplace());
618 TEST_F(TemplateURLServiceTest, Reset) {
619 // Add a new TemplateURL.
620 test_util()->VerifyLoad();
621 const size_t initial_count = model()->GetTemplateURLs().size();
622 TemplateURLData data;
623 data.short_name = ASCIIToUTF16("google");
624 data.SetKeyword(ASCIIToUTF16("keyword"));
625 data.SetURL("http://www.google.com/foo/bar");
626 data.favicon_url = GURL("http://favicon.url");
627 data.date_created = Time::FromTimeT(100);
628 data.last_modified = Time::FromTimeT(100);
629 TemplateURL* t_url = new TemplateURL(data);
630 model()->Add(t_url);
632 VerifyObserverCount(1);
633 base::RunLoop().RunUntilIdle();
635 base::Time now = base::Time::Now();
636 scoped_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock);
637 clock->SetNow(now);
638 model()->set_clock(clock.Pass());
640 // Reset the short name, keyword, url and make sure it takes.
641 const base::string16 new_short_name(ASCIIToUTF16("a"));
642 const base::string16 new_keyword(ASCIIToUTF16("b"));
643 const std::string new_url("c");
644 model()->ResetTemplateURL(t_url, new_short_name, new_keyword, new_url);
645 ASSERT_EQ(new_short_name, t_url->short_name());
646 ASSERT_EQ(new_keyword, t_url->keyword());
647 ASSERT_EQ(new_url, t_url->url());
649 // Make sure the mappings in the model were updated.
650 ASSERT_EQ(t_url, model()->GetTemplateURLForKeyword(new_keyword));
651 ASSERT_TRUE(
652 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")) == NULL);
654 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data()));
656 // Reload the model from the database and make sure the change took.
657 test_util()->ResetModel(true);
658 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
659 const TemplateURL* read_url = model()->GetTemplateURLForKeyword(new_keyword);
660 ASSERT_TRUE(read_url);
661 AssertEquals(*cloned_url, *read_url);
662 AssertTimesEqual(now, read_url->last_modified());
665 TEST_F(TemplateURLServiceTest, DefaultSearchProvider) {
666 // Add a new TemplateURL.
667 test_util()->VerifyLoad();
668 const size_t initial_count = model()->GetTemplateURLs().size();
669 TemplateURL* t_url = AddKeywordWithDate(
670 "name1", "key1", "http://foo1/{searchTerms}", "http://sugg1",
671 std::string(), "http://icon1", true, "UTF-8;UTF-16", Time(), Time());
672 test_util()->ResetObserverCount();
674 model()->SetUserSelectedDefaultSearchProvider(t_url);
675 ASSERT_EQ(t_url, model()->GetDefaultSearchProvider());
676 ASSERT_TRUE(t_url->safe_for_autoreplace());
677 ASSERT_TRUE(t_url->show_in_default_list());
679 // Setting the default search provider should have caused notification.
680 VerifyObserverCount(1);
681 base::RunLoop().RunUntilIdle();
683 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data()));
685 // Make sure when we reload we get a default search provider.
686 test_util()->ResetModel(true);
687 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
688 ASSERT_TRUE(model()->GetDefaultSearchProvider());
689 AssertEquals(*cloned_url, *model()->GetDefaultSearchProvider());
692 TEST_F(TemplateURLServiceTest, CantReplaceWithSameKeyword) {
693 test_util()->ChangeModelToLoadState();
694 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"), GURL(), NULL));
695 TemplateURL* t_url = AddKeywordWithDate(
696 "name1", "foo", "http://foo1", "http://sugg1", std::string(),
697 "http://icon1", true, "UTF-8;UTF-16", Time(), Time());
699 // Can still replace, newly added template url is marked safe to replace.
700 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"),
701 GURL("http://foo2"), NULL));
703 // ResetTemplateURL marks the TemplateURL as unsafe to replace, so it should
704 // no longer be replaceable.
705 model()->ResetTemplateURL(t_url, t_url->short_name(), t_url->keyword(),
706 t_url->url());
708 ASSERT_FALSE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"),
709 GURL("http://foo2"), NULL));
712 TEST_F(TemplateURLServiceTest, CantReplaceWithSameHosts) {
713 test_util()->ChangeModelToLoadState();
714 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"),
715 GURL("http://foo.com"), NULL));
716 TemplateURL* t_url = AddKeywordWithDate(
717 "name1", "foo", "http://foo.com", "http://sugg1", std::string(),
718 "http://icon1", true, "UTF-8;UTF-16", Time(), Time());
720 // Can still replace, newly added template url is marked safe to replace.
721 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("bar"),
722 GURL("http://foo.com"), NULL));
724 // ResetTemplateURL marks the TemplateURL as unsafe to replace, so it should
725 // no longer be replaceable.
726 model()->ResetTemplateURL(t_url, t_url->short_name(), t_url->keyword(),
727 t_url->url());
729 ASSERT_FALSE(model()->CanReplaceKeyword(ASCIIToUTF16("bar"),
730 GURL("http://foo.com"), NULL));
733 TEST_F(TemplateURLServiceTest, HasDefaultSearchProvider) {
734 // We should have a default search provider even if we haven't loaded.
735 ASSERT_TRUE(model()->GetDefaultSearchProvider());
737 // Now force the model to load and make sure we still have a default.
738 test_util()->VerifyLoad();
740 ASSERT_TRUE(model()->GetDefaultSearchProvider());
743 TEST_F(TemplateURLServiceTest, DefaultSearchProviderLoadedFromPrefs) {
744 test_util()->VerifyLoad();
746 TemplateURLData data;
747 data.short_name = ASCIIToUTF16("a");
748 data.safe_for_autoreplace = true;
749 data.SetURL("http://url/{searchTerms}");
750 data.suggestions_url = "http://url2";
751 data.instant_url = "http://instant";
752 data.date_created = Time::FromTimeT(100);
753 data.last_modified = Time::FromTimeT(100);
754 TemplateURL* t_url = new TemplateURL(data);
755 model()->Add(t_url);
756 const TemplateURLID id = t_url->id();
758 model()->SetUserSelectedDefaultSearchProvider(t_url);
759 base::RunLoop().RunUntilIdle();
760 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data()));
762 // Reset the model and don't load it. The template url we set as the default
763 // should be pulled from prefs now.
764 test_util()->ResetModel(false);
766 // NOTE: This doesn't use AssertEquals as only a subset of the TemplateURLs
767 // value are persisted to prefs.
768 const TemplateURL* default_turl = model()->GetDefaultSearchProvider();
769 ASSERT_TRUE(default_turl);
770 EXPECT_EQ(ASCIIToUTF16("a"), default_turl->short_name());
771 EXPECT_EQ("http://url/{searchTerms}", default_turl->url());
772 EXPECT_EQ("http://url2", default_turl->suggestions_url());
773 EXPECT_EQ("http://instant", default_turl->instant_url());
774 EXPECT_EQ(id, default_turl->id());
776 // Now do a load and make sure the default search provider really takes.
777 test_util()->VerifyLoad();
779 ASSERT_TRUE(model()->GetDefaultSearchProvider());
780 AssertEquals(*cloned_url, *model()->GetDefaultSearchProvider());
783 TEST_F(TemplateURLServiceTest, RepairPrepopulatedSearchEngines) {
784 test_util()->VerifyLoad();
786 // Edit Google search engine.
787 TemplateURL* google = model()->GetTemplateURLForKeyword(
788 ASCIIToUTF16("google.com"));
789 ASSERT_TRUE(google);
790 model()->ResetTemplateURL(google, ASCIIToUTF16("trash"), ASCIIToUTF16("xxx"),
791 "http://www.foo.com/s?q={searchTerms}");
792 EXPECT_EQ(ASCIIToUTF16("trash"), google->short_name());
793 EXPECT_EQ(ASCIIToUTF16("xxx"), google->keyword());
795 // Add third-party default search engine.
796 TemplateURL* user_dse = AddKeywordWithDate(
797 "malware", "google.com", "http://www.goo.com/s?q={searchTerms}",
798 std::string(), std::string(), std::string(),
799 true, "UTF-8", Time(), Time());
800 model()->SetUserSelectedDefaultSearchProvider(user_dse);
801 EXPECT_EQ(user_dse, model()->GetDefaultSearchProvider());
803 // Remove bing.
804 TemplateURL* bing = model()->GetTemplateURLForKeyword(
805 ASCIIToUTF16("bing.com"));
806 ASSERT_TRUE(bing);
807 model()->Remove(bing);
808 EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("bing.com")));
810 // Register an extension with bing keyword.
811 model()->RegisterOmniboxKeyword("abcdefg", "extension_name", "bing.com",
812 "http://abcdefg");
813 EXPECT_TRUE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("bing.com")));
815 model()->RepairPrepopulatedSearchEngines();
817 // Google is default.
818 ASSERT_EQ(google, model()->GetDefaultSearchProvider());
819 // The keyword wasn't reverted.
820 EXPECT_EQ(ASCIIToUTF16("trash"), google->short_name());
821 EXPECT_EQ("www.google.com",
822 google->GenerateSearchURL(model()->search_terms_data()).host());
824 // Bing was repaired.
825 bing = model()->GetTemplateURLForKeyword(ASCIIToUTF16("bing.com"));
826 ASSERT_TRUE(bing);
827 EXPECT_EQ(TemplateURL::NORMAL, bing->GetType());
829 // User search engine is preserved.
830 EXPECT_EQ(user_dse, model()->GetTemplateURLForHost("www.goo.com"));
831 EXPECT_EQ(ASCIIToUTF16("google.com"), user_dse->keyword());
834 TEST_F(TemplateURLServiceTest, RepairSearchEnginesWithManagedDefault) {
835 // Set a managed preference that establishes a default search provider.
836 const char kName[] = "test1";
837 const char kKeyword[] = "test.com";
838 const char kSearchURL[] = "http://test.com/search?t={searchTerms}";
839 const char kIconURL[] = "http://test.com/icon.jpg";
840 const char kEncodings[] = "UTF-16;UTF-32";
841 const char kAlternateURL[] = "http://test.com/search#t={searchTerms}";
842 const char kSearchTermsReplacementKey[] = "espv";
843 test_util()->SetManagedDefaultSearchPreferences(true, kName, kKeyword,
844 kSearchURL, std::string(),
845 kIconURL, kEncodings,
846 kAlternateURL,
847 kSearchTermsReplacementKey);
848 test_util()->VerifyLoad();
849 // Verify that the default manager we are getting is the managed one.
850 TemplateURLData data;
851 data.short_name = ASCIIToUTF16(kName);
852 data.SetKeyword(ASCIIToUTF16(kKeyword));
853 data.SetURL(kSearchURL);
854 data.favicon_url = GURL(kIconURL);
855 data.show_in_default_list = true;
856 base::SplitString(kEncodings, ';', &data.input_encodings);
857 data.alternate_urls.push_back(kAlternateURL);
858 data.search_terms_replacement_key = kSearchTermsReplacementKey;
859 scoped_ptr<TemplateURL> expected_managed_default(new TemplateURL(data));
860 EXPECT_TRUE(model()->is_default_search_managed());
861 const TemplateURL* actual_managed_default =
862 model()->GetDefaultSearchProvider();
863 ExpectSimilar(expected_managed_default.get(), actual_managed_default);
865 // The following call has no effect on the managed search engine.
866 model()->RepairPrepopulatedSearchEngines();
868 EXPECT_TRUE(model()->is_default_search_managed());
869 actual_managed_default = model()->GetDefaultSearchProvider();
870 ExpectSimilar(expected_managed_default.get(), actual_managed_default);
873 TEST_F(TemplateURLServiceTest, UpdateKeywordSearchTermsForURL) {
874 struct TestData {
875 const std::string url;
876 const base::string16 term;
877 } data[] = {
878 { "http://foo/", base::string16() },
879 { "http://foo/foo?q=xx", base::string16() },
880 { "http://x/bar?q=xx", base::string16() },
881 { "http://x/foo?y=xx", base::string16() },
882 { "http://x/foo?q=xx", ASCIIToUTF16("xx") },
883 { "http://x/foo?a=b&q=xx", ASCIIToUTF16("xx") },
884 { "http://x/foo?q=b&q=xx", base::string16() },
885 { "http://x/foo#query=xx", ASCIIToUTF16("xx") },
886 { "http://x/foo?q=b#query=xx", ASCIIToUTF16("xx") },
887 { "http://x/foo?q=b#q=xx", ASCIIToUTF16("b") },
888 { "http://x/foo?query=b#q=xx", base::string16() },
891 test_util()->ChangeModelToLoadState();
892 AddKeywordWithDate("name", "x", "http://x/foo?q={searchTerms}",
893 "http://sugg1", "http://x/foo#query={searchTerms}",
894 "http://icon1", false, "UTF-8;UTF-16", Time(), Time());
896 for (size_t i = 0; i < arraysize(data); ++i) {
897 TemplateURLService::URLVisitedDetails details = {
898 GURL(data[i].url), false
900 model()->UpdateKeywordSearchTermsForURL(details);
901 EXPECT_EQ(data[i].term, test_util()->GetAndClearSearchTerm());
905 TEST_F(TemplateURLServiceTest, DontUpdateKeywordSearchForNonReplaceable) {
906 struct TestData {
907 const std::string url;
908 } data[] = {
909 { "http://foo/" },
910 { "http://x/bar?q=xx" },
911 { "http://x/foo?y=xx" },
914 test_util()->ChangeModelToLoadState();
915 AddKeywordWithDate("name", "x", "http://x/foo", "http://sugg1", std::string(),
916 "http://icon1", false, "UTF-8;UTF-16", Time(), Time());
918 for (size_t i = 0; i < arraysize(data); ++i) {
919 TemplateURLService::URLVisitedDetails details = {
920 GURL(data[i].url), false
922 model()->UpdateKeywordSearchTermsForURL(details);
923 ASSERT_EQ(base::string16(), test_util()->GetAndClearSearchTerm());
927 TEST_F(TemplateURLServiceWithoutFallbackTest, ChangeGoogleBaseValue) {
928 // NOTE: Do not load the prepopulate data, which also has a {google:baseURL}
929 // keyword in it and would confuse this test.
930 test_util()->ChangeModelToLoadState();
932 test_util()->SetGoogleBaseURL(GURL("http://google.com/"));
933 const TemplateURL* t_url = AddKeywordWithDate(
934 "name", "google.com", "{google:baseURL}?q={searchTerms}", "http://sugg1",
935 std::string(), "http://icon1", false, "UTF-8;UTF-16", Time(), Time());
936 ASSERT_EQ(t_url, model()->GetTemplateURLForHost("google.com"));
937 EXPECT_EQ("google.com", t_url->url_ref().GetHost(search_terms_data()));
938 EXPECT_EQ(ASCIIToUTF16("google.com"), t_url->keyword());
940 // Change the Google base url.
941 test_util()->ResetObserverCount();
942 test_util()->SetGoogleBaseURL(GURL("http://google.co.uk/"));
943 VerifyObserverCount(1);
945 // Make sure the host->TemplateURL map was updated appropriately.
946 ASSERT_EQ(t_url, model()->GetTemplateURLForHost("google.co.uk"));
947 EXPECT_TRUE(model()->GetTemplateURLForHost("google.com") == NULL);
948 EXPECT_EQ("google.co.uk", t_url->url_ref().GetHost(search_terms_data()));
949 EXPECT_EQ(ASCIIToUTF16("google.co.uk"), t_url->keyword());
950 EXPECT_EQ("http://google.co.uk/?q=x", t_url->url_ref().ReplaceSearchTerms(
951 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("x")), search_terms_data()));
953 // Now add a manual entry and then change the Google base URL such that the
954 // autogenerated Google search keyword would conflict.
955 TemplateURL* manual = AddKeywordWithDate(
956 "manual", "google.de", "http://google.de/search?q={searchTerms}",
957 std::string(), std::string(), std::string(), false, "UTF-8", Time(),
958 Time());
959 test_util()->SetGoogleBaseURL(GURL("http://google.de"));
961 // Verify that the manual entry is untouched, and the autogenerated keyword
962 // has not changed.
963 ASSERT_EQ(manual,
964 model()->GetTemplateURLForKeyword(ASCIIToUTF16("google.de")));
965 EXPECT_EQ("google.de", manual->url_ref().GetHost(search_terms_data()));
966 ASSERT_EQ(t_url,
967 model()->GetTemplateURLForKeyword(ASCIIToUTF16("google.co.uk")));
968 EXPECT_EQ("google.de", t_url->url_ref().GetHost(search_terms_data()));
969 EXPECT_EQ(ASCIIToUTF16("google.co.uk"), t_url->keyword());
971 // Change the base URL again and verify that the autogenerated keyword follows
972 // even though it didn't match the base URL, while the manual entry is still
973 // untouched.
974 test_util()->SetGoogleBaseURL(GURL("http://google.fr/"));
975 ASSERT_EQ(manual, model()->GetTemplateURLForHost("google.de"));
976 EXPECT_EQ("google.de", manual->url_ref().GetHost(search_terms_data()));
977 EXPECT_EQ(ASCIIToUTF16("google.de"), manual->keyword());
978 ASSERT_EQ(t_url, model()->GetTemplateURLForHost("google.fr"));
979 EXPECT_TRUE(model()->GetTemplateURLForHost("google.co.uk") == NULL);
980 EXPECT_EQ("google.fr", t_url->url_ref().GetHost(search_terms_data()));
981 EXPECT_EQ(ASCIIToUTF16("google.fr"), t_url->keyword());
984 // Make sure TemplateURLService generates a KEYWORD_GENERATED visit for
985 // KEYWORD visits.
986 TEST_F(TemplateURLServiceTest, GenerateVisitOnKeyword) {
987 test_util()->profile()->CreateBookmarkModel(false);
988 ASSERT_TRUE(test_util()->profile()->CreateHistoryService(true, false));
989 test_util()->ResetModel(true);
991 // Create a keyword.
992 TemplateURL* t_url = AddKeywordWithDate(
993 "keyword", "keyword", "http://foo.com/foo?query={searchTerms}",
994 "http://sugg1", std::string(), "http://icon1", true, "UTF-8;UTF-16",
995 base::Time::Now(), base::Time::Now());
997 // Add a visit that matches the url of the keyword.
998 HistoryService* history = HistoryServiceFactory::GetForProfile(
999 test_util()->profile(), ServiceAccessType::EXPLICIT_ACCESS);
1000 history->AddPage(
1001 GURL(t_url->url_ref().ReplaceSearchTerms(
1002 TemplateURLRef::SearchTermsArgs(ASCIIToUTF16("blah")),
1003 search_terms_data())),
1004 base::Time::Now(), NULL, 0, GURL(), history::RedirectList(),
1005 ui::PAGE_TRANSITION_KEYWORD, history::SOURCE_BROWSED, false);
1007 // Wait for history to finish processing the request.
1008 test_util()->profile()->BlockUntilHistoryProcessesPendingRequests();
1010 // Query history for the generated url.
1011 base::CancelableTaskTracker tracker;
1012 QueryHistoryCallbackImpl callback;
1013 history->QueryURL(GURL("http://keyword"),
1014 true,
1015 base::Bind(&QueryHistoryCallbackImpl::Callback,
1016 base::Unretained(&callback)),
1017 &tracker);
1019 // Wait for the request to be processed.
1020 test_util()->profile()->BlockUntilHistoryProcessesPendingRequests();
1022 // And make sure the url and visit were added.
1023 EXPECT_TRUE(callback.success);
1024 EXPECT_NE(0, callback.row.id());
1025 ASSERT_EQ(1U, callback.visits.size());
1026 EXPECT_EQ(ui::PAGE_TRANSITION_KEYWORD_GENERATED,
1027 ui::PageTransitionStripQualifier(callback.visits[0].transition));
1030 // Make sure that the load routine deletes prepopulated engines that no longer
1031 // exist in the prepopulate data.
1032 TEST_F(TemplateURLServiceTest, LoadDeletesUnusedProvider) {
1033 // Create a preloaded template url. Add it to a loaded model and wait for the
1034 // saves to finish.
1035 TemplateURL* t_url = CreatePreloadedTemplateURL(true, 999999);
1036 test_util()->ChangeModelToLoadState();
1037 model()->Add(t_url);
1038 ASSERT_TRUE(
1039 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")) != NULL);
1040 base::RunLoop().RunUntilIdle();
1042 // Ensure that merging clears this engine.
1043 test_util()->ResetModel(true);
1044 ASSERT_TRUE(
1045 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")) == NULL);
1047 // Wait for any saves to finish.
1048 base::RunLoop().RunUntilIdle();
1050 // Reload the model to verify that the database was updated as a result of the
1051 // merge.
1052 test_util()->ResetModel(true);
1053 ASSERT_TRUE(
1054 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")) == NULL);
1057 // Make sure that load routine doesn't delete prepopulated engines that no
1058 // longer exist in the prepopulate data if it has been modified by the user.
1059 TEST_F(TemplateURLServiceTest, LoadRetainsModifiedProvider) {
1060 // Create a preloaded template url and add it to a loaded model.
1061 TemplateURL* t_url = CreatePreloadedTemplateURL(false, 999999);
1062 test_util()->ChangeModelToLoadState();
1063 model()->Add(t_url);
1065 // Do the copy after t_url is added so that the id is set.
1066 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data()));
1067 ASSERT_EQ(t_url, model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")));
1069 // Wait for any saves to finish.
1070 base::RunLoop().RunUntilIdle();
1072 // Ensure that merging won't clear it if the user has edited it.
1073 test_util()->ResetModel(true);
1074 const TemplateURL* url_for_unittest =
1075 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest"));
1076 ASSERT_TRUE(url_for_unittest != NULL);
1077 AssertEquals(*cloned_url, *url_for_unittest);
1079 // Wait for any saves to finish.
1080 base::RunLoop().RunUntilIdle();
1082 // Reload the model to verify that save/reload retains the item.
1083 test_util()->ResetModel(true);
1084 ASSERT_TRUE(
1085 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")) != NULL);
1088 // Make sure that load routine doesn't delete
1089 // prepopulated engines that no longer exist in the prepopulate data if
1090 // it has been modified by the user.
1091 TEST_F(TemplateURLServiceTest, LoadSavesPrepopulatedDefaultSearchProvider) {
1092 test_util()->VerifyLoad();
1093 // Verify that the default search provider is set to something.
1094 TemplateURL* default_search = model()->GetDefaultSearchProvider();
1095 ASSERT_TRUE(default_search != NULL);
1096 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(default_search->data()));
1098 // Wait for any saves to finish.
1099 base::RunLoop().RunUntilIdle();
1101 // Reload the model and check that the default search provider
1102 // was properly saved.
1103 test_util()->ResetModel(true);
1104 default_search = model()->GetDefaultSearchProvider();
1105 ASSERT_TRUE(default_search != NULL);
1106 AssertEquals(*cloned_url, *default_search);
1109 // Make sure that the load routine doesn't delete
1110 // prepopulated engines that no longer exist in the prepopulate data if
1111 // it is the default search provider.
1112 TEST_F(TemplateURLServiceTest, LoadRetainsDefaultProvider) {
1113 // Set the default search provider to a preloaded template url which
1114 // is not in the current set of preloaded template urls and save
1115 // the result.
1116 TemplateURL* t_url = CreatePreloadedTemplateURL(true, 999999);
1117 test_util()->ChangeModelToLoadState();
1118 model()->Add(t_url);
1119 model()->SetUserSelectedDefaultSearchProvider(t_url);
1120 // Do the copy after t_url is added and set as default so that its
1121 // internal state is correct.
1122 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data()));
1124 ASSERT_EQ(t_url, model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")));
1125 ASSERT_EQ(t_url, model()->GetDefaultSearchProvider());
1126 base::RunLoop().RunUntilIdle();
1128 // Ensure that merging won't clear the prepopulated template url
1129 // which is no longer present if it's the default engine.
1130 test_util()->ResetModel(true);
1132 const TemplateURL* keyword_url =
1133 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest"));
1134 ASSERT_TRUE(keyword_url != NULL);
1135 AssertEquals(*cloned_url, *keyword_url);
1136 ASSERT_EQ(keyword_url, model()->GetDefaultSearchProvider());
1139 // Wait for any saves to finish.
1140 base::RunLoop().RunUntilIdle();
1142 // Reload the model to verify that the update was saved.
1143 test_util()->ResetModel(true);
1145 const TemplateURL* keyword_url =
1146 model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest"));
1147 ASSERT_TRUE(keyword_url != NULL);
1148 AssertEquals(*cloned_url, *keyword_url);
1149 ASSERT_EQ(keyword_url, model()->GetDefaultSearchProvider());
1153 // Make sure that the load routine sets a default search provider if it was
1154 // missing and not managed.
1155 TEST_F(TemplateURLServiceTest, LoadEnsuresDefaultSearchProviderExists) {
1156 // Force the model to load and make sure we have a default search provider.
1157 test_util()->VerifyLoad();
1158 EXPECT_TRUE(model()->GetDefaultSearchProvider());
1160 EXPECT_TRUE(model()->GetDefaultSearchProvider()->SupportsReplacement(
1161 search_terms_data()));
1163 // Make default search provider unusable (no search terms).
1164 model()->ResetTemplateURL(model()->GetDefaultSearchProvider(),
1165 ASCIIToUTF16("test"), ASCIIToUTF16("test"),
1166 "http://example.com/");
1167 base::RunLoop().RunUntilIdle();
1169 // Reset the model and load it. There should be a usable default search
1170 // provider.
1171 test_util()->ResetModel(true);
1173 ASSERT_TRUE(model()->GetDefaultSearchProvider());
1174 EXPECT_TRUE(model()->GetDefaultSearchProvider()->SupportsReplacement(
1175 search_terms_data()));
1178 // Simulates failing to load the webdb and makes sure the default search
1179 // provider is valid.
1180 TEST_F(TemplateURLServiceTest, FailedInit) {
1181 test_util()->VerifyLoad();
1183 test_util()->ClearModel();
1184 test_util()->web_data_service()->ShutdownDatabase();
1186 test_util()->ResetModel(false);
1187 model()->Load();
1188 base::RunLoop().RunUntilIdle();
1190 ASSERT_TRUE(model()->GetDefaultSearchProvider());
1193 // Verifies that if the default search URL preference is managed, we report
1194 // the default search as managed. Also check that we are getting the right
1195 // values.
1196 TEST_F(TemplateURLServiceTest, TestManagedDefaultSearch) {
1197 test_util()->VerifyLoad();
1198 const size_t initial_count = model()->GetTemplateURLs().size();
1199 test_util()->ResetObserverCount();
1201 // Set a regular default search provider.
1202 TemplateURL* regular_default = AddKeywordWithDate(
1203 "name1", "key1", "http://foo1/{searchTerms}", "http://sugg1",
1204 std::string(), "http://icon1", true, "UTF-8;UTF-16", Time(), Time());
1205 VerifyObserverCount(1);
1206 model()->SetUserSelectedDefaultSearchProvider(regular_default);
1207 // Adding the URL and setting the default search provider should have caused
1208 // notifications.
1209 VerifyObserverCount(1);
1210 EXPECT_FALSE(model()->is_default_search_managed());
1211 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
1213 // Set a managed preference that establishes a default search provider.
1214 const char kName[] = "test1";
1215 const char kKeyword[] = "test.com";
1216 const char kSearchURL[] = "http://test.com/search?t={searchTerms}";
1217 const char kIconURL[] = "http://test.com/icon.jpg";
1218 const char kEncodings[] = "UTF-16;UTF-32";
1219 const char kAlternateURL[] = "http://test.com/search#t={searchTerms}";
1220 const char kSearchTermsReplacementKey[] = "espv";
1221 test_util()->SetManagedDefaultSearchPreferences(true, kName, kKeyword,
1222 kSearchURL, std::string(), kIconURL, kEncodings, kAlternateURL,
1223 kSearchTermsReplacementKey);
1224 VerifyObserverFired();
1225 EXPECT_TRUE(model()->is_default_search_managed());
1226 EXPECT_EQ(initial_count + 2, model()->GetTemplateURLs().size());
1228 // Verify that the default manager we are getting is the managed one.
1229 TemplateURLData data;
1230 data.short_name = ASCIIToUTF16(kName);
1231 data.SetKeyword(ASCIIToUTF16(kKeyword));
1232 data.SetURL(kSearchURL);
1233 data.favicon_url = GURL(kIconURL);
1234 data.show_in_default_list = true;
1235 base::SplitString(kEncodings, ';', &data.input_encodings);
1236 data.alternate_urls.push_back(kAlternateURL);
1237 data.search_terms_replacement_key = kSearchTermsReplacementKey;
1238 scoped_ptr<TemplateURL> expected_managed_default1(new TemplateURL(data));
1239 const TemplateURL* actual_managed_default =
1240 model()->GetDefaultSearchProvider();
1241 ExpectSimilar(expected_managed_default1.get(), actual_managed_default);
1242 EXPECT_TRUE(actual_managed_default->show_in_default_list());
1244 // Update the managed preference and check that the model has changed.
1245 const char kNewName[] = "test2";
1246 const char kNewKeyword[] = "other.com";
1247 const char kNewSearchURL[] = "http://other.com/search?t={searchTerms}";
1248 const char kNewSuggestURL[] = "http://other.com/suggest?t={searchTerms}";
1249 test_util()->SetManagedDefaultSearchPreferences(true, kNewName, kNewKeyword,
1250 kNewSearchURL, kNewSuggestURL, std::string(), std::string(),
1251 std::string(), std::string());
1252 VerifyObserverFired();
1253 EXPECT_TRUE(model()->is_default_search_managed());
1254 EXPECT_EQ(initial_count + 2, model()->GetTemplateURLs().size());
1256 // Verify that the default manager we are now getting is the correct one.
1257 TemplateURLData data2;
1258 data2.short_name = ASCIIToUTF16(kNewName);
1259 data2.SetKeyword(ASCIIToUTF16(kNewKeyword));
1260 data2.SetURL(kNewSearchURL);
1261 data2.suggestions_url = kNewSuggestURL;
1262 data2.show_in_default_list = true;
1263 scoped_ptr<TemplateURL> expected_managed_default2(new TemplateURL(data2));
1264 actual_managed_default = model()->GetDefaultSearchProvider();
1265 ExpectSimilar(expected_managed_default2.get(), actual_managed_default);
1266 EXPECT_EQ(actual_managed_default->show_in_default_list(), true);
1268 // Remove all the managed prefs and check that we are no longer managed.
1269 test_util()->RemoveManagedDefaultSearchPreferences();
1270 VerifyObserverFired();
1271 EXPECT_FALSE(model()->is_default_search_managed());
1272 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
1274 // The default should now be the user preference.
1275 const TemplateURL* actual_final_managed_default =
1276 model()->GetDefaultSearchProvider();
1277 ExpectSimilar(regular_default, actual_final_managed_default);
1278 EXPECT_EQ(actual_final_managed_default->show_in_default_list(), true);
1280 // Disable the default search provider through policy.
1281 test_util()->SetManagedDefaultSearchPreferences(false, std::string(),
1282 std::string(), std::string(), std::string(), std::string(),
1283 std::string(), std::string(), std::string());
1284 VerifyObserverFired();
1285 EXPECT_TRUE(model()->is_default_search_managed());
1286 EXPECT_TRUE(NULL == model()->GetDefaultSearchProvider());
1287 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
1289 // Re-enable it.
1290 test_util()->SetManagedDefaultSearchPreferences(true, kName, kKeyword,
1291 kSearchURL, std::string(), kIconURL, kEncodings, kAlternateURL,
1292 kSearchTermsReplacementKey);
1293 VerifyObserverFired();
1294 EXPECT_TRUE(model()->is_default_search_managed());
1295 EXPECT_EQ(initial_count + 2, model()->GetTemplateURLs().size());
1297 // Verify that the default manager we are getting is the managed one.
1298 actual_managed_default = model()->GetDefaultSearchProvider();
1299 ExpectSimilar(expected_managed_default1.get(), actual_managed_default);
1300 EXPECT_EQ(actual_managed_default->show_in_default_list(), true);
1302 // Clear the model and disable the default search provider through policy.
1303 // Verify that there is no default search provider after loading the model.
1304 // This checks against regressions of http://crbug.com/67180
1306 // First, remove the preferences, reset the model, and set a default.
1307 test_util()->RemoveManagedDefaultSearchPreferences();
1308 test_util()->ResetModel(true);
1309 TemplateURL* new_default =
1310 model()->GetTemplateURLForKeyword(ASCIIToUTF16("key1"));
1311 ASSERT_FALSE(new_default == NULL);
1312 model()->SetUserSelectedDefaultSearchProvider(new_default);
1313 EXPECT_EQ(new_default, model()->GetDefaultSearchProvider());
1315 // Now reset the model again but load it after setting the preferences.
1316 test_util()->ResetModel(false);
1317 test_util()->SetManagedDefaultSearchPreferences(false, std::string(),
1318 std::string(), std::string(), std::string(), std::string(),
1319 std::string(), std::string(), std::string());
1320 test_util()->VerifyLoad();
1321 EXPECT_TRUE(model()->is_default_search_managed());
1322 EXPECT_TRUE(model()->GetDefaultSearchProvider() == NULL);
1325 // Test that if we load a TemplateURL with an empty GUID, the load process
1326 // assigns it a newly generated GUID.
1327 TEST_F(TemplateURLServiceTest, PatchEmptySyncGUID) {
1328 // Add a new TemplateURL.
1329 test_util()->VerifyLoad();
1330 const size_t initial_count = model()->GetTemplateURLs().size();
1332 TemplateURLData data;
1333 data.short_name = ASCIIToUTF16("google");
1334 data.SetKeyword(ASCIIToUTF16("keyword"));
1335 data.SetURL("http://www.google.com/foo/bar");
1336 data.sync_guid.clear();
1337 TemplateURL* t_url = new TemplateURL(data);
1338 model()->Add(t_url);
1340 VerifyObserverCount(1);
1341 base::RunLoop().RunUntilIdle();
1342 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
1344 // Reload the model to verify it was actually saved to the database and
1345 // assigned a new GUID when brought back.
1346 test_util()->ResetModel(true);
1347 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
1348 const TemplateURL* loaded_url =
1349 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"));
1350 ASSERT_FALSE(loaded_url == NULL);
1351 ASSERT_FALSE(loaded_url->sync_guid().empty());
1354 // Test that if we load a TemplateURL with duplicate input encodings, the load
1355 // process de-dupes them.
1356 TEST_F(TemplateURLServiceTest, DuplicateInputEncodings) {
1357 // Add a new TemplateURL.
1358 test_util()->VerifyLoad();
1359 const size_t initial_count = model()->GetTemplateURLs().size();
1361 TemplateURLData data;
1362 data.short_name = ASCIIToUTF16("google");
1363 data.SetKeyword(ASCIIToUTF16("keyword"));
1364 data.SetURL("http://www.google.com/foo/bar");
1365 std::vector<std::string> encodings;
1366 data.input_encodings.push_back("UTF-8");
1367 data.input_encodings.push_back("UTF-8");
1368 data.input_encodings.push_back("UTF-16");
1369 data.input_encodings.push_back("UTF-8");
1370 data.input_encodings.push_back("Big5");
1371 data.input_encodings.push_back("UTF-16");
1372 data.input_encodings.push_back("Big5");
1373 data.input_encodings.push_back("Windows-1252");
1374 TemplateURL* t_url = new TemplateURL(data);
1375 model()->Add(t_url);
1377 VerifyObserverCount(1);
1378 base::RunLoop().RunUntilIdle();
1379 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
1380 const TemplateURL* loaded_url =
1381 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"));
1382 ASSERT_TRUE(loaded_url != NULL);
1383 EXPECT_EQ(8U, loaded_url->input_encodings().size());
1385 // Reload the model to verify it was actually saved to the database and the
1386 // duplicate encodings were removed.
1387 test_util()->ResetModel(true);
1388 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size());
1389 loaded_url = model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"));
1390 ASSERT_FALSE(loaded_url == NULL);
1391 EXPECT_EQ(4U, loaded_url->input_encodings().size());
1394 TEST_F(TemplateURLServiceTest, DefaultExtensionEngine) {
1395 test_util()->VerifyLoad();
1396 // Add third-party default search engine.
1397 TemplateURL* user_dse = AddKeywordWithDate(
1398 "user", "user", "http://www.goo.com/s?q={searchTerms}",
1399 std::string(), std::string(), std::string(),
1400 true, "UTF-8", Time(), Time());
1401 model()->SetUserSelectedDefaultSearchProvider(user_dse);
1402 EXPECT_EQ(user_dse, model()->GetDefaultSearchProvider());
1404 TemplateURL* ext_dse = CreateKeywordWithDate(
1405 model(), "ext", "ext", "http://www.search.com/s?q={searchTerms}",
1406 std::string(), std::string(), std::string(),
1407 true, true, "UTF-8", Time(), Time());
1408 scoped_ptr<TemplateURL::AssociatedExtensionInfo> extension_info(
1409 new TemplateURL::AssociatedExtensionInfo(
1410 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION, "ext"));
1411 extension_info->wants_to_be_default_engine = true;
1412 model()->AddExtensionControlledTURL(ext_dse, extension_info.Pass());
1413 EXPECT_EQ(ext_dse, model()->GetDefaultSearchProvider());
1415 model()->RemoveExtensionControlledTURL(
1416 "ext", TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION);
1417 ExpectSimilar(user_dse, model()->GetDefaultSearchProvider());
1420 TEST_F(TemplateURLServiceTest, ExtensionEnginesNotPersist) {
1421 test_util()->VerifyLoad();
1422 // Add third-party default search engine.
1423 TemplateURL* user_dse = AddKeywordWithDate(
1424 "user", "user", "http://www.goo.com/s?q={searchTerms}",
1425 std::string(), std::string(), std::string(),
1426 true, "UTF-8", Time(), Time());
1427 model()->SetUserSelectedDefaultSearchProvider(user_dse);
1428 EXPECT_EQ(user_dse, model()->GetDefaultSearchProvider());
1430 TemplateURL* ext_dse = CreateKeywordWithDate(
1431 model(), "ext1", "ext1", "http://www.ext1.com/s?q={searchTerms}",
1432 std::string(), std::string(), std::string(),
1433 true, false, "UTF-8", Time(), Time());
1434 scoped_ptr<TemplateURL::AssociatedExtensionInfo> extension_info(
1435 new TemplateURL::AssociatedExtensionInfo(
1436 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION, "ext1"));
1437 extension_info->wants_to_be_default_engine = false;
1438 model()->AddExtensionControlledTURL(ext_dse, extension_info.Pass());
1439 EXPECT_EQ(user_dse, model()->GetDefaultSearchProvider());
1441 ext_dse = CreateKeywordWithDate(
1442 model(), "ext2", "ext2", "http://www.ext2.com/s?q={searchTerms}",
1443 std::string(), std::string(), std::string(),
1444 true, true, "UTF-8", Time(), Time());
1445 extension_info.reset(new TemplateURL::AssociatedExtensionInfo(
1446 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION, "ext2"));
1447 extension_info->wants_to_be_default_engine = true;
1448 model()->AddExtensionControlledTURL(ext_dse, extension_info.Pass());
1449 EXPECT_EQ(ext_dse, model()->GetDefaultSearchProvider());
1451 test_util()->ResetModel(true);
1452 user_dse = model()->GetTemplateURLForKeyword(ASCIIToUTF16("user"));
1453 ExpectSimilar(user_dse, model()->GetDefaultSearchProvider());
1454 EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("ext1")));
1455 EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("ext2")));
1458 TEST_F(TemplateURLServiceTest, ExtensionEngineVsPolicy) {
1459 // Set a managed preference that establishes a default search provider.
1460 const char kName[] = "test";
1461 const char kKeyword[] = "test.com";
1462 const char kSearchURL[] = "http://test.com/search?t={searchTerms}";
1463 const char kIconURL[] = "http://test.com/icon.jpg";
1464 const char kEncodings[] = "UTF-16;UTF-32";
1465 const char kAlternateURL[] = "http://test.com/search#t={searchTerms}";
1466 const char kSearchTermsReplacementKey[] = "espv";
1467 test_util()->SetManagedDefaultSearchPreferences(
1468 true, kName, kKeyword, kSearchURL, std::string(), kIconURL, kEncodings,
1469 kAlternateURL, kSearchTermsReplacementKey);
1470 test_util()->VerifyLoad();
1471 // Verify that the default manager we are getting is the managed one.
1472 TemplateURLData data;
1473 data.short_name = ASCIIToUTF16(kName);
1474 data.SetKeyword(ASCIIToUTF16(kKeyword));
1475 data.SetURL(kSearchURL);
1476 data.favicon_url = GURL(kIconURL);
1477 data.show_in_default_list = true;
1478 base::SplitString(kEncodings, ';', &data.input_encodings);
1479 data.alternate_urls.push_back(kAlternateURL);
1480 data.search_terms_replacement_key = kSearchTermsReplacementKey;
1481 scoped_ptr<TemplateURL> expected_managed_default(new TemplateURL(data));
1482 EXPECT_TRUE(model()->is_default_search_managed());
1483 const TemplateURL* actual_managed_default =
1484 model()->GetDefaultSearchProvider();
1485 ExpectSimilar(expected_managed_default.get(), actual_managed_default);
1487 TemplateURL* ext_dse = CreateKeywordWithDate(
1488 model(), "ext1", "ext1", "http://www.ext1.com/s?q={searchTerms}",
1489 std::string(), std::string(), std::string(),
1490 true, true, "UTF-8", Time(), Time());
1491 scoped_ptr<TemplateURL::AssociatedExtensionInfo> extension_info(
1492 new TemplateURL::AssociatedExtensionInfo(
1493 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION, "ext1"));
1494 extension_info->wants_to_be_default_engine = true;
1495 model()->AddExtensionControlledTURL(ext_dse, extension_info.Pass());
1496 EXPECT_EQ(ext_dse, model()->GetTemplateURLForKeyword(ASCIIToUTF16("ext1")));
1497 EXPECT_TRUE(model()->is_default_search_managed());
1498 actual_managed_default = model()->GetDefaultSearchProvider();
1499 ExpectSimilar(expected_managed_default.get(), actual_managed_default);