search_host_to_urls_map_unittest.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2014 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.
  4. #include "components/search_engines/search_host_to_urls_map.h"
  5. #include <stddef.h>
  6. #include <memory>
  7. #include "components/search_engines/search_terms_data.h"
  8. #include "components/search_engines/template_url.h"
  9. #include "components/search_engines/template_url_service.h"
  10. #include "testing/gtest/include/gtest/gtest.h"
  11. typedef SearchHostToURLsMap::TemplateURLSet TemplateURLSet;
  12. // Basic functionality for the SearchHostToURLsMap tests.
  13. class SearchHostToURLsMapTest : public testing::Test {
  14. public:
  15. SearchHostToURLsMapTest() {}
  16. SearchHostToURLsMapTest(const SearchHostToURLsMapTest&) = delete;
  17. SearchHostToURLsMapTest& operator=(const SearchHostToURLsMapTest&) = delete;
  18. void SetUp() override;
  19. protected:
  20. std::unique_ptr<SearchHostToURLsMap> provider_map_;
  21. TemplateURLService::OwnedTemplateURLVector template_urls_;
  22. std::string host_;
  23. };
  24. void SearchHostToURLsMapTest::SetUp() {
  25. // Add some entries to the search host map.
  26. host_ = "www.unittest.com";
  27. TemplateURLData data;
  28. data.SetURL("http://" + host_ + "/path1");
  29. data.last_modified = base::Time() + base::Microseconds(15);
  30. template_urls_.push_back(std::make_unique<TemplateURL>(data));
  31. // The second one should be slightly newer.
  32. data.SetURL("http://" + host_ + "/path2");
  33. data.last_modified = base::Time() + base::Microseconds(25);
  34. template_urls_.push_back(std::make_unique<TemplateURL>(data));
  35. provider_map_ = std::make_unique<SearchHostToURLsMap>();
  36. provider_map_->Init(template_urls_, SearchTermsData());
  37. }
  38. TEST_F(SearchHostToURLsMapTest, Add) {
  39. std::string new_host = "example.com";
  40. TemplateURLData data;
  41. data.SetURL("http://" + new_host + "/");
  42. TemplateURL new_t_url(data);
  43. provider_map_->Add(&new_t_url, SearchTermsData());
  44. ASSERT_EQ(&new_t_url, provider_map_->GetTemplateURLForHost(new_host));
  45. }
  46. TEST_F(SearchHostToURLsMapTest, Remove) {
  47. provider_map_->Remove(template_urls_[0].get());
  48. const TemplateURL* found_url = provider_map_->GetTemplateURLForHost(host_);
  49. ASSERT_EQ(template_urls_[1].get(), found_url);
  50. const TemplateURLSet* urls = provider_map_->GetURLsForHost(host_);
  51. ASSERT_TRUE(urls != nullptr);
  52. int url_count = 0;
  53. for (auto i(urls->begin()); i != urls->end(); ++i) {
  54. url_count++;
  55. ASSERT_EQ(template_urls_[1].get(), *i);
  56. }
  57. ASSERT_EQ(1, url_count);
  58. }
  59. TEST_F(SearchHostToURLsMapTest, GetsBestTemplateURLForKnownHost) {
  60. // The second one should be slightly newer.
  61. const TemplateURL* found_url = provider_map_->GetTemplateURLForHost(host_);
  62. ASSERT_TRUE(found_url == template_urls_[1].get());
  63. TemplateURLData data;
  64. data.SetURL("http://" + host_ + "/path1");
  65. // Make the new TemplateURL "better" by having it created by policy.
  66. data.created_by_policy = true;
  67. TemplateURL new_t_url(data);
  68. provider_map_->Add(&new_t_url, SearchTermsData());
  69. found_url = provider_map_->GetTemplateURLForHost(host_);
  70. EXPECT_EQ(found_url, &new_t_url) << "We should have found the new better "
  71. "TemplateURL that was created by policy.";
  72. }
  73. TEST_F(SearchHostToURLsMapTest, GetTemplateURLForUnknownHost) {
  74. const TemplateURL* found_url =
  75. provider_map_->GetTemplateURLForHost("a" + host_);
  76. ASSERT_TRUE(found_url == nullptr);
  77. }
  78. TEST_F(SearchHostToURLsMapTest, GetURLsForKnownHost) {
  79. const TemplateURLSet* urls = provider_map_->GetURLsForHost(host_);
  80. ASSERT_TRUE(urls != nullptr);
  81. for (const auto& url : template_urls_)
  82. EXPECT_NE(urls->end(), urls->find(url.get()));
  83. }
  84. TEST_F(SearchHostToURLsMapTest, GetURLsForUnknownHost) {
  85. const SearchHostToURLsMap::TemplateURLSet* urls =
  86. provider_map_->GetURLsForHost("a" + host_);
  87. ASSERT_TRUE(urls == nullptr);
  88. }