Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / sync / test / integration / profile_sync_service_harness.h
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.
4
5 #ifndef CHROME_BROWSER_SYNC_TEST_INTEGRATION_PROFILE_SYNC_SERVICE_HARNESS_H_
6 #define CHROME_BROWSER_SYNC_TEST_INTEGRATION_PROFILE_SYNC_SERVICE_HARNESS_H_
7
8 #include <string>
9 #include <vector>
10
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"
16
17 class Profile;
18 class StatusChangeChecker;
19
20 namespace invalidation {
21 class P2PInvalidationService;
22 }
23
24 namespace browser_sync {
25 namespace sessions {
26 class SyncSessionSnapshot;
27 }
28 }
29
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);
42
43   static ProfileSyncServiceHarness* CreateForIntegrationTest(
44       Profile* profile,
45       const std::string& username,
46       const std::string& password,
47       invalidation::P2PInvalidationService* invalidation_service);
48
49   virtual ~ProfileSyncServiceHarness();
50
51   // Sets the GAIA credentials with which to sign in to sync.
52   void SetCredentials(const std::string& username, const std::string& password);
53
54   // Returns true if sync is disabled for this client.
55   bool IsSyncDisabled() const;
56
57   // Returns true if an auth error has been encountered.
58   bool HasAuthError() const;
59
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();
65
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);
69
70   // ProfileSyncServiceObserver implementation.
71   virtual void OnStateChanged() OVERRIDE;
72   virtual void OnSyncCycleCompleted() OVERRIDE;
73
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();
77
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();
81
82   // Blocks the caller until sync has been disabled for this client. Returns
83   // true if sync is disabled.
84   bool AwaitSyncDisabled();
85
86   // Blocks the caller until sync setup is complete for this client. Returns
87   // true if sync setup is complete.
88   bool AwaitSyncSetupCompletion();
89
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);
93
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);
102
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);
109
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);
116
117   // Blocks the caller until |service_| indicates that a passphrase is required.
118   bool AwaitPassphraseRequired();
119
120   // Blocks the caller until |service_| indicates that the passphrase set by
121   // calling SetDecryptionPassphrase has been accepted.
122   bool AwaitPassphraseAccepted();
123
124   // Returns the ProfileSyncService member of the sync client.
125   ProfileSyncService* service() const { return service_; }
126
127   // Returns the debug name for this profile. Used for logging.
128   const std::string& profile_debug_name() const { return profile_debug_name_; }
129
130   // Returns the status of the ProfileSyncService member of the sync client.
131   ProfileSyncService::Status GetStatus() const;
132
133   // See ProfileSyncService::ShouldPushChanges().
134   bool ServiceIsPushingChanges() const { return service_->ShouldPushChanges(); }
135
136   // Enables sync for a particular sync datatype. Returns true on success.
137   bool EnableSyncForDatatype(syncer::ModelType datatype);
138
139   // Disables sync for a particular sync datatype. Returns true on success.
140   bool DisableSyncForDatatype(syncer::ModelType datatype);
141
142   // Enables sync for all sync datatypes. Returns true on success.
143   bool EnableSyncForAllDatatypes();
144
145   // Disables sync for all sync datatypes. Returns true on success.
146   bool DisableSyncForAllDatatypes();
147
148   // Returns a snapshot of the current sync session.
149   syncer::sessions::SyncSessionSnapshot GetLastSessionSnapshot() const;
150
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();
155
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();
159
160   // Returns true if encryption is complete for all datatypes, and false
161   // otherwise.
162   bool IsEncryptionComplete() const;
163
164   // Check if |type| is registered and the controller is running.
165   bool IsTypeRunning(syncer::ModelType type);
166
167   // Check if |type| is being synced.
168   bool IsTypePreferred(syncer::ModelType type);
169
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;
173
174   // Get the number of sync datatypes registered (ignoring whatever state
175   // they're in).
176   size_t GetNumDatatypes() const;
177
178   // Gets the |auto_start_enabled_| variable from the |service_|.
179   bool AutoStartEnabled();
180
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);
188
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
191   // it is called.
192   std::string GenerateFakeOAuth2RefreshTokenString();
193
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;
197
198   // Returns true if this client has downloaded all the items that the
199   // other client has.
200   bool MatchesPartnerClient() const;
201
202  private:
203   ProfileSyncServiceHarness(
204       Profile* profile,
205       const std::string& username,
206       const std::string& password,
207       invalidation::P2PInvalidationService* invalidation_service);
208
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();
212
213   // Signals that sync setup is complete, and that PSS may begin syncing.
214   void FinishSyncSetup();
215
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
218   // found.
219   std::string GetSerializedProgressMarker(syncer::ModelType model_type) const;
220
221   // Returns true if a client has nothing left to commit and its progress
222   // markers are up to date.
223   bool HasLatestProgressMarkers() const;
224
225   // Gets detailed status from |service_| in pretty-printable form.
226   std::string GetServiceStatus();
227
228   // Sync profile associated with this sync client.
229   Profile* profile_;
230
231   // ProfileSyncService object associated with |profile_|.
232   ProfileSyncService* service_;
233
234   // P2PInvalidationService associated with |profile_|.
235   invalidation::P2PInvalidationService* p2p_invalidation_service_;
236
237   // The harness of the client whose update progress marker we're expecting
238   // eventually match.
239   ProfileSyncServiceHarness* progress_marker_partner_;
240
241   // Credentials used for GAIA authentication.
242   std::string username_;
243   std::string password_;
244
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_;
248
249   // Used for logging.
250   const std::string profile_debug_name_;
251
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_;
255
256   DISALLOW_COPY_AND_ASSIGN(ProfileSyncServiceHarness);
257 };
258
259 #endif  // CHROME_BROWSER_SYNC_TEST_INTEGRATION_PROFILE_SYNC_SERVICE_HARNESS_H_