testing_profile.h revision 7dbb3d5cf0c15f500944d211057644d6a2f37371
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 BrowserContextDependencyManager; 41class CommandLine; 42class ExtensionSpecialStoragePolicy; 43class HostContentSettingsMap; 44class PrefServiceSyncable; 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() const 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 quota::SpecialStoragePolicy* GetSpecialStoragePolicy() OVERRIDE; 194 195 virtual TestingProfile* AsTestingProfile() OVERRIDE; 196 virtual std::string GetProfileName() OVERRIDE; 197 void set_incognito(bool incognito) { incognito_ = incognito; } 198 // Assumes ownership. 199 virtual void SetOffTheRecordProfile(Profile* profile); 200 virtual void SetOriginalProfile(Profile* profile); 201 virtual Profile* GetOffTheRecordProfile() OVERRIDE; 202 virtual void DestroyOffTheRecordProfile() OVERRIDE {} 203 virtual bool HasOffTheRecordProfile() OVERRIDE; 204 virtual Profile* GetOriginalProfile() OVERRIDE; 205 virtual ExtensionService* GetExtensionService() OVERRIDE; 206 void SetExtensionSpecialStoragePolicy( 207 ExtensionSpecialStoragePolicy* extension_special_storage_policy); 208 virtual ExtensionSpecialStoragePolicy* 209 GetExtensionSpecialStoragePolicy() OVERRIDE; 210 // The CookieMonster will only be returned if a Context has been created. Do 211 // this by calling CreateRequestContext(). See the note at GetRequestContext 212 // for more information. 213 net::CookieMonster* GetCookieMonster(); 214 215 virtual PrefService* GetPrefs() OVERRIDE; 216 217 virtual history::TopSites* GetTopSites() OVERRIDE; 218 virtual history::TopSites* GetTopSitesWithoutCreating() OVERRIDE; 219 220 void CreateRequestContext(); 221 // Clears out the created request context (which must be done before shutting 222 // down the IO thread to avoid leaks). 223 void ResetRequestContext(); 224 225 virtual net::URLRequestContextGetter* GetMediaRequestContext() OVERRIDE; 226 virtual net::URLRequestContextGetter* GetMediaRequestContextForRenderProcess( 227 int renderer_child_id) OVERRIDE; 228 virtual net::URLRequestContextGetter* 229 GetRequestContextForExtensions() OVERRIDE; 230 virtual net::URLRequestContextGetter* 231 GetMediaRequestContextForStoragePartition( 232 const base::FilePath& partition_path, 233 bool in_memory) OVERRIDE; 234 virtual void RequestMIDISysExPermission( 235 int render_process_id, 236 int render_view_id, 237 const GURL& requesting_frame, 238 const MIDISysExPermissionCallback& callback) OVERRIDE; 239 virtual net::URLRequestContextGetter* CreateRequestContextForStoragePartition( 240 const base::FilePath& partition_path, 241 bool in_memory, 242 content::ProtocolHandlerMap* protocol_handlers) OVERRIDE; 243 virtual net::SSLConfigService* GetSSLConfigService() OVERRIDE; 244 virtual HostContentSettingsMap* GetHostContentSettingsMap() OVERRIDE; 245 virtual std::wstring GetName(); 246 virtual void SetName(const std::wstring& name) {} 247 virtual std::wstring GetID(); 248 virtual void SetID(const std::wstring& id); 249 void set_last_session_exited_cleanly(bool value) { 250 last_session_exited_cleanly_ = value; 251 } 252 virtual void MergeResourceString(int message_id, 253 std::wstring* output_string) {} 254 virtual void MergeResourceInteger(int message_id, int* output_value) {} 255 virtual void MergeResourceBoolean(int message_id, bool* output_value) {} 256 virtual bool IsSameProfile(Profile *p) OVERRIDE; 257 virtual base::Time GetStartTime() const OVERRIDE; 258 virtual base::FilePath last_selected_directory() OVERRIDE; 259 virtual void set_last_selected_directory(const base::FilePath& path) OVERRIDE; 260 virtual bool WasCreatedByVersionOrLater(const std::string& version) OVERRIDE; 261 virtual bool IsGuestSession() const OVERRIDE; 262 virtual void SetExitType(ExitType exit_type) OVERRIDE {} 263 virtual ExitType GetLastSessionExitType() OVERRIDE; 264#if defined(OS_CHROMEOS) 265 virtual void SetupChromeOSEnterpriseExtensionObserver() OVERRIDE { 266 } 267 virtual void InitChromeOSPreferences() OVERRIDE { 268 } 269 virtual void ChangeAppLocale(const std::string&, 270 AppLocaleChangedVia) OVERRIDE { 271 } 272 virtual void OnLogin() OVERRIDE { 273 } 274#endif // defined(OS_CHROMEOS) 275 276 virtual PrefProxyConfigTracker* GetProxyConfigTracker() OVERRIDE; 277 278 // Schedules a task on the history backend and runs a nested loop until the 279 // task is processed. This has the effect of blocking the caller until the 280 // history service processes all pending requests. 281 void BlockUntilHistoryProcessesPendingRequests(); 282 283 virtual chrome_browser_net::Predictor* GetNetworkPredictor() OVERRIDE; 284 virtual void ClearNetworkingHistorySince( 285 base::Time time, 286 const base::Closure& completion) OVERRIDE; 287 virtual GURL GetHomePage() OVERRIDE; 288 289 virtual PrefService* GetOffTheRecordPrefs() OVERRIDE; 290 291 protected: 292 base::Time start_time_; 293 scoped_ptr<PrefServiceSyncable> prefs_; 294 // ref only for right type, lifecycle is managed by prefs_ 295 TestingPrefServiceSyncable* testing_prefs_; 296 297 private: 298 // Creates a temporary directory for use by this profile. 299 void CreateTempProfileDir(); 300 301 // Common initialization between the two constructors. 302 void Init(); 303 304 // Finishes initialization when a profile is created asynchronously. 305 void FinishInit(); 306 307 // Creates a TestingPrefService and associates it with the TestingProfile. 308 void CreateTestingPrefService(); 309 310 // Creates a ProfilePolicyConnector that the ProfilePolicyConnectorFactory 311 // maps to this profile. 312 void CreateProfilePolicyConnector(); 313 314 // Internally, this is a TestURLRequestContextGetter that creates a dummy 315 // request context. Currently, only the CookieMonster is hooked up. 316 scoped_refptr<net::URLRequestContextGetter> request_context_; 317 scoped_refptr<net::URLRequestContextGetter> extensions_request_context_; 318 319 std::wstring id_; 320 321 bool incognito_; 322 scoped_ptr<Profile> incognito_profile_; 323 Profile* original_profile_; 324 325 // Did the last session exit cleanly? Default is true. 326 bool last_session_exited_cleanly_; 327 328 scoped_refptr<HostContentSettingsMap> host_content_settings_map_; 329 330 base::FilePath last_selected_directory_; 331 scoped_refptr<history::TopSites> top_sites_; // For history and thumbnails. 332 333 scoped_refptr<ExtensionSpecialStoragePolicy> 334 extension_special_storage_policy_; 335 336 // The proxy prefs tracker. 337 scoped_ptr<PrefProxyConfigTracker> pref_proxy_config_tracker_; 338 339 // We use a temporary directory to store testing profile data. In a multi- 340 // profile environment, this is invalid and the directory is managed by the 341 // TestingProfileManager. 342 base::ScopedTempDir temp_dir_; 343 // The path to this profile. This will be valid in either of the two above 344 // cases. 345 base::FilePath profile_path_; 346 347 // We keep a weak pointer to the dependency manager we want to notify on our 348 // death. Defaults to the Singleton implementation but overridable for 349 // testing. 350 BrowserContextDependencyManager* browser_context_dependency_manager_; 351 352 scoped_ptr<content::MockResourceContext> resource_context_; 353 354 scoped_ptr<policy::ProfilePolicyConnector> profile_policy_connector_; 355 356 // Weak pointer to a delegate for indicating that a profile was created. 357 Delegate* delegate_; 358}; 359 360#endif // CHROME_TEST_BASE_TESTING_PROFILE_H_ 361