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 "chrome/test/base/testing_profile.h"
     15 #include "testing/gtest/include/gtest/gtest.h"
     16 
     17 namespace testing {
     18 
     19 class ProfileManager : public ::ProfileManager {
     20  public:
     21   explicit ProfileManager(const base::FilePath& user_data_dir)
     22       : ::ProfileManager(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* profile = new TestingProfile(
     61       profile_path,
     62       NULL,
     63       scoped_refptr<ExtensionSpecialStoragePolicy>(),
     64       prefs.Pass());
     65   profile_manager_->AddProfile(profile);  // Takes ownership.
     66 
     67   // Update the user metadata.
     68   ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache();
     69   size_t index = cache.GetIndexOfProfileWithPath(profile_path);
     70   cache.SetAvatarIconOfProfileAtIndex(index, avatar_id);
     71   cache.SetManagedUserIdOfProfileAtIndex(index, managed_user_id);
     72   // SetNameOfProfileAtIndex may reshuffle the list of profiles, so we do it
     73   // last.
     74   cache.SetNameOfProfileAtIndex(index, user_name);
     75 
     76   testing_profiles_.insert(std::make_pair(profile_name, profile));
     77 
     78   return profile;
     79 }
     80 
     81 TestingProfile* TestingProfileManager::CreateTestingProfile(
     82     const std::string& name) {
     83   DCHECK(called_set_up_);
     84   return CreateTestingProfile(name, scoped_ptr<PrefServiceSyncable>(),
     85                               UTF8ToUTF16(name), 0, std::string());
     86 }
     87 
     88 void TestingProfileManager::DeleteTestingProfile(const std::string& name) {
     89   DCHECK(called_set_up_);
     90 
     91   TestingProfilesMap::iterator it = testing_profiles_.find(name);
     92   DCHECK(it != testing_profiles_.end());
     93 
     94   TestingProfile* profile = it->second;
     95 
     96   ProfileInfoCache& cache = profile_manager_->GetProfileInfoCache();
     97   cache.DeleteProfileFromCache(profile->GetPath());
     98 
     99   profile_manager_->profiles_info_.erase(profile->GetPath());
    100 }
    101 
    102 void TestingProfileManager::DeleteProfileInfoCache() {
    103   profile_manager_->profile_info_cache_.reset(NULL);
    104 }
    105 
    106 void TestingProfileManager::SetLoggedIn(bool logged_in) {
    107   profile_manager_->logged_in_ = logged_in;
    108 }
    109 
    110 const base::FilePath& TestingProfileManager::profiles_dir() {
    111   DCHECK(called_set_up_);
    112   return profiles_dir_.path();
    113 }
    114 
    115 ProfileManager* TestingProfileManager::profile_manager() {
    116   DCHECK(called_set_up_);
    117   return profile_manager_;
    118 }
    119 
    120 ProfileInfoCache* TestingProfileManager::profile_info_cache() {
    121   DCHECK(called_set_up_);
    122   return &profile_manager_->GetProfileInfoCache();
    123 }
    124 
    125 void TestingProfileManager::SetUpInternal() {
    126   ASSERT_FALSE(browser_process_->profile_manager())
    127       << "ProfileManager already exists";
    128 
    129   // Set up the directory for profiles.
    130   ASSERT_TRUE(profiles_dir_.CreateUniqueTempDir());
    131 
    132   profile_manager_ = new testing::ProfileManager(profiles_dir_.path());
    133   browser_process_->SetProfileManager(profile_manager_);  // Takes ownership.
    134 
    135   called_set_up_ = true;
    136 }
    137