OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include "base/basictypes.h" |
| 6 #include "base/compiler_specific.h" |
5 #include "base/file_util.h" | 7 #include "base/file_util.h" |
6 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
7 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
8 #include "base/values.h" | 10 #include "base/values.h" |
9 #include "chrome/browser/signin/signin_manager.h" | 11 #include "chrome/browser/signin/signin_manager.h" |
10 #include "chrome/browser/signin/signin_manager_factory.h" | 12 #include "chrome/browser/signin/signin_manager_factory.h" |
11 #include "chrome/browser/signin/token_service.h" | 13 #include "chrome/browser/signin/token_service.h" |
12 #include "chrome/browser/signin/token_service_factory.h" | 14 #include "chrome/browser/signin/token_service_factory.h" |
13 #include "chrome/browser/sync/glue/bookmark_data_type_controller.h" | 15 #include "chrome/browser/sync/glue/bookmark_data_type_controller.h" |
14 #include "chrome/browser/sync/glue/data_type_controller.h" | 16 #include "chrome/browser/sync/glue/data_type_controller.h" |
15 #include "chrome/browser/sync/profile_sync_components_factory_mock.h" | 17 #include "chrome/browser/sync/profile_sync_components_factory_mock.h" |
16 #include "chrome/browser/sync/test_profile_sync_service.h" | 18 #include "chrome/browser/sync/test_profile_sync_service.h" |
17 #include "chrome/common/chrome_version_info.h" | 19 #include "chrome/common/chrome_version_info.h" |
18 #include "chrome/common/net/gaia/gaia_constants.h" | 20 #include "chrome/common/net/gaia/gaia_constants.h" |
19 #include "chrome/common/pref_names.h" | 21 #include "chrome/common/pref_names.h" |
20 #include "chrome/test/base/testing_pref_service.h" | 22 #include "chrome/test/base/testing_pref_service.h" |
21 #include "chrome/test/base/testing_profile.h" | 23 #include "chrome/test/base/testing_profile.h" |
22 #include "content/public/common/content_client.h" | 24 #include "content/public/common/content_client.h" |
23 #include "content/public/test/test_browser_thread.h" | 25 #include "content/public/test/test_browser_thread.h" |
24 #include "google/cacheinvalidation/include/types.h" | 26 #include "google/cacheinvalidation/include/types.h" |
25 #include "sync/js/js_arg_list.h" | 27 #include "sync/js/js_arg_list.h" |
26 #include "sync/js/js_event_details.h" | 28 #include "sync/js/js_event_details.h" |
27 #include "sync/js/js_test_util.h" | 29 #include "sync/js/js_test_util.h" |
28 #include "sync/notifier/fake_invalidation_handler.h" | 30 #include "sync/notifier/fake_invalidation_handler.h" |
| 31 #include "sync/notifier/invalidator.h" |
| 32 #include "sync/notifier/invalidator_test_template.h" |
29 #include "sync/notifier/object_id_state_map_test_util.h" | 33 #include "sync/notifier/object_id_state_map_test_util.h" |
30 #include "testing/gmock/include/gmock/gmock.h" | 34 #include "testing/gmock/include/gmock/gmock.h" |
31 #include "testing/gtest/include/gtest/gtest.h" | 35 #include "testing/gtest/include/gtest/gtest.h" |
32 #include "webkit/glue/webkit_glue.h" | 36 #include "webkit/glue/webkit_glue.h" |
33 | 37 |
34 // TODO(akalin): Add tests here that exercise the whole | 38 // TODO(akalin): Add tests here that exercise the whole |
35 // ProfileSyncService/SyncBackendHost stack while mocking out as | 39 // ProfileSyncService/SyncBackendHost stack while mocking out as |
36 // little as possible. | 40 // little as possible. |
37 | 41 |
38 namespace browser_sync { | 42 namespace browser_sync { |
39 | 43 |
40 namespace { | 44 namespace { |
41 | 45 |
42 using content::BrowserThread; | 46 using content::BrowserThread; |
43 using testing::_; | 47 using testing::_; |
44 using testing::AtLeast; | 48 using testing::AtLeast; |
45 using testing::AtMost; | 49 using testing::AtMost; |
46 using testing::Mock; | 50 using testing::Mock; |
47 using testing::Return; | 51 using testing::Return; |
48 using testing::StrictMock; | 52 using testing::StrictMock; |
49 | 53 |
50 class ProfileSyncServiceTest : public testing::Test { | 54 class ProfileSyncServiceTestHarness { |
51 protected: | 55 public: |
52 ProfileSyncServiceTest() | 56 ProfileSyncServiceTestHarness() |
53 : ui_thread_(BrowserThread::UI, &ui_loop_), | 57 : ui_thread_(BrowserThread::UI, &ui_loop_), |
54 db_thread_(BrowserThread::DB), | 58 db_thread_(BrowserThread::DB), |
55 file_thread_(BrowserThread::FILE), | 59 file_thread_(BrowserThread::FILE), |
56 io_thread_(BrowserThread::IO) {} | 60 io_thread_(BrowserThread::IO) {} |
57 | 61 |
58 virtual ~ProfileSyncServiceTest() {} | 62 ~ProfileSyncServiceTestHarness() {} |
59 | 63 |
60 virtual void SetUp() { | 64 void SetUp() { |
61 file_thread_.Start(); | 65 file_thread_.Start(); |
62 io_thread_.StartIOThread(); | 66 io_thread_.StartIOThread(); |
63 profile_.reset(new TestingProfile()); | 67 profile.reset(new TestingProfile()); |
64 profile_->CreateRequestContext(); | 68 profile->CreateRequestContext(); |
65 | 69 |
66 // We need to set the user agent before the backend host can call | 70 // We need to set the user agent before the backend host can call |
67 // webkit_glue::GetUserAgent(). | 71 // webkit_glue::GetUserAgent(). |
68 webkit_glue::SetUserAgent(content::GetContentClient()->GetUserAgent(), | 72 webkit_glue::SetUserAgent(content::GetContentClient()->GetUserAgent(), |
69 false); | 73 false); |
70 } | 74 } |
71 | 75 |
72 virtual void TearDown() { | 76 void TearDown() { |
73 // Kill the service before the profile. | 77 // Kill the service before the profile. |
74 service_.reset(); | 78 service.reset(); |
75 profile_.reset(); | 79 profile.reset(); |
76 // Pump messages posted by the sync core thread (which may end up | 80 // Pump messages posted by the sync thread (which may end up |
77 // posting on the IO thread). | 81 // posting on the IO thread). |
78 ui_loop_.RunAllPending(); | 82 ui_loop_.RunAllPending(); |
79 io_thread_.Stop(); | 83 io_thread_.Stop(); |
80 file_thread_.Stop(); | 84 file_thread_.Stop(); |
81 // Ensure that the sync objects destruct to avoid memory leaks. | 85 // Ensure that the sync objects destruct to avoid memory leaks. |
82 ui_loop_.RunAllPending(); | 86 ui_loop_.RunAllPending(); |
83 } | 87 } |
84 | 88 |
85 // TODO(akalin): Refactor the StartSyncService*() functions below. | 89 // TODO(akalin): Refactor the StartSyncService*() functions below. |
86 | 90 |
87 void StartSyncService() { | 91 void StartSyncService() { |
88 StartSyncServiceAndSetInitialSyncEnded( | 92 StartSyncServiceAndSetInitialSyncEnded( |
89 true, true, false, true, syncer::STORAGE_IN_MEMORY); | 93 true, true, false, true, syncer::STORAGE_IN_MEMORY); |
90 } | 94 } |
91 | 95 |
92 void StartSyncServiceAndSetInitialSyncEnded( | 96 void StartSyncServiceAndSetInitialSyncEnded( |
93 bool set_initial_sync_ended, | 97 bool set_initial_sync_ended, |
94 bool issue_auth_token, | 98 bool issue_auth_token, |
95 bool synchronous_sync_configuration, | 99 bool synchronous_sync_configuration, |
96 bool sync_setup_completed, | 100 bool sync_setup_completed, |
97 syncer::StorageOption storage_option) { | 101 syncer::StorageOption storage_option) { |
98 if (!service_.get()) { | 102 if (!service.get()) { |
99 SigninManager* signin = | 103 SigninManager* signin = |
100 SigninManagerFactory::GetForProfile(profile_.get()); | 104 SigninManagerFactory::GetForProfile(profile.get()); |
101 signin->SetAuthenticatedUsername("test"); | 105 signin->SetAuthenticatedUsername("test"); |
102 ProfileSyncComponentsFactoryMock* factory = | 106 ProfileSyncComponentsFactoryMock* factory = |
103 new ProfileSyncComponentsFactoryMock(); | 107 new ProfileSyncComponentsFactoryMock(); |
104 service_.reset(new TestProfileSyncService( | 108 service.reset(new TestProfileSyncService( |
105 factory, | 109 factory, |
106 profile_.get(), | 110 profile.get(), |
107 signin, | 111 signin, |
108 ProfileSyncService::AUTO_START, | 112 ProfileSyncService::AUTO_START, |
109 true, | 113 true, |
110 base::Closure())); | 114 base::Closure())); |
111 if (!set_initial_sync_ended) | 115 if (!set_initial_sync_ended) |
112 service_->dont_set_initial_sync_ended_on_init(); | 116 service->dont_set_initial_sync_ended_on_init(); |
113 if (synchronous_sync_configuration) | 117 if (synchronous_sync_configuration) |
114 service_->set_synchronous_sync_configuration(); | 118 service->set_synchronous_sync_configuration(); |
115 service_->set_storage_option(storage_option); | 119 service->set_storage_option(storage_option); |
116 if (!sync_setup_completed) | 120 if (!sync_setup_completed) |
117 profile_->GetPrefs()->SetBoolean(prefs::kSyncHasSetupCompleted, false); | 121 profile->GetPrefs()->SetBoolean(prefs::kSyncHasSetupCompleted, false); |
118 | 122 |
119 // Register the bookmark data type. | 123 // Register the bookmark data type. |
120 ON_CALL(*factory, CreateDataTypeManager(_, _, _)). | 124 ON_CALL(*factory, CreateDataTypeManager(_, _, _)). |
121 WillByDefault(ReturnNewDataTypeManager()); | 125 WillByDefault(ReturnNewDataTypeManager()); |
122 | 126 |
123 if (issue_auth_token) { | 127 if (issue_auth_token) { |
124 IssueTestTokens(); | 128 IssueTestTokens(); |
125 } | 129 } |
126 service_->Initialize(); | 130 service->Initialize(); |
127 } | 131 } |
128 } | 132 } |
129 | 133 |
130 void IssueTestTokens() { | 134 void IssueTestTokens() { |
131 TokenService* token_service = | 135 TokenService* token_service = |
132 TokenServiceFactory::GetForProfile(profile_.get()); | 136 TokenServiceFactory::GetForProfile(profile.get()); |
133 token_service->IssueAuthTokenForTest( | 137 token_service->IssueAuthTokenForTest( |
134 GaiaConstants::kSyncService, "token1"); | 138 GaiaConstants::kSyncService, "token1"); |
135 token_service->IssueAuthTokenForTest( | 139 token_service->IssueAuthTokenForTest( |
136 GaiaConstants::kGaiaOAuth2LoginRefreshToken, "token2"); | 140 GaiaConstants::kGaiaOAuth2LoginRefreshToken, "token2"); |
137 } | 141 } |
138 | 142 |
| 143 scoped_ptr<TestProfileSyncService> service; |
| 144 scoped_ptr<TestingProfile> profile; |
| 145 |
| 146 private: |
139 MessageLoop ui_loop_; | 147 MessageLoop ui_loop_; |
140 // Needed by |service_|. | 148 // Needed by |service|. |
141 content::TestBrowserThread ui_thread_; | 149 content::TestBrowserThread ui_thread_; |
142 content::TestBrowserThread db_thread_; | 150 content::TestBrowserThread db_thread_; |
143 // Needed by DisableAndEnableSyncTemporarily test case. | 151 // Needed by DisableAndEnableSyncTemporarily test case. |
144 content::TestBrowserThread file_thread_; | 152 content::TestBrowserThread file_thread_; |
145 // Needed by |service| and |profile_|'s request context. | 153 // Needed by |service| and |profile|'s request context. |
146 content::TestBrowserThread io_thread_; | 154 content::TestBrowserThread io_thread_; |
| 155 }; |
147 | 156 |
148 scoped_ptr<TestProfileSyncService> service_; | 157 class ProfileSyncServiceTest : public testing::Test { |
149 scoped_ptr<TestingProfile> profile_; | 158 protected: |
| 159 virtual void SetUp() { |
| 160 harness_.SetUp(); |
| 161 } |
| 162 |
| 163 virtual void TearDown() { |
| 164 harness_.TearDown(); |
| 165 } |
| 166 |
| 167 ProfileSyncServiceTestHarness harness_; |
150 }; | 168 }; |
151 | 169 |
152 TEST_F(ProfileSyncServiceTest, InitialState) { | 170 TEST_F(ProfileSyncServiceTest, InitialState) { |
153 SigninManager* signin = SigninManagerFactory::GetForProfile(profile_.get()); | 171 SigninManager* signin = |
154 service_.reset(new TestProfileSyncService( | 172 SigninManagerFactory::GetForProfile(harness_.profile.get()); |
| 173 harness_.service.reset(new TestProfileSyncService( |
155 new ProfileSyncComponentsFactoryMock(), | 174 new ProfileSyncComponentsFactoryMock(), |
156 profile_.get(), | 175 harness_.profile.get(), |
157 signin, | 176 signin, |
158 ProfileSyncService::MANUAL_START, | 177 ProfileSyncService::MANUAL_START, |
159 true, | 178 true, |
160 base::Closure())); | 179 base::Closure())); |
161 EXPECT_TRUE( | 180 EXPECT_TRUE( |
162 service_->sync_service_url().spec() == | 181 harness_.service->sync_service_url().spec() == |
163 ProfileSyncService::kSyncServerUrl || | 182 ProfileSyncService::kSyncServerUrl || |
164 service_->sync_service_url().spec() == | 183 harness_.service->sync_service_url().spec() == |
165 ProfileSyncService::kDevServerUrl); | 184 ProfileSyncService::kDevServerUrl); |
166 } | 185 } |
167 | 186 |
168 TEST_F(ProfileSyncServiceTest, DisabledByPolicy) { | 187 TEST_F(ProfileSyncServiceTest, DisabledByPolicy) { |
169 profile_->GetTestingPrefService()->SetManagedPref( | 188 harness_.profile->GetTestingPrefService()->SetManagedPref( |
170 prefs::kSyncManaged, | 189 prefs::kSyncManaged, |
171 Value::CreateBooleanValue(true)); | 190 Value::CreateBooleanValue(true)); |
172 SigninManager* signin = SigninManagerFactory::GetForProfile(profile_.get()); | 191 SigninManager* signin = |
173 service_.reset(new TestProfileSyncService( | 192 SigninManagerFactory::GetForProfile(harness_.profile.get()); |
| 193 harness_.service.reset(new TestProfileSyncService( |
174 new ProfileSyncComponentsFactoryMock(), | 194 new ProfileSyncComponentsFactoryMock(), |
175 profile_.get(), | 195 harness_.profile.get(), |
176 signin, | 196 signin, |
177 ProfileSyncService::MANUAL_START, | 197 ProfileSyncService::MANUAL_START, |
178 true, | 198 true, |
179 base::Closure())); | 199 base::Closure())); |
180 service_->Initialize(); | 200 harness_.service->Initialize(); |
181 EXPECT_TRUE(service_->IsManaged()); | 201 EXPECT_TRUE(harness_.service->IsManaged()); |
182 } | 202 } |
183 | 203 |
184 TEST_F(ProfileSyncServiceTest, AbortedByShutdown) { | 204 TEST_F(ProfileSyncServiceTest, AbortedByShutdown) { |
185 SigninManager* signin = SigninManagerFactory::GetForProfile(profile_.get()); | 205 SigninManager* signin = |
| 206 SigninManagerFactory::GetForProfile(harness_.profile.get()); |
186 signin->SetAuthenticatedUsername("test"); | 207 signin->SetAuthenticatedUsername("test"); |
187 ProfileSyncComponentsFactoryMock* factory = | 208 ProfileSyncComponentsFactoryMock* factory = |
188 new ProfileSyncComponentsFactoryMock(); | 209 new ProfileSyncComponentsFactoryMock(); |
189 service_.reset(new TestProfileSyncService( | 210 harness_.service.reset(new TestProfileSyncService( |
190 factory, | 211 factory, |
191 profile_.get(), | 212 harness_.profile.get(), |
192 signin, | 213 signin, |
193 ProfileSyncService::AUTO_START, | 214 ProfileSyncService::AUTO_START, |
194 true, | 215 true, |
195 base::Closure())); | 216 base::Closure())); |
196 EXPECT_CALL(*factory, CreateDataTypeManager(_, _, _)).Times(0); | 217 EXPECT_CALL(*factory, CreateDataTypeManager(_, _, _)).Times(0); |
197 EXPECT_CALL(*factory, CreateBookmarkSyncComponents(_, _)). | 218 EXPECT_CALL(*factory, CreateBookmarkSyncComponents(_, _)). |
198 Times(0); | 219 Times(0); |
199 service_->RegisterDataTypeController( | 220 harness_.service->RegisterDataTypeController( |
200 new BookmarkDataTypeController(service_->factory(), | 221 new BookmarkDataTypeController(harness_.service->factory(), |
201 profile_.get(), | 222 harness_.profile.get(), |
202 service_.get())); | 223 harness_.service.get())); |
203 | 224 |
204 service_->Initialize(); | 225 harness_.service->Initialize(); |
205 service_.reset(); | 226 harness_.service.reset(); |
206 } | 227 } |
207 | 228 |
208 TEST_F(ProfileSyncServiceTest, DisableAndEnableSyncTemporarily) { | 229 TEST_F(ProfileSyncServiceTest, DisableAndEnableSyncTemporarily) { |
209 SigninManager* signin = SigninManagerFactory::GetForProfile(profile_.get()); | 230 SigninManager* signin = |
| 231 SigninManagerFactory::GetForProfile(harness_.profile.get()); |
210 signin->SetAuthenticatedUsername("test"); | 232 signin->SetAuthenticatedUsername("test"); |
211 ProfileSyncComponentsFactoryMock* factory = | 233 ProfileSyncComponentsFactoryMock* factory = |
212 new ProfileSyncComponentsFactoryMock(); | 234 new ProfileSyncComponentsFactoryMock(); |
213 service_.reset(new TestProfileSyncService( | 235 harness_.service.reset(new TestProfileSyncService( |
214 factory, | 236 factory, |
215 profile_.get(), | 237 harness_.profile.get(), |
216 signin, | 238 signin, |
217 ProfileSyncService::AUTO_START, | 239 ProfileSyncService::AUTO_START, |
218 true, | 240 true, |
219 base::Closure())); | 241 base::Closure())); |
220 // Register the bookmark data type. | 242 // Register the bookmark data type. |
221 EXPECT_CALL(*factory, CreateDataTypeManager(_, _, _)). | 243 EXPECT_CALL(*factory, CreateDataTypeManager(_, _, _)). |
222 WillRepeatedly(ReturnNewDataTypeManager()); | 244 WillRepeatedly(ReturnNewDataTypeManager()); |
223 | 245 |
224 IssueTestTokens(); | 246 harness_.IssueTestTokens(); |
225 | 247 |
226 service_->Initialize(); | 248 harness_.service->Initialize(); |
227 EXPECT_TRUE(service_->sync_initialized()); | 249 EXPECT_TRUE(harness_.service->sync_initialized()); |
228 EXPECT_TRUE(service_->GetBackendForTest() != NULL); | 250 EXPECT_TRUE(harness_.service->GetBackendForTest() != NULL); |
229 EXPECT_FALSE(profile_->GetPrefs()->GetBoolean(prefs::kSyncSuppressStart)); | 251 EXPECT_FALSE( |
| 252 harness_.profile->GetPrefs()->GetBoolean(prefs::kSyncSuppressStart)); |
230 | 253 |
231 service_->StopAndSuppress(); | 254 harness_.service->StopAndSuppress(); |
232 EXPECT_FALSE(service_->sync_initialized()); | 255 EXPECT_FALSE(harness_.service->sync_initialized()); |
233 EXPECT_TRUE(profile_->GetPrefs()->GetBoolean(prefs::kSyncSuppressStart)); | 256 EXPECT_TRUE( |
| 257 harness_.profile->GetPrefs()->GetBoolean(prefs::kSyncSuppressStart)); |
234 | 258 |
235 service_->UnsuppressAndStart(); | 259 harness_.service->UnsuppressAndStart(); |
236 EXPECT_TRUE(service_->sync_initialized()); | 260 EXPECT_TRUE(harness_.service->sync_initialized()); |
237 EXPECT_FALSE(profile_->GetPrefs()->GetBoolean(prefs::kSyncSuppressStart)); | 261 EXPECT_FALSE( |
| 262 harness_.profile->GetPrefs()->GetBoolean(prefs::kSyncSuppressStart)); |
238 } | 263 } |
239 | 264 |
240 TEST_F(ProfileSyncServiceTest, JsControllerHandlersBasic) { | 265 TEST_F(ProfileSyncServiceTest, JsControllerHandlersBasic) { |
241 StartSyncService(); | 266 harness_.StartSyncService(); |
242 EXPECT_TRUE(service_->sync_initialized()); | 267 EXPECT_TRUE(harness_.service->sync_initialized()); |
243 EXPECT_TRUE(service_->GetBackendForTest() != NULL); | 268 EXPECT_TRUE(harness_.service->GetBackendForTest() != NULL); |
244 | 269 |
245 syncer::JsController* js_controller = service_->GetJsController(); | 270 syncer::JsController* js_controller = harness_.service->GetJsController(); |
246 StrictMock<syncer::MockJsEventHandler> event_handler; | 271 StrictMock<syncer::MockJsEventHandler> event_handler; |
247 js_controller->AddJsEventHandler(&event_handler); | 272 js_controller->AddJsEventHandler(&event_handler); |
248 js_controller->RemoveJsEventHandler(&event_handler); | 273 js_controller->RemoveJsEventHandler(&event_handler); |
249 } | 274 } |
250 | 275 |
251 TEST_F(ProfileSyncServiceTest, | 276 TEST_F(ProfileSyncServiceTest, |
252 JsControllerHandlersDelayedBackendInitialization) { | 277 JsControllerHandlersDelayedBackendInitialization) { |
253 StartSyncServiceAndSetInitialSyncEnded(true, false, false, true, | 278 harness_.StartSyncServiceAndSetInitialSyncEnded(true, false, false, true, |
254 syncer::STORAGE_IN_MEMORY); | 279 syncer::STORAGE_IN_MEMORY); |
255 | 280 |
256 StrictMock<syncer::MockJsEventHandler> event_handler; | 281 StrictMock<syncer::MockJsEventHandler> event_handler; |
257 EXPECT_CALL(event_handler, HandleJsEvent(_, _)).Times(AtLeast(1)); | 282 EXPECT_CALL(event_handler, HandleJsEvent(_, _)).Times(AtLeast(1)); |
258 | 283 |
259 EXPECT_EQ(NULL, service_->GetBackendForTest()); | 284 EXPECT_EQ(NULL, harness_.service->GetBackendForTest()); |
260 EXPECT_FALSE(service_->sync_initialized()); | 285 EXPECT_FALSE(harness_.service->sync_initialized()); |
261 | 286 |
262 syncer::JsController* js_controller = service_->GetJsController(); | 287 syncer::JsController* js_controller = harness_.service->GetJsController(); |
263 js_controller->AddJsEventHandler(&event_handler); | 288 js_controller->AddJsEventHandler(&event_handler); |
264 // Since we're doing synchronous initialization, backend should be | 289 // Since we're doing synchronous initialization, backend should be |
265 // initialized by this call. | 290 // initialized by this call. |
266 IssueTestTokens(); | 291 harness_.IssueTestTokens(); |
267 EXPECT_TRUE(service_->sync_initialized()); | 292 EXPECT_TRUE(harness_.service->sync_initialized()); |
268 js_controller->RemoveJsEventHandler(&event_handler); | 293 js_controller->RemoveJsEventHandler(&event_handler); |
269 } | 294 } |
270 | 295 |
271 TEST_F(ProfileSyncServiceTest, JsControllerProcessJsMessageBasic) { | 296 TEST_F(ProfileSyncServiceTest, JsControllerProcessJsMessageBasic) { |
272 StartSyncService(); | 297 harness_.StartSyncService(); |
273 | 298 |
274 StrictMock<syncer::MockJsReplyHandler> reply_handler; | 299 StrictMock<syncer::MockJsReplyHandler> reply_handler; |
275 | 300 |
276 ListValue arg_list1; | 301 ListValue arg_list1; |
277 arg_list1.Append(Value::CreateStringValue("TRANSIENT_NOTIFICATION_ERROR")); | 302 arg_list1.Append(Value::CreateStringValue("TRANSIENT_INVALIDATION_ERROR")); |
278 syncer::JsArgList args1(&arg_list1); | 303 syncer::JsArgList args1(&arg_list1); |
279 EXPECT_CALL(reply_handler, | 304 EXPECT_CALL(reply_handler, |
280 HandleJsReply("getNotificationState", HasArgs(args1))); | 305 HandleJsReply("getNotificationState", HasArgs(args1))); |
281 | 306 |
282 { | 307 { |
283 syncer::JsController* js_controller = service_->GetJsController(); | 308 syncer::JsController* js_controller = harness_.service->GetJsController(); |
284 js_controller->ProcessJsMessage("getNotificationState", args1, | 309 js_controller->ProcessJsMessage("getNotificationState", args1, |
285 reply_handler.AsWeakHandle()); | 310 reply_handler.AsWeakHandle()); |
286 } | 311 } |
287 | 312 |
288 // This forces the sync thread to process the message and reply. | 313 // This forces the sync thread to process the message and reply. |
289 service_.reset(); | 314 harness_.TearDown(); |
290 ui_loop_.RunAllPending(); | |
291 } | 315 } |
292 | 316 |
293 TEST_F(ProfileSyncServiceTest, | 317 TEST_F(ProfileSyncServiceTest, |
294 JsControllerProcessJsMessageBasicDelayedBackendInitialization) { | 318 JsControllerProcessJsMessageBasicDelayedBackendInitialization) { |
295 StartSyncServiceAndSetInitialSyncEnded(true, false, false, true, | 319 harness_.StartSyncServiceAndSetInitialSyncEnded(true, false, false, true, |
296 syncer::STORAGE_IN_MEMORY); | 320 syncer::STORAGE_IN_MEMORY); |
297 | 321 |
298 StrictMock<syncer::MockJsReplyHandler> reply_handler; | 322 StrictMock<syncer::MockJsReplyHandler> reply_handler; |
299 | 323 |
300 ListValue arg_list1; | 324 ListValue arg_list1; |
301 arg_list1.Append(Value::CreateStringValue("TRANSIENT_NOTIFICATION_ERROR")); | 325 arg_list1.Append(Value::CreateStringValue("TRANSIENT_INVALIDATION_ERROR")); |
302 syncer::JsArgList args1(&arg_list1); | 326 syncer::JsArgList args1(&arg_list1); |
303 EXPECT_CALL(reply_handler, | 327 EXPECT_CALL(reply_handler, |
304 HandleJsReply("getNotificationState", HasArgs(args1))); | 328 HandleJsReply("getNotificationState", HasArgs(args1))); |
305 | 329 |
306 { | 330 { |
307 syncer::JsController* js_controller = service_->GetJsController(); | 331 syncer::JsController* js_controller = harness_.service->GetJsController(); |
308 js_controller->ProcessJsMessage("getNotificationState", | 332 js_controller->ProcessJsMessage("getNotificationState", |
309 args1, reply_handler.AsWeakHandle()); | 333 args1, reply_handler.AsWeakHandle()); |
310 } | 334 } |
311 | 335 |
312 IssueTestTokens(); | 336 harness_.IssueTestTokens(); |
313 | 337 |
314 // This forces the sync thread to process the message and reply. | 338 // This forces the sync thread to process the message and reply. |
315 service_.reset(); | 339 harness_.TearDown(); |
316 ui_loop_.RunAllPending(); | |
317 } | 340 } |
318 | 341 |
319 // Make sure that things still work if sync is not enabled, but some old sync | 342 // Make sure that things still work if sync is not enabled, but some old sync |
320 // databases are lingering in the "Sync Data" folder. | 343 // databases are lingering in the "Sync Data" folder. |
321 TEST_F(ProfileSyncServiceTest, TestStartupWithOldSyncData) { | 344 TEST_F(ProfileSyncServiceTest, TestStartupWithOldSyncData) { |
322 const char* nonsense1 = "reginald"; | 345 const char* nonsense1 = "reginald"; |
323 const char* nonsense2 = "beartato"; | 346 const char* nonsense2 = "beartato"; |
324 const char* nonsense3 = "harrison"; | 347 const char* nonsense3 = "harrison"; |
325 FilePath temp_directory = profile_->GetPath().AppendASCII("Sync Data"); | 348 FilePath temp_directory = |
| 349 harness_.profile->GetPath().AppendASCII("Sync Data"); |
326 FilePath sync_file1 = | 350 FilePath sync_file1 = |
327 temp_directory.AppendASCII("BookmarkSyncSettings.sqlite3"); | 351 temp_directory.AppendASCII("BookmarkSyncSettings.sqlite3"); |
328 FilePath sync_file2 = temp_directory.AppendASCII("SyncData.sqlite3"); | 352 FilePath sync_file2 = temp_directory.AppendASCII("SyncData.sqlite3"); |
329 FilePath sync_file3 = temp_directory.AppendASCII("nonsense_file"); | 353 FilePath sync_file3 = temp_directory.AppendASCII("nonsense_file"); |
330 ASSERT_TRUE(file_util::CreateDirectory(temp_directory)); | 354 ASSERT_TRUE(file_util::CreateDirectory(temp_directory)); |
331 ASSERT_NE(-1, | 355 ASSERT_NE(-1, |
332 file_util::WriteFile(sync_file1, nonsense1, strlen(nonsense1))); | 356 file_util::WriteFile(sync_file1, nonsense1, strlen(nonsense1))); |
333 ASSERT_NE(-1, | 357 ASSERT_NE(-1, |
334 file_util::WriteFile(sync_file2, nonsense2, strlen(nonsense2))); | 358 file_util::WriteFile(sync_file2, nonsense2, strlen(nonsense2))); |
335 ASSERT_NE(-1, | 359 ASSERT_NE(-1, |
336 file_util::WriteFile(sync_file3, nonsense3, strlen(nonsense3))); | 360 file_util::WriteFile(sync_file3, nonsense3, strlen(nonsense3))); |
337 | 361 |
338 StartSyncServiceAndSetInitialSyncEnded(false, false, true, false, | 362 harness_.StartSyncServiceAndSetInitialSyncEnded(false, false, true, false, |
339 syncer::STORAGE_ON_DISK); | 363 syncer::STORAGE_ON_DISK); |
340 EXPECT_FALSE(service_->HasSyncSetupCompleted()); | 364 EXPECT_FALSE(harness_.service->HasSyncSetupCompleted()); |
341 EXPECT_FALSE(service_->sync_initialized()); | 365 EXPECT_FALSE(harness_.service->sync_initialized()); |
342 | 366 |
343 // Since we're doing synchronous initialization, backend should be | 367 // Since we're doing synchronous initialization, backend should be |
344 // initialized by this call. | 368 // initialized by this call. |
345 IssueTestTokens(); | 369 harness_.IssueTestTokens(); |
346 | 370 |
347 // Stop the service so we can read the new Sync Data files that were | 371 // Stop the service so we can read the new Sync Data files that were |
348 // created. | 372 // created. |
349 service_.reset(); | 373 harness_.service.reset(); |
350 | 374 |
351 // This file should have been deleted when the whole directory was nuked. | 375 // This file should have been deleted when the whole directory was nuked. |
352 ASSERT_FALSE(file_util::PathExists(sync_file3)); | 376 ASSERT_FALSE(file_util::PathExists(sync_file3)); |
353 ASSERT_FALSE(file_util::PathExists(sync_file1)); | 377 ASSERT_FALSE(file_util::PathExists(sync_file1)); |
354 | 378 |
355 // This will still exist, but the text should have changed. | 379 // This will still exist, but the text should have changed. |
356 ASSERT_TRUE(file_util::PathExists(sync_file2)); | 380 ASSERT_TRUE(file_util::PathExists(sync_file2)); |
357 std::string file2text; | 381 std::string file2text; |
358 ASSERT_TRUE(file_util::ReadFileToString(sync_file2, &file2text)); | 382 ASSERT_TRUE(file_util::ReadFileToString(sync_file2, &file2text)); |
359 ASSERT_NE(file2text.compare(nonsense2), 0); | 383 ASSERT_NE(file2text.compare(nonsense2), 0); |
360 } | 384 } |
361 | 385 |
362 // Simulates a scenario where a database is corrupted and it is impossible to | 386 // Simulates a scenario where a database is corrupted and it is impossible to |
363 // recreate it. This test is useful mainly when it is run under valgrind. Its | 387 // recreate it. This test is useful mainly when it is run under valgrind. Its |
364 // expectations are not very interesting. | 388 // expectations are not very interesting. |
365 TEST_F(ProfileSyncServiceTest, FailToOpenDatabase) { | 389 TEST_F(ProfileSyncServiceTest, FailToOpenDatabase) { |
366 StartSyncServiceAndSetInitialSyncEnded(false, true, true, true, | 390 harness_.StartSyncServiceAndSetInitialSyncEnded(false, true, true, true, |
367 syncer::STORAGE_INVALID); | 391 syncer::STORAGE_INVALID); |
368 | 392 |
369 // The backend is not ready. Ensure the PSS knows this. | 393 // The backend is not ready. Ensure the PSS knows this. |
370 EXPECT_FALSE(service_->sync_initialized()); | 394 EXPECT_FALSE(harness_.service->sync_initialized()); |
371 } | |
372 | |
373 // Register for some IDs with the ProfileSyncService and trigger some | |
374 // invalidation messages. They should be received by the handler. | |
375 // Then unregister and trigger the invalidation messages again. Those | |
376 // shouldn't be received by the handler. | |
377 TEST_F(ProfileSyncServiceTest, UpdateRegisteredInvalidationIds) { | |
378 StartSyncService(); | |
379 | |
380 syncer::ObjectIdSet ids; | |
381 ids.insert(invalidation::ObjectId(1, "id1")); | |
382 ids.insert(invalidation::ObjectId(2, "id2")); | |
383 const syncer::ObjectIdStateMap& states = | |
384 syncer::ObjectIdSetToStateMap(ids, "payload"); | |
385 | |
386 syncer::FakeInvalidationHandler handler; | |
387 | |
388 service_->RegisterInvalidationHandler(&handler); | |
389 service_->UpdateRegisteredInvalidationIds(&handler, ids); | |
390 | |
391 SyncBackendHostForProfileSyncTest* const backend = | |
392 service_->GetBackendForTest(); | |
393 | |
394 backend->EmitOnNotificationsEnabled(); | |
395 EXPECT_EQ(syncer::NO_NOTIFICATION_ERROR, | |
396 handler.GetNotificationsDisabledReason()); | |
397 | |
398 backend->EmitOnIncomingNotification(states, syncer::REMOTE_NOTIFICATION); | |
399 EXPECT_THAT(states, Eq(handler.GetLastNotificationIdStateMap())); | |
400 EXPECT_EQ(syncer::REMOTE_NOTIFICATION, handler.GetLastNotificationSource()); | |
401 | |
402 backend->EmitOnNotificationsDisabled(syncer::TRANSIENT_NOTIFICATION_ERROR); | |
403 EXPECT_EQ(syncer::TRANSIENT_NOTIFICATION_ERROR, | |
404 handler.GetNotificationsDisabledReason()); | |
405 | |
406 Mock::VerifyAndClearExpectations(&handler); | |
407 | |
408 service_->UnregisterInvalidationHandler(&handler); | |
409 | |
410 backend->EmitOnNotificationsEnabled(); | |
411 backend->EmitOnIncomingNotification(states, syncer::REMOTE_NOTIFICATION); | |
412 backend->EmitOnNotificationsDisabled(syncer::TRANSIENT_NOTIFICATION_ERROR); | |
413 } | 395 } |
414 | 396 |
415 // Register for some IDs with the ProfileSyncService, restart sync, | 397 // Register for some IDs with the ProfileSyncService, restart sync, |
416 // and trigger some invalidation messages. They should still be | 398 // and trigger some invalidation messages. They should still be |
417 // received by the handler. | 399 // received by the handler. |
418 TEST_F(ProfileSyncServiceTest, UpdateRegisteredInvalidationIdsPersistence) { | 400 TEST_F(ProfileSyncServiceTest, UpdateRegisteredInvalidationIdsPersistence) { |
419 StartSyncService(); | 401 harness_.StartSyncService(); |
420 | 402 |
421 syncer::ObjectIdSet ids; | 403 syncer::ObjectIdSet ids; |
422 ids.insert(invalidation::ObjectId(3, "id3")); | 404 ids.insert(invalidation::ObjectId(3, "id3")); |
423 const syncer::ObjectIdStateMap& states = | 405 const syncer::ObjectIdStateMap& states = |
424 syncer::ObjectIdSetToStateMap(ids, "payload"); | 406 syncer::ObjectIdSetToStateMap(ids, "payload"); |
425 | 407 |
426 syncer::FakeInvalidationHandler handler; | 408 syncer::FakeInvalidationHandler handler; |
427 | 409 |
428 service_->RegisterInvalidationHandler(&handler); | 410 harness_.service->RegisterInvalidationHandler(&handler); |
429 service_->UpdateRegisteredInvalidationIds(&handler, ids); | 411 harness_.service->UpdateRegisteredInvalidationIds(&handler, ids); |
430 | 412 |
431 service_->StopAndSuppress(); | 413 harness_.service->StopAndSuppress(); |
432 service_->UnsuppressAndStart(); | 414 harness_.service->UnsuppressAndStart(); |
433 | 415 |
434 SyncBackendHostForProfileSyncTest* const backend = | 416 SyncBackendHostForProfileSyncTest* const backend = |
435 service_->GetBackendForTest(); | 417 harness_.service->GetBackendForTest(); |
436 | 418 |
437 backend->EmitOnNotificationsEnabled(); | 419 backend->EmitOnInvalidatorStateChange(syncer::INVALIDATIONS_ENABLED); |
438 EXPECT_EQ(syncer::NO_NOTIFICATION_ERROR, | 420 EXPECT_EQ(syncer::INVALIDATIONS_ENABLED, handler.GetInvalidatorState()); |
439 handler.GetNotificationsDisabledReason()); | |
440 | 421 |
441 backend->EmitOnIncomingNotification(states, syncer::REMOTE_NOTIFICATION); | 422 backend->EmitOnIncomingInvalidation(states, syncer::REMOTE_INVALIDATION); |
442 EXPECT_THAT(states, Eq(handler.GetLastNotificationIdStateMap())); | 423 EXPECT_THAT(states, Eq(handler.GetLastInvalidationIdStateMap())); |
443 EXPECT_EQ(syncer::REMOTE_NOTIFICATION, handler.GetLastNotificationSource()); | 424 EXPECT_EQ(syncer::REMOTE_INVALIDATION, handler.GetLastInvalidationSource()); |
444 | 425 |
445 backend->EmitOnNotificationsDisabled(syncer::TRANSIENT_NOTIFICATION_ERROR); | 426 backend->EmitOnInvalidatorStateChange(syncer::TRANSIENT_INVALIDATION_ERROR); |
446 EXPECT_EQ(syncer::TRANSIENT_NOTIFICATION_ERROR, | 427 EXPECT_EQ(syncer::TRANSIENT_INVALIDATION_ERROR, |
447 handler.GetNotificationsDisabledReason()); | 428 handler.GetInvalidatorState()); |
448 } | 429 } |
449 | 430 |
| 431 // Thin Invalidator wrapper around ProfileSyncService. |
| 432 class ProfileSyncServiceInvalidator : public syncer::Invalidator { |
| 433 public: |
| 434 explicit ProfileSyncServiceInvalidator(ProfileSyncService* service) |
| 435 : service_(service) {} |
| 436 |
| 437 virtual ~ProfileSyncServiceInvalidator() {} |
| 438 |
| 439 // Invalidator implementation. |
| 440 virtual void RegisterHandler(syncer::InvalidationHandler* handler) OVERRIDE { |
| 441 service_->RegisterInvalidationHandler(handler); |
| 442 } |
| 443 |
| 444 virtual void UpdateRegisteredIds(syncer::InvalidationHandler* handler, |
| 445 const syncer::ObjectIdSet& ids) OVERRIDE { |
| 446 service_->UpdateRegisteredInvalidationIds(handler, ids); |
| 447 } |
| 448 |
| 449 virtual void UnregisterHandler( |
| 450 syncer::InvalidationHandler* handler) OVERRIDE { |
| 451 service_->UnregisterInvalidationHandler(handler); |
| 452 } |
| 453 |
| 454 virtual syncer::InvalidatorState GetInvalidatorState() const OVERRIDE { |
| 455 return service_->GetInvalidatorState(); |
| 456 } |
| 457 |
| 458 virtual void SetUniqueId(const std::string& unique_id) OVERRIDE { |
| 459 // Do nothing. |
| 460 } |
| 461 |
| 462 virtual void SetStateDeprecated(const std::string& state) OVERRIDE { |
| 463 // Do nothing. |
| 464 } |
| 465 |
| 466 virtual void UpdateCredentials( |
| 467 const std::string& email, const std::string& token) OVERRIDE { |
| 468 // Do nothing. |
| 469 } |
| 470 |
| 471 virtual void SendInvalidation( |
| 472 const syncer::ObjectIdStateMap& id_state_map) OVERRIDE { |
| 473 // Do nothing. |
| 474 } |
| 475 |
| 476 private: |
| 477 ProfileSyncService* const service_; |
| 478 |
| 479 DISALLOW_COPY_AND_ASSIGN(ProfileSyncServiceInvalidator); |
| 480 }; |
| 481 |
450 } // namespace | 482 } // namespace |
| 483 |
| 484 |
| 485 // ProfileSyncServiceInvalidatorTestDelegate has to be visible from |
| 486 // the syncer namespace (where InvalidatorTest lives). |
| 487 class ProfileSyncServiceInvalidatorTestDelegate { |
| 488 public: |
| 489 ProfileSyncServiceInvalidatorTestDelegate() {} |
| 490 |
| 491 ~ProfileSyncServiceInvalidatorTestDelegate() { |
| 492 DestroyInvalidator(); |
| 493 } |
| 494 |
| 495 void CreateInvalidator( |
| 496 const std::string& initial_state, |
| 497 const base::WeakPtr<syncer::InvalidationStateTracker>& |
| 498 invalidation_state_tracker) { |
| 499 DCHECK(!invalidator_.get()); |
| 500 harness_.SetUp(); |
| 501 harness_.StartSyncService(); |
| 502 invalidator_.reset( |
| 503 new ProfileSyncServiceInvalidator(harness_.service.get())); |
| 504 } |
| 505 |
| 506 ProfileSyncServiceInvalidator* GetInvalidator() { |
| 507 return invalidator_.get(); |
| 508 } |
| 509 |
| 510 void DestroyInvalidator() { |
| 511 invalidator_.reset(); |
| 512 harness_.TearDown(); |
| 513 } |
| 514 |
| 515 void WaitForInvalidator() { |
| 516 // Do nothing. |
| 517 } |
| 518 |
| 519 void TriggerOnInvalidatorStateChange(syncer::InvalidatorState state) { |
| 520 harness_.service->GetBackendForTest()->EmitOnInvalidatorStateChange(state); |
| 521 } |
| 522 |
| 523 void TriggerOnIncomingInvalidation( |
| 524 const syncer::ObjectIdStateMap& id_state_map, |
| 525 syncer::IncomingInvalidationSource source) { |
| 526 harness_.service->GetBackendForTest()->EmitOnIncomingInvalidation( |
| 527 id_state_map, source); |
| 528 } |
| 529 |
| 530 static bool InvalidatorHandlesDeprecatedState() { |
| 531 return false; |
| 532 } |
| 533 |
| 534 private: |
| 535 ProfileSyncServiceTestHarness harness_; |
| 536 scoped_ptr<ProfileSyncServiceInvalidator> invalidator_; |
| 537 }; |
| 538 |
451 } // namespace browser_sync | 539 } // namespace browser_sync |
| 540 |
| 541 namespace syncer { |
| 542 namespace { |
| 543 |
| 544 // ProfileSyncService should behave just like an invalidator. |
| 545 INSTANTIATE_TYPED_TEST_CASE_P( |
| 546 ProfileSyncServiceInvalidatorTest, InvalidatorTest, |
| 547 ::browser_sync::ProfileSyncServiceInvalidatorTestDelegate); |
| 548 |
| 549 } // namespace |
| 550 } // namespace syncer |
OLD | NEW |