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. 4 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" 11 12 typedef SearchHostToURLsMap::TemplateURLSet TemplateURLSet; 13 14 // Basic functionality for the SearchHostToURLsMap tests. 15 class SearchHostToURLsMapTest : public testing::Test { 16 public: 17 SearchHostToURLsMapTest() {} 18 19 virtual void SetUp(); 20 21 protected: 22 scoped_ptr<SearchHostToURLsMap> provider_map_; 23 scoped_ptr<TemplateURL> t_urls_[2]; 24 std::string host_; 25 26 DISALLOW_COPY_AND_ASSIGN(SearchHostToURLsMapTest); 27 }; 28 29 void SearchHostToURLsMapTest::SetUp() { 30 // Add some entries to the search host map. 31 host_ = "www.unittest.com"; 32 TemplateURLData data; 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()); 40 41 provider_map_.reset(new SearchHostToURLsMap); 42 UIThreadSearchTermsData search_terms_data(NULL); 43 provider_map_->Init(template_urls, search_terms_data); 44 } 45 46 TEST_F(SearchHostToURLsMapTest, Add) { 47 std::string new_host = "example.com"; 48 TemplateURLData data; 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); 53 54 ASSERT_EQ(&new_t_url, provider_map_->GetTemplateURLForHost(new_host)); 55 } 56 57 TEST_F(SearchHostToURLsMapTest, Remove) { 58 UIThreadSearchTermsData search_terms_data(NULL); 59 provider_map_->Remove(t_urls_[0].get(), search_terms_data); 60 61 const TemplateURL* found_url = provider_map_->GetTemplateURLForHost(host_); 62 ASSERT_EQ(t_urls_[1].get(), found_url); 63 64 const TemplateURLSet* urls = provider_map_->GetURLsForHost(host_); 65 ASSERT_TRUE(urls != NULL); 66 67 int url_count = 0; 68 for (TemplateURLSet::const_iterator i(urls->begin()); i != urls->end(); ++i) { 69 url_count++; 70 ASSERT_EQ(t_urls_[1].get(), *i); 71 } 72 ASSERT_EQ(1, url_count); 73 } 74 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()); 78 } 79 80 TEST_F(SearchHostToURLsMapTest, GetTemplateURLForUnknownHost) { 81 const TemplateURL* found_url = 82 provider_map_->GetTemplateURLForHost("a" + host_); 83 ASSERT_TRUE(found_url == NULL); 84 } 85 86 TEST_F(SearchHostToURLsMapTest, GetURLsForKnownHost) { 87 const TemplateURLSet* urls = provider_map_->GetURLsForHost(host_); 88 ASSERT_TRUE(urls != NULL); 89 90 bool found_urls[arraysize(t_urls_)] = { false }; 91 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()) { 96 found_urls[i] = true; 97 break; 98 } 99 } 100 } 101 102 for (size_t i = 0; i < arraysize(found_urls); ++i) 103 ASSERT_TRUE(found_urls[i]); 104 } 105 106 TEST_F(SearchHostToURLsMapTest, GetURLsForUnknownHost) { 107 const SearchHostToURLsMap::TemplateURLSet* urls = 108 provider_map_->GetURLsForHost("a" + host_); 109 ASSERT_TRUE(urls == NULL); 110 } 111