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
parent 1ea46f6f
...@@ -96,14 +96,9 @@ class AutocompleteResultTest : public testing::Test { ...@@ -96,14 +96,9 @@ class AutocompleteResultTest : public testing::Test {
TemplateURLPrepopulateData::InitCountryCode( TemplateURLPrepopulateData::InitCountryCode(
std::string() /* unknown country code */); std::string() /* unknown country code */);
#endif #endif
test_util_.SetUp();
test_util_.VerifyLoad(); test_util_.VerifyLoad();
} }
virtual void TearDown() OVERRIDE {
test_util_.TearDown();
}
// Configures |match| from |data|. // Configures |match| from |data|.
static void PopulateAutocompleteMatch(const TestData& data, static void PopulateAutocompleteMatch(const TestData& data,
AutocompleteMatch* match); AutocompleteMatch* match);
......
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
#include "chrome/browser/profile_resetter/brandcoded_default_settings.h" #include "chrome/browser/profile_resetter/brandcoded_default_settings.h"
#include "chrome/browser/profile_resetter/profile_reset_global_error.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_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.h"
#include "chrome/browser/ui/global_error/global_error_service.h" #include "chrome/browser/ui/global_error/global_error_service.h"
#include "chrome/browser/ui/global_error/global_error_service_factory.h" #include "chrome/browser/ui/global_error/global_error_service_factory.h"
...@@ -162,8 +162,7 @@ void ServicePendingBrancodedConfigFetch(net::TestURLFetcher* fetcher, ...@@ -162,8 +162,7 @@ void ServicePendingBrancodedConfigFetch(net::TestURLFetcher* fetcher,
// ExtensionServiceTestBase sets up a TestingProfile with the ExtensionService, // ExtensionServiceTestBase sets up a TestingProfile with the ExtensionService,
// we then add the TemplateURLService, so the ProfileResetter can be exercised. // we then add the TemplateURLService, so the ProfileResetter can be exercised.
class AutomaticProfileResetterDelegateTest class AutomaticProfileResetterDelegateTest
: public extensions::ExtensionServiceTestBase, : public extensions::ExtensionServiceTestBase {
public TemplateURLServiceTestUtilBase {
protected: protected:
AutomaticProfileResetterDelegateTest() {} AutomaticProfileResetterDelegateTest() {}
virtual ~AutomaticProfileResetterDelegateTest() {} virtual ~AutomaticProfileResetterDelegateTest() {}
...@@ -173,13 +172,15 @@ class AutomaticProfileResetterDelegateTest ...@@ -173,13 +172,15 @@ class AutomaticProfileResetterDelegateTest
ExtensionServiceInitParams params = CreateDefaultInitParams(); ExtensionServiceInitParams params = CreateDefaultInitParams();
params.pref_file.clear(); // Prescribes a TestingPrefService to be created. params.pref_file.clear(); // Prescribes a TestingPrefService to be created.
InitializeExtensionService(params); InitializeExtensionService(params);
TemplateURLServiceTestUtilBase::CreateTemplateUrlService(); template_url_service_test_util_.reset(
new TemplateURLServiceFactoryTestUtil(profile_.get()));
resetter_delegate_.reset( resetter_delegate_.reset(
new AutomaticProfileResetterDelegateUnderTest(profile())); new AutomaticProfileResetterDelegateUnderTest(profile()));
} }
virtual void TearDown() OVERRIDE { virtual void TearDown() OVERRIDE {
resetter_delegate_.reset(); resetter_delegate_.reset();
template_url_service_test_util_.reset();
extensions::ExtensionServiceTestBase::TearDown(); extensions::ExtensionServiceTestBase::TearDown();
} }
...@@ -233,10 +234,12 @@ class AutomaticProfileResetterDelegateTest ...@@ -233,10 +234,12 @@ class AutomaticProfileResetterDelegateTest
return resetter_delegate_.get(); return resetter_delegate_.get();
} }
// TemplateURLServiceTestUtilBase: TemplateURLServiceFactoryTestUtil* template_url_service_test_util() {
virtual TestingProfile* profile() const OVERRIDE { return profile_.get(); } return template_url_service_test_util_.get();
}
private: private:
scoped_ptr<TemplateURLServiceFactoryTestUtil> template_url_service_test_util_;
net::TestURLFetcherFactory test_url_fetcher_factory_; net::TestURLFetcherFactory test_url_fetcher_factory_;
scoped_ptr<AutomaticProfileResetterDelegateUnderTest> resetter_delegate_; scoped_ptr<AutomaticProfileResetterDelegateUnderTest> resetter_delegate_;
...@@ -346,7 +349,7 @@ TEST_F(AutomaticProfileResetterDelegateTest, ...@@ -346,7 +349,7 @@ TEST_F(AutomaticProfileResetterDelegateTest,
DefaultSearchProviderDataWhenNotManaged) { DefaultSearchProviderDataWhenNotManaged) {
TemplateURLService* template_url_service = TemplateURLService* template_url_service =
TemplateURLServiceFactory::GetForProfile(profile()); TemplateURLServiceFactory::GetForProfile(profile());
TemplateURLServiceTestUtilBase::VerifyLoad(); template_url_service_test_util()->VerifyLoad();
// Check that the "managed state" and the details returned by the delegate are // Check that the "managed state" and the details returned by the delegate are
// correct. We verify the details against the data stored by // correct. We verify the details against the data stored by
...@@ -373,12 +376,12 @@ TEST_F(AutomaticProfileResetterDelegateTest, ...@@ -373,12 +376,12 @@ TEST_F(AutomaticProfileResetterDelegateTest,
const char kTestName[] = "name"; const char kTestName[] = "name";
const char kTestKeyword[] = "keyword"; const char kTestKeyword[] = "keyword";
TemplateURLServiceTestUtilBase::VerifyLoad(); template_url_service_test_util()->VerifyLoad();
EXPECT_FALSE(resetter_delegate()->IsDefaultSearchProviderManaged()); EXPECT_FALSE(resetter_delegate()->IsDefaultSearchProviderManaged());
// Set managed preferences to emulate a default search provider set by policy. // 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(), true, kTestName, kTestKeyword, kTestSearchURL, std::string(),
std::string(), std::string(), std::string(), std::string()); std::string(), std::string(), std::string(), std::string());
...@@ -391,8 +394,8 @@ TEST_F(AutomaticProfileResetterDelegateTest, ...@@ -391,8 +394,8 @@ TEST_F(AutomaticProfileResetterDelegateTest,
// Set managed preferences to emulate that having a default search provider is // Set managed preferences to emulate that having a default search provider is
// disabled by policy. // disabled by policy.
RemoveManagedDefaultSearchPreferences(); template_url_service_test_util()->RemoveManagedDefaultSearchPreferences();
SetManagedDefaultSearchPreferences( template_url_service_test_util()->SetManagedDefaultSearchPreferences(
false, std::string(), std::string(), std::string(), std::string(), false, std::string(), std::string(), std::string(), std::string(),
std::string(), std::string(), std::string(), std::string()); std::string(), std::string(), std::string(), std::string());
...@@ -405,7 +408,7 @@ TEST_F(AutomaticProfileResetterDelegateTest, ...@@ -405,7 +408,7 @@ TEST_F(AutomaticProfileResetterDelegateTest,
GetPrepopulatedSearchProvidersDetails) { GetPrepopulatedSearchProvidersDetails) {
TemplateURLService* template_url_service = TemplateURLService* template_url_service =
TemplateURLServiceFactory::GetForProfile(profile()); TemplateURLServiceFactory::GetForProfile(profile());
TemplateURLServiceTestUtilBase::VerifyLoad(); template_url_service_test_util()->VerifyLoad();
scoped_ptr<base::ListValue> search_engines_details( scoped_ptr<base::ListValue> search_engines_details(
resetter_delegate()->GetPrepopulatedSearchProvidersDetails()); resetter_delegate()->GetPrepopulatedSearchProvidersDetails());
......
...@@ -14,11 +14,9 @@ ...@@ -14,11 +14,9 @@
#include "base/logging.h" #include "base/logging.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/sequenced_task_runner_helpers.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/google/core/browser/google_url_tracker.h"
#include "components/search_engines/search_host_to_urls_map.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.h"
#include "components/search_engines/template_url_service.h" #include "components/search_engines/template_url_service.h"
#include "components/search_engines/util.h" #include "components/search_engines/util.h"
...@@ -181,14 +179,14 @@ static bool IsSameOrigin(const GURL& requested_origin, ...@@ -181,14 +179,14 @@ static bool IsSameOrigin(const GURL& requested_origin,
} // namespace } // namespace
SearchProviderInstallData::SearchProviderInstallData( SearchProviderInstallData::SearchProviderInstallData(
Profile* profile, TemplateURLService* template_url_service,
const std::string& google_base_url,
GoogleURLTracker* google_url_tracker,
content::RenderProcessHost* host) content::RenderProcessHost* host)
: template_url_service_(TemplateURLServiceFactory::GetForProfile(profile)), : template_url_service_(template_url_service),
google_base_url_(UIThreadSearchTermsData(profile).GoogleBaseURLValue()), google_base_url_(google_base_url),
weak_factory_(this) { weak_factory_(this) {
// GoogleURLTracker is not created in tests. // GoogleURLTracker is not created in tests.
GoogleURLTracker* google_url_tracker =
GoogleURLTrackerFactory::GetForProfile(profile);
if (google_url_tracker) { if (google_url_tracker) {
// GoogleURLObserver is responsible for killing itself when // GoogleURLObserver is responsible for killing itself when
// the given notification occurs. // the given notification occurs.
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
class GURL; class GURL;
class Profile; class GoogleURLTracker;
class SearchHostToURLsMap; class SearchHostToURLsMap;
class TemplateURL; class TemplateURL;
class TemplateURLService; class TemplateURLService;
...@@ -45,7 +45,10 @@ class SearchProviderInstallData { ...@@ -45,7 +45,10 @@ class SearchProviderInstallData {
// signal to this class that it no longer needs to be kept up to date. (Note // 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 // that this class may be deleted before or after that death occurs. It
// doesn't matter.) // 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(); virtual ~SearchProviderInstallData();
// Use to determine when the search provider information is loaded. The // Use to determine when the search provider information is loaded. The
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include "chrome/browser/search_engines/template_url_service_test_util.h" #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_pref_service_syncable.h"
#include "chrome/test/base/testing_profile.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.h"
#include "components/search_engines/template_url_prepopulate_data.h" #include "components/search_engines/template_url_prepopulate_data.h"
#include "components/search_engines/template_url_service.h" #include "components/search_engines/template_url_service.h"
...@@ -152,10 +153,10 @@ void SearchProviderInstallDataTest::SetUp() { ...@@ -152,10 +153,10 @@ void SearchProviderInstallDataTest::SetUp() {
TemplateURLPrepopulateData::InitCountryCode( TemplateURLPrepopulateData::InitCountryCode(
std::string() /* unknown country code */); std::string() /* unknown country code */);
#endif #endif
util_.SetUp();
process_.reset(new content::MockRenderProcessHost(util_.profile())); process_.reset(new content::MockRenderProcessHost(util_.profile()));
install_data_ = install_data_ = new SearchProviderInstallData(
new SearchProviderInstallData(util_.profile(), process_.get()); util_.model(), SearchTermsData().GoogleBaseURLValue(), NULL,
process_.get());
} }
void SearchProviderInstallDataTest::TearDown() { void SearchProviderInstallDataTest::TearDown() {
...@@ -166,7 +167,6 @@ void SearchProviderInstallDataTest::TearDown() { ...@@ -166,7 +167,6 @@ void SearchProviderInstallDataTest::TearDown() {
// It doesn't matter that this happens after install_data_ is deleted. // It doesn't matter that this happens after install_data_ is deleted.
process_.reset(); process_.reset();
util_.TearDown();
testing::Test::TearDown(); testing::Test::TearDown();
} }
......
...@@ -6,7 +6,10 @@ ...@@ -6,7 +6,10 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/logging.h" #include "base/logging.h"
#include "chrome/browser/google/google_url_tracker_factory.h"
#include "chrome/browser/profiles/profile.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 "chrome/common/render_messages.h"
#include "content/public/browser/render_process_host.h" #include "content/public/browser/render_process_host.h"
#include "url/gurl.h" #include "url/gurl.h"
...@@ -18,7 +21,9 @@ SearchProviderInstallStateMessageFilter( ...@@ -18,7 +21,9 @@ SearchProviderInstallStateMessageFilter(
int render_process_id, int render_process_id,
Profile* profile) Profile* profile)
: BrowserMessageFilter(ChromeMsgStart), : BrowserMessageFilter(ChromeMsgStart),
provider_data_(profile, provider_data_(TemplateURLServiceFactory::GetForProfile(profile),
UIThreadSearchTermsData(profile).GoogleBaseURLValue(),
GoogleURLTrackerFactory::GetForProfile(profile),
content::RenderProcessHost::FromID(render_process_id)), content::RenderProcessHost::FromID(render_process_id)),
is_off_the_record_(profile->IsOffTheRecord()), is_off_the_record_(profile->IsOffTheRecord()),
weak_factory_(this) { weak_factory_(this) {
......
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
#include "base/message_loop/message_loop.h" #include "base/message_loop/message_loop.h"
#include "base/path_service.h" #include "base/path_service.h"
#include "base/strings/utf_string_conversions.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/browser/search_engines/template_url_service_test_util.h"
#include "chrome/common/chrome_paths.h" #include "chrome/common/chrome_paths.h"
#include "chrome/test/base/testing_profile.h" #include "chrome/test/base/testing_profile.h"
...@@ -27,18 +26,16 @@ class TemplateURLFetcherTest : public testing::Test { ...@@ -27,18 +26,16 @@ class TemplateURLFetcherTest : public testing::Test {
TemplateURLFetcherTest(); TemplateURLFetcherTest();
virtual void SetUp() OVERRIDE { virtual void SetUp() OVERRIDE {
test_util_.SetUp();
TestingProfile* profile = test_util_.profile(); TestingProfile* profile = test_util_.profile();
ASSERT_TRUE(profile);
ASSERT_TRUE(TemplateURLFetcherFactory::GetForProfile(profile));
ASSERT_TRUE(profile->GetRequestContext()); ASSERT_TRUE(profile->GetRequestContext());
template_url_fetcher_.reset(new TemplateURLFetcher(
test_util_.model(), profile->GetRequestContext()));
ASSERT_TRUE(test_server_.InitializeAndWaitUntilReady()); ASSERT_TRUE(test_server_.InitializeAndWaitUntilReady());
} }
virtual void TearDown() OVERRIDE { virtual void TearDown() OVERRIDE {
ASSERT_TRUE(test_server_.ShutdownAndWaitUntilComplete()); ASSERT_TRUE(test_server_.ShutdownAndWaitUntilComplete());
test_util_.TearDown();
} }
// Called when the callback is destroyed. // Called when the callback is destroyed.
...@@ -61,6 +58,7 @@ class TemplateURLFetcherTest : public testing::Test { ...@@ -61,6 +58,7 @@ class TemplateURLFetcherTest : public testing::Test {
void WaitForDownloadToFinish(); void WaitForDownloadToFinish();
TemplateURLServiceTestUtil test_util_; TemplateURLServiceTestUtil test_util_;
scoped_ptr<TemplateURLFetcher> template_url_fetcher_;
net::test_server::EmbeddedTestServer test_server_; net::test_server::EmbeddedTestServer test_server_;
// The last TemplateURL to come from a callback. // The last TemplateURL to come from a callback.
...@@ -125,14 +123,13 @@ void TemplateURLFetcherTest::StartDownload( ...@@ -125,14 +123,13 @@ void TemplateURLFetcherTest::StartDownload(
base::Bind(&TemplateURLFetcherTest::DestroyedCallback, base::Bind(&TemplateURLFetcherTest::DestroyedCallback,
base::Unretained(this))); base::Unretained(this)));
TemplateURLFetcherFactory::GetForProfile( template_url_fetcher_->ScheduleDownload(
test_util_.profile())->ScheduleDownload( keyword, osdd_url, favicon_url,
keyword, osdd_url, favicon_url, TemplateURLFetcher::URLFetcherCustomizeCallback(),
TemplateURLFetcher::URLFetcherCustomizeCallback(), base::Bind(&TemplateURLFetcherTest::ConfirmAddSearchProvider,
base::Bind(&TemplateURLFetcherTest::ConfirmAddSearchProvider, base::Unretained(this),
base::Unretained(this), base::Owned(callback_destruction_notifier)),
base::Owned(callback_destruction_notifier)), provider_type);
provider_type);
} }
void TemplateURLFetcherTest::WaitForDownloadToFinish() { void TemplateURLFetcherTest::WaitForDownloadToFinish() {
...@@ -197,11 +194,8 @@ TEST_F(TemplateURLFetcherTest, DuplicatesThrownAway) { ...@@ -197,11 +194,8 @@ TEST_F(TemplateURLFetcherTest, DuplicatesThrownAway) {
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
StartDownload(test_cases[i].keyword, test_cases[i].osdd_file_name, StartDownload(test_cases[i].keyword, test_cases[i].osdd_file_name,
test_cases[i].provider_type, false); test_cases[i].provider_type, false);
ASSERT_EQ( ASSERT_EQ(1, template_url_fetcher_->requests_count())
1, << test_cases[i].description;
TemplateURLFetcherFactory::GetForProfile(
test_util_.profile())->requests_count()) <<
test_cases[i].description;
ASSERT_EQ(i + 1, static_cast<size_t>(callbacks_destroyed_)); ASSERT_EQ(i + 1, static_cast<size_t>(callbacks_destroyed_));
} }
......
// 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_);
}
// 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_
...@@ -157,12 +157,12 @@ class TemplateURLServiceSyncTest : public testing::Test { ...@@ -157,12 +157,12 @@ class TemplateURLServiceSyncTest : public testing::Test {
virtual void SetUp() OVERRIDE; virtual void SetUp() OVERRIDE;
virtual void TearDown() 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 // For readability, we redefine an accessor for Model A for use in tests that
// involve syncing two models. // 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(); } 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(); } TestChangeProcessor* processor() { return sync_processor_.get(); }
scoped_ptr<syncer::SyncChangeProcessor> PassProcessor(); scoped_ptr<syncer::SyncChangeProcessor> PassProcessor();
scoped_ptr<syncer::SyncErrorFactory> CreateAndPassSyncErrorFactory(); scoped_ptr<syncer::SyncErrorFactory> CreateAndPassSyncErrorFactory();
...@@ -210,7 +210,7 @@ class TemplateURLServiceSyncTest : public testing::Test { ...@@ -210,7 +210,7 @@ class TemplateURLServiceSyncTest : public testing::Test {
protected: protected:
// We keep two TemplateURLServices to test syncing between them. // 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<TestingProfile> profile_b_;
scoped_ptr<TemplateURLService> model_b_; scoped_ptr<TemplateURLService> model_b_;
...@@ -228,11 +228,11 @@ TemplateURLServiceSyncTest::TemplateURLServiceSyncTest() ...@@ -228,11 +228,11 @@ TemplateURLServiceSyncTest::TemplateURLServiceSyncTest()
void TemplateURLServiceSyncTest::SetUp() { void TemplateURLServiceSyncTest::SetUp() {
DefaultSearchManager::SetFallbackSearchEnginesDisabledForTesting(true); DefaultSearchManager::SetFallbackSearchEnginesDisabledForTesting(true);
test_util_a_.SetUp(); test_util_a_.reset(new TemplateURLServiceTestUtil);
// Use ChangeToLoadState() instead of VerifyLoad() so we don't actually pull // 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 // in the prepopulate data, which the sync tests don't care about (and would
// just foul them up). // just foul them up).
test_util_a_.ChangeModelToLoadState(); test_util_a_->ChangeModelToLoadState();
profile_b_.reset(new TestingProfile); profile_b_.reset(new TestingProfile);
TemplateURLServiceFactory::GetInstance()-> TemplateURLServiceFactory::GetInstance()->
RegisterUserPrefsOnBrowserContextForTest(profile_b_.get()); RegisterUserPrefsOnBrowserContextForTest(profile_b_.get());
...@@ -244,7 +244,7 @@ void TemplateURLServiceSyncTest::SetUp() { ...@@ -244,7 +244,7 @@ void TemplateURLServiceSyncTest::SetUp() {
} }
void TemplateURLServiceSyncTest::TearDown() { void TemplateURLServiceSyncTest::TearDown() {
test_util_a_.TearDown(); test_util_a_.reset();
DefaultSearchManager::SetFallbackSearchEnginesDisabledForTesting(false); DefaultSearchManager::SetFallbackSearchEnginesDisabledForTesting(false);
} }
...@@ -1661,7 +1661,7 @@ TEST_F(TemplateURLServiceSyncTest, SyncWithManagedDefaultSearch) { ...@@ -1661,7 +1661,7 @@ TEST_F(TemplateURLServiceSyncTest, SyncWithManagedDefaultSearch) {
const char kAlternateURL[] = const char kAlternateURL[] =
"http://manageddefault.com/search#t={searchTerms}"; "http://manageddefault.com/search#t={searchTerms}";
const char kSearchTermsReplacementKey[] = "espv"; const char kSearchTermsReplacementKey[] = "espv";
test_util_a_.SetManagedDefaultSearchPreferences(true, kName, kName, test_util_a_->SetManagedDefaultSearchPreferences(true, kName, kName,
kSearchURL, std::string(), kIconURL, kEncodings, kAlternateURL, kSearchURL, std::string(), kIconURL, kEncodings, kAlternateURL,
kSearchTermsReplacementKey); kSearchTermsReplacementKey);
const TemplateURL* dsp_turl = model()->GetDefaultSearchProvider(); const TemplateURL* dsp_turl = model()->GetDefaultSearchProvider();
...@@ -1692,7 +1692,7 @@ TEST_F(TemplateURLServiceSyncTest, SyncWithManagedDefaultSearch) { ...@@ -1692,7 +1692,7 @@ TEST_F(TemplateURLServiceSyncTest, SyncWithManagedDefaultSearch) {
// from Sync. // from Sync.
const TemplateURL* expected_default = const TemplateURL* expected_default =
model()->GetTemplateURLForGUID("newdefault"); model()->GetTemplateURLForGUID("newdefault");
test_util_a_.RemoveManagedDefaultSearchPreferences(); test_util_a_->RemoveManagedDefaultSearchPreferences();
EXPECT_EQ(expected_default, model()->GetDefaultSearchProvider()); EXPECT_EQ(expected_default, model()->GetDefaultSearchProvider());
} }
...@@ -1846,7 +1846,7 @@ TEST_F(TemplateURLServiceSyncTest, PreSyncUpdates) { ...@@ -1846,7 +1846,7 @@ TEST_F(TemplateURLServiceSyncTest, PreSyncUpdates) {
// Merge the prepopulate search engines. // Merge the prepopulate search engines.
base::Time pre_merge_time = base::Time::Now(); base::Time pre_merge_time = base::Time::Now();
base::RunLoop().RunUntilIdle(); 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 // The newly added search engine should have been safely merged, with an
// updated time. // updated time.
...@@ -1895,7 +1895,7 @@ TEST_F(TemplateURLServiceSyncTest, PreSyncUpdates) { ...@@ -1895,7 +1895,7 @@ TEST_F(TemplateURLServiceSyncTest, PreSyncUpdates) {
TEST_F(TemplateURLServiceSyncTest, SyncBaseURLs) { TEST_F(TemplateURLServiceSyncTest, SyncBaseURLs) {
// Verify that bringing in a remote TemplateURL that uses Google base URLs // 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. // 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; syncer::SyncDataList initial_data;
scoped_ptr<TemplateURL> turl(CreateTestTemplateURL( scoped_ptr<TemplateURL> turl(CreateTestTemplateURL(
ASCIIToUTF16("google.co.uk"), "{google:baseURL}search?q={searchTerms}", ASCIIToUTF16("google.co.uk"), "{google:baseURL}search?q={searchTerms}",
...@@ -1920,7 +1920,7 @@ TEST_F(TemplateURLServiceSyncTest, SyncBaseURLs) { ...@@ -1920,7 +1920,7 @@ TEST_F(TemplateURLServiceSyncTest, SyncBaseURLs) {
// A local change to the Google base URL should update the keyword and // A local change to the Google base URL should update the keyword and
// generate a sync change. // 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(ASCIIToUTF16("google.co.in"), synced_turl->keyword());
EXPECT_EQ(1U, processor()->change_list_size()); EXPECT_EQ(1U, processor()->change_list_size());
ASSERT_TRUE(processor()->contains_guid("guid")); ASSERT_TRUE(processor()->contains_guid("guid"));
......
...@@ -9,13 +9,13 @@ ...@@ -9,13 +9,13 @@
#include "base/strings/string_split.h" #include "base/strings/string_split.h"
#include "base/threading/thread.h" #include "base/threading/thread.h"
#include "chrome/browser/search_engines/chrome_template_url_service_client.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/search_engines/ui_thread_search_terms_data.h"
#include "chrome/browser/webdata/web_data_service_factory.h" #include "chrome/browser/webdata/web_data_service_factory.h"
#include "chrome/test/base/testing_pref_service_syncable.h" #include "chrome/test/base/testing_pref_service_syncable.h"
#include "chrome/test/base/testing_profile.h" #include "chrome/test/base/testing_profile.h"
#include "components/google/core/browser/google_url_tracker.h" #include "components/google/core/browser/google_url_tracker.h"
#include "components/search_engines/default_search_manager.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 "components/search_engines/template_url_service.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -27,10 +27,6 @@ ...@@ -27,10 +27,6 @@
// SetKeywordSearchTermsForURL. // SetKeywordSearchTermsForURL.
class TestingTemplateURLService : public TemplateURLService { class TestingTemplateURLService : public TemplateURLService {
public: public:
static KeyedService* Build(content::BrowserContext* profile) {
return new TestingTemplateURLService(static_cast<Profile*>(profile));
}
explicit TestingTemplateURLService(Profile* profile) explicit TestingTemplateURLService(Profile* profile)
: TemplateURLService( : TemplateURLService(
profile->GetPrefs(), profile->GetPrefs(),
...@@ -62,37 +58,47 @@ class TestingTemplateURLService : public TemplateURLService { ...@@ -62,37 +58,47 @@ class TestingTemplateURLService : public TemplateURLService {
DISALLOW_COPY_AND_ASSIGN(TestingTemplateURLService); DISALLOW_COPY_AND_ASSIGN(TestingTemplateURLService);
}; };
// TemplateURLServiceTestUtilBase ---------------------------------------------
TemplateURLServiceTestUtilBase::TemplateURLServiceTestUtilBase() TemplateURLServiceTestUtil::TemplateURLServiceTestUtil()
: changed_count_(0) { : 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() { TemplateURLServiceTestUtil::~TemplateURLServiceTestUtil() {
profile()->CreateWebDataService(); ClearModel();
profile_.reset();
UIThreadSearchTermsData::SetGoogleBaseURL(std::string());
TemplateURLService* service = static_cast<TemplateURLService*>( // Flush the message loop to make application verifiers happy.
TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( base::RunLoop().RunUntilIdle();
profile(), TestingTemplateURLService::Build));
service->AddObserver(this);
} }
void TemplateURLServiceTestUtilBase::OnTemplateURLServiceChanged() { void TemplateURLServiceTestUtil::OnTemplateURLServiceChanged() {
changed_count_++; changed_count_++;
} }
int TemplateURLServiceTestUtilBase::GetObserverCount() { int TemplateURLServiceTestUtil::GetObserverCount() {
return changed_count_; return changed_count_;
} }
void TemplateURLServiceTestUtilBase::ResetObserverCount() { void TemplateURLServiceTestUtil::ResetObserverCount() {
changed_count_ = 0; changed_count_ = 0;
} }
void TemplateURLServiceTestUtilBase::VerifyLoad() { void TemplateURLServiceTestUtil::VerifyLoad() {
ASSERT_FALSE(model()->loaded()); ASSERT_FALSE(model()->loaded());
model()->Load(); model()->Load();
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
...@@ -100,7 +106,7 @@ void TemplateURLServiceTestUtilBase::VerifyLoad() { ...@@ -100,7 +106,7 @@ void TemplateURLServiceTestUtilBase::VerifyLoad() {
ResetObserverCount(); ResetObserverCount();
} }
void TemplateURLServiceTestUtilBase::ChangeModelToLoadState() { void TemplateURLServiceTestUtil::ChangeModelToLoadState() {
model()->ChangeToLoadedState(); model()->ChangeToLoadedState();
// Initialize the web data service so that the database gets updated with // Initialize the web data service so that the database gets updated with
// any changes made. // any changes made.
...@@ -111,34 +117,33 @@ void TemplateURLServiceTestUtilBase::ChangeModelToLoadState() { ...@@ -111,34 +117,33 @@ void TemplateURLServiceTestUtilBase::ChangeModelToLoadState() {
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
} }
void TemplateURLServiceTestUtilBase::ClearModel() { void TemplateURLServiceTestUtil::ClearModel() {
TemplateURLServiceFactory::GetInstance()->SetTestingFactory( model_->Shutdown();
profile(), NULL); model_.reset();
} }
void TemplateURLServiceTestUtilBase::ResetModel(bool verify_load) { void TemplateURLServiceTestUtil::ResetModel(bool verify_load) {
TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( if (model_)
profile(), TestingTemplateURLService::Build); ClearModel();
model_.reset(new TestingTemplateURLService(profile()));
model()->AddObserver(this); model()->AddObserver(this);
changed_count_ = 0; changed_count_ = 0;
if (verify_load) if (verify_load)
VerifyLoad(); VerifyLoad();
} }
base::string16 TemplateURLServiceTestUtilBase::GetAndClearSearchTerm() { base::string16 TemplateURLServiceTestUtil::GetAndClearSearchTerm() {
return return model_->GetAndClearSearchTerm();
static_cast<TestingTemplateURLService*>(model())->GetAndClearSearchTerm();
} }
void TemplateURLServiceTestUtilBase::SetGoogleBaseURL( void TemplateURLServiceTestUtil::SetGoogleBaseURL(const GURL& base_url) {
const GURL& base_url) const {
DCHECK(base_url.is_valid()); DCHECK(base_url.is_valid());
UIThreadSearchTermsData data(profile()); UIThreadSearchTermsData data(profile());
UIThreadSearchTermsData::SetGoogleBaseURL(base_url.spec()); UIThreadSearchTermsData::SetGoogleBaseURL(base_url.spec());
TemplateURLServiceFactory::GetForProfile(profile())->GoogleBaseURLChanged(); model_->GoogleBaseURLChanged();
} }
void TemplateURLServiceTestUtilBase::SetManagedDefaultSearchPreferences( void TemplateURLServiceTestUtil::SetManagedDefaultSearchPreferences(
bool enabled, bool enabled,
const std::string& name, const std::string& name,
const std::string& keyword, const std::string& keyword,
...@@ -148,93 +153,17 @@ void TemplateURLServiceTestUtilBase::SetManagedDefaultSearchPreferences( ...@@ -148,93 +153,17 @@ void TemplateURLServiceTestUtilBase::SetManagedDefaultSearchPreferences(
const std::string& encodings, const std::string& encodings,
const std::string& alternate_url, const std::string& alternate_url,
const std::string& search_terms_replacement_key) { const std::string& search_terms_replacement_key) {
TestingPrefServiceSyncable* pref_service = profile()->GetTestingPrefService(); DefaultSearchPrefTestUtil::SetManagedPref(
scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue); profile()->GetTestingPrefService(),
if (!enabled) { enabled, name, keyword, search_url, suggest_url, icon_url, encodings,
value->SetBoolean(DefaultSearchManager::kDisabledByPolicy, true); alternate_url, search_terms_replacement_key);
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);
} }
TemplateURLService* TemplateURLServiceTestUtilBase::model() const { void TemplateURLServiceTestUtil::RemoveManagedDefaultSearchPreferences() {
return TemplateURLServiceFactory::GetForProfile(profile()); DefaultSearchPrefTestUtil::RemoveManagedPref(
} profile()->GetTestingPrefService());
// 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();
} }
TestingProfile* TemplateURLServiceTestUtil::profile() const { TemplateURLService* TemplateURLServiceTestUtil::model() {
return profile_.get(); return model_.get();
} }
...@@ -9,9 +9,7 @@ ...@@ -9,9 +9,7 @@
#include "base/basictypes.h" #include "base/basictypes.h"
#include "base/files/scoped_temp_dir.h" #include "base/files/scoped_temp_dir.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "components/search_engines/template_url_service_observer.h" #include "components/search_engines/template_url_service_observer.h"
#include "content/public/test/test_browser_thread_bundle.h" #include "content/public/test/test_browser_thread_bundle.h"
...@@ -20,16 +18,11 @@ class GURL; ...@@ -20,16 +18,11 @@ class GURL;
class TemplateURLService; class TemplateURLService;
class TestingProfile; class TestingProfile;
class TestingTemplateURLService; class TestingTemplateURLService;
class TestingProfile;
// TemplateURLServiceTestUtilBase contains basic API to ease testing of class TemplateURLServiceTestUtil : public TemplateURLServiceObserver {
// TemplateURLService. User should take care of the infrastructure separately.
class TemplateURLServiceTestUtilBase : public TemplateURLServiceObserver {
public: public:
TemplateURLServiceTestUtilBase(); TemplateURLServiceTestUtil();
virtual ~TemplateURLServiceTestUtilBase(); virtual ~TemplateURLServiceTestUtil();
void CreateTemplateUrlService();
// TemplateURLServiceObserver implemementation. // TemplateURLServiceObserver implemementation.
virtual void OnTemplateURLServiceChanged() OVERRIDE; virtual void OnTemplateURLServiceChanged() OVERRIDE;
...@@ -59,7 +52,7 @@ class TemplateURLServiceTestUtilBase : public TemplateURLServiceObserver { ...@@ -59,7 +52,7 @@ class TemplateURLServiceTestUtilBase : public TemplateURLServiceObserver {
base::string16 GetAndClearSearchTerm(); base::string16 GetAndClearSearchTerm();
// Set the google base url. |base_url| must be valid. // 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 // Set the managed preferences for the default search provider and trigger
// notification. If |alternate_url| is empty, uses an empty list of alternate // notification. If |alternate_url| is empty, uses an empty list of alternate
...@@ -80,41 +73,17 @@ class TemplateURLServiceTestUtilBase : public TemplateURLServiceObserver { ...@@ -80,41 +73,17 @@ class TemplateURLServiceTestUtilBase : public TemplateURLServiceObserver {
void RemoveManagedDefaultSearchPreferences(); void RemoveManagedDefaultSearchPreferences();
// Returns the TemplateURLService. // Returns the TemplateURLService.
TemplateURLService* model() const; TemplateURLService* model();
// 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();
// Returns the TestingProfile. // Returns the TestingProfile.
virtual TestingProfile* profile() const OVERRIDE; TestingProfile* profile() { return profile_.get(); }
private: private:
// Needed to make the DeleteOnUIThread trait of WebDataService work
// properly.
content::TestBrowserThreadBundle thread_bundle_; content::TestBrowserThreadBundle thread_bundle_;
scoped_ptr<TestingProfile> profile_; scoped_ptr<TestingProfile> profile_;
base::ScopedTempDir temp_dir_; base::ScopedTempDir temp_dir_;
int changed_count_;
scoped_ptr<TestingTemplateURLService> model_;
DISALLOW_COPY_AND_ASSIGN(TemplateURLServiceTestUtil); DISALLOW_COPY_AND_ASSIGN(TemplateURLServiceTestUtil);
}; };
......
...@@ -9,13 +9,14 @@ ...@@ -9,13 +9,14 @@
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/tracked_objects.h" #include "base/tracked_objects.h"
#include "chrome/browser/search_engines/template_url_service_factory.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/glue/search_engine_data_type_controller.h"
#include "chrome/browser/sync/profile_sync_components_factory_mock.h" #include "chrome/browser/sync/profile_sync_components_factory_mock.h"
#include "chrome/browser/sync/profile_sync_service_mock.h" #include "chrome/browser/sync/profile_sync_service_mock.h"
#include "chrome/test/base/profile_mock.h" #include "chrome/test/base/profile_mock.h"
#include "components/sync_driver/data_type_controller_mock.h" #include "components/sync_driver/data_type_controller_mock.h"
#include "components/sync_driver/fake_generic_change_processor.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 "sync/api/fake_syncable_service.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -30,18 +31,16 @@ namespace { ...@@ -30,18 +31,16 @@ namespace {
class SyncSearchEngineDataTypeControllerTest : public testing::Test { class SyncSearchEngineDataTypeControllerTest : public testing::Test {
public: public:
SyncSearchEngineDataTypeControllerTest() { } SyncSearchEngineDataTypeControllerTest() : test_util_(&profile_) { }
virtual void SetUp() { virtual void SetUp() {
test_util_.SetUp(); service_.reset(new ProfileSyncServiceMock(&profile_));
service_.reset(new ProfileSyncServiceMock(test_util_.profile()));
profile_sync_factory_.reset(new ProfileSyncComponentsFactoryMock()); 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. // This allows us to control the associated TemplateURLService.
search_engine_dtc_ = search_engine_dtc_ = new SearchEngineDataTypeController(
new SearchEngineDataTypeController( profile_sync_factory_.get(), &profile_,
profile_sync_factory_.get(), test_util_.profile(), DataTypeController::DisableTypeCallback());
DataTypeController::DisableTypeCallback());
} }
virtual void TearDown() { virtual void TearDown() {
...@@ -49,7 +48,6 @@ class SyncSearchEngineDataTypeControllerTest : public testing::Test { ...@@ -49,7 +48,6 @@ class SyncSearchEngineDataTypeControllerTest : public testing::Test {
syncable_service_.StopSyncing(syncer::SEARCH_ENGINES); syncable_service_.StopSyncing(syncer::SEARCH_ENGINES);
search_engine_dtc_ = NULL; search_engine_dtc_ = NULL;
service_.reset(); service_.reset();
test_util_.TearDown();
} }
protected: protected:
...@@ -78,10 +76,9 @@ class SyncSearchEngineDataTypeControllerTest : public testing::Test { ...@@ -78,10 +76,9 @@ class SyncSearchEngineDataTypeControllerTest : public testing::Test {
base::Unretained(&start_callback_))); base::Unretained(&start_callback_)));
} }
// This also manages a BrowserThread and MessageLoop for us. Note that this content::TestBrowserThreadBundle thread_bundle_;
// must be declared here as the destruction order of the BrowserThread TestingProfile profile_;
// matters - we could leak if this is declared below. TemplateURLServiceFactoryTestUtil test_util_;
TemplateURLServiceTestUtil test_util_;
scoped_refptr<SearchEngineDataTypeController> search_engine_dtc_; scoped_refptr<SearchEngineDataTypeController> search_engine_dtc_;
scoped_ptr<ProfileSyncComponentsFactoryMock> profile_sync_factory_; scoped_ptr<ProfileSyncComponentsFactoryMock> profile_sync_factory_;
scoped_ptr<ProfileSyncServiceMock> service_; scoped_ptr<ProfileSyncServiceMock> service_;
......
...@@ -6,12 +6,14 @@ ...@@ -6,12 +6,14 @@
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "chrome/browser/profiles/profile.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/keyword_editor_controller.h"
#include "chrome/browser/ui/search_engines/template_url_table_model.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 "chrome/test/base/testing_profile.h"
#include "components/search_engines/template_url.h" #include "components/search_engines/template_url.h"
#include "components/search_engines/template_url_service.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 "testing/gtest/include/gtest/gtest.h"
#include "ui/base/models/table_model_observer.h" #include "ui/base/models/table_model_observer.h"
...@@ -28,34 +30,33 @@ class KeywordEditorControllerTest : public testing::Test, ...@@ -28,34 +30,33 @@ class KeywordEditorControllerTest : public testing::Test,
public ui::TableModelObserver { public ui::TableModelObserver {
public: public:
KeywordEditorControllerTest() KeywordEditorControllerTest()
: simulate_load_failure_(false), : util_(&profile_),
simulate_load_failure_(false),
model_changed_count_(0), model_changed_count_(0),
items_changed_count_(0), items_changed_count_(0),
added_count_(0), added_count_(0),
removed_count_(0) {} removed_count_(0) {}
explicit KeywordEditorControllerTest(bool simulate_load_failure) explicit KeywordEditorControllerTest(bool simulate_load_failure)
: simulate_load_failure_(simulate_load_failure), : util_(&profile_),
simulate_load_failure_(simulate_load_failure),
model_changed_count_(0), model_changed_count_(0),
items_changed_count_(0), items_changed_count_(0),
added_count_(0), added_count_(0),
removed_count_(0) {} removed_count_(0) {}
virtual void SetUp() OVERRIDE { virtual void SetUp() OVERRIDE {
util_.SetUp();
if (simulate_load_failure_) if (simulate_load_failure_)
util_.model()->OnWebDataServiceRequestDone(0, NULL); util_.model()->OnWebDataServiceRequestDone(0, NULL);
else else
util_.ChangeModelToLoadState(); util_.VerifyLoad();
controller_.reset(new KeywordEditorController(util_.profile())); controller_.reset(new KeywordEditorController(&profile_));
controller_->table_model()->SetObserver(this); controller_->table_model()->SetObserver(this);
} }
virtual void TearDown() OVERRIDE { virtual void TearDown() OVERRIDE {
controller_.reset(); controller_.reset();
util_.TearDown();
} }
virtual void OnModelChanged() OVERRIDE { virtual void OnModelChanged() OVERRIDE {
...@@ -102,15 +103,17 @@ class KeywordEditorControllerTest : public testing::Test, ...@@ -102,15 +103,17 @@ class KeywordEditorControllerTest : public testing::Test,
TemplateURLTableModel* table_model() { return controller_->table_model(); } TemplateURLTableModel* table_model() { return controller_->table_model(); }
KeywordEditorController* controller() { return controller_.get(); } 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 items_changed_count() const { return items_changed_count_; }
int added_count() const { return added_count_; } int added_count() const { return added_count_; }
int removed_count() const { return removed_count_; } int removed_count() const { return removed_count_; }
private: private:
content::TestBrowserThreadBundle thread_bundle_;
TestingProfile profile_;
scoped_ptr<KeywordEditorController> controller_; scoped_ptr<KeywordEditorController> controller_;
TemplateURLServiceTestUtil util_; TemplateURLServiceFactoryTestUtil util_;
bool simulate_load_failure_; bool simulate_load_failure_;
int model_changed_count_; int model_changed_count_;
......
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
'../components/components.gyp:metrics_test_support', '../components/components.gyp:metrics_test_support',
'../components/components.gyp:password_manager_core_browser_test_support', '../components/components.gyp:password_manager_core_browser_test_support',
'../components/components.gyp:pref_registry_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:signin_core_browser_test_support',
'../components/components.gyp:sync_driver_test_support', '../components/components.gyp:sync_driver_test_support',
'../content/content.gyp:content_app_both', '../content/content.gyp:content_app_both',
...@@ -172,6 +173,8 @@ ...@@ -172,6 +173,8 @@
'browser/prefs/pref_service_mock_factory.h', 'browser/prefs/pref_service_mock_factory.h',
'browser/profile_resetter/profile_resetter_test_base.cc', 'browser/profile_resetter/profile_resetter_test_base.cc',
'browser/profile_resetter/profile_resetter_test_base.h', '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.cc',
'browser/search_engines/template_url_service_test_util.h', 'browser/search_engines/template_url_service_test_util.h',
'browser/sessions/session_service_test_helper.cc', 'browser/sessions/session_service_test_helper.cc',
......
...@@ -32,7 +32,6 @@ ...@@ -32,7 +32,6 @@
#include "chrome/browser/extensions/extension_action.h" #include "chrome/browser/extensions/extension_action.h"
#include "chrome/browser/history/history_service_factory.h" #include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/profiles/profile.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.h"
#include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog_queue.h" #include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog_queue.h"
#include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser.h"
......
...@@ -69,5 +69,20 @@ ...@@ -69,5 +69,20 @@
'search_engines/util.h', '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',
],
},
], ],
} }
// 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();
}
// 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_
...@@ -399,7 +399,7 @@ class TemplateURLService : public WebDataServiceConsumer, ...@@ -399,7 +399,7 @@ class TemplateURLService : public WebDataServiceConsumer,
FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, MergeInSyncTemplateURL); FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, MergeInSyncTemplateURL);
friend class InstantUnitTestBase; friend class InstantUnitTestBase;
friend class TemplateURLServiceTestUtilBase; friend class TemplateURLServiceTestUtil;
typedef std::map<base::string16, TemplateURL*> KeywordToTemplateMap; typedef std::map<base::string16, TemplateURL*> KeywordToTemplateMap;
typedef std::map<std::string, TemplateURL*> GUIDToTemplateMap; typedef std::map<std::string, TemplateURL*> GUIDToTemplateMap;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment