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 "sync/internal_api/public/base/model_type.h"
14#include "sync/internal_api/public/sessions/sync_session_snapshot.h"
15
16class Profile;
17class ProfileSyncService;
18
19// An instance of this class is basically our notion of a "sync client" for
20// automation purposes. It harnesses the ProfileSyncService member of the
21// profile passed to it on construction and automates certain things like setup
22// and authentication. It provides ways to "wait" adequate periods of time for
23// several clients to get to the same state.
24class ProfileSyncServiceHarness {
25 public:
26  static ProfileSyncServiceHarness* Create(
27      Profile* profile,
28      const std::string& username,
29      const std::string& password);
30  virtual ~ProfileSyncServiceHarness();
31
32  // Sets the GAIA credentials with which to sign in to sync.
33  void SetCredentials(const std::string& username, const std::string& password);
34
35  // Creates a ProfileSyncService for the profile passed at construction and
36  // enables sync for all available datatypes. Returns true only after sync has
37  // been fully initialized and authenticated, and we are ready to process
38  // changes.
39  bool SetupSync();
40
41  // Same as the above method, but enables sync only for the datatypes contained
42  // in |synced_datatypes|.
43  bool SetupSync(syncer::ModelTypeSet synced_datatypes);
44
45  // Calling this acts as a barrier and blocks the caller until |this| and
46  // |partner| have both completed a sync cycle.  When calling this method,
47  // the |partner| should be the passive responder who responds to the actions
48  // of |this|.  This method relies upon the synchronization of callbacks
49  // from the message queue. Returns true if two sync cycles have completed.
50  // Note: Use this method when exactly one client makes local change(s), and
51  // exactly one client is waiting to receive those changes.
52  bool AwaitMutualSyncCycleCompletion(ProfileSyncServiceHarness* partner);
53
54  // Blocks the caller until |this| completes its ongoing sync cycle and every
55  // other client in |partners| have achieved identical download progresses.
56  // Note: Use this method when exactly one client makes local change(s),
57  // and more than one client is waiting to receive those changes.
58  bool AwaitGroupSyncCycleCompletion(
59      std::vector<ProfileSyncServiceHarness*>& partners);
60
61  // Blocks the caller until every client in |clients| completes its ongoing
62  // sync cycle and all the clients' progress markers match.  Note: Use this
63  // method when more than one client makes local change(s), and more than one
64  // client is waiting to receive those changes.
65  static bool AwaitQuiescence(
66      std::vector<ProfileSyncServiceHarness*>& clients);
67
68  // Returns the ProfileSyncService member of the sync client.
69  ProfileSyncService* service() const { return service_; }
70
71  // Returns the debug name for this profile. Used for logging.
72  const std::string& profile_debug_name() const { return profile_debug_name_; }
73
74  // Enables sync for a particular sync datatype. Returns true on success.
75  bool EnableSyncForDatatype(syncer::ModelType datatype);
76
77  // Disables sync for a particular sync datatype. Returns true on success.
78  bool DisableSyncForDatatype(syncer::ModelType datatype);
79
80  // Enables sync for all sync datatypes. Returns true on success.
81  bool EnableSyncForAllDatatypes();
82
83  // Disables sync for all sync datatypes. Returns true on success.
84  bool DisableSyncForAllDatatypes();
85
86  // Returns a snapshot of the current sync session.
87  syncer::sessions::SyncSessionSnapshot GetLastSessionSnapshot() const;
88
89  // Check if |type| is being synced.
90  bool IsTypePreferred(syncer::ModelType type);
91
92  // Returns a string that can be used as the value of an oauth2 refresh token.
93  // This function guarantees that a different string is returned each time
94  // it is called.
95  std::string GenerateFakeOAuth2RefreshTokenString();
96
97  // Returns a string with relevant info about client's sync state (if
98  // available), annotated with |message|. Useful for logging.
99  std::string GetClientInfoString(const std::string& message) const;
100
101 private:
102  ProfileSyncServiceHarness(
103      Profile* profile,
104      const std::string& username,
105      const std::string& password);
106
107  // Signals that sync setup is complete, and that PSS may begin syncing.
108  void FinishSyncSetup();
109
110  // Gets detailed status from |service_| in pretty-printable form.
111  std::string GetServiceStatus();
112
113  // Returns true if sync is disabled for this client.
114  bool IsSyncDisabled() const;
115
116  // Sync profile associated with this sync client.
117  Profile* profile_;
118
119  // ProfileSyncService object associated with |profile_|.
120  ProfileSyncService* service_;
121
122  // Credentials used for GAIA authentication.
123  std::string username_;
124  std::string password_;
125
126  // Number used by GenerateFakeOAuth2RefreshTokenString() to make sure that
127  // all refresh tokens used in the tests are different.
128  int oauth2_refesh_token_number_;
129
130  // Used for logging.
131  const std::string profile_debug_name_;
132
133  DISALLOW_COPY_AND_ASSIGN(ProfileSyncServiceHarness);
134};
135
136#endif  // CHROME_BROWSER_SYNC_TEST_INTEGRATION_PROFILE_SYNC_SERVICE_HARNESS_H_
137