profile_sync_service_harness.h revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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
17class Profile;
18class StatusChangeChecker;
19class P2PInvalidationForwarder;
20
21namespace invalidation {
22class P2PInvalidationService;
23}
24
25namespace browser_sync {
26namespace sessions {
27class SyncSessionSnapshot;
28}
29}
30
31// An instance of this class is basically our notion of a "sync client" for
32// automation purposes. It harnesses the ProfileSyncService member of the
33// profile passed to it on construction and automates certain things like setup
34// and authentication. It provides ways to "wait" adequate periods of time for
35// several clients to get to the same state.
36class ProfileSyncServiceHarness
37    : public ProfileSyncServiceObserver {
38 public:
39  static ProfileSyncServiceHarness* Create(
40      Profile* profile,
41      const std::string& username,
42      const std::string& password);
43
44  static ProfileSyncServiceHarness* CreateForIntegrationTest(
45      Profile* profile,
46      const std::string& username,
47      const std::string& password,
48      invalidation::P2PInvalidationService* invalidation_service);
49
50  virtual ~ProfileSyncServiceHarness();
51
52  // Sets the GAIA credentials with which to sign in to sync.
53  void SetCredentials(const std::string& username, const std::string& password);
54
55  // Returns true if sync is disabled for this client.
56  bool IsSyncDisabled() const;
57
58  // Returns true if an auth error has been encountered.
59  bool HasAuthError() const;
60
61  // Creates a ProfileSyncService for the profile passed at construction and
62  // enables sync for all available datatypes. Returns true only after sync has
63  // been fully initialized and authenticated, and we are ready to process
64  // changes.
65  bool SetupSync();
66
67  // Same as the above method, but enables sync only for the datatypes contained
68  // in |synced_datatypes|.
69  bool SetupSync(syncer::ModelTypeSet synced_datatypes);
70
71  // ProfileSyncServiceObserver implementation.
72  virtual void OnStateChanged() OVERRIDE;
73  virtual void OnSyncCycleCompleted() OVERRIDE;
74
75  // Blocks the caller until the sync backend host associated with this harness
76  // has been initialized.  Returns true if the wait was successful.
77  bool AwaitBackendInitialized();
78
79  // Blocks the caller until the client has nothing left to commit and its
80  // progress markers are up to date. Returns true if successful.
81  bool AwaitCommitActivityCompletion();
82
83  // Blocks the caller until sync has been disabled for this client. Returns
84  // true if sync is disabled.
85  bool AwaitSyncDisabled();
86
87  // Blocks the caller until sync setup is complete for this client. Returns
88  // true if sync setup is complete.
89  bool AwaitSyncSetupCompletion();
90
91  // Calling this acts as a barrier and blocks the caller until |this| and
92  // |partner| have both completed a sync cycle.  When calling this method,
93  // the |partner| should be the passive responder who responds to the actions
94  // of |this|.  This method relies upon the synchronization of callbacks
95  // from the message queue. Returns true if two sync cycles have completed.
96  // Note: Use this method when exactly one client makes local change(s), and
97  // exactly one client is waiting to receive those changes.
98  bool AwaitMutualSyncCycleCompletion(ProfileSyncServiceHarness* partner);
99
100  // Blocks the caller until |this| completes its ongoing sync cycle and every
101  // other client in |partners| have achieved identical download progresses.
102  // Note: Use this method when exactly one client makes local change(s),
103  // and more than one client is waiting to receive those changes.
104  bool AwaitGroupSyncCycleCompletion(
105      std::vector<ProfileSyncServiceHarness*>& partners);
106
107  // Blocks the caller until every client in |clients| completes its ongoing
108  // sync cycle and all the clients' progress markers match.  Note: Use this
109  // method when more than one client makes local change(s), and more than one
110  // client is waiting to receive those changes.
111  static bool AwaitQuiescence(
112      std::vector<ProfileSyncServiceHarness*>& clients);
113
114  // Blocks the caller until |service_| indicates that a passphrase is required.
115  bool AwaitPassphraseRequired();
116
117  // Blocks the caller until |service_| indicates that the passphrase set by
118  // calling SetDecryptionPassphrase has been accepted.
119  bool AwaitPassphraseAccepted();
120
121  // Returns the ProfileSyncService member of the sync client.
122  ProfileSyncService* service() const { return service_; }
123
124  // Returns the debug name for this profile. Used for logging.
125  const std::string& profile_debug_name() const { return profile_debug_name_; }
126
127  // Returns the status of the ProfileSyncService member of the sync client.
128  ProfileSyncService::Status GetStatus() const;
129
130  // See ProfileSyncService::ShouldPushChanges().
131  bool ServiceIsPushingChanges() const { return service_->ShouldPushChanges(); }
132
133  // Enables sync for a particular sync datatype. Returns true on success.
134  bool EnableSyncForDatatype(syncer::ModelType datatype);
135
136  // Disables sync for a particular sync datatype. Returns true on success.
137  bool DisableSyncForDatatype(syncer::ModelType datatype);
138
139  // Enables sync for all sync datatypes. Returns true on success.
140  bool EnableSyncForAllDatatypes();
141
142  // Disables sync for all sync datatypes. Returns true on success.
143  bool DisableSyncForAllDatatypes();
144
145  // Returns a snapshot of the current sync session.
146  syncer::sessions::SyncSessionSnapshot GetLastSessionSnapshot() const;
147
148  // Encrypts all datatypes. This method will block while the sync backend host
149  // performs the encryption, or a timeout is reached. Returns true if
150  // encryption is complete and we are fully synced, and false if we timed out.
151  bool EnableEncryption();
152
153  // Waits until encryption is complete for all datatypes. Returns true if
154  // encryption is complete and we are fully synced, and false if we timed out.
155  bool WaitForEncryption();
156
157  // Returns true if encryption is complete for all datatypes, and false
158  // otherwise.
159  bool IsEncryptionComplete() const;
160
161  // Check if |type| is registered and the controller is running.
162  bool IsTypeRunning(syncer::ModelType type);
163
164  // Check if |type| is being synced.
165  bool IsTypePreferred(syncer::ModelType type);
166
167  // Gets the |auto_start_enabled_| variable from the |service_|.
168  bool AutoStartEnabled();
169
170  // Runs the UI message loop and waits until the Run() method of |checker|
171  // returns true, indicating that the status change we are waiting for has
172  // taken place. Caller retains ownership of |checker|, which must outlive this
173  // method. Returns true if the status change was observed. In case of a
174  // timeout, we CHECK(false).
175  bool AwaitStatusChange(StatusChangeChecker* checker);
176
177  // Returns a string that can be used as the value of an oauth2 refresh token.
178  // This function guarantees that a different string is returned each time
179  // it is called.
180  std::string GenerateFakeOAuth2RefreshTokenString();
181
182  // Returns a string with relevant info about client's sync state (if
183  // available), annotated with |message|. Useful for logging.
184  std::string GetClientInfoString(const std::string& message) const;
185
186 private:
187  ProfileSyncServiceHarness(
188      Profile* profile,
189      const std::string& username,
190      const std::string& password,
191      invalidation::P2PInvalidationService* invalidation_service);
192
193  // Quits the current message loop. Called when the status change being waited
194  // on has occurred, or in the event of a timeout.
195  void QuitMessageLoop();
196
197  // Signals that sync setup is complete, and that PSS may begin syncing.
198  void FinishSyncSetup();
199
200  // Gets the current progress marker of the current sync session for a
201  // particular datatype. Returns an empty string if the progress marker isn't
202  // found.
203  std::string GetSerializedProgressMarker(syncer::ModelType model_type) const;
204
205  // Gets detailed status from |service_| in pretty-printable form.
206  std::string GetServiceStatus();
207
208  // Sync profile associated with this sync client.
209  Profile* profile_;
210
211  // ProfileSyncService object associated with |profile_|.
212  ProfileSyncService* service_;
213
214  // An bridge between the ProfileSyncService and P2PInvalidationService.
215  scoped_ptr<P2PInvalidationForwarder> p2p_invalidation_forwarder_;
216
217  // Credentials used for GAIA authentication.
218  std::string username_;
219  std::string password_;
220
221  // Number used by GenerateFakeOAuth2RefreshTokenString() to make sure that
222  // all refresh tokens used in the tests are different.
223  int oauth2_refesh_token_number_;
224
225  // Used for logging.
226  const std::string profile_debug_name_;
227
228  // Keeps track of the state change on which we are waiting. PSSHarness can
229  // wait on only one status change at a time.
230  StatusChangeChecker* status_change_checker_;
231
232  DISALLOW_COPY_AND_ASSIGN(ProfileSyncServiceHarness);
233};
234
235#endif  // CHROME_BROWSER_SYNC_TEST_INTEGRATION_PROFILE_SYNC_SERVICE_HARNESS_H_
236