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 #ifndef CHROME_TEST_BASE_TESTING_PROFILE_MANAGER_H_ 6 #define CHROME_TEST_BASE_TESTING_PROFILE_MANAGER_H_ 7 8 #include <map> 9 #include <memory> 10 #include <string> 11 12 #include "base/compiler_specific.h" 13 #include "base/files/file_path.h" 14 #include "base/strings/string16.h" 15 #include "base/test/scoped_path_override.h" 16 #include "chrome/test/base/scoped_testing_local_state.h" 17 #include "chrome/test/base/testing_profile.h" 18 #include "components/policy/core/common/policy_service.h" 19 20 class ProfileInfoCache; 21 class ProfileAttributesStorage; 22 class ProfileManager; 23 class TestingBrowserProcess; 24 25 namespace sync_preferences { 26 class PrefServiceSyncable; 27 } 28 29 // The TestingProfileManager is a TestingProfile factory for a multi-profile 30 // environment. It will bring up a full ProfileManager and attach it to the 31 // TestingBrowserProcess set up in your test. 32 // 33 // When a Profile is needed for testing, create it through the factory method 34 // below instead of creating it via |new TestingProfile|. It is not possible 35 // to register profiles created in that fashion with the ProfileManager. 36 class TestingProfileManager { 37 public: 38 explicit TestingProfileManager(TestingBrowserProcess* browser_process); 39 TestingProfileManager(TestingBrowserProcess* browser_process, 40 ScopedTestingLocalState* local_state); 41 TestingProfileManager(const TestingProfileManager&) = delete; 42 TestingProfileManager& operator=(const TestingProfileManager&) = delete; 43 ~TestingProfileManager(); 44 45 // This needs to be called in testing::Test::SetUp() to put the object in a 46 // valid state. Some work cannot be done in a constructor because it may 47 // call gtest asserts to verify setup. The result of this call can be used 48 // to ASSERT before doing more SetUp work in the test. 49 // |profiles_dir| is the path in which new directories would be placed. 50 // If empty, one will be created (and deleted upon destruction of |this|). 51 // If not empty, it will be used, but ownership is maintained by the caller. 52 bool SetUp(const base::FilePath& profiles_path = base::FilePath()) 53 WARN_UNUSED_RESULT; 54 55 // Creates a new TestingProfile whose data lives in a directory related to 56 // profile_name, which is a non-user-visible key for the test environment. 57 // |prefs| is the PrefService used by the profile. If it is NULL, the profile 58 // creates a PrefService on demand. 59 // |user_name|, |avatar_id| and |supervised_user_id| are passed along to the 60 // ProfileInfoCache and provide the user-visible profile metadata. This will 61 // register the TestingProfile with the profile subsystem as well. The 62 // subsystem owns the Profile and returns a weak pointer. 63 // |factories| contains BCKSs to use with the newly created profile. 64 TestingProfile* CreateTestingProfile( 65 const std::string& profile_name, 66 std::unique_ptr<sync_preferences::PrefServiceSyncable> prefs, 67 const base::string16& user_name, 68 int avatar_id, 69 const std::string& supervised_user_id, 70 TestingProfile::TestingFactories testing_factories, 71 base::Optional<bool> override_new_profile = base::nullopt, 72 base::Optional<std::unique_ptr<policy::PolicyService>> policy_service = 73 base::nullopt); 74 75 // Small helpers for creating testing profiles. Just forward to above. 76 TestingProfile* CreateTestingProfile(const std::string& name); 77 TestingProfile* CreateTestingProfile( 78 const std::string& name, 79 TestingProfile::TestingFactories testing_factories); 80 81 // Creates a new guest TestingProfile whose data lives in the guest profile 82 // test environment directory, as specified by the profile manager. 83 // This profile will not be added to the ProfileInfoCache. This will 84 // register the TestingProfile with the profile subsystem as well. 85 // The subsystem owns the Profile and returns a weak pointer. 86 TestingProfile* CreateGuestProfile(); 87 88 // Creates a new system TestingProfile whose data lives in the system profile 89 // test environment directory, as specified by the profile manager. 90 // This profile will not be added to the ProfileInfoCache. This will 91 // register the TestingProfile with the profile subsystem as well. 92 // The subsystem owns the Profile and returns a weak pointer. 93 TestingProfile* CreateSystemProfile(); 94 95 // Deletes a TestingProfile from the profile subsystem. 96 void DeleteTestingProfile(const std::string& profile_name); 97 98 // Deletes all TestingProfiles from the profile subsystem, including guest 99 // profiles. 100 void DeleteAllTestingProfiles(); 101 102 // Deletes a guest TestingProfile from the profile manager. 103 void DeleteGuestProfile(); 104 105 // Deletes a system TestingProfile from the profile manager. 106 void DeleteSystemProfile(); 107 108 // Deletes the cache instance. This is useful for testing that the cache is 109 // properly persisting data. 110 void DeleteProfileInfoCache(); 111 112 // Sets the last used profile; also sets the active time to now. 113 void UpdateLastUser(Profile* last_active); 114 115 // Helper accessors. 116 const base::FilePath& profiles_dir(); 117 ProfileManager* profile_manager(); 118 ProfileAttributesStorage* profile_attributes_storage(); local_state()119 ScopedTestingLocalState* local_state() { return local_state_; } 120 121 private: 122 friend class ProfileAttributesStorageTest; 123 friend class ProfileInfoCacheTest; 124 friend class ProfileNameVerifierObserver; 125 126 typedef std::map<std::string, TestingProfile*> TestingProfilesMap; 127 128 // Does the actual ASSERT-checked SetUp work. This function cannot have a 129 // return value, so it sets the |called_set_up_| flag on success and that is 130 // returned in the public SetUp. 131 void SetUpInternal(const base::FilePath& profiles_path); 132 133 // Deprecated helper accessor. Use profile_attributes_storage() instead. 134 ProfileInfoCache* profile_info_cache(); 135 136 // Whether SetUp() was called to put the object in a valid state. 137 bool called_set_up_; 138 139 // |profiles_path_| is the path under which new directories for the profiles 140 // will be placed. 141 base::FilePath profiles_path_; 142 143 // The user data directory in the path service is overriden because some 144 // functions, e.g. GetPathOfHighResAvatarAtIndex, get the user data directory 145 // by the path service instead of the profile manager. The override is scoped 146 // with the help of this variable. 147 std::unique_ptr<base::ScopedPathOverride> user_data_dir_override_; 148 149 // Weak reference to the browser process on which the ProfileManager is set. 150 TestingBrowserProcess* browser_process_; 151 152 // Local state in which all the profiles are registered. 153 ScopedTestingLocalState* local_state_; 154 155 // Owned local state for when it's not provided in the constructor. 156 std::unique_ptr<ScopedTestingLocalState> owned_local_state_; 157 158 // Weak reference to the profile manager. 159 ProfileManager* profile_manager_; 160 161 // Map of profile_name to TestingProfile* from CreateTestingProfile(). 162 TestingProfilesMap testing_profiles_; 163 }; 164 165 #endif // CHROME_TEST_BASE_TESTING_PROFILE_MANAGER_H_ 166