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.
5 #ifndef CHROME_TEST_BASE_TESTING_PROFILE_MANAGER_H_
6 #define CHROME_TEST_BASE_TESTING_PROFILE_MANAGER_H_
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"
19 class ProfileInfoCache
;
21 class TestingBrowserProcess
;
24 namespace syncable_prefs
{
25 class PrefServiceSyncable
;
28 // The TestingProfileManager is a TestingProfile factory for a multi-profile
29 // environment. It will bring up a full ProfileManager and attach it to the
30 // TestingBrowserProcess set up in your test.
32 // When a Profile is needed for testing, create it through the factory method
33 // below instead of creating it via |new TestingProfile|. It is not possible
34 // to register profiles created in that fashion with the ProfileManager.
35 class TestingProfileManager
{
37 explicit TestingProfileManager(TestingBrowserProcess
* browser_process
);
38 ~TestingProfileManager();
40 // This needs to be called in testing::Test::SetUp() to put the object in a
41 // valid state. Some work cannot be done in a constructor because it may
42 // call gtest asserts to verify setup. The result of this call can be used
43 // to ASSERT before doing more SetUp work in the test.
44 bool SetUp() WARN_UNUSED_RESULT
;
46 // Creates a new TestingProfile whose data lives in a directory related to
47 // profile_name, which is a non-user-visible key for the test environment.
48 // |prefs| is the PrefService used by the profile. If it is NULL, the profile
49 // creates a PrefService on demand.
50 // |user_name|, |avatar_id| and |supervised_user_id| are passed along to the
51 // ProfileInfoCache and provide the user-visible profile metadata. This will
52 // register the TestingProfile with the profile subsystem as well. The
53 // subsystem owns the Profile and returns a weak pointer.
54 // |factories| contains BCKSs to use with the newly created profile.
55 TestingProfile
* CreateTestingProfile(
56 const std::string
& profile_name
,
57 scoped_ptr
<syncable_prefs::PrefServiceSyncable
> prefs
,
58 const base::string16
& user_name
,
60 const std::string
& supervised_user_id
,
61 const TestingProfile::TestingFactories
& factories
);
63 // Small helper for creating testing profiles. Just forwards to above.
64 TestingProfile
* CreateTestingProfile(const std::string
& name
);
66 // Creates a new guest TestingProfile whose data lives in the guest profile
67 // test environment directory, as specified by the profile manager.
68 // This profile will not be added to the ProfileInfoCache. This will
69 // register the TestingProfile with the profile subsystem as well.
70 // The subsystem owns the Profile and returns a weak pointer.
71 TestingProfile
* CreateGuestProfile();
73 // Creates a new system TestingProfile whose data lives in the system profile
74 // test environment directory, as specified by the profile manager.
75 // This profile will not be added to the ProfileInfoCache. This will
76 // register the TestingProfile with the profile subsystem as well.
77 // The subsystem owns the Profile and returns a weak pointer.
78 TestingProfile
* CreateSystemProfile();
80 // Deletes a TestingProfile from the profile subsystem.
81 void DeleteTestingProfile(const std::string
& profile_name
);
83 // Deletes all TestingProfiles from the profile subsystem, including guest
85 void DeleteAllTestingProfiles();
87 // Deletes a guest TestingProfile from the profile manager.
88 void DeleteGuestProfile();
90 // Deletes a system TestingProfile from the profile manager.
91 void DeleteSystemProfile();
93 // Deletes the cache instance. This is useful for testing that the cache is
94 // properly persisting data.
95 void DeleteProfileInfoCache();
97 // Sets ProfileManager's logged_in state. This is only useful on ChromeOS.
98 void SetLoggedIn(bool logged_in
);
100 // Sets the last used profile; also sets the active time to now.
101 void UpdateLastUser(Profile
* last_active
);
104 const base::FilePath
& profiles_dir();
105 ProfileManager
* profile_manager();
106 ProfileInfoCache
* profile_info_cache();
109 typedef std::map
<std::string
, TestingProfile
*> TestingProfilesMap
;
111 // Does the actual ASSERT-checked SetUp work. This function cannot have a
112 // return value, so it sets the |called_set_up_| flag on success and that is
113 // returned in the public SetUp.
114 void SetUpInternal();
116 // Whether SetUp() was called to put the object in a valid state.
119 // The directory in which new profiles are placed.
120 base::ScopedTempDir profiles_dir_
;
122 // Weak reference to the browser process on which the ProfileManager is set.
123 TestingBrowserProcess
* browser_process_
;
125 // Local state in which all the profiles are registered.
126 ScopedTestingLocalState local_state_
;
128 // Weak reference to the profile manager.
129 ProfileManager
* profile_manager_
;
131 // Map of profile_name to TestingProfile* from CreateTestingProfile().
132 TestingProfilesMap testing_profiles_
;
134 DISALLOW_COPY_AND_ASSIGN(TestingProfileManager
);
137 #endif // CHROME_TEST_BASE_TESTING_PROFILE_MANAGER_H_