Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / sync / test / integration / profile_sync_service_harness.h
blob81004fcafe32f1c64d8c3c07129cb48fc06a231c
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_PROFILE_SYNC_SERVICE_HARNESS_H_
6 #define CHROME_BROWSER_SYNC_TEST_INTEGRATION_PROFILE_SYNC_SERVICE_HARNESS_H_
8 #include <string>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "chrome/browser/sync/profile_sync_service.h"
14 #include "chrome/browser/sync/profile_sync_service_observer.h"
15 #include "sync/internal_api/public/base/model_type.h"
17 class Profile;
18 class StatusChangeChecker;
20 namespace invalidation {
21 class P2PInvalidationService;
24 namespace browser_sync {
25 namespace sessions {
26 class SyncSessionSnapshot;
30 // An instance of this class is basically our notion of a "sync client" for
31 // automation purposes. It harnesses the ProfileSyncService member of the
32 // profile passed to it on construction and automates certain things like setup
33 // and authentication. It provides ways to "wait" adequate periods of time for
34 // several clients to get to the same state.
35 class ProfileSyncServiceHarness
36 : public ProfileSyncServiceObserver {
37 public:
38 static ProfileSyncServiceHarness* Create(
39 Profile* profile,
40 const std::string& username,
41 const std::string& password);
43 static ProfileSyncServiceHarness* CreateForIntegrationTest(
44 Profile* profile,
45 const std::string& username,
46 const std::string& password,
47 invalidation::P2PInvalidationService* invalidation_service);
49 virtual ~ProfileSyncServiceHarness();
51 // Sets the GAIA credentials with which to sign in to sync.
52 void SetCredentials(const std::string& username, const std::string& password);
54 // Returns true if sync is disabled for this client.
55 bool IsSyncDisabled() const;
57 // Returns true if an auth error has been encountered.
58 bool HasAuthError() const;
60 // Creates a ProfileSyncService for the profile passed at construction and
61 // enables sync for all available datatypes. Returns true only after sync has
62 // been fully initialized and authenticated, and we are ready to process
63 // changes.
64 bool SetupSync();
66 // Same as the above method, but enables sync only for the datatypes contained
67 // in |synced_datatypes|.
68 bool SetupSync(syncer::ModelTypeSet synced_datatypes);
70 // ProfileSyncServiceObserver implementation.
71 virtual void OnStateChanged() OVERRIDE;
72 virtual void OnSyncCycleCompleted() OVERRIDE;
74 // Blocks the caller until the sync backend host associated with this harness
75 // has been initialized. Returns true if the wait was successful.
76 bool AwaitBackendInitialized();
78 // Blocks the caller until this harness has completed a single sync cycle
79 // since the previous one. Returns true if a sync cycle has completed.
80 bool AwaitDataSyncCompletion();
82 // Blocks the caller until this harness has completed as many sync cycles as
83 // are required to ensure its progress marker matches the latest available on
84 // the server.
86 // Note: When other clients are committing changes this will not be reliable.
87 // If your test involves changes to multiple clients, you should use one of
88 // the other Await* functions, such as AwaitMutualSyncCycleComplete. Refer to
89 // the documentation of those functions for more details.
90 bool AwaitFullSyncCompletion();
92 // Blocks the caller until sync has been disabled for this client. Returns
93 // true if sync is disabled.
94 bool AwaitSyncDisabled();
96 // Blocks the caller until this harness has observed that the sync engine
97 // has downloaded all the changes seen by the |partner| harness's client.
98 bool WaitUntilProgressMarkersMatch(ProfileSyncServiceHarness* partner);
100 // Calling this acts as a barrier and blocks the caller until |this| and
101 // |partner| have both completed a sync cycle. When calling this method,
102 // the |partner| should be the passive responder who responds to the actions
103 // of |this|. This method relies upon the synchronization of callbacks
104 // from the message queue. Returns true if two sync cycles have completed.
105 // Note: Use this method when exactly one client makes local change(s), and
106 // exactly one client is waiting to receive those changes.
107 bool AwaitMutualSyncCycleCompletion(ProfileSyncServiceHarness* partner);
109 // Blocks the caller until |this| completes its ongoing sync cycle and every
110 // other client in |partners| have achieved identical download progresses.
111 // Note: Use this method when exactly one client makes local change(s),
112 // and more than one client is waiting to receive those changes.
113 bool AwaitGroupSyncCycleCompletion(
114 std::vector<ProfileSyncServiceHarness*>& partners);
116 // Blocks the caller until every client in |clients| completes its ongoing
117 // sync cycle and all the clients' progress markers match. Note: Use this
118 // method when more than one client makes local change(s), and more than one
119 // client is waiting to receive those changes.
120 static bool AwaitQuiescence(
121 std::vector<ProfileSyncServiceHarness*>& clients);
123 // Blocks the caller until |service_| indicates that a passphrase is required.
124 bool AwaitPassphraseRequired();
126 // Blocks the caller until |service_| indicates that the passphrase set by
127 // calling SetDecryptionPassphrase has been accepted.
128 bool AwaitPassphraseAccepted();
130 // Returns the ProfileSyncService member of the sync client.
131 ProfileSyncService* service() const { return service_; }
133 // Returns the debug name for this profile. Used for logging.
134 const std::string& profile_debug_name() const { return profile_debug_name_; }
136 // Returns the status of the ProfileSyncService member of the sync client.
137 ProfileSyncService::Status GetStatus() const;
139 // See ProfileSyncService::ShouldPushChanges().
140 bool ServiceIsPushingChanges() const { return service_->ShouldPushChanges(); }
142 // Enables sync for a particular sync datatype. Returns true on success.
143 bool EnableSyncForDatatype(syncer::ModelType datatype);
145 // Disables sync for a particular sync datatype. Returns true on success.
146 bool DisableSyncForDatatype(syncer::ModelType datatype);
148 // Enables sync for all sync datatypes. Returns true on success.
149 bool EnableSyncForAllDatatypes();
151 // Disables sync for all sync datatypes. Returns true on success.
152 bool DisableSyncForAllDatatypes();
154 // Returns a snapshot of the current sync session.
155 syncer::sessions::SyncSessionSnapshot GetLastSessionSnapshot() const;
157 // Encrypts all datatypes. This method will block while the sync backend host
158 // performs the encryption, or a timeout is reached. Returns true if
159 // encryption is complete and we are fully synced, and false if we timed out.
160 bool EnableEncryption();
162 // Waits until encryption is complete for all datatypes. Returns true if
163 // encryption is complete and we are fully synced, and false if we timed out.
164 bool WaitForEncryption();
166 // Returns true if encryption is complete for all datatypes, and false
167 // otherwise.
168 bool IsEncryptionComplete() const;
170 // Check if |type| is registered and the controller is running.
171 bool IsTypeRunning(syncer::ModelType type);
173 // Check if |type| is being synced.
174 bool IsTypePreferred(syncer::ModelType type);
176 // Returns true if the sync client has no unsynced items.
177 bool IsDataSynced() const;
179 // Returns true if the sync client has no unsynced items and its progress
180 // markers are believed to be up to date.
182 // Although we can't detect when commits from other clients invalidate our
183 // local progress markers, we do know when our own commits have invalidated
184 // our timestmaps. This check returns true when this client has, to the best
185 // of its knowledge, downloaded the latest progress markers.
186 bool IsFullySynced() const;
188 // Get the number of sync entries this client has. This includes all top
189 // level or permanent items, and can include recently deleted entries.
190 size_t GetNumEntries() const;
192 // Get the number of sync datatypes registered (ignoring whatever state
193 // they're in).
194 size_t GetNumDatatypes() const;
196 // Gets the |auto_start_enabled_| variable from the |service_|.
197 bool AutoStartEnabled();
199 // Runs the UI message loop and waits until the Run() method of |checker|
200 // returns true, indicating that the status change we are waiting for has
201 // taken place. Caller retains ownership of |checker|, which must outlive this
202 // method. Returns true if the status change was observed. In case of a
203 // timeout, we log the |source| of the call to this method, and return false.
204 bool AwaitStatusChange(StatusChangeChecker* checker,
205 const std::string& source);
207 // Returns a string that can be used as the value of an oauth2 refresh token.
208 // This function guarantees that a different string is returned each time
209 // it is called.
210 std::string GenerateFakeOAuth2RefreshTokenString();
212 // Returns a string with relevant info about client's sync state (if
213 // available), annotated with |message|. Useful for logging.
214 std::string GetClientInfoString(const std::string& message) const;
216 // Returns true if this client has downloaded all the items that the
217 // other client has.
218 bool MatchesPartnerClient() const;
220 // Returns true if there is a backend migration in progress.
221 bool HasPendingBackendMigration() const;
223 private:
224 ProfileSyncServiceHarness(
225 Profile* profile,
226 const std::string& username,
227 const std::string& password,
228 invalidation::P2PInvalidationService* invalidation_service);
230 // Quits the current message loop. Called when the status change being waited
231 // on has occurred, or in the event of a timeout.
232 void QuitMessageLoop();
234 // A helper for implementing IsDataSynced() and IsFullySynced().
235 bool IsDataSyncedImpl() const;
237 // Signals that sync setup is complete, and that PSS may begin syncing.
238 void FinishSyncSetup();
240 // Gets the current progress marker of the current sync session for a
241 // particular datatype. Returns an empty string if the progress marker isn't
242 // found.
243 std::string GetSerializedProgressMarker(syncer::ModelType model_type) const;
245 // Gets detailed status from |service_| in pretty-printable form.
246 std::string GetServiceStatus();
248 // Sync profile associated with this sync client.
249 Profile* profile_;
251 // ProfileSyncService object associated with |profile_|.
252 ProfileSyncService* service_;
254 // P2PInvalidationService associated with |profile_|.
255 invalidation::P2PInvalidationService* p2p_invalidation_service_;
257 // The harness of the client whose update progress marker we're expecting
258 // eventually match.
259 ProfileSyncServiceHarness* progress_marker_partner_;
261 // Credentials used for GAIA authentication.
262 std::string username_;
263 std::string password_;
265 // Number used by GenerateFakeOAuth2RefreshTokenString() to make sure that
266 // all refresh tokens used in the tests are different.
267 int oauth2_refesh_token_number_;
269 // Used for logging.
270 const std::string profile_debug_name_;
272 // Keeps track of the state change on which we are waiting. PSSHarness can
273 // wait on only one status change at a time.
274 StatusChangeChecker* status_change_checker_;
276 DISALLOW_COPY_AND_ASSIGN(ProfileSyncServiceHarness);
279 #endif // CHROME_BROWSER_SYNC_TEST_INTEGRATION_PROFILE_SYNC_SERVICE_HARNESS_H_