- add sources.
[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/test/base/testing_browser_process.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 namespace testing {
18
19 class ProfileManager : public ::ProfileManagerWithoutInit {
20  public:
21   explicit ProfileManager(const base::FilePath& user_data_dir)
22       : ::ProfileManagerWithoutInit(user_data_dir) {}
23
24  protected:
25   virtual Profile* CreateProfileHelper(
26       const base::FilePath& file_path) OVERRIDE {
27     return new TestingProfile(file_path);
28   }
29 };
30
31 }  // namespace testing
32
33 TestingProfileManager::TestingProfileManager(TestingBrowserProcess* process)
34     : called_set_up_(false),
35       browser_process_(process),
36       local_state_(process) {
37 }
38
39 TestingProfileManager::~TestingProfileManager() {
40 }
41
42 bool TestingProfileManager::SetUp() {
43   SetUpInternal();
44   return called_set_up_;
45 }
46
47 TestingProfile* TestingProfileManager::CreateTestingProfile(
48     const std::string& profile_name,
49     scoped_ptr<PrefServiceSyncable> prefs,
50     const string16& user_name,
51     int avatar_id,
52     const std::string& managed_user_id) {
53   DCHECK(called_set_up_);
54
55   // Create a path for the profile based on the name.
56   base::FilePath profile_path(profiles_dir_.path());
57   profile_path = profile_path.AppendASCII(profile_name);
58
59   // Create the profile and register it.
60   TestingProfile::Builder builder;
61   builder.SetPath(profile_path);
62   builder.SetPrefService(prefs.Pass());
63   builder.SetManagedUserId(managed_user_id);
64
65   TestingProfile* profile = builder.Build().release();
66   profile_manager_->AddProfile(profile);  // Takes ownership.
67
68   // Update the user metadata.
69   ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache();
70   size_t index = cache.GetIndexOfProfileWithPath(profile_path);
71   cache.SetAvatarIconOfProfileAtIndex(index, avatar_id);
72   cache.SetManagedUserIdOfProfileAtIndex(index, managed_user_id);
73   // SetNameOfProfileAtIndex may reshuffle the list of profiles, so we do it
74   // last.
75   cache.SetNameOfProfileAtIndex(index, user_name);
76
77   testing_profiles_.insert(std::make_pair(profile_name, profile));
78
79   return profile;
80 }
81
82 TestingProfile* TestingProfileManager::CreateTestingProfile(
83     const std::string& name) {
84   DCHECK(called_set_up_);
85   return CreateTestingProfile(name, scoped_ptr<PrefServiceSyncable>(),
86                               UTF8ToUTF16(name), 0, std::string());
87 }
88
89 void TestingProfileManager::DeleteTestingProfile(const std::string& name) {
90   DCHECK(called_set_up_);
91
92   TestingProfilesMap::iterator it = testing_profiles_.find(name);
93   DCHECK(it != testing_profiles_.end());
94
95   TestingProfile* profile = it->second;
96
97   ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache();
98   cache.DeleteProfileFromCache(profile->GetPath());
99
100   profile_manager_->profiles_info_.erase(profile->GetPath());
101 }
102
103 void TestingProfileManager::DeleteProfileInfoCache() {
104   profile_manager_->profile_info_cache_.reset(NULL);
105 }
106
107 void TestingProfileManager::SetLoggedIn(bool logged_in) {
108   profile_manager_->logged_in_ = logged_in;
109 }
110
111 const base::FilePath& TestingProfileManager::profiles_dir() {
112   DCHECK(called_set_up_);
113   return profiles_dir_.path();
114 }
115
116 ProfileManager* TestingProfileManager::profile_manager() {
117   DCHECK(called_set_up_);
118   return profile_manager_;
119 }
120
121 ProfileInfoCache* TestingProfileManager::profile_info_cache() {
122   DCHECK(called_set_up_);
123   return &profile_manager_->GetProfileInfoCache();
124 }
125
126 void TestingProfileManager::SetUpInternal() {
127   ASSERT_FALSE(browser_process_->profile_manager())
128       << "ProfileManager already exists";
129
130   // Set up the directory for profiles.
131   ASSERT_TRUE(profiles_dir_.CreateUniqueTempDir());
132
133   profile_manager_ = new testing::ProfileManager(profiles_dir_.path());
134   browser_process_->SetProfileManager(profile_manager_);  // Takes ownership.
135
136   called_set_up_ = true;
137 }