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 "components/search_engines/template_url_service.h" | |
| 6 | |
| 5 #include "base/bind.h" | 7 #include "base/bind.h" |
| 6 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 7 #include "base/callback.h" | 9 #include "base/callback.h" |
| 8 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/scoped_ptr.h" | |
| 9 #include "base/memory/scoped_vector.h" | 12 #include "base/memory/scoped_vector.h" |
| 10 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 11 #include "base/strings/string_split.h" | 14 #include "base/strings/string_split.h" |
| 12 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 14 #include "base/task/cancelable_task_tracker.h" | 17 #include "base/task/cancelable_task_tracker.h" |
| 15 #include "base/test/mock_time_provider.h" | 18 #include "base/test/simple_test_clock.h" |
| 16 #include "base/threading/thread.h" | 19 #include "base/threading/thread.h" |
| 17 #include "base/time/time.h" | 20 #include "base/time/time.h" |
| 18 #include "chrome/browser/history/history_service.h" | 21 #include "chrome/browser/history/history_service.h" |
| 19 #include "chrome/browser/history/history_service_factory.h" | 22 #include "chrome/browser/history/history_service_factory.h" |
| 20 #include "chrome/browser/search_engines/template_url_service_test_util.h" | 23 #include "chrome/browser/search_engines/template_url_service_test_util.h" |
| 21 #include "chrome/test/base/testing_profile.h" | 24 #include "chrome/test/base/testing_profile.h" |
| 22 #include "components/search_engines/keyword_web_data_service.h" | 25 #include "components/search_engines/keyword_web_data_service.h" |
| 23 #include "components/search_engines/search_host_to_urls_map.h" | 26 #include "components/search_engines/search_host_to_urls_map.h" |
| 24 #include "components/search_engines/search_terms_data.h" | 27 #include "components/search_engines/search_terms_data.h" |
| 25 #include "components/search_engines/template_url.h" | 28 #include "components/search_engines/template_url.h" |
| 26 #include "components/search_engines/template_url_prepopulate_data.h" | 29 #include "components/search_engines/template_url_prepopulate_data.h" |
| 27 #include "components/search_engines/template_url_service.h" | |
| 28 #include "content/public/test/test_browser_thread_bundle.h" | 30 #include "content/public/test/test_browser_thread_bundle.h" |
| 29 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
| 30 | 32 |
| 31 using base::ASCIIToUTF16; | 33 using base::ASCIIToUTF16; |
| 32 using base::Time; | 34 using base::Time; |
| 33 using base::TimeDelta; | 35 using base::TimeDelta; |
| 34 using ::testing::Return; | |
| 35 using ::testing::StrictMock; | |
| 36 | 36 |
| 37 namespace { | 37 namespace { |
| 38 | 38 |
| 39 // QueryHistoryCallbackImpl --------------------------------------------------- | 39 // QueryHistoryCallbackImpl --------------------------------------------------- |
| 40 | 40 |
| 41 struct QueryHistoryCallbackImpl { | 41 struct QueryHistoryCallbackImpl { |
| 42 QueryHistoryCallbackImpl() : success(false) {} | 42 QueryHistoryCallbackImpl() : success(false) {} |
| 43 | 43 |
| 44 void Callback(bool success, | 44 void Callback(bool success, |
| 45 const history::URLRow& row, | 45 const history::URLRow& row, |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 220 ASSERT_EQ(expected.keyword(), actual.keyword()); | 220 ASSERT_EQ(expected.keyword(), actual.keyword()); |
| 221 ASSERT_EQ(expected.url(), actual.url()); | 221 ASSERT_EQ(expected.url(), actual.url()); |
| 222 ASSERT_EQ(expected.suggestions_url(), actual.suggestions_url()); | 222 ASSERT_EQ(expected.suggestions_url(), actual.suggestions_url()); |
| 223 ASSERT_EQ(expected.favicon_url(), actual.favicon_url()); | 223 ASSERT_EQ(expected.favicon_url(), actual.favicon_url()); |
| 224 ASSERT_EQ(expected.alternate_urls(), actual.alternate_urls()); | 224 ASSERT_EQ(expected.alternate_urls(), actual.alternate_urls()); |
| 225 ASSERT_EQ(expected.show_in_default_list(), actual.show_in_default_list()); | 225 ASSERT_EQ(expected.show_in_default_list(), actual.show_in_default_list()); |
| 226 ASSERT_EQ(expected.safe_for_autoreplace(), actual.safe_for_autoreplace()); | 226 ASSERT_EQ(expected.safe_for_autoreplace(), actual.safe_for_autoreplace()); |
| 227 ASSERT_EQ(expected.input_encodings(), actual.input_encodings()); | 227 ASSERT_EQ(expected.input_encodings(), actual.input_encodings()); |
| 228 ASSERT_EQ(expected.id(), actual.id()); | 228 ASSERT_EQ(expected.id(), actual.id()); |
| 229 ASSERT_EQ(expected.date_created(), actual.date_created()); | 229 ASSERT_EQ(expected.date_created(), actual.date_created()); |
| 230 ASSERT_EQ(expected.last_modified(), actual.last_modified()); | 230 // Note that times are stored with a granularity of one second. |
|
Peter Kasting
2015/01/22 00:00:23
Doesn't this mean the assert would have to be LE i
Ilya Sherman
2015/01/22 02:38:22
The issue is not that we might tick over a second.
Peter Kasting
2015/01/22 17:54:22
Right, but what I'm worried about is, if the start
Ilya Sherman
2015/01/22 23:13:11
That's not possible -- the test Clock will always
| |
| 231 ASSERT_LT((expected.last_modified() - actual.last_modified()).magnitude(), | |
|
tfarina
2015/01/16 05:01:39
Looks like this accounts for the small difference
| |
| 232 base::TimeDelta::FromSeconds(1)); | |
| 231 ASSERT_EQ(expected.sync_guid(), actual.sync_guid()); | 233 ASSERT_EQ(expected.sync_guid(), actual.sync_guid()); |
| 232 ASSERT_EQ(expected.search_terms_replacement_key(), | 234 ASSERT_EQ(expected.search_terms_replacement_key(), |
| 233 actual.search_terms_replacement_key()); | 235 actual.search_terms_replacement_key()); |
| 234 } | 236 } |
| 235 | 237 |
| 236 TemplateURL* TemplateURLServiceTest::CreatePreloadedTemplateURL( | 238 TemplateURL* TemplateURLServiceTest::CreatePreloadedTemplateURL( |
| 237 bool safe_for_autoreplace, | 239 bool safe_for_autoreplace, |
| 238 int prepopulate_id) { | 240 int prepopulate_id) { |
| 239 TemplateURLData data; | 241 TemplateURLData data; |
| 240 data.short_name = ASCIIToUTF16("unittest"); | 242 data.short_name = ASCIIToUTF16("unittest"); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 298 test_util()->ResetModel(true); | 300 test_util()->ResetModel(true); |
| 299 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); | 301 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); |
| 300 TemplateURL* loaded_url = | 302 TemplateURL* loaded_url = |
| 301 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")); | 303 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")); |
| 302 ASSERT_TRUE(loaded_url != NULL); | 304 ASSERT_TRUE(loaded_url != NULL); |
| 303 AssertEquals(*cloned_url, *loaded_url); | 305 AssertEquals(*cloned_url, *loaded_url); |
| 304 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("keyword"), GURL(), | 306 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("keyword"), GURL(), |
| 305 NULL)); | 307 NULL)); |
| 306 | 308 |
| 307 // We expect the last_modified time to be updated to the present time on an | 309 // We expect the last_modified time to be updated to the present time on an |
| 308 // explicit reset. We have to set up the expectation here because ResetModel | 310 // explicit reset. |
| 309 // resets the TimeProvider in the TemplateURLService. | 311 base::Time now = base::Time::Now(); |
| 310 StrictMock<base::MockTimeProvider> mock_time; | 312 scoped_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock); |
|
tfarina
2015/01/16 04:28:51
Doesn't have crashed for you? I mean, not this lin
Ilya Sherman
2015/01/16 04:39:18
This doesn't crash, because the ownership is trans
| |
| 311 model()->set_time_provider(&base::MockTimeProvider::StaticNow); | 313 clock->SetNow(now); |
| 312 EXPECT_CALL(mock_time, Now()).WillOnce(Return(base::Time::FromDoubleT(1337))); | 314 model()->set_clock(clock.Pass()); |
| 313 | 315 |
| 314 // Mutate an element and verify it succeeded. | 316 // Mutate an element and verify it succeeded. |
| 315 model()->ResetTemplateURL(loaded_url, ASCIIToUTF16("a"), ASCIIToUTF16("b"), | 317 model()->ResetTemplateURL(loaded_url, ASCIIToUTF16("a"), ASCIIToUTF16("b"), |
| 316 "c"); | 318 "c"); |
| 317 ASSERT_EQ(ASCIIToUTF16("a"), loaded_url->short_name()); | 319 ASSERT_EQ(ASCIIToUTF16("a"), loaded_url->short_name()); |
| 318 ASSERT_EQ(ASCIIToUTF16("b"), loaded_url->keyword()); | 320 ASSERT_EQ(ASCIIToUTF16("b"), loaded_url->keyword()); |
| 319 ASSERT_EQ("c", loaded_url->url()); | 321 ASSERT_EQ("c", loaded_url->url()); |
| 320 ASSERT_FALSE(loaded_url->safe_for_autoreplace()); | 322 ASSERT_FALSE(loaded_url->safe_for_autoreplace()); |
| 321 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("keyword"), GURL(), | 323 ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("keyword"), GURL(), |
| 322 NULL)); | 324 NULL)); |
| 323 ASSERT_FALSE(model()->CanReplaceKeyword(ASCIIToUTF16("b"), GURL(), NULL)); | 325 ASSERT_FALSE(model()->CanReplaceKeyword(ASCIIToUTF16("b"), GURL(), NULL)); |
| 324 cloned_url.reset(new TemplateURL(loaded_url->data())); | 326 cloned_url.reset(new TemplateURL(loaded_url->data())); |
| 325 base::RunLoop().RunUntilIdle(); | 327 base::RunLoop().RunUntilIdle(); |
| 326 test_util()->ResetModel(true); | 328 test_util()->ResetModel(true); |
| 327 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); | 329 ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); |
| 328 loaded_url = model()->GetTemplateURLForKeyword(ASCIIToUTF16("b")); | 330 loaded_url = model()->GetTemplateURLForKeyword(ASCIIToUTF16("b")); |
| 329 ASSERT_TRUE(loaded_url != NULL); | 331 ASSERT_TRUE(loaded_url != NULL); |
| 330 AssertEquals(*cloned_url, *loaded_url); | 332 AssertEquals(*cloned_url, *loaded_url); |
|
tfarina
2015/01/16 04:28:51
This didn't pass for me locally.
Ilya Sherman
2015/01/16 04:39:18
Note that I modified the AssertEquals implementati
| |
| 331 // We changed a TemplateURL in the service, so ensure that the time was | 333 // We changed a TemplateURL in the service, so ensure that the time was |
| 332 // updated. | 334 // updated. Note that times are stored with a granularity of one second. |
| 333 ASSERT_EQ(base::Time::FromDoubleT(1337), loaded_url->last_modified()); | 335 ASSERT_LT((now - loaded_url->last_modified()).magnitude(), |
| 336 base::TimeDelta::FromSeconds(1)); | |
| 334 | 337 |
| 335 // Remove an element and verify it succeeded. | 338 // Remove an element and verify it succeeded. |
| 336 model()->Remove(loaded_url); | 339 model()->Remove(loaded_url); |
| 337 VerifyObserverCount(1); | 340 VerifyObserverCount(1); |
| 338 test_util()->ResetModel(true); | 341 test_util()->ResetModel(true); |
| 339 ASSERT_EQ(initial_count, model()->GetTemplateURLs().size()); | 342 ASSERT_EQ(initial_count, model()->GetTemplateURLs().size()); |
| 340 EXPECT_TRUE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("b")) == NULL); | 343 EXPECT_TRUE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("b")) == NULL); |
| 341 } | 344 } |
| 342 | 345 |
| 343 TEST_F(TemplateURLServiceTest, AddSameKeyword) { | 346 TEST_F(TemplateURLServiceTest, AddSameKeyword) { |
| (...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 613 data.SetURL("http://www.google.com/foo/bar"); | 616 data.SetURL("http://www.google.com/foo/bar"); |
| 614 data.favicon_url = GURL("http://favicon.url"); | 617 data.favicon_url = GURL("http://favicon.url"); |
| 615 data.date_created = Time::FromTimeT(100); | 618 data.date_created = Time::FromTimeT(100); |
| 616 data.last_modified = Time::FromTimeT(100); | 619 data.last_modified = Time::FromTimeT(100); |
| 617 TemplateURL* t_url = new TemplateURL(data); | 620 TemplateURL* t_url = new TemplateURL(data); |
| 618 model()->Add(t_url); | 621 model()->Add(t_url); |
| 619 | 622 |
| 620 VerifyObserverCount(1); | 623 VerifyObserverCount(1); |
| 621 base::RunLoop().RunUntilIdle(); | 624 base::RunLoop().RunUntilIdle(); |
| 622 | 625 |
| 623 StrictMock<base::MockTimeProvider> mock_time; | |
| 624 model()->set_time_provider(&base::MockTimeProvider::StaticNow); | |
| 625 EXPECT_CALL(mock_time, Now()).WillOnce(Return(base::Time::FromDoubleT(1337))); | |
| 626 | |
| 627 // Reset the short name, keyword, url and make sure it takes. | 626 // Reset the short name, keyword, url and make sure it takes. |
| 628 const base::string16 new_short_name(ASCIIToUTF16("a")); | 627 const base::string16 new_short_name(ASCIIToUTF16("a")); |
| 629 const base::string16 new_keyword(ASCIIToUTF16("b")); | 628 const base::string16 new_keyword(ASCIIToUTF16("b")); |
| 630 const std::string new_url("c"); | 629 const std::string new_url("c"); |
| 631 model()->ResetTemplateURL(t_url, new_short_name, new_keyword, new_url); | 630 model()->ResetTemplateURL(t_url, new_short_name, new_keyword, new_url); |
| 632 ASSERT_EQ(new_short_name, t_url->short_name()); | 631 ASSERT_EQ(new_short_name, t_url->short_name()); |
| 633 ASSERT_EQ(new_keyword, t_url->keyword()); | 632 ASSERT_EQ(new_keyword, t_url->keyword()); |
| 634 ASSERT_EQ(new_url, t_url->url()); | 633 ASSERT_EQ(new_url, t_url->url()); |
| 635 | 634 |
| 636 // Make sure the mappings in the model were updated. | 635 // Make sure the mappings in the model were updated. |
| 637 ASSERT_EQ(t_url, model()->GetTemplateURLForKeyword(new_keyword)); | 636 ASSERT_EQ(t_url, model()->GetTemplateURLForKeyword(new_keyword)); |
| 638 ASSERT_TRUE( | 637 ASSERT_TRUE( |
| 639 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")) == NULL); | 638 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")) == NULL); |
| 640 | 639 |
| 641 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data())); | 640 scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data())); |
| 642 | 641 |
| 642 base::Time now = base::Time::Now(); | |
|
tfarina
2015/01/16 05:01:39
Have you had to move this down for some reason? Or
Ilya Sherman
2015/01/16 05:11:07
That's a good question. I took your CL as a start
Peter Kasting
2015/01/22 00:00:23
Hmm. It seems like it ought to fail to me, since
Ilya Sherman
2015/01/22 02:38:22
I'm not sure what's causing the test to continue t
Peter Kasting
2015/01/22 17:54:22
Yeah, I wasn't so much asking you to fix it as hop
Ilya Sherman
2015/01/22 23:13:11
Fair enough. I didn't investigate deeply enough t
| |
| 643 scoped_ptr<base::SimpleTestClock> clock(new base::SimpleTestClock); | |
| 644 clock->SetNow(now); | |
| 645 model()->set_clock(clock.Pass()); | |
| 646 | |
| 643 // Reload the model from the database and make sure the change took. | 647 // Reload the model from the database and make sure the change took. |
| 644 test_util()->ResetModel(true); | 648 test_util()->ResetModel(true); |
| 645 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); | 649 EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); |
| 646 const TemplateURL* read_url = model()->GetTemplateURLForKeyword(new_keyword); | 650 const TemplateURL* read_url = model()->GetTemplateURLForKeyword(new_keyword); |
| 647 ASSERT_TRUE(read_url); | 651 ASSERT_TRUE(read_url); |
| 648 AssertEquals(*cloned_url, *read_url); | 652 AssertEquals(*cloned_url, *read_url); |
| 649 ASSERT_EQ(base::Time::FromDoubleT(1337), read_url->last_modified()); | 653 // Note that times are stored with a granularity of one second. |
| 654 ASSERT_LT((now - read_url->last_modified()).magnitude(), | |
| 655 base::TimeDelta::FromSeconds(1)); | |
| 650 } | 656 } |
| 651 | 657 |
| 652 TEST_F(TemplateURLServiceTest, DefaultSearchProvider) { | 658 TEST_F(TemplateURLServiceTest, DefaultSearchProvider) { |
| 653 // Add a new TemplateURL. | 659 // Add a new TemplateURL. |
| 654 test_util()->VerifyLoad(); | 660 test_util()->VerifyLoad(); |
| 655 const size_t initial_count = model()->GetTemplateURLs().size(); | 661 const size_t initial_count = model()->GetTemplateURLs().size(); |
| 656 TemplateURL* t_url = AddKeywordWithDate( | 662 TemplateURL* t_url = AddKeywordWithDate( |
| 657 "name1", "key1", "http://foo1/{searchTerms}", "http://sugg1", | 663 "name1", "key1", "http://foo1/{searchTerms}", "http://sugg1", |
| 658 std::string(), "http://icon1", true, "UTF-8;UTF-16", Time(), Time()); | 664 std::string(), "http://icon1", true, "UTF-8;UTF-16", Time(), Time()); |
| 659 test_util()->ResetObserverCount(); | 665 test_util()->ResetObserverCount(); |
| (...skipping 818 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1478 scoped_ptr<TemplateURL::AssociatedExtensionInfo> extension_info( | 1484 scoped_ptr<TemplateURL::AssociatedExtensionInfo> extension_info( |
| 1479 new TemplateURL::AssociatedExtensionInfo( | 1485 new TemplateURL::AssociatedExtensionInfo( |
| 1480 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION, "ext1")); | 1486 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION, "ext1")); |
| 1481 extension_info->wants_to_be_default_engine = true; | 1487 extension_info->wants_to_be_default_engine = true; |
| 1482 model()->AddExtensionControlledTURL(ext_dse, extension_info.Pass()); | 1488 model()->AddExtensionControlledTURL(ext_dse, extension_info.Pass()); |
| 1483 EXPECT_EQ(ext_dse, model()->GetTemplateURLForKeyword(ASCIIToUTF16("ext1"))); | 1489 EXPECT_EQ(ext_dse, model()->GetTemplateURLForKeyword(ASCIIToUTF16("ext1"))); |
| 1484 EXPECT_TRUE(model()->is_default_search_managed()); | 1490 EXPECT_TRUE(model()->is_default_search_managed()); |
| 1485 actual_managed_default = model()->GetDefaultSearchProvider(); | 1491 actual_managed_default = model()->GetDefaultSearchProvider(); |
| 1486 ExpectSimilar(expected_managed_default.get(), actual_managed_default); | 1492 ExpectSimilar(expected_managed_default.get(), actual_managed_default); |
| 1487 } | 1493 } |
| OLD | NEW |