| 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 util_.reset(new TemplateURLServiceFactoryTestUtil(profile_.get())); |
| 177 resetter_delegate_.reset( | 176 resetter_delegate_.reset( |
| 178 new AutomaticProfileResetterDelegateUnderTest(profile())); | 177 new AutomaticProfileResetterDelegateUnderTest(profile())); |
| 179 } | 178 } |
| 180 | 179 |
| 181 virtual void TearDown() OVERRIDE { | 180 virtual void TearDown() OVERRIDE { |
| 182 resetter_delegate_.reset(); | 181 resetter_delegate_.reset(); |
| 182 util_.reset(); |
| 183 extensions::ExtensionServiceTestBase::TearDown(); | 183 extensions::ExtensionServiceTestBase::TearDown(); |
| 184 } | 184 } |
| 185 | 185 |
| 186 scoped_ptr<TemplateURL> CreateTestTemplateURL() { | 186 scoped_ptr<TemplateURL> CreateTestTemplateURL() { |
| 187 TemplateURLData data; | 187 TemplateURLData data; |
| 188 | 188 |
| 189 data.SetURL("http://example.com/search?q={searchTerms}"); | 189 data.SetURL("http://example.com/search?q={searchTerms}"); |
| 190 data.suggestions_url = "http://example.com/suggest?q={searchTerms}"; | 190 data.suggestions_url = "http://example.com/suggest?q={searchTerms}"; |
| 191 data.instant_url = "http://example.com/instant?q={searchTerms}"; | 191 data.instant_url = "http://example.com/instant?q={searchTerms}"; |
| 192 data.image_url = "http://example.com/image?q={searchTerms}"; | 192 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()); | 226 GlobalErrorServiceFactory::GetForProfile(profile()); |
| 227 GlobalError* global_error = global_error_service-> | 227 GlobalError* global_error = global_error_service-> |
| 228 GetGlobalErrorByMenuItemCommandID(IDC_SHOW_SETTINGS_RESET_BUBBLE); | 228 GetGlobalErrorByMenuItemCommandID(IDC_SHOW_SETTINGS_RESET_BUBBLE); |
| 229 EXPECT_EQ(active, !!global_error); | 229 EXPECT_EQ(active, !!global_error); |
| 230 } | 230 } |
| 231 | 231 |
| 232 AutomaticProfileResetterDelegateUnderTest* resetter_delegate() { | 232 AutomaticProfileResetterDelegateUnderTest* resetter_delegate() { |
| 233 return resetter_delegate_.get(); | 233 return resetter_delegate_.get(); |
| 234 } | 234 } |
| 235 | 235 |
| 236 // TemplateURLServiceTestUtilBase: | 236 TemplateURLServiceFactoryTestUtil* util() { return util_.get(); } |
| 237 virtual TestingProfile* profile() const OVERRIDE { return profile_.get(); } | |
| 238 | 237 |
| 239 private: | 238 private: |
| 239 scoped_ptr<TemplateURLServiceFactoryTestUtil> util_; |
| 240 net::TestURLFetcherFactory test_url_fetcher_factory_; | 240 net::TestURLFetcherFactory test_url_fetcher_factory_; |
| 241 scoped_ptr<AutomaticProfileResetterDelegateUnderTest> resetter_delegate_; | 241 scoped_ptr<AutomaticProfileResetterDelegateUnderTest> resetter_delegate_; |
| 242 | 242 |
| 243 DISALLOW_COPY_AND_ASSIGN(AutomaticProfileResetterDelegateTest); | 243 DISALLOW_COPY_AND_ASSIGN(AutomaticProfileResetterDelegateTest); |
| 244 }; | 244 }; |
| 245 | 245 |
| 246 | 246 |
| 247 // Tests --------------------------------------------------------------------- | 247 // Tests --------------------------------------------------------------------- |
| 248 | 248 |
| 249 TEST_F(AutomaticProfileResetterDelegateTest, | 249 TEST_F(AutomaticProfileResetterDelegateTest, |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 339 EXPECT_CALL(mock_target, Run()); | 339 EXPECT_CALL(mock_target, Run()); |
| 340 late_resetter_delegate->RequestCallbackWhenTemplateURLServiceIsLoaded( | 340 late_resetter_delegate->RequestCallbackWhenTemplateURLServiceIsLoaded( |
| 341 mock_target.CreateClosure()); | 341 mock_target.CreateClosure()); |
| 342 base::RunLoop().RunUntilIdle(); | 342 base::RunLoop().RunUntilIdle(); |
| 343 } | 343 } |
| 344 | 344 |
| 345 TEST_F(AutomaticProfileResetterDelegateTest, | 345 TEST_F(AutomaticProfileResetterDelegateTest, |
| 346 DefaultSearchProviderDataWhenNotManaged) { | 346 DefaultSearchProviderDataWhenNotManaged) { |
| 347 TemplateURLService* template_url_service = | 347 TemplateURLService* template_url_service = |
| 348 TemplateURLServiceFactory::GetForProfile(profile()); | 348 TemplateURLServiceFactory::GetForProfile(profile()); |
| 349 TemplateURLServiceTestUtilBase::VerifyLoad(); | 349 util()->VerifyLoad(); |
| 350 | 350 |
| 351 // Check that the "managed state" and the details returned by the delegate are | 351 // Check that the "managed state" and the details returned by the delegate are |
| 352 // correct. We verify the details against the data stored by | 352 // correct. We verify the details against the data stored by |
| 353 // TemplateURLService into Prefs. | 353 // TemplateURLService into Prefs. |
| 354 scoped_ptr<TemplateURL> owned_custom_dsp(CreateTestTemplateURL()); | 354 scoped_ptr<TemplateURL> owned_custom_dsp(CreateTestTemplateURL()); |
| 355 TemplateURL* custom_dsp = owned_custom_dsp.get(); | 355 TemplateURL* custom_dsp = owned_custom_dsp.get(); |
| 356 template_url_service->Add(owned_custom_dsp.release()); | 356 template_url_service->Add(owned_custom_dsp.release()); |
| 357 template_url_service->SetUserSelectedDefaultSearchProvider(custom_dsp); | 357 template_url_service->SetUserSelectedDefaultSearchProvider(custom_dsp); |
| 358 | 358 |
| 359 PrefService* prefs = profile()->GetPrefs(); | 359 PrefService* prefs = profile()->GetPrefs(); |
| 360 ASSERT_TRUE(prefs); | 360 ASSERT_TRUE(prefs); |
| 361 scoped_ptr<base::DictionaryValue> dsp_details( | 361 scoped_ptr<base::DictionaryValue> dsp_details( |
| 362 resetter_delegate()->GetDefaultSearchProviderDetails()); | 362 resetter_delegate()->GetDefaultSearchProviderDetails()); |
| 363 const base::DictionaryValue* expected_dsp_details = | 363 const base::DictionaryValue* expected_dsp_details = |
| 364 GetDefaultSearchProviderDetailsFromPrefs(prefs); | 364 GetDefaultSearchProviderDetailsFromPrefs(prefs); |
| 365 | 365 |
| 366 ExpectDetailsMatch(*expected_dsp_details, *dsp_details); | 366 ExpectDetailsMatch(*expected_dsp_details, *dsp_details); |
| 367 EXPECT_FALSE(resetter_delegate()->IsDefaultSearchProviderManaged()); | 367 EXPECT_FALSE(resetter_delegate()->IsDefaultSearchProviderManaged()); |
| 368 } | 368 } |
| 369 | 369 |
| 370 TEST_F(AutomaticProfileResetterDelegateTest, | 370 TEST_F(AutomaticProfileResetterDelegateTest, |
| 371 DefaultSearchProviderDataWhenManaged) { | 371 DefaultSearchProviderDataWhenManaged) { |
| 372 const char kTestSearchURL[] = "http://example.com/search?q={searchTerms}"; | 372 const char kTestSearchURL[] = "http://example.com/search?q={searchTerms}"; |
| 373 const char kTestName[] = "name"; | 373 const char kTestName[] = "name"; |
| 374 const char kTestKeyword[] = "keyword"; | 374 const char kTestKeyword[] = "keyword"; |
| 375 | 375 |
| 376 TemplateURLServiceTestUtilBase::VerifyLoad(); | 376 util()->VerifyLoad(); |
| 377 | 377 |
| 378 EXPECT_FALSE(resetter_delegate()->IsDefaultSearchProviderManaged()); | 378 EXPECT_FALSE(resetter_delegate()->IsDefaultSearchProviderManaged()); |
| 379 | 379 |
| 380 // Set managed preferences to emulate a default search provider set by policy. | 380 // Set managed preferences to emulate a default search provider set by policy. |
| 381 SetManagedDefaultSearchPreferences( | 381 util()->SetManagedDefaultSearchPreferences( |
| 382 true, kTestName, kTestKeyword, kTestSearchURL, std::string(), | 382 true, kTestName, kTestKeyword, kTestSearchURL, std::string(), |
| 383 std::string(), std::string(), std::string(), std::string()); | 383 std::string(), std::string(), std::string(), std::string()); |
| 384 | 384 |
| 385 EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged()); | 385 EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged()); |
| 386 scoped_ptr<base::DictionaryValue> dsp_details( | 386 scoped_ptr<base::DictionaryValue> dsp_details( |
| 387 resetter_delegate()->GetDefaultSearchProviderDetails()); | 387 resetter_delegate()->GetDefaultSearchProviderDetails()); |
| 388 // Checking that all details are correct is already done by the above test. | 388 // Checking that all details are correct is already done by the above test. |
| 389 // Just make sure details are reported about the correct engine. | 389 // Just make sure details are reported about the correct engine. |
| 390 base::ExpectDictStringValue(kTestSearchURL, *dsp_details, "search_url"); | 390 base::ExpectDictStringValue(kTestSearchURL, *dsp_details, "search_url"); |
| 391 | 391 |
| 392 // Set managed preferences to emulate that having a default search provider is | 392 // Set managed preferences to emulate that having a default search provider is |
| 393 // disabled by policy. | 393 // disabled by policy. |
| 394 RemoveManagedDefaultSearchPreferences(); | 394 util()->RemoveManagedDefaultSearchPreferences(); |
| 395 SetManagedDefaultSearchPreferences( | 395 util()->SetManagedDefaultSearchPreferences( |
| 396 false, std::string(), std::string(), std::string(), std::string(), | 396 false, std::string(), std::string(), std::string(), std::string(), |
| 397 std::string(), std::string(), std::string(), std::string()); | 397 std::string(), std::string(), std::string(), std::string()); |
| 398 | 398 |
| 399 dsp_details = resetter_delegate()->GetDefaultSearchProviderDetails(); | 399 dsp_details = resetter_delegate()->GetDefaultSearchProviderDetails(); |
| 400 EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged()); | 400 EXPECT_TRUE(resetter_delegate()->IsDefaultSearchProviderManaged()); |
| 401 EXPECT_TRUE(dsp_details->empty()); | 401 EXPECT_TRUE(dsp_details->empty()); |
| 402 } | 402 } |
| 403 | 403 |
| 404 TEST_F(AutomaticProfileResetterDelegateTest, | 404 TEST_F(AutomaticProfileResetterDelegateTest, |
| 405 GetPrepopulatedSearchProvidersDetails) { | 405 GetPrepopulatedSearchProvidersDetails) { |
| 406 TemplateURLService* template_url_service = | 406 TemplateURLService* template_url_service = |
| 407 TemplateURLServiceFactory::GetForProfile(profile()); | 407 TemplateURLServiceFactory::GetForProfile(profile()); |
| 408 TemplateURLServiceTestUtilBase::VerifyLoad(); | 408 util()->VerifyLoad(); |
| 409 | 409 |
| 410 scoped_ptr<base::ListValue> search_engines_details( | 410 scoped_ptr<base::ListValue> search_engines_details( |
| 411 resetter_delegate()->GetPrepopulatedSearchProvidersDetails()); | 411 resetter_delegate()->GetPrepopulatedSearchProvidersDetails()); |
| 412 | 412 |
| 413 // Do the same kind of verification as for GetDefaultSearchEngineDetails: | 413 // Do the same kind of verification as for GetDefaultSearchEngineDetails: |
| 414 // subsequently set each pre-populated engine as the default, so we can verify | 414 // 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 | 415 // that the details returned by the delegate about one particular engine are |
| 416 // correct in comparison to what has been stored to the Prefs. | 416 // correct in comparison to what has been stored to the Prefs. |
| 417 std::vector<TemplateURL*> prepopulated_engines = | 417 std::vector<TemplateURL*> prepopulated_engines = |
| 418 template_url_service->GetTemplateURLs(); | 418 template_url_service->GetTemplateURLs(); |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 596 if (ProfileResetGlobalError::IsSupportedOnPlatform()) | 596 if (ProfileResetGlobalError::IsSupportedOnPlatform()) |
| 597 ExpectResetPromptState(true /*active*/); | 597 ExpectResetPromptState(true /*active*/); |
| 598 else | 598 else |
| 599 ExpectResetPromptState(false /*active*/); | 599 ExpectResetPromptState(false /*active*/); |
| 600 resetter_delegate()->DismissPrompt(); | 600 resetter_delegate()->DismissPrompt(); |
| 601 ExpectResetPromptState(false /*active*/); | 601 ExpectResetPromptState(false /*active*/); |
| 602 resetter_delegate()->DismissPrompt(); | 602 resetter_delegate()->DismissPrompt(); |
| 603 } | 603 } |
| 604 | 604 |
| 605 } // namespace | 605 } // namespace |
| OLD | NEW |