Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / test / base / testing_profile_manager.cc
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.
4
5 #include "chrome/test/base/testing_profile_manager.h"
6
7 #include "base/memory/ref_counted.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/prefs/pref_service_syncable.h"
10 #include "chrome/browser/profiles/profile_info_cache.h"
11 #include "chrome/browser/profiles/profile_manager.h"
12 #include "chrome/common/chrome_constants.h"
13 #include "chrome/test/base/testing_browser_process.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 #if defined(OS_CHROMEOS)
17 #include "chrome/browser/chromeos/profiles/profile_helper.h"
18 #endif
19
20 const char kGuestProfileName[] = "Guest";
21
22 namespace testing {
23
24 class ProfileManager : public ::ProfileManagerWithoutInit {
25  public:
26   explicit ProfileManager(const base::FilePath& user_data_dir)
27       : ::ProfileManagerWithoutInit(user_data_dir) {}
28
29  protected:
30   Profile* CreateProfileHelper(const base::FilePath& file_path) override {
31     return new TestingProfile(file_path);
32   }
33 };
34
35 }  // namespace testing
36
37 TestingProfileManager::TestingProfileManager(TestingBrowserProcess* process)
38     : called_set_up_(false),
39       browser_process_(process),
40       local_state_(process),
41       profile_manager_(NULL) {
42 }
43
44 TestingProfileManager::~TestingProfileManager() {
45   // Destroying this class also destroys the LocalState, so make sure the
46   // associated ProfileManager is also destroyed.
47   browser_process_->SetProfileManager(NULL);
48 }
49
50 bool TestingProfileManager::SetUp() {
51   SetUpInternal();
52   return called_set_up_;
53 }
54
55 TestingProfile* TestingProfileManager::CreateTestingProfile(
56     const std::string& profile_name,
57     scoped_ptr<PrefServiceSyncable> prefs,
58     const base::string16& user_name,
59     int avatar_id,
60     const std::string& supervised_user_id,
61     const TestingProfile::TestingFactories& factories) {
62   DCHECK(called_set_up_);
63
64   // Create a path for the profile based on the name.
65   base::FilePath profile_path(profiles_dir_.path());
66 #if defined(OS_CHROMEOS)
67   if (profile_name != chrome::kInitialProfile) {
68     profile_path =
69         profile_path.Append(chromeos::ProfileHelper::Get()->GetUserProfileDir(
70             chromeos::ProfileHelper::GetUserIdHashByUserIdForTesting(
71                 profile_name)));
72   } else {
73     profile_path = profile_path.AppendASCII(profile_name);
74   }
75 #else
76   profile_path = profile_path.AppendASCII(profile_name);
77 #endif
78
79   // Create the profile and register it.
80   TestingProfile::Builder builder;
81   builder.SetPath(profile_path);
82   builder.SetPrefService(prefs.Pass());
83   builder.SetSupervisedUserId(supervised_user_id);
84
85   for (TestingProfile::TestingFactories::const_iterator it = factories.begin();
86        it != factories.end(); ++it) {
87     builder.AddTestingFactory(it->first, it->second);
88   }
89
90   TestingProfile* profile = builder.Build().release();
91   profile->set_profile_name(profile_name);
92   profile_manager_->AddProfile(profile);  // Takes ownership.
93
94   // Update the user metadata.
95   ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache();
96   size_t index = cache.GetIndexOfProfileWithPath(profile_path);
97   cache.SetAvatarIconOfProfileAtIndex(index, avatar_id);
98   cache.SetSupervisedUserIdOfProfileAtIndex(index, supervised_user_id);
99   // SetNameOfProfileAtIndex may reshuffle the list of profiles, so we do it
100   // last.
101   cache.SetNameOfProfileAtIndex(index, user_name);
102
103   testing_profiles_.insert(std::make_pair(profile_name, profile));
104
105   return profile;
106 }
107
108 TestingProfile* TestingProfileManager::CreateTestingProfile(
109     const std::string& name) {
110   DCHECK(called_set_up_);
111   return CreateTestingProfile(name, scoped_ptr<PrefServiceSyncable>(),
112                               base::UTF8ToUTF16(name), 0, std::string(),
113                               TestingProfile::TestingFactories());
114 }
115
116 TestingProfile* TestingProfileManager::CreateGuestProfile() {
117   DCHECK(called_set_up_);
118
119   // Create the profile and register it.
120   TestingProfile::Builder builder;
121   builder.SetGuestSession();
122   builder.SetPath(ProfileManager::GetGuestProfilePath());
123
124   // Add the guest profile to the profile manager, but not to the info cache.
125   TestingProfile* profile = builder.Build().release();
126   profile->set_profile_name(kGuestProfileName);
127
128   // Set up a profile with an off the record profile.
129   TestingProfile::Builder().BuildIncognito(profile);
130
131   profile_manager_->AddProfile(profile);  // Takes ownership.
132   profile_manager_->SetGuestProfilePrefs(profile);
133
134   testing_profiles_.insert(std::make_pair(kGuestProfileName, profile));
135
136   return profile;
137 }
138
139 void TestingProfileManager::DeleteTestingProfile(const std::string& name) {
140   DCHECK(called_set_up_);
141
142   TestingProfilesMap::iterator it = testing_profiles_.find(name);
143   DCHECK(it != testing_profiles_.end());
144
145   TestingProfile* profile = it->second;
146
147   ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache();
148   cache.DeleteProfileFromCache(profile->GetPath());
149
150   profile_manager_->profiles_info_.erase(profile->GetPath());
151 }
152
153 void TestingProfileManager::DeleteAllTestingProfiles() {
154   for (TestingProfilesMap::iterator it = testing_profiles_.begin();
155        it != testing_profiles_.end(); ++it) {
156     TestingProfile* profile = it->second;
157     ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache();
158     cache.DeleteProfileFromCache(profile->GetPath());
159   }
160   testing_profiles_.clear();
161 }
162
163
164 void TestingProfileManager::DeleteGuestProfile() {
165   DCHECK(called_set_up_);
166
167   TestingProfilesMap::iterator it = testing_profiles_.find(kGuestProfileName);
168   DCHECK(it != testing_profiles_.end());
169
170   profile_manager_->profiles_info_.erase(ProfileManager::GetGuestProfilePath());
171 }
172
173 void TestingProfileManager::DeleteProfileInfoCache() {
174   profile_manager_->profile_info_cache_.reset(NULL);
175 }
176
177 void TestingProfileManager::SetLoggedIn(bool logged_in) {
178   profile_manager_->logged_in_ = logged_in;
179 }
180
181 const base::FilePath& TestingProfileManager::profiles_dir() {
182   DCHECK(called_set_up_);
183   return profiles_dir_.path();
184 }
185
186 ProfileManager* TestingProfileManager::profile_manager() {
187   DCHECK(called_set_up_);
188   return profile_manager_;
189 }
190
191 ProfileInfoCache* TestingProfileManager::profile_info_cache() {
192   DCHECK(called_set_up_);
193   return &profile_manager_->GetProfileInfoCache();
194 }
195
196 void TestingProfileManager::SetUpInternal() {
197   ASSERT_FALSE(browser_process_->profile_manager())
198       << "ProfileManager already exists";
199
200   // Set up the directory for profiles.
201   ASSERT_TRUE(profiles_dir_.CreateUniqueTempDir());
202
203   profile_manager_ = new testing::ProfileManager(profiles_dir_.path());
204   browser_process_->SetProfileManager(profile_manager_);  // Takes ownership.
205
206   called_set_up_ = true;
207 }