blob: 8a00515b18ce27b8ce7bc2f24370bd10a8fe476a [file] [log] [blame]
[email protected]72a31b42010-02-17 22:26:331// Copyright (c) 2010 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_PROFILE_SYNC_SERVICE_H_
6#define CHROME_BROWSER_SYNC_TEST_PROFILE_SYNC_SERVICE_H_
[email protected]32b76ef2010-07-26 23:08:247#pragma once
[email protected]72a31b42010-02-17 22:26:338
9#include <string>
10
11#include "base/message_loop.h"
12#include "chrome/browser/profile.h"
[email protected]18af9a22010-08-11 00:47:1913#include "chrome/browser/sync/engine/syncapi.h"
[email protected]e3e43d92010-02-26 22:02:3814#include "chrome/browser/sync/profile_sync_factory.h"
[email protected]72a31b42010-02-17 22:26:3315#include "chrome/browser/sync/profile_sync_service.h"
[email protected]7cef1c442010-07-07 17:05:2216#include "chrome/browser/sync/glue/data_type_controller.h"
17#include "chrome/browser/sync/glue/data_type_manager_impl.h"
18#include "chrome/browser/sync/glue/sync_backend_host.h"
[email protected]18af9a22010-08-11 00:47:1919#include "chrome/browser/sync/sessions/session_state.h"
20#include "chrome/browser/sync/syncable/directory_manager.h"
21#include "chrome/browser/sync/syncable/syncable.h"
[email protected]7cef1c442010-07-07 17:05:2222#include "chrome/common/notification_service.h"
[email protected]18af9a22010-08-11 00:47:1923#include "chrome/test/profile_mock.h"
[email protected]72a31b42010-02-17 22:26:3324#include "chrome/test/sync/test_http_bridge_factory.h"
[email protected]7cef1c442010-07-07 17:05:2225#include "testing/gmock/include/gmock/gmock.h"
26
[email protected]18af9a22010-08-11 00:47:1927using browser_sync::ModelSafeRoutingInfo;
28using browser_sync::sessions::ErrorCounters;
29using browser_sync::sessions::SyncerStatus;
30using browser_sync::sessions::SyncSessionSnapshot;
31using sync_api::UserShare;
32using syncable::DirectoryManager;
33using syncable::ModelType;
34using syncable::ScopedDirLookup;
35
[email protected]7cef1c442010-07-07 17:05:2236ACTION_P(CallOnPaused, core) {
37 core->OnPaused();
38};
39
40ACTION_P(CallOnResumed, core) {
41 core->OnResumed();
42}
43
44ACTION(ReturnNewDataTypeManager) {
45 return new browser_sync::DataTypeManagerImpl(arg0, arg1);
46}
47
[email protected]18af9a22010-08-11 00:47:1948namespace browser_sync {
49
[email protected]7cef1c442010-07-07 17:05:2250// Mocks out the SyncerThread operations (Pause/Resume) since no thread is
51// running in these tests, and allows tests to provide a task on construction
52// to set up initial nodes to mock out an actual server initial sync
53// download.
[email protected]18af9a22010-08-11 00:47:1954class SyncBackendHostForProfileSyncTest : public SyncBackendHost {
[email protected]7cef1c442010-07-07 17:05:2255 public:
[email protected]18af9a22010-08-11 00:47:1956 // |initial_condition_setup_task| can be used to populate nodes before the
57 // OnBackendInitialized callback fires.
58 // |set_initial_sync_ended_on_init| determines whether we pretend that a full
59 // initial download has occurred and set bits for enabled data types. If
60 // this is false, configuring data types will require a syncer nudge.
61 // |synchronous_init| causes initialization to block until the syncapi has
62 // completed setting itself up and called us back.
63 SyncBackendHostForProfileSyncTest(SyncFrontend* frontend,
[email protected]7cef1c442010-07-07 17:05:2264 Profile* profile,
65 const FilePath& profile_path,
[email protected]18af9a22010-08-11 00:47:1966 const DataTypeController::TypeMap& data_type_controllers,
[email protected]7cef1c442010-07-07 17:05:2267 Task* initial_condition_setup_task,
68 int num_expected_resumes,
[email protected]18af9a22010-08-11 00:47:1969 int num_expected_pauses,
70 bool set_initial_sync_ended_on_init,
71 bool synchronous_init)
[email protected]7cef1c442010-07-07 17:05:2272 : browser_sync::SyncBackendHost(frontend, profile, profile_path,
73 data_type_controllers),
[email protected]18af9a22010-08-11 00:47:1974 initial_condition_setup_task_(initial_condition_setup_task),
75 set_initial_sync_ended_on_init_(set_initial_sync_ended_on_init),
76 synchronous_init_(synchronous_init) {
[email protected]7cef1c442010-07-07 17:05:2277 // By default, the RequestPause and RequestResume methods will
78 // send the confirmation notification and return true.
79 ON_CALL(*this, RequestPause()).
80 WillByDefault(testing::DoAll(CallOnPaused(core_),
81 testing::Return(true)));
82 ON_CALL(*this, RequestResume()).
83 WillByDefault(testing::DoAll(CallOnResumed(core_),
84 testing::Return(true)));
[email protected]18af9a22010-08-11 00:47:1985 ON_CALL(*this, RequestNudge()).WillByDefault(testing::Invoke(this,
86 &SyncBackendHostForProfileSyncTest::
87 SimulateSyncCycleCompletedInitialSyncEnded));
88
[email protected]7cef1c442010-07-07 17:05:2289 EXPECT_CALL(*this, RequestPause()).Times(num_expected_pauses);
90 EXPECT_CALL(*this, RequestResume()).Times(num_expected_resumes);
[email protected]18af9a22010-08-11 00:47:1991 EXPECT_CALL(*this, RequestNudge()).
92 Times(set_initial_sync_ended_on_init ? 0 : 1);
[email protected]7cef1c442010-07-07 17:05:2293 }
94
95 MOCK_METHOD0(RequestPause, bool());
96 MOCK_METHOD0(RequestResume, bool());
[email protected]18af9a22010-08-11 00:47:1997 MOCK_METHOD0(RequestNudge, void());
98
99 void SetInitialSyncEndedForEnabledTypes() {
100 UserShare* user_share = core_->syncapi()->GetUserShare();
101 DirectoryManager* dir_manager = user_share->dir_manager.get();
102
[email protected]265d8c322010-09-08 01:38:35103 ScopedDirLookup dir(dir_manager, user_share->authenticated_name);
[email protected]18af9a22010-08-11 00:47:19104 if (!dir.good())
105 FAIL();
106
107 ModelSafeRoutingInfo enabled_types;
108 GetModelSafeRoutingInfo(&enabled_types);
109 for (ModelSafeRoutingInfo::const_iterator i = enabled_types.begin();
110 i != enabled_types.end(); ++i) {
111 dir->set_initial_sync_ended_for_type(i->first, true);
112 }
113 }
[email protected]7cef1c442010-07-07 17:05:22114
115 virtual void HandleInitializationCompletedOnFrontendLoop() {
[email protected]18af9a22010-08-11 00:47:19116 set_syncapi_initialized(); // Need to do this asap so task below works.
117
118 // Set up any nodes the test wants around before model association.
[email protected]7cef1c442010-07-07 17:05:22119 if (initial_condition_setup_task_) {
120 initial_condition_setup_task_->Run();
121 }
122
[email protected]18af9a22010-08-11 00:47:19123 // Pretend we downloaded initial updates and set initial sync ended bits
124 // if we were asked to.
125 if (set_initial_sync_ended_on_init_)
126 SetInitialSyncEndedForEnabledTypes();
127
[email protected]7cef1c442010-07-07 17:05:22128 SyncBackendHost::HandleInitializationCompletedOnFrontendLoop();
129 }
[email protected]18af9a22010-08-11 00:47:19130
131 // Called when a nudge comes in.
132 void SimulateSyncCycleCompletedInitialSyncEnded() {
133 syncable::ModelTypeBitSet sync_ended;
134 ModelSafeRoutingInfo enabled_types;
135 GetModelSafeRoutingInfo(&enabled_types);
136 for (ModelSafeRoutingInfo::const_iterator i = enabled_types.begin();
137 i != enabled_types.end(); ++i) {
138 sync_ended.set(i->first);
139 }
140 core_->HandleSyncCycleCompletedOnFrontendLoop(new SyncSessionSnapshot(
141 SyncerStatus(), ErrorCounters(), 0, 0, false,
142 sync_ended, false, false, 0, 0, false));
143 }
144
145 virtual sync_api::HttpPostProviderFactory* MakeHttpBridgeFactory(
146 URLRequestContextGetter* getter) {
147 return new browser_sync::TestHttpBridgeFactory;
148 }
149
150 virtual void InitCore(const Core::DoInitializeOptions& options) {
151 std::wstring user = L"testuser";
152 core_loop()->PostTask(FROM_HERE,
153 NewRunnableMethod(core_.get(),
154 &SyncBackendHost::Core::DoInitializeForTest,
155 user,
156 options.http_bridge_factory,
[email protected]265d8c322010-09-08 01:38:35157 options.auth_http_bridge_factory,
[email protected]33cd0042010-09-08 19:25:28158 options.delete_sync_data_folder));
[email protected]18af9a22010-08-11 00:47:19159
160 // TODO(akalin): Figure out a better way to do this.
161 if (synchronous_init_) {
162 // The SyncBackend posts a task to the current loop when
163 // initialization completes.
164 MessageLoop::current()->Run();
165 }
166 }
167
168 static void SetDefaultExpectationsForWorkerCreation(ProfileMock* profile) {
169 EXPECT_CALL(*profile, GetPasswordStore(testing::_)).
170 WillOnce(testing::Return((PasswordStore*)NULL));
171 EXPECT_CALL(*profile, GetHistoryService(testing::_)).
172 WillOnce(testing::Return((HistoryService*)NULL));
173 }
174
[email protected]7cef1c442010-07-07 17:05:22175 private:
176 Task* initial_condition_setup_task_;
[email protected]18af9a22010-08-11 00:47:19177 bool set_initial_sync_ended_on_init_;
178 bool synchronous_init_;
[email protected]7cef1c442010-07-07 17:05:22179
180};
[email protected]72a31b42010-02-17 22:26:33181
[email protected]18af9a22010-08-11 00:47:19182} // namespace browser_sync
183
[email protected]72a31b42010-02-17 22:26:33184class TestProfileSyncService : public ProfileSyncService {
185 public:
[email protected]7cef1c442010-07-07 17:05:22186 TestProfileSyncService(ProfileSyncFactory* factory,
187 Profile* profile,
[email protected]265d8c322010-09-08 01:38:35188 bool bootstrap_sync_authentication,
[email protected]7cef1c442010-07-07 17:05:22189 bool synchronous_backend_initialization,
190 Task* initial_condition_setup_task)
[email protected]265d8c322010-09-08 01:38:35191 : ProfileSyncService(factory, profile, bootstrap_sync_authentication),
[email protected]4c217582010-05-13 21:37:14192 synchronous_backend_initialization_(
[email protected]7cef1c442010-07-07 17:05:22193 synchronous_backend_initialization),
194 synchronous_sync_configuration_(false),
195 num_expected_resumes_(1),
196 num_expected_pauses_(1),
[email protected]18af9a22010-08-11 00:47:19197 initial_condition_setup_task_(initial_condition_setup_task),
198 set_initial_sync_ended_on_init_(true) {
[email protected]72a31b42010-02-17 22:26:33199 RegisterPreferences();
200 SetSyncSetupCompleted();
201 }
[email protected]66761b952010-06-25 21:30:38202 virtual ~TestProfileSyncService() { }
[email protected]72a31b42010-02-17 22:26:33203
[email protected]18af9a22010-08-11 00:47:19204 virtual void CreateBackend() {
205 backend_.reset(new browser_sync::SyncBackendHostForProfileSyncTest(
206 this, profile(),
[email protected]7cef1c442010-07-07 17:05:22207 profile()->GetPath(), data_type_controllers(),
208 initial_condition_setup_task_.release(),
[email protected]18af9a22010-08-11 00:47:19209 num_expected_resumes_, num_expected_pauses_,
210 set_initial_sync_ended_on_init_,
211 synchronous_backend_initialization_));
[email protected]72a31b42010-02-17 22:26:33212 }
213
214 virtual void OnBackendInitialized() {
215 ProfileSyncService::OnBackendInitialized();
[email protected]4c217582010-05-13 21:37:14216 // TODO(akalin): Figure out a better way to do this.
217 if (synchronous_backend_initialization_) {
218 MessageLoop::current()->Quit();
219 }
[email protected]72a31b42010-02-17 22:26:33220 }
221
[email protected]7cef1c442010-07-07 17:05:22222 virtual void Observe(NotificationType type,
223 const NotificationSource& source,
224 const NotificationDetails& details) {
225 ProfileSyncService::Observe(type, source, details);
226 if (type == NotificationType::SYNC_CONFIGURE_DONE &&
227 !synchronous_sync_configuration_) {
228 MessageLoop::current()->Quit();
229 }
230 }
231
232 void set_num_expected_resumes(int times) {
233 num_expected_resumes_ = times;
234 }
235 void set_num_expected_pauses(int num) {
236 num_expected_pauses_ = num;
237 }
[email protected]18af9a22010-08-11 00:47:19238 void dont_set_initial_sync_ended_on_init() {
239 set_initial_sync_ended_on_init_ = false;
240 }
[email protected]7cef1c442010-07-07 17:05:22241 void set_synchronous_sync_configuration() {
242 synchronous_sync_configuration_ = true;
243 }
244
[email protected]72a31b42010-02-17 22:26:33245 private:
246 // When testing under ChromiumOS, this method must not return an empty
247 // value value in order for the profile sync service to start.
248 virtual std::string GetLsidForAuthBootstraping() {
249 return "foo";
250 }
[email protected]4c217582010-05-13 21:37:14251
252 bool synchronous_backend_initialization_;
[email protected]7cef1c442010-07-07 17:05:22253
254 // Set to true when a mock data type manager is being used and the configure
255 // step is performed synchronously.
256 bool synchronous_sync_configuration_;
257 bool set_expect_resume_expectations_;
258 int num_expected_resumes_;
259 int num_expected_pauses_;
260
261 scoped_ptr<Task> initial_condition_setup_task_;
[email protected]18af9a22010-08-11 00:47:19262 bool set_initial_sync_ended_on_init_;
[email protected]72a31b42010-02-17 22:26:33263};
264
265#endif // CHROME_BROWSER_SYNC_TEST_PROFILE_SYNC_SERVICE_H_