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