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.
5 #include "base/basictypes.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "components/search_engines/search_host_to_urls_map.h"
8 #include "components/search_engines/search_terms_data.h"
9 #include "components/search_engines/template_url.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 typedef SearchHostToURLsMap::TemplateURLSet TemplateURLSet
;
14 // Basic functionality for the SearchHostToURLsMap tests.
15 class SearchHostToURLsMapTest
: public testing::Test
{
17 SearchHostToURLsMapTest() {}
19 void SetUp() override
;
22 scoped_ptr
<SearchHostToURLsMap
> provider_map_
;
23 scoped_ptr
<TemplateURL
> t_urls_
[2];
26 DISALLOW_COPY_AND_ASSIGN(SearchHostToURLsMapTest
);
29 void SearchHostToURLsMapTest::SetUp() {
30 // Add some entries to the search host map.
31 host_
= "www.unittest.com";
33 data
.SetURL("http://" + host_
+ "/path1");
34 t_urls_
[0].reset(new TemplateURL(data
));
35 data
.SetURL("http://" + host_
+ "/path2");
36 t_urls_
[1].reset(new TemplateURL(data
));
37 std::vector
<TemplateURL
*> template_urls
;
38 template_urls
.push_back(t_urls_
[0].get());
39 template_urls
.push_back(t_urls_
[1].get());
41 provider_map_
.reset(new SearchHostToURLsMap
);
42 provider_map_
->Init(template_urls
, SearchTermsData());
45 TEST_F(SearchHostToURLsMapTest
, Add
) {
46 std::string new_host
= "example.com";
48 data
.SetURL("http://" + new_host
+ "/");
49 TemplateURL
new_t_url(data
);
50 provider_map_
->Add(&new_t_url
, SearchTermsData());
52 ASSERT_EQ(&new_t_url
, provider_map_
->GetTemplateURLForHost(new_host
));
55 TEST_F(SearchHostToURLsMapTest
, Remove
) {
56 provider_map_
->Remove(t_urls_
[0].get());
58 const TemplateURL
* found_url
= provider_map_
->GetTemplateURLForHost(host_
);
59 ASSERT_EQ(t_urls_
[1].get(), found_url
);
61 const TemplateURLSet
* urls
= provider_map_
->GetURLsForHost(host_
);
62 ASSERT_TRUE(urls
!= NULL
);
65 for (TemplateURLSet::const_iterator
i(urls
->begin()); i
!= urls
->end(); ++i
) {
67 ASSERT_EQ(t_urls_
[1].get(), *i
);
69 ASSERT_EQ(1, url_count
);
72 TEST_F(SearchHostToURLsMapTest
, GetTemplateURLForKnownHost
) {
73 const TemplateURL
* found_url
= provider_map_
->GetTemplateURLForHost(host_
);
74 ASSERT_TRUE(found_url
== t_urls_
[0].get() || found_url
== t_urls_
[1].get());
77 TEST_F(SearchHostToURLsMapTest
, GetTemplateURLForUnknownHost
) {
78 const TemplateURL
* found_url
=
79 provider_map_
->GetTemplateURLForHost("a" + host_
);
80 ASSERT_TRUE(found_url
== NULL
);
83 TEST_F(SearchHostToURLsMapTest
, GetURLsForKnownHost
) {
84 const TemplateURLSet
* urls
= provider_map_
->GetURLsForHost(host_
);
85 ASSERT_TRUE(urls
!= NULL
);
87 bool found_urls
[arraysize(t_urls_
)] = { false };
89 for (TemplateURLSet::const_iterator
i(urls
->begin()); i
!= urls
->end(); ++i
) {
90 const TemplateURL
* url
= *i
;
91 for (size_t i
= 0; i
< arraysize(found_urls
); ++i
) {
92 if (url
== t_urls_
[i
].get()) {
99 for (size_t i
= 0; i
< arraysize(found_urls
); ++i
)
100 ASSERT_TRUE(found_urls
[i
]);
103 TEST_F(SearchHostToURLsMapTest
, GetURLsForUnknownHost
) {
104 const SearchHostToURLsMap::TemplateURLSet
* urls
=
105 provider_map_
->GetURLsForHost("a" + host_
);
106 ASSERT_TRUE(urls
== NULL
);