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 a guest TestingProfile from the profile manager. 74 void DeleteGuestProfile(); 75 76 // Deletes the cache instance. This is useful for testing that the cache is 77 // properly persisting data. 78 void DeleteProfileInfoCache(); 79 80 // Sets ProfileManager's logged_in state. This is only useful on ChromeOS. 81 void SetLoggedIn(bool logged_in); 82 83 // Helper accessors. 84 const base::FilePath& profiles_dir(); 85 ProfileManager* profile_manager(); 86 ProfileInfoCache* profile_info_cache(); 87 88 private: 89 typedef std::map<std::string, TestingProfile*> TestingProfilesMap; 90 91 // Does the actual ASSERT-checked SetUp work. This function cannot have a 92 // return value, so it sets the |called_set_up_| flag on success and that is 93 // returned in the public SetUp. 94 void SetUpInternal(); 95 96 // Whether SetUp() was called to put the object in a valid state. 97 bool called_set_up_; 98 99 // The directory in which new profiles are placed. 100 base::ScopedTempDir profiles_dir_; 101 102 // Weak reference to the browser process on which the ProfileManager is set. 103 TestingBrowserProcess* browser_process_; 104 105 // Local state in which all the profiles are registered. 106 ScopedTestingLocalState local_state_; 107 108 // Weak reference to the profile manager. 109 ProfileManager* profile_manager_; 110 111 // Map of profile_name to TestingProfile* from CreateTestingProfile(). 112 TestingProfilesMap testing_profiles_; 113 114 DISALLOW_COPY_AND_ASSIGN(TestingProfileManager); 115 }; 116 117 #endif // CHROME_TEST_BASE_TESTING_PROFILE_MANAGER_H_ 118