Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/bind_helpers.h" | 6 #include "base/bind_helpers.h" |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/memory/scoped_vector.h" | 9 #include "base/memory/scoped_vector.h" |
| 10 #include "base/string_split.h" | 10 #include "base/string_split.h" |
| 11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
| 12 #include "base/test/mock_time_provider.h" | 12 #include "base/test/mock_time_provider.h" |
| 13 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
| 14 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
| 15 #include "chrome/browser/history/history.h" | 15 #include "chrome/browser/history/history.h" |
| 16 #include "chrome/browser/history/history_notifications.h" | 16 #include "chrome/browser/history/history_notifications.h" |
| 17 #include "chrome/browser/search_engines/search_host_to_urls_map.h" | 17 #include "chrome/browser/search_engines/search_host_to_urls_map.h" |
| 18 #include "chrome/browser/search_engines/search_terms_data.h" | 18 #include "chrome/browser/search_engines/search_terms_data.h" |
| 19 #include "chrome/browser/search_engines/template_url.h" | 19 #include "chrome/browser/search_engines/template_url.h" |
| 20 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" | 20 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" |
| 21 #include "chrome/browser/search_engines/template_url_service.h" | 21 #include "chrome/browser/search_engines/template_url_service.h" |
| 22 #include "chrome/browser/search_engines/template_url_service_test_util.h" | 22 #include "chrome/browser/search_engines/template_url_service_test_util.h" |
| 23 #include "chrome/browser/webdata/web_database.h" | 23 #include "chrome/browser/webdata/web_database.h" |
| 24 #include "chrome/common/url_constants.h" | |
| 24 #include "chrome/test/base/testing_profile.h" | 25 #include "chrome/test/base/testing_profile.h" |
| 25 #include "content/test/test_browser_thread.h" | 26 #include "content/test/test_browser_thread.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 27 | 28 |
| 28 using base::Time; | 29 using base::Time; |
| 29 using base::TimeDelta; | 30 using base::TimeDelta; |
| 30 using content::BrowserThread; | 31 using content::BrowserThread; |
| 31 using ::testing::Return; | 32 using ::testing::Return; |
| 32 using ::testing::StrictMock; | 33 using ::testing::StrictMock; |
| 33 | 34 |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 130 class TemplateURLServiceTest : public testing::Test { | 131 class TemplateURLServiceTest : public testing::Test { |
| 131 public: | 132 public: |
| 132 TemplateURLServiceTest(); | 133 TemplateURLServiceTest(); |
| 133 | 134 |
| 134 // testing::Test | 135 // testing::Test |
| 135 virtual void SetUp(); | 136 virtual void SetUp(); |
| 136 virtual void TearDown(); | 137 virtual void TearDown(); |
| 137 | 138 |
| 138 TemplateURL* AddKeywordWithDate(const std::string& short_name, | 139 TemplateURL* AddKeywordWithDate(const std::string& short_name, |
| 139 const std::string& keyword, | 140 const std::string& keyword, |
| 140 bool autogenerate_keyword, | |
| 141 const std::string& url, | 141 const std::string& url, |
| 142 const std::string& suggest_url, | 142 const std::string& suggest_url, |
| 143 const std::string& favicon_url, | 143 const std::string& favicon_url, |
| 144 bool safe_for_autoreplace, | 144 bool safe_for_autoreplace, |
| 145 const std::string& encodings, | 145 const std::string& encodings, |
| 146 Time date_created, | 146 Time date_created, |
| 147 Time last_modified); | 147 Time last_modified); |
| 148 | 148 |
| 149 // Verifies the two TemplateURLs are equal. | 149 // Verifies the two TemplateURLs are equal. |
| 150 void AssertEquals(const TemplateURL& expected, const TemplateURL& actual); | 150 void AssertEquals(const TemplateURL& expected, const TemplateURL& actual); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 191 test_util_.SetUp(); | 191 test_util_.SetUp(); |
| 192 } | 192 } |
| 193 | 193 |
| 194 void TemplateURLServiceTest::TearDown() { | 194 void TemplateURLServiceTest::TearDown() { |
| 195 test_util_.TearDown(); | 195 test_util_.TearDown(); |
| 196 } | 196 } |
| 197 | 197 |
| 198 TemplateURL* TemplateURLServiceTest::AddKeywordWithDate( | 198 TemplateURL* TemplateURLServiceTest::AddKeywordWithDate( |
| 199 const std::string& short_name, | 199 const std::string& short_name, |
| 200 const std::string& keyword, | 200 const std::string& keyword, |
| 201 bool autogenerate_keyword, | |
| 202 const std::string& url, | 201 const std::string& url, |
| 203 const std::string& suggest_url, | 202 const std::string& suggest_url, |
| 204 const std::string& favicon_url, | 203 const std::string& favicon_url, |
| 205 bool safe_for_autoreplace, | 204 bool safe_for_autoreplace, |
| 206 const std::string& encodings, | 205 const std::string& encodings, |
| 207 Time date_created, | 206 Time date_created, |
| 208 Time last_modified) { | 207 Time last_modified) { |
| 209 TemplateURLData data; | 208 TemplateURLData data; |
| 210 data.short_name = UTF8ToUTF16(short_name); | 209 data.short_name = UTF8ToUTF16(short_name); |
| 211 data.SetKeyword(UTF8ToUTF16(keyword)); | 210 data.SetKeyword(UTF8ToUTF16(keyword)); |
| 212 data.SetAutogenerateKeyword(autogenerate_keyword); | |
| 213 data.SetURL(url); | 211 data.SetURL(url); |
| 214 data.suggestions_url = suggest_url; | 212 data.suggestions_url = suggest_url; |
| 215 data.favicon_url = GURL(favicon_url); | 213 data.favicon_url = GURL(favicon_url); |
| 216 data.safe_for_autoreplace = safe_for_autoreplace; | 214 data.safe_for_autoreplace = safe_for_autoreplace; |
| 217 base::SplitString(encodings, ';', &data.input_encodings); | 215 base::SplitString(encodings, ';', &data.input_encodings); |
| 218 data.date_created = date_created; | 216 data.date_created = date_created; |
| 219 data.last_modified = last_modified; | 217 data.last_modified = last_modified; |
| 220 TemplateURL* t_url = new TemplateURL(test_util_.profile(), data); | 218 TemplateURL* t_url = new TemplateURL(test_util_.profile(), data); |
| 221 model()->Add(t_url); | 219 model()->Add(t_url); |
| 222 EXPECT_NE(0, t_url->id()); | 220 EXPECT_NE(0, t_url->id()); |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 405 // updated. | 403 // updated. |
| 406 ASSERT_EQ(base::Time::FromDoubleT(1337), loaded_url->last_modified()); | 404 ASSERT_EQ(base::Time::FromDoubleT(1337), loaded_url->last_modified()); |
| 407 | 405 |
| 408 // Remove an element and verify it succeeded. | 406 // Remove an element and verify it succeeded. |
| 409 model()->Remove(loaded_url); | 407 model()->Remove(loaded_url); |
| 410 VerifyObserverCount(1); | 408 VerifyObserverCount(1); |
| 411 test_util_.ResetModel(true); | 409 test_util_.ResetModel(true); |
| 412 ASSERT_EQ(initial_count, model()->GetTemplateURLs().size()); | 410 ASSERT_EQ(initial_count, model()->GetTemplateURLs().size()); |
| 413 EXPECT_TRUE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("b")) == NULL); | 411 EXPECT_TRUE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("b")) == NULL); |
| 414 } | 412 } |
| 415 | 413 |
|
SteveT
2012/05/08 01:17:15
Optional: Would it be helpful at all to write a sm
Peter Kasting
2012/05/08 01:31:42
I think that helper is simple enough that the broa
| |
| 414 TEST_F(TemplateURLServiceTest, AddSameKeyword) { | |
| 415 test_util_.VerifyLoad(); | |
| 416 | |
| 417 AddKeywordWithDate("first", "keyword", "http://test1", std::string(), | |
| 418 std::string(), true, "UTF-8", Time(), Time()); | |
| 419 VerifyObserverCount(1); | |
| 420 | |
| 421 // Test what happens when we try to add a TemplateURL with the same keyword as | |
| 422 // one in the model. | |
| 423 TemplateURLData data; | |
| 424 data.short_name = ASCIIToUTF16("second"); | |
| 425 data.SetKeyword(ASCIIToUTF16("keyword")); | |
| 426 data.SetURL("http://test2"); | |
| 427 data.safe_for_autoreplace = false; | |
| 428 TemplateURL* t_url = new TemplateURL(test_util_.profile(), data); | |
| 429 model()->Add(t_url); | |
| 430 | |
| 431 // Because the old TemplateURL was replaceable and the new one wasn't, the new | |
| 432 // one should have replaced the old. | |
| 433 VerifyObserverCount(1); | |
| 434 EXPECT_EQ(t_url, model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); | |
| 435 EXPECT_EQ(ASCIIToUTF16("second"), t_url->short_name()); | |
| 436 EXPECT_EQ(ASCIIToUTF16("keyword"), t_url->keyword()); | |
| 437 EXPECT_FALSE(t_url->safe_for_autoreplace()); | |
| 438 | |
| 439 // Now try adding a replaceable TemplateURL. This should just delete the | |
| 440 // passed-in URL. | |
| 441 data.short_name = ASCIIToUTF16("third"); | |
| 442 data.SetURL("http://test3"); | |
| 443 data.safe_for_autoreplace = true; | |
| 444 model()->Add(new TemplateURL(test_util_.profile(), data)); | |
| 445 VerifyObserverCount(0); | |
| 446 EXPECT_EQ(t_url, model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); | |
| 447 EXPECT_EQ(ASCIIToUTF16("second"), t_url->short_name()); | |
| 448 EXPECT_EQ(ASCIIToUTF16("keyword"), t_url->keyword()); | |
| 449 EXPECT_FALSE(t_url->safe_for_autoreplace()); | |
| 450 | |
| 451 // Now try adding a non-replaceable TemplateURL again. This should uniquify | |
| 452 // the existing entry's keyword. | |
| 453 data.short_name = ASCIIToUTF16("fourth"); | |
| 454 data.SetURL("http://test4"); | |
| 455 data.safe_for_autoreplace = false; | |
| 456 TemplateURL* t_url2 = new TemplateURL(test_util_.profile(), data); | |
| 457 model()->Add(t_url2); | |
| 458 VerifyObserverCount(2); | |
| 459 EXPECT_EQ(t_url2, model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); | |
| 460 EXPECT_EQ(ASCIIToUTF16("fourth"), t_url2->short_name()); | |
| 461 EXPECT_EQ(ASCIIToUTF16("keyword"), t_url2->keyword()); | |
| 462 EXPECT_EQ(ASCIIToUTF16("second"), t_url->short_name()); | |
| 463 EXPECT_EQ(ASCIIToUTF16("test2"), t_url->keyword()); | |
| 464 } | |
| 465 | |
| 466 TEST_F(TemplateURLServiceTest, AddExtensionKeyword) { | |
| 467 TemplateURL* original1 = AddKeywordWithDate("replaceable", "keyword1", | |
| 468 "http://test1", std::string(), std::string(), true, "UTF-8", Time(), | |
| 469 Time()); | |
| 470 TemplateURL* original2 = AddKeywordWithDate("nonreplaceable", "keyword2", | |
| 471 "http://test2", std::string(), std::string(), false, "UTF-8", Time(), | |
| 472 Time()); | |
| 473 TemplateURL* original3 = AddKeywordWithDate("extension", "keyword3", | |
| 474 std::string(chrome::kExtensionScheme) + "://test3", std::string(), | |
| 475 std::string(), false, "UTF-8", Time(), Time()); | |
| 476 | |
| 477 // Add an extension keyword that conflicts with each of the above three | |
| 478 // keywords. | |
| 479 TemplateURLData data; | |
| 480 data.short_name = ASCIIToUTF16("test"); | |
| 481 data.SetKeyword(ASCIIToUTF16("keyword1")); | |
| 482 data.SetURL(std::string(chrome::kExtensionScheme) + "://test4"); | |
| 483 data.safe_for_autoreplace = false; | |
| 484 | |
| 485 // Extension keywords should override replaceable keywords. | |
| 486 TemplateURL* extension1 = new TemplateURL(test_util_.profile(), data); | |
| 487 model()->Add(extension1); | |
| 488 ASSERT_EQ(extension1, | |
| 489 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword1"))); | |
| 490 model()->Remove(extension1); | |
| 491 EXPECT_EQ(original1, | |
| 492 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword1"))); | |
| 493 | |
| 494 // They should not override non-replaceable keywords. | |
| 495 data.SetKeyword(ASCIIToUTF16("keyword2")); | |
| 496 TemplateURL* extension2 = new TemplateURL(test_util_.profile(), data); | |
| 497 model()->Add(extension2); | |
| 498 ASSERT_EQ(original2, | |
| 499 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword2"))); | |
| 500 model()->Remove(original2); | |
| 501 EXPECT_EQ(extension2, | |
| 502 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword2"))); | |
| 503 | |
| 504 // They should override extension keywords added earlier. | |
| 505 data.SetKeyword(ASCIIToUTF16("keyword3")); | |
| 506 TemplateURL* extension3 = new TemplateURL(test_util_.profile(), data); | |
| 507 model()->Add(extension3); | |
| 508 ASSERT_EQ(extension3, | |
| 509 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword3"))); | |
| 510 model()->Remove(extension3); | |
| 511 EXPECT_EQ(original3, | |
| 512 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword3"))); | |
| 513 } | |
| 514 | |
| 515 TEST_F(TemplateURLServiceTest, AddSameKeywordWithExtensionPresent) { | |
| 516 test_util_.VerifyLoad(); | |
| 517 | |
| 518 // Similar to the AddSameKeyword test, but with an extension keyword masking a | |
| 519 // replaceable TemplateURL. We should still do correct conflict resolution | |
| 520 // between the non-template URLs. | |
| 521 AddKeywordWithDate("replaceable", "keyword", "http://test1", std::string(), | |
| 522 std::string(), true, "UTF-8", Time(), Time()); | |
| 523 TemplateURL* extension = AddKeywordWithDate("extension", "keyword", | |
| 524 std::string(chrome::kExtensionScheme) + "://test2", std::string(), | |
| 525 std::string(), false, "UTF-8", Time(), Time()); | |
| 526 | |
| 527 // Adding another replaceable keyword should remove the existing one, but | |
| 528 // leave the extension as the associated URL for this keyword. | |
| 529 TemplateURLData data; | |
| 530 data.short_name = ASCIIToUTF16("name1"); | |
| 531 data.SetKeyword(ASCIIToUTF16("keyword")); | |
| 532 data.SetURL("http://test3"); | |
| 533 data.safe_for_autoreplace = true; | |
| 534 TemplateURL* t_url = new TemplateURL(test_util_.profile(), data); | |
| 535 model()->Add(t_url); | |
| 536 EXPECT_EQ(extension, | |
| 537 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); | |
| 538 EXPECT_TRUE(model()->GetTemplateURLForHost("test1") == NULL); | |
| 539 EXPECT_EQ(t_url, model()->GetTemplateURLForHost("test3")); | |
| 540 | |
| 541 // Adding a nonreplaceable keyword should remove the existing replaceable | |
| 542 // keyword and replace the extension as the associated URL for this keyword, | |
| 543 // but not evict the extension from the service entirely. | |
| 544 data.short_name = ASCIIToUTF16("name2"); | |
| 545 data.SetURL("http://test4"); | |
| 546 data.safe_for_autoreplace = false; | |
| 547 TemplateURL* t_url2 = new TemplateURL(test_util_.profile(), data); | |
| 548 model()->Add(t_url2); | |
| 549 EXPECT_EQ(t_url2, | |
| 550 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword"))); | |
| 551 EXPECT_TRUE(model()->GetTemplateURLForHost("test3") == NULL); | |
| 552 // Note that extensions don't use host-based keywords, so we can't check that | |
| 553 // the extension is still in the model using GetTemplateURLForHost(); and we | |
| 554 // have to create a full-fledged Extension to use | |
| 555 // GetTemplateURLForExtension(), which is annoying, so just grab all the URLs | |
| 556 // from the model and search for |extension| within them. | |
| 557 TemplateURLService::TemplateURLVector template_urls( | |
| 558 model()->GetTemplateURLs()); | |
| 559 EXPECT_FALSE(std::find(template_urls.begin(), template_urls.end(), | |
| 560 extension) == template_urls.end()); | |
| 561 } | |
| 562 | |
| 416 TEST_F(TemplateURLServiceTest, GenerateKeyword) { | 563 TEST_F(TemplateURLServiceTest, GenerateKeyword) { |
| 417 ASSERT_EQ(string16(), TemplateURLService::GenerateKeyword(GURL(), true)); | |
| 418 // Shouldn't generate keywords for https. | |
| 419 ASSERT_EQ(string16(), | |
| 420 TemplateURLService::GenerateKeyword(GURL("https://blah"), true)); | |
| 421 ASSERT_EQ(ASCIIToUTF16("foo"), | 564 ASSERT_EQ(ASCIIToUTF16("foo"), |
| 422 TemplateURLService::GenerateKeyword(GURL("http://foo"), true)); | 565 TemplateURLService::GenerateKeyword(GURL("http://foo"))); |
| 423 // www. should be stripped. | 566 // www. should be stripped. |
| 424 ASSERT_EQ(ASCIIToUTF16("foo"), | 567 ASSERT_EQ(ASCIIToUTF16("foo"), |
| 425 TemplateURLService::GenerateKeyword(GURL("http://www.foo"), true)); | 568 TemplateURLService::GenerateKeyword(GURL("http://www.foo"))); |
| 426 // Shouldn't generate keywords with paths, if autodetected. | 569 // Make sure we don't get a trailing '/'. |
| 427 ASSERT_EQ(string16(), | |
| 428 TemplateURLService::GenerateKeyword(GURL("http://blah/foo"), true)); | |
| 429 ASSERT_EQ(ASCIIToUTF16("blah"), | 570 ASSERT_EQ(ASCIIToUTF16("blah"), |
| 430 TemplateURLService::GenerateKeyword(GURL("http://blah/foo"), | 571 TemplateURLService::GenerateKeyword(GURL("http://blah/"))); |
| 431 false)); | |
| 432 // FTP shouldn't generate a keyword. | |
| 433 ASSERT_EQ(string16(), | |
| 434 TemplateURLService::GenerateKeyword(GURL("ftp://blah/"), true)); | |
| 435 // Make sure we don't get a trailing / | |
| 436 ASSERT_EQ(ASCIIToUTF16("blah"), | |
| 437 TemplateURLService::GenerateKeyword(GURL("http://blah/"), true)); | |
| 438 // Don't generate the empty string. | 572 // Don't generate the empty string. |
| 439 ASSERT_EQ(ASCIIToUTF16("www"), | 573 ASSERT_EQ(ASCIIToUTF16("www"), |
| 440 TemplateURLService::GenerateKeyword(GURL("http://www."), false)); | 574 TemplateURLService::GenerateKeyword(GURL("http://www."))); |
| 441 } | 575 } |
| 442 | 576 |
| 443 TEST_F(TemplateURLServiceTest, GenerateSearchURL) { | 577 TEST_F(TemplateURLServiceTest, GenerateSearchURL) { |
| 444 scoped_refptr<TestGenerateSearchURL> test_generate_search_url( | 578 scoped_refptr<TestGenerateSearchURL> test_generate_search_url( |
| 445 new TestGenerateSearchURL(NULL)); | 579 new TestGenerateSearchURL(NULL)); |
| 446 test_generate_search_url->RunTest(); | 580 test_generate_search_url->RunTest(); |
| 447 EXPECT_TRUE(test_generate_search_url->passed()); | 581 EXPECT_TRUE(test_generate_search_url->passed()); |
| 448 } | 582 } |
| 449 | 583 |
| 450 TEST_F(TemplateURLServiceTest, GenerateSearchURLUsingTermsData) { | 584 TEST_F(TemplateURLServiceTest, GenerateSearchURLUsingTermsData) { |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 464 | 598 |
| 465 TEST_F(TemplateURLServiceTest, ClearBrowsingData_Keywords) { | 599 TEST_F(TemplateURLServiceTest, ClearBrowsingData_Keywords) { |
| 466 Time now = Time::Now(); | 600 Time now = Time::Now(); |
| 467 TimeDelta one_day = TimeDelta::FromDays(1); | 601 TimeDelta one_day = TimeDelta::FromDays(1); |
| 468 Time month_ago = now - TimeDelta::FromDays(30); | 602 Time month_ago = now - TimeDelta::FromDays(30); |
| 469 | 603 |
| 470 // Nothing has been added. | 604 // Nothing has been added. |
| 471 EXPECT_EQ(0U, model()->GetTemplateURLs().size()); | 605 EXPECT_EQ(0U, model()->GetTemplateURLs().size()); |
| 472 | 606 |
| 473 // Create one with a 0 time. | 607 // Create one with a 0 time. |
| 474 AddKeywordWithDate("name1", "key1", false, "http://foo1", "http://suggest1", | 608 AddKeywordWithDate("name1", "key1", "http://foo1", "http://suggest1", |
| 475 "http://icon1", true, "UTF-8;UTF-16", Time(), Time()); | 609 "http://icon1", true, "UTF-8;UTF-16", Time(), Time()); |
| 476 // Create one for now and +/- 1 day. | 610 // Create one for now and +/- 1 day. |
| 477 AddKeywordWithDate("name2", "key2", false, "http://foo2", "http://suggest2", | 611 AddKeywordWithDate("name2", "key2", "http://foo2", "http://suggest2", |
| 478 "http://icon2", true, "UTF-8;UTF-16", now - one_day, Time()); | 612 "http://icon2", true, "UTF-8;UTF-16", now - one_day, Time()); |
| 479 AddKeywordWithDate("name3", "key3", false, "http://foo3", std::string(), | 613 AddKeywordWithDate("name3", "key3", "http://foo3", std::string(), |
| 480 std::string(), true, std::string(), now, Time()); | 614 std::string(), true, std::string(), now, Time()); |
| 481 AddKeywordWithDate("name4", "key4", false, "http://foo4", std::string(), | 615 AddKeywordWithDate("name4", "key4", "http://foo4", std::string(), |
| 482 std::string(), true, std::string(), now + one_day, Time()); | 616 std::string(), true, std::string(), now + one_day, Time()); |
| 483 // Try the other three states. | 617 // Try the other three states. |
| 484 AddKeywordWithDate("name5", "key5", false, "http://foo5", "http://suggest5", | 618 AddKeywordWithDate("name5", "key5", "http://foo5", "http://suggest5", |
| 485 "http://icon5", false, "UTF-8;UTF-16", now, Time()); | 619 "http://icon5", false, "UTF-8;UTF-16", now, Time()); |
| 486 AddKeywordWithDate("name6", "key6", false, "http://foo6", "http://suggest6", | 620 AddKeywordWithDate("name6", "key6", "http://foo6", "http://suggest6", |
| 487 "http://icon6", false, "UTF-8;UTF-16", month_ago, Time()); | 621 "http://icon6", false, "UTF-8;UTF-16", month_ago, Time()); |
| 488 | 622 |
| 489 // We just added a few items, validate them. | 623 // We just added a few items, validate them. |
| 490 EXPECT_EQ(6U, model()->GetTemplateURLs().size()); | 624 EXPECT_EQ(6U, model()->GetTemplateURLs().size()); |
| 491 | 625 |
| 492 // Try removing from current timestamp. This should delete the one in the | 626 // Try removing from current timestamp. This should delete the one in the |
| 493 // future and one very recent one. | 627 // future and one very recent one. |
| 494 model()->RemoveAutoGeneratedSince(now); | 628 model()->RemoveAutoGeneratedSince(now); |
| 495 EXPECT_EQ(4U, model()->GetTemplateURLs().size()); | 629 EXPECT_EQ(4U, model()->GetTemplateURLs().size()); |
| 496 | 630 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 522 | 656 |
| 523 TEST_F(TemplateURLServiceTest, ClearBrowsingData_KeywordsForOrigin) { | 657 TEST_F(TemplateURLServiceTest, ClearBrowsingData_KeywordsForOrigin) { |
| 524 Time now = Time::Now(); | 658 Time now = Time::Now(); |
| 525 TimeDelta one_day = TimeDelta::FromDays(1); | 659 TimeDelta one_day = TimeDelta::FromDays(1); |
| 526 Time month_ago = now - TimeDelta::FromDays(30); | 660 Time month_ago = now - TimeDelta::FromDays(30); |
| 527 | 661 |
| 528 // Nothing has been added. | 662 // Nothing has been added. |
| 529 EXPECT_EQ(0U, model()->GetTemplateURLs().size()); | 663 EXPECT_EQ(0U, model()->GetTemplateURLs().size()); |
| 530 | 664 |
| 531 // Create one for now and +/- 1 day. | 665 // Create one for now and +/- 1 day. |
| 532 AddKeywordWithDate("name1", "key1", false, "http://foo1", "http://suggest1", | 666 AddKeywordWithDate("name1", "key1", "http://foo1", "http://suggest1", |
| 533 "http://icon2", true, "UTF-8;UTF-16", now - one_day, Time()); | 667 "http://icon2", true, "UTF-8;UTF-16", now - one_day, Time()); |
| 534 AddKeywordWithDate("name2", "key2", false, "http://foo2", std::string(), | 668 AddKeywordWithDate("name2", "key2", "http://foo2", std::string(), |
| 535 std::string(), true, std::string(), now, Time()); | 669 std::string(), true, std::string(), now, Time()); |
| 536 AddKeywordWithDate("name3", "key3", false, "http://foo3", std::string(), | 670 AddKeywordWithDate("name3", "key3", "http://foo3", std::string(), |
| 537 std::string(), true, std::string(), now + one_day, Time()); | 671 std::string(), true, std::string(), now + one_day, Time()); |
| 538 | 672 |
| 539 // We just added a few items, validate them. | 673 // We just added a few items, validate them. |
| 540 EXPECT_EQ(3U, model()->GetTemplateURLs().size()); | 674 EXPECT_EQ(3U, model()->GetTemplateURLs().size()); |
| 541 | 675 |
| 542 // Try removing foo2. This should delete foo2, but leave foo1 and 3 untouched. | 676 // Try removing foo2. This should delete foo2, but leave foo1 and 3 untouched. |
| 543 model()->RemoveAutoGeneratedForOriginBetween(GURL("http://foo2"), month_ago, | 677 model()->RemoveAutoGeneratedForOriginBetween(GURL("http://foo2"), month_ago, |
| 544 now + one_day); | 678 now + one_day); |
| 545 EXPECT_EQ(2U, model()->GetTemplateURLs().size()); | 679 EXPECT_EQ(2U, model()->GetTemplateURLs().size()); |
| 546 EXPECT_EQ(ASCIIToUTF16("key1"), model()->GetTemplateURLs()[0]->keyword()); | 680 EXPECT_EQ(ASCIIToUTF16("key1"), model()->GetTemplateURLs()[0]->keyword()); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 611 const TemplateURL* read_url = model()->GetTemplateURLForKeyword(new_keyword); | 745 const TemplateURL* read_url = model()->GetTemplateURLForKeyword(new_keyword); |
| 612 ASSERT_TRUE(read_url); | 746 ASSERT_TRUE(read_url); |
| 613 AssertEquals(*cloned_url, *read_url); | 747 AssertEquals(*cloned_url, *read_url); |
| 614 ASSERT_EQ(base::Time::FromDoubleT(1337), read_url->last_modified()); | 748 ASSERT_EQ(base::Time::FromDoubleT(1337), read_url->last_modified()); |
| 615 } | 749 } |
| 616 | 750 |
| 617 TEST_F(TemplateURLServiceTest, DefaultSearchProvider) { | 751 TEST_F(TemplateURLServiceTest, DefaultSearchProvider) { |
| 618 // Add a new TemplateURL. | 752 // Add a new TemplateURL. |
| 619 test_util_.VerifyLoad(); | 753 test_util_.VerifyLoad(); |
| 620 const size_t initial_count = model()->GetTemplateURLs().size(); | 754 const size_t initial_count = model()->GetTemplateURLs().size(); |
| 621 TemplateURL* t_url = AddKeywordWithDate("name1", "key1", false, | 755 TemplateURL* t_url = AddKeywordWithDate("name1", "key1", |
| 622 "http://foo1/{searchTerms}", "http://sugg1", "http://icon1", true, | 756 "http://foo1/{searchTerms}", "http://sugg1", "http://icon1", true, |
| 623 "UTF-8;UTF-16", Time(), Time()); | 757 "UTF-8;UTF-16", Time(), Time()); |
| 624 test_util_.ResetObserverCount(); | 758 test_util_.ResetObserverCount(); |
| 625 | 759 |
| 626 model()->SetDefaultSearchProvider(t_url); | 760 model()->SetDefaultSearchProvider(t_url); |
| 627 ASSERT_EQ(t_url, model()->GetDefaultSearchProvider()); | 761 ASSERT_EQ(t_url, model()->GetDefaultSearchProvider()); |
| 628 ASSERT_TRUE(t_url->safe_for_autoreplace()); | 762 ASSERT_TRUE(t_url->safe_for_autoreplace()); |
| 629 ASSERT_TRUE(t_url->show_in_default_list()); | 763 ASSERT_TRUE(t_url->show_in_default_list()); |
| 630 | 764 |
| 631 // Setting the default search provider should have caused notification. | 765 // Setting the default search provider should have caused notification. |
| 632 VerifyObserverCount(1); | 766 VerifyObserverCount(1); |
| 633 test_util_.BlockTillServiceProcessesRequests(); | 767 test_util_.BlockTillServiceProcessesRequests(); |
| 634 | 768 |
| 635 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->profile(), | 769 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->profile(), |
| 636 t_url->data())); | 770 t_url->data())); |
| 637 | 771 |
| 638 // Make sure when we reload we get a default search provider. | 772 // Make sure when we reload we get a default search provider. |
| 639 test_util_.ResetModel(true); | 773 test_util_.ResetModel(true); |
| 640 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); | 774 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); |
| 641 ASSERT_TRUE(model()->GetDefaultSearchProvider()); | 775 ASSERT_TRUE(model()->GetDefaultSearchProvider()); |
| 642 AssertEquals(*cloned_url, *model()->GetDefaultSearchProvider()); | 776 AssertEquals(*cloned_url, *model()->GetDefaultSearchProvider()); |
| 643 } | 777 } |
| 644 | 778 |
| 645 TEST_F(TemplateURLServiceTest, TemplateURLWithNoKeyword) { | |
| 646 test_util_.VerifyLoad(); | |
| 647 | |
| 648 const size_t initial_count = model()->GetTemplateURLs().size(); | |
| 649 | |
| 650 AddKeywordWithDate("name1", std::string(), false, "http://foo1", | |
| 651 "http://sugg1", "http://icon1", true, "UTF-8;UTF-16", Time(), Time()); | |
| 652 | |
| 653 // We just added a few items, validate them. | |
| 654 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); | |
| 655 | |
| 656 // Reload the model from the database and make sure we get the url back. | |
| 657 test_util_.ResetModel(true); | |
| 658 | |
| 659 ASSERT_EQ(1 + initial_count, model()->GetTemplateURLs().size()); | |
| 660 | |
| 661 bool found_keyword = false; | |
| 662 for (size_t i = 0; i < initial_count + 1; ++i) { | |
| 663 if (model()->GetTemplateURLs()[i]->keyword().empty()) { | |
| 664 found_keyword = true; | |
| 665 break; | |
| 666 } | |
| 667 } | |
| 668 ASSERT_TRUE(found_keyword); | |
| 669 } | |
| 670 | |
| 671 TEST_F(TemplateURLServiceTest, CantReplaceWithSameKeyword) { | 779 TEST_F(TemplateURLServiceTest, CantReplaceWithSameKeyword) { |
| 672 test_util_.ChangeModelToLoadState(); | 780 test_util_.ChangeModelToLoadState(); |
| 673 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"), GURL(), NULL)); | 781 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"), GURL(), NULL)); |
| 674 TemplateURL* t_url = AddKeywordWithDate("name1", "foo", false, "http://foo1", | 782 TemplateURL* t_url = AddKeywordWithDate("name1", "foo", "http://foo1", |
| 675 "http://sugg1", "http://icon1", true, "UTF-8;UTF-16", Time(), Time()); | 783 "http://sugg1", "http://icon1", true, "UTF-8;UTF-16", Time(), Time()); |
| 676 | 784 |
| 677 // Can still replace, newly added template url is marked safe to replace. | 785 // Can still replace, newly added template url is marked safe to replace. |
| 678 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"), | 786 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"), |
| 679 GURL("http://foo2"), NULL)); | 787 GURL("http://foo2"), NULL)); |
| 680 | 788 |
| 681 // ResetTemplateURL marks the TemplateURL as unsafe to replace, so it should | 789 // ResetTemplateURL marks the TemplateURL as unsafe to replace, so it should |
| 682 // no longer be replaceable. | 790 // no longer be replaceable. |
| 683 model()->ResetTemplateURL(t_url, t_url->short_name(), t_url->keyword(), | 791 model()->ResetTemplateURL(t_url, t_url->short_name(), t_url->keyword(), |
| 684 t_url->url()); | 792 t_url->url()); |
| 685 | 793 |
| 686 ASSERT_FALSE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"), | 794 ASSERT_FALSE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"), |
| 687 GURL("http://foo2"), NULL)); | 795 GURL("http://foo2"), NULL)); |
| 688 } | 796 } |
| 689 | 797 |
| 690 TEST_F(TemplateURLServiceTest, CantReplaceWithSameHosts) { | 798 TEST_F(TemplateURLServiceTest, CantReplaceWithSameHosts) { |
| 691 test_util_.ChangeModelToLoadState(); | 799 test_util_.ChangeModelToLoadState(); |
| 692 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"), | 800 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"), |
| 693 GURL("http://foo.com"), NULL)); | 801 GURL("http://foo.com"), NULL)); |
| 694 TemplateURL* t_url = AddKeywordWithDate("name1", "foo", false, | 802 TemplateURL* t_url = AddKeywordWithDate("name1", "foo", "http://foo.com", |
| 695 "http://foo.com", "http://sugg1", "http://icon1", true, "UTF-8;UTF-16", | 803 "http://sugg1", "http://icon1", true, "UTF-8;UTF-16", Time(), Time()); |
| 696 Time(), Time()); | |
| 697 | 804 |
| 698 // Can still replace, newly added template url is marked safe to replace. | 805 // Can still replace, newly added template url is marked safe to replace. |
| 699 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("bar"), | 806 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("bar"), |
| 700 GURL("http://foo.com"), NULL)); | 807 GURL("http://foo.com"), NULL)); |
| 701 | 808 |
| 702 // ResetTemplateURL marks the TemplateURL as unsafe to replace, so it should | 809 // ResetTemplateURL marks the TemplateURL as unsafe to replace, so it should |
| 703 // no longer be replaceable. | 810 // no longer be replaceable. |
| 704 model()->ResetTemplateURL(t_url, t_url->short_name(), t_url->keyword(), | 811 model()->ResetTemplateURL(t_url, t_url->short_name(), t_url->keyword(), |
| 705 t_url->url()); | 812 t_url->url()); |
| 706 | 813 |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 813 { "http://foo/", string16() }, | 920 { "http://foo/", string16() }, |
| 814 { "http://foo/foo?q=xx", string16() }, | 921 { "http://foo/foo?q=xx", string16() }, |
| 815 { "http://x/bar?q=xx", string16() }, | 922 { "http://x/bar?q=xx", string16() }, |
| 816 { "http://x/foo?y=xx", string16() }, | 923 { "http://x/foo?y=xx", string16() }, |
| 817 { "http://x/foo?q=xx", ASCIIToUTF16("xx") }, | 924 { "http://x/foo?q=xx", ASCIIToUTF16("xx") }, |
| 818 { "http://x/foo?a=b&q=xx", ASCIIToUTF16("xx") }, | 925 { "http://x/foo?a=b&q=xx", ASCIIToUTF16("xx") }, |
| 819 { "http://x/foo?q=b&q=xx", string16() }, | 926 { "http://x/foo?q=b&q=xx", string16() }, |
| 820 }; | 927 }; |
| 821 | 928 |
| 822 test_util_.ChangeModelToLoadState(); | 929 test_util_.ChangeModelToLoadState(); |
| 823 AddKeywordWithDate("name", "x", false, "http://x/foo?q={searchTerms}", | 930 AddKeywordWithDate("name", "x", "http://x/foo?q={searchTerms}", |
| 824 "http://sugg1", "http://icon1", false, "UTF-8;UTF-16", Time(), Time()); | 931 "http://sugg1", "http://icon1", false, "UTF-8;UTF-16", Time(), Time()); |
| 825 | 932 |
| 826 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { | 933 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { |
| 827 history::URLVisitedDetails details; | 934 history::URLVisitedDetails details; |
| 828 details.row = history::URLRow(GURL(data[i].url)); | 935 details.row = history::URLRow(GURL(data[i].url)); |
| 829 details.transition = content::PageTransitionFromInt(0); | 936 details.transition = content::PageTransitionFromInt(0); |
| 830 model()->UpdateKeywordSearchTermsForURL(details); | 937 model()->UpdateKeywordSearchTermsForURL(details); |
| 831 EXPECT_EQ(data[i].term, test_util_.GetAndClearSearchTerm()); | 938 EXPECT_EQ(data[i].term, test_util_.GetAndClearSearchTerm()); |
| 832 } | 939 } |
| 833 } | 940 } |
| 834 | 941 |
| 835 TEST_F(TemplateURLServiceTest, DontUpdateKeywordSearchForNonReplaceable) { | 942 TEST_F(TemplateURLServiceTest, DontUpdateKeywordSearchForNonReplaceable) { |
| 836 struct TestData { | 943 struct TestData { |
| 837 const std::string url; | 944 const std::string url; |
| 838 } data[] = { | 945 } data[] = { |
| 839 { "http://foo/" }, | 946 { "http://foo/" }, |
| 840 { "http://x/bar?q=xx" }, | 947 { "http://x/bar?q=xx" }, |
| 841 { "http://x/foo?y=xx" }, | 948 { "http://x/foo?y=xx" }, |
| 842 }; | 949 }; |
| 843 | 950 |
| 844 test_util_.ChangeModelToLoadState(); | 951 test_util_.ChangeModelToLoadState(); |
| 845 AddKeywordWithDate("name", "x", false, "http://x/foo", "http://sugg1", | 952 AddKeywordWithDate("name", "x", "http://x/foo", "http://sugg1", |
| 846 "http://icon1", false, "UTF-8;UTF-16", Time(), Time()); | 953 "http://icon1", false, "UTF-8;UTF-16", Time(), Time()); |
| 847 | 954 |
| 848 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { | 955 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { |
| 849 history::URLVisitedDetails details; | 956 history::URLVisitedDetails details; |
| 850 details.row = history::URLRow(GURL(data[i].url)); | 957 details.row = history::URLRow(GURL(data[i].url)); |
| 851 details.transition = content::PageTransitionFromInt(0); | 958 details.transition = content::PageTransitionFromInt(0); |
| 852 model()->UpdateKeywordSearchTermsForURL(details); | 959 model()->UpdateKeywordSearchTermsForURL(details); |
| 853 ASSERT_EQ(string16(), test_util_.GetAndClearSearchTerm()); | 960 ASSERT_EQ(string16(), test_util_.GetAndClearSearchTerm()); |
| 854 } | 961 } |
| 855 } | 962 } |
| 856 | 963 |
| 857 TEST_F(TemplateURLServiceTest, ChangeGoogleBaseValue) { | 964 TEST_F(TemplateURLServiceTest, ChangeGoogleBaseValue) { |
| 858 // NOTE: Do not do a VerifyLoad() here as it will load the prepopulate data, | 965 // NOTE: Do not do a VerifyLoad() here as it will load the prepopulate data, |
| 859 // which also has a {google:baseURL} keyword in it, which will confuse this | 966 // which also has a {google:baseURL} keyword in it, which will confuse this |
| 860 // test. | 967 // test. |
| 861 test_util_.ChangeModelToLoadState(); | 968 test_util_.ChangeModelToLoadState(); |
| 862 test_util_.SetGoogleBaseURL(GURL("http://google.com/")); | 969 test_util_.SetGoogleBaseURL(GURL("http://google.com/")); |
| 863 const TemplateURL* t_url = AddKeywordWithDate("name", "google.com", true, | 970 const TemplateURL* t_url = AddKeywordWithDate("name", "google.com", |
| 864 "{google:baseURL}?q={searchTerms}", "http://sugg1", "http://icon1", | 971 "{google:baseURL}?q={searchTerms}", "http://sugg1", "http://icon1", |
| 865 false, "UTF-8;UTF-16", Time(), Time()); | 972 false, "UTF-8;UTF-16", Time(), Time()); |
| 866 ASSERT_EQ(t_url, model()->GetTemplateURLForHost("google.com")); | 973 ASSERT_EQ(t_url, model()->GetTemplateURLForHost("google.com")); |
| 867 EXPECT_EQ("google.com", t_url->url_ref().GetHost()); | 974 EXPECT_EQ("google.com", t_url->url_ref().GetHost()); |
| 868 EXPECT_EQ(ASCIIToUTF16("google.com"), t_url->keyword()); | 975 EXPECT_EQ(ASCIIToUTF16("google.com"), t_url->keyword()); |
| 869 | 976 |
| 870 // Change the Google base url. | 977 // Change the Google base url. |
| 871 test_util_.ResetObserverCount(); | 978 test_util_.ResetObserverCount(); |
| 872 test_util_.SetGoogleBaseURL(GURL("http://google.co.uk/")); | 979 test_util_.SetGoogleBaseURL(GURL("http://google.co.uk/")); |
| 873 VerifyObserverCount(1); | 980 VerifyObserverCount(1); |
| 874 | 981 |
| 875 // Make sure the host->TemplateURL map was updated appropriately. | 982 // Make sure the host->TemplateURL map was updated appropriately. |
| 876 ASSERT_EQ(t_url, model()->GetTemplateURLForHost("google.co.uk")); | 983 ASSERT_EQ(t_url, model()->GetTemplateURLForHost("google.co.uk")); |
| 877 EXPECT_TRUE(model()->GetTemplateURLForHost("google.com") == NULL); | 984 EXPECT_TRUE(model()->GetTemplateURLForHost("google.com") == NULL); |
| 878 EXPECT_EQ("google.co.uk", t_url->url_ref().GetHost()); | 985 EXPECT_EQ("google.co.uk", t_url->url_ref().GetHost()); |
| 879 EXPECT_EQ(ASCIIToUTF16("google.co.uk"), t_url->keyword()); | 986 EXPECT_EQ(ASCIIToUTF16("google.co.uk"), t_url->keyword()); |
| 880 EXPECT_EQ("http://google.co.uk/?q=x", t_url->url_ref().ReplaceSearchTerms( | 987 EXPECT_EQ("http://google.co.uk/?q=x", t_url->url_ref().ReplaceSearchTerms( |
| 881 ASCIIToUTF16("x"), TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); | 988 ASCIIToUTF16("x"), TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); |
| 989 | |
| 990 // Now add a manual entry and then change the Google base URL such that the | |
| 991 // autogenerated Google search keyword would conflict. | |
| 992 TemplateURL* manual = AddKeywordWithDate("manual", "google.de", | |
| 993 "http://google.de/search?q={searchTerms}", std::string(), std::string(), | |
| 994 false, "UTF-8", Time(), Time()); | |
| 995 test_util_.SetGoogleBaseURL(GURL("http://google.de")); | |
| 996 | |
| 997 // Verify that the manual entry is untouched, and the autogenerated keyword | |
| 998 // has not changed. | |
| 999 ASSERT_EQ(manual, | |
| 1000 model()->GetTemplateURLForKeyword(ASCIIToUTF16("google.de"))); | |
| 1001 EXPECT_EQ("google.de", manual->url_ref().GetHost()); | |
| 1002 ASSERT_EQ(t_url, | |
| 1003 model()->GetTemplateURLForKeyword(ASCIIToUTF16("google.co.uk"))); | |
| 1004 EXPECT_EQ("google.de", t_url->url_ref().GetHost()); | |
| 1005 EXPECT_EQ(ASCIIToUTF16("google.co.uk"), t_url->keyword()); | |
| 1006 | |
| 1007 // Change the base URL again and verify that the autogenerated keyword follows | |
| 1008 // even though it didn't match the base URL, while the manual entry is still | |
| 1009 // untouched. | |
| 1010 test_util_.SetGoogleBaseURL(GURL("http://google.fr/")); | |
| 1011 ASSERT_EQ(manual, model()->GetTemplateURLForHost("google.de")); | |
| 1012 EXPECT_EQ("google.de", manual->url_ref().GetHost()); | |
| 1013 EXPECT_EQ(ASCIIToUTF16("google.de"), manual->keyword()); | |
| 1014 ASSERT_EQ(t_url, model()->GetTemplateURLForHost("google.fr")); | |
| 1015 EXPECT_TRUE(model()->GetTemplateURLForHost("google.co.uk") == NULL); | |
| 1016 EXPECT_EQ("google.fr", t_url->url_ref().GetHost()); | |
| 1017 EXPECT_EQ(ASCIIToUTF16("google.fr"), t_url->keyword()); | |
| 882 } | 1018 } |
| 883 | 1019 |
| 884 struct QueryHistoryCallbackImpl { | 1020 struct QueryHistoryCallbackImpl { |
| 885 QueryHistoryCallbackImpl() : success(false) {} | 1021 QueryHistoryCallbackImpl() : success(false) {} |
| 886 | 1022 |
| 887 void Callback(HistoryService::Handle handle, | 1023 void Callback(HistoryService::Handle handle, |
| 888 bool success, | 1024 bool success, |
| 889 const history::URLRow* row, | 1025 const history::URLRow* row, |
| 890 history::VisitVector* visits) { | 1026 history::VisitVector* visits) { |
| 891 this->success = success; | 1027 this->success = success; |
| 892 if (row) | 1028 if (row) |
| 893 this->row = *row; | 1029 this->row = *row; |
| 894 if (visits) | 1030 if (visits) |
| 895 this->visits = *visits; | 1031 this->visits = *visits; |
| 896 } | 1032 } |
| 897 | 1033 |
| 898 bool success; | 1034 bool success; |
| 899 history::URLRow row; | 1035 history::URLRow row; |
| 900 history::VisitVector visits; | 1036 history::VisitVector visits; |
| 901 }; | 1037 }; |
| 902 | 1038 |
| 903 // Make sure TemplateURLService generates a KEYWORD_GENERATED visit for | 1039 // Make sure TemplateURLService generates a KEYWORD_GENERATED visit for |
| 904 // KEYWORD visits. | 1040 // KEYWORD visits. |
| 905 TEST_F(TemplateURLServiceTest, GenerateVisitOnKeyword) { | 1041 TEST_F(TemplateURLServiceTest, GenerateVisitOnKeyword) { |
| 906 test_util_.VerifyLoad(); | 1042 test_util_.VerifyLoad(); |
| 907 test_util_.profile()->CreateHistoryService(true, false); | 1043 test_util_.profile()->CreateHistoryService(true, false); |
| 908 | 1044 |
| 909 // Create a keyword. | 1045 // Create a keyword. |
| 910 TemplateURL* t_url = AddKeywordWithDate("keyword", "keyword", false, | 1046 TemplateURL* t_url = AddKeywordWithDate("keyword", "keyword", |
| 911 "http://foo.com/foo?query={searchTerms}", "http://sugg1", "http://icon1", | 1047 "http://foo.com/foo?query={searchTerms}", "http://sugg1", "http://icon1", |
| 912 true, "UTF-8;UTF-16", base::Time::Now(), base::Time::Now()); | 1048 true, "UTF-8;UTF-16", base::Time::Now(), base::Time::Now()); |
| 913 | 1049 |
| 914 // Add a visit that matches the url of the keyword. | 1050 // Add a visit that matches the url of the keyword. |
| 915 HistoryService* history = | 1051 HistoryService* history = |
| 916 test_util_.profile()->GetHistoryService(Profile::EXPLICIT_ACCESS); | 1052 test_util_.profile()->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 917 history->AddPage( | 1053 history->AddPage( |
| 918 GURL(t_url->url_ref().ReplaceSearchTerms(ASCIIToUTF16("blah"), | 1054 GURL(t_url->url_ref().ReplaceSearchTerms(ASCIIToUTF16("blah"), |
| 919 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())), | 1055 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())), |
| 920 NULL, 0, GURL(), content::PAGE_TRANSITION_KEYWORD, | 1056 NULL, 0, GURL(), content::PAGE_TRANSITION_KEYWORD, |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1107 test_util_.BlockTillServiceProcessesRequests(); | 1243 test_util_.BlockTillServiceProcessesRequests(); |
| 1108 | 1244 |
| 1109 // Reset the model and load it. There should be a usable default search | 1245 // Reset the model and load it. There should be a usable default search |
| 1110 // provider. | 1246 // provider. |
| 1111 test_util_.ResetModel(true); | 1247 test_util_.ResetModel(true); |
| 1112 | 1248 |
| 1113 ASSERT_TRUE(model()->GetDefaultSearchProvider()); | 1249 ASSERT_TRUE(model()->GetDefaultSearchProvider()); |
| 1114 EXPECT_TRUE(model()->GetDefaultSearchProvider()->SupportsReplacement()); | 1250 EXPECT_TRUE(model()->GetDefaultSearchProvider()->SupportsReplacement()); |
| 1115 } | 1251 } |
| 1116 | 1252 |
| 1117 // Make sure that the load does update of auto-keywords correctly. | |
| 1118 // This test basically verifies that no asserts or crashes occur | |
| 1119 // during this operation. | |
| 1120 TEST_F(TemplateURLServiceTest, LoadDoesAutoKeywordUpdate) { | |
| 1121 string16 prepopulated_url; | |
| 1122 scoped_ptr<TemplateURL> t_url( | |
| 1123 CreateReplaceablePreloadedTemplateURL(false, 0, &prepopulated_url)); | |
| 1124 TemplateURLData data(t_url->data()); | |
| 1125 data.SetAutogenerateKeyword(true); | |
| 1126 data.SetURL("{google:baseURL}?q={searchTerms}"); | |
| 1127 | |
| 1128 // Then add it to the model and save it all. | |
| 1129 test_util_.ChangeModelToLoadState(); | |
| 1130 model()->Add(new TemplateURL(test_util_.profile(), data)); | |
| 1131 test_util_.BlockTillServiceProcessesRequests(); | |
| 1132 | |
| 1133 // Now reload the model and verify that the merge updates the url. | |
| 1134 test_util_.ResetModel(true); | |
| 1135 | |
| 1136 // Wait for any saves to finish. | |
| 1137 test_util_.BlockTillServiceProcessesRequests(); | |
| 1138 } | |
| 1139 | |
| 1140 // Simulates failing to load the webdb and makes sure the default search | 1253 // Simulates failing to load the webdb and makes sure the default search |
| 1141 // provider is valid. | 1254 // provider is valid. |
| 1142 TEST_F(TemplateURLServiceTest, FailedInit) { | 1255 TEST_F(TemplateURLServiceTest, FailedInit) { |
| 1143 test_util_.VerifyLoad(); | 1256 test_util_.VerifyLoad(); |
| 1144 | 1257 |
| 1145 test_util_.ClearModel(); | 1258 test_util_.ClearModel(); |
| 1146 WebDataService* web_service = | 1259 WebDataService* web_service = |
| 1147 test_util_.profile()->GetWebDataService(Profile::EXPLICIT_ACCESS); | 1260 test_util_.profile()->GetWebDataService(Profile::EXPLICIT_ACCESS); |
| 1148 web_service->UnloadDatabase(); | 1261 web_service->UnloadDatabase(); |
| 1149 web_service->set_failed_init(true); | 1262 web_service->set_failed_init(true); |
| 1150 | 1263 |
| 1151 test_util_.ResetModel(false); | 1264 test_util_.ResetModel(false); |
| 1152 model()->Load(); | 1265 model()->Load(); |
| 1153 test_util_.BlockTillServiceProcessesRequests(); | 1266 test_util_.BlockTillServiceProcessesRequests(); |
| 1154 | 1267 |
| 1155 ASSERT_TRUE(model()->GetDefaultSearchProvider()); | 1268 ASSERT_TRUE(model()->GetDefaultSearchProvider()); |
| 1156 } | 1269 } |
| 1157 | 1270 |
| 1158 // Verifies that if the default search URL preference is managed, we report | 1271 // Verifies that if the default search URL preference is managed, we report |
| 1159 // the default search as managed. Also check that we are getting the right | 1272 // the default search as managed. Also check that we are getting the right |
| 1160 // values. | 1273 // values. |
| 1161 TEST_F(TemplateURLServiceTest, TestManagedDefaultSearch) { | 1274 TEST_F(TemplateURLServiceTest, TestManagedDefaultSearch) { |
| 1162 test_util_.VerifyLoad(); | 1275 test_util_.VerifyLoad(); |
| 1163 const size_t initial_count = model()->GetTemplateURLs().size(); | 1276 const size_t initial_count = model()->GetTemplateURLs().size(); |
| 1164 test_util_.ResetObserverCount(); | 1277 test_util_.ResetObserverCount(); |
| 1165 | 1278 |
| 1166 // Set a regular default search provider. | 1279 // Set a regular default search provider. |
| 1167 TemplateURL* regular_default = AddKeywordWithDate("name1", "key1", false, | 1280 TemplateURL* regular_default = AddKeywordWithDate("name1", "key1", |
| 1168 "http://foo1/{searchTerms}", "http://sugg1", "http://icon1", true, | 1281 "http://foo1/{searchTerms}", "http://sugg1", "http://icon1", true, |
| 1169 "UTF-8;UTF-16", Time(), Time()); | 1282 "UTF-8;UTF-16", Time(), Time()); |
| 1170 VerifyObserverCount(1); | 1283 VerifyObserverCount(1); |
| 1171 model()->SetDefaultSearchProvider(regular_default); | 1284 model()->SetDefaultSearchProvider(regular_default); |
| 1172 // Adding the URL and setting the default search provider should have caused | 1285 // Adding the URL and setting the default search provider should have caused |
| 1173 // notifications. | 1286 // notifications. |
| 1174 VerifyObserverCount(1); | 1287 VerifyObserverCount(1); |
| 1175 EXPECT_FALSE(model()->is_default_search_managed()); | 1288 EXPECT_FALSE(model()->is_default_search_managed()); |
| 1176 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); | 1289 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); |
| 1177 | 1290 |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1260 ExpectSimilar(expected_managed_default1.get(), actual_managed_default); | 1373 ExpectSimilar(expected_managed_default1.get(), actual_managed_default); |
| 1261 EXPECT_EQ(actual_managed_default->show_in_default_list(), true); | 1374 EXPECT_EQ(actual_managed_default->show_in_default_list(), true); |
| 1262 | 1375 |
| 1263 // Clear the model and disable the default search provider through policy. | 1376 // Clear the model and disable the default search provider through policy. |
| 1264 // Verify that there is no default search provider after loading the model. | 1377 // Verify that there is no default search provider after loading the model. |
| 1265 // This checks against regressions of http://crbug.com/67180 | 1378 // This checks against regressions of http://crbug.com/67180 |
| 1266 | 1379 |
| 1267 // First, remove the preferences, reset the model, and set a default. | 1380 // First, remove the preferences, reset the model, and set a default. |
| 1268 test_util_.RemoveManagedDefaultSearchPreferences(); | 1381 test_util_.RemoveManagedDefaultSearchPreferences(); |
| 1269 test_util_.ResetModel(true); | 1382 test_util_.ResetModel(true); |
| 1270 TemplateURL* t_url = AddKeywordWithDate("name1", "key1", false, | 1383 TemplateURL* new_default = |
| 1271 "http://foo1/{searchTerms}", "http://sugg1", "http://icon1", true, | 1384 model()->GetTemplateURLForKeyword(ASCIIToUTF16("key1")); |
| 1272 "UTF-8;UTF-16", Time(), Time()); | 1385 ASSERT_FALSE(new_default == NULL); |
| 1273 model()->SetDefaultSearchProvider(t_url); | 1386 model()->SetDefaultSearchProvider(new_default); |
| 1274 EXPECT_EQ(t_url, model()->GetDefaultSearchProvider()); | 1387 EXPECT_EQ(new_default, model()->GetDefaultSearchProvider()); |
| 1275 | 1388 |
| 1276 // Now reset the model again but load it after setting the preferences. | 1389 // Now reset the model again but load it after setting the preferences. |
| 1277 test_util_.ResetModel(false); | 1390 test_util_.ResetModel(false); |
| 1278 test_util_.SetManagedDefaultSearchPreferences(false, std::string(), | 1391 test_util_.SetManagedDefaultSearchPreferences(false, std::string(), |
| 1279 std::string(), std::string(), std::string(), std::string(), | 1392 std::string(), std::string(), std::string(), std::string(), |
| 1280 std::string()); | 1393 std::string()); |
| 1281 test_util_.VerifyLoad(); | 1394 test_util_.VerifyLoad(); |
| 1282 EXPECT_TRUE(model()->is_default_search_managed()); | 1395 EXPECT_TRUE(model()->is_default_search_managed()); |
| 1283 EXPECT_TRUE(model()->GetDefaultSearchProvider() == NULL); | 1396 EXPECT_TRUE(model()->GetDefaultSearchProvider() == NULL); |
| 1284 } | 1397 } |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 1304 | 1417 |
| 1305 // Reload the model to verify it was actually saved to the database and | 1418 // Reload the model to verify it was actually saved to the database and |
| 1306 // assigned a new GUID when brought back. | 1419 // assigned a new GUID when brought back. |
| 1307 test_util_.ResetModel(true); | 1420 test_util_.ResetModel(true); |
| 1308 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); | 1421 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); |
| 1309 const TemplateURL* loaded_url = | 1422 const TemplateURL* loaded_url = |
| 1310 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")); | 1423 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")); |
| 1311 ASSERT_TRUE(loaded_url != NULL); | 1424 ASSERT_TRUE(loaded_url != NULL); |
| 1312 ASSERT_FALSE(loaded_url->sync_guid().empty()); | 1425 ASSERT_FALSE(loaded_url->sync_guid().empty()); |
| 1313 } | 1426 } |
| OLD | NEW |