Fire an error if a pref used in the UI is missing once all prefs are fetched.
[chromium-blink-merge.git] / chrome / browser / sync / test / integration / sync_test.h
blob9e8cd45691740f611dd1310267f7794f4e1ae443
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_
8 #include <string>
9 #include <vector>
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;
31 namespace base {
32 class CommandLine;
35 namespace fake_server {
36 class FakeServer;
37 class FakeServerInvalidationService;
40 namespace net {
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 {
51 public:
52 // The different types of live sync tests that can be implemented.
53 enum TestType {
54 // Tests where only one client profile is synced with the server. Typically
55 // sanity level tests.
56 SINGLE_CLIENT,
58 // Tests that use one client profile and are not compatible with
59 // FakeServer.
60 // TODO(pvalenzuela): Delete this value when all SINGLE_CLIENT_LEGACY tests
61 // are compatible with FakeServer and switched to SINGLE_CLIENT. See
62 // crbug.com/323265.
63 SINGLE_CLIENT_LEGACY,
65 // Tests where two client profiles are synced with the server. Typically
66 // functionality level tests.
67 TWO_CLIENT,
69 // Tests that use two client profiles and are not compatible with
70 // FakeServer.
71 // TODO(pvalenzuela): Delete this value when all TWO_CLIENT_LEGACY tests are
72 // compatible with FakeServer and switched to TWO_CLIENT. See
73 // crbug.com/323265.
74 TWO_CLIENT_LEGACY,
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
78 // works.
79 MULTIPLE_CLIENT
82 // The type of server we're running against.
83 enum ServerType {
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
105 // specified.
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 the server being used supports controlling
181 // notifications.
182 bool ServerSupportsNotificationControl() const;
184 // Disable notifications on the server. This operation is available
185 // only if ServerSupportsNotificationControl() returned true.
186 void DisableNotifications();
188 // Enable notifications on the server. This operation is available
189 // only if ServerSupportsNotificationControl() returned true.
190 void EnableNotifications();
192 // Sets the mock gaia response for when an OAuth2 token is requested.
193 // Each call to this method will overwrite responses that were previously set.
194 void SetOAuth2TokenResponse(const std::string& response_data,
195 net::HttpStatusCode response_code,
196 net::URLRequestStatus::Status status);
198 // Trigger a notification to be sent to all clients. This operation
199 // is available only if ServerSupportsNotificationControl() returned
200 // true.
201 void TriggerNotification(syncer::ModelTypeSet changed_types);
203 // Returns true if the server being used supports injecting errors.
204 bool ServerSupportsErrorTriggering() const;
206 // Triggers a migration for one or more datatypes, and waits
207 // for the server to complete it. This operation is available
208 // only if ServerSupportsErrorTriggering() returned true.
209 void TriggerMigrationDoneError(syncer::ModelTypeSet model_types);
211 // Triggers an XMPP auth error on the server. Note the server will
212 // stay in this state until shut down.
213 void TriggerXmppAuthError();
215 // Triggers the creation the Synced Bookmarks folder on the server.
216 void TriggerCreateSyncedBookmarks();
218 // Returns the FakeServer being used for the test or NULL if FakeServer is
219 // not being used.
220 fake_server::FakeServer* GetFakeServer() const;
222 protected:
223 // Add custom switches needed for running the test.
224 virtual void AddTestSwitches(base::CommandLine* cl);
226 // Append the command line switches to enable experimental types that aren't
227 // on by default yet.
228 virtual void AddOptionalTypesToCommandLine(base::CommandLine* cl);
230 // BrowserTestBase override. Destroys all the sync clients and sync
231 // profiles created by a test.
232 void TearDownOnMainThread() override;
234 // InProcessBrowserTest override. Changes behavior of the default host
235 // resolver to avoid DNS lookup errors.
236 void SetUpInProcessBrowserTestFixture() override;
238 // InProcessBrowserTest override. Resets the host resolver its default
239 // behavior.
240 void TearDownInProcessBrowserTestFixture() override;
242 // Creates Profile, Browser and ProfileSyncServiceHarness instances for
243 // |index|. Used by SetupClients().
244 virtual void InitializeInstance(int index);
246 // Implementations of the EnableNotifications() and DisableNotifications()
247 // functions defined above.
248 void DisableNotificationsImpl();
249 void EnableNotificationsImpl();
251 // If non-empty, |contents| will be written to a profile's Preferences file
252 // before the Profile object is created.
253 void SetPreexistingPreferencesFileContents(const std::string& contents);
255 // GAIA account used by the test case.
256 std::string username_;
258 // GAIA password used by the test case.
259 std::string password_;
261 // Locally available plain text file in which GAIA credentials are stored.
262 base::FilePath password_file_;
264 // The FakeServer used in tests with server type IN_PROCESS_FAKE_SERVER.
265 scoped_ptr<fake_server::FakeServer> fake_server_;
267 private:
268 // Helper to ProfileManager::CreateProfileAsync that creates a new profile
269 // used for UI Signin. Blocks until profile is created.
270 static Profile* MakeProfileForUISignin(const base::FilePath::StringType name);
272 // Callback for CreateNewProfile() method. It runs the quit_closure once
273 // profile is created successfully.
274 static void CreateProfileCallback(const base::Closure& quit_closure,
275 Profile* profile,
276 Profile::CreateStatus status);
278 // Helper to Profile::CreateProfile that handles path creation. It creates
279 // a profile then registers it as a testing profile.
280 Profile* MakeProfile(const base::FilePath::StringType name);
282 // Helper method used to read GAIA credentials from a local password file
283 // specified via the "--password-file-for-test" command line switch.
284 // Note: The password file must be a plain text file with exactly two lines --
285 // the username on the first line and the password on the second line.
286 void ReadPasswordFile();
288 // Helper method that starts up a sync test server if required.
289 void SetUpTestServerIfRequired();
291 // Helper method used to start up a local python test server. Note: We set up
292 // an XMPP-only python server if |server_type_| is LOCAL_LIVE_SERVER and mock
293 // gaia credentials are in use. Returns true if successful.
294 bool SetUpLocalPythonTestServer();
296 // Helper method used to start up a local sync test server. Returns true if
297 // successful.
298 bool SetUpLocalTestServer();
300 // Helper method used to destroy the local python sync test server if one was
301 // created. Returns true if successful.
302 bool TearDownLocalPythonTestServer();
304 // Helper method used to destroy the local sync test server if one was
305 // created. Returns true if successful.
306 bool TearDownLocalTestServer();
308 // Helper method that waits for up to |wait| for the test server
309 // to start. Splits the time into |intervals| intervals, and polls the
310 // server after each interval to see if it has started. Returns true if
311 // successful.
312 bool WaitForTestServerToStart(base::TimeDelta wait, int intervals);
314 // Helper method used to check if the test server is up and running.
315 bool IsTestServerRunning();
317 void SetupNetwork(net::URLRequestContextGetter* context);
319 // Helper method used to set up fake responses for kClientLoginUrl,
320 // kIssueAuthTokenUrl, kGetUserInfoUrl and kSearchDomainCheckUrl in order to
321 // mock out calls to GAIA servers.
322 void SetupMockGaiaResponses();
324 // Helper method used to clear any fake responses that might have been set for
325 // various gaia URLs, cancel any outstanding URL requests, and return to using
326 // the default URLFetcher creation mechanism.
327 void ClearMockGaiaResponses();
329 // Decide which sync server implementation to run against based on the type
330 // of test being run and command line args passed in.
331 void DecideServerType();
333 // Sets up the client-side invalidations infrastructure depending on the
334 // value of |server_type_|.
335 void InitializeInvalidations(int index);
337 // Python sync test server, started on demand.
338 syncer::LocalSyncTestServer sync_server_;
340 // Helper class to whitelist the notification port.
341 scoped_ptr<net::ScopedPortException> xmpp_port_;
343 // Used to differentiate between single-client, two-client, multi-client and
344 // many-client tests.
345 TestType test_type_;
347 // Tells us what kind of server we're using (some tests run only on certain
348 // server types).
349 ServerType server_type_;
351 // Number of sync clients that will be created by a test.
352 int num_clients_;
354 // Collection of sync profiles used by a test. A sync profile maintains sync
355 // data contained within its own subdirectory under the chrome user data
356 // directory. Profiles are owned by the ProfileManager.
357 std::vector<Profile*> profiles_;
359 // Collection of pointers to the browser objects used by a test. One browser
360 // instance is created for each sync profile. Browser object lifetime is
361 // managed by BrowserList, so we don't use a ScopedVector here.
362 std::vector<Browser*> browsers_;
364 // Collection of sync clients used by a test. A sync client is associated with
365 // a sync profile, and implements methods that sync the contents of the
366 // profile with the server.
367 ScopedVector<ProfileSyncServiceHarness> clients_;
369 // A set of objects to listen for commit activity and broadcast notifications
370 // of this activity to its peer sync clients.
371 ScopedVector<P2PInvalidationForwarder> invalidation_forwarders_;
373 // A set of objects to listen for commit activity and broadcast refresh
374 // notifications of this activity to its peer sync clients.
375 ScopedVector<P2PSyncRefresher> sync_refreshers_;
377 // Collection of pointers to FakeServerInvalidation objects for each profile.
378 std::vector<fake_server::FakeServerInvalidationService*>
379 fake_server_invalidation_services_;
381 // Sync profile against which changes to individual profiles are verified. We
382 // don't need a corresponding verifier sync client because the contents of the
383 // verifier profile are strictly local, and are not meant to be synced.
384 Profile* verifier_;
386 // Indicates whether changes to a profile should also change the verifier
387 // profile or not.
388 bool use_verifier_;
390 // Indicates whether or not notifications were explicitly enabled/disabled.
391 // Defaults to true.
392 bool notifications_enabled_;
394 // Sync integration tests need to make live DNS requests for access to
395 // GAIA and sync server URLs under google.com. We use a scoped version
396 // to override the default resolver while the test is active.
397 scoped_ptr<net::ScopedDefaultHostResolverProc> mock_host_resolver_override_;
399 // Used to start and stop the local test server.
400 base::Process test_server_;
402 // Fake URLFetcher factory used to mock out GAIA signin.
403 scoped_ptr<net::FakeURLFetcherFactory> fake_factory_;
405 // The URLFetcherImplFactory instance used to instantiate |fake_factory_|.
406 scoped_ptr<net::URLFetcherImplFactory> factory_;
408 // The contents to be written to a profile's Preferences file before the
409 // Profile object is created. If empty, no preexisting file will be written.
410 std::string preexisting_preferences_file_contents_;
412 DISALLOW_COPY_AND_ASSIGN(SyncTest);
415 #endif // CHROME_BROWSER_SYNC_TEST_INTEGRATION_SYNC_TEST_H_