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