1 // Copyright (c) 2012 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 "chrome/browser/search_engines/search_host_to_urls_map.h"
8 #include "chrome/browser/search_engines/search_terms_data.h"
9 #include "chrome/browser/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() {}
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(NULL
, data
));
35 data
.SetURL("http://" + host_
+ "/path2");
36 t_urls_
[1].reset(new TemplateURL(NULL
, 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 UIThreadSearchTermsData
search_terms_data(NULL
);
43 provider_map_
->Init(template_urls
, search_terms_data
);
46 TEST_F(SearchHostToURLsMapTest
, Add
) {
47 std::string new_host
= "example.com";
49 data
.SetURL("http://" + new_host
+ "/");
50 TemplateURL
new_t_url(NULL
, data
);
51 UIThreadSearchTermsData
search_terms_data(NULL
);
52 provider_map_
->Add(&new_t_url
, search_terms_data
);
54 ASSERT_EQ(&new_t_url
, provider_map_
->GetTemplateURLForHost(new_host
));
57 TEST_F(SearchHostToURLsMapTest
, Remove
) {
58 UIThreadSearchTermsData
search_terms_data(NULL
);
59 provider_map_
->Remove(t_urls_
[0].get(), search_terms_data
);
61 const TemplateURL
* found_url
= provider_map_
->GetTemplateURLForHost(host_
);
62 ASSERT_EQ(t_urls_
[1].get(), found_url
);
64 const TemplateURLSet
* urls
= provider_map_
->GetURLsForHost(host_
);
65 ASSERT_TRUE(urls
!= NULL
);
68 for (TemplateURLSet::const_iterator
i(urls
->begin()); i
!= urls
->end(); ++i
) {
70 ASSERT_EQ(t_urls_
[1].get(), *i
);
72 ASSERT_EQ(1, url_count
);
75 TEST_F(SearchHostToURLsMapTest
, GetTemplateURLForKnownHost
) {
76 const TemplateURL
* found_url
= provider_map_
->GetTemplateURLForHost(host_
);
77 ASSERT_TRUE(found_url
== t_urls_
[0].get() || found_url
== t_urls_
[1].get());
80 TEST_F(SearchHostToURLsMapTest
, GetTemplateURLForUnknownHost
) {
81 const TemplateURL
* found_url
=
82 provider_map_
->GetTemplateURLForHost("a" + host_
);
83 ASSERT_TRUE(found_url
== NULL
);
86 TEST_F(SearchHostToURLsMapTest
, GetURLsForKnownHost
) {
87 const TemplateURLSet
* urls
= provider_map_
->GetURLsForHost(host_
);
88 ASSERT_TRUE(urls
!= NULL
);
90 bool found_urls
[arraysize(t_urls_
)] = { false };
92 for (TemplateURLSet::const_iterator
i(urls
->begin()); i
!= urls
->end(); ++i
) {
93 const TemplateURL
* url
= *i
;
94 for (size_t i
= 0; i
< arraysize(found_urls
); ++i
) {
95 if (url
== t_urls_
[i
].get()) {
102 for (size_t i
= 0; i
< arraysize(found_urls
); ++i
)
103 ASSERT_TRUE(found_urls
[i
]);
106 TEST_F(SearchHostToURLsMapTest
, GetURLsForUnknownHost
) {
107 const SearchHostToURLsMap::TemplateURLSet
* urls
=
108 provider_map_
->GetURLsForHost("a" + host_
);
109 ASSERT_TRUE(urls
== NULL
);