Update To 11.40.268.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 "sync/internal_api/public/base/model_type.h"
14 #include "sync/internal_api/public/sessions/sync_session_snapshot.h"
15
16 class Profile;
17 class 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.
24 class 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   // Blocks the caller until the sync backend is initialized or some end state
69   // (e.g., auth error) is reached. Returns true if and only if the backend
70   // initialized successfully. See ProfileSyncService's backend_initialized()
71   // method for the definition of backend initialization.
72   bool AwaitBackendInitialization();
73
74   // Blocks the caller until sync setup is complete. Returns true if and only
75   // if sync setup completed successfully. See ProfileSyncServiceBase's
76   // SyncActive() method for the definition of what successful means here.
77   bool AwaitSyncSetupCompletion();
78
79   // Returns the ProfileSyncService member of the sync client.
80   ProfileSyncService* service() const { return service_; }
81
82   // Returns the debug name for this profile. Used for logging.
83   const std::string& profile_debug_name() const { return profile_debug_name_; }
84
85   // Enables sync for a particular sync datatype. Returns true on success.
86   bool EnableSyncForDatatype(syncer::ModelType datatype);
87
88   // Disables sync for a particular sync datatype. Returns true on success.
89   bool DisableSyncForDatatype(syncer::ModelType datatype);
90
91   // Enables sync for all sync datatypes. Returns true on success.
92   bool EnableSyncForAllDatatypes();
93
94   // Disables sync for all sync datatypes. Returns true on success.
95   bool DisableSyncForAllDatatypes();
96
97   // Returns a snapshot of the current sync session.
98   syncer::sessions::SyncSessionSnapshot GetLastSessionSnapshot() const;
99
100   // Check if |type| is being synced.
101   bool IsTypePreferred(syncer::ModelType type);
102
103   // Returns a string that can be used as the value of an oauth2 refresh token.
104   // This function guarantees that a different string is returned each time
105   // it is called.
106   std::string GenerateFakeOAuth2RefreshTokenString();
107
108   // Returns a string with relevant info about client's sync state (if
109   // available), annotated with |message|. Useful for logging.
110   std::string GetClientInfoString(const std::string& message) const;
111
112  private:
113   ProfileSyncServiceHarness(
114       Profile* profile,
115       const std::string& username,
116       const std::string& password);
117
118   // Signals that sync setup is complete, and that PSS may begin syncing.
119   void FinishSyncSetup();
120
121   // Gets detailed status from |service_| in pretty-printable form.
122   std::string GetServiceStatus();
123
124   // Returns true if sync is disabled for this client.
125   bool IsSyncDisabled() const;
126
127   // Sync profile associated with this sync client.
128   Profile* profile_;
129
130   // ProfileSyncService object associated with |profile_|.
131   ProfileSyncService* service_;
132
133   // Credentials used for GAIA authentication.
134   std::string username_;
135   std::string password_;
136
137   // Number used by GenerateFakeOAuth2RefreshTokenString() to make sure that
138   // all refresh tokens used in the tests are different.
139   int oauth2_refesh_token_number_;
140
141   // Used for logging.
142   const std::string profile_debug_name_;
143
144   DISALLOW_COPY_AND_ASSIGN(ProfileSyncServiceHarness);
145 };
146
147 #endif  // CHROME_BROWSER_SYNC_TEST_INTEGRATION_PROFILE_SYNC_SERVICE_HARNESS_H_