1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/test/base/testing_profile_manager.h"
7 #include "base/file_path.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/utf_string_conversions.h"
10 #include "chrome/browser/profiles/profile_info_cache.h"
11 #include "chrome/browser/profiles/profile_manager.h"
12 #include "chrome/test/base/testing_browser_process.h"
13 #include "chrome/test/base/testing_profile.h"
14 #include "testing/gtest/include/gtest/gtest.h"
18 class ProfileManager
: public ::ProfileManager
{
20 explicit ProfileManager(const FilePath
& user_data_dir
)
21 : ::ProfileManager(user_data_dir
) {}
24 virtual Profile
* CreateProfileHelper(const FilePath
& file_path
) OVERRIDE
{
25 return new TestingProfile(file_path
);
29 } // namespace testing
31 TestingProfileManager::TestingProfileManager(TestingBrowserProcess
* process
)
32 : called_set_up_(false),
33 browser_process_(process
),
34 local_state_(process
) {
37 TestingProfileManager::~TestingProfileManager() {
40 bool TestingProfileManager::SetUp() {
42 return called_set_up_
;
45 TestingProfile
* TestingProfileManager::CreateTestingProfile(
46 const std::string
& profile_name
,
47 const string16
& user_name
,
49 DCHECK(called_set_up_
);
51 // Create a path for the profile based on the name.
52 FilePath
profile_path(profiles_dir_
.path());
53 profile_path
= profile_path
.AppendASCII(profile_name
);
55 // Create the profile and register it.
56 TestingProfile
* profile
= new TestingProfile(profile_path
);
57 profile_manager_
->AddProfile(profile
); // Takes ownership.
59 // Update the user metadata.
60 ProfileInfoCache
& cache
= profile_manager_
->GetProfileInfoCache();
61 size_t index
= cache
.GetIndexOfProfileWithPath(profile_path
);
62 cache
.SetNameOfProfileAtIndex(index
, user_name
);
63 cache
.SetAvatarIconOfProfileAtIndex(index
, avatar_id
);
65 testing_profiles_
.insert(std::make_pair(profile_name
, profile
));
70 TestingProfile
* TestingProfileManager::CreateTestingProfile(
71 const std::string
& name
) {
72 DCHECK(called_set_up_
);
73 return CreateTestingProfile(name
, UTF8ToUTF16(name
), 0);
76 void TestingProfileManager::DeleteTestingProfile(const std::string
& name
) {
77 DCHECK(called_set_up_
);
79 TestingProfilesMap::iterator it
= testing_profiles_
.find(name
);
80 DCHECK(it
!= testing_profiles_
.end());
82 TestingProfile
* profile
= it
->second
;
84 ProfileInfoCache
& cache
= profile_manager_
->GetProfileInfoCache();
85 cache
.DeleteProfileFromCache(profile
->GetPath());
87 profile_manager_
->profiles_info_
.erase(profile
->GetPath());
90 void TestingProfileManager::DeleteProfileInfoCache() {
91 profile_manager_
->profile_info_cache_
.reset(NULL
);
94 void TestingProfileManager::SetLoggedIn(bool logged_in
) {
95 profile_manager_
->logged_in_
= logged_in
;
98 ProfileManager
* TestingProfileManager::profile_manager() {
99 DCHECK(called_set_up_
);
100 return profile_manager_
;
103 ProfileInfoCache
* TestingProfileManager::profile_info_cache() {
104 DCHECK(called_set_up_
);
105 return &profile_manager_
->GetProfileInfoCache();
108 void TestingProfileManager::SetUpInternal() {
109 ASSERT_FALSE(browser_process_
->profile_manager())
110 << "ProfileManager already exists";
112 // Set up the directory for profiles.
113 ASSERT_TRUE(profiles_dir_
.CreateUniqueTempDir());
115 profile_manager_
= new testing::ProfileManager(profiles_dir_
.path());
116 browser_process_
->SetProfileManager(profile_manager_
); // Takes ownership.
118 called_set_up_
= true;