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