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 <string> 6 7 #include "base/basictypes.h" 8 #include "base/bind.h" 9 #include "base/memory/ref_counted.h" 10 #include "base/memory/scoped_ptr.h" 11 #include "base/message_loop/message_loop.h" 12 #include "base/run_loop.h" 13 #include "base/strings/utf_string_conversions.h" 14 #include "chrome/browser/search_engines/search_provider_install_data.h" 15 #include "chrome/browser/search_engines/template_url.h" 16 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" 17 #include "chrome/browser/search_engines/template_url_service.h" 18 #include "chrome/browser/search_engines/template_url_service_test_util.h" 19 #include "chrome/common/pref_names.h" 20 #include "chrome/test/base/testing_pref_service_syncable.h" 21 #include "chrome/test/base/testing_profile.h" 22 #include "content/public/test/mock_render_process_host.h" 23 #include "content/public/test/test_browser_thread.h" 24 #include "testing/gtest/include/gtest/gtest.h" 25 26 using content::BrowserThread; 27 28 namespace { 29 30 // TestGetInstallState -------------------------------------------------------- 31 32 // Test the SearchProviderInstallData::GetInstallState. 33 class TestGetInstallState { 34 public: 35 explicit TestGetInstallState(SearchProviderInstallData* install_data); 36 37 // Runs all of the test cases. 38 void RunTests(const std::string& search_provider_host, 39 const std::string& default_search_provider_host); 40 41 private: 42 // Callback for when SearchProviderInstallData is ready to have 43 // GetInstallState called. Runs all of the test cases. 44 void DoInstallStateTests(const std::string& search_provider_host, 45 const std::string& default_search_provider_host); 46 47 // Does a verification for one url and its expected state. 48 void VerifyInstallState(SearchProviderInstallData::State expected_state, 49 const std::string& url); 50 51 SearchProviderInstallData* install_data_; 52 53 DISALLOW_COPY_AND_ASSIGN(TestGetInstallState); 54 }; 55 56 TestGetInstallState::TestGetInstallState( 57 SearchProviderInstallData* install_data) 58 : install_data_(install_data) { 59 } 60 61 void TestGetInstallState::RunTests( 62 const std::string& search_provider_host, 63 const std::string& default_search_provider_host) { 64 install_data_->CallWhenLoaded( 65 base::Bind(&TestGetInstallState::DoInstallStateTests, 66 base::Unretained(this), 67 search_provider_host, default_search_provider_host)); 68 base::RunLoop().RunUntilIdle(); 69 } 70 71 void TestGetInstallState::DoInstallStateTests( 72 const std::string& search_provider_host, 73 const std::string& default_search_provider_host) { 74 SCOPED_TRACE("search provider: " + search_provider_host + 75 ", default search provider: " + default_search_provider_host); 76 // Installed but not default. 77 VerifyInstallState(SearchProviderInstallData::INSTALLED_BUT_NOT_DEFAULT, 78 "http://" + search_provider_host + "/"); 79 VerifyInstallState(SearchProviderInstallData::INSTALLED_BUT_NOT_DEFAULT, 80 "http://" + search_provider_host + ":80/"); 81 82 // Not installed. 83 VerifyInstallState(SearchProviderInstallData::NOT_INSTALLED, 84 "http://" + search_provider_host + ":96/"); 85 86 // Not installed due to different scheme. 87 VerifyInstallState(SearchProviderInstallData::NOT_INSTALLED, 88 "https://" + search_provider_host + "/"); 89 90 // Not installed. 91 VerifyInstallState(SearchProviderInstallData::NOT_INSTALLED, 92 "http://a" + search_provider_host + "/"); 93 94 // Installed as default. 95 if (!default_search_provider_host.empty()) { 96 VerifyInstallState(SearchProviderInstallData::INSTALLED_AS_DEFAULT, 97 "http://" + default_search_provider_host + "/"); 98 } 99 } 100 101 void TestGetInstallState::VerifyInstallState( 102 SearchProviderInstallData::State expected_state, 103 const std::string& url) { 104 105 SearchProviderInstallData::State actual_state = 106 install_data_->GetInstallState(GURL(url)); 107 EXPECT_EQ(expected_state, actual_state) 108 << "GetInstallState for " << url << " failed. Expected " 109 << expected_state << ". Actual " << actual_state << "."; 110 } 111 112 } // namespace 113 114 // SearchProviderInstallDataTest ---------------------------------------------- 115 116 // Provides basic test set-up/tear-down functionality needed by all tests 117 // that use TemplateURLServiceTestUtil. 118 class SearchProviderInstallDataTest : public testing::Test { 119 public: 120 SearchProviderInstallDataTest(); 121 122 virtual void SetUp() OVERRIDE; 123 virtual void TearDown() OVERRIDE; 124 125 protected: 126 TemplateURL* AddNewTemplateURL(const std::string& url, 127 const base::string16& keyword); 128 129 // Sets the Google base URL to |base_url| and runs the IO thread for 130 // |SearchProviderInstallData| to process the update. 131 void SetGoogleBaseURLAndProcessOnIOThread(GURL base_url); 132 133 TemplateURLServiceTestUtil util_; 134 135 // Provides the search provider install state on the I/O thread. It must be 136 // deleted on the I/O thread, which is why it isn't a scoped_ptr. 137 SearchProviderInstallData* install_data_; 138 139 // A mock RenderProcessHost that the SearchProviderInstallData will scope its 140 // lifetime to. 141 scoped_ptr<content::MockRenderProcessHost> process_; 142 143 DISALLOW_COPY_AND_ASSIGN(SearchProviderInstallDataTest); 144 }; 145 146 SearchProviderInstallDataTest::SearchProviderInstallDataTest() 147 : install_data_(NULL) { 148 } 149 150 void SearchProviderInstallDataTest::SetUp() { 151 testing::Test::SetUp(); 152 #if defined(OS_ANDROID) 153 TemplateURLPrepopulateData::InitCountryCode( 154 std::string() /* unknown country code */); 155 #endif 156 util_.SetUp(); 157 process_.reset(new content::MockRenderProcessHost(util_.profile())); 158 install_data_ = 159 new SearchProviderInstallData(util_.profile(), process_.get()); 160 } 161 162 void SearchProviderInstallDataTest::TearDown() { 163 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, install_data_); 164 install_data_ = NULL; 165 166 // Make sure that the install data class on the UI thread gets cleaned up. 167 // It doesn't matter that this happens after install_data_ is deleted. 168 process_.reset(); 169 170 util_.TearDown(); 171 testing::Test::TearDown(); 172 } 173 174 TemplateURL* SearchProviderInstallDataTest::AddNewTemplateURL( 175 const std::string& url, 176 const base::string16& keyword) { 177 TemplateURLData data; 178 data.short_name = keyword; 179 data.SetKeyword(keyword); 180 data.SetURL(url); 181 TemplateURL* t_url = new TemplateURL(data); 182 util_.model()->Add(t_url); 183 return t_url; 184 } 185 186 void SearchProviderInstallDataTest::SetGoogleBaseURLAndProcessOnIOThread( 187 GURL base_url) { 188 util_.SetGoogleBaseURL(base_url); 189 BrowserThread::PostTask( 190 BrowserThread::IO, 191 FROM_HERE, 192 base::Bind(&SearchProviderInstallData::OnGoogleURLChange, 193 base::Unretained(install_data_), 194 base_url.spec())); 195 196 // Wait for the I/O thread to process the update notification. 197 base::RunLoop().RunUntilIdle(); 198 } 199 200 // Actual tests --------------------------------------------------------------- 201 202 TEST_F(SearchProviderInstallDataTest, GetInstallState) { 203 // Set up the database. 204 util_.ChangeModelToLoadState(); 205 std::string host = "www.unittest.com"; 206 AddNewTemplateURL("http://" + host + "/path", base::ASCIIToUTF16("unittest")); 207 208 // Wait for the changes to be saved. 209 base::RunLoop().RunUntilIdle(); 210 211 // Verify the search providers install state (with no default set). 212 TestGetInstallState test_get_install_state(install_data_); 213 test_get_install_state.RunTests(host, std::string()); 214 215 // Set-up a default and try it all one more time. 216 std::string default_host = "www.mmm.com"; 217 TemplateURL* default_url = 218 AddNewTemplateURL("http://" + default_host + "/", 219 base::ASCIIToUTF16("mmm")); 220 util_.model()->SetUserSelectedDefaultSearchProvider(default_url); 221 test_get_install_state.RunTests(host, default_host); 222 } 223 224 TEST_F(SearchProviderInstallDataTest, ManagedDefaultSearch) { 225 // Set up the database. 226 util_.ChangeModelToLoadState(); 227 std::string host = "www.unittest.com"; 228 AddNewTemplateURL("http://" + host + "/path", base::ASCIIToUTF16("unittest")); 229 230 // Set a managed preference that establishes a default search provider. 231 std::string host2 = "www.managedtest.com"; 232 util_.SetManagedDefaultSearchPreferences( 233 true, 234 "managed", 235 "managed", 236 "http://" + host2 + "/p{searchTerms}", 237 std::string(), 238 std::string(), 239 std::string(), 240 std::string(), 241 std::string()); 242 243 EXPECT_TRUE(util_.model()->is_default_search_managed()); 244 245 // Wait for the changes to be saved. 246 base::RunLoop().RunUntilIdle(); 247 248 // Verify the search providers install state. The default search should be 249 // the managed one we previously set. 250 TestGetInstallState test_get_install_state(install_data_); 251 test_get_install_state.RunTests(host, host2); 252 } 253 254 TEST_F(SearchProviderInstallDataTest, GoogleBaseUrlChange) { 255 TestGetInstallState test_get_install_state(install_data_); 256 257 // Set up the database. 258 util_.ChangeModelToLoadState(); 259 std::string google_host = "w.com"; 260 SetGoogleBaseURLAndProcessOnIOThread(GURL("http://" + google_host + "/")); 261 262 AddNewTemplateURL("{google:baseURL}?q={searchTerms}", 263 base::ASCIIToUTF16("t")); 264 TemplateURL* default_url = 265 AddNewTemplateURL("http://d.com/", base::ASCIIToUTF16("d")); 266 util_.model()->SetUserSelectedDefaultSearchProvider(default_url); 267 268 // Wait for the changes to be saved. 269 base::RunLoop().RunUntilIdle(); 270 271 // Verify the search providers install state (with no default set). 272 test_get_install_state.RunTests(google_host, std::string()); 273 274 // Change the Google base url. 275 google_host = "foo.com"; 276 SetGoogleBaseURLAndProcessOnIOThread(GURL("http://" + google_host + "/")); 277 278 // Verify that the change got picked up. 279 test_get_install_state.RunTests(google_host, std::string()); 280 } 281