| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/profile_resetter/automatic_profile_resetter_delegate.h" | 5 #include "chrome/browser/profile_resetter/automatic_profile_resetter_delegate.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
| 14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 15 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/strings/string_split.h" | 16 #include "base/strings/string_split.h" |
| 17 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
| 18 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
| 19 #include "base/test/values_test_util.h" | 19 #include "base/test/values_test_util.h" |
| 20 #include "base/values.h" | 20 #include "base/values.h" |
| 21 #include "chrome/app/chrome_command_ids.h" | 21 #include "chrome/app/chrome_command_ids.h" |
| 22 #include "chrome/browser/chrome_notification_types.h" | 22 #include "chrome/browser/chrome_notification_types.h" |
| 23 #include "chrome/browser/extensions/extension_service.h" | 23 #include "chrome/browser/extensions/extension_service.h" |
| 24 #include "chrome/browser/extensions/extension_service_test_base.h" | 24 #include "chrome/browser/extensions/extension_service_test_base.h" |
| 25 #include "chrome/browser/google/google_brand.h" | 25 #include "chrome/browser/google/google_brand.h" |
| 26 #include "chrome/browser/profile_resetter/brandcoded_default_settings.h" | 26 #include "chrome/browser/profile_resetter/brandcoded_default_settings.h" |
| 27 #include "chrome/browser/profile_resetter/profile_reset_global_error.h" | 27 #include "chrome/browser/profile_resetter/profile_reset_global_error.h" |
| 28 #include "chrome/browser/search_engines/template_url_service_factory.h" | 28 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 29 #include "chrome/browser/search_engines/template_url_service_test_util.h" | 29 #include "chrome/browser/search_engines/template_url_service_factory_test_util.h
" |
| 30 #include "chrome/browser/ui/global_error/global_error.h" | 30 #include "chrome/browser/ui/global_error/global_error.h" |
| 31 #include "chrome/browser/ui/global_error/global_error_service.h" | 31 #include "chrome/browser/ui/global_error/global_error_service.h" |
| 32 #include "chrome/browser/ui/global_error/global_error_service_factory.h" | 32 #include "chrome/browser/ui/global_error/global_error_service_factory.h" |
| 33 #include "chrome/common/pref_names.h" | 33 #include "chrome/common/pref_names.h" |
| 34 #include "chrome/test/base/testing_pref_service_syncable.h" | 34 #include "chrome/test/base/testing_pref_service_syncable.h" |
| 35 #include "chrome/test/base/testing_profile.h" | 35 #include "chrome/test/base/testing_profile.h" |
| 36 #include "components/search_engines/default_search_manager.h" | 36 #include "components/search_engines/default_search_manager.h" |
| 37 #include "components/search_engines/template_url_prepopulate_data.h" | 37 #include "components/search_engines/template_url_prepopulate_data.h" |
| 38 #include "components/search_engines/template_url_service.h" | 38 #include "components/search_engines/template_url_service.h" |
| 39 #include "content/public/browser/notification_service.h" | 39 #include "content/public/browser/notification_service.h" |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 } | 155 } |
| 156 fetcher->delegate()->OnURLFetchComplete(fetcher); | 156 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 157 } | 157 } |
| 158 | 158 |
| 159 | 159 |
| 160 // Test fixture -------------------------------------------------------------- | 160 // Test fixture -------------------------------------------------------------- |
| 161 | 161 |
| 162 // ExtensionServiceTestBase sets up a TestingProfile with the ExtensionService, | 162 // ExtensionServiceTestBase sets up a TestingProfile with the ExtensionService, |
| 163 // we then add the TemplateURLService, so the ProfileResetter can be exercised. | 163 // we then add the TemplateURLService, so the ProfileResetter can be exercised. |
| 164 class AutomaticProfileResetterDelegateTest | 164 class AutomaticProfileResetterDelegateTest |
| 165 : public extensions::ExtensionServiceTestBase, | 165 : public extensions::ExtensionServiceTestBase { |
| 166 public TemplateURLServiceTestUtilBase { | |
| 167 protected: | 166 protected: |
| 168 AutomaticProfileResetterDelegateTest() {} | 167 AutomaticProfileResetterDelegateTest() {} |
| 169 virtual ~AutomaticProfileResetterDelegateTest() {} | 168 virtual ~AutomaticProfileResetterDelegateTest() {} |
| 170 | 169 |
| 171 virtual void SetUp() OVERRIDE { | 170 virtual void SetUp() OVERRIDE { |
| 172 extensions::ExtensionServiceTestBase::SetUp(); | 171 extensions::ExtensionServiceTestBase::SetUp(); |
| 173 ExtensionServiceInitParams params = CreateDefaultInitParams(); | 172 ExtensionServiceInitParams params = CreateDefaultInitParams(); |
| 174 params.pref_file.clear(); // Prescribes a TestingPrefService to be created. | 173 params.pref_file.clear(); // Prescribes a TestingPrefService to be created. |
| 175 InitializeExtensionService(params); | 174 InitializeExtensionService(params); |
| 176 TemplateURLServiceTestUtilBase::CreateTemplateUrlService(); | 175 template_url_service_test_util_.reset( |
| 176 new TemplateURLServiceFactoryTestUtil(profile_.get())); |
| 177 resetter_delegate_.reset( | 177 resetter_delegate_.reset( |
| 178 new AutomaticProfileResetterDelegateUnderTest(profile())); | 178 new AutomaticProfileResetterDelegateUnderTest(profile())); |
| 179 } | 179 } |
| 180 | 180 |
| 181 virtual void TearDown() OVERRIDE { | 181 virtual void TearDown() OVERRIDE { |
| 182 resetter_delegate_.reset(); | 182 resetter_delegate_.reset(); |
| 183 template_url_service_test_util_.reset(); |
| 183 extensions::ExtensionServiceTestBase::TearDown(); | 184 extensions::ExtensionServiceTestBase::TearDown(); |
| 184 } | 185 } |
| 185 | 186 |
| 186 scoped_ptr<TemplateURL> CreateTestTemplateURL() { | 187 scoped_ptr<TemplateURL> CreateTestTemplateURL() { |
| 187 TemplateURLData data; | 188 TemplateURLData data; |
| 188 | 189 |
| 189 data.SetURL("http://example.com/search?q={searchTerms}"); | 190 data.SetURL("http://example.com/search?q={searchTerms}"); |
| 190 data.suggestions_url = "http://example.com/suggest?q={searchTerms}"; | 191 data.suggestions_url = "http://example.com/suggest?q={searchTerms}"; |
| 191 data.instant_url = "http://example.com/instant?q={searchTerms}"; | 192 data.instant_url = "http://example.com/instant?q={searchTerms}"; |
| 192 data.image_url = "http://example.com/image?q={searchTerms}"; | 193 data.image_url = "http://example.com/image?q={searchTerms}"; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 GlobalErrorServiceFactory::GetForProfile(profile()); | 227 GlobalErrorServiceFactory::GetForProfile(profile()); |
| 227 GlobalError* global_error = global_error_service-> | 228 GlobalError* global_error = global_error_service-> |
| 228 GetGlobalErrorByMenuItemCommandID(IDC_SHOW_SETTINGS_RESET_BUBBLE); | 229 GetGlobalErrorByMenuItemCommandID(IDC_SHOW_SETTINGS_RESET_BUBBLE); |
| 229 EXPECT_EQ(active, !!global_error); | 230 EXPECT_EQ(active, !!global_error); |
| 230 } | 231 } |
| 231 | 232 |
| 232 AutomaticProfileResetterDelegateUnderTest* resetter_delegate() { | 233 AutomaticProfileResetterDelegateUnderTest* resetter_delegate() { |
| 233 return resetter_delegate_.get(); | 234 return resetter_delegate_.get(); |
| 234 } | 235 } |
| 235 | 236 |
| 236 // TemplateURLServiceTestUtilBase: | 237 TemplateURLServiceFactoryTestUtil* template_url_service_test_util() { |
| 237 virtual TestingProfile* profile() const OVERRIDE { return profile_.get(); } | 238 return template_url_service_test_util_.get(); |
| 239 } |
| 238 | 240 |
| 239 private: | 241 private: |
| 242 scoped_ptr<TemplateURLServiceFactoryTestUtil> template_url_service_test_util_; |
| 240 net::TestURLFetcherFactory test_url_fetcher_factory_; | 243 net::TestURLFetcherFactory test_url_fetcher_factory_; |
| 241 scoped_ptr<AutomaticProfileResetterDelegateUnderTest> resetter_delegate_; | 244 scoped_ptr<AutomaticProfileResetterDelegateUnderTest> resetter_delegate_; |
| 242 | 245 |
| 243 DISALLOW_COPY_AND_ASSIGN(AutomaticProfileResetterDelegateTest); | 246 DISALLOW_COPY_AND_ASSIGN(AutomaticProfileResetterDelegateTest); |
| 244 }; | 247 }; |
| 245 | 248 |
| 246 | 249 |
| 247 // Tests --------------------------------------------------------------------- | 250 // Tests --------------------------------------------------------------------- |
| 248 | 251 |
| 249 TEST_F(AutomaticProfileResetterDelegateTest, | 252 TEST_F(AutomaticProfileResetterDelegateTest, |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 339 EXPECT_CALL(mock_target, Run()); | 342 EXPECT_CALL(mock_target, Run()); |
| 340 late_resetter_delegate->RequestCallbackWhenTemplateURLServiceIsLoaded( | 343 late_resetter_delegate->RequestCallbackWhenTemplateURLServiceIsLoaded( |
| 341 mock_target.CreateClosure()); | 344 mock_target.CreateClosure()); |
| 342 base::RunLoop().RunUntilIdle(); | 345 base::RunLoop().RunUntilIdle(); |
| 343 } | 346 } |
| 344 | 347 |
| 345 TEST_F(AutomaticProfileResetterDelegateTest, | 348 TEST_F(AutomaticProfileResetterDelegateTest, |
| 346 DefaultSearchProviderDataWhenNotManaged) { | 349 DefaultSearchProviderDataWhenNotManaged) { |
| 347 TemplateURLService* template_url_service = | 350 TemplateURLService* template_url_service = |
| 348 TemplateURLServiceFactory::GetForProfile(profile()); | 351 TemplateURLServiceFactory::GetForProfile(profile()); |
| 349 TemplateURLServiceTestUtilBase::VerifyLoad(); | 352 template_url_service_test_util()->VerifyLoad(); |
| 350 | 353 |
| 351 // Check that the "managed state" and the details returned by the delegate are | 354 // Check that the "managed state" and the details returned by the delegate are |
| 352 // correct. We verify the details against the data stored by | 355 // correct. We verify the details against the data stored by |
| 353 // TemplateURLService into Prefs. | 356 // TemplateURLService into Prefs. |
| 354 scoped_ptr<TemplateURL> owned_custom_dsp(CreateTestTemplateURL()); | 357 scoped_ptr<TemplateURL> owned_custom_dsp(CreateTestTemplateURL()); |
| 355 TemplateURL* custom_dsp = owned_custom_dsp.get(); | 358 TemplateURL* custom_dsp = owned_custom_dsp.get(); |
| 356 template_url_service->Add(owned_custom_dsp.release()); | 359 template_url_service->Add(owned_custom_dsp.release()); |
| 357 template_url_service->SetUserSelectedDefaultSearchProvider(custom_dsp); | 360 template_url_service->SetUserSelectedDefaultSearchProvider(custom_dsp); |
| 358 | 361 |
| 359 PrefService* prefs = profile()->GetPrefs(); | 362 PrefService* prefs = profile()->GetPrefs(); |
| 360 ASSERT_TRUE(prefs); | 363 ASSERT_TRUE(prefs); |
| 361 scoped_ptr<base::DictionaryValue> dsp_details( | 364 scoped_ptr<base::DictionaryValue> dsp_details( |
| 362 resetter_delegate()->GetDefaultSearchProviderDetails()); | 365 resetter_delegate()->GetDefaultSearchProviderDetails()); |
| 363 const base::DictionaryValue* expected_dsp_details = | 366 const base::DictionaryValue* expected_dsp_details = |
| 364 GetDefaultSearchProviderDetailsFromPrefs(prefs); | 367 GetDefaultSearchProviderDetailsFromPrefs(prefs); |
| 365 | 368 |
| 366 ExpectDetailsMatch(*expected_dsp_details, *dsp_details); | 369 ExpectDetailsMatch(*expected_dsp_details, *dsp_details); |
| 367 EXPECT_FALSE(resetter_delegate()->IsDefaultSearchProviderManaged()); | 370 EXPECT_FALSE(resetter_delegate()->IsDefaultSearchProviderManaged()); |
| 368 } | 371 } |
| 369 | 372 |
| 370 TEST_F(AutomaticProfileResetterDelegateTest, | 373 TEST_F(AutomaticProfileResetterDelegateTest, |
| 371 DefaultSearchProviderDataWhenManaged) { | 374 DefaultSearchProviderDataWhenManaged) { |
| 372 const char kTestSearchURL[] = "http://example.com/search?q={searchTerms}"; | 375 const char kTestSearchURL[] = "http://example.com/search?q={searchTerms}"; |
| 373 const char kTestName[] = "name"; | 376 const char kTestName[] = "name"; |
| 374 const char kTestKeyword[] = "keyword"; | 377 const char kTestKeyword[] = "keyword"; |
| 375 | 378 |
| 376 TemplateURLServiceTestUtilBase::VerifyLoad(); | 379 template_url_service_test_util()->VerifyLoad(); |
| 377 | 380 |
| 378 EXPECT_FALSE(resetter_delegate()->IsDefaultSearchProviderManaged()); | 381 EXPECT_FALSE(resetter_delegate()->IsDefaultSearchProviderManaged()); |
| 379 | 382 |
| 380 // Set managed preferences to emulate a default search provider set by policy. | 383 // Set managed preferences to emulate a default search provider set by policy. |
| 381 SetManagedDefaultSearchPreferences( | 384 template_url_service_test_util()->SetManagedDefaultSearchPreferences( |
| 382 true, kTestName, kTestKeyword, kTestSearchURL, std::string(), | 385 true, kTestName, kTestKeyword, kTestSearchURL, std::string(), |
| 383 std::string(), std::string(), std::string(), std::string()); | 386 std::string(), std::string(), std::string(), std::string()); |
| 384 | 387 |
| 385 EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged()); | 388 EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged()); |
| 386 scoped_ptr<base::DictionaryValue> dsp_details( | 389 scoped_ptr<base::DictionaryValue> dsp_details( |
| 387 resetter_delegate()->GetDefaultSearchProviderDetails()); | 390 resetter_delegate()->GetDefaultSearchProviderDetails()); |
| 388 // Checking that all details are correct is already done by the above test. | 391 // Checking that all details are correct is already done by the above test. |
| 389 // Just make sure details are reported about the correct engine. | 392 // Just make sure details are reported about the correct engine. |
| 390 base::ExpectDictStringValue(kTestSearchURL, *dsp_details, "search_url"); | 393 base::ExpectDictStringValue(kTestSearchURL, *dsp_details, "search_url"); |
| 391 | 394 |
| 392 // Set managed preferences to emulate that having a default search provider is | 395 // Set managed preferences to emulate that having a default search provider is |
| 393 // disabled by policy. | 396 // disabled by policy. |
| 394 RemoveManagedDefaultSearchPreferences(); | 397 template_url_service_test_util()->RemoveManagedDefaultSearchPreferences(); |
| 395 SetManagedDefaultSearchPreferences( | 398 template_url_service_test_util()->SetManagedDefaultSearchPreferences( |
| 396 false, std::string(), std::string(), std::string(), std::string(), | 399 false, std::string(), std::string(), std::string(), std::string(), |
| 397 std::string(), std::string(), std::string(), std::string()); | 400 std::string(), std::string(), std::string(), std::string()); |
| 398 | 401 |
| 399 dsp_details = resetter_delegate()->GetDefaultSearchProviderDetails(); | 402 dsp_details = resetter_delegate()->GetDefaultSearchProviderDetails(); |
| 400 EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged()); | 403 EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged()); |
| 401 EXPECT_TRUE(dsp_details->empty()); | 404 EXPECT_TRUE(dsp_details->empty()); |
| 402 } | 405 } |
| 403 | 406 |
| 404 TEST_F(AutomaticProfileResetterDelegateTest, | 407 TEST_F(AutomaticProfileResetterDelegateTest, |
| 405 GetPrepopulatedSearchProvidersDetails) { | 408 GetPrepopulatedSearchProvidersDetails) { |
| 406 TemplateURLService* template_url_service = | 409 TemplateURLService* template_url_service = |
| 407 TemplateURLServiceFactory::GetForProfile(profile()); | 410 TemplateURLServiceFactory::GetForProfile(profile()); |
| 408 TemplateURLServiceTestUtilBase::VerifyLoad(); | 411 template_url_service_test_util()->VerifyLoad(); |
| 409 | 412 |
| 410 scoped_ptr<base::ListValue> search_engines_details( | 413 scoped_ptr<base::ListValue> search_engines_details( |
| 411 resetter_delegate()->GetPrepopulatedSearchProvidersDetails()); | 414 resetter_delegate()->GetPrepopulatedSearchProvidersDetails()); |
| 412 | 415 |
| 413 // Do the same kind of verification as for GetDefaultSearchEngineDetails: | 416 // Do the same kind of verification as for GetDefaultSearchEngineDetails: |
| 414 // subsequently set each pre-populated engine as the default, so we can verify | 417 // subsequently set each pre-populated engine as the default, so we can verify |
| 415 // that the details returned by the delegate about one particular engine are | 418 // that the details returned by the delegate about one particular engine are |
| 416 // correct in comparison to what has been stored to the Prefs. | 419 // correct in comparison to what has been stored to the Prefs. |
| 417 std::vector<TemplateURL*> prepopulated_engines = | 420 std::vector<TemplateURL*> prepopulated_engines = |
| 418 template_url_service->GetTemplateURLs(); | 421 template_url_service->GetTemplateURLs(); |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 596 if (ProfileResetGlobalError::IsSupportedOnPlatform()) | 599 if (ProfileResetGlobalError::IsSupportedOnPlatform()) |
| 597 ExpectResetPromptState(true /*active*/); | 600 ExpectResetPromptState(true /*active*/); |
| 598 else | 601 else |
| 599 ExpectResetPromptState(false /*active*/); | 602 ExpectResetPromptState(false /*active*/); |
| 600 resetter_delegate()->DismissPrompt(); | 603 resetter_delegate()->DismissPrompt(); |
| 601 ExpectResetPromptState(false /*active*/); | 604 ExpectResetPromptState(false /*active*/); |
| 602 resetter_delegate()->DismissPrompt(); | 605 resetter_delegate()->DismissPrompt(); |
| 603 } | 606 } |
| 604 | 607 |
| 605 } // namespace | 608 } // namespace |
| OLD | NEW |