| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/memory/ref_counted.h" | |
| 10 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/run_loop.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "chrome/browser/search_engines/search_provider_install_data.h" | 12 #include "chrome/browser/search_engines/search_provider_install_data.h" |
| 13 #include "chrome/browser/search_engines/template_url.h" | 13 #include "chrome/browser/search_engines/template_url.h" |
| 14 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" | 14 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" |
| 15 #include "chrome/browser/search_engines/template_url_service.h" | 15 #include "chrome/browser/search_engines/template_url_service.h" |
| 16 #include "chrome/browser/search_engines/template_url_service_test_util.h" | 16 #include "chrome/browser/search_engines/template_url_service_test_util.h" |
| 17 #include "chrome/common/chrome_notification_types.h" | 17 #include "chrome/common/chrome_notification_types.h" |
| 18 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
| 19 #include "chrome/test/base/testing_pref_service_syncable.h" | 19 #include "chrome/test/base/testing_pref_service_syncable.h" |
| 20 #include "chrome/test/base/testing_profile.h" | 20 #include "chrome/test/base/testing_profile.h" |
| 21 #include "content/public/browser/notification_service.h" | 21 #include "content/public/browser/notification_service.h" |
| 22 #include "content/public/browser/notification_source.h" | 22 #include "content/public/browser/notification_source.h" |
| 23 #include "content/public/test/test_browser_thread.h" | 23 #include "content/public/test/test_browser_thread.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 25 | 25 |
| 26 using content::BrowserThread; | 26 using content::BrowserThread; |
| 27 | 27 |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 // TestGetInstallState -------------------------------------------------------- | 30 // TestGetInstallState -------------------------------------------------------- |
| 31 | 31 |
| 32 // Test the SearchProviderInstallData::GetInstallState. | 32 // Test the SearchProviderInstallData::GetInstallState. |
| 33 class TestGetInstallState : | 33 class TestGetInstallState { |
| 34 public base::RefCountedThreadSafe<TestGetInstallState> { | |
| 35 public: | 34 public: |
| 36 explicit TestGetInstallState(SearchProviderInstallData* install_data); | 35 explicit TestGetInstallState(SearchProviderInstallData* install_data); |
| 36 ~TestGetInstallState(); |
| 37 | 37 |
| 38 void set_search_provider_host( | 38 // Runs all of the test cases. |
| 39 const std::string& search_provider_host) { | 39 void RunTests(const std::string& search_provider_host, |
| 40 search_provider_host_ = search_provider_host; | 40 const std::string& default_search_provider_host); |
| 41 } | |
| 42 | |
| 43 void set_default_search_provider_host( | |
| 44 const std::string& default_search_provider_host) { | |
| 45 default_search_provider_host_ = default_search_provider_host; | |
| 46 } | |
| 47 | |
| 48 // Runs the test. Returns true if all passed. False if any failed. | |
| 49 bool RunTests(); | |
| 50 | 41 |
| 51 private: | 42 private: |
| 52 friend class base::RefCountedThreadSafe<TestGetInstallState>; | |
| 53 ~TestGetInstallState(); | |
| 54 | |
| 55 // Starts the test run on the IO thread. | |
| 56 void StartTestOnIOThread(); | |
| 57 | |
| 58 // Callback for when SearchProviderInstallData is ready to have | 43 // Callback for when SearchProviderInstallData is ready to have |
| 59 // GetInstallState called. Runs all of the test cases. | 44 // GetInstallState called. Runs all of the test cases. |
| 60 void DoInstallStateTests(); | 45 void DoInstallStateTests(const std::string& search_provider_host, |
| 46 const std::string& default_search_provider_host); |
| 61 | 47 |
| 62 // Does a verification for one url and its expected state. | 48 // Does a verification for one url and its expected state. |
| 63 void VerifyInstallState(SearchProviderInstallData::State expected_state, | 49 void VerifyInstallState(SearchProviderInstallData::State expected_state, |
| 64 const std::string& url); | 50 const std::string& url); |
| 65 | 51 |
| 66 SearchProviderInstallData* install_data_; | 52 SearchProviderInstallData* install_data_; |
| 67 base::MessageLoop* main_loop_; | |
| 68 | |
| 69 // A host which should be a search provider but not the default. | |
| 70 std::string search_provider_host_; | |
| 71 | |
| 72 // A host which should be a search provider but not the default. | |
| 73 std::string default_search_provider_host_; | |
| 74 | |
| 75 // Used to indicate if DoInstallStateTests passed all test. | |
| 76 bool passed_; | |
| 77 | 53 |
| 78 DISALLOW_COPY_AND_ASSIGN(TestGetInstallState); | 54 DISALLOW_COPY_AND_ASSIGN(TestGetInstallState); |
| 79 }; | 55 }; |
| 80 | 56 |
| 81 TestGetInstallState::TestGetInstallState( | 57 TestGetInstallState::TestGetInstallState( |
| 82 SearchProviderInstallData* install_data) | 58 SearchProviderInstallData* install_data) |
| 83 : install_data_(install_data), | 59 : install_data_(install_data) { |
| 84 main_loop_(NULL), | |
| 85 passed_(false) { | |
| 86 } | |
| 87 | |
| 88 bool TestGetInstallState::RunTests() { | |
| 89 passed_ = true; | |
| 90 | |
| 91 main_loop_ = base::MessageLoop::current(); | |
| 92 | |
| 93 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)->PostTask( | |
| 94 FROM_HERE, | |
| 95 base::Bind(&TestGetInstallState::StartTestOnIOThread, this)); | |
| 96 // Run the current message loop. When the test is finished on the I/O thread, | |
| 97 // it invokes Quit, which unblocks this. | |
| 98 base::MessageLoop::current()->Run(); | |
| 99 main_loop_ = NULL; | |
| 100 | |
| 101 // Let the testing code know what the result is. | |
| 102 return passed_; | |
| 103 } | 60 } |
| 104 | 61 |
| 105 TestGetInstallState::~TestGetInstallState() { | 62 TestGetInstallState::~TestGetInstallState() { |
| 106 } | 63 } |
| 107 | 64 |
| 108 void TestGetInstallState::StartTestOnIOThread() { | 65 void TestGetInstallState::RunTests( |
| 66 const std::string& search_provider_host, |
| 67 const std::string& default_search_provider_host) { |
| 109 install_data_->CallWhenLoaded( | 68 install_data_->CallWhenLoaded( |
| 110 base::Bind(&TestGetInstallState::DoInstallStateTests, this)); | 69 base::Bind(&TestGetInstallState::DoInstallStateTests, |
| 70 base::Unretained(this), |
| 71 search_provider_host, default_search_provider_host)); |
| 72 base::RunLoop().RunUntilIdle(); |
| 111 } | 73 } |
| 112 | 74 |
| 113 void TestGetInstallState::DoInstallStateTests() { | 75 void TestGetInstallState::DoInstallStateTests( |
| 76 const std::string& search_provider_host, |
| 77 const std::string& default_search_provider_host) { |
| 78 SCOPED_TRACE("search provider: " + search_provider_host + |
| 79 ", default search provider: " + default_search_provider_host); |
| 114 // Installed but not default. | 80 // Installed but not default. |
| 115 VerifyInstallState(SearchProviderInstallData::INSTALLED_BUT_NOT_DEFAULT, | 81 VerifyInstallState(SearchProviderInstallData::INSTALLED_BUT_NOT_DEFAULT, |
| 116 "http://" + search_provider_host_ + "/"); | 82 "http://" + search_provider_host + "/"); |
| 117 VerifyInstallState(SearchProviderInstallData::INSTALLED_BUT_NOT_DEFAULT, | 83 VerifyInstallState(SearchProviderInstallData::INSTALLED_BUT_NOT_DEFAULT, |
| 118 "http://" + search_provider_host_ + ":80/"); | 84 "http://" + search_provider_host + ":80/"); |
| 119 | 85 |
| 120 // Not installed. | 86 // Not installed. |
| 121 VerifyInstallState(SearchProviderInstallData::NOT_INSTALLED, | 87 VerifyInstallState(SearchProviderInstallData::NOT_INSTALLED, |
| 122 "http://" + search_provider_host_ + ":96/"); | 88 "http://" + search_provider_host + ":96/"); |
| 123 | 89 |
| 124 // Not installed due to different scheme. | 90 // Not installed due to different scheme. |
| 125 VerifyInstallState(SearchProviderInstallData::NOT_INSTALLED, | 91 VerifyInstallState(SearchProviderInstallData::NOT_INSTALLED, |
| 126 "https://" + search_provider_host_ + "/"); | 92 "https://" + search_provider_host + "/"); |
| 127 | 93 |
| 128 // Not installed. | 94 // Not installed. |
| 129 VerifyInstallState(SearchProviderInstallData::NOT_INSTALLED, | 95 VerifyInstallState(SearchProviderInstallData::NOT_INSTALLED, |
| 130 "http://a" + search_provider_host_ + "/"); | 96 "http://a" + search_provider_host + "/"); |
| 131 | 97 |
| 132 // Installed as default. | 98 // Installed as default. |
| 133 if (!default_search_provider_host_.empty()) { | 99 if (!default_search_provider_host.empty()) { |
| 134 VerifyInstallState(SearchProviderInstallData::INSTALLED_AS_DEFAULT, | 100 VerifyInstallState(SearchProviderInstallData::INSTALLED_AS_DEFAULT, |
| 135 "http://" + default_search_provider_host_ + "/"); | 101 "http://" + default_search_provider_host + "/"); |
| 136 } | 102 } |
| 137 | |
| 138 // All done. | |
| 139 main_loop_->PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); | |
| 140 } | 103 } |
| 141 | 104 |
| 142 void TestGetInstallState::VerifyInstallState( | 105 void TestGetInstallState::VerifyInstallState( |
| 143 SearchProviderInstallData::State expected_state, | 106 SearchProviderInstallData::State expected_state, |
| 144 const std::string& url) { | 107 const std::string& url) { |
| 145 | 108 |
| 146 SearchProviderInstallData::State actual_state = | 109 SearchProviderInstallData::State actual_state = |
| 147 install_data_->GetInstallState(GURL(url)); | 110 install_data_->GetInstallState(GURL(url)); |
| 148 if (expected_state == actual_state) | 111 EXPECT_EQ(expected_state, actual_state) |
| 149 return; | 112 << "GetInstallState for " << url << " failed. Expected " |
| 150 | 113 << expected_state << ". Actual " << actual_state << "."; |
| 151 passed_ = false; | |
| 152 LOG(ERROR) << "GetInstallState for " << url << " failed. Expected " << | |
| 153 expected_state << ". Actual " << actual_state << "."; | |
| 154 } | 114 } |
| 155 | 115 |
| 156 }; // namespace | 116 }; // namespace |
| 157 | 117 |
| 158 | 118 |
| 159 // SearchProviderInstallDataTest ---------------------------------------------- | 119 // SearchProviderInstallDataTest ---------------------------------------------- |
| 160 | 120 |
| 161 // Provides basic test set-up/tear-down functionality needed by all tests | 121 // Provides basic test set-up/tear-down functionality needed by all tests |
| 162 // that use TemplateURLServiceTestUtil. | 122 // that use TemplateURLServiceTestUtil. |
| 163 class SearchProviderInstallDataTest : public testing::Test { | 123 class SearchProviderInstallDataTest : public testing::Test { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 186 : install_data_(NULL) { | 146 : install_data_(NULL) { |
| 187 } | 147 } |
| 188 | 148 |
| 189 void SearchProviderInstallDataTest::SetUp() { | 149 void SearchProviderInstallDataTest::SetUp() { |
| 190 testing::Test::SetUp(); | 150 testing::Test::SetUp(); |
| 191 #if defined(OS_ANDROID) | 151 #if defined(OS_ANDROID) |
| 192 TemplateURLPrepopulateData::InitCountryCode( | 152 TemplateURLPrepopulateData::InitCountryCode( |
| 193 std::string() /* unknown country code */); | 153 std::string() /* unknown country code */); |
| 194 #endif | 154 #endif |
| 195 util_.SetUp(); | 155 util_.SetUp(); |
| 196 util_.StartIOThread(); | |
| 197 install_data_ = new SearchProviderInstallData(util_.profile(), | 156 install_data_ = new SearchProviderInstallData(util_.profile(), |
| 198 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 157 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| 199 content::Source<SearchProviderInstallDataTest>(this)); | 158 content::Source<SearchProviderInstallDataTest>(this)); |
| 200 } | 159 } |
| 201 | 160 |
| 202 void SearchProviderInstallDataTest::TearDown() { | 161 void SearchProviderInstallDataTest::TearDown() { |
| 203 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, install_data_); | 162 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, install_data_); |
| 204 install_data_ = NULL; | 163 install_data_ = NULL; |
| 205 | 164 |
| 206 // Make sure that the install data class on the UI thread gets cleaned up. | 165 // Make sure that the install data class on the UI thread gets cleaned up. |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 | 209 |
| 251 // Actual tests --------------------------------------------------------------- | 210 // Actual tests --------------------------------------------------------------- |
| 252 | 211 |
| 253 TEST_F(SearchProviderInstallDataTest, GetInstallState) { | 212 TEST_F(SearchProviderInstallDataTest, GetInstallState) { |
| 254 // Set up the database. | 213 // Set up the database. |
| 255 util_.ChangeModelToLoadState(); | 214 util_.ChangeModelToLoadState(); |
| 256 std::string host = "www.unittest.com"; | 215 std::string host = "www.unittest.com"; |
| 257 AddNewTemplateURL("http://" + host + "/path", ASCIIToUTF16("unittest")); | 216 AddNewTemplateURL("http://" + host + "/path", ASCIIToUTF16("unittest")); |
| 258 | 217 |
| 259 // Wait for the changes to be saved. | 218 // Wait for the changes to be saved. |
| 260 TemplateURLServiceTestUtil::BlockTillServiceProcessesRequests(); | 219 base::RunLoop().RunUntilIdle(); |
| 261 | 220 |
| 262 // Verify the search providers install state (with no default set). | 221 // Verify the search providers install state (with no default set). |
| 263 scoped_refptr<TestGetInstallState> test_get_install_state( | 222 TestGetInstallState test_get_install_state(install_data_); |
| 264 new TestGetInstallState(install_data_)); | 223 test_get_install_state.RunTests(host, std::string()); |
| 265 test_get_install_state->set_search_provider_host(host); | |
| 266 EXPECT_TRUE(test_get_install_state->RunTests()); | |
| 267 | 224 |
| 268 // Set-up a default and try it all one more time. | 225 // Set-up a default and try it all one more time. |
| 269 std::string default_host = "www.mmm.com"; | 226 std::string default_host = "www.mmm.com"; |
| 270 TemplateURL* default_url = | 227 TemplateURL* default_url = |
| 271 AddNewTemplateURL("http://" + default_host + "/", ASCIIToUTF16("mmm")); | 228 AddNewTemplateURL("http://" + default_host + "/", ASCIIToUTF16("mmm")); |
| 272 util_.model()->SetDefaultSearchProvider(default_url); | 229 util_.model()->SetDefaultSearchProvider(default_url); |
| 273 test_get_install_state->set_default_search_provider_host(default_host); | 230 test_get_install_state.RunTests(host, default_host); |
| 274 EXPECT_TRUE(test_get_install_state->RunTests()); | |
| 275 } | 231 } |
| 276 | 232 |
| 277 | |
| 278 TEST_F(SearchProviderInstallDataTest, ManagedDefaultSearch) { | 233 TEST_F(SearchProviderInstallDataTest, ManagedDefaultSearch) { |
| 279 // Set up the database. | 234 // Set up the database. |
| 280 util_.ChangeModelToLoadState(); | 235 util_.ChangeModelToLoadState(); |
| 281 std::string host = "www.unittest.com"; | 236 std::string host = "www.unittest.com"; |
| 282 AddNewTemplateURL("http://" + host + "/path", ASCIIToUTF16("unittest")); | 237 AddNewTemplateURL("http://" + host + "/path", ASCIIToUTF16("unittest")); |
| 283 | 238 |
| 284 // Set a managed preference that establishes a default search provider. | 239 // Set a managed preference that establishes a default search provider. |
| 285 std::string host2 = "www.managedtest.com"; | 240 std::string host2 = "www.managedtest.com"; |
| 286 std::string url2 = "http://" + host2 + "/p{searchTerms}"; | 241 std::string url2 = "http://" + host2 + "/p{searchTerms}"; |
| 287 SimulateDefaultSearchIsManaged(url2); | 242 SimulateDefaultSearchIsManaged(url2); |
| 288 EXPECT_TRUE(util_.model()->is_default_search_managed()); | 243 EXPECT_TRUE(util_.model()->is_default_search_managed()); |
| 289 | 244 |
| 290 // Wait for the changes to be saved. | 245 // Wait for the changes to be saved. |
| 291 util_.BlockTillServiceProcessesRequests(); | 246 base::RunLoop().RunUntilIdle(); |
| 292 | 247 |
| 293 // Verify the search providers install state. The default search should be | 248 // Verify the search providers install state. The default search should be |
| 294 // the managed one we previously set. | 249 // the managed one we previously set. |
| 295 scoped_refptr<TestGetInstallState> test_get_install_state( | 250 TestGetInstallState test_get_install_state(install_data_); |
| 296 new TestGetInstallState(install_data_)); | 251 test_get_install_state.RunTests(host, host2); |
| 297 test_get_install_state->set_search_provider_host(host); | |
| 298 test_get_install_state->set_default_search_provider_host(host2); | |
| 299 EXPECT_TRUE(test_get_install_state->RunTests()); | |
| 300 } | 252 } |
| 301 | 253 |
| 302 | 254 |
| 303 TEST_F(SearchProviderInstallDataTest, GoogleBaseUrlChange) { | 255 TEST_F(SearchProviderInstallDataTest, GoogleBaseUrlChange) { |
| 304 scoped_refptr<TestGetInstallState> test_get_install_state( | 256 TestGetInstallState test_get_install_state(install_data_); |
| 305 new TestGetInstallState(install_data_)); | |
| 306 | 257 |
| 307 // Set up the database. | 258 // Set up the database. |
| 308 util_.ChangeModelToLoadState(); | 259 util_.ChangeModelToLoadState(); |
| 309 std::string google_host = "w.com"; | 260 std::string google_host = "w.com"; |
| 310 util_.SetGoogleBaseURL(GURL("http://" + google_host + "/")); | 261 util_.SetGoogleBaseURL(GURL("http://" + google_host + "/")); |
| 311 // Wait for the I/O thread to process the update notification. | 262 // Wait for the I/O thread to process the update notification. |
| 312 TemplateURLServiceTestUtil::BlockTillIOThreadProcessesRequests(); | 263 base::RunLoop().RunUntilIdle(); |
| 313 | 264 |
| 314 AddNewTemplateURL("{google:baseURL}?q={searchTerms}", ASCIIToUTF16("t")); | 265 AddNewTemplateURL("{google:baseURL}?q={searchTerms}", ASCIIToUTF16("t")); |
| 315 TemplateURL* default_url = | 266 TemplateURL* default_url = |
| 316 AddNewTemplateURL("http://d.com/", ASCIIToUTF16("d")); | 267 AddNewTemplateURL("http://d.com/", ASCIIToUTF16("d")); |
| 317 util_.model()->SetDefaultSearchProvider(default_url); | 268 util_.model()->SetDefaultSearchProvider(default_url); |
| 318 | 269 |
| 319 // Wait for the changes to be saved. | 270 // Wait for the changes to be saved. |
| 320 TemplateURLServiceTestUtil::BlockTillServiceProcessesRequests(); | 271 base::RunLoop().RunUntilIdle(); |
| 321 | 272 |
| 322 // Verify the search providers install state (with no default set). | 273 // Verify the search providers install state (with no default set). |
| 323 test_get_install_state->set_search_provider_host(google_host); | 274 test_get_install_state.RunTests(google_host, std::string()); |
| 324 EXPECT_TRUE(test_get_install_state->RunTests()); | |
| 325 | 275 |
| 326 // Change the Google base url. | 276 // Change the Google base url. |
| 327 google_host = "foo.com"; | 277 google_host = "foo.com"; |
| 328 util_.SetGoogleBaseURL(GURL("http://" + google_host + "/")); | 278 util_.SetGoogleBaseURL(GURL("http://" + google_host + "/")); |
| 329 // Wait for the I/O thread to process the update notification. | 279 // Wait for the I/O thread to process the update notification. |
| 330 TemplateURLServiceTestUtil::BlockTillIOThreadProcessesRequests(); | 280 base::RunLoop().RunUntilIdle(); |
| 331 | 281 |
| 332 // Verify that the change got picked up. | 282 // Verify that the change got picked up. |
| 333 test_get_install_state->set_search_provider_host(google_host); | 283 test_get_install_state.RunTests(google_host, std::string()); |
| 334 EXPECT_TRUE(test_get_install_state->RunTests()); | |
| 335 } | 284 } |
| OLD | NEW |