Upload upstream chromium 114.0.5735.31
[platform/framework/web/chromium-efl.git] / components / search_engines / search_host_to_urls_map_unittest.cc
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.
4
5 #include "components/search_engines/search_host_to_urls_map.h"
6
7 #include <stddef.h>
8
9 #include <memory>
10
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"
15
16 typedef SearchHostToURLsMap::TemplateURLSet TemplateURLSet;
17
18 // Basic functionality for the SearchHostToURLsMap tests.
19 class SearchHostToURLsMapTest : public testing::Test {
20  public:
21   SearchHostToURLsMapTest() {}
22
23   SearchHostToURLsMapTest(const SearchHostToURLsMapTest&) = delete;
24   SearchHostToURLsMapTest& operator=(const SearchHostToURLsMapTest&) = delete;
25
26   void SetUp() override;
27
28  protected:
29   std::unique_ptr<SearchHostToURLsMap> provider_map_;
30   TemplateURLService::OwnedTemplateURLVector template_urls_;
31   std::string host_;
32 };
33
34 void SearchHostToURLsMapTest::SetUp() {
35   // Add some entries to the search host map.
36   host_ = "www.unittest.com";
37   TemplateURLData data;
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));
41
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));
46
47   provider_map_ = std::make_unique<SearchHostToURLsMap>();
48   provider_map_->Init(template_urls_, SearchTermsData());
49 }
50
51 TEST_F(SearchHostToURLsMapTest, Add) {
52   std::string new_host = "example.com";
53   TemplateURLData data;
54   data.SetURL("http://" + new_host + "/");
55   TemplateURL new_t_url(data);
56   provider_map_->Add(&new_t_url, SearchTermsData());
57
58   ASSERT_EQ(&new_t_url, provider_map_->GetTemplateURLForHost(new_host));
59 }
60
61 TEST_F(SearchHostToURLsMapTest, Remove) {
62   provider_map_->Remove(template_urls_[0].get());
63
64   const TemplateURL* found_url = provider_map_->GetTemplateURLForHost(host_);
65   ASSERT_EQ(template_urls_[1].get(), found_url);
66
67   const TemplateURLSet* urls = provider_map_->GetURLsForHost(host_);
68   ASSERT_TRUE(urls != nullptr);
69
70   int url_count = 0;
71   for (auto i(urls->begin()); i != urls->end(); ++i) {
72     url_count++;
73     ASSERT_EQ(template_urls_[1].get(), *i);
74   }
75   ASSERT_EQ(1, url_count);
76 }
77
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());
82
83   TemplateURLData data;
84   data.SetURL("http://" + host_ + "/path1");
85   // Make the new TemplateURL "better" by having it created by policy.
86   data.created_by_policy = true;
87
88   TemplateURL new_t_url(data);
89   provider_map_->Add(&new_t_url, SearchTermsData());
90
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.";
94 }
95
96 TEST_F(SearchHostToURLsMapTest, GetTemplateURLForUnknownHost) {
97   const TemplateURL* found_url =
98       provider_map_->GetTemplateURLForHost("a" + host_);
99   ASSERT_TRUE(found_url == nullptr);
100 }
101
102 TEST_F(SearchHostToURLsMapTest, GetURLsForKnownHost) {
103   const TemplateURLSet* urls = provider_map_->GetURLsForHost(host_);
104   ASSERT_TRUE(urls != nullptr);
105
106   for (const auto& url : template_urls_)
107     EXPECT_NE(urls->end(), urls->find(url.get()));
108 }
109
110 TEST_F(SearchHostToURLsMapTest, GetURLsForUnknownHost) {
111   const SearchHostToURLsMap::TemplateURLSet* urls =
112       provider_map_->GetURLsForHost("a" + host_);
113   ASSERT_TRUE(urls == nullptr);
114 }