1 // Copyright 2014 The Chromium Authors
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/search_host_to_urls_map.h"
11 #include "components/search_engines/search_terms_data.h"
12 #include "components/search_engines/template_url.h"
13 #include "components/search_engines/template_url_service.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 typedef SearchHostToURLsMap::TemplateURLSet TemplateURLSet;
18 // Basic functionality for the SearchHostToURLsMap tests.
19 class SearchHostToURLsMapTest : public testing::Test {
21 SearchHostToURLsMapTest() {}
23 SearchHostToURLsMapTest(const SearchHostToURLsMapTest&) = delete;
24 SearchHostToURLsMapTest& operator=(const SearchHostToURLsMapTest&) = delete;
26 void SetUp() override;
29 std::unique_ptr<SearchHostToURLsMap> provider_map_;
30 TemplateURLService::OwnedTemplateURLVector template_urls_;
34 void SearchHostToURLsMapTest::SetUp() {
35 // Add some entries to the search host map.
36 host_ = "www.unittest.com";
38 data.SetURL("http://" + host_ + "/path1");
39 data.last_modified = base::Time() + base::Microseconds(15);
40 template_urls_.push_back(std::make_unique<TemplateURL>(data));
42 // The second one should be slightly newer.
43 data.SetURL("http://" + host_ + "/path2");
44 data.last_modified = base::Time() + base::Microseconds(25);
45 template_urls_.push_back(std::make_unique<TemplateURL>(data));
47 provider_map_ = std::make_unique<SearchHostToURLsMap>();
48 provider_map_->Init(template_urls_, SearchTermsData());
51 TEST_F(SearchHostToURLsMapTest, Add) {
52 std::string new_host = "example.com";
54 data.SetURL("http://" + new_host + "/");
55 TemplateURL new_t_url(data);
56 provider_map_->Add(&new_t_url, SearchTermsData());
58 ASSERT_EQ(&new_t_url, provider_map_->GetTemplateURLForHost(new_host));
61 TEST_F(SearchHostToURLsMapTest, Remove) {
62 provider_map_->Remove(template_urls_[0].get());
64 const TemplateURL* found_url = provider_map_->GetTemplateURLForHost(host_);
65 ASSERT_EQ(template_urls_[1].get(), found_url);
67 const TemplateURLSet* urls = provider_map_->GetURLsForHost(host_);
68 ASSERT_TRUE(urls != nullptr);
71 for (auto i(urls->begin()); i != urls->end(); ++i) {
73 ASSERT_EQ(template_urls_[1].get(), *i);
75 ASSERT_EQ(1, url_count);
78 TEST_F(SearchHostToURLsMapTest, GetsBestTemplateURLForKnownHost) {
79 // The second one should be slightly newer.
80 const TemplateURL* found_url = provider_map_->GetTemplateURLForHost(host_);
81 ASSERT_TRUE(found_url == template_urls_[1].get());
84 data.SetURL("http://" + host_ + "/path1");
85 // Make the new TemplateURL "better" by having it created by policy.
86 data.created_by_policy = true;
88 TemplateURL new_t_url(data);
89 provider_map_->Add(&new_t_url, SearchTermsData());
91 found_url = provider_map_->GetTemplateURLForHost(host_);
92 EXPECT_EQ(found_url, &new_t_url) << "We should have found the new better "
93 "TemplateURL that was created by policy.";
96 TEST_F(SearchHostToURLsMapTest, GetTemplateURLForUnknownHost) {
97 const TemplateURL* found_url =
98 provider_map_->GetTemplateURLForHost("a" + host_);
99 ASSERT_TRUE(found_url == nullptr);
102 TEST_F(SearchHostToURLsMapTest, GetURLsForKnownHost) {
103 const TemplateURLSet* urls = provider_map_->GetURLsForHost(host_);
104 ASSERT_TRUE(urls != nullptr);
106 for (const auto& url : template_urls_)
107 EXPECT_NE(urls->end(), urls->find(url.get()));
110 TEST_F(SearchHostToURLsMapTest, GetURLsForUnknownHost) {
111 const SearchHostToURLsMap::TemplateURLSet* urls =
112 provider_map_->GetURLsForHost("a" + host_);
113 ASSERT_TRUE(urls == nullptr);