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 <string> 10 11 #include "base/compiler_specific.h" 12 #include "base/files/file_path.h" 13 #include "base/files/scoped_temp_dir.h" 14 #include "base/memory/scoped_ptr.h" 15 #include "base/strings/string16.h" 16 #include "chrome/test/base/scoped_testing_local_state.h" 17 #include "chrome/test/base/testing_profile.h" 18 19 class PrefServiceSyncable; 20 class ProfileInfoCache; 21 class ProfileManager; 22 class TestingBrowserProcess; 23 class TestingProfile; 24 25 // The TestingProfileManager is a TestingProfile factory for a multi-profile 26 // environment. It will bring up a full ProfileManager and attach it to the 27 // TestingBrowserProcess set up in your test. 28 // 29 // When a Profile is needed for testing, create it through the factory method 30 // below instead of creating it via |new TestingProfile|. It is not possible 31 // to register profiles created in that fashion with the ProfileManager. 32 class TestingProfileManager { 33 public: 34 explicit TestingProfileManager(TestingBrowserProcess* browser_process); 35 ~TestingProfileManager(); 36 37 // This needs to be called in testing::Test::SetUp() to put the object in a 38 // valid state. Some work cannot be done in a constructor because it may 39 // call gtest asserts to verify setup. The result of this call can be used 40 // to ASSERT before doing more SetUp work in the test. 41 bool SetUp() WARN_UNUSED_RESULT; 42 43 // Creates a new TestingProfile whose data lives in a directory related to 44 // profile_name, which is a non-user-visible key for the test environment. 45 // |prefs| is the PrefService used by the profile. If it is NULL, the profile 46 // creates a PrefService on demand. 47 // |user_name|, |avatar_id| and |supervised_user_id| are passed along to the 48 // ProfileInfoCache and provide the user-visible profile metadata. This will 49 // register the TestingProfile with the profile subsystem as well. The 50 // subsystem owns the Profile and returns a weak pointer. 51 // |factories| contains BCKSs to use with the newly created profile. 52 TestingProfile* CreateTestingProfile( 53 const std::string& profile_name, 54 scoped_ptr<PrefServiceSyncable> prefs, 55 const base::string16& user_name, 56 int avatar_id, 57 const std::string& supervised_user_id, 58 const TestingProfile::TestingFactories& factories); 59 60 // Small helper for creating testing profiles. Just forwards to above. 61 TestingProfile* CreateTestingProfile(const std::string& name); 62 63 // Creates a new guest TestingProfile whose data lives in the guest profile 64 // test environment directory, as specified by the profile manager. 65 // This profile will not be added to the ProfileInfoCache. This will 66 // register the TestingProfile with the profile subsystem as well. 67 // The subsystem owns the Profile and returns a weak pointer. 68 TestingProfile* CreateGuestProfile(); 69 70 // Deletes a TestingProfile from the profile subsystem. 71 void DeleteTestingProfile(const std::string& profile_name); 72 73 // Deletes all TestingProfiles from the profile subsystem, including guest 74 // profiles. 75 void DeleteAllTestingProfiles(); 76 77 // Deletes a guest TestingProfile from the profile manager. 78 void DeleteGuestProfile(); 79 80 // Deletes the cache instance. This is useful for testing that the cache is 81 // properly persisting data. 82 void DeleteProfileInfoCache(); 83 84 // Sets ProfileManager's logged_in state. This is only useful on ChromeOS. 85 void SetLoggedIn(bool logged_in); 86 87 // Helper accessors. 88 const base::FilePath& profiles_dir(); 89 ProfileManager* profile_manager(); 90 ProfileInfoCache* profile_info_cache(); 91 92 private: 93 typedef std::map<std::string, TestingProfile*> TestingProfilesMap; 94 95 // Does the actual ASSERT-checked SetUp work. This function cannot have a 96 // return value, so it sets the |called_set_up_| flag on success and that is 97 // returned in the public SetUp. 98 void SetUpInternal(); 99 100 // Whether SetUp() was called to put the object in a valid state. 101 bool called_set_up_; 102 103 // The directory in which new profiles are placed. 104 base::ScopedTempDir profiles_dir_; 105 106 // Weak reference to the browser process on which the ProfileManager is set. 107 TestingBrowserProcess* browser_process_; 108 109 // Local state in which all the profiles are registered. 110 ScopedTestingLocalState local_state_; 111 112 // Weak reference to the profile manager. 113 ProfileManager* profile_manager_; 114 115 // Map of profile_name to TestingProfile* from CreateTestingProfile(). 116 TestingProfilesMap testing_profiles_; 117 118 DISALLOW_COPY_AND_ASSIGN(TestingProfileManager); 119 }; 120 121 #endif // CHROME_TEST_BASE_TESTING_PROFILE_MANAGER_H_ 122