| 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" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "chrome/browser/search_engines/search_provider_install_data.h" | 14 #include "chrome/browser/search_engines/search_provider_install_data.h" |
| 15 #include "chrome/browser/search_engines/template_url_service_test_util.h" | 15 #include "chrome/browser/search_engines/template_url_service_test_util.h" |
| 16 #include "chrome/test/base/testing_pref_service_syncable.h" | 16 #include "chrome/test/base/testing_pref_service_syncable.h" |
| 17 #include "chrome/test/base/testing_profile.h" | 17 #include "chrome/test/base/testing_profile.h" |
| 18 #include "components/search_engines/search_terms_data.h" | 18 #include "components/search_engines/search_terms_data.h" |
| 19 #include "components/search_engines/template_url.h" | 19 #include "components/search_engines/template_url.h" |
| 20 #include "components/search_engines/template_url_prepopulate_data.h" | 20 #include "components/search_engines/template_url_prepopulate_data.h" |
| 21 #include "components/search_engines/template_url_service.h" | 21 #include "components/search_engines/template_url_service.h" |
| 22 #include "content/public/browser/browser_thread.h" |
| 22 #include "content/public/test/mock_render_process_host.h" | 23 #include "content/public/test/mock_render_process_host.h" |
| 23 #include "content/public/test/test_browser_thread.h" | 24 #include "content/public/test/test_browser_thread_bundle.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 25 | 26 |
| 26 using content::BrowserThread; | 27 using content::BrowserThread; |
| 27 | 28 |
| 28 namespace { | 29 namespace { |
| 29 | 30 |
| 30 // TestGetInstallState -------------------------------------------------------- | 31 // TestGetInstallState -------------------------------------------------------- |
| 31 | 32 |
| 32 // Test the SearchProviderInstallData::GetInstallState. | 33 // Test the SearchProviderInstallData::GetInstallState. |
| 33 class TestGetInstallState { | 34 class TestGetInstallState { |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 | 116 |
| 116 // Provides basic test set-up/tear-down functionality needed by all tests | 117 // Provides basic test set-up/tear-down functionality needed by all tests |
| 117 // that use TemplateURLServiceTestUtil. | 118 // that use TemplateURLServiceTestUtil. |
| 118 class SearchProviderInstallDataTest : public testing::Test { | 119 class SearchProviderInstallDataTest : public testing::Test { |
| 119 public: | 120 public: |
| 120 SearchProviderInstallDataTest(); | 121 SearchProviderInstallDataTest(); |
| 121 | 122 |
| 122 virtual void SetUp() OVERRIDE; | 123 virtual void SetUp() OVERRIDE; |
| 123 virtual void TearDown() OVERRIDE; | 124 virtual void TearDown() OVERRIDE; |
| 124 | 125 |
| 125 protected: | |
| 126 TemplateURL* AddNewTemplateURL(const std::string& url, | 126 TemplateURL* AddNewTemplateURL(const std::string& url, |
| 127 const base::string16& keyword); | 127 const base::string16& keyword); |
| 128 | 128 |
| 129 // Sets the Google base URL to |base_url| and runs the IO thread for | 129 // Sets the Google base URL to |base_url| and runs the IO thread for |
| 130 // |SearchProviderInstallData| to process the update. | 130 // |SearchProviderInstallData| to process the update. |
| 131 void SetGoogleBaseURLAndProcessOnIOThread(GURL base_url); | 131 void SetGoogleBaseURLAndProcessOnIOThread(GURL base_url); |
| 132 | 132 |
| 133 TemplateURLServiceTestUtil* util() { return &util_; } |
| 134 SearchProviderInstallData* install_data() { return install_data_; } |
| 135 |
| 136 private: |
| 137 content::TestBrowserThreadBundle thread_bundle_; // To set up BrowserThreads. |
| 133 TemplateURLServiceTestUtil util_; | 138 TemplateURLServiceTestUtil util_; |
| 134 | 139 |
| 135 // Provides the search provider install state on the I/O thread. It must be | 140 // 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. | 141 // deleted on the I/O thread, which is why it isn't a scoped_ptr. |
| 137 SearchProviderInstallData* install_data_; | 142 SearchProviderInstallData* install_data_; |
| 138 | 143 |
| 139 // A mock RenderProcessHost that the SearchProviderInstallData will scope its | 144 // A mock RenderProcessHost that the SearchProviderInstallData will scope its |
| 140 // lifetime to. | 145 // lifetime to. |
| 141 scoped_ptr<content::MockRenderProcessHost> process_; | 146 scoped_ptr<content::MockRenderProcessHost> process_; |
| 142 | 147 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 base_url.spec())); | 198 base_url.spec())); |
| 194 | 199 |
| 195 // Wait for the I/O thread to process the update notification. | 200 // Wait for the I/O thread to process the update notification. |
| 196 base::RunLoop().RunUntilIdle(); | 201 base::RunLoop().RunUntilIdle(); |
| 197 } | 202 } |
| 198 | 203 |
| 199 // Actual tests --------------------------------------------------------------- | 204 // Actual tests --------------------------------------------------------------- |
| 200 | 205 |
| 201 TEST_F(SearchProviderInstallDataTest, GetInstallState) { | 206 TEST_F(SearchProviderInstallDataTest, GetInstallState) { |
| 202 // Set up the database. | 207 // Set up the database. |
| 203 util_.ChangeModelToLoadState(); | 208 util()->ChangeModelToLoadState(); |
| 204 std::string host = "www.unittest.com"; | 209 std::string host = "www.unittest.com"; |
| 205 AddNewTemplateURL("http://" + host + "/path", base::ASCIIToUTF16("unittest")); | 210 AddNewTemplateURL("http://" + host + "/path", base::ASCIIToUTF16("unittest")); |
| 206 | 211 |
| 207 // Wait for the changes to be saved. | 212 // Wait for the changes to be saved. |
| 208 base::RunLoop().RunUntilIdle(); | 213 base::RunLoop().RunUntilIdle(); |
| 209 | 214 |
| 210 // Verify the search providers install state (with no default set). | 215 // Verify the search providers install state (with no default set). |
| 211 TestGetInstallState test_get_install_state(install_data_); | 216 TestGetInstallState test_get_install_state(install_data()); |
| 212 test_get_install_state.RunTests(host, std::string()); | 217 test_get_install_state.RunTests(host, std::string()); |
| 213 | 218 |
| 214 // Set-up a default and try it all one more time. | 219 // Set-up a default and try it all one more time. |
| 215 std::string default_host = "www.mmm.com"; | 220 std::string default_host = "www.mmm.com"; |
| 216 TemplateURL* default_url = | 221 TemplateURL* default_url = |
| 217 AddNewTemplateURL("http://" + default_host + "/", | 222 AddNewTemplateURL("http://" + default_host + "/", |
| 218 base::ASCIIToUTF16("mmm")); | 223 base::ASCIIToUTF16("mmm")); |
| 219 util_.model()->SetUserSelectedDefaultSearchProvider(default_url); | 224 util()->model()->SetUserSelectedDefaultSearchProvider(default_url); |
| 220 test_get_install_state.RunTests(host, default_host); | 225 test_get_install_state.RunTests(host, default_host); |
| 221 } | 226 } |
| 222 | 227 |
| 223 TEST_F(SearchProviderInstallDataTest, ManagedDefaultSearch) { | 228 TEST_F(SearchProviderInstallDataTest, ManagedDefaultSearch) { |
| 224 // Set up the database. | 229 // Set up the database. |
| 225 util_.ChangeModelToLoadState(); | 230 util()->ChangeModelToLoadState(); |
| 226 std::string host = "www.unittest.com"; | 231 std::string host = "www.unittest.com"; |
| 227 AddNewTemplateURL("http://" + host + "/path", base::ASCIIToUTF16("unittest")); | 232 AddNewTemplateURL("http://" + host + "/path", base::ASCIIToUTF16("unittest")); |
| 228 | 233 |
| 229 // Set a managed preference that establishes a default search provider. | 234 // Set a managed preference that establishes a default search provider. |
| 230 std::string host2 = "www.managedtest.com"; | 235 std::string host2 = "www.managedtest.com"; |
| 231 util_.SetManagedDefaultSearchPreferences( | 236 util()->SetManagedDefaultSearchPreferences( |
| 232 true, | 237 true, |
| 233 "managed", | 238 "managed", |
| 234 "managed", | 239 "managed", |
| 235 "http://" + host2 + "/p{searchTerms}", | 240 "http://" + host2 + "/p{searchTerms}", |
| 236 std::string(), | 241 std::string(), |
| 237 std::string(), | 242 std::string(), |
| 238 std::string(), | 243 std::string(), |
| 239 std::string(), | 244 std::string(), |
| 240 std::string()); | 245 std::string()); |
| 241 | 246 |
| 242 EXPECT_TRUE(util_.model()->is_default_search_managed()); | 247 EXPECT_TRUE(util()->model()->is_default_search_managed()); |
| 243 | 248 |
| 244 // Wait for the changes to be saved. | 249 // Wait for the changes to be saved. |
| 245 base::RunLoop().RunUntilIdle(); | 250 base::RunLoop().RunUntilIdle(); |
| 246 | 251 |
| 247 // Verify the search providers install state. The default search should be | 252 // Verify the search providers install state. The default search should be |
| 248 // the managed one we previously set. | 253 // the managed one we previously set. |
| 249 TestGetInstallState test_get_install_state(install_data_); | 254 TestGetInstallState test_get_install_state(install_data()); |
| 250 test_get_install_state.RunTests(host, host2); | 255 test_get_install_state.RunTests(host, host2); |
| 251 } | 256 } |
| 252 | 257 |
| 253 TEST_F(SearchProviderInstallDataTest, GoogleBaseUrlChange) { | 258 TEST_F(SearchProviderInstallDataTest, GoogleBaseUrlChange) { |
| 254 TestGetInstallState test_get_install_state(install_data_); | 259 TestGetInstallState test_get_install_state(install_data()); |
| 255 | 260 |
| 256 // Set up the database. | 261 // Set up the database. |
| 257 util_.ChangeModelToLoadState(); | 262 util()->ChangeModelToLoadState(); |
| 258 std::string google_host = "w.com"; | 263 std::string google_host = "w.com"; |
| 259 SetGoogleBaseURLAndProcessOnIOThread(GURL("http://" + google_host + "/")); | 264 SetGoogleBaseURLAndProcessOnIOThread(GURL("http://" + google_host + "/")); |
| 260 | 265 |
| 261 AddNewTemplateURL("{google:baseURL}?q={searchTerms}", | 266 AddNewTemplateURL("{google:baseURL}?q={searchTerms}", |
| 262 base::ASCIIToUTF16("t")); | 267 base::ASCIIToUTF16("t")); |
| 263 TemplateURL* default_url = | 268 TemplateURL* default_url = |
| 264 AddNewTemplateURL("http://d.com/", base::ASCIIToUTF16("d")); | 269 AddNewTemplateURL("http://d.com/", base::ASCIIToUTF16("d")); |
| 265 util_.model()->SetUserSelectedDefaultSearchProvider(default_url); | 270 util()->model()->SetUserSelectedDefaultSearchProvider(default_url); |
| 266 | 271 |
| 267 // Wait for the changes to be saved. | 272 // Wait for the changes to be saved. |
| 268 base::RunLoop().RunUntilIdle(); | 273 base::RunLoop().RunUntilIdle(); |
| 269 | 274 |
| 270 // Verify the search providers install state (with no default set). | 275 // Verify the search providers install state (with no default set). |
| 271 test_get_install_state.RunTests(google_host, std::string()); | 276 test_get_install_state.RunTests(google_host, std::string()); |
| 272 | 277 |
| 273 // Change the Google base url. | 278 // Change the Google base url. |
| 274 google_host = "foo.com"; | 279 google_host = "foo.com"; |
| 275 SetGoogleBaseURLAndProcessOnIOThread(GURL("http://" + google_host + "/")); | 280 SetGoogleBaseURLAndProcessOnIOThread(GURL("http://" + google_host + "/")); |
| 276 | 281 |
| 277 // Verify that the change got picked up. | 282 // Verify that the change got picked up. |
| 278 test_get_install_state.RunTests(google_host, std::string()); | 283 test_get_install_state.RunTests(google_host, std::string()); |
| 279 } | 284 } |
| OLD | NEW |