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