diff options
author | rsesek@chromium.org <rsesek@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-31 17:03:48 +0000 |
---|---|---|
committer | rsesek@chromium.org <rsesek@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-31 17:03:48 +0000 |
commit | 49a2563a2fd3c7a337f60c7df5e9c86b8dea946d (patch) | |
tree | 38f93a24ade006a278debd0f9cd762b1495fce5f /chrome | |
parent | 101488068d95c5f2fd64fbb0b0f70b5de0c6bae3 (diff) | |
download | chromium_src-49a2563a2fd3c7a337f60c7df5e9c86b8dea946d.zip chromium_src-49a2563a2fd3c7a337f60c7df5e9c86b8dea946d.tar.gz chromium_src-49a2563a2fd3c7a337f60c7df5e9c86b8dea946d.tar.bz2 |
Revamp the multi-profiles testing infrastructure.
This creates TestingProfileManager to handle setting up the structures needed to
test multi-profiles in unit_tests. It requires there to be a TestingBrowserProcess
in place. This also adds a second constructor to TestingProfile for multi-profiles
testing.
BUG=none
TEST=unit_tests
Review URL: http://codereview.chromium.org/7748002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@98991 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/browser/browser_process_impl.cc | 4 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_event_router_forwarder_unittest.cc | 24 | ||||
-rw-r--r-- | chrome/browser/profiles/avatar_menu_model_unittest.cc | 74 | ||||
-rw-r--r-- | chrome/browser/profiles/fake_profile_info_interface.cc | 43 | ||||
-rw-r--r-- | chrome/browser/profiles/fake_profile_info_interface.h | 40 | ||||
-rw-r--r-- | chrome/browser/profiles/profile_manager.cc | 14 | ||||
-rw-r--r-- | chrome/browser/profiles/profile_manager.h | 11 | ||||
-rw-r--r-- | chrome/browser/profiles/profile_manager_unittest.cc | 2 | ||||
-rw-r--r-- | chrome/browser/tabs/tab_strip_model_unittest.cc | 11 | ||||
-rw-r--r-- | chrome/browser/ui/cocoa/browser/avatar_menu_bubble_controller_unittest.mm | 46 | ||||
-rw-r--r-- | chrome/chrome_tests.gypi | 4 | ||||
-rw-r--r-- | chrome/test/base/testing_profile.cc | 23 | ||||
-rw-r--r-- | chrome/test/base/testing_profile.h | 17 | ||||
-rw-r--r-- | chrome/test/base/testing_profile_manager.cc | 96 | ||||
-rw-r--r-- | chrome/test/base/testing_profile_manager.h | 88 |
15 files changed, 300 insertions, 197 deletions
diff --git a/chrome/browser/browser_process_impl.cc b/chrome/browser/browser_process_impl.cc index 24a06ee..c46580a 100644 --- a/chrome/browser/browser_process_impl.cc +++ b/chrome/browser/browser_process_impl.cc @@ -855,7 +855,9 @@ void BrowserProcessImpl::CreateProfileManager() { DCHECK(!created_profile_manager_ && profile_manager_.get() == NULL); created_profile_manager_ = true; - profile_manager_.reset(new ProfileManager()); + FilePath user_data_dir; + PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); + profile_manager_.reset(new ProfileManager(user_data_dir)); } void BrowserProcessImpl::CreateLocalState() { diff --git a/chrome/browser/extensions/extension_event_router_forwarder_unittest.cc b/chrome/browser/extensions/extension_event_router_forwarder_unittest.cc index beaac8f..4a785d6 100644 --- a/chrome/browser/extensions/extension_event_router_forwarder_unittest.cc +++ b/chrome/browser/extensions/extension_event_router_forwarder_unittest.cc @@ -10,6 +10,7 @@ #include "chrome/browser/profiles/profile_manager.h" #include "chrome/test/base/testing_browser_process.h" #include "chrome/test/base/testing_profile.h" +#include "chrome/test/base/testing_profile_manager.h" #include "content/browser/browser_thread.h" #include "googleurl/src/gurl.h" #include "testing/gmock/include/gmock/gmock.h" @@ -36,33 +37,27 @@ class ExtensionEventRouterForwarderTest : public testing::Test { protected: ExtensionEventRouterForwarderTest() : ui_thread_(BrowserThread::UI, &message_loop_), - io_thread_(BrowserThread::IO) { + io_thread_(BrowserThread::IO), + profile_manager_( + static_cast<TestingBrowserProcess*>(g_browser_process)) { #if defined(OS_MACOSX) base::SystemMonitor::AllocateSystemIOPorts(); #endif dummy.reset(new base::SystemMonitor); } - ~ExtensionEventRouterForwarderTest() { - } - virtual void SetUp() { + ASSERT_TRUE(profile_manager_.SetUp()); + // Inject a BrowserProcess with a ProfileManager. ASSERT_TRUE(io_thread_.Start()); - TestingBrowserProcess* browser_process = - static_cast<TestingBrowserProcess*>(g_browser_process); - browser_process->SetProfileManager(new ProfileManager); - - profile1_ = new TestingProfile(); - profile2_ = new TestingProfile(); - - browser_process->profile_manager()->RegisterProfile(profile1_, true); - browser_process->profile_manager()->RegisterProfile(profile2_, true); + profile1_ = profile_manager_.CreateTestingProfile("one"); + profile2_ = profile_manager_.CreateTestingProfile("two"); } TestingProfile* CreateIncognitoProfile(TestingProfile* base) { - TestingProfile* incognito = new TestingProfile(); + TestingProfile* incognito = new TestingProfile; // Owned by |base|. incognito->set_incognito(true); base->SetOffTheRecordProfile(incognito); return incognito; @@ -71,6 +66,7 @@ class ExtensionEventRouterForwarderTest : public testing::Test { MessageLoopForUI message_loop_; BrowserThread ui_thread_; BrowserThread io_thread_; + TestingProfileManager profile_manager_; scoped_ptr<base::SystemMonitor> dummy; // Profiles are weak pointers, owned by ProfileManager in |browser_process_|. TestingProfile* profile1_; diff --git a/chrome/browser/profiles/avatar_menu_model_unittest.cc b/chrome/browser/profiles/avatar_menu_model_unittest.cc index fa862d7..3d3ca9e 100644 --- a/chrome/browser/profiles/avatar_menu_model_unittest.cc +++ b/chrome/browser/profiles/avatar_menu_model_unittest.cc @@ -7,10 +7,9 @@ #include "base/string16.h" #include "base/utf_string_conversions.h" #include "chrome/browser/profiles/avatar_menu_model_observer.h" -#include "chrome/browser/profiles/fake_profile_info_interface.h" -#include "chrome/browser/profiles/profile_info_interface.h" -#include "chrome/common/chrome_notification_types.h" -#include "content/common/notification_service.h" +#include "chrome/browser/profiles/profile_info_cache.h" +#include "chrome/test/base/testing_browser_process.h" +#include "chrome/test/base/testing_profile_manager.h" #include "testing/gtest/include/gtest/gtest.h" namespace { @@ -33,91 +32,80 @@ class MockObserver : public AvatarMenuModelObserver { class AvatarMenuModelTest : public testing::Test { public: - FakeProfileInfo* cache() { - return &cache_; + AvatarMenuModelTest() + : manager_(static_cast<TestingBrowserProcess*>(g_browser_process)) { } - Browser* browser() { - return NULL; + virtual void SetUp() { + ASSERT_TRUE(manager_.SetUp()); } - const gfx::Image& GetTestImage() { - return FakeProfileInfo::GetTestImage(); - } + Browser* browser() { return NULL; } + + TestingProfileManager* manager() { return &manager_; } private: - FakeProfileInfo cache_; + TestingProfileManager manager_; }; TEST_F(AvatarMenuModelTest, InitialCreation) { - std::vector<AvatarMenuModel::Item*>* profiles = cache()->mock_profiles(); + string16 name1(ASCIIToUTF16("Test 1")); + string16 name2(ASCIIToUTF16("Test 2")); - AvatarMenuModel::Item profile1(0, GetTestImage()); - profile1.name = ASCIIToUTF16("Test 1"); - profiles->push_back(&profile1); - - AvatarMenuModel::Item profile2(1, GetTestImage()); - profile2.name = ASCIIToUTF16("Test 2"); - profiles->push_back(&profile2); + manager()->CreateTestingProfile("p1", name1, 0); + manager()->CreateTestingProfile("p2", name2, 0); MockObserver observer; EXPECT_EQ(0, observer.change_count()); - AvatarMenuModel model(cache(), &observer, browser()); + AvatarMenuModel model(manager()->profile_info_cache(), &observer, browser()); EXPECT_EQ(0, observer.change_count()); ASSERT_EQ(2U, model.GetNumberOfItems()); const AvatarMenuModel::Item& item1 = model.GetItemAt(0); EXPECT_EQ(0U, item1.model_index); - EXPECT_EQ(ASCIIToUTF16("Test 1"), item1.name); + EXPECT_EQ(name1, item1.name); const AvatarMenuModel::Item& item2 = model.GetItemAt(1); EXPECT_EQ(1U, item2.model_index); - EXPECT_EQ(ASCIIToUTF16("Test 2"), item2.name); + EXPECT_EQ(name2, item2.name); } TEST_F(AvatarMenuModelTest, ChangeOnNotify) { - std::vector<AvatarMenuModel::Item*>* profiles = cache()->mock_profiles(); - - AvatarMenuModel::Item profile1(0, GetTestImage()); - profile1.name = ASCIIToUTF16("Test 1"); - profiles->push_back(&profile1); + string16 name1(ASCIIToUTF16("Test 1")); + string16 name2(ASCIIToUTF16("Test 2")); - AvatarMenuModel::Item profile2(1, GetTestImage()); - profile2.name = ASCIIToUTF16("Test 2"); - profiles->push_back(&profile2); + manager()->CreateTestingProfile("p1", name1, 0); + manager()->CreateTestingProfile("p2", name2, 0); MockObserver observer; EXPECT_EQ(0, observer.change_count()); - AvatarMenuModel model(cache(), &observer, browser()); + AvatarMenuModel model(manager()->profile_info_cache(), &observer, browser()); EXPECT_EQ(0, observer.change_count()); EXPECT_EQ(2U, model.GetNumberOfItems()); - AvatarMenuModel::Item profile3(2, GetTestImage()); - profile3.name = ASCIIToUTF16("Test 3"); - profiles->insert(profiles->begin() + 1, &profile3); + string16 name3(ASCIIToUTF16("Test 3")); + manager()->CreateTestingProfile("p3", name3, 0); - NotificationService::current()->Notify( - chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, - NotificationService::AllSources(), - NotificationService::NoDetails()); - EXPECT_EQ(1, observer.change_count()); + // Three changes happened via the call to CreateTestingProfile: adding the + // profile to the cache, setting the user name, and changing the avatar. + EXPECT_EQ(3, observer.change_count()); ASSERT_EQ(3U, model.GetNumberOfItems()); const AvatarMenuModel::Item& item1 = model.GetItemAt(0); EXPECT_EQ(0U, item1.model_index); - EXPECT_EQ(ASCIIToUTF16("Test 1"), item1.name); + EXPECT_EQ(name1, item1.name); const AvatarMenuModel::Item& item2 = model.GetItemAt(1); EXPECT_EQ(1U, item2.model_index); - EXPECT_EQ(ASCIIToUTF16("Test 3"), item2.name); + EXPECT_EQ(name2, item2.name); const AvatarMenuModel::Item& item3 = model.GetItemAt(2); EXPECT_EQ(2U, item3.model_index); - EXPECT_EQ(ASCIIToUTF16("Test 2"), item3.name); + EXPECT_EQ(name3, item3.name); } } // namespace diff --git a/chrome/browser/profiles/fake_profile_info_interface.cc b/chrome/browser/profiles/fake_profile_info_interface.cc deleted file mode 100644 index 25466bc..0000000 --- a/chrome/browser/profiles/fake_profile_info_interface.cc +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright (c) 2011 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/profiles/fake_profile_info_interface.h" - -#include "grit/theme_resources.h" -#include "ui/base/resource/resource_bundle.h" - -FakeProfileInfo::FakeProfileInfo() {} -FakeProfileInfo::~FakeProfileInfo() {} - -std::vector<AvatarMenuModel::Item*>* FakeProfileInfo::mock_profiles() { - return &profiles_; -} - -// static -gfx::Image& FakeProfileInfo::GetTestImage() { - return ResourceBundle::GetSharedInstance().GetImageNamed( - IDR_PROFILE_AVATAR_0); -} - -size_t FakeProfileInfo::GetNumberOfProfiles() const { - return profiles_.size(); -} - -size_t FakeProfileInfo::GetIndexOfProfileWithPath( - const FilePath& profile_path) const { - return std::string::npos; -} - -string16 FakeProfileInfo::GetNameOfProfileAtIndex(size_t index) const { - return profiles_[index]->name; -} - -FilePath FakeProfileInfo::GetPathOfProfileAtIndex(size_t index) const { - return FilePath(); -} - -const gfx::Image& FakeProfileInfo::GetAvatarIconOfProfileAtIndex( - size_t index) const { - return profiles_[index]->icon; -} diff --git a/chrome/browser/profiles/fake_profile_info_interface.h b/chrome/browser/profiles/fake_profile_info_interface.h deleted file mode 100644 index 4686eb3..0000000 --- a/chrome/browser/profiles/fake_profile_info_interface.h +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright (c) 2011 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_PROFILES_FAKE_PROFILE_INFO_INTERFACE_H_ -#define CHROME_BROWSER_PROFILES_FAKE_PROFILE_INFO_INTERFACE_H_ - -#include <vector> - -#include "base/string16.h" -#include "base/utf_string_conversions.h" -#include "chrome/browser/profiles/avatar_menu_model.h" -#include "chrome/browser/profiles/avatar_menu_model_observer.h" -#include "chrome/browser/profiles/profile_info_interface.h" -#include "chrome/common/chrome_notification_types.h" -#include "ui/gfx/image/image.h" - -class FakeProfileInfo : public ProfileInfoInterface { - public: - FakeProfileInfo(); - virtual ~FakeProfileInfo(); - - std::vector<AvatarMenuModel::Item*>* mock_profiles(); - - static gfx::Image& GetTestImage(); - - // ProfileInfoInterface: - virtual size_t GetNumberOfProfiles() const OVERRIDE; - virtual size_t GetIndexOfProfileWithPath( - const FilePath& profile_path) const OVERRIDE; - virtual string16 GetNameOfProfileAtIndex(size_t index) const OVERRIDE; - virtual FilePath GetPathOfProfileAtIndex(size_t index) const OVERRIDE; - virtual const gfx::Image& GetAvatarIconOfProfileAtIndex( - size_t index) const OVERRIDE; - - private: - std::vector<AvatarMenuModel::Item*> profiles_; -}; - -#endif // CHROME_BROWSER_PROFILES_FAKE_PROFILE_INFO_INTERFACE_H_ diff --git a/chrome/browser/profiles/profile_manager.cc b/chrome/browser/profiles/profile_manager.cc index 9e77534..27a9e5d 100644 --- a/chrome/browser/profiles/profile_manager.cc +++ b/chrome/browser/profiles/profile_manager.cc @@ -103,8 +103,10 @@ Profile* ProfileManager::GetLastUsedProfile() { return profile_manager->GetLastUsedProfile(user_data_dir); } -ProfileManager::ProfileManager() : logged_in_(false), - will_import_(false) { +ProfileManager::ProfileManager(const FilePath& user_data_dir) + : user_data_dir_(user_data_dir), + logged_in_(false), + will_import_(false) { BrowserList::AddObserver(this); #if defined(OS_CHROMEOS) registrar_.Add( @@ -519,10 +521,8 @@ ProfileManager::GetSortedProfilesFromDirectoryMap() { ProfileInfoCache& ProfileManager::GetProfileInfoCache() { if (!profile_info_cache_.get()) { - FilePath user_data_dir; - PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); profile_info_cache_.reset(new ProfileInfoCache( - g_browser_process->local_state(), user_data_dir)); + g_browser_process->local_state(), user_data_dir_)); } return *profile_info_cache_.get(); } @@ -578,3 +578,7 @@ bool ProfileManager::IsMultipleProfilesEnabled() { #endif return CommandLine::ForCurrentProcess()->HasSwitch(switches::kMultiProfiles); } + +ProfileManagerWithoutInit::ProfileManagerWithoutInit( + const FilePath& user_data_dir) : ProfileManager(user_data_dir) { +} diff --git a/chrome/browser/profiles/profile_manager.h b/chrome/browser/profiles/profile_manager.h index 258f541..a47e257 100644 --- a/chrome/browser/profiles/profile_manager.h +++ b/chrome/browser/profiles/profile_manager.h @@ -23,7 +23,6 @@ #include "content/common/notification_observer.h" #include "content/common/notification_registrar.h" -class FilePath; class NewProfileLauncher; class ProfileInfoCache; @@ -58,7 +57,7 @@ class ProfileManager : public base::NonThreadSafe, public NotificationObserver, public Profile::Delegate { public: - ProfileManager(); + explicit ProfileManager(const FilePath& user_data_dir); virtual ~ProfileManager(); // Invokes SessionServiceFactory::ShutdownForProfile() for all profiles. @@ -179,7 +178,7 @@ class ProfileManager : public base::NonThreadSafe, virtual void DoFinalInit(Profile* profile, bool go_off_the_record); private: - friend class ExtensionEventRouterForwarderTest; + friend class TestingProfileManager; // This struct contains information about profiles which are being loaded or // were loaded. @@ -227,6 +226,9 @@ class ProfileManager : public base::NonThreadSafe, NotificationRegistrar registrar_; + // The path to the user data directory (DIR_USER_DATA). + const FilePath user_data_dir_; + // Indicates that a user has logged in and that the profile specified // in the --login-profile command line argument should be used as the // default. @@ -252,6 +254,9 @@ class ProfileManager : public base::NonThreadSafe, // Same as the ProfileManager, but doesn't initialize some services of the // profile. This one is useful in unittests. class ProfileManagerWithoutInit : public ProfileManager { + public: + explicit ProfileManagerWithoutInit(const FilePath& user_data_dir); + protected: virtual void DoFinalInit(Profile*, bool) {} }; diff --git a/chrome/browser/profiles/profile_manager_unittest.cc b/chrome/browser/profiles/profile_manager_unittest.cc index b2e1fec..979b942 100644 --- a/chrome/browser/profiles/profile_manager_unittest.cc +++ b/chrome/browser/profiles/profile_manager_unittest.cc @@ -44,7 +44,7 @@ class ProfileManagerTest : public testing::Test { db_thread_(BrowserThread::DB, &message_loop_), file_thread_(BrowserThread::FILE, &message_loop_), io_thread_(local_state_.Get(), NULL, extension_event_router_forwarder_), - profile_manager_(new ProfileManagerWithoutInit) { + profile_manager_(new ProfileManagerWithoutInit(temp_dir_.path())) { #if defined(OS_MACOSX) base::SystemMonitor::AllocateSystemIOPorts(); #endif diff --git a/chrome/browser/tabs/tab_strip_model_unittest.cc b/chrome/browser/tabs/tab_strip_model_unittest.cc index a6402c6..893217a 100644 --- a/chrome/browser/tabs/tab_strip_model_unittest.cc +++ b/chrome/browser/tabs/tab_strip_model_unittest.cc @@ -13,12 +13,10 @@ #include "base/string_number_conversions.h" #include "base/string_split.h" #include "base/string_util.h" -#include "base/system_monitor/system_monitor.h" #include "base/utf_string_conversions.h" #include "chrome/browser/defaults.h" #include "chrome/browser/extensions/extension_tab_helper.h" #include "chrome/browser/profiles/profile.h" -#include "chrome/browser/profiles/profile_manager.h" #include "chrome/browser/tabs/tab_strip_model.h" #include "chrome/browser/tabs/tab_strip_model_delegate.h" #include "chrome/browser/tabs/tab_strip_model_order_controller.h" @@ -154,10 +152,6 @@ class TabStripModelTest : public RenderViewHostTestHarness { TabStripModelTest() : RenderViewHostTestHarness(), browser_thread_(BrowserThread::UI, &message_loop_) { -#if defined(OS_MACOSX) - base::SystemMonitor::AllocateSystemIOPorts(); -#endif - system_monitor.reset(new base::SystemMonitor); } TabContentsWrapper* CreateTabContents() { @@ -273,11 +267,6 @@ class TabStripModelTest : public RenderViewHostTestHarness { std::wstring test_dir_; std::wstring profile_path_; std::map<TabContents*, int> foo_; - - // ProfileManager requires a base::SystemMonitor. - scoped_ptr<base::SystemMonitor> system_monitor; - - ProfileManager pm_; }; class MockTabStripModelObserver : public TabStripModelObserver { diff --git a/chrome/browser/ui/cocoa/browser/avatar_menu_bubble_controller_unittest.mm b/chrome/browser/ui/cocoa/browser/avatar_menu_bubble_controller_unittest.mm index 638a8ab..b95d89c 100644 --- a/chrome/browser/ui/cocoa/browser/avatar_menu_bubble_controller_unittest.mm +++ b/chrome/browser/ui/cocoa/browser/avatar_menu_bubble_controller_unittest.mm @@ -6,12 +6,14 @@ #include "base/memory/scoped_nsobject.h" #include "base/utf_string_conversions.h" -#include "chrome/browser/profiles/fake_profile_info_interface.h" +#include "chrome/browser/profiles/avatar_menu_model.h" +#include "chrome/browser/profiles/avatar_menu_model_observer.h" +#include "chrome/browser/profiles/profile_info_cache.h" #import "chrome/browser/ui/cocoa/browser_test_helper.h" #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" #import "chrome/browser/ui/cocoa/hyperlink_button_cell.h" -#include "chrome/common/chrome_notification_types.h" -#include "content/common/notification_service.h" +#include "chrome/test/base/testing_browser_process.h" +#include "chrome/test/base/testing_profile_manager.h" #include "testing/gtest_mac.h" class FakeBridge : public AvatarMenuModelObserver { @@ -21,21 +23,19 @@ class FakeBridge : public AvatarMenuModelObserver { class AvatarMenuBubbleControllerTest : public CocoaTest { public: - virtual void SetUp() { - info_.reset(new FakeProfileInfo); + AvatarMenuBubbleControllerTest() + : manager_(static_cast<TestingBrowserProcess*>(g_browser_process)) { + } - profile1_.reset( - new AvatarMenuModel::Item(0, FakeProfileInfo::GetTestImage())); - profile1_->name = ASCIIToUTF16("Test 1"); - info()->mock_profiles()->push_back(profile1_.get()); + virtual void SetUp() { + CocoaTest::SetUp(); + ASSERT_TRUE(manager_.SetUp()); - profile2_.reset( - new AvatarMenuModel::Item(1, FakeProfileInfo::GetTestImage())); - profile2_->name = ASCIIToUTF16("Test 2"); - info()->mock_profiles()->push_back(profile2_.get()); + manager_.CreateTestingProfile("test1", ASCIIToUTF16("Test 1"), 1); + manager_.CreateTestingProfile("test2", ASCIIToUTF16("Test 2"), 0); bridge_ = new FakeBridge; - model_ = new AvatarMenuModel(info(), bridge(), NULL); + model_ = new AvatarMenuModel(manager_.profile_info_cache(), bridge(), NULL); NSRect frame = [test_window() frame]; NSPoint point = NSMakePoint(NSMidX(frame), NSMidY(frame)); @@ -46,18 +46,13 @@ class AvatarMenuBubbleControllerTest : public CocoaTest { anchoredAt:point]; } - FakeProfileInfo* info() { return info_.get(); } + TestingProfileManager* manager() { return &manager_; } AvatarMenuBubbleController* controller() { return controller_; } AvatarMenuModel* model() { return model_; } FakeBridge* bridge() { return bridge_; } private: - BrowserTestHelper browser_helper_; - - scoped_ptr<AvatarMenuModel::Item> profile1_; - scoped_ptr<AvatarMenuModel::Item> profile2_; - - scoped_ptr<FakeProfileInfo> info_; + TestingProfileManager manager_; // Weak; releases self. AvatarMenuBubbleController* controller_; @@ -108,14 +103,7 @@ TEST_F(AvatarMenuBubbleControllerTest, PerformLayout) { scoped_nsobject<NSMutableArray> oldItems([[controller() items] copy]); // Now create a new profile and notify the delegate. - AvatarMenuModel::Item profile3(2, FakeProfileInfo::GetTestImage()); - profile3.name = ASCIIToUTF16("Test 3"); - info()->mock_profiles()->push_back(&profile3); - - NotificationService::current()->Notify( - chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, - NotificationService::AllSources(), - NotificationService::NoDetails()); + manager()->CreateTestingProfile("test3", ASCIIToUTF16("Test 3"), 0); // Testing the bridge is not worth the effort... [controller() performLayout]; diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index e0bcea2..1cdbf8d 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -184,6 +184,8 @@ 'test/base/testing_pref_service.h', 'test/base/testing_profile.cc', 'test/base/testing_profile.h', + 'test/base/testing_profile_manager.cc', + 'test/base/testing_profile_manager.h', 'test/base/thread_observer_helper.h', 'test/base/ui_test_utils.cc', 'test/base/ui_test_utils.h', @@ -1592,8 +1594,6 @@ 'browser/process_info_snapshot_mac_unittest.cc', 'browser/process_singleton_mac_unittest.cc', 'browser/profiles/avatar_menu_model_unittest.cc', - 'browser/profiles/fake_profile_info_interface.cc', - 'browser/profiles/fake_profile_info_interface.h', 'browser/profiles/profile_dependency_manager_unittest.cc', 'browser/profiles/profile_info_cache_unittest.cc', 'browser/profiles/profile_manager_unittest.cc', diff --git a/chrome/test/base/testing_profile.cc b/chrome/test/base/testing_profile.cc index 4c03a3b..dcb57ef 100644 --- a/chrome/test/base/testing_profile.cc +++ b/chrome/test/base/testing_profile.cc @@ -127,8 +127,6 @@ TestingProfile::TestingProfile() incognito_(false), last_session_exited_cleanly_(true), profile_dependency_manager_(ProfileDependencyManager::GetInstance()) { - profile_dependency_manager_->CreateProfileServices(this, true); - if (!temp_dir_.CreateUniqueTempDir()) { LOG(ERROR) << "Failed to create unique temporary directory."; @@ -153,6 +151,24 @@ TestingProfile::TestingProfile() } } + profile_path_ = temp_dir_.path(); + + Init(); +} + +TestingProfile::TestingProfile(const FilePath& path) + : start_time_(Time::Now()), + testing_prefs_(NULL), + incognito_(false), + last_session_exited_cleanly_(true), + profile_path_(path), + profile_dependency_manager_(ProfileDependencyManager::GetInstance()) { + Init(); +} + +void TestingProfile::Init() { + profile_dependency_manager_->CreateProfileServices(this, true); + // Install profile keyed service factory hooks for dummy/test services DesktopNotificationServiceFactory::GetInstance()->SetTestingFactory( this, CreateTestDesktopNotificationService); @@ -347,8 +363,7 @@ ExtensionService* TestingProfile::CreateExtensionService( } FilePath TestingProfile::GetPath() { - DCHECK(temp_dir_.IsValid()); // TODO(phajdan.jr): do it better. - return temp_dir_.path(); + return profile_path_; } TestingPrefService* TestingProfile::GetTestingPrefService() { diff --git a/chrome/test/base/testing_profile.h b/chrome/test/base/testing_profile.h index 25519da..5057da5 100644 --- a/chrome/test/base/testing_profile.h +++ b/chrome/test/base/testing_profile.h @@ -58,8 +58,15 @@ class URLRequestContextGetter; class TestingProfile : public Profile { public: + // Default constructor that cannot be used with multi-profiles. TestingProfile(); + // Multi-profile aware constructor that takes the path to a directory managed + // for this profile. This constructor is meant to be used by + // TestingProfileManager::CreateTestingProfile. If you need to create multi- + // profile profiles, use that factory method instead of this directly. + explicit TestingProfile(const FilePath& path); + virtual ~TestingProfile(); // Creates the favicon service. Consequent calls would recreate the service. @@ -287,6 +294,9 @@ class TestingProfile : public Profile { TestingPrefService* testing_prefs_; private: + // Common initialization between the two constructors. + void Init(); + // Destroys favicon service if it has been created. void DestroyFaviconService(); @@ -377,8 +387,13 @@ class TestingProfile : public Profile { // The proxy prefs tracker. scoped_refptr<PrefProxyConfigTracker> pref_proxy_config_tracker_; - // We use a temporary directory to store testing profile data. + // We use a temporary directory to store testing profile data. In a multi- + // profile environment, this is invalid and the directory is managed by the + // TestingProfileManager. ScopedTempDir temp_dir_; + // The path to this profile. This will be valid in either of the two above + // cases. + FilePath profile_path_; scoped_ptr<ChromeURLDataManager> chrome_url_data_manager_; diff --git a/chrome/test/base/testing_profile_manager.cc b/chrome/test/base/testing_profile_manager.cc new file mode 100644 index 0000000..9376d57 --- /dev/null +++ b/chrome/test/base/testing_profile_manager.cc @@ -0,0 +1,96 @@ +// Copyright (c) 2011 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/test/base/testing_profile_manager.h" + +#include "base/file_path.h" +#include "base/memory/scoped_ptr.h" +#include "base/utf_string_conversions.h" +#include "chrome/browser/profiles/profile_info_cache.h" +#include "chrome/browser/profiles/profile_manager.h" +#include "chrome/test/base/testing_browser_process.h" +#include "chrome/test/base/testing_profile.h" +#include "testing/gtest/include/gtest/gtest.h" + +TestingProfileManager::TestingProfileManager(TestingBrowserProcess* process) + : called_set_up_(false), + browser_process_(process), + local_state_(process) { +} + +TestingProfileManager::~TestingProfileManager() { +} + +bool TestingProfileManager::SetUp() { + SetUpInternal(); + return called_set_up_; +} + +TestingProfile* TestingProfileManager::CreateTestingProfile( + const std::string& profile_name, + const string16& user_name, + int avatar_id) { + DCHECK(called_set_up_); + + // Create a path for the profile based on the name. + FilePath profile_path(profiles_dir_.path()); + profile_path = profile_path.AppendASCII(profile_name); + + // Create the profile and register it. + TestingProfile* profile = new TestingProfile(profile_path); + profile_manager_->AddProfile(profile); // Takes ownership. + + // Update the user metadata. + ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache(); + size_t index = cache.GetIndexOfProfileWithPath(profile_path); + cache.SetNameOfProfileAtIndex(index, user_name); + cache.SetAvatarIconOfProfileAtIndex(index, avatar_id); + + testing_profiles_.insert(std::make_pair(profile_name, profile)); + + return profile; +} + +TestingProfile* TestingProfileManager::CreateTestingProfile( + const std::string& name) { + DCHECK(called_set_up_); + return CreateTestingProfile(name, UTF8ToUTF16(name), 0); +} + +void TestingProfileManager::DeleteTestingProfile(const std::string& name) { + DCHECK(called_set_up_); + + TestingProfilesMap::iterator it = testing_profiles_.find(name); + DCHECK(it != testing_profiles_.end()); + + scoped_ptr<TestingProfile> profile(it->second); + + profile_manager_->profiles_info_.erase(profile->GetPath()); + + ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache(); + cache.DeleteProfileFromCache(profile->GetPath()); +} + +ProfileManager* TestingProfileManager::profile_manager() { + DCHECK(called_set_up_); + return profile_manager_; +} + +ProfileInfoCache* TestingProfileManager::profile_info_cache() { + DCHECK(called_set_up_); + return &profile_manager_->GetProfileInfoCache(); +} + +void TestingProfileManager::SetUpInternal() { + ASSERT_FALSE(browser_process_->profile_manager()) + << "ProfileManager already exists"; + + // Set up the directory for profiles. + ASSERT_TRUE(profiles_dir_.CreateUniqueTempDir()); + + profile_manager_ = new ProfileManager(profiles_dir_.path()); + browser_process_->SetProfileManager(profile_manager_); // Takes ownership. + + called_set_up_ = true; +} diff --git a/chrome/test/base/testing_profile_manager.h b/chrome/test/base/testing_profile_manager.h new file mode 100644 index 0000000..ae6a0ed --- /dev/null +++ b/chrome/test/base/testing_profile_manager.h @@ -0,0 +1,88 @@ +// Copyright (c) 2011 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_TEST_BASE_TESTING_PROFILE_MANAGER_H_ +#define CHROME_TEST_BASE_TESTING_PROFILE_MANAGER_H_ + +#include <map> +#include <string> + +#include "base/compiler_specific.h" +#include "base/scoped_temp_dir.h" +#include "base/string16.h" +#include "chrome/test/base/testing_pref_service.h" + +class ProfileInfoCache; +class ProfileManager; +class TestingBrowserProcess; +class TestingProfile; + +// The TestingProfileManager is a TestingProfile factory for a multi-profile +// environment. It will bring up a full ProfileManager and attach it to the +// TestingBrowserProcess set up in your test. +// +// When a Profile is needed for testing, create it through the factory method +// below instead of creating it via |new TestingProfile|. It is not possible +// to register profiles created in that fashion with the ProfileManager. +class TestingProfileManager { + public: + explicit TestingProfileManager(TestingBrowserProcess* browser_process); + ~TestingProfileManager(); + + // This needs to be called in testing::Test::SetUp() to put the object in a + // valid state. Some work cannot be done in a constructor because it may + // call gtest asserts to verify setup. The result of this call can be used + // to ASSERT before doing more SetUp work in the test. + bool SetUp() WARN_UNUSED_RESULT; + + // Creates a new TestingProfile whose data lives in a directory related to + // profile_name, which is a non-user-visible key for the test environment. + // |user_name| and |avatar_id| are passed along to the ProfileInfoCache and + // provide the user-visible profile metadata. This will register the + // TestingProfile with the profile subsystem as well. The subsystem owns the + // Profile and returns a weak pointer. + TestingProfile* CreateTestingProfile(const std::string& profile_name, + const string16& user_name, + int avatar_id); + + // Small helper for creating testing profiles. Just forwards to above. + TestingProfile* CreateTestingProfile(const std::string& name); + + // Deletes a TestingProfile from the profile subsystem. + void DeleteTestingProfile(const std::string& profile_name); + + // Helper accessors. + ProfileManager* profile_manager(); + ProfileInfoCache* profile_info_cache(); + + private: + typedef std::map<std::string, TestingProfile*> TestingProfilesMap; + + // Does the actual ASSERT-checked SetUp work. This function cannot have a + // return value, so it sets the |called_set_up_| flag on success and that is + // returned in the public SetUp. + void SetUpInternal(); + + // Whether SetUp() was called to put the object in a valid state. + bool called_set_up_; + + // The directory in which new profiles are placed. + ScopedTempDir profiles_dir_; + + // Weak reference to the browser process on which the ProfileManager is set. + TestingBrowserProcess* browser_process_; + + // Local state in which all the profiles are registered. + ScopedTestingLocalState local_state_; + + // Weak reference to the profile manager. + ProfileManager* profile_manager_; + + // Map of profile_name to TestingProfile* from CreateTestingProfile(). + TestingProfilesMap testing_profiles_; + + DISALLOW_COPY_AND_ASSIGN(TestingProfileManager); +}; + +#endif // CHROME_TEST_BASE_TESTING_PROFILE_MANAGER_H_ |