1 // Copyright (c) 2011 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 virtual void TearDown() { 21 TemplateURLRef::SetGoogleBaseURL(NULL); 22 } 23 24 protected: 25 void SetGoogleBaseURL(const std::string& base_url) const { 26 TemplateURLRef::SetGoogleBaseURL(new std::string(base_url)); 27 } 28 29 scoped_ptr<SearchHostToURLsMap> provider_map_; 30 TemplateURL t_urls_[2]; 31 std::string host_; 32 33 DISALLOW_COPY_AND_ASSIGN(SearchHostToURLsMapTest); 34 }; 35 36 void SearchHostToURLsMapTest::SetUp() { 37 // Add some entries to the search host map. 38 host_ = "www.unittest.com"; 39 t_urls_[0].SetURL("http://" + host_ + "/path1", 0, 0); 40 t_urls_[1].SetURL("http://" + host_ + "/path2", 0, 0); 41 42 std::vector<const TemplateURL*> template_urls; 43 template_urls.push_back(&t_urls_[0]); 44 template_urls.push_back(&t_urls_[1]); 45 46 provider_map_.reset(new SearchHostToURLsMap); 47 UIThreadSearchTermsData search_terms_data; 48 provider_map_->Init(template_urls, search_terms_data); 49 } 50 51 TEST_F(SearchHostToURLsMapTest, Add) { 52 std::string new_host = "example.com"; 53 TemplateURL new_t_url; 54 new_t_url.SetURL("http://" + new_host + "/", 0, 0); 55 UIThreadSearchTermsData search_terms_data; 56 provider_map_->Add(&new_t_url, search_terms_data); 57 58 ASSERT_EQ(&new_t_url, provider_map_->GetTemplateURLForHost(new_host)); 59 } 60 61 TEST_F(SearchHostToURLsMapTest, Remove) { 62 provider_map_->Remove(&t_urls_[0]); 63 64 const TemplateURL* found_url = provider_map_->GetTemplateURLForHost(host_); 65 ASSERT_TRUE(found_url == &t_urls_[1]); 66 67 const TemplateURLSet* urls = provider_map_->GetURLsForHost(host_); 68 ASSERT_TRUE(urls != NULL); 69 70 int url_count = 0; 71 for (TemplateURLSet::const_iterator i = urls->begin(); 72 i != urls->end(); ++i) { 73 url_count++; 74 ASSERT_TRUE(*i == &t_urls_[1]); 75 } 76 ASSERT_EQ(1, url_count); 77 } 78 79 TEST_F(SearchHostToURLsMapTest, Update) { 80 std::string new_host = "example.com"; 81 TemplateURL new_values; 82 new_values.SetURL("http://" + new_host + "/", 0, 0); 83 84 UIThreadSearchTermsData search_terms_data; 85 provider_map_->Update(&t_urls_[0], new_values, search_terms_data); 86 87 ASSERT_EQ(&t_urls_[0], provider_map_->GetTemplateURLForHost(new_host)); 88 ASSERT_EQ(&t_urls_[1], provider_map_->GetTemplateURLForHost(host_)); 89 } 90 91 TEST_F(SearchHostToURLsMapTest, UpdateGoogleBaseURLs) { 92 UIThreadSearchTermsData search_terms_data; 93 std::string google_base_url = "google.com"; 94 SetGoogleBaseURL("http://" + google_base_url +"/"); 95 96 // Add in a url with the templated Google base url. 97 TemplateURL new_t_url; 98 new_t_url.SetURL("{google:baseURL}?q={searchTerms}", 0, 0); 99 provider_map_->Add(&new_t_url, search_terms_data); 100 ASSERT_EQ(&new_t_url, provider_map_->GetTemplateURLForHost(google_base_url)); 101 102 // Now change the Google base url and verify the result. 103 std::string new_google_base_url = "other.com"; 104 SetGoogleBaseURL("http://" + new_google_base_url +"/"); 105 provider_map_->UpdateGoogleBaseURLs(search_terms_data); 106 ASSERT_EQ(&new_t_url, provider_map_->GetTemplateURLForHost( 107 new_google_base_url)); 108 } 109 110 TEST_F(SearchHostToURLsMapTest, GetTemplateURLForKnownHost) { 111 const TemplateURL* found_url = provider_map_->GetTemplateURLForHost(host_); 112 ASSERT_TRUE(found_url == &t_urls_[0] || found_url == &t_urls_[1]); 113 } 114 115 TEST_F(SearchHostToURLsMapTest, GetTemplateURLForUnknownHost) { 116 const TemplateURL* found_url = provider_map_->GetTemplateURLForHost( 117 "a" + host_); 118 ASSERT_TRUE(found_url == NULL); 119 } 120 121 TEST_F(SearchHostToURLsMapTest, GetURLsForKnownHost) { 122 const TemplateURLSet* urls = provider_map_->GetURLsForHost(host_); 123 ASSERT_TRUE(urls != NULL); 124 125 bool found_urls[arraysize(t_urls_)] = { 0 }; 126 127 for (TemplateURLSet::const_iterator i = urls->begin(); 128 i != urls->end(); ++i) { 129 const TemplateURL* url = *i; 130 for (size_t i = 0; i < arraysize(found_urls); ++i) { 131 if (url == &t_urls_[i]) { 132 found_urls[i] = true; 133 break; 134 } 135 } 136 } 137 138 for (size_t i = 0; i < arraysize(found_urls); ++i) 139 ASSERT_TRUE(found_urls[i]); 140 } 141 142 TEST_F(SearchHostToURLsMapTest, GetURLsForUnknownHost) { 143 const TemplateURLSet* urls = provider_map_->GetURLsForHost("a" + host_); 144 ASSERT_TRUE(urls == NULL); 145 } 146