diff options
author | bauerb@chromium.org <bauerb@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-04-21 12:47:38 +0000 |
---|---|---|
committer | bauerb@chromium.org <bauerb@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-04-21 12:47:38 +0000 |
commit | 7deb6a02fdebb5d7228fbde9818db456bac9e308 (patch) | |
tree | 75c8085c12cb7cfd6eac7433615f4904ac6eca89 /chrome/browser | |
parent | 8aee776c2a59a338499ec91deff736c822067216 (diff) | |
download | chromium_src-7deb6a02fdebb5d7228fbde9818db456bac9e308.zip chromium_src-7deb6a02fdebb5d7228fbde9818db456bac9e308.tar.gz chromium_src-7deb6a02fdebb5d7228fbde9818db456bac9e308.tar.bz2 |
Add a ScopedTestingLocalState for unit tests which need Local State.
BUG=79691
TEST=unit tests work
Review URL: http://codereview.chromium.org/6873037
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@82478 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
6 files changed, 50 insertions, 123 deletions
diff --git a/chrome/browser/extensions/apps_promo_unittest.cc b/chrome/browser/extensions/apps_promo_unittest.cc index 3816b78..d6c3bac 100644 --- a/chrome/browser/extensions/apps_promo_unittest.cc +++ b/chrome/browser/extensions/apps_promo_unittest.cc @@ -8,7 +8,7 @@ #include "chrome/browser/ui/webui/shown_sections_handler.h" #include "chrome/common/extensions/extension.h" #include "chrome/common/pref_names.h" -#include "chrome/test/testing_browser_process.h" +#include "chrome/test/testing_browser_process_test.h" #include "chrome/test/testing_pref_service.h" #include "googleurl/src/gurl.h" #include "testing/gtest/include/gtest/gtest.h" @@ -23,47 +23,30 @@ const char kPromoExpire[] = "No thanks."; } // namespace -class ExtensionAppsPromo : public testing::Test { +class ExtensionAppsPromo : public TestingBrowserProcessTest { public: - TestingPrefService* local_state() { return &local_state_; } TestingPrefService* prefs() { return &prefs_; } - AppsPromo* apps_promo() { return apps_promo_; } + AppsPromo* apps_promo() { return &apps_promo_; } protected: explicit ExtensionAppsPromo(); - virtual ~ExtensionAppsPromo(); // testing::Test virtual void SetUp(); - virtual void TearDown(); private: - TestingPrefService local_state_; TestingPrefService prefs_; - AppsPromo* apps_promo_; + ScopedTestingLocalState local_state_; + AppsPromo apps_promo_; }; -ExtensionAppsPromo::ExtensionAppsPromo() : - apps_promo_(new AppsPromo(&prefs_)) { -} - -ExtensionAppsPromo::~ExtensionAppsPromo() { - delete apps_promo_; +ExtensionAppsPromo::ExtensionAppsPromo() + : local_state_(testing_browser_process_.get()), + apps_promo_(&prefs_) { } void ExtensionAppsPromo::SetUp() { - browser::RegisterLocalState(&local_state_); browser::RegisterUserPrefs(&prefs_); - - TestingBrowserProcess* testing_browser_process = - static_cast<TestingBrowserProcess*>(g_browser_process); - testing_browser_process->SetPrefService(&local_state_); -} - -void ExtensionAppsPromo::TearDown() { - TestingBrowserProcess* testing_browser_process = - static_cast<TestingBrowserProcess*>(g_browser_process); - testing_browser_process->SetPrefService(NULL); } // TODO(dpolukhin): On Chrome OS all apps are installed via external extensions, diff --git a/chrome/browser/google/google_url_tracker_unittest.cc b/chrome/browser/google/google_url_tracker_unittest.cc index 86f959d..ede4e4b 100644 --- a/chrome/browser/google/google_url_tracker_unittest.cc +++ b/chrome/browser/google/google_url_tracker_unittest.cc @@ -12,7 +12,7 @@ #include "chrome/common/net/test_url_fetcher_factory.h" #include "chrome/common/net/url_fetcher.h" #include "chrome/common/pref_names.h" -#include "chrome/test/testing_browser_process.h" +#include "chrome/test/testing_browser_process_test.h" #include "chrome/test/testing_pref_service.h" #include "content/browser/browser_thread.h" #include "content/common/notification_service.h" @@ -99,7 +99,7 @@ InfoBarDelegate* CreateTestInfobar( // GoogleURLTrackerTest ------------------------------------------------------- -class GoogleURLTrackerTest : public testing::Test { +class GoogleURLTrackerTest : public TestingBrowserProcessTest { protected: GoogleURLTrackerTest(); virtual ~GoogleURLTrackerTest(); @@ -130,10 +130,10 @@ class GoogleURLTrackerTest : public testing::Test { scoped_ptr<TestNotificationObserver> observer_; private: - MessageLoop* message_loop_; - BrowserThread* io_thread_; + MessageLoop message_loop_; + BrowserThread io_thread_; scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; - TestingPrefService local_state_; + ScopedTestingLocalState local_state_; TestURLFetcherFactory fetcher_factory_; NotificationRegistrar registrar_; @@ -141,25 +141,20 @@ class GoogleURLTrackerTest : public testing::Test { GoogleURLTrackerTest::GoogleURLTrackerTest() : observer_(new TestNotificationObserver), - message_loop_(NULL), - io_thread_(NULL) { + message_loop_(MessageLoop::TYPE_IO), + io_thread_(BrowserThread::IO, &message_loop_), + local_state_(testing_browser_process_.get()) { } GoogleURLTrackerTest::~GoogleURLTrackerTest() { } void GoogleURLTrackerTest::SetUp() { - message_loop_ = new MessageLoop(MessageLoop::TYPE_IO); - io_thread_ = new BrowserThread(BrowserThread::IO, message_loop_); network_change_notifier_.reset(net::NetworkChangeNotifier::CreateMock()); - browser::RegisterLocalState(&local_state_); - TestingBrowserProcess* testing_browser_process = - static_cast<TestingBrowserProcess*>(g_browser_process); - testing_browser_process->SetPrefService(&local_state_); GoogleURLTracker* tracker = new GoogleURLTracker; tracker->queue_wakeup_task_ = false; MessageLoop::current()->RunAllPending(); - testing_browser_process->SetGoogleURLTracker(tracker); + testing_browser_process_.get()->SetGoogleURLTracker(tracker); URLFetcher::set_factory(&fetcher_factory_); g_browser_process->google_url_tracker()->infobar_creator_ = @@ -168,13 +163,8 @@ void GoogleURLTrackerTest::SetUp() { void GoogleURLTrackerTest::TearDown() { URLFetcher::set_factory(NULL); - TestingBrowserProcess* testing_browser_process = - static_cast<TestingBrowserProcess*>(g_browser_process); - testing_browser_process->SetGoogleURLTracker(NULL); - testing_browser_process->SetPrefService(NULL); + testing_browser_process_.get()->SetGoogleURLTracker(NULL); network_change_notifier_.reset(); - delete io_thread_; - delete message_loop_; } TestURLFetcher* GoogleURLTrackerTest::GetFetcherByID(int expected_id) { diff --git a/chrome/browser/profiles/profile_manager_unittest.cc b/chrome/browser/profiles/profile_manager_unittest.cc index 7080055..4a4a4b8 100644 --- a/chrome/browser/profiles/profile_manager_unittest.cc +++ b/chrome/browser/profiles/profile_manager_unittest.cc @@ -14,7 +14,7 @@ #include "chrome/common/chrome_constants.h" #include "chrome/common/chrome_paths.h" #include "chrome/common/chrome_switches.h" -#include "chrome/test/testing_browser_process.h" +#include "chrome/test/testing_browser_process_test.h" #include "chrome/test/testing_pref_service.h" #include "content/browser/browser_thread.h" #include "content/common/notification_service.h" @@ -29,31 +29,22 @@ Profile* g_created_profile; } // namespace -class ProfileManagerTest : public testing::Test { +class ProfileManagerTest : public TestingBrowserProcessTest { protected: ProfileManagerTest() : ui_thread_(BrowserThread::UI, &message_loop_), file_thread_(BrowserThread::FILE, &message_loop_), - profile_manager_(new ProfileManagerWithoutInit) { + profile_manager_(new ProfileManagerWithoutInit), + local_state_(testing_browser_process_.get()) { } virtual void SetUp() { // Create a new temporary directory, and store the path ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); - - // Create a local_state PrefService. - browser::RegisterLocalState(&test_local_state_); - TestingBrowserProcess* testing_browser_process = - static_cast<TestingBrowserProcess*>(g_browser_process); - testing_browser_process->SetPrefService(&test_local_state_); } virtual void TearDown() { profile_manager_.reset(); - - TestingBrowserProcess* testing_browser_process = - static_cast<TestingBrowserProcess*>(g_browser_process); - testing_browser_process->SetPrefService(NULL); } class MockObserver : public ProfileManager::Observer { @@ -73,7 +64,7 @@ class ProfileManagerTest : public testing::Test { // Also will test profile deletion. scoped_ptr<ProfileManager> profile_manager_; - TestingPrefService test_local_state_; + ScopedTestingLocalState local_state_; }; TEST_F(ProfileManagerTest, GetProfile) { diff --git a/chrome/browser/translate/translate_manager_browsertest.cc b/chrome/browser/translate/translate_manager_browsertest.cc index afbd9ff..ff01361 100644 --- a/chrome/browser/translate/translate_manager_browsertest.cc +++ b/chrome/browser/translate/translate_manager_browsertest.cc @@ -196,6 +196,7 @@ class TranslateManagerTest : public TabContentsWrapperTestHarness, } NotificationObserverMock pref_observer_; + ScopedTestingBrowserProcess testing_browser_process_; private: NotificationRegistrar notification_registrar_; @@ -785,8 +786,7 @@ TEST_F(TranslateManagerTest, ServerReportsUnsupportedLanguage) { // Tests that no translate infobar is shown when Chrome is in a language that // the translate server does not support. TEST_F(TranslateManagerTest, UnsupportedUILanguage) { - TestingBrowserProcess* browser_process = - static_cast<TestingBrowserProcess*>(g_browser_process); + TestingBrowserProcess* browser_process = testing_browser_process_.get(); std::string original_lang = browser_process->GetApplicationLocale(); browser_process->SetApplicationLocale("qbz"); diff --git a/chrome/browser/ui/shell_dialogs_unittest.cc b/chrome/browser/ui/shell_dialogs_unittest.cc index 2269405..752d44b 100644 --- a/chrome/browser/ui/shell_dialogs_unittest.cc +++ b/chrome/browser/ui/shell_dialogs_unittest.cc @@ -74,22 +74,14 @@ TEST_F(FileSelectionDialogTest, ExpectAsynchronousListenerCall) { MessageLoopForUI message_loop; BrowserThread ui_thread(BrowserThread::UI, &message_loop); - TestingPrefService test_local_state; - browser::RegisterLocalState(&test_local_state); - static_cast<TestingBrowserProcess*>(g_browser_process)-> - SetPrefService(&test_local_state); + ScopedTestingLocalState local_state_(testing_browser_process_.get()); - FileSelectionUser* file_selection_user; + scoped_ptr<FileSelectionUser> file_selection_user(new FileSelectionUser()); // Disallow file-selection dialogs. - test_local_state.SetManagedPref( + local_state_.Get()->SetManagedPref( prefs::kAllowFileSelectionDialogs, Value::CreateBooleanValue(false)); - file_selection_user = new FileSelectionUser(); file_selection_user->StartFileSelection(); - delete file_selection_user; - - static_cast<TestingBrowserProcess*>(g_browser_process)-> - SetPrefService(NULL); } diff --git a/chrome/browser/web_resource/promo_resource_service_unittest.cc b/chrome/browser/web_resource/promo_resource_service_unittest.cc index 38f3a31..a9c0e22 100644 --- a/chrome/browser/web_resource/promo_resource_service_unittest.cc +++ b/chrome/browser/web_resource/promo_resource_service_unittest.cc @@ -11,27 +11,27 @@ #include "chrome/browser/prefs/pref_service.h" #include "chrome/browser/web_resource/promo_resource_service.h" #include "chrome/common/pref_names.h" -#include "chrome/test/testing_browser_process.h" +#include "chrome/test/testing_browser_process_test.h" #include "chrome/test/testing_pref_service.h" #include "chrome/test/testing_profile.h" #include "testing/gtest/include/gtest/gtest.h" -typedef testing::Test PromoResourceServiceTest; +class PromoResourceServiceTest : public TestingBrowserProcessTest { + public: + PromoResourceServiceTest() + : local_state_(testing_browser_process_.get()), + web_resource_service_(new PromoResourceService(&profile_)) { + } + + protected: + TestingProfile profile_; + ScopedTestingLocalState local_state_; + scoped_refptr<PromoResourceService> web_resource_service_; +}; // Verifies that custom dates read from a web resource server are written to // the preferences file. TEST_F(PromoResourceServiceTest, UnpackLogoSignal) { - // Set up a testing profile and create a promo resource service. - TestingProfile profile; - TestingPrefService local_state; - TestingBrowserProcess* testing_browser_process = - static_cast<TestingBrowserProcess*>(g_browser_process); - testing_browser_process->SetPrefService(&local_state); - browser::RegisterLocalState(&local_state); - - scoped_refptr<PromoResourceService> web_resource_service( - new PromoResourceService(&profile)); - // Set up start and end dates in a Dictionary as if parsed from the service. std::string json = "{ " " \"topic\": {" @@ -51,8 +51,8 @@ TEST_F(PromoResourceServiceTest, UnpackLogoSignal) { base::JSONReader::Read(json, false))); // Check that prefs are set correctly. - web_resource_service->UnpackLogoSignal(*(test_json.get())); - PrefService* prefs = profile.GetPrefs(); + web_resource_service_->UnpackLogoSignal(*(test_json.get())); + PrefService* prefs = profile_.GetPrefs(); ASSERT_TRUE(prefs != NULL); double logo_start = @@ -82,7 +82,7 @@ TEST_F(PromoResourceServiceTest, UnpackLogoSignal) { base::JSONReader::Read(json, false))); // Check that prefs are set correctly. - web_resource_service->UnpackLogoSignal(*(test_json.get())); + web_resource_service_->UnpackLogoSignal(*(test_json.get())); logo_start = prefs->GetDouble(prefs::kNTPCustomLogoStart); EXPECT_EQ(logo_start, 1267365600); // date changes to Feb 28 2010 1400 GMT. @@ -101,27 +101,14 @@ TEST_F(PromoResourceServiceTest, UnpackLogoSignal) { base::JSONReader::Read(json, false))); // Check that prefs are set correctly. - web_resource_service->UnpackLogoSignal(*(test_json.get())); + web_resource_service_->UnpackLogoSignal(*(test_json.get())); logo_start = prefs->GetDouble(prefs::kNTPCustomLogoStart); EXPECT_EQ(logo_start, 0); // date value reset to 0; logo_end = prefs->GetDouble(prefs::kNTPCustomLogoEnd); EXPECT_EQ(logo_end, 0); // date value reset to 0; - - testing_browser_process->SetPrefService(NULL); } TEST_F(PromoResourceServiceTest, UnpackPromoSignal) { - // Set up a testing profile and create a promo resource service. - TestingProfile profile; - TestingPrefService local_state; - TestingBrowserProcess* testing_browser_process = - static_cast<TestingBrowserProcess*>(g_browser_process); - testing_browser_process->SetPrefService(&local_state); - browser::RegisterLocalState(&local_state); - - scoped_refptr<PromoResourceService> web_resource_service( - new PromoResourceService(&profile)); - // Set up start and end dates and promo line in a Dictionary as if parsed // from the service. std::string json = "{ " @@ -147,8 +134,8 @@ TEST_F(PromoResourceServiceTest, UnpackPromoSignal) { MessageLoop loop; // Check that prefs are set correctly. - web_resource_service->UnpackPromoSignal(*(test_json.get())); - PrefService* prefs = profile.GetPrefs(); + web_resource_service_->UnpackPromoSignal(*(test_json.get())); + PrefService* prefs = profile_.GetPrefs(); ASSERT_TRUE(prefs != NULL); std::string promo_line = prefs->GetString(prefs::kNTPPromoLine); @@ -177,24 +164,10 @@ TEST_F(PromoResourceServiceTest, UnpackPromoSignal) { double promo_end = prefs->GetDouble(prefs::kNTPPromoEnd); EXPECT_EQ(promo_end, 1327971600); // unix epoch for Jan 31 2012 0100 GMT. - - testing_browser_process->SetPrefService(NULL); } TEST_F(PromoResourceServiceTest, UnpackWebStoreSignal) { - // Set up a testing profile and create a promo resource service. - TestingProfile profile; - TestingPrefService local_state; - TestingBrowserProcess* testing_browser_process = - static_cast<TestingBrowserProcess*>(g_browser_process); - testing_browser_process->SetPrefService(&local_state); - - browser::RegisterLocalState(&local_state); - - scoped_refptr<PromoResourceService> web_resource_service( - new PromoResourceService(&profile)); - - web_resource_service->set_channel("dev"); + web_resource_service_->set_channel("dev"); // Set up start and end dates and promo line in a Dictionary as if parsed // from the service. @@ -219,8 +192,8 @@ TEST_F(PromoResourceServiceTest, UnpackWebStoreSignal) { MessageLoop loop; // Check that prefs are set correctly. - web_resource_service->UnpackWebStoreSignal(*(test_json.get())); - PrefService* prefs = profile.GetPrefs(); + web_resource_service_->UnpackWebStoreSignal(*(test_json.get())); + PrefService* prefs = profile_.GetPrefs(); ASSERT_TRUE(prefs != NULL); EXPECT_EQ("341252", AppsPromo::GetPromoId()); @@ -228,8 +201,6 @@ TEST_F(PromoResourceServiceTest, UnpackWebStoreSignal) { EXPECT_EQ("The button label!", AppsPromo::GetPromoButtonText()); EXPECT_EQ(GURL("http://link.com"), AppsPromo::GetPromoLink()); EXPECT_EQ("No thanks, hide this.", AppsPromo::GetPromoExpireText()); - - testing_browser_process->SetPrefService(NULL); } TEST_F(PromoResourceServiceTest, IsBuildTargeted) { |