OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_TEST_BASE_TESTING_PROFILE_H_ | 5 #ifndef CHROME_TEST_BASE_TESTING_PROFILE_H_ |
6 #define CHROME_TEST_BASE_TESTING_PROFILE_H_ | 6 #define CHROME_TEST_BASE_TESTING_PROFILE_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 // platforms but must be different on ChromeOS because a logged-in user cannot | 51 // platforms but must be different on ChromeOS because a logged-in user cannot |
52 // use "Default" as profile directory. | 52 // use "Default" as profile directory. |
53 // Browser- and UI tests should always use this to get to the user's profile | 53 // Browser- and UI tests should always use this to get to the user's profile |
54 // directory. Unit-tests, though, should use |kInitialProfile|, which is | 54 // directory. Unit-tests, though, should use |kInitialProfile|, which is |
55 // always "Default", because they are runnining without logged-in user. | 55 // always "Default", because they are runnining without logged-in user. |
56 static const char kTestUserProfileDir[]; | 56 static const char kTestUserProfileDir[]; |
57 | 57 |
58 // Default constructor that cannot be used with multi-profiles. | 58 // Default constructor that cannot be used with multi-profiles. |
59 TestingProfile(); | 59 TestingProfile(); |
60 | 60 |
61 // Helper class for building an instance of TestingProfile (allows injecting | |
62 // mocks for various services prior to profile initialization). | |
63 // TODO(atwilson): Remove non-default constructors and various setters in | |
64 // favor of using the Builder API. | |
65 class Builder { | |
66 public: | |
67 Builder(); | |
68 ~Builder(); | |
69 | |
70 // Sets a Delegate to be called back when the Profile is fully initialized. | |
71 // This causes the final initialization to be performed via a task so the | |
72 // caller must run a MessageLoop. Caller maintains ownership of the Delegate | |
73 // and must manage its lifetime so it continues to exist until profile | |
74 // initialization is complete. | |
75 void SetDelegate(Delegate* delegate); | |
76 | |
77 // Sets the ExtensionSpecialStoragePolicy to be returned by | |
78 // GetExtensionSpecialStoragePolicy(). | |
79 void SetExtensionSpecialStoragePolicy( | |
80 scoped_refptr<ExtensionSpecialStoragePolicy> policy); | |
81 | |
82 // Sets the path to the directory to be used to hold profile data. | |
83 void SetPath(const FilePath& path); | |
84 | |
85 // Sets the PrefService to be used by this profile. | |
86 void SetPrefService(scoped_ptr<PrefService> prefs); | |
87 | |
88 // Sets the UserCloudPolicyManager to be used by this profile. | |
89 void SetUserCloudPolicyManager( | |
90 scoped_ptr<policy::UserCloudPolicyManager> manager); | |
91 | |
92 // Creates the TestingProfile using previously-set settings. | |
93 scoped_ptr<TestingProfile> Build(); | |
94 | |
95 private: | |
96 // If true, Build() has already been called. | |
97 bool build_called_; | |
98 | |
99 // Various staging variables where values are held until Build() is invoked. | |
100 scoped_ptr<policy::UserCloudPolicyManager> user_cloud_policy_manager_; | |
101 scoped_ptr<PrefService> pref_service_; | |
102 scoped_refptr<ExtensionSpecialStoragePolicy> extension_policy_; | |
103 FilePath path_; | |
104 Delegate* delegate_; | |
105 | |
106 DISALLOW_COPY_AND_ASSIGN(Builder); | |
107 }; | |
108 | |
109 // Multi-profile aware constructor that takes the path to a directory managed | 61 // Multi-profile aware constructor that takes the path to a directory managed |
110 // for this profile. This constructor is meant to be used by | 62 // for this profile. This constructor is meant to be used by |
111 // TestingProfileManager::CreateTestingProfile. If you need to create multi- | 63 // TestingProfileManager::CreateTestingProfile. If you need to create multi- |
112 // profile profiles, use that factory method instead of this directly. | 64 // profile profiles, use that factory method instead of this directly. |
113 // Exception: if you need to create multi-profile profiles for testing the | 65 // Exception: if you need to create multi-profile profiles for testing the |
114 // ProfileManager, then use the constructor below instead. | 66 // ProfileManager, then use the constructor below instead. |
115 explicit TestingProfile(const FilePath& path); | 67 explicit TestingProfile(const FilePath& path); |
116 | 68 |
117 // Multi-profile aware constructor that takes the path to a directory managed | 69 // Multi-profile aware constructor that takes the path to a directory managed |
118 // for this profile and a delegate. This constructor is meant to be used | 70 // for this profile and a delegate. This constructor is meant to be used |
119 // for unittesting the ProfileManager. | 71 // for unittesting the ProfileManager. |
120 TestingProfile(const FilePath& path, Delegate* delegate); | 72 TestingProfile(const FilePath& path, Delegate* delegate); |
121 | 73 |
122 // Full constructor allowing the setting of all possible instance data. | |
123 // Callers should use Builder::Build() instead of invoking this constructor. | |
124 TestingProfile(const FilePath& path, | |
125 Delegate* delegate, | |
126 scoped_refptr<ExtensionSpecialStoragePolicy> extension_policy, | |
127 scoped_ptr<PrefService> prefs, | |
128 scoped_ptr<policy::UserCloudPolicyManager> manager); | |
129 | |
130 virtual ~TestingProfile(); | 74 virtual ~TestingProfile(); |
131 | 75 |
132 // Creates the favicon service. Consequent calls would recreate the service. | 76 // Creates the favicon service. Consequent calls would recreate the service. |
133 void CreateFaviconService(); | 77 void CreateFaviconService(); |
134 | 78 |
135 // Creates the history service. If |delete_file| is true, the history file is | 79 // Creates the history service. If |delete_file| is true, the history file is |
136 // deleted first, then the HistoryService is created. As TestingProfile | 80 // deleted first, then the HistoryService is created. As TestingProfile |
137 // deletes the directory containing the files used by HistoryService, this | 81 // deletes the directory containing the files used by HistoryService, this |
138 // only matters if you're recreating the HistoryService. If |no_db| is true, | 82 // only matters if you're recreating the HistoryService. If |no_db| is true, |
139 // the history backend will fail to initialize its database; this is useful | 83 // the history backend will fail to initialize its database; this is useful |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 ExtensionSpecialStoragePolicy* extension_special_storage_policy); | 160 ExtensionSpecialStoragePolicy* extension_special_storage_policy); |
217 virtual ExtensionSpecialStoragePolicy* | 161 virtual ExtensionSpecialStoragePolicy* |
218 GetExtensionSpecialStoragePolicy() OVERRIDE; | 162 GetExtensionSpecialStoragePolicy() OVERRIDE; |
219 virtual FaviconService* GetFaviconService(ServiceAccessType access) OVERRIDE; | 163 virtual FaviconService* GetFaviconService(ServiceAccessType access) OVERRIDE; |
220 virtual HistoryService* GetHistoryService(ServiceAccessType access) OVERRIDE; | 164 virtual HistoryService* GetHistoryService(ServiceAccessType access) OVERRIDE; |
221 virtual HistoryService* GetHistoryServiceWithoutCreating() OVERRIDE; | 165 virtual HistoryService* GetHistoryServiceWithoutCreating() OVERRIDE; |
222 // The CookieMonster will only be returned if a Context has been created. Do | 166 // The CookieMonster will only be returned if a Context has been created. Do |
223 // this by calling CreateRequestContext(). See the note at GetRequestContext | 167 // this by calling CreateRequestContext(). See the note at GetRequestContext |
224 // for more information. | 168 // for more information. |
225 net::CookieMonster* GetCookieMonster(); | 169 net::CookieMonster* GetCookieMonster(); |
226 | |
227 virtual policy::UserCloudPolicyManager* GetUserCloudPolicyManager() OVERRIDE; | |
228 virtual policy::PolicyService* GetPolicyService() OVERRIDE; | 170 virtual policy::PolicyService* GetPolicyService() OVERRIDE; |
229 // Sets the profile's PrefService. If a pref service hasn't been explicitly | 171 // Sets the profile's PrefService. If a pref service hasn't been explicitly |
230 // set GetPrefs creates one, so normally you need not invoke this. If you need | 172 // set GetPrefs creates one, so normally you need not invoke this. If you need |
231 // to set a pref service you must invoke this before GetPrefs. | 173 // to set a pref service you must invoke this before GetPrefs. |
232 // TestingPrefService takes ownership of |prefs|. | 174 // TestingPrefService takes ownership of |prefs|. |
233 void SetPrefService(PrefService* prefs); | 175 void SetPrefService(PrefService* prefs); |
234 virtual PrefService* GetPrefs() OVERRIDE; | 176 virtual PrefService* GetPrefs() OVERRIDE; |
235 virtual history::TopSites* GetTopSites() OVERRIDE; | 177 virtual history::TopSites* GetTopSites() OVERRIDE; |
236 virtual history::TopSites* GetTopSitesWithoutCreating() OVERRIDE; | 178 virtual history::TopSites* GetTopSitesWithoutCreating() OVERRIDE; |
237 | 179 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 | 235 |
294 virtual PrefService* GetOffTheRecordPrefs() OVERRIDE; | 236 virtual PrefService* GetOffTheRecordPrefs() OVERRIDE; |
295 | 237 |
296 protected: | 238 protected: |
297 base::Time start_time_; | 239 base::Time start_time_; |
298 scoped_ptr<PrefService> prefs_; | 240 scoped_ptr<PrefService> prefs_; |
299 // ref only for right type, lifecycle is managed by prefs_ | 241 // ref only for right type, lifecycle is managed by prefs_ |
300 TestingPrefService* testing_prefs_; | 242 TestingPrefService* testing_prefs_; |
301 | 243 |
302 private: | 244 private: |
303 // Creates a temporary directory for use by this profile. | |
304 void CreateTempProfileDir(); | |
305 | |
306 // Common initialization between the two constructors. | 245 // Common initialization between the two constructors. |
307 void Init(); | 246 void Init(); |
308 | 247 |
309 // Finishes initialization when a profile is created asynchronously. | 248 // Finishes initialization when a profile is created asynchronously. |
310 void FinishInit(); | 249 void FinishInit(); |
311 | 250 |
312 // Destroys favicon service if it has been created. | 251 // Destroys favicon service if it has been created. |
313 void DestroyFaviconService(); | 252 void DestroyFaviconService(); |
314 | 253 |
315 // Creates a TestingPrefService and associates it with the TestingProfile. | 254 // Creates a TestingPrefService and associates it with the TestingProfile. |
(...skipping 27 matching lines...) Expand all Loading... |
343 | 282 |
344 FilePath last_selected_directory_; | 283 FilePath last_selected_directory_; |
345 scoped_refptr<history::TopSites> top_sites_; // For history and thumbnails. | 284 scoped_refptr<history::TopSites> top_sites_; // For history and thumbnails. |
346 | 285 |
347 scoped_refptr<ExtensionSpecialStoragePolicy> | 286 scoped_refptr<ExtensionSpecialStoragePolicy> |
348 extension_special_storage_policy_; | 287 extension_special_storage_policy_; |
349 | 288 |
350 // The proxy prefs tracker. | 289 // The proxy prefs tracker. |
351 scoped_ptr<PrefProxyConfigTracker> pref_proxy_config_tracker_; | 290 scoped_ptr<PrefProxyConfigTracker> pref_proxy_config_tracker_; |
352 | 291 |
353 // UserCloudPolicyManager returned by GetUserCloudPolicyManager(). | |
354 scoped_ptr<policy::UserCloudPolicyManager> user_cloud_policy_manager_; | |
355 | |
356 // We use a temporary directory to store testing profile data. In a multi- | 292 // We use a temporary directory to store testing profile data. In a multi- |
357 // profile environment, this is invalid and the directory is managed by the | 293 // profile environment, this is invalid and the directory is managed by the |
358 // TestingProfileManager. | 294 // TestingProfileManager. |
359 ScopedTempDir temp_dir_; | 295 ScopedTempDir temp_dir_; |
360 // The path to this profile. This will be valid in either of the two above | 296 // The path to this profile. This will be valid in either of the two above |
361 // cases. | 297 // cases. |
362 FilePath profile_path_; | 298 FilePath profile_path_; |
363 | 299 |
364 // We keep a weak pointer to the dependency manager we want to notify on our | 300 // We keep a weak pointer to the dependency manager we want to notify on our |
365 // death. Defaults to the Singleton implementation but overridable for | 301 // death. Defaults to the Singleton implementation but overridable for |
366 // testing. | 302 // testing. |
367 ProfileDependencyManager* profile_dependency_manager_; | 303 ProfileDependencyManager* profile_dependency_manager_; |
368 | 304 |
369 scoped_ptr<content::MockResourceContext> resource_context_; | 305 scoped_ptr<content::MockResourceContext> resource_context_; |
370 | 306 |
371 // Weak pointer to a delegate for indicating that a profile was created. | 307 // Weak pointer to a delegate for indicating that a profile was created. |
372 Delegate* delegate_; | 308 Delegate* delegate_; |
373 }; | 309 }; |
374 | 310 |
375 #endif // CHROME_TEST_BASE_TESTING_PROFILE_H_ | 311 #endif // CHROME_TEST_BASE_TESTING_PROFILE_H_ |
OLD | NEW |