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 "sync/internal_api/public/base/model_type.h"
14 #include "sync/internal_api/public/sessions/sync_session_snapshot.h"
17 class ProfileSyncService
;
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
{
26 // The type of profile signin method to authenticate a profile.
27 enum class SigninType
{
28 // Fakes user signin process.
30 // Uses UI signin flow and connects to GAIA servers for authentication.
34 static ProfileSyncServiceHarness
* Create(
36 const std::string
& username
,
37 const std::string
& password
,
38 SigninType signin_type
);
39 virtual ~ProfileSyncServiceHarness();
41 // Sets the GAIA credentials with which to sign in to sync.
42 void SetCredentials(const std::string
& username
, const std::string
& password
);
44 // Creates a ProfileSyncService for the profile passed at construction and
45 // enables sync for all available datatypes. Returns true only after sync has
46 // been fully initialized and authenticated, and we are ready to process
50 // Same as the above method, but enables sync only for the datatypes contained
51 // in |synced_datatypes|.
52 bool SetupSync(syncer::ModelTypeSet synced_datatypes
);
54 // Calling this acts as a barrier and blocks the caller until |this| and
55 // |partner| have both completed a sync cycle. When calling this method,
56 // the |partner| should be the passive responder who responds to the actions
57 // of |this|. This method relies upon the synchronization of callbacks
58 // from the message queue. Returns true if two sync cycles have completed.
59 // Note: Use this method when exactly one client makes local change(s), and
60 // exactly one client is waiting to receive those changes.
61 bool AwaitMutualSyncCycleCompletion(ProfileSyncServiceHarness
* partner
);
63 // Blocks the caller until |this| completes its ongoing sync cycle and every
64 // other client in |partners| have achieved identical download progresses.
65 // Note: Use this method when exactly one client makes local change(s),
66 // and more than one client is waiting to receive those changes.
67 bool AwaitGroupSyncCycleCompletion(
68 std::vector
<ProfileSyncServiceHarness
*>& partners
);
70 // Blocks the caller until every client in |clients| completes its ongoing
71 // sync cycle and all the clients' progress markers match. Note: Use this
72 // method when more than one client makes local change(s), and more than one
73 // client is waiting to receive those changes.
74 static bool AwaitQuiescence(
75 std::vector
<ProfileSyncServiceHarness
*>& clients
);
77 // Blocks the caller until the sync backend is initialized or some end state
78 // (e.g., auth error) is reached. Returns true if and only if the backend
79 // initialized successfully. See ProfileSyncService's IsBackendInitialized()
80 // method for the definition of backend initialization.
81 bool AwaitBackendInitialization();
83 // Blocks the caller until sync setup is complete. Returns true if and only
84 // if sync setup completed successfully. See sync_driver::SyncService's
85 // IsSyncActive() method for the definition of what successful means here.
86 bool AwaitSyncSetupCompletion();
88 // Returns the ProfileSyncService member of the sync client.
89 ProfileSyncService
* service() const { return service_
; }
91 // Returns the debug name for this profile. Used for logging.
92 const std::string
& profile_debug_name() const { return profile_debug_name_
; }
94 // Enables sync for a particular sync datatype. Returns true on success.
95 bool EnableSyncForDatatype(syncer::ModelType datatype
);
97 // Disables sync for a particular sync datatype. Returns true on success.
98 bool DisableSyncForDatatype(syncer::ModelType datatype
);
100 // Enables sync for all sync datatypes. Returns true on success.
101 bool EnableSyncForAllDatatypes();
103 // Disables sync for all sync datatypes. Returns true on success.
104 bool DisableSyncForAllDatatypes();
106 // Returns a snapshot of the current sync session.
107 syncer::sessions::SyncSessionSnapshot
GetLastSessionSnapshot() const;
109 // Check if |type| is being synced.
110 bool IsTypePreferred(syncer::ModelType type
);
112 // Returns a string that can be used as the value of an oauth2 refresh token.
113 // This function guarantees that a different string is returned each time
115 std::string
GenerateFakeOAuth2RefreshTokenString();
117 // Returns a string with relevant info about client's sync state (if
118 // available), annotated with |message|. Useful for logging.
119 std::string
GetClientInfoString(const std::string
& message
) const;
122 ProfileSyncServiceHarness(
124 const std::string
& username
,
125 const std::string
& password
,
126 SigninType signin_type
);
128 // Signals that sync setup is complete, and that PSS may begin syncing.
129 void FinishSyncSetup();
131 // Gets detailed status from |service_| in pretty-printable form.
132 std::string
GetServiceStatus();
134 // Returns true if sync is disabled for this client.
135 bool IsSyncDisabled() const;
137 // Sync profile associated with this sync client.
140 // ProfileSyncService object associated with |profile_|.
141 ProfileSyncService
* service_
;
143 // Credentials used for GAIA authentication.
144 std::string username_
;
145 std::string password_
;
147 // Used to decide what method of profile signin to use.
148 const SigninType signin_type_
;
150 // Number used by GenerateFakeOAuth2RefreshTokenString() to make sure that
151 // all refresh tokens used in the tests are different.
152 int oauth2_refesh_token_number_
;
155 const std::string profile_debug_name_
;
157 DISALLOW_COPY_AND_ASSIGN(ProfileSyncServiceHarness
);
160 #endif // CHROME_BROWSER_SYNC_TEST_INTEGRATION_PROFILE_SYNC_SERVICE_HARNESS_H_