testing_profile.h revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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_H_ 6#define CHROME_TEST_BASE_TESTING_PROFILE_H_ 7 8#include <string> 9 10#include "base/files/scoped_temp_dir.h" 11#include "base/memory/ref_counted.h" 12#include "base/memory/scoped_ptr.h" 13#include "chrome/browser/profiles/profile.h" 14 15namespace content { 16class MockResourceContext; 17} 18 19namespace extensions { 20class ExtensionPrefs; 21} 22 23namespace history { 24class TopSites; 25} 26 27namespace net { 28class CookieMonster; 29class URLRequestContextGetter; 30} 31 32namespace policy { 33class ProfilePolicyConnector; 34} 35 36namespace quota { 37class SpecialStoragePolicy; 38} 39 40class CommandLine; 41class ExtensionSpecialStoragePolicy; 42class HostContentSettingsMap; 43class PrefServiceSyncable; 44class ProfileDependencyManager; 45class ProfileSyncService; 46class TemplateURLService; 47class TestingPrefServiceSyncable; 48 49class TestingProfile : public Profile { 50 public: 51 // Profile directory name for the test user. This is "Default" on most 52 // platforms but must be different on ChromeOS because a logged-in user cannot 53 // use "Default" as profile directory. 54 // Browser- and UI tests should always use this to get to the user's profile 55 // directory. Unit-tests, though, should use |kInitialProfile|, which is 56 // always "Default", because they are runnining without logged-in user. 57 static const char kTestUserProfileDir[]; 58 59 // Default constructor that cannot be used with multi-profiles. 60 TestingProfile(); 61 62 // Helper class for building an instance of TestingProfile (allows injecting 63 // mocks for various services prior to profile initialization). 64 // TODO(atwilson): Remove non-default constructors and various setters in 65 // favor of using the Builder API. 66 class Builder { 67 public: 68 Builder(); 69 ~Builder(); 70 71 // Sets a Delegate to be called back when the Profile is fully initialized. 72 // This causes the final initialization to be performed via a task so the 73 // caller must run a MessageLoop. Caller maintains ownership of the Delegate 74 // and must manage its lifetime so it continues to exist until profile 75 // initialization is complete. 76 void SetDelegate(Delegate* delegate); 77 78 // Sets the ExtensionSpecialStoragePolicy to be returned by 79 // GetExtensionSpecialStoragePolicy(). 80 void SetExtensionSpecialStoragePolicy( 81 scoped_refptr<ExtensionSpecialStoragePolicy> policy); 82 83 // Sets the path to the directory to be used to hold profile data. 84 void SetPath(const base::FilePath& path); 85 86 // Sets the PrefService to be used by this profile. 87 void SetPrefService(scoped_ptr<PrefServiceSyncable> prefs); 88 89 // Creates the TestingProfile using previously-set settings. 90 scoped_ptr<TestingProfile> Build(); 91 92 private: 93 // If true, Build() has already been called. 94 bool build_called_; 95 96 // Various staging variables where values are held until Build() is invoked. 97 scoped_ptr<PrefServiceSyncable> pref_service_; 98 scoped_refptr<ExtensionSpecialStoragePolicy> extension_policy_; 99 base::FilePath path_; 100 Delegate* delegate_; 101 102 DISALLOW_COPY_AND_ASSIGN(Builder); 103 }; 104 105 // Multi-profile aware constructor that takes the path to a directory managed 106 // for this profile. This constructor is meant to be used by 107 // TestingProfileManager::CreateTestingProfile. If you need to create multi- 108 // profile profiles, use that factory method instead of this directly. 109 // Exception: if you need to create multi-profile profiles for testing the 110 // ProfileManager, then use the constructor below instead. 111 explicit TestingProfile(const base::FilePath& path); 112 113 // Multi-profile aware constructor that takes the path to a directory managed 114 // for this profile and a delegate. This constructor is meant to be used 115 // for unittesting the ProfileManager. 116 TestingProfile(const base::FilePath& path, Delegate* delegate); 117 118 // Full constructor allowing the setting of all possible instance data. 119 // Callers should use Builder::Build() instead of invoking this constructor. 120 TestingProfile(const base::FilePath& path, 121 Delegate* delegate, 122 scoped_refptr<ExtensionSpecialStoragePolicy> extension_policy, 123 scoped_ptr<PrefServiceSyncable> prefs); 124 125 virtual ~TestingProfile(); 126 127 // Creates the favicon service. Consequent calls would recreate the service. 128 void CreateFaviconService(); 129 130 // Creates the history service. If |delete_file| is true, the history file is 131 // deleted first, then the HistoryService is created. As TestingProfile 132 // deletes the directory containing the files used by HistoryService, this 133 // only matters if you're recreating the HistoryService. If |no_db| is true, 134 // the history backend will fail to initialize its database; this is useful 135 // for testing error conditions. 136 void CreateHistoryService(bool delete_file, bool no_db); 137 138 // Shuts down and nulls out the reference to HistoryService. 139 void DestroyHistoryService(); 140 141 // Creates TopSites. This returns immediately, and top sites may not be 142 // loaded. Use BlockUntilTopSitesLoaded to ensure TopSites has finished 143 // loading. 144 void CreateTopSites(); 145 146 // Shuts down and nulls out the reference to TopSites. 147 void DestroyTopSites(); 148 149 // Creates the BookmkarBarModel. If not invoked the bookmark bar model is 150 // NULL. If |delete_file| is true, the bookmarks file is deleted first, then 151 // the model is created. As TestingProfile deletes the directory containing 152 // the files used by HistoryService, the boolean only matters if you're 153 // recreating the BookmarkModel. 154 // 155 // NOTE: this does not block until the bookmarks are loaded. For that use 156 // ui_test_utils::WaitForBookmarkModelToLoad. 157 void CreateBookmarkModel(bool delete_file); 158 159 // Creates a WebDataService. If not invoked, the web data service is NULL. 160 void CreateWebDataService(); 161 162 // Blocks until the HistoryService finishes restoring its in-memory cache. 163 // This is NOT invoked from CreateHistoryService. 164 void BlockUntilHistoryIndexIsRefreshed(); 165 166 // Blocks until TopSites finishes loading. 167 void BlockUntilTopSitesLoaded(); 168 169 TestingPrefServiceSyncable* GetTestingPrefService(); 170 171 // content::BrowserContext 172 virtual base::FilePath GetPath() OVERRIDE; 173 virtual scoped_refptr<base::SequencedTaskRunner> GetIOTaskRunner() OVERRIDE; 174 virtual bool IsOffTheRecord() const OVERRIDE; 175 virtual content::DownloadManagerDelegate* 176 GetDownloadManagerDelegate() OVERRIDE; 177 // Returns a testing ContextGetter (if one has been created via 178 // CreateRequestContext) or NULL. This is not done on-demand for two reasons: 179 // (1) Some tests depend on GetRequestContext() returning NULL. (2) Because 180 // of the special memory management considerations for the 181 // TestURLRequestContextGetter class, many tests would find themseleves 182 // leaking if they called this method without the necessary IO thread. This 183 // getter is currently only capable of returning a Context that helps test 184 // the CookieMonster. See implementation comments for more details. 185 virtual net::URLRequestContextGetter* GetRequestContext() OVERRIDE; 186 virtual net::URLRequestContextGetter* CreateRequestContext( 187 content::ProtocolHandlerMap* protocol_handlers) OVERRIDE; 188 virtual net::URLRequestContextGetter* GetRequestContextForRenderProcess( 189 int renderer_child_id) OVERRIDE; 190 virtual content::ResourceContext* GetResourceContext() OVERRIDE; 191 virtual content::GeolocationPermissionContext* 192 GetGeolocationPermissionContext() OVERRIDE; 193 virtual content::SpeechRecognitionPreferences* 194 GetSpeechRecognitionPreferences() OVERRIDE; 195 virtual quota::SpecialStoragePolicy* GetSpecialStoragePolicy() OVERRIDE; 196 197 virtual TestingProfile* AsTestingProfile() OVERRIDE; 198 virtual std::string GetProfileName() OVERRIDE; 199 void set_incognito(bool incognito) { incognito_ = incognito; } 200 // Assumes ownership. 201 virtual void SetOffTheRecordProfile(Profile* profile); 202 virtual void SetOriginalProfile(Profile* profile); 203 virtual Profile* GetOffTheRecordProfile() OVERRIDE; 204 virtual void DestroyOffTheRecordProfile() OVERRIDE {} 205 virtual bool HasOffTheRecordProfile() OVERRIDE; 206 virtual Profile* GetOriginalProfile() OVERRIDE; 207 virtual ExtensionService* GetExtensionService() OVERRIDE; 208 void SetExtensionSpecialStoragePolicy( 209 ExtensionSpecialStoragePolicy* extension_special_storage_policy); 210 virtual ExtensionSpecialStoragePolicy* 211 GetExtensionSpecialStoragePolicy() OVERRIDE; 212 // The CookieMonster will only be returned if a Context has been created. Do 213 // this by calling CreateRequestContext(). See the note at GetRequestContext 214 // for more information. 215 net::CookieMonster* GetCookieMonster(); 216 217 virtual PrefService* GetPrefs() OVERRIDE; 218 219 virtual history::TopSites* GetTopSites() OVERRIDE; 220 virtual history::TopSites* GetTopSitesWithoutCreating() OVERRIDE; 221 222 void CreateRequestContext(); 223 // Clears out the created request context (which must be done before shutting 224 // down the IO thread to avoid leaks). 225 void ResetRequestContext(); 226 227 virtual net::URLRequestContextGetter* GetMediaRequestContext() OVERRIDE; 228 virtual net::URLRequestContextGetter* GetMediaRequestContextForRenderProcess( 229 int renderer_child_id) OVERRIDE; 230 virtual net::URLRequestContextGetter* 231 GetRequestContextForExtensions() OVERRIDE; 232 virtual net::URLRequestContextGetter* 233 GetMediaRequestContextForStoragePartition( 234 const base::FilePath& partition_path, 235 bool in_memory) OVERRIDE; 236 virtual net::URLRequestContextGetter* CreateRequestContextForStoragePartition( 237 const base::FilePath& partition_path, 238 bool in_memory, 239 content::ProtocolHandlerMap* protocol_handlers) OVERRIDE; 240 virtual net::SSLConfigService* GetSSLConfigService() OVERRIDE; 241 virtual HostContentSettingsMap* GetHostContentSettingsMap() OVERRIDE; 242 virtual std::wstring GetName(); 243 virtual void SetName(const std::wstring& name) {} 244 virtual std::wstring GetID(); 245 virtual void SetID(const std::wstring& id); 246 void set_last_session_exited_cleanly(bool value) { 247 last_session_exited_cleanly_ = value; 248 } 249 virtual void MergeResourceString(int message_id, 250 std::wstring* output_string) {} 251 virtual void MergeResourceInteger(int message_id, int* output_value) {} 252 virtual void MergeResourceBoolean(int message_id, bool* output_value) {} 253 virtual bool IsSameProfile(Profile *p) OVERRIDE; 254 virtual base::Time GetStartTime() const OVERRIDE; 255 virtual base::FilePath last_selected_directory() OVERRIDE; 256 virtual void set_last_selected_directory(const base::FilePath& path) OVERRIDE; 257 virtual bool WasCreatedByVersionOrLater(const std::string& version) OVERRIDE; 258 virtual void SetExitType(ExitType exit_type) OVERRIDE {} 259 virtual ExitType GetLastSessionExitType() OVERRIDE; 260#if defined(OS_CHROMEOS) 261 virtual void SetupChromeOSEnterpriseExtensionObserver() OVERRIDE { 262 } 263 virtual void InitChromeOSPreferences() OVERRIDE { 264 } 265 virtual void ChangeAppLocale(const std::string&, 266 AppLocaleChangedVia) OVERRIDE { 267 } 268 virtual void OnLogin() OVERRIDE { 269 } 270#endif // defined(OS_CHROMEOS) 271 272 virtual PrefProxyConfigTracker* GetProxyConfigTracker() OVERRIDE; 273 274 // Schedules a task on the history backend and runs a nested loop until the 275 // task is processed. This has the effect of blocking the caller until the 276 // history service processes all pending requests. 277 void BlockUntilHistoryProcessesPendingRequests(); 278 279 virtual chrome_browser_net::Predictor* GetNetworkPredictor() OVERRIDE; 280 virtual void ClearNetworkingHistorySince( 281 base::Time time, 282 const base::Closure& completion) OVERRIDE; 283 virtual GURL GetHomePage() OVERRIDE; 284 285 virtual PrefService* GetOffTheRecordPrefs() OVERRIDE; 286 287 protected: 288 base::Time start_time_; 289 scoped_ptr<PrefServiceSyncable> prefs_; 290 // ref only for right type, lifecycle is managed by prefs_ 291 TestingPrefServiceSyncable* testing_prefs_; 292 293 private: 294 // Creates a temporary directory for use by this profile. 295 void CreateTempProfileDir(); 296 297 // Common initialization between the two constructors. 298 void Init(); 299 300 // Finishes initialization when a profile is created asynchronously. 301 void FinishInit(); 302 303 // Creates a TestingPrefService and associates it with the TestingProfile. 304 void CreateTestingPrefService(); 305 306 // Creates a ProfilePolicyConnector that the ProfilePolicyConnectorFactory 307 // maps to this profile. 308 void CreateProfilePolicyConnector(); 309 310 // Internally, this is a TestURLRequestContextGetter that creates a dummy 311 // request context. Currently, only the CookieMonster is hooked up. 312 scoped_refptr<net::URLRequestContextGetter> request_context_; 313 scoped_refptr<net::URLRequestContextGetter> extensions_request_context_; 314 315 std::wstring id_; 316 317 bool incognito_; 318 scoped_ptr<Profile> incognito_profile_; 319 Profile* original_profile_; 320 321 // Did the last session exit cleanly? Default is true. 322 bool last_session_exited_cleanly_; 323 324 scoped_refptr<HostContentSettingsMap> host_content_settings_map_; 325 326 base::FilePath last_selected_directory_; 327 scoped_refptr<history::TopSites> top_sites_; // For history and thumbnails. 328 329 scoped_refptr<ExtensionSpecialStoragePolicy> 330 extension_special_storage_policy_; 331 332 // The proxy prefs tracker. 333 scoped_ptr<PrefProxyConfigTracker> pref_proxy_config_tracker_; 334 335 // We use a temporary directory to store testing profile data. In a multi- 336 // profile environment, this is invalid and the directory is managed by the 337 // TestingProfileManager. 338 base::ScopedTempDir temp_dir_; 339 // The path to this profile. This will be valid in either of the two above 340 // cases. 341 base::FilePath profile_path_; 342 343 // We keep a weak pointer to the dependency manager we want to notify on our 344 // death. Defaults to the Singleton implementation but overridable for 345 // testing. 346 ProfileDependencyManager* profile_dependency_manager_; 347 348 scoped_ptr<content::MockResourceContext> resource_context_; 349 350 scoped_ptr<policy::ProfilePolicyConnector> profile_policy_connector_; 351 352 // Weak pointer to a delegate for indicating that a profile was created. 353 Delegate* delegate_; 354}; 355 356#endif // CHROME_TEST_BASE_TESTING_PROFILE_H_ 357