1 // Copyright 2013 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_BROWSER_SYNC_TEST_INTEGRATION_SYNC_TEST_H_
6 #define CHROME_BROWSER_SYNC_TEST_INTEGRATION_SYNC_TEST_H_
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/scoped_vector.h"
15 #include "base/process/process.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/test/base/in_process_browser_test.h"
18 #include "net/dns/mock_host_resolver.h"
19 #include "net/http/http_status_code.h"
20 #include "net/url_request/url_request_status.h"
21 #include "sync/internal_api/public/base/model_type.h"
22 #include "sync/protocol/sync_protocol_error.h"
23 #include "sync/test/fake_server/fake_server.h"
24 #include "sync/test/local_sync_test_server.h"
26 class ProfileSyncService
;
27 class ProfileSyncServiceHarness
;
28 class P2PInvalidationForwarder
;
29 class P2PSyncRefresher
;
35 namespace fake_server
{
37 class FakeServerInvalidationService
;
41 class FakeURLFetcherFactory
;
42 class ScopedDefaultHostResolverProc
;
43 class URLFetcherImplFactory
;
44 class URLRequestContextGetter
;
47 // This is the base class for integration tests for all sync data types. Derived
48 // classes must be defined for each sync data type. Individual tests are defined
49 // using the IN_PROC_BROWSER_TEST_F macro.
50 class SyncTest
: public InProcessBrowserTest
{
52 // The different types of live sync tests that can be implemented.
54 // Tests where only one client profile is synced with the server. Typically
55 // sanity level tests.
58 // Tests that use one client profile and are not compatible with
60 // TODO(pvalenzuela): Delete this value when all SINGLE_CLIENT_LEGACY tests
61 // are compatible with FakeServer and switched to SINGLE_CLIENT. See
65 // Tests where two client profiles are synced with the server. Typically
66 // functionality level tests.
69 // Tests that use two client profiles and are not compatible with
71 // TODO(pvalenzuela): Delete this value when all TWO_CLIENT_LEGACY tests are
72 // compatible with FakeServer and switched to TWO_CLIENT. See
76 // Tests where three or more client profiles are synced with the server.
77 // Typically, these tests create client side races and verify that sync
82 // The type of server we're running against.
84 SERVER_TYPE_UNDECIDED
,
85 LOCAL_PYTHON_SERVER
, // The mock python server that runs locally and is
86 // part of the Chromium checkout.
87 LOCAL_LIVE_SERVER
, // Some other server (maybe the real binary used by
88 // Google's sync service) that can be started on
89 // a per-test basis by running a command
90 EXTERNAL_LIVE_SERVER
, // A remote server that the test code has no control
91 // over whatsoever; cross your fingers that the
92 // account state is initially clean.
93 IN_PROCESS_FAKE_SERVER
, // The fake Sync server (FakeServer) running
94 // in-process (bypassing HTTP calls). This
95 // ServerType will eventually replace
96 // LOCAL_PYTHON_SERVER.
99 // A SyncTest must be associated with a particular test type.
100 explicit SyncTest(TestType test_type
);
102 ~SyncTest() override
;
104 // Validates command line parameters and creates a local python test server if
106 void SetUp() override
;
108 // Brings down local python test server if one was created.
109 void TearDown() override
;
111 // Sets up command line flags required for sync tests.
112 void SetUpCommandLine(base::CommandLine
* cl
) override
;
114 // Used to get the number of sync clients used by a test.
115 int num_clients() WARN_UNUSED_RESULT
{ return num_clients_
; }
117 // Returns a pointer to a particular sync profile. Callee owns the object
118 // and manages its lifetime.
119 Profile
* GetProfile(int index
) WARN_UNUSED_RESULT
;
121 // Returns a pointer to a particular browser. Callee owns the object
122 // and manages its lifetime.
123 Browser
* GetBrowser(int index
) WARN_UNUSED_RESULT
;
125 // Returns a pointer to a particular sync client. Callee owns the object
126 // and manages its lifetime.
127 ProfileSyncServiceHarness
* GetClient(int index
) WARN_UNUSED_RESULT
;
129 // Returns a reference to the collection of sync clients. Callee owns the
130 // object and manages its lifetime.
131 std::vector
<ProfileSyncServiceHarness
*>& clients() WARN_UNUSED_RESULT
{
132 return clients_
.get();
135 // Returns a ProfileSyncService at the given index.
136 ProfileSyncService
* GetSyncService(int index
);
138 // Returns the set of ProfileSyncServices.
139 std::vector
<ProfileSyncService
*> GetSyncServices();
141 // Returns a pointer to the sync profile that is used to verify changes to
142 // individual sync profiles. Callee owns the object and manages its lifetime.
143 Profile
* verifier() WARN_UNUSED_RESULT
;
145 // Used to determine whether the verifier profile should be updated or not.
146 bool use_verifier() WARN_UNUSED_RESULT
{ return use_verifier_
; }
148 // After calling this method, changes made to a profile will no longer be
149 // reflected in the verifier profile. Note: Not all datatypes use this.
150 // TODO(rsimha): Hook up all datatypes to this mechanism.
151 void DisableVerifier();
153 // Initializes sync clients and profiles but does not sync any of them.
154 virtual bool SetupClients() WARN_UNUSED_RESULT
;
156 // Initializes sync clients and profiles if required and syncs each of them.
157 virtual bool SetupSync() WARN_UNUSED_RESULT
;
159 // Sets whether or not the sync clients in this test should respond to
160 // notifications of their own commits. Real sync clients do not do this, but
161 // many test assertions require this behavior.
163 // Default is to return true. Test should override this if they require
164 // different behavior.
165 virtual bool TestUsesSelfNotifications();
167 // Kicks off encryption for profile |index|.
168 bool EnableEncryption(int index
);
170 // Checks if encryption is complete for profile |index|.
171 bool IsEncryptionComplete(int index
);
173 // Waits until IsEncryptionComplete returns true or a timeout is reached.
174 bool AwaitEncryptionComplete(int index
);
176 // Blocks until all sync clients have completed their mutual sync cycles.
177 // Returns true if a quiescent state was successfully reached.
178 bool AwaitQuiescence();
180 // Returns true if we are running tests against external servers.
181 bool UsingExternalServers();
183 // Returns true if the server being used supports controlling
185 bool ServerSupportsNotificationControl() const;
187 // Disable notifications on the server. This operation is available
188 // only if ServerSupportsNotificationControl() returned true.
189 void DisableNotifications();
191 // Enable notifications on the server. This operation is available
192 // only if ServerSupportsNotificationControl() returned true.
193 void EnableNotifications();
195 // Sets the mock gaia response for when an OAuth2 token is requested.
196 // Each call to this method will overwrite responses that were previously set.
197 void SetOAuth2TokenResponse(const std::string
& response_data
,
198 net::HttpStatusCode response_code
,
199 net::URLRequestStatus::Status status
);
201 // Trigger a notification to be sent to all clients. This operation
202 // is available only if ServerSupportsNotificationControl() returned
204 void TriggerNotification(syncer::ModelTypeSet changed_types
);
206 // Returns true if the server being used supports injecting errors.
207 bool ServerSupportsErrorTriggering() const;
209 // Triggers a migration for one or more datatypes, and waits
210 // for the server to complete it. This operation is available
211 // only if ServerSupportsErrorTriggering() returned true.
212 void TriggerMigrationDoneError(syncer::ModelTypeSet model_types
);
214 // Triggers an XMPP auth error on the server. Note the server will
215 // stay in this state until shut down.
216 void TriggerXmppAuthError();
218 // Triggers the creation the Synced Bookmarks folder on the server.
219 void TriggerCreateSyncedBookmarks();
221 // Returns the FakeServer being used for the test or NULL if FakeServer is
223 fake_server::FakeServer
* GetFakeServer() const;
225 // Triggers a sync for the given |model_types| for the Profile at |index|.
226 void TriggerSyncForModelTypes(int index
, syncer::ModelTypeSet model_types
);
229 // Add custom switches needed for running the test.
230 virtual void AddTestSwitches(base::CommandLine
* cl
);
232 // Append the command line switches to enable experimental types that aren't
233 // on by default yet.
234 virtual void AddOptionalTypesToCommandLine(base::CommandLine
* cl
);
236 // BrowserTestBase override. Destroys all the sync clients and sync
237 // profiles created by a test.
238 void TearDownOnMainThread() override
;
240 // InProcessBrowserTest override. Changes behavior of the default host
241 // resolver to avoid DNS lookup errors.
242 void SetUpInProcessBrowserTestFixture() override
;
244 // InProcessBrowserTest override. Resets the host resolver its default
246 void TearDownInProcessBrowserTestFixture() override
;
248 // Creates Profile, Browser and ProfileSyncServiceHarness instances for
249 // |index|. Used by SetupClients().
250 virtual void InitializeInstance(int index
);
252 // Implementations of the EnableNotifications() and DisableNotifications()
253 // functions defined above.
254 void DisableNotificationsImpl();
255 void EnableNotificationsImpl();
257 // If non-empty, |contents| will be written to a profile's Preferences file
258 // before the Profile object is created.
259 void SetPreexistingPreferencesFileContents(const std::string
& contents
);
261 // GAIA account used by the test case.
262 std::string username_
;
264 // GAIA password used by the test case.
265 std::string password_
;
267 // Locally available plain text file in which GAIA credentials are stored.
268 base::FilePath password_file_
;
270 // The FakeServer used in tests with server type IN_PROCESS_FAKE_SERVER.
271 scoped_ptr
<fake_server::FakeServer
> fake_server_
;
274 // Helper to ProfileManager::CreateProfileAsync that creates a new profile
275 // used for UI Signin. Blocks until profile is created.
276 static Profile
* MakeProfileForUISignin(const base::FilePath::StringType name
);
278 // Callback for CreateNewProfile() method. It runs the quit_closure once
279 // profile is created successfully.
280 static void CreateProfileCallback(const base::Closure
& quit_closure
,
282 Profile::CreateStatus status
);
284 // Helper to Profile::CreateProfile that handles path creation. It creates
285 // a profile then registers it as a testing profile.
286 Profile
* MakeProfile(const base::FilePath::StringType name
);
288 // Helper method used to create a Gaia account at runtime.
289 // This function should only be called when running against external servers
290 // which support this functionality.
291 // Returns true if account creation was successful, false otherwise.
292 bool CreateGaiaAccount(const std::string
& username
,
293 const std::string
& password
);
295 // Helper method used to read GAIA credentials from a local password file
296 // specified via the "--password-file-for-test" command line switch.
297 // Note: The password file must be a plain text file with exactly two lines --
298 // the username on the first line and the password on the second line.
299 void ReadPasswordFile();
301 // Helper method that starts up a sync test server if required.
302 void SetUpTestServerIfRequired();
304 // Helper method used to start up a local python test server. Note: We set up
305 // an XMPP-only python server if |server_type_| is LOCAL_LIVE_SERVER and mock
306 // gaia credentials are in use. Returns true if successful.
307 bool SetUpLocalPythonTestServer();
309 // Helper method used to start up a local sync test server. Returns true if
311 bool SetUpLocalTestServer();
313 // Helper method used to destroy the local python sync test server if one was
314 // created. Returns true if successful.
315 bool TearDownLocalPythonTestServer();
317 // Helper method used to destroy the local sync test server if one was
318 // created. Returns true if successful.
319 bool TearDownLocalTestServer();
321 // Helper method that waits for up to |wait| for the test server
322 // to start. Splits the time into |intervals| intervals, and polls the
323 // server after each interval to see if it has started. Returns true if
325 bool WaitForTestServerToStart(base::TimeDelta wait
, int intervals
);
327 // Helper method used to check if the test server is up and running.
328 bool IsTestServerRunning();
330 void SetupNetwork(net::URLRequestContextGetter
* context
);
332 // Helper method used to set up fake responses for kClientLoginUrl,
333 // kIssueAuthTokenUrl, kGetUserInfoUrl and kSearchDomainCheckUrl in order to
334 // mock out calls to GAIA servers.
335 void SetupMockGaiaResponses();
337 // Helper method used to clear any fake responses that might have been set for
338 // various gaia URLs, cancel any outstanding URL requests, and return to using
339 // the default URLFetcher creation mechanism.
340 void ClearMockGaiaResponses();
342 // Decide which sync server implementation to run against based on the type
343 // of test being run and command line args passed in.
344 void DecideServerType();
346 // Sets up the client-side invalidations infrastructure depending on the
347 // value of |server_type_|.
348 void InitializeInvalidations(int index
);
350 // Python sync test server, started on demand.
351 syncer::LocalSyncTestServer sync_server_
;
353 // Helper class to whitelist the notification port.
354 scoped_ptr
<net::ScopedPortException
> xmpp_port_
;
356 // Used to differentiate between single-client, two-client, multi-client and
357 // many-client tests.
360 // Tells us what kind of server we're using (some tests run only on certain
362 ServerType server_type_
;
364 // Number of sync clients that will be created by a test.
367 // Collection of sync profiles used by a test. A sync profile maintains sync
368 // data contained within its own subdirectory under the chrome user data
369 // directory. Profiles are owned by the ProfileManager.
370 std::vector
<Profile
*> profiles_
;
372 // Collection of pointers to the browser objects used by a test. One browser
373 // instance is created for each sync profile. Browser object lifetime is
374 // managed by BrowserList, so we don't use a ScopedVector here.
375 std::vector
<Browser
*> browsers_
;
377 // Collection of sync clients used by a test. A sync client is associated with
378 // a sync profile, and implements methods that sync the contents of the
379 // profile with the server.
380 ScopedVector
<ProfileSyncServiceHarness
> clients_
;
382 // A set of objects to listen for commit activity and broadcast notifications
383 // of this activity to its peer sync clients.
384 ScopedVector
<P2PInvalidationForwarder
> invalidation_forwarders_
;
386 // A set of objects to listen for commit activity and broadcast refresh
387 // notifications of this activity to its peer sync clients.
388 ScopedVector
<P2PSyncRefresher
> sync_refreshers_
;
390 // Collection of pointers to FakeServerInvalidation objects for each profile.
391 std::vector
<fake_server::FakeServerInvalidationService
*>
392 fake_server_invalidation_services_
;
394 // Sync profile against which changes to individual profiles are verified. We
395 // don't need a corresponding verifier sync client because the contents of the
396 // verifier profile are strictly local, and are not meant to be synced.
399 // Indicates whether changes to a profile should also change the verifier
403 // Indicates whether or not notifications were explicitly enabled/disabled.
405 bool notifications_enabled_
;
407 // Indicates the need to create Gaia user account at runtime. This can only be
408 // set if tests are run against external servers with support for user
409 // creation via http requests.
410 bool create_gaia_account_at_runtime_
;
412 // Sync integration tests need to make live DNS requests for access to
413 // GAIA and sync server URLs under google.com. We use a scoped version
414 // to override the default resolver while the test is active.
415 scoped_ptr
<net::ScopedDefaultHostResolverProc
> mock_host_resolver_override_
;
417 // Used to start and stop the local test server.
418 base::Process test_server_
;
420 // Fake URLFetcher factory used to mock out GAIA signin.
421 scoped_ptr
<net::FakeURLFetcherFactory
> fake_factory_
;
423 // The URLFetcherImplFactory instance used to instantiate |fake_factory_|.
424 scoped_ptr
<net::URLFetcherImplFactory
> factory_
;
426 // The contents to be written to a profile's Preferences file before the
427 // Profile object is created. If empty, no preexisting file will be written.
428 std::string preexisting_preferences_file_contents_
;
430 DISALLOW_COPY_AND_ASSIGN(SyncTest
);
433 #endif // CHROME_BROWSER_SYNC_TEST_INTEGRATION_SYNC_TEST_H_