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_
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"
18 class StatusChangeChecker;
20 namespace invalidation {
21 class P2PInvalidationService;
24 namespace browser_sync {
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 {
38 static ProfileSyncServiceHarness* Create(
40 const std::string& username,
41 const std::string& password);
43 static ProfileSyncServiceHarness* CreateForIntegrationTest(
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
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 the client has nothing left to commit and its
79 // progress markers are up to date. Returns true if successful.
80 bool AwaitCommitActivityCompletion();
82 // Blocks the caller until sync has been disabled for this client. Returns
83 // true if sync is disabled.
84 bool AwaitSyncDisabled();
86 // Blocks the caller until sync setup is complete for this client. Returns
87 // true if sync setup is complete.
88 bool AwaitSyncSetupCompletion();
90 // Blocks the caller until this harness has observed that the sync engine
91 // has downloaded all the changes seen by the |partner| harness's client.
92 bool WaitUntilProgressMarkersMatch(ProfileSyncServiceHarness* partner);
94 // Calling this acts as a barrier and blocks the caller until |this| and
95 // |partner| have both completed a sync cycle. When calling this method,
96 // the |partner| should be the passive responder who responds to the actions
97 // of |this|. This method relies upon the synchronization of callbacks
98 // from the message queue. Returns true if two sync cycles have completed.
99 // Note: Use this method when exactly one client makes local change(s), and
100 // exactly one client is waiting to receive those changes.
101 bool AwaitMutualSyncCycleCompletion(ProfileSyncServiceHarness* partner);
103 // Blocks the caller until |this| completes its ongoing sync cycle and every
104 // other client in |partners| have achieved identical download progresses.
105 // Note: Use this method when exactly one client makes local change(s),
106 // and more than one client is waiting to receive those changes.
107 bool AwaitGroupSyncCycleCompletion(
108 std::vector<ProfileSyncServiceHarness*>& partners);
110 // Blocks the caller until every client in |clients| completes its ongoing
111 // sync cycle and all the clients' progress markers match. Note: Use this
112 // method when more than one client makes local change(s), and more than one
113 // client is waiting to receive those changes.
114 static bool AwaitQuiescence(
115 std::vector<ProfileSyncServiceHarness*>& clients);
117 // Blocks the caller until |service_| indicates that a passphrase is required.
118 bool AwaitPassphraseRequired();
120 // Blocks the caller until |service_| indicates that the passphrase set by
121 // calling SetDecryptionPassphrase has been accepted.
122 bool AwaitPassphraseAccepted();
124 // Returns the ProfileSyncService member of the sync client.
125 ProfileSyncService* service() const { return service_; }
127 // Returns the debug name for this profile. Used for logging.
128 const std::string& profile_debug_name() const { return profile_debug_name_; }
130 // Returns the status of the ProfileSyncService member of the sync client.
131 ProfileSyncService::Status GetStatus() const;
133 // See ProfileSyncService::ShouldPushChanges().
134 bool ServiceIsPushingChanges() const { return service_->ShouldPushChanges(); }
136 // Enables sync for a particular sync datatype. Returns true on success.
137 bool EnableSyncForDatatype(syncer::ModelType datatype);
139 // Disables sync for a particular sync datatype. Returns true on success.
140 bool DisableSyncForDatatype(syncer::ModelType datatype);
142 // Enables sync for all sync datatypes. Returns true on success.
143 bool EnableSyncForAllDatatypes();
145 // Disables sync for all sync datatypes. Returns true on success.
146 bool DisableSyncForAllDatatypes();
148 // Returns a snapshot of the current sync session.
149 syncer::sessions::SyncSessionSnapshot GetLastSessionSnapshot() const;
151 // Encrypts all datatypes. This method will block while the sync backend host
152 // performs the encryption, or a timeout is reached. Returns true if
153 // encryption is complete and we are fully synced, and false if we timed out.
154 bool EnableEncryption();
156 // Waits until encryption is complete for all datatypes. Returns true if
157 // encryption is complete and we are fully synced, and false if we timed out.
158 bool WaitForEncryption();
160 // Returns true if encryption is complete for all datatypes, and false
162 bool IsEncryptionComplete() const;
164 // Check if |type| is registered and the controller is running.
165 bool IsTypeRunning(syncer::ModelType type);
167 // Check if |type| is being synced.
168 bool IsTypePreferred(syncer::ModelType type);
170 // Get the number of sync entries this client has. This includes all top
171 // level or permanent items, and can include recently deleted entries.
172 size_t GetNumEntries() const;
174 // Get the number of sync datatypes registered (ignoring whatever state
176 size_t GetNumDatatypes() const;
178 // Gets the |auto_start_enabled_| variable from the |service_|.
179 bool AutoStartEnabled();
181 // Runs the UI message loop and waits until the Run() method of |checker|
182 // returns true, indicating that the status change we are waiting for has
183 // taken place. Caller retains ownership of |checker|, which must outlive this
184 // method. Returns true if the status change was observed. In case of a
185 // timeout, we log the |source| of the call to this method, and return false.
186 bool AwaitStatusChange(StatusChangeChecker* checker,
187 const std::string& source);
189 // Returns a string that can be used as the value of an oauth2 refresh token.
190 // This function guarantees that a different string is returned each time
192 std::string GenerateFakeOAuth2RefreshTokenString();
194 // Returns a string with relevant info about client's sync state (if
195 // available), annotated with |message|. Useful for logging.
196 std::string GetClientInfoString(const std::string& message) const;
198 // Returns true if this client has downloaded all the items that the
200 bool MatchesPartnerClient() const;
203 ProfileSyncServiceHarness(
205 const std::string& username,
206 const std::string& password,
207 invalidation::P2PInvalidationService* invalidation_service);
209 // Quits the current message loop. Called when the status change being waited
210 // on has occurred, or in the event of a timeout.
211 void QuitMessageLoop();
213 // Signals that sync setup is complete, and that PSS may begin syncing.
214 void FinishSyncSetup();
216 // Gets the current progress marker of the current sync session for a
217 // particular datatype. Returns an empty string if the progress marker isn't
219 std::string GetSerializedProgressMarker(syncer::ModelType model_type) const;
221 // Returns true if a client has nothing left to commit and its progress
222 // markers are up to date.
223 bool HasLatestProgressMarkers() const;
225 // Gets detailed status from |service_| in pretty-printable form.
226 std::string GetServiceStatus();
228 // Sync profile associated with this sync client.
231 // ProfileSyncService object associated with |profile_|.
232 ProfileSyncService* service_;
234 // P2PInvalidationService associated with |profile_|.
235 invalidation::P2PInvalidationService* p2p_invalidation_service_;
237 // The harness of the client whose update progress marker we're expecting
239 ProfileSyncServiceHarness* progress_marker_partner_;
241 // Credentials used for GAIA authentication.
242 std::string username_;
243 std::string password_;
245 // Number used by GenerateFakeOAuth2RefreshTokenString() to make sure that
246 // all refresh tokens used in the tests are different.
247 int oauth2_refesh_token_number_;
250 const std::string profile_debug_name_;
252 // Keeps track of the state change on which we are waiting. PSSHarness can
253 // wait on only one status change at a time.
254 StatusChangeChecker* status_change_checker_;
256 DISALLOW_COPY_AND_ASSIGN(ProfileSyncServiceHarness);
259 #endif // CHROME_BROWSER_SYNC_TEST_INTEGRATION_PROFILE_SYNC_SERVICE_HARNESS_H_