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 PrefServiceSyncable
;
20 class ProfileInfoCache
;
22 class TestingBrowserProcess
;
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.
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
{
34 explicit TestingProfileManager(TestingBrowserProcess
* browser_process
);
35 ~TestingProfileManager();
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
;
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
,
57 const std::string
& supervised_user_id
,
58 const TestingProfile::TestingFactories
& factories
);
60 // Small helper for creating testing profiles. Just forwards to above.
61 TestingProfile
* CreateTestingProfile(const std::string
& name
);
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();
70 // Creates a new system TestingProfile whose data lives in the system profile
71 // test environment directory, as specified by the profile manager.
72 // This profile will not be added to the ProfileInfoCache. This will
73 // register the TestingProfile with the profile subsystem as well.
74 // The subsystem owns the Profile and returns a weak pointer.
75 TestingProfile
* CreateSystemProfile();
77 // Deletes a TestingProfile from the profile subsystem.
78 void DeleteTestingProfile(const std::string
& profile_name
);
80 // Deletes all TestingProfiles from the profile subsystem, including guest
82 void DeleteAllTestingProfiles();
84 // Deletes a guest TestingProfile from the profile manager.
85 void DeleteGuestProfile();
87 // Deletes a system TestingProfile from the profile manager.
88 void DeleteSystemProfile();
90 // Deletes the cache instance. This is useful for testing that the cache is
91 // properly persisting data.
92 void DeleteProfileInfoCache();
94 // Sets ProfileManager's logged_in state. This is only useful on ChromeOS.
95 void SetLoggedIn(bool logged_in
);
97 // Sets the last used profile; also sets the active time to now.
98 void UpdateLastUser(Profile
* last_active
);
101 const base::FilePath
& profiles_dir();
102 ProfileManager
* profile_manager();
103 ProfileInfoCache
* profile_info_cache();
106 typedef std::map
<std::string
, TestingProfile
*> TestingProfilesMap
;
108 // Does the actual ASSERT-checked SetUp work. This function cannot have a
109 // return value, so it sets the |called_set_up_| flag on success and that is
110 // returned in the public SetUp.
111 void SetUpInternal();
113 // Whether SetUp() was called to put the object in a valid state.
116 // The directory in which new profiles are placed.
117 base::ScopedTempDir profiles_dir_
;
119 // Weak reference to the browser process on which the ProfileManager is set.
120 TestingBrowserProcess
* browser_process_
;
122 // Local state in which all the profiles are registered.
123 ScopedTestingLocalState local_state_
;
125 // Weak reference to the profile manager.
126 ProfileManager
* profile_manager_
;
128 // Map of profile_name to TestingProfile* from CreateTestingProfile().
129 TestingProfilesMap testing_profiles_
;
131 DISALLOW_COPY_AND_ASSIGN(TestingProfileManager
);
134 #endif // CHROME_TEST_BASE_TESTING_PROFILE_MANAGER_H_