OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/search_host_to_urls_map.h" | 5 #include "components/search_engines/search_host_to_urls_map.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/ptr_util.h" | |
12 #include "components/search_engines/search_terms_data.h" | 13 #include "components/search_engines/search_terms_data.h" |
13 #include "components/search_engines/template_url.h" | 14 #include "components/search_engines/template_url.h" |
15 #include "components/search_engines/template_url_service.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
15 | 17 |
16 typedef SearchHostToURLsMap::TemplateURLSet TemplateURLSet; | 18 typedef SearchHostToURLsMap::TemplateURLSet TemplateURLSet; |
17 | 19 |
18 // Basic functionality for the SearchHostToURLsMap tests. | 20 // Basic functionality for the SearchHostToURLsMap tests. |
19 class SearchHostToURLsMapTest : public testing::Test { | 21 class SearchHostToURLsMapTest : public testing::Test { |
20 public: | 22 public: |
21 SearchHostToURLsMapTest() {} | 23 SearchHostToURLsMapTest() {} |
22 | 24 |
23 void SetUp() override; | 25 void SetUp() override; |
24 | 26 |
25 protected: | 27 protected: |
26 std::unique_ptr<SearchHostToURLsMap> provider_map_; | 28 std::unique_ptr<SearchHostToURLsMap> provider_map_; |
27 std::unique_ptr<TemplateURL> t_urls_[2]; | 29 TemplateURLService::OwnedTemplateURLVector template_urls_; |
28 std::string host_; | 30 std::string host_; |
29 | 31 |
30 DISALLOW_COPY_AND_ASSIGN(SearchHostToURLsMapTest); | 32 DISALLOW_COPY_AND_ASSIGN(SearchHostToURLsMapTest); |
31 }; | 33 }; |
32 | 34 |
33 void SearchHostToURLsMapTest::SetUp() { | 35 void SearchHostToURLsMapTest::SetUp() { |
34 // Add some entries to the search host map. | 36 // Add some entries to the search host map. |
35 host_ = "www.unittest.com"; | 37 host_ = "www.unittest.com"; |
36 TemplateURLData data; | 38 TemplateURLData data; |
37 data.SetURL("http://" + host_ + "/path1"); | 39 data.SetURL("http://" + host_ + "/path1"); |
38 t_urls_[0].reset(new TemplateURL(data)); | 40 template_urls_.push_back(base::MakeUnique<TemplateURL>(data)); |
39 data.SetURL("http://" + host_ + "/path2"); | 41 data.SetURL("http://" + host_ + "/path2"); |
40 t_urls_[1].reset(new TemplateURL(data)); | 42 template_urls_.push_back(base::MakeUnique<TemplateURL>(data)); |
41 std::vector<TemplateURL*> template_urls; | |
42 template_urls.push_back(t_urls_[0].get()); | |
43 template_urls.push_back(t_urls_[1].get()); | |
44 | 43 |
45 provider_map_.reset(new SearchHostToURLsMap); | 44 provider_map_.reset(new SearchHostToURLsMap); |
46 provider_map_->Init(template_urls, SearchTermsData()); | 45 provider_map_->Init(template_urls_, SearchTermsData()); |
47 } | 46 } |
48 | 47 |
49 TEST_F(SearchHostToURLsMapTest, Add) { | 48 TEST_F(SearchHostToURLsMapTest, Add) { |
50 std::string new_host = "example.com"; | 49 std::string new_host = "example.com"; |
51 TemplateURLData data; | 50 TemplateURLData data; |
52 data.SetURL("http://" + new_host + "/"); | 51 data.SetURL("http://" + new_host + "/"); |
53 TemplateURL new_t_url(data); | 52 TemplateURL new_t_url(data); |
54 provider_map_->Add(&new_t_url, SearchTermsData()); | 53 provider_map_->Add(&new_t_url, SearchTermsData()); |
55 | 54 |
56 ASSERT_EQ(&new_t_url, provider_map_->GetTemplateURLForHost(new_host)); | 55 ASSERT_EQ(&new_t_url, provider_map_->GetTemplateURLForHost(new_host)); |
57 } | 56 } |
58 | 57 |
59 TEST_F(SearchHostToURLsMapTest, Remove) { | 58 TEST_F(SearchHostToURLsMapTest, Remove) { |
60 provider_map_->Remove(t_urls_[0].get()); | 59 provider_map_->Remove(template_urls_[0].get()); |
61 | 60 |
62 const TemplateURL* found_url = provider_map_->GetTemplateURLForHost(host_); | 61 const TemplateURL* found_url = provider_map_->GetTemplateURLForHost(host_); |
63 ASSERT_EQ(t_urls_[1].get(), found_url); | 62 ASSERT_EQ(template_urls_[1].get(), found_url); |
64 | 63 |
65 const TemplateURLSet* urls = provider_map_->GetURLsForHost(host_); | 64 const TemplateURLSet* urls = provider_map_->GetURLsForHost(host_); |
66 ASSERT_TRUE(urls != NULL); | 65 ASSERT_TRUE(urls != NULL); |
67 | 66 |
68 int url_count = 0; | 67 int url_count = 0; |
69 for (TemplateURLSet::const_iterator i(urls->begin()); i != urls->end(); ++i) { | 68 for (TemplateURLSet::const_iterator i(urls->begin()); i != urls->end(); ++i) { |
70 url_count++; | 69 url_count++; |
71 ASSERT_EQ(t_urls_[1].get(), *i); | 70 ASSERT_EQ(template_urls_[1].get(), *i); |
72 } | 71 } |
73 ASSERT_EQ(1, url_count); | 72 ASSERT_EQ(1, url_count); |
74 } | 73 } |
75 | 74 |
76 TEST_F(SearchHostToURLsMapTest, GetTemplateURLForKnownHost) { | 75 TEST_F(SearchHostToURLsMapTest, GetTemplateURLForKnownHost) { |
77 const TemplateURL* found_url = provider_map_->GetTemplateURLForHost(host_); | 76 const TemplateURL* found_url = provider_map_->GetTemplateURLForHost(host_); |
78 ASSERT_TRUE(found_url == t_urls_[0].get() || found_url == t_urls_[1].get()); | 77 ASSERT_TRUE(found_url == template_urls_[0].get() || |
78 found_url == template_urls_[1].get()); | |
79 } | 79 } |
80 | 80 |
81 TEST_F(SearchHostToURLsMapTest, GetTemplateURLForUnknownHost) { | 81 TEST_F(SearchHostToURLsMapTest, GetTemplateURLForUnknownHost) { |
82 const TemplateURL* found_url = | 82 const TemplateURL* found_url = |
83 provider_map_->GetTemplateURLForHost("a" + host_); | 83 provider_map_->GetTemplateURLForHost("a" + host_); |
84 ASSERT_TRUE(found_url == NULL); | 84 ASSERT_TRUE(found_url == NULL); |
85 } | 85 } |
86 | 86 |
87 TEST_F(SearchHostToURLsMapTest, GetURLsForKnownHost) { | 87 TEST_F(SearchHostToURLsMapTest, GetURLsForKnownHost) { |
88 const TemplateURLSet* urls = provider_map_->GetURLsForHost(host_); | 88 const TemplateURLSet* urls = provider_map_->GetURLsForHost(host_); |
89 ASSERT_TRUE(urls != NULL); | 89 ASSERT_TRUE(urls != NULL); |
90 | 90 |
91 bool found_urls[arraysize(t_urls_)] = { false }; | 91 std::map<size_t, bool> found_urls; |
Peter Kasting
2016/08/31 04:12:56
Wow, this function is a mess.
Seems like everythi
Avi (use Gerrit)
2016/09/01 00:34:27
Done.
| |
92 | 92 |
93 for (TemplateURLSet::const_iterator i(urls->begin()); i != urls->end(); ++i) { | 93 for (auto i = urls->begin(); i != urls->end(); ++i) { |
94 const TemplateURL* url = *i; | 94 const TemplateURL* url = *i; |
95 for (size_t i = 0; i < arraysize(found_urls); ++i) { | 95 for (size_t i = 0; i < template_urls_.size(); ++i) { |
96 if (url == t_urls_[i].get()) { | 96 if (url == template_urls_[i].get()) { |
97 found_urls[i] = true; | 97 found_urls[i] = true; |
98 break; | 98 break; |
99 } | 99 } |
100 } | 100 } |
101 } | 101 } |
102 | 102 |
103 for (size_t i = 0; i < arraysize(found_urls); ++i) | 103 for (size_t i = 0; i < template_urls_.size(); ++i) |
104 ASSERT_TRUE(found_urls[i]); | 104 ASSERT_TRUE(found_urls[i]); |
105 } | 105 } |
106 | 106 |
107 TEST_F(SearchHostToURLsMapTest, GetURLsForUnknownHost) { | 107 TEST_F(SearchHostToURLsMapTest, GetURLsForUnknownHost) { |
108 const SearchHostToURLsMap::TemplateURLSet* urls = | 108 const SearchHostToURLsMap::TemplateURLSet* urls = |
109 provider_map_->GetURLsForHost("a" + host_); | 109 provider_map_->GetURLsForHost("a" + host_); |
110 ASSERT_TRUE(urls == NULL); | 110 ASSERT_TRUE(urls == NULL); |
111 } | 111 } |
OLD | NEW |