diff options
author | hashimoto@chromium.org <hashimoto@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-07-17 07:39:05 +0000 |
---|---|---|
committer | hashimoto@chromium.org <hashimoto@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-07-17 07:39:05 +0000 |
commit | 7d0d3743d253348452d2c5b055d30d50b4f7c2b5 (patch) | |
tree | 8f4565cdfac85a4cd304720526e753abc71276a8 | |
parent | 1ea46f6fe0aa266d4b1ff2a0c4f6039633fd36e6 (diff) | |
download | chromium_src-7d0d3743d253348452d2c5b055d30d50b4f7c2b5.zip chromium_src-7d0d3743d253348452d2c5b055d30d50b4f7c2b5.tar.gz chromium_src-7d0d3743d253348452d2c5b055d30d50b4f7c2b5.tar.bz2 |
Stop using TemplateURLServiceTestUtil to initialize TemplateURLServiceFactory
To drop all dependencies on chrome, TemplateURLServiceTestUtil is going to stop interact with TemplateURLServiceFactory.
All tests which have used the util to initialize the factory are rewritten to do it with TemplateURLServiceFactoryTestUtil.
BUG=387985
TEST=unit_tests
TBR=sky@chromium.org for an include removal in ui_test_utils.cc
Review URL: https://codereview.chromium.org/376413002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@283695 0039d316-1c4b-4281-b951-d872f2087c98
21 files changed, 479 insertions, 337 deletions
diff --git a/chrome/browser/autocomplete/autocomplete_result_unittest.cc b/chrome/browser/autocomplete/autocomplete_result_unittest.cc index 265bf56..51c3559 100644 --- a/chrome/browser/autocomplete/autocomplete_result_unittest.cc +++ b/chrome/browser/autocomplete/autocomplete_result_unittest.cc @@ -96,14 +96,9 @@ class AutocompleteResultTest : public testing::Test { TemplateURLPrepopulateData::InitCountryCode( std::string() /* unknown country code */); #endif - test_util_.SetUp(); test_util_.VerifyLoad(); } - virtual void TearDown() OVERRIDE { - test_util_.TearDown(); - } - // Configures |match| from |data|. static void PopulateAutocompleteMatch(const TestData& data, AutocompleteMatch* match); diff --git a/chrome/browser/profile_resetter/automatic_profile_resetter_delegate_unittest.cc b/chrome/browser/profile_resetter/automatic_profile_resetter_delegate_unittest.cc index b9ea0fb..560b277 100644 --- a/chrome/browser/profile_resetter/automatic_profile_resetter_delegate_unittest.cc +++ b/chrome/browser/profile_resetter/automatic_profile_resetter_delegate_unittest.cc @@ -26,7 +26,7 @@ #include "chrome/browser/profile_resetter/brandcoded_default_settings.h" #include "chrome/browser/profile_resetter/profile_reset_global_error.h" #include "chrome/browser/search_engines/template_url_service_factory.h" -#include "chrome/browser/search_engines/template_url_service_test_util.h" +#include "chrome/browser/search_engines/template_url_service_factory_test_util.h" #include "chrome/browser/ui/global_error/global_error.h" #include "chrome/browser/ui/global_error/global_error_service.h" #include "chrome/browser/ui/global_error/global_error_service_factory.h" @@ -162,8 +162,7 @@ void ServicePendingBrancodedConfigFetch(net::TestURLFetcher* fetcher, // ExtensionServiceTestBase sets up a TestingProfile with the ExtensionService, // we then add the TemplateURLService, so the ProfileResetter can be exercised. class AutomaticProfileResetterDelegateTest - : public extensions::ExtensionServiceTestBase, - public TemplateURLServiceTestUtilBase { + : public extensions::ExtensionServiceTestBase { protected: AutomaticProfileResetterDelegateTest() {} virtual ~AutomaticProfileResetterDelegateTest() {} @@ -173,13 +172,15 @@ class AutomaticProfileResetterDelegateTest ExtensionServiceInitParams params = CreateDefaultInitParams(); params.pref_file.clear(); // Prescribes a TestingPrefService to be created. InitializeExtensionService(params); - TemplateURLServiceTestUtilBase::CreateTemplateUrlService(); + template_url_service_test_util_.reset( + new TemplateURLServiceFactoryTestUtil(profile_.get())); resetter_delegate_.reset( new AutomaticProfileResetterDelegateUnderTest(profile())); } virtual void TearDown() OVERRIDE { resetter_delegate_.reset(); + template_url_service_test_util_.reset(); extensions::ExtensionServiceTestBase::TearDown(); } @@ -233,10 +234,12 @@ class AutomaticProfileResetterDelegateTest return resetter_delegate_.get(); } - // TemplateURLServiceTestUtilBase: - virtual TestingProfile* profile() const OVERRIDE { return profile_.get(); } + TemplateURLServiceFactoryTestUtil* template_url_service_test_util() { + return template_url_service_test_util_.get(); + } private: + scoped_ptr<TemplateURLServiceFactoryTestUtil> template_url_service_test_util_; net::TestURLFetcherFactory test_url_fetcher_factory_; scoped_ptr<AutomaticProfileResetterDelegateUnderTest> resetter_delegate_; @@ -346,7 +349,7 @@ TEST_F(AutomaticProfileResetterDelegateTest, DefaultSearchProviderDataWhenNotManaged) { TemplateURLService* template_url_service = TemplateURLServiceFactory::GetForProfile(profile()); - TemplateURLServiceTestUtilBase::VerifyLoad(); + template_url_service_test_util()->VerifyLoad(); // Check that the "managed state" and the details returned by the delegate are // correct. We verify the details against the data stored by @@ -373,12 +376,12 @@ TEST_F(AutomaticProfileResetterDelegateTest, const char kTestName[] = "name"; const char kTestKeyword[] = "keyword"; - TemplateURLServiceTestUtilBase::VerifyLoad(); + template_url_service_test_util()->VerifyLoad(); EXPECT_FALSE(resetter_delegate()->IsDefaultSearchProviderManaged()); // Set managed preferences to emulate a default search provider set by policy. - SetManagedDefaultSearchPreferences( + template_url_service_test_util()->SetManagedDefaultSearchPreferences( true, kTestName, kTestKeyword, kTestSearchURL, std::string(), std::string(), std::string(), std::string(), std::string()); @@ -391,8 +394,8 @@ TEST_F(AutomaticProfileResetterDelegateTest, // Set managed preferences to emulate that having a default search provider is // disabled by policy. - RemoveManagedDefaultSearchPreferences(); - SetManagedDefaultSearchPreferences( + template_url_service_test_util()->RemoveManagedDefaultSearchPreferences(); + template_url_service_test_util()->SetManagedDefaultSearchPreferences( false, std::string(), std::string(), std::string(), std::string(), std::string(), std::string(), std::string(), std::string()); @@ -405,7 +408,7 @@ TEST_F(AutomaticProfileResetterDelegateTest, GetPrepopulatedSearchProvidersDetails) { TemplateURLService* template_url_service = TemplateURLServiceFactory::GetForProfile(profile()); - TemplateURLServiceTestUtilBase::VerifyLoad(); + template_url_service_test_util()->VerifyLoad(); scoped_ptr<base::ListValue> search_engines_details( resetter_delegate()->GetPrepopulatedSearchProvidersDetails()); diff --git a/chrome/browser/search_engines/search_provider_install_data.cc b/chrome/browser/search_engines/search_provider_install_data.cc index 69920d2..836b13d 100644 --- a/chrome/browser/search_engines/search_provider_install_data.cc +++ b/chrome/browser/search_engines/search_provider_install_data.cc @@ -14,11 +14,9 @@ #include "base/logging.h" #include "base/memory/ref_counted.h" #include "base/sequenced_task_runner_helpers.h" -#include "chrome/browser/google/google_url_tracker_factory.h" -#include "chrome/browser/search_engines/template_url_service_factory.h" -#include "chrome/browser/search_engines/ui_thread_search_terms_data.h" #include "components/google/core/browser/google_url_tracker.h" #include "components/search_engines/search_host_to_urls_map.h" +#include "components/search_engines/search_terms_data.h" #include "components/search_engines/template_url.h" #include "components/search_engines/template_url_service.h" #include "components/search_engines/util.h" @@ -181,14 +179,14 @@ static bool IsSameOrigin(const GURL& requested_origin, } // namespace SearchProviderInstallData::SearchProviderInstallData( - Profile* profile, + TemplateURLService* template_url_service, + const std::string& google_base_url, + GoogleURLTracker* google_url_tracker, content::RenderProcessHost* host) - : template_url_service_(TemplateURLServiceFactory::GetForProfile(profile)), - google_base_url_(UIThreadSearchTermsData(profile).GoogleBaseURLValue()), + : template_url_service_(template_url_service), + google_base_url_(google_base_url), weak_factory_(this) { // GoogleURLTracker is not created in tests. - GoogleURLTracker* google_url_tracker = - GoogleURLTrackerFactory::GetForProfile(profile); if (google_url_tracker) { // GoogleURLObserver is responsible for killing itself when // the given notification occurs. diff --git a/chrome/browser/search_engines/search_provider_install_data.h b/chrome/browser/search_engines/search_provider_install_data.h index 853e449..512da28 100644 --- a/chrome/browser/search_engines/search_provider_install_data.h +++ b/chrome/browser/search_engines/search_provider_install_data.h @@ -15,7 +15,7 @@ #include "base/memory/weak_ptr.h" class GURL; -class Profile; +class GoogleURLTracker; class SearchHostToURLsMap; class TemplateURL; class TemplateURLService; @@ -45,7 +45,10 @@ class SearchProviderInstallData { // signal to this class that it no longer needs to be kept up to date. (Note // that this class may be deleted before or after that death occurs. It // doesn't matter.) - SearchProviderInstallData(Profile* profile, content::RenderProcessHost* host); + SearchProviderInstallData(TemplateURLService* template_url_service, + const std::string& google_base_url, + GoogleURLTracker* google_url_tracker, + content::RenderProcessHost* host); virtual ~SearchProviderInstallData(); // Use to determine when the search provider information is loaded. The diff --git a/chrome/browser/search_engines/search_provider_install_data_unittest.cc b/chrome/browser/search_engines/search_provider_install_data_unittest.cc index 8e309be..1bca439 100644 --- a/chrome/browser/search_engines/search_provider_install_data_unittest.cc +++ b/chrome/browser/search_engines/search_provider_install_data_unittest.cc @@ -15,6 +15,7 @@ #include "chrome/browser/search_engines/template_url_service_test_util.h" #include "chrome/test/base/testing_pref_service_syncable.h" #include "chrome/test/base/testing_profile.h" +#include "components/search_engines/search_terms_data.h" #include "components/search_engines/template_url.h" #include "components/search_engines/template_url_prepopulate_data.h" #include "components/search_engines/template_url_service.h" @@ -152,10 +153,10 @@ void SearchProviderInstallDataTest::SetUp() { TemplateURLPrepopulateData::InitCountryCode( std::string() /* unknown country code */); #endif - util_.SetUp(); process_.reset(new content::MockRenderProcessHost(util_.profile())); - install_data_ = - new SearchProviderInstallData(util_.profile(), process_.get()); + install_data_ = new SearchProviderInstallData( + util_.model(), SearchTermsData().GoogleBaseURLValue(), NULL, + process_.get()); } void SearchProviderInstallDataTest::TearDown() { @@ -166,7 +167,6 @@ void SearchProviderInstallDataTest::TearDown() { // It doesn't matter that this happens after install_data_ is deleted. process_.reset(); - util_.TearDown(); testing::Test::TearDown(); } diff --git a/chrome/browser/search_engines/search_provider_install_state_message_filter.cc b/chrome/browser/search_engines/search_provider_install_state_message_filter.cc index 8d658a2..e4effe2 100644 --- a/chrome/browser/search_engines/search_provider_install_state_message_filter.cc +++ b/chrome/browser/search_engines/search_provider_install_state_message_filter.cc @@ -6,7 +6,10 @@ #include "base/bind.h" #include "base/logging.h" +#include "chrome/browser/google/google_url_tracker_factory.h" #include "chrome/browser/profiles/profile.h" +#include "chrome/browser/search_engines/template_url_service_factory.h" +#include "chrome/browser/search_engines/ui_thread_search_terms_data.h" #include "chrome/common/render_messages.h" #include "content/public/browser/render_process_host.h" #include "url/gurl.h" @@ -18,7 +21,9 @@ SearchProviderInstallStateMessageFilter( int render_process_id, Profile* profile) : BrowserMessageFilter(ChromeMsgStart), - provider_data_(profile, + provider_data_(TemplateURLServiceFactory::GetForProfile(profile), + UIThreadSearchTermsData(profile).GoogleBaseURLValue(), + GoogleURLTrackerFactory::GetForProfile(profile), content::RenderProcessHost::FromID(render_process_id)), is_off_the_record_(profile->IsOffTheRecord()), weak_factory_(this) { diff --git a/chrome/browser/search_engines/template_url_fetcher_unittest.cc b/chrome/browser/search_engines/template_url_fetcher_unittest.cc index 6550cf3..48d0cc8 100644 --- a/chrome/browser/search_engines/template_url_fetcher_unittest.cc +++ b/chrome/browser/search_engines/template_url_fetcher_unittest.cc @@ -8,7 +8,6 @@ #include "base/message_loop/message_loop.h" #include "base/path_service.h" #include "base/strings/utf_string_conversions.h" -#include "chrome/browser/search_engines/template_url_fetcher_factory.h" #include "chrome/browser/search_engines/template_url_service_test_util.h" #include "chrome/common/chrome_paths.h" #include "chrome/test/base/testing_profile.h" @@ -27,18 +26,16 @@ class TemplateURLFetcherTest : public testing::Test { TemplateURLFetcherTest(); virtual void SetUp() OVERRIDE { - test_util_.SetUp(); TestingProfile* profile = test_util_.profile(); - ASSERT_TRUE(profile); - ASSERT_TRUE(TemplateURLFetcherFactory::GetForProfile(profile)); - ASSERT_TRUE(profile->GetRequestContext()); + template_url_fetcher_.reset(new TemplateURLFetcher( + test_util_.model(), profile->GetRequestContext())); + ASSERT_TRUE(test_server_.InitializeAndWaitUntilReady()); } virtual void TearDown() OVERRIDE { ASSERT_TRUE(test_server_.ShutdownAndWaitUntilComplete()); - test_util_.TearDown(); } // Called when the callback is destroyed. @@ -61,6 +58,7 @@ class TemplateURLFetcherTest : public testing::Test { void WaitForDownloadToFinish(); TemplateURLServiceTestUtil test_util_; + scoped_ptr<TemplateURLFetcher> template_url_fetcher_; net::test_server::EmbeddedTestServer test_server_; // The last TemplateURL to come from a callback. @@ -125,14 +123,13 @@ void TemplateURLFetcherTest::StartDownload( base::Bind(&TemplateURLFetcherTest::DestroyedCallback, base::Unretained(this))); - TemplateURLFetcherFactory::GetForProfile( - test_util_.profile())->ScheduleDownload( - keyword, osdd_url, favicon_url, - TemplateURLFetcher::URLFetcherCustomizeCallback(), - base::Bind(&TemplateURLFetcherTest::ConfirmAddSearchProvider, - base::Unretained(this), - base::Owned(callback_destruction_notifier)), - provider_type); + template_url_fetcher_->ScheduleDownload( + keyword, osdd_url, favicon_url, + TemplateURLFetcher::URLFetcherCustomizeCallback(), + base::Bind(&TemplateURLFetcherTest::ConfirmAddSearchProvider, + base::Unretained(this), + base::Owned(callback_destruction_notifier)), + provider_type); } void TemplateURLFetcherTest::WaitForDownloadToFinish() { @@ -197,11 +194,8 @@ TEST_F(TemplateURLFetcherTest, DuplicatesThrownAway) { for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { StartDownload(test_cases[i].keyword, test_cases[i].osdd_file_name, test_cases[i].provider_type, false); - ASSERT_EQ( - 1, - TemplateURLFetcherFactory::GetForProfile( - test_util_.profile())->requests_count()) << - test_cases[i].description; + ASSERT_EQ(1, template_url_fetcher_->requests_count()) + << test_cases[i].description; ASSERT_EQ(i + 1, static_cast<size_t>(callbacks_destroyed_)); } diff --git a/chrome/browser/search_engines/template_url_service_factory_test_util.cc b/chrome/browser/search_engines/template_url_service_factory_test_util.cc new file mode 100644 index 0000000..b4bba85 --- /dev/null +++ b/chrome/browser/search_engines/template_url_service_factory_test_util.cc @@ -0,0 +1,57 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/search_engines/template_url_service_factory_test_util.h" + +#include "base/run_loop.h" +#include "chrome/browser/search_engines/template_url_service_factory.h" +#include "chrome/test/base/testing_pref_service_syncable.h" +#include "chrome/test/base/testing_profile.h" +#include "components/search_engines/default_search_pref_test_util.h" +#include "components/search_engines/template_url_service.h" + +TemplateURLServiceFactoryTestUtil::TemplateURLServiceFactoryTestUtil( + TestingProfile* profile) + : profile_(profile) { + profile_->CreateWebDataService(); + + TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( + profile_, TemplateURLServiceFactory::BuildInstanceFor); +} + +TemplateURLServiceFactoryTestUtil::~TemplateURLServiceFactoryTestUtil() { + // Flush the message loop to make application verifiers happy. + base::RunLoop().RunUntilIdle(); +} + +void TemplateURLServiceFactoryTestUtil::VerifyLoad() { + model()->Load(); + base::RunLoop().RunUntilIdle(); +} + +void TemplateURLServiceFactoryTestUtil::SetManagedDefaultSearchPreferences( + bool enabled, + const std::string& name, + const std::string& keyword, + const std::string& search_url, + const std::string& suggest_url, + const std::string& icon_url, + const std::string& encodings, + const std::string& alternate_url, + const std::string& search_terms_replacement_key) { + DefaultSearchPrefTestUtil::SetManagedPref( + profile_->GetTestingPrefService(), + enabled, name, keyword, search_url, suggest_url, icon_url, encodings, + alternate_url, search_terms_replacement_key); +} + +void +TemplateURLServiceFactoryTestUtil::RemoveManagedDefaultSearchPreferences() { + DefaultSearchPrefTestUtil::RemoveManagedPref( + profile_->GetTestingPrefService()); +} + +TemplateURLService* TemplateURLServiceFactoryTestUtil::model() const { + return TemplateURLServiceFactory::GetForProfile(profile_); +} diff --git a/chrome/browser/search_engines/template_url_service_factory_test_util.h b/chrome/browser/search_engines/template_url_service_factory_test_util.h new file mode 100644 index 0000000..d8fb481 --- /dev/null +++ b/chrome/browser/search_engines/template_url_service_factory_test_util.h @@ -0,0 +1,52 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_FACTORY_TEST_UTIL_H_ +#define CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_FACTORY_TEST_UTIL_H_ + +#include <string> + +#include "base/basictypes.h" + +class TemplateURLService; +class TestingProfile; + +// TemplateURLServiceFactoryTestUtil initializes TemplateURLServiceFactory to +// return a valid TemplateURLService instance for the given profile. +class TemplateURLServiceFactoryTestUtil { + public: + explicit TemplateURLServiceFactoryTestUtil(TestingProfile* profile); + virtual ~TemplateURLServiceFactoryTestUtil(); + + // Makes sure the load was successful. + void VerifyLoad(); + + // Set the managed preferences for the default search provider and trigger + // notification. If |alternate_url| is empty, uses an empty list of alternate + // URLs, otherwise use a list containing a single entry. + void SetManagedDefaultSearchPreferences( + bool enabled, + const std::string& name, + const std::string& keyword, + const std::string& search_url, + const std::string& suggest_url, + const std::string& icon_url, + const std::string& encodings, + const std::string& alternate_url, + const std::string& search_terms_replacement_key); + + // Remove all the managed preferences for the default search provider and + // trigger notification. + void RemoveManagedDefaultSearchPreferences(); + + // Returns the TemplateURLService. + TemplateURLService* model() const; + + private: + TestingProfile* profile_; + + DISALLOW_COPY_AND_ASSIGN(TemplateURLServiceFactoryTestUtil); +}; + +#endif // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_FACTORY_TEST_UTIL_H_ diff --git a/chrome/browser/search_engines/template_url_service_sync_unittest.cc b/chrome/browser/search_engines/template_url_service_sync_unittest.cc index 61b0502..aad5815 100644 --- a/chrome/browser/search_engines/template_url_service_sync_unittest.cc +++ b/chrome/browser/search_engines/template_url_service_sync_unittest.cc @@ -157,12 +157,12 @@ class TemplateURLServiceSyncTest : public testing::Test { virtual void SetUp() OVERRIDE; virtual void TearDown() OVERRIDE; - TemplateURLService* model() { return test_util_a_.model(); } + TemplateURLService* model() { return test_util_a_->model(); } // For readability, we redefine an accessor for Model A for use in tests that // involve syncing two models. - TemplateURLService* model_a() { return test_util_a_.model(); } + TemplateURLService* model_a() { return test_util_a_->model(); } TemplateURLService* model_b() { return model_b_.get(); } - TestingProfile* profile_a() { return test_util_a_.profile(); } + TestingProfile* profile_a() { return test_util_a_->profile(); } TestChangeProcessor* processor() { return sync_processor_.get(); } scoped_ptr<syncer::SyncChangeProcessor> PassProcessor(); scoped_ptr<syncer::SyncErrorFactory> CreateAndPassSyncErrorFactory(); @@ -210,7 +210,7 @@ class TemplateURLServiceSyncTest : public testing::Test { protected: // We keep two TemplateURLServices to test syncing between them. - TemplateURLServiceTestUtil test_util_a_; + scoped_ptr<TemplateURLServiceTestUtil> test_util_a_; scoped_ptr<TestingProfile> profile_b_; scoped_ptr<TemplateURLService> model_b_; @@ -228,11 +228,11 @@ TemplateURLServiceSyncTest::TemplateURLServiceSyncTest() void TemplateURLServiceSyncTest::SetUp() { DefaultSearchManager::SetFallbackSearchEnginesDisabledForTesting(true); - test_util_a_.SetUp(); + test_util_a_.reset(new TemplateURLServiceTestUtil); // Use ChangeToLoadState() instead of VerifyLoad() so we don't actually pull // in the prepopulate data, which the sync tests don't care about (and would // just foul them up). - test_util_a_.ChangeModelToLoadState(); + test_util_a_->ChangeModelToLoadState(); profile_b_.reset(new TestingProfile); TemplateURLServiceFactory::GetInstance()-> RegisterUserPrefsOnBrowserContextForTest(profile_b_.get()); @@ -244,7 +244,7 @@ void TemplateURLServiceSyncTest::SetUp() { } void TemplateURLServiceSyncTest::TearDown() { - test_util_a_.TearDown(); + test_util_a_.reset(); DefaultSearchManager::SetFallbackSearchEnginesDisabledForTesting(false); } @@ -1661,7 +1661,7 @@ TEST_F(TemplateURLServiceSyncTest, SyncWithManagedDefaultSearch) { const char kAlternateURL[] = "http://manageddefault.com/search#t={searchTerms}"; const char kSearchTermsReplacementKey[] = "espv"; - test_util_a_.SetManagedDefaultSearchPreferences(true, kName, kName, + test_util_a_->SetManagedDefaultSearchPreferences(true, kName, kName, kSearchURL, std::string(), kIconURL, kEncodings, kAlternateURL, kSearchTermsReplacementKey); const TemplateURL* dsp_turl = model()->GetDefaultSearchProvider(); @@ -1692,7 +1692,7 @@ TEST_F(TemplateURLServiceSyncTest, SyncWithManagedDefaultSearch) { // from Sync. const TemplateURL* expected_default = model()->GetTemplateURLForGUID("newdefault"); - test_util_a_.RemoveManagedDefaultSearchPreferences(); + test_util_a_->RemoveManagedDefaultSearchPreferences(); EXPECT_EQ(expected_default, model()->GetDefaultSearchProvider()); } @@ -1846,7 +1846,7 @@ TEST_F(TemplateURLServiceSyncTest, PreSyncUpdates) { // Merge the prepopulate search engines. base::Time pre_merge_time = base::Time::Now(); base::RunLoop().RunUntilIdle(); - test_util_a_.ResetModel(true); + test_util_a_->ResetModel(true); // The newly added search engine should have been safely merged, with an // updated time. @@ -1895,7 +1895,7 @@ TEST_F(TemplateURLServiceSyncTest, PreSyncUpdates) { TEST_F(TemplateURLServiceSyncTest, SyncBaseURLs) { // Verify that bringing in a remote TemplateURL that uses Google base URLs // causes it to get a local keyword that matches the local base URL. - test_util_a_.SetGoogleBaseURL(GURL("http://google.com/")); + test_util_a_->SetGoogleBaseURL(GURL("http://google.com/")); syncer::SyncDataList initial_data; scoped_ptr<TemplateURL> turl(CreateTestTemplateURL( ASCIIToUTF16("google.co.uk"), "{google:baseURL}search?q={searchTerms}", @@ -1920,7 +1920,7 @@ TEST_F(TemplateURLServiceSyncTest, SyncBaseURLs) { // A local change to the Google base URL should update the keyword and // generate a sync change. - test_util_a_.SetGoogleBaseURL(GURL("http://google.co.in/")); + test_util_a_->SetGoogleBaseURL(GURL("http://google.co.in/")); EXPECT_EQ(ASCIIToUTF16("google.co.in"), synced_turl->keyword()); EXPECT_EQ(1U, processor()->change_list_size()); ASSERT_TRUE(processor()->contains_guid("guid")); diff --git a/chrome/browser/search_engines/template_url_service_test_util.cc b/chrome/browser/search_engines/template_url_service_test_util.cc index 73b412a..22cc554 100644 --- a/chrome/browser/search_engines/template_url_service_test_util.cc +++ b/chrome/browser/search_engines/template_url_service_test_util.cc @@ -9,13 +9,13 @@ #include "base/strings/string_split.h" #include "base/threading/thread.h" #include "chrome/browser/search_engines/chrome_template_url_service_client.h" -#include "chrome/browser/search_engines/template_url_service_factory.h" #include "chrome/browser/search_engines/ui_thread_search_terms_data.h" #include "chrome/browser/webdata/web_data_service_factory.h" #include "chrome/test/base/testing_pref_service_syncable.h" #include "chrome/test/base/testing_profile.h" #include "components/google/core/browser/google_url_tracker.h" #include "components/search_engines/default_search_manager.h" +#include "components/search_engines/default_search_pref_test_util.h" #include "components/search_engines/template_url_service.h" #include "testing/gtest/include/gtest/gtest.h" @@ -27,10 +27,6 @@ // SetKeywordSearchTermsForURL. class TestingTemplateURLService : public TemplateURLService { public: - static KeyedService* Build(content::BrowserContext* profile) { - return new TestingTemplateURLService(static_cast<Profile*>(profile)); - } - explicit TestingTemplateURLService(Profile* profile) : TemplateURLService( profile->GetPrefs(), @@ -62,37 +58,47 @@ class TestingTemplateURLService : public TemplateURLService { DISALLOW_COPY_AND_ASSIGN(TestingTemplateURLService); }; -// TemplateURLServiceTestUtilBase --------------------------------------------- -TemplateURLServiceTestUtilBase::TemplateURLServiceTestUtilBase() - : changed_count_(0) { -} +TemplateURLServiceTestUtil::TemplateURLServiceTestUtil() + : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), + changed_count_(0) { + // Make unique temp directory. + EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); + profile_.reset(new TestingProfile(temp_dir_.path())); -TemplateURLServiceTestUtilBase::~TemplateURLServiceTestUtilBase() { + profile()->CreateWebDataService(); + + model_.reset(new TestingTemplateURLService(profile())); + model_->AddObserver(this); + +#if defined(OS_CHROMEOS) + google_brand::chromeos::ClearBrandForCurrentSession(); +#endif } -void TemplateURLServiceTestUtilBase::CreateTemplateUrlService() { - profile()->CreateWebDataService(); +TemplateURLServiceTestUtil::~TemplateURLServiceTestUtil() { + ClearModel(); + profile_.reset(); + + UIThreadSearchTermsData::SetGoogleBaseURL(std::string()); - TemplateURLService* service = static_cast<TemplateURLService*>( - TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( - profile(), TestingTemplateURLService::Build)); - service->AddObserver(this); + // Flush the message loop to make application verifiers happy. + base::RunLoop().RunUntilIdle(); } -void TemplateURLServiceTestUtilBase::OnTemplateURLServiceChanged() { +void TemplateURLServiceTestUtil::OnTemplateURLServiceChanged() { changed_count_++; } -int TemplateURLServiceTestUtilBase::GetObserverCount() { +int TemplateURLServiceTestUtil::GetObserverCount() { return changed_count_; } -void TemplateURLServiceTestUtilBase::ResetObserverCount() { +void TemplateURLServiceTestUtil::ResetObserverCount() { changed_count_ = 0; } -void TemplateURLServiceTestUtilBase::VerifyLoad() { +void TemplateURLServiceTestUtil::VerifyLoad() { ASSERT_FALSE(model()->loaded()); model()->Load(); base::RunLoop().RunUntilIdle(); @@ -100,7 +106,7 @@ void TemplateURLServiceTestUtilBase::VerifyLoad() { ResetObserverCount(); } -void TemplateURLServiceTestUtilBase::ChangeModelToLoadState() { +void TemplateURLServiceTestUtil::ChangeModelToLoadState() { model()->ChangeToLoadedState(); // Initialize the web data service so that the database gets updated with // any changes made. @@ -111,34 +117,33 @@ void TemplateURLServiceTestUtilBase::ChangeModelToLoadState() { base::RunLoop().RunUntilIdle(); } -void TemplateURLServiceTestUtilBase::ClearModel() { - TemplateURLServiceFactory::GetInstance()->SetTestingFactory( - profile(), NULL); +void TemplateURLServiceTestUtil::ClearModel() { + model_->Shutdown(); + model_.reset(); } -void TemplateURLServiceTestUtilBase::ResetModel(bool verify_load) { - TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( - profile(), TestingTemplateURLService::Build); +void TemplateURLServiceTestUtil::ResetModel(bool verify_load) { + if (model_) + ClearModel(); + model_.reset(new TestingTemplateURLService(profile())); model()->AddObserver(this); changed_count_ = 0; if (verify_load) VerifyLoad(); } -base::string16 TemplateURLServiceTestUtilBase::GetAndClearSearchTerm() { - return - static_cast<TestingTemplateURLService*>(model())->GetAndClearSearchTerm(); +base::string16 TemplateURLServiceTestUtil::GetAndClearSearchTerm() { + return model_->GetAndClearSearchTerm(); } -void TemplateURLServiceTestUtilBase::SetGoogleBaseURL( - const GURL& base_url) const { +void TemplateURLServiceTestUtil::SetGoogleBaseURL(const GURL& base_url) { DCHECK(base_url.is_valid()); UIThreadSearchTermsData data(profile()); UIThreadSearchTermsData::SetGoogleBaseURL(base_url.spec()); - TemplateURLServiceFactory::GetForProfile(profile())->GoogleBaseURLChanged(); + model_->GoogleBaseURLChanged(); } -void TemplateURLServiceTestUtilBase::SetManagedDefaultSearchPreferences( +void TemplateURLServiceTestUtil::SetManagedDefaultSearchPreferences( bool enabled, const std::string& name, const std::string& keyword, @@ -148,93 +153,17 @@ void TemplateURLServiceTestUtilBase::SetManagedDefaultSearchPreferences( const std::string& encodings, const std::string& alternate_url, const std::string& search_terms_replacement_key) { - TestingPrefServiceSyncable* pref_service = profile()->GetTestingPrefService(); - scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue); - if (!enabled) { - value->SetBoolean(DefaultSearchManager::kDisabledByPolicy, true); - pref_service->SetManagedPref( - DefaultSearchManager::kDefaultSearchProviderDataPrefName, - value.release()); - return; - } - - EXPECT_FALSE(keyword.empty()); - EXPECT_FALSE(search_url.empty()); - value->Set(DefaultSearchManager::kShortName, - base::Value::CreateStringValue(name)); - value->Set(DefaultSearchManager::kKeyword, - base::Value::CreateStringValue(keyword)); - value->Set(DefaultSearchManager::kURL, - base::Value::CreateStringValue(search_url)); - value->Set(DefaultSearchManager::kSuggestionsURL, - base::Value::CreateStringValue(suggest_url)); - value->Set(DefaultSearchManager::kFaviconURL, - base::Value::CreateStringValue(icon_url)); - value->Set(DefaultSearchManager::kSearchTermsReplacementKey, - base::Value::CreateStringValue(search_terms_replacement_key)); - - std::vector<std::string> encodings_items; - base::SplitString(encodings, ';', &encodings_items); - scoped_ptr<base::ListValue> encodings_list(new base::ListValue); - for (std::vector<std::string>::const_iterator it = encodings_items.begin(); - it != encodings_items.end(); - ++it) { - encodings_list->AppendString(*it); - } - value->Set(DefaultSearchManager::kInputEncodings, encodings_list.release()); - - scoped_ptr<base::ListValue> alternate_url_list(new base::ListValue()); - if (!alternate_url.empty()) - alternate_url_list->Append(base::Value::CreateStringValue(alternate_url)); - value->Set(DefaultSearchManager::kAlternateURLs, - alternate_url_list.release()); - - pref_service->SetManagedPref( - DefaultSearchManager::kDefaultSearchProviderDataPrefName, - value.release()); -} - -void TemplateURLServiceTestUtilBase::RemoveManagedDefaultSearchPreferences() { - TestingPrefServiceSyncable* pref_service = profile()->GetTestingPrefService(); - pref_service->RemoveManagedPref( - DefaultSearchManager::kDefaultSearchProviderDataPrefName); + DefaultSearchPrefTestUtil::SetManagedPref( + profile()->GetTestingPrefService(), + enabled, name, keyword, search_url, suggest_url, icon_url, encodings, + alternate_url, search_terms_replacement_key); } -TemplateURLService* TemplateURLServiceTestUtilBase::model() const { - return TemplateURLServiceFactory::GetForProfile(profile()); -} - - -// TemplateURLServiceTestUtil ------------------------------------------------- - -TemplateURLServiceTestUtil::TemplateURLServiceTestUtil() - : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { -} - -TemplateURLServiceTestUtil::~TemplateURLServiceTestUtil() { -} - -void TemplateURLServiceTestUtil::SetUp() { - // Make unique temp directory. - ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); - profile_.reset(new TestingProfile(temp_dir_.path())); - - TemplateURLServiceTestUtilBase::CreateTemplateUrlService(); - -#if defined(OS_CHROMEOS) - google_brand::chromeos::ClearBrandForCurrentSession(); -#endif -} - -void TemplateURLServiceTestUtil::TearDown() { - profile_.reset(); - - UIThreadSearchTermsData::SetGoogleBaseURL(std::string()); - - // Flush the message loop to make application verifiers happy. - base::RunLoop().RunUntilIdle(); +void TemplateURLServiceTestUtil::RemoveManagedDefaultSearchPreferences() { + DefaultSearchPrefTestUtil::RemoveManagedPref( + profile()->GetTestingPrefService()); } -TestingProfile* TemplateURLServiceTestUtil::profile() const { - return profile_.get(); +TemplateURLService* TemplateURLServiceTestUtil::model() { + return model_.get(); } diff --git a/chrome/browser/search_engines/template_url_service_test_util.h b/chrome/browser/search_engines/template_url_service_test_util.h index 83f4fad..c05c20d 100644 --- a/chrome/browser/search_engines/template_url_service_test_util.h +++ b/chrome/browser/search_engines/template_url_service_test_util.h @@ -9,9 +9,7 @@ #include "base/basictypes.h" #include "base/files/scoped_temp_dir.h" -#include "base/memory/ref_counted.h" #include "base/memory/scoped_ptr.h" -#include "base/message_loop/message_loop.h" #include "base/strings/string16.h" #include "components/search_engines/template_url_service_observer.h" #include "content/public/test/test_browser_thread_bundle.h" @@ -20,16 +18,11 @@ class GURL; class TemplateURLService; class TestingProfile; class TestingTemplateURLService; -class TestingProfile; -// TemplateURLServiceTestUtilBase contains basic API to ease testing of -// TemplateURLService. User should take care of the infrastructure separately. -class TemplateURLServiceTestUtilBase : public TemplateURLServiceObserver { +class TemplateURLServiceTestUtil : public TemplateURLServiceObserver { public: - TemplateURLServiceTestUtilBase(); - virtual ~TemplateURLServiceTestUtilBase(); - - void CreateTemplateUrlService(); + TemplateURLServiceTestUtil(); + virtual ~TemplateURLServiceTestUtil(); // TemplateURLServiceObserver implemementation. virtual void OnTemplateURLServiceChanged() OVERRIDE; @@ -59,7 +52,7 @@ class TemplateURLServiceTestUtilBase : public TemplateURLServiceObserver { base::string16 GetAndClearSearchTerm(); // Set the google base url. |base_url| must be valid. - void SetGoogleBaseURL(const GURL& base_url) const; + void SetGoogleBaseURL(const GURL& base_url); // Set the managed preferences for the default search provider and trigger // notification. If |alternate_url| is empty, uses an empty list of alternate @@ -80,41 +73,17 @@ class TemplateURLServiceTestUtilBase : public TemplateURLServiceObserver { void RemoveManagedDefaultSearchPreferences(); // Returns the TemplateURLService. - TemplateURLService* model() const; - - // Returns the TestingProfile. - virtual TestingProfile* profile() const = 0; - - private: - int changed_count_; - - DISALLOW_COPY_AND_ASSIGN(TemplateURLServiceTestUtilBase); -}; - -// TemplateURLServiceTestUtil sets up TestingProfile, TemplateURLService and -// required threads. -class TemplateURLServiceTestUtil : public TemplateURLServiceTestUtilBase { - public: - TemplateURLServiceTestUtil(); - virtual ~TemplateURLServiceTestUtil(); - - // Sets up the data structures for this class (mirroring gtest standard - // methods). - void SetUp(); - - // Cleans up data structures for this class (mirroring gtest standard - // methods). - void TearDown(); + TemplateURLService* model(); // Returns the TestingProfile. - virtual TestingProfile* profile() const OVERRIDE; + TestingProfile* profile() { return profile_.get(); } private: - // Needed to make the DeleteOnUIThread trait of WebDataService work - // properly. content::TestBrowserThreadBundle thread_bundle_; scoped_ptr<TestingProfile> profile_; base::ScopedTempDir temp_dir_; + int changed_count_; + scoped_ptr<TestingTemplateURLService> model_; DISALLOW_COPY_AND_ASSIGN(TemplateURLServiceTestUtil); }; diff --git a/chrome/browser/search_engines/template_url_service_unittest.cc b/chrome/browser/search_engines/template_url_service_unittest.cc index 2db6b69..256b1b3 100644 --- a/chrome/browser/search_engines/template_url_service_unittest.cc +++ b/chrome/browser/search_engines/template_url_service_unittest.cc @@ -157,9 +157,9 @@ class TemplateURLServiceTest : public testing::Test { public: TemplateURLServiceTest(); - // testing::Test - virtual void SetUp(); - virtual void TearDown(); + // testing::Test: + virtual void SetUp() OVERRIDE; + virtual void TearDown() OVERRIDE; TemplateURL* AddKeywordWithDate(const std::string& short_name, const std::string& keyword, @@ -184,13 +184,13 @@ class TemplateURLServiceTest : public testing::Test { // visually noisy in the test code. void VerifyObserverCount(int expected_changed_count); void VerifyObserverFired(); - TemplateURLService* model() { return test_util_.model(); } + TemplateURLService* model() { return test_util_->model(); } const SearchTermsData& search_terms_data() { return model()->search_terms_data(); } protected: - TemplateURLServiceTestUtil test_util_; + scoped_ptr<TemplateURLServiceTestUtil> test_util_; DISALLOW_COPY_AND_ASSIGN(TemplateURLServiceTest); }; @@ -214,11 +214,11 @@ TemplateURLServiceTest::TemplateURLServiceTest() { } void TemplateURLServiceTest::SetUp() { - test_util_.SetUp(); + test_util_.reset(new TemplateURLServiceTestUtil); } void TemplateURLServiceTest::TearDown() { - test_util_.TearDown(); + test_util_.reset(); } TemplateURL* TemplateURLServiceTest::AddKeywordWithDate( @@ -274,25 +274,25 @@ TemplateURL* TemplateURLServiceTest::CreatePreloadedTemplateURL( } void TemplateURLServiceTest::VerifyObserverCount(int expected_changed_count) { - EXPECT_EQ(expected_changed_count, test_util_.GetObserverCount()); - test_util_.ResetObserverCount(); + EXPECT_EQ(expected_changed_count, test_util_->GetObserverCount()); + test_util_->ResetObserverCount(); } void TemplateURLServiceTest::VerifyObserverFired() { - EXPECT_LE(1, test_util_.GetObserverCount()); - test_util_.ResetObserverCount(); + EXPECT_LE(1, test_util_->GetObserverCount()); + test_util_->ResetObserverCount(); } // Actual tests --------------------------------------------------------------- TEST_F(TemplateURLServiceTest, Load) { - test_util_.VerifyLoad(); + test_util_->VerifyLoad(); } TEST_F(TemplateURLServiceTest, AddUpdateRemove) { // Add a new TemplateURL. - test_util_.VerifyLoad(); + test_util_->VerifyLoad(); const size_t initial_count = model()->GetTemplateURLs().size(); TemplateURLData data; @@ -318,7 +318,7 @@ TEST_F(TemplateURLServiceTest, AddUpdateRemove) { scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data())); // Reload the model to verify it was actually saved to the database. - test_util_.ResetModel(true); + test_util_->ResetModel(true); ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); TemplateURL* loaded_url = model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")); @@ -346,7 +346,7 @@ TEST_F(TemplateURLServiceTest, AddUpdateRemove) { ASSERT_FALSE(model()->CanReplaceKeyword(ASCIIToUTF16("b"), GURL(), NULL)); cloned_url.reset(new TemplateURL(loaded_url->data())); base::RunLoop().RunUntilIdle(); - test_util_.ResetModel(true); + test_util_->ResetModel(true); ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); loaded_url = model()->GetTemplateURLForKeyword(ASCIIToUTF16("b")); ASSERT_TRUE(loaded_url != NULL); @@ -358,13 +358,13 @@ TEST_F(TemplateURLServiceTest, AddUpdateRemove) { // Remove an element and verify it succeeded. model()->Remove(loaded_url); VerifyObserverCount(1); - test_util_.ResetModel(true); + test_util_->ResetModel(true); ASSERT_EQ(initial_count, model()->GetTemplateURLs().size()); EXPECT_TRUE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("b")) == NULL); } TEST_F(TemplateURLServiceTest, AddSameKeyword) { - test_util_.VerifyLoad(); + test_util_->VerifyLoad(); AddKeywordWithDate( "first", "keyword", "http://test1", std::string(), std::string(), @@ -417,7 +417,7 @@ TEST_F(TemplateURLServiceTest, AddSameKeyword) { } TEST_F(TemplateURLServiceTest, AddExtensionKeyword) { - test_util_.VerifyLoad(); + test_util_->VerifyLoad(); TemplateURL* original1 = AddKeywordWithDate( "replaceable", "keyword1", "http://test1", std::string(), std::string(), @@ -458,7 +458,7 @@ TEST_F(TemplateURLServiceTest, AddExtensionKeyword) { } TEST_F(TemplateURLServiceTest, AddSameKeywordWithExtensionPresent) { - test_util_.VerifyLoad(); + test_util_->VerifyLoad(); // Similar to the AddSameKeyword test, but with an extension keyword masking a // replaceable TemplateURL. We should still do correct conflict resolution @@ -618,7 +618,7 @@ TEST_F(TemplateURLServiceTest, ClearBrowsingData_KeywordsForOrigin) { TEST_F(TemplateURLServiceTest, Reset) { // Add a new TemplateURL. - test_util_.VerifyLoad(); + test_util_->VerifyLoad(); const size_t initial_count = model()->GetTemplateURLs().size(); TemplateURLData data; data.short_name = ASCIIToUTF16("google"); @@ -654,7 +654,7 @@ TEST_F(TemplateURLServiceTest, Reset) { scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data())); // Reload the model from the database and make sure the change took. - test_util_.ResetModel(true); + test_util_->ResetModel(true); EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); const TemplateURL* read_url = model()->GetTemplateURLForKeyword(new_keyword); ASSERT_TRUE(read_url); @@ -664,12 +664,12 @@ TEST_F(TemplateURLServiceTest, Reset) { TEST_F(TemplateURLServiceTest, DefaultSearchProvider) { // Add a new TemplateURL. - test_util_.VerifyLoad(); + test_util_->VerifyLoad(); const size_t initial_count = model()->GetTemplateURLs().size(); TemplateURL* t_url = AddKeywordWithDate( "name1", "key1", "http://foo1/{searchTerms}", "http://sugg1", std::string(), "http://icon1", true, "UTF-8;UTF-16", Time(), Time()); - test_util_.ResetObserverCount(); + test_util_->ResetObserverCount(); model()->SetUserSelectedDefaultSearchProvider(t_url); ASSERT_EQ(t_url, model()->GetDefaultSearchProvider()); @@ -683,14 +683,14 @@ TEST_F(TemplateURLServiceTest, DefaultSearchProvider) { scoped_ptr<TemplateURL> cloned_url(new TemplateURL(t_url->data())); // Make sure when we reload we get a default search provider. - test_util_.ResetModel(true); + test_util_->ResetModel(true); EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); ASSERT_TRUE(model()->GetDefaultSearchProvider()); AssertEquals(*cloned_url, *model()->GetDefaultSearchProvider()); } TEST_F(TemplateURLServiceTest, CantReplaceWithSameKeyword) { - test_util_.ChangeModelToLoadState(); + test_util_->ChangeModelToLoadState(); ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"), GURL(), NULL)); TemplateURL* t_url = AddKeywordWithDate( "name1", "foo", "http://foo1", "http://sugg1", std::string(), @@ -710,7 +710,7 @@ TEST_F(TemplateURLServiceTest, CantReplaceWithSameKeyword) { } TEST_F(TemplateURLServiceTest, CantReplaceWithSameHosts) { - test_util_.ChangeModelToLoadState(); + test_util_->ChangeModelToLoadState(); ASSERT_TRUE(model()->CanReplaceKeyword(ASCIIToUTF16("foo"), GURL("http://foo.com"), NULL)); TemplateURL* t_url = AddKeywordWithDate( @@ -735,13 +735,13 @@ TEST_F(TemplateURLServiceTest, HasDefaultSearchProvider) { ASSERT_TRUE(model()->GetDefaultSearchProvider()); // Now force the model to load and make sure we still have a default. - test_util_.VerifyLoad(); + test_util_->VerifyLoad(); ASSERT_TRUE(model()->GetDefaultSearchProvider()); } TEST_F(TemplateURLServiceTest, DefaultSearchProviderLoadedFromPrefs) { - test_util_.VerifyLoad(); + test_util_->VerifyLoad(); TemplateURLData data; data.short_name = ASCIIToUTF16("a"); @@ -761,7 +761,7 @@ TEST_F(TemplateURLServiceTest, DefaultSearchProviderLoadedFromPrefs) { // Reset the model and don't load it. The template url we set as the default // should be pulled from prefs now. - test_util_.ResetModel(false); + test_util_->ResetModel(false); // NOTE: This doesn't use AssertEquals as only a subset of the TemplateURLs // value are persisted to prefs. @@ -774,14 +774,14 @@ TEST_F(TemplateURLServiceTest, DefaultSearchProviderLoadedFromPrefs) { EXPECT_EQ(id, default_turl->id()); // Now do a load and make sure the default search provider really takes. - test_util_.VerifyLoad(); + test_util_->VerifyLoad(); ASSERT_TRUE(model()->GetDefaultSearchProvider()); AssertEquals(*cloned_url, *model()->GetDefaultSearchProvider()); } TEST_F(TemplateURLServiceTest, RepairPrepopulatedSearchEngines) { - test_util_.VerifyLoad(); + test_util_->VerifyLoad(); // Edit Google search engine. TemplateURL* google = model()->GetTemplateURLForKeyword( @@ -840,12 +840,12 @@ TEST_F(TemplateURLServiceTest, RepairSearchEnginesWithManagedDefault) { const char kEncodings[] = "UTF-16;UTF-32"; const char kAlternateURL[] = "http://test.com/search#t={searchTerms}"; const char kSearchTermsReplacementKey[] = "espv"; - test_util_.SetManagedDefaultSearchPreferences(true, kName, kKeyword, - kSearchURL, std::string(), - kIconURL, kEncodings, - kAlternateURL, - kSearchTermsReplacementKey); - test_util_.VerifyLoad(); + test_util_->SetManagedDefaultSearchPreferences(true, kName, kKeyword, + kSearchURL, std::string(), + kIconURL, kEncodings, + kAlternateURL, + kSearchTermsReplacementKey); + test_util_->VerifyLoad(); // Verify that the default manager we are getting is the managed one. TemplateURLData data; data.short_name = ASCIIToUTF16(kName); @@ -888,7 +888,7 @@ TEST_F(TemplateURLServiceTest, UpdateKeywordSearchTermsForURL) { { "http://x/foo?query=b#q=xx", base::string16() }, }; - test_util_.ChangeModelToLoadState(); + test_util_->ChangeModelToLoadState(); AddKeywordWithDate("name", "x", "http://x/foo?q={searchTerms}", "http://sugg1", "http://x/foo#query={searchTerms}", "http://icon1", false, "UTF-8;UTF-16", Time(), Time()); @@ -898,7 +898,7 @@ TEST_F(TemplateURLServiceTest, UpdateKeywordSearchTermsForURL) { GURL(data[i].url), false }; model()->UpdateKeywordSearchTermsForURL(details); - EXPECT_EQ(data[i].term, test_util_.GetAndClearSearchTerm()); + EXPECT_EQ(data[i].term, test_util_->GetAndClearSearchTerm()); } } @@ -911,7 +911,7 @@ TEST_F(TemplateURLServiceTest, DontUpdateKeywordSearchForNonReplaceable) { { "http://x/foo?y=xx" }, }; - test_util_.ChangeModelToLoadState(); + test_util_->ChangeModelToLoadState(); AddKeywordWithDate("name", "x", "http://x/foo", "http://sugg1", std::string(), "http://icon1", false, "UTF-8;UTF-16", Time(), Time()); @@ -920,16 +920,16 @@ TEST_F(TemplateURLServiceTest, DontUpdateKeywordSearchForNonReplaceable) { GURL(data[i].url), false }; model()->UpdateKeywordSearchTermsForURL(details); - ASSERT_EQ(base::string16(), test_util_.GetAndClearSearchTerm()); + ASSERT_EQ(base::string16(), test_util_->GetAndClearSearchTerm()); } } TEST_F(TemplateURLServiceWithoutFallbackTest, ChangeGoogleBaseValue) { // NOTE: Do not load the prepopulate data, which also has a {google:baseURL} // keyword in it and would confuse this test. - test_util_.ChangeModelToLoadState(); + test_util_->ChangeModelToLoadState(); - test_util_.SetGoogleBaseURL(GURL("http://google.com/")); + test_util_->SetGoogleBaseURL(GURL("http://google.com/")); const TemplateURL* t_url = AddKeywordWithDate( "name", "google.com", "{google:baseURL}?q={searchTerms}", "http://sugg1", std::string(), "http://icon1", false, "UTF-8;UTF-16", Time(), Time()); @@ -938,8 +938,8 @@ TEST_F(TemplateURLServiceWithoutFallbackTest, ChangeGoogleBaseValue) { EXPECT_EQ(ASCIIToUTF16("google.com"), t_url->keyword()); // Change the Google base url. - test_util_.ResetObserverCount(); - test_util_.SetGoogleBaseURL(GURL("http://google.co.uk/")); + test_util_->ResetObserverCount(); + test_util_->SetGoogleBaseURL(GURL("http://google.co.uk/")); VerifyObserverCount(1); // Make sure the host->TemplateURL map was updated appropriately. @@ -956,7 +956,7 @@ TEST_F(TemplateURLServiceWithoutFallbackTest, ChangeGoogleBaseValue) { "manual", "google.de", "http://google.de/search?q={searchTerms}", std::string(), std::string(), std::string(), false, "UTF-8", Time(), Time()); - test_util_.SetGoogleBaseURL(GURL("http://google.de")); + test_util_->SetGoogleBaseURL(GURL("http://google.de")); // Verify that the manual entry is untouched, and the autogenerated keyword // has not changed. @@ -971,7 +971,7 @@ TEST_F(TemplateURLServiceWithoutFallbackTest, ChangeGoogleBaseValue) { // Change the base URL again and verify that the autogenerated keyword follows // even though it didn't match the base URL, while the manual entry is still // untouched. - test_util_.SetGoogleBaseURL(GURL("http://google.fr/")); + test_util_->SetGoogleBaseURL(GURL("http://google.fr/")); ASSERT_EQ(manual, model()->GetTemplateURLForHost("google.de")); EXPECT_EQ("google.de", manual->url_ref().GetHost(search_terms_data())); EXPECT_EQ(ASCIIToUTF16("google.de"), manual->keyword()); @@ -984,8 +984,8 @@ TEST_F(TemplateURLServiceWithoutFallbackTest, ChangeGoogleBaseValue) { // Make sure TemplateURLService generates a KEYWORD_GENERATED visit for // KEYWORD visits. TEST_F(TemplateURLServiceTest, GenerateVisitOnKeyword) { - test_util_.VerifyLoad(); - ASSERT_TRUE(test_util_.profile()->CreateHistoryService(true, false)); + test_util_->VerifyLoad(); + ASSERT_TRUE(test_util_->profile()->CreateHistoryService(true, false)); // Create a keyword. TemplateURL* t_url = AddKeywordWithDate( @@ -995,7 +995,7 @@ TEST_F(TemplateURLServiceTest, GenerateVisitOnKeyword) { // Add a visit that matches the url of the keyword. HistoryService* history = - HistoryServiceFactory::GetForProfile(test_util_.profile(), + HistoryServiceFactory::GetForProfile(test_util_->profile(), Profile::EXPLICIT_ACCESS); history->AddPage( GURL(t_url->url_ref().ReplaceSearchTerms( @@ -1005,7 +1005,7 @@ TEST_F(TemplateURLServiceTest, GenerateVisitOnKeyword) { content::PAGE_TRANSITION_KEYWORD, history::SOURCE_BROWSED, false); // Wait for history to finish processing the request. - test_util_.profile()->BlockUntilHistoryProcessesPendingRequests(); + test_util_->profile()->BlockUntilHistoryProcessesPendingRequests(); // Query history for the generated url. base::CancelableTaskTracker tracker; @@ -1017,7 +1017,7 @@ TEST_F(TemplateURLServiceTest, GenerateVisitOnKeyword) { &tracker); // Wait for the request to be processed. - test_util_.profile()->BlockUntilHistoryProcessesPendingRequests(); + test_util_->profile()->BlockUntilHistoryProcessesPendingRequests(); // And make sure the url and visit were added. EXPECT_TRUE(callback.success); @@ -1033,14 +1033,14 @@ TEST_F(TemplateURLServiceTest, LoadDeletesUnusedProvider) { // Create a preloaded template url. Add it to a loaded model and wait for the // saves to finish. TemplateURL* t_url = CreatePreloadedTemplateURL(true, 999999); - test_util_.ChangeModelToLoadState(); + test_util_->ChangeModelToLoadState(); model()->Add(t_url); ASSERT_TRUE( model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")) != NULL); base::RunLoop().RunUntilIdle(); // Ensure that merging clears this engine. - test_util_.ResetModel(true); + test_util_->ResetModel(true); ASSERT_TRUE( model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")) == NULL); @@ -1049,7 +1049,7 @@ TEST_F(TemplateURLServiceTest, LoadDeletesUnusedProvider) { // Reload the model to verify that the database was updated as a result of the // merge. - test_util_.ResetModel(true); + test_util_->ResetModel(true); ASSERT_TRUE( model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")) == NULL); } @@ -1059,7 +1059,7 @@ TEST_F(TemplateURLServiceTest, LoadDeletesUnusedProvider) { TEST_F(TemplateURLServiceTest, LoadRetainsModifiedProvider) { // Create a preloaded template url and add it to a loaded model. TemplateURL* t_url = CreatePreloadedTemplateURL(false, 999999); - test_util_.ChangeModelToLoadState(); + test_util_->ChangeModelToLoadState(); model()->Add(t_url); // Do the copy after t_url is added so that the id is set. @@ -1070,7 +1070,7 @@ TEST_F(TemplateURLServiceTest, LoadRetainsModifiedProvider) { base::RunLoop().RunUntilIdle(); // Ensure that merging won't clear it if the user has edited it. - test_util_.ResetModel(true); + test_util_->ResetModel(true); const TemplateURL* url_for_unittest = model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")); ASSERT_TRUE(url_for_unittest != NULL); @@ -1080,7 +1080,7 @@ TEST_F(TemplateURLServiceTest, LoadRetainsModifiedProvider) { base::RunLoop().RunUntilIdle(); // Reload the model to verify that save/reload retains the item. - test_util_.ResetModel(true); + test_util_->ResetModel(true); ASSERT_TRUE( model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")) != NULL); } @@ -1089,7 +1089,7 @@ TEST_F(TemplateURLServiceTest, LoadRetainsModifiedProvider) { // prepopulated engines that no longer exist in the prepopulate data if // it has been modified by the user. TEST_F(TemplateURLServiceTest, LoadSavesPrepopulatedDefaultSearchProvider) { - test_util_.VerifyLoad(); + test_util_->VerifyLoad(); // Verify that the default search provider is set to something. TemplateURL* default_search = model()->GetDefaultSearchProvider(); ASSERT_TRUE(default_search != NULL); @@ -1100,7 +1100,7 @@ TEST_F(TemplateURLServiceTest, LoadSavesPrepopulatedDefaultSearchProvider) { // Reload the model and check that the default search provider // was properly saved. - test_util_.ResetModel(true); + test_util_->ResetModel(true); default_search = model()->GetDefaultSearchProvider(); ASSERT_TRUE(default_search != NULL); AssertEquals(*cloned_url, *default_search); @@ -1114,7 +1114,7 @@ TEST_F(TemplateURLServiceTest, LoadRetainsDefaultProvider) { // is not in the current set of preloaded template urls and save // the result. TemplateURL* t_url = CreatePreloadedTemplateURL(true, 999999); - test_util_.ChangeModelToLoadState(); + test_util_->ChangeModelToLoadState(); model()->Add(t_url); model()->SetUserSelectedDefaultSearchProvider(t_url); // Do the copy after t_url is added and set as default so that its @@ -1127,7 +1127,7 @@ TEST_F(TemplateURLServiceTest, LoadRetainsDefaultProvider) { // Ensure that merging won't clear the prepopulated template url // which is no longer present if it's the default engine. - test_util_.ResetModel(true); + test_util_->ResetModel(true); { const TemplateURL* keyword_url = model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")); @@ -1140,7 +1140,7 @@ TEST_F(TemplateURLServiceTest, LoadRetainsDefaultProvider) { base::RunLoop().RunUntilIdle(); // Reload the model to verify that the update was saved. - test_util_.ResetModel(true); + test_util_->ResetModel(true); { const TemplateURL* keyword_url = model()->GetTemplateURLForKeyword(ASCIIToUTF16("unittest")); @@ -1154,7 +1154,7 @@ TEST_F(TemplateURLServiceTest, LoadRetainsDefaultProvider) { // missing and not managed. TEST_F(TemplateURLServiceTest, LoadEnsuresDefaultSearchProviderExists) { // Force the model to load and make sure we have a default search provider. - test_util_.VerifyLoad(); + test_util_->VerifyLoad(); EXPECT_TRUE(model()->GetDefaultSearchProvider()); EXPECT_TRUE(model()->GetDefaultSearchProvider()->SupportsReplacement( @@ -1168,7 +1168,7 @@ TEST_F(TemplateURLServiceTest, LoadEnsuresDefaultSearchProviderExists) { // Reset the model and load it. There should be a usable default search // provider. - test_util_.ResetModel(true); + test_util_->ResetModel(true); ASSERT_TRUE(model()->GetDefaultSearchProvider()); EXPECT_TRUE(model()->GetDefaultSearchProvider()->SupportsReplacement( @@ -1178,13 +1178,13 @@ TEST_F(TemplateURLServiceTest, LoadEnsuresDefaultSearchProviderExists) { // Simulates failing to load the webdb and makes sure the default search // provider is valid. TEST_F(TemplateURLServiceTest, FailedInit) { - test_util_.VerifyLoad(); + test_util_->VerifyLoad(); - test_util_.ClearModel(); + test_util_->ClearModel(); WebDataServiceFactory::GetKeywordWebDataForProfile( - test_util_.profile(), Profile::EXPLICIT_ACCESS)->ShutdownDatabase(); + test_util_->profile(), Profile::EXPLICIT_ACCESS)->ShutdownDatabase(); - test_util_.ResetModel(false); + test_util_->ResetModel(false); model()->Load(); base::RunLoop().RunUntilIdle(); @@ -1195,9 +1195,9 @@ TEST_F(TemplateURLServiceTest, FailedInit) { // the default search as managed. Also check that we are getting the right // values. TEST_F(TemplateURLServiceTest, TestManagedDefaultSearch) { - test_util_.VerifyLoad(); + test_util_->VerifyLoad(); const size_t initial_count = model()->GetTemplateURLs().size(); - test_util_.ResetObserverCount(); + test_util_->ResetObserverCount(); // Set a regular default search provider. TemplateURL* regular_default = AddKeywordWithDate( @@ -1219,7 +1219,7 @@ TEST_F(TemplateURLServiceTest, TestManagedDefaultSearch) { const char kEncodings[] = "UTF-16;UTF-32"; const char kAlternateURL[] = "http://test.com/search#t={searchTerms}"; const char kSearchTermsReplacementKey[] = "espv"; - test_util_.SetManagedDefaultSearchPreferences(true, kName, kKeyword, + test_util_->SetManagedDefaultSearchPreferences(true, kName, kKeyword, kSearchURL, std::string(), kIconURL, kEncodings, kAlternateURL, kSearchTermsReplacementKey); VerifyObserverFired(); @@ -1247,7 +1247,7 @@ TEST_F(TemplateURLServiceTest, TestManagedDefaultSearch) { const char kNewKeyword[] = "other.com"; const char kNewSearchURL[] = "http://other.com/search?t={searchTerms}"; const char kNewSuggestURL[] = "http://other.com/suggest?t={searchTerms}"; - test_util_.SetManagedDefaultSearchPreferences(true, kNewName, kNewKeyword, + test_util_->SetManagedDefaultSearchPreferences(true, kNewName, kNewKeyword, kNewSearchURL, kNewSuggestURL, std::string(), std::string(), std::string(), std::string()); VerifyObserverFired(); @@ -1267,7 +1267,7 @@ TEST_F(TemplateURLServiceTest, TestManagedDefaultSearch) { EXPECT_EQ(actual_managed_default->show_in_default_list(), true); // Remove all the managed prefs and check that we are no longer managed. - test_util_.RemoveManagedDefaultSearchPreferences(); + test_util_->RemoveManagedDefaultSearchPreferences(); VerifyObserverFired(); EXPECT_FALSE(model()->is_default_search_managed()); EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); @@ -1279,7 +1279,7 @@ TEST_F(TemplateURLServiceTest, TestManagedDefaultSearch) { EXPECT_EQ(actual_final_managed_default->show_in_default_list(), true); // Disable the default search provider through policy. - test_util_.SetManagedDefaultSearchPreferences(false, std::string(), + test_util_->SetManagedDefaultSearchPreferences(false, std::string(), std::string(), std::string(), std::string(), std::string(), std::string(), std::string(), std::string()); VerifyObserverFired(); @@ -1288,7 +1288,7 @@ TEST_F(TemplateURLServiceTest, TestManagedDefaultSearch) { EXPECT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); // Re-enable it. - test_util_.SetManagedDefaultSearchPreferences(true, kName, kKeyword, + test_util_->SetManagedDefaultSearchPreferences(true, kName, kKeyword, kSearchURL, std::string(), kIconURL, kEncodings, kAlternateURL, kSearchTermsReplacementKey); VerifyObserverFired(); @@ -1305,8 +1305,8 @@ TEST_F(TemplateURLServiceTest, TestManagedDefaultSearch) { // This checks against regressions of http://crbug.com/67180 // First, remove the preferences, reset the model, and set a default. - test_util_.RemoveManagedDefaultSearchPreferences(); - test_util_.ResetModel(true); + test_util_->RemoveManagedDefaultSearchPreferences(); + test_util_->ResetModel(true); TemplateURL* new_default = model()->GetTemplateURLForKeyword(ASCIIToUTF16("key1")); ASSERT_FALSE(new_default == NULL); @@ -1314,11 +1314,11 @@ TEST_F(TemplateURLServiceTest, TestManagedDefaultSearch) { EXPECT_EQ(new_default, model()->GetDefaultSearchProvider()); // Now reset the model again but load it after setting the preferences. - test_util_.ResetModel(false); - test_util_.SetManagedDefaultSearchPreferences(false, std::string(), + test_util_->ResetModel(false); + test_util_->SetManagedDefaultSearchPreferences(false, std::string(), std::string(), std::string(), std::string(), std::string(), std::string(), std::string(), std::string()); - test_util_.VerifyLoad(); + test_util_->VerifyLoad(); EXPECT_TRUE(model()->is_default_search_managed()); EXPECT_TRUE(model()->GetDefaultSearchProvider() == NULL); } @@ -1327,7 +1327,7 @@ TEST_F(TemplateURLServiceTest, TestManagedDefaultSearch) { // assigns it a newly generated GUID. TEST_F(TemplateURLServiceTest, PatchEmptySyncGUID) { // Add a new TemplateURL. - test_util_.VerifyLoad(); + test_util_->VerifyLoad(); const size_t initial_count = model()->GetTemplateURLs().size(); TemplateURLData data; @@ -1344,7 +1344,7 @@ TEST_F(TemplateURLServiceTest, PatchEmptySyncGUID) { // Reload the model to verify it was actually saved to the database and // assigned a new GUID when brought back. - test_util_.ResetModel(true); + test_util_->ResetModel(true); ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); const TemplateURL* loaded_url = model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")); @@ -1356,7 +1356,7 @@ TEST_F(TemplateURLServiceTest, PatchEmptySyncGUID) { // process de-dupes them. TEST_F(TemplateURLServiceTest, DuplicateInputEncodings) { // Add a new TemplateURL. - test_util_.VerifyLoad(); + test_util_->VerifyLoad(); const size_t initial_count = model()->GetTemplateURLs().size(); TemplateURLData data; @@ -1385,7 +1385,7 @@ TEST_F(TemplateURLServiceTest, DuplicateInputEncodings) { // Reload the model to verify it was actually saved to the database and the // duplicate encodings were removed. - test_util_.ResetModel(true); + test_util_->ResetModel(true); ASSERT_EQ(initial_count + 1, model()->GetTemplateURLs().size()); loaded_url = model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword")); ASSERT_FALSE(loaded_url == NULL); @@ -1393,7 +1393,7 @@ TEST_F(TemplateURLServiceTest, DuplicateInputEncodings) { } TEST_F(TemplateURLServiceTest, DefaultExtensionEngine) { - test_util_.VerifyLoad(); + test_util_->VerifyLoad(); // Add third-party default search engine. TemplateURL* user_dse = AddKeywordWithDate( "user", "user", "http://www.goo.com/s?q={searchTerms}", @@ -1419,7 +1419,7 @@ TEST_F(TemplateURLServiceTest, DefaultExtensionEngine) { } TEST_F(TemplateURLServiceTest, ExtensionEnginesNotPersist) { - test_util_.VerifyLoad(); + test_util_->VerifyLoad(); // Add third-party default search engine. TemplateURL* user_dse = AddKeywordWithDate( "user", "user", "http://www.goo.com/s?q={searchTerms}", @@ -1449,7 +1449,7 @@ TEST_F(TemplateURLServiceTest, ExtensionEnginesNotPersist) { model()->AddExtensionControlledTURL(ext_dse, extension_info.Pass()); EXPECT_EQ(ext_dse, model()->GetDefaultSearchProvider()); - test_util_.ResetModel(true); + test_util_->ResetModel(true); user_dse = model()->GetTemplateURLForKeyword(ASCIIToUTF16("user")); ExpectSimilar(user_dse, model()->GetDefaultSearchProvider()); EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("ext1"))); @@ -1465,10 +1465,10 @@ TEST_F(TemplateURLServiceTest, ExtensionEngineVsPolicy) { const char kEncodings[] = "UTF-16;UTF-32"; const char kAlternateURL[] = "http://test.com/search#t={searchTerms}"; const char kSearchTermsReplacementKey[] = "espv"; - test_util_.SetManagedDefaultSearchPreferences( + test_util_->SetManagedDefaultSearchPreferences( true, kName, kKeyword, kSearchURL, std::string(), kIconURL, kEncodings, kAlternateURL, kSearchTermsReplacementKey); - test_util_.VerifyLoad(); + test_util_->VerifyLoad(); // Verify that the default manager we are getting is the managed one. TemplateURLData data; data.short_name = ASCIIToUTF16(kName); diff --git a/chrome/browser/sync/glue/search_engine_data_type_controller_unittest.cc b/chrome/browser/sync/glue/search_engine_data_type_controller_unittest.cc index dfb6b29..52f0b91 100644 --- a/chrome/browser/sync/glue/search_engine_data_type_controller_unittest.cc +++ b/chrome/browser/sync/glue/search_engine_data_type_controller_unittest.cc @@ -9,13 +9,14 @@ #include "base/run_loop.h" #include "base/tracked_objects.h" #include "chrome/browser/search_engines/template_url_service_factory.h" -#include "chrome/browser/search_engines/template_url_service_test_util.h" +#include "chrome/browser/search_engines/template_url_service_factory_test_util.h" #include "chrome/browser/sync/glue/search_engine_data_type_controller.h" #include "chrome/browser/sync/profile_sync_components_factory_mock.h" #include "chrome/browser/sync/profile_sync_service_mock.h" #include "chrome/test/base/profile_mock.h" #include "components/sync_driver/data_type_controller_mock.h" #include "components/sync_driver/fake_generic_change_processor.h" +#include "content/public/test/test_browser_thread_bundle.h" #include "sync/api/fake_syncable_service.h" #include "testing/gtest/include/gtest/gtest.h" @@ -30,18 +31,16 @@ namespace { class SyncSearchEngineDataTypeControllerTest : public testing::Test { public: - SyncSearchEngineDataTypeControllerTest() { } + SyncSearchEngineDataTypeControllerTest() : test_util_(&profile_) { } virtual void SetUp() { - test_util_.SetUp(); - service_.reset(new ProfileSyncServiceMock(test_util_.profile())); + service_.reset(new ProfileSyncServiceMock(&profile_)); profile_sync_factory_.reset(new ProfileSyncComponentsFactoryMock()); - // Feed the DTC test_util_'s profile so it is reused later. + // Feed the DTC the profile so it is reused later. // This allows us to control the associated TemplateURLService. - search_engine_dtc_ = - new SearchEngineDataTypeController( - profile_sync_factory_.get(), test_util_.profile(), - DataTypeController::DisableTypeCallback()); + search_engine_dtc_ = new SearchEngineDataTypeController( + profile_sync_factory_.get(), &profile_, + DataTypeController::DisableTypeCallback()); } virtual void TearDown() { @@ -49,7 +48,6 @@ class SyncSearchEngineDataTypeControllerTest : public testing::Test { syncable_service_.StopSyncing(syncer::SEARCH_ENGINES); search_engine_dtc_ = NULL; service_.reset(); - test_util_.TearDown(); } protected: @@ -78,10 +76,9 @@ class SyncSearchEngineDataTypeControllerTest : public testing::Test { base::Unretained(&start_callback_))); } - // This also manages a BrowserThread and MessageLoop for us. Note that this - // must be declared here as the destruction order of the BrowserThread - // matters - we could leak if this is declared below. - TemplateURLServiceTestUtil test_util_; + content::TestBrowserThreadBundle thread_bundle_; + TestingProfile profile_; + TemplateURLServiceFactoryTestUtil test_util_; scoped_refptr<SearchEngineDataTypeController> search_engine_dtc_; scoped_ptr<ProfileSyncComponentsFactoryMock> profile_sync_factory_; scoped_ptr<ProfileSyncServiceMock> service_; diff --git a/chrome/browser/ui/search_engines/keyword_editor_controller_unittest.cc b/chrome/browser/ui/search_engines/keyword_editor_controller_unittest.cc index b84a081..58d3e6a 100644 --- a/chrome/browser/ui/search_engines/keyword_editor_controller_unittest.cc +++ b/chrome/browser/ui/search_engines/keyword_editor_controller_unittest.cc @@ -6,12 +6,14 @@ #include "base/strings/string16.h" #include "base/strings/utf_string_conversions.h" #include "chrome/browser/profiles/profile.h" -#include "chrome/browser/search_engines/template_url_service_test_util.h" +#include "chrome/browser/search_engines/template_url_service_factory_test_util.h" #include "chrome/browser/ui/search_engines/keyword_editor_controller.h" #include "chrome/browser/ui/search_engines/template_url_table_model.h" +#include "chrome/test/base/testing_pref_service_syncable.h" #include "chrome/test/base/testing_profile.h" #include "components/search_engines/template_url.h" #include "components/search_engines/template_url_service.h" +#include "content/public/test/test_browser_thread_bundle.h" #include "testing/gtest/include/gtest/gtest.h" #include "ui/base/models/table_model_observer.h" @@ -28,34 +30,33 @@ class KeywordEditorControllerTest : public testing::Test, public ui::TableModelObserver { public: KeywordEditorControllerTest() - : simulate_load_failure_(false), + : util_(&profile_), + simulate_load_failure_(false), model_changed_count_(0), items_changed_count_(0), added_count_(0), removed_count_(0) {} explicit KeywordEditorControllerTest(bool simulate_load_failure) - : simulate_load_failure_(simulate_load_failure), + : util_(&profile_), + simulate_load_failure_(simulate_load_failure), model_changed_count_(0), items_changed_count_(0), added_count_(0), removed_count_(0) {} virtual void SetUp() OVERRIDE { - util_.SetUp(); - if (simulate_load_failure_) util_.model()->OnWebDataServiceRequestDone(0, NULL); else - util_.ChangeModelToLoadState(); + util_.VerifyLoad(); - controller_.reset(new KeywordEditorController(util_.profile())); + controller_.reset(new KeywordEditorController(&profile_)); controller_->table_model()->SetObserver(this); } virtual void TearDown() OVERRIDE { controller_.reset(); - util_.TearDown(); } virtual void OnModelChanged() OVERRIDE { @@ -102,15 +103,17 @@ class KeywordEditorControllerTest : public testing::Test, TemplateURLTableModel* table_model() { return controller_->table_model(); } KeywordEditorController* controller() { return controller_.get(); } - const TemplateURLServiceTestUtil* util() const { return &util_; } + const TemplateURLServiceFactoryTestUtil* util() const { return &util_; } int items_changed_count() const { return items_changed_count_; } int added_count() const { return added_count_; } int removed_count() const { return removed_count_; } private: + content::TestBrowserThreadBundle thread_bundle_; + TestingProfile profile_; scoped_ptr<KeywordEditorController> controller_; - TemplateURLServiceTestUtil util_; + TemplateURLServiceFactoryTestUtil util_; bool simulate_load_failure_; int model_changed_count_; diff --git a/chrome/chrome_tests_unit.gypi b/chrome/chrome_tests_unit.gypi index 6c8bf00..d1e34d9 100644 --- a/chrome/chrome_tests_unit.gypi +++ b/chrome/chrome_tests_unit.gypi @@ -26,6 +26,7 @@ '../components/components.gyp:metrics_test_support', '../components/components.gyp:password_manager_core_browser_test_support', '../components/components.gyp:pref_registry_test_support', + '../components/components.gyp:search_engines_test_support', '../components/components.gyp:signin_core_browser_test_support', '../components/components.gyp:sync_driver_test_support', '../content/content.gyp:content_app_both', @@ -172,6 +173,8 @@ 'browser/prefs/pref_service_mock_factory.h', 'browser/profile_resetter/profile_resetter_test_base.cc', 'browser/profile_resetter/profile_resetter_test_base.h', + 'browser/search_engines/template_url_service_factory_test_util.cc', + 'browser/search_engines/template_url_service_factory_test_util.h', 'browser/search_engines/template_url_service_test_util.cc', 'browser/search_engines/template_url_service_test_util.h', 'browser/sessions/session_service_test_helper.cc', diff --git a/chrome/test/base/ui_test_utils.cc b/chrome/test/base/ui_test_utils.cc index ff06e5d..2fa3c02 100644 --- a/chrome/test/base/ui_test_utils.cc +++ b/chrome/test/base/ui_test_utils.cc @@ -32,7 +32,6 @@ #include "chrome/browser/extensions/extension_action.h" #include "chrome/browser/history/history_service_factory.h" #include "chrome/browser/profiles/profile.h" -#include "chrome/browser/search_engines/template_url_service_test_util.h" #include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog.h" #include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog_queue.h" #include "chrome/browser/ui/browser.h" diff --git a/components/search_engines.gypi b/components/search_engines.gypi index f3314a0..2768918 100644 --- a/components/search_engines.gypi +++ b/components/search_engines.gypi @@ -69,5 +69,20 @@ 'search_engines/util.h', ], }, + { + 'target_name': 'search_engines_test_support', + 'type': 'static_library', + 'dependencies': [ + '../testing/gtest.gyp:gtest', + 'search_engines', + ], + 'include_dirs': [ + '..', + ], + 'sources': [ + 'search_engines/default_search_pref_test_util.cc', + 'search_engines/default_search_pref_test_util.h', + ], + }, ], } diff --git a/components/search_engines/default_search_pref_test_util.cc b/components/search_engines/default_search_pref_test_util.cc new file mode 100644 index 0000000..f3041b6 --- /dev/null +++ b/components/search_engines/default_search_pref_test_util.cc @@ -0,0 +1,60 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "components/search_engines/default_search_pref_test_util.h" + +#include "base/strings/string_split.h" +#include "components/search_engines/default_search_manager.h" +#include "testing/gtest/include/gtest/gtest.h" + +// static +scoped_ptr<base::DictionaryValue> +DefaultSearchPrefTestUtil::CreateDefaultSearchPreferenceValue( + bool enabled, + const std::string& name, + const std::string& keyword, + const std::string& search_url, + const std::string& suggest_url, + const std::string& icon_url, + const std::string& encodings, + const std::string& alternate_url, + const std::string& search_terms_replacement_key) { + scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue); + if (!enabled) { + value->SetBoolean(DefaultSearchManager::kDisabledByPolicy, true); + return value.Pass(); + } + + EXPECT_FALSE(keyword.empty()); + EXPECT_FALSE(search_url.empty()); + value->Set(DefaultSearchManager::kShortName, + base::Value::CreateStringValue(name)); + value->Set(DefaultSearchManager::kKeyword, + base::Value::CreateStringValue(keyword)); + value->Set(DefaultSearchManager::kURL, + base::Value::CreateStringValue(search_url)); + value->Set(DefaultSearchManager::kSuggestionsURL, + base::Value::CreateStringValue(suggest_url)); + value->Set(DefaultSearchManager::kFaviconURL, + base::Value::CreateStringValue(icon_url)); + value->Set(DefaultSearchManager::kSearchTermsReplacementKey, + base::Value::CreateStringValue(search_terms_replacement_key)); + + std::vector<std::string> encodings_items; + base::SplitString(encodings, ';', &encodings_items); + scoped_ptr<base::ListValue> encodings_list(new base::ListValue); + for (std::vector<std::string>::const_iterator it = encodings_items.begin(); + it != encodings_items.end(); + ++it) { + encodings_list->AppendString(*it); + } + value->Set(DefaultSearchManager::kInputEncodings, encodings_list.release()); + + scoped_ptr<base::ListValue> alternate_url_list(new base::ListValue()); + if (!alternate_url.empty()) + alternate_url_list->Append(base::Value::CreateStringValue(alternate_url)); + value->Set(DefaultSearchManager::kAlternateURLs, + alternate_url_list.release()); + return value.Pass(); +} diff --git a/components/search_engines/default_search_pref_test_util.h b/components/search_engines/default_search_pref_test_util.h new file mode 100644 index 0000000..53484e6 --- /dev/null +++ b/components/search_engines/default_search_pref_test_util.h @@ -0,0 +1,60 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef COMPONENTS_SEARCH_ENGINES_DEFAULT_SEARCH_PREF_TEST_UTIL_H_ +#define COMPONENTS_SEARCH_ENGINES_DEFAULT_SEARCH_PREF_TEST_UTIL_H_ + +#include <string> + +#include "base/basictypes.h" +#include "base/memory/scoped_ptr.h" +#include "base/values.h" +#include "components/search_engines/default_search_manager.h" + +class DefaultSearchPrefTestUtil { + public: + // Creates a DictionaryValue which can be used as a + // kDefaultSearchProviderDataPrefName preference value. + static scoped_ptr<base::DictionaryValue> CreateDefaultSearchPreferenceValue( + bool enabled, + const std::string& name, + const std::string& keyword, + const std::string& search_url, + const std::string& suggest_url, + const std::string& icon_url, + const std::string& encodings, + const std::string& alternate_url, + const std::string& search_terms_replacement_key); + + // Set the managed preferences for the default search provider and trigger + // notification. If |alternate_url| is empty, uses an empty list of alternate + // URLs, otherwise use a list containing a single entry. + template<typename TestingPrefService> + static void SetManagedPref(TestingPrefService* pref_service, + bool enabled, + const std::string& name, + const std::string& keyword, + const std::string& search_url, + const std::string& suggest_url, + const std::string& icon_url, + const std::string& encodings, + const std::string& alternate_url, + const std::string& search_terms_replacement_key) { + pref_service->SetManagedPref( + DefaultSearchManager::kDefaultSearchProviderDataPrefName, + CreateDefaultSearchPreferenceValue( + enabled, name, keyword, search_url, suggest_url, icon_url, + encodings, alternate_url, search_terms_replacement_key).release()); + } + + // Remove all the managed preferences for the default search provider and + // trigger notification. + template<typename TestingPrefService> + static void RemoveManagedPref(TestingPrefService* pref_service) { + pref_service->RemoveManagedPref( + DefaultSearchManager::kDefaultSearchProviderDataPrefName); + } +}; + +#endif // COMPONENTS_SEARCH_ENGINES_DEFAULT_SEARCH_PREF_TEST_UTIL_H_ diff --git a/components/search_engines/template_url_service.h b/components/search_engines/template_url_service.h index da49772..3e4f19a 100644 --- a/components/search_engines/template_url_service.h +++ b/components/search_engines/template_url_service.h @@ -399,7 +399,7 @@ class TemplateURLService : public WebDataServiceConsumer, FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, MergeInSyncTemplateURL); friend class InstantUnitTestBase; - friend class TemplateURLServiceTestUtilBase; + friend class TemplateURLServiceTestUtil; typedef std::map<base::string16, TemplateURL*> KeywordToTemplateMap; typedef std::map<std::string, TemplateURL*> GUIDToTemplateMap; |