Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(59)

Side by Side Diff: chrome/browser/sync/profile_sync_service_unittest.cc

Issue 10805002: [Sync] Enable adding notifier observers from ProfileSyncService (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Sync to head, address comments Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/file_util.h" 5 #include "base/file_util.h"
6 #include "base/memory/scoped_ptr.h" 6 #include "base/memory/scoped_ptr.h"
7 #include "base/message_loop.h" 7 #include "base/message_loop.h"
8 #include "base/values.h" 8 #include "base/values.h"
9 #include "chrome/browser/signin/signin_manager.h" 9 #include "chrome/browser/signin/signin_manager.h"
10 #include "chrome/browser/signin/signin_manager_factory.h" 10 #include "chrome/browser/signin/signin_manager_factory.h"
11 #include "chrome/browser/signin/token_service.h" 11 #include "chrome/browser/signin/token_service.h"
12 #include "chrome/browser/signin/token_service_factory.h" 12 #include "chrome/browser/signin/token_service_factory.h"
13 #include "chrome/browser/sync/glue/bookmark_data_type_controller.h" 13 #include "chrome/browser/sync/glue/bookmark_data_type_controller.h"
14 #include "chrome/browser/sync/glue/data_type_controller.h" 14 #include "chrome/browser/sync/glue/data_type_controller.h"
15 #include "chrome/browser/sync/profile_sync_components_factory_mock.h" 15 #include "chrome/browser/sync/profile_sync_components_factory_mock.h"
16 #include "chrome/browser/sync/test_profile_sync_service.h" 16 #include "chrome/browser/sync/test_profile_sync_service.h"
17 #include "chrome/common/chrome_version_info.h" 17 #include "chrome/common/chrome_version_info.h"
18 #include "chrome/common/net/gaia/gaia_constants.h" 18 #include "chrome/common/net/gaia/gaia_constants.h"
19 #include "chrome/common/pref_names.h" 19 #include "chrome/common/pref_names.h"
20 #include "chrome/test/base/testing_pref_service.h" 20 #include "chrome/test/base/testing_pref_service.h"
21 #include "chrome/test/base/testing_profile.h" 21 #include "chrome/test/base/testing_profile.h"
22 #include "content/public/common/content_client.h" 22 #include "content/public/common/content_client.h"
23 #include "content/public/test/test_browser_thread.h" 23 #include "content/public/test/test_browser_thread.h"
24 #include "google/cacheinvalidation/include/types.h"
24 #include "sync/js/js_arg_list.h" 25 #include "sync/js/js_arg_list.h"
25 #include "sync/js/js_event_details.h" 26 #include "sync/js/js_event_details.h"
26 #include "sync/js/js_test_util.h" 27 #include "sync/js/js_test_util.h"
28 #include "sync/notifier/mock_sync_notifier_observer.h"
27 #include "testing/gmock/include/gmock/gmock.h" 29 #include "testing/gmock/include/gmock/gmock.h"
28 #include "testing/gtest/include/gtest/gtest.h" 30 #include "testing/gtest/include/gtest/gtest.h"
29 #include "webkit/glue/webkit_glue.h" 31 #include "webkit/glue/webkit_glue.h"
30 32
31 // TODO(akalin): Add tests here that exercise the whole 33 // TODO(akalin): Add tests here that exercise the whole
32 // ProfileSyncService/SyncBackendHost stack while mocking out as 34 // ProfileSyncService/SyncBackendHost stack while mocking out as
33 // little as possible. 35 // little as possible.
34 36
35 namespace browser_sync { 37 namespace browser_sync {
36 38
37 namespace { 39 namespace {
38 40
39 using content::BrowserThread; 41 using content::BrowserThread;
40 using testing::_; 42 using testing::_;
41 using testing::AtLeast; 43 using testing::AtLeast;
42 using testing::AtMost; 44 using testing::AtMost;
45 using testing::Mock;
43 using testing::Return; 46 using testing::Return;
44 using testing::StrictMock; 47 using testing::StrictMock;
45 48
46 class ProfileSyncServiceTest : public testing::Test { 49 class ProfileSyncServiceTest : public testing::Test {
47 protected: 50 protected:
48 ProfileSyncServiceTest() 51 ProfileSyncServiceTest()
49 : ui_thread_(BrowserThread::UI, &ui_loop_), 52 : ui_thread_(BrowserThread::UI, &ui_loop_),
50 db_thread_(BrowserThread::DB), 53 db_thread_(BrowserThread::DB),
51 file_thread_(BrowserThread::FILE), 54 file_thread_(BrowserThread::FILE),
52 io_thread_(BrowserThread::IO) {} 55 io_thread_(BrowserThread::IO) {}
(...skipping 22 matching lines...) Expand all
75 io_thread_.Stop(); 78 io_thread_.Stop();
76 file_thread_.Stop(); 79 file_thread_.Stop();
77 // Ensure that the sync objects destruct to avoid memory leaks. 80 // Ensure that the sync objects destruct to avoid memory leaks.
78 ui_loop_.RunAllPending(); 81 ui_loop_.RunAllPending();
79 } 82 }
80 83
81 // TODO(akalin): Refactor the StartSyncService*() functions below. 84 // TODO(akalin): Refactor the StartSyncService*() functions below.
82 85
83 void StartSyncService() { 86 void StartSyncService() {
84 StartSyncServiceAndSetInitialSyncEnded( 87 StartSyncServiceAndSetInitialSyncEnded(
85 true, true, false, true, true, syncer::STORAGE_IN_MEMORY); 88 true, true, false, true, 1, syncer::STORAGE_IN_MEMORY);
86 } 89 }
87 90
88 void StartSyncServiceAndSetInitialSyncEnded( 91 void StartSyncServiceAndSetInitialSyncEnded(
89 bool set_initial_sync_ended, 92 bool set_initial_sync_ended,
90 bool issue_auth_token, 93 bool issue_auth_token,
91 bool synchronous_sync_configuration, 94 bool synchronous_sync_configuration,
92 bool sync_setup_completed, 95 bool sync_setup_completed,
93 bool expect_create_dtm, 96 int expected_create_dtm_calls,
tim (not reviewing) 2012/08/02 17:43:23 Oh boy. 1 and 0 :| An enum CREATE_DTM, DONT_CREATE
akalin 2012/08/02 22:06:00 Done.
94 syncer::StorageOption storage_option) { 97 syncer::StorageOption storage_option) {
95 if (!service_.get()) { 98 if (!service_.get()) {
96 SigninManager* signin = 99 SigninManager* signin =
97 SigninManagerFactory::GetForProfile(profile_.get()); 100 SigninManagerFactory::GetForProfile(profile_.get());
98 signin->SetAuthenticatedUsername("test"); 101 signin->SetAuthenticatedUsername("test");
99 ProfileSyncComponentsFactoryMock* factory = 102 ProfileSyncComponentsFactoryMock* factory =
100 new ProfileSyncComponentsFactoryMock(); 103 new ProfileSyncComponentsFactoryMock();
101 service_.reset(new TestProfileSyncService( 104 service_.reset(new TestProfileSyncService(
102 factory, 105 factory,
103 profile_.get(), 106 profile_.get(),
104 signin, 107 signin,
105 ProfileSyncService::AUTO_START, 108 ProfileSyncService::AUTO_START,
106 true, 109 true,
107 base::Closure())); 110 base::Closure()));
108 if (!set_initial_sync_ended) 111 if (!set_initial_sync_ended)
109 service_->dont_set_initial_sync_ended_on_init(); 112 service_->dont_set_initial_sync_ended_on_init();
110 if (synchronous_sync_configuration) 113 if (synchronous_sync_configuration)
111 service_->set_synchronous_sync_configuration(); 114 service_->set_synchronous_sync_configuration();
112 service_->set_storage_option(storage_option); 115 service_->set_storage_option(storage_option);
113 if (!sync_setup_completed) 116 if (!sync_setup_completed)
114 profile_->GetPrefs()->SetBoolean(prefs::kSyncHasSetupCompleted, false); 117 profile_->GetPrefs()->SetBoolean(prefs::kSyncHasSetupCompleted, false);
115 118
116 if (expect_create_dtm) { 119 // Register the bookmark data type.
117 // Register the bookmark data type. 120 EXPECT_CALL(*factory, CreateDataTypeManager(_, _)).
118 EXPECT_CALL(*factory, CreateDataTypeManager(_, _)). 121 Times(expected_create_dtm_calls).
119 WillOnce(ReturnNewDataTypeManager()); 122 WillRepeatedly(ReturnNewDataTypeManager());
120 } else {
121 EXPECT_CALL(*factory, CreateDataTypeManager(_, _)).
122 Times(0);
123 }
124 123
125 if (issue_auth_token) { 124 if (issue_auth_token) {
126 IssueTestTokens(); 125 IssueTestTokens();
127 } 126 }
128 service_->Initialize(); 127 service_->Initialize();
129 } 128 }
130 } 129 }
131 130
132 void IssueTestTokens() { 131 void IssueTestTokens() {
133 TokenService* token_service = 132 TokenService* token_service =
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 EXPECT_TRUE(service_->GetBackendForTest() != NULL); 244 EXPECT_TRUE(service_->GetBackendForTest() != NULL);
246 245
247 syncer::JsController* js_controller = service_->GetJsController(); 246 syncer::JsController* js_controller = service_->GetJsController();
248 StrictMock<syncer::MockJsEventHandler> event_handler; 247 StrictMock<syncer::MockJsEventHandler> event_handler;
249 js_controller->AddJsEventHandler(&event_handler); 248 js_controller->AddJsEventHandler(&event_handler);
250 js_controller->RemoveJsEventHandler(&event_handler); 249 js_controller->RemoveJsEventHandler(&event_handler);
251 } 250 }
252 251
253 TEST_F(ProfileSyncServiceTest, 252 TEST_F(ProfileSyncServiceTest,
254 JsControllerHandlersDelayedBackendInitialization) { 253 JsControllerHandlersDelayedBackendInitialization) {
255 StartSyncServiceAndSetInitialSyncEnded(true, false, false, true, true, 254 StartSyncServiceAndSetInitialSyncEnded(true, false, false, true, 1,
256 syncer::STORAGE_IN_MEMORY); 255 syncer::STORAGE_IN_MEMORY);
257 256
258 StrictMock<syncer::MockJsEventHandler> event_handler; 257 StrictMock<syncer::MockJsEventHandler> event_handler;
259 EXPECT_CALL(event_handler, HandleJsEvent(_, _)).Times(AtLeast(1)); 258 EXPECT_CALL(event_handler, HandleJsEvent(_, _)).Times(AtLeast(1));
260 259
261 EXPECT_EQ(NULL, service_->GetBackendForTest()); 260 EXPECT_EQ(NULL, service_->GetBackendForTest());
262 EXPECT_FALSE(service_->sync_initialized()); 261 EXPECT_FALSE(service_->sync_initialized());
263 262
264 syncer::JsController* js_controller = service_->GetJsController(); 263 syncer::JsController* js_controller = service_->GetJsController();
265 js_controller->AddJsEventHandler(&event_handler); 264 js_controller->AddJsEventHandler(&event_handler);
(...skipping 21 matching lines...) Expand all
287 reply_handler.AsWeakHandle()); 286 reply_handler.AsWeakHandle());
288 } 287 }
289 288
290 // This forces the sync thread to process the message and reply. 289 // This forces the sync thread to process the message and reply.
291 service_.reset(); 290 service_.reset();
292 ui_loop_.RunAllPending(); 291 ui_loop_.RunAllPending();
293 } 292 }
294 293
295 TEST_F(ProfileSyncServiceTest, 294 TEST_F(ProfileSyncServiceTest,
296 JsControllerProcessJsMessageBasicDelayedBackendInitialization) { 295 JsControllerProcessJsMessageBasicDelayedBackendInitialization) {
297 StartSyncServiceAndSetInitialSyncEnded(true, false, false, true, true, 296 StartSyncServiceAndSetInitialSyncEnded(true, false, false, true, 1,
298 syncer::STORAGE_IN_MEMORY); 297 syncer::STORAGE_IN_MEMORY);
299 298
300 StrictMock<syncer::MockJsReplyHandler> reply_handler; 299 StrictMock<syncer::MockJsReplyHandler> reply_handler;
301 300
302 ListValue arg_list1; 301 ListValue arg_list1;
303 arg_list1.Append(Value::CreateBooleanValue(false)); 302 arg_list1.Append(Value::CreateBooleanValue(false));
304 syncer::JsArgList args1(&arg_list1); 303 syncer::JsArgList args1(&arg_list1);
305 EXPECT_CALL(reply_handler, 304 EXPECT_CALL(reply_handler,
306 HandleJsReply("getNotificationState", HasArgs(args1))); 305 HandleJsReply("getNotificationState", HasArgs(args1)));
307 306
(...skipping 22 matching lines...) Expand all
330 FilePath sync_file2 = temp_directory.AppendASCII("SyncData.sqlite3"); 329 FilePath sync_file2 = temp_directory.AppendASCII("SyncData.sqlite3");
331 FilePath sync_file3 = temp_directory.AppendASCII("nonsense_file"); 330 FilePath sync_file3 = temp_directory.AppendASCII("nonsense_file");
332 ASSERT_TRUE(file_util::CreateDirectory(temp_directory)); 331 ASSERT_TRUE(file_util::CreateDirectory(temp_directory));
333 ASSERT_NE(-1, 332 ASSERT_NE(-1,
334 file_util::WriteFile(sync_file1, nonsense1, strlen(nonsense1))); 333 file_util::WriteFile(sync_file1, nonsense1, strlen(nonsense1)));
335 ASSERT_NE(-1, 334 ASSERT_NE(-1,
336 file_util::WriteFile(sync_file2, nonsense2, strlen(nonsense2))); 335 file_util::WriteFile(sync_file2, nonsense2, strlen(nonsense2)));
337 ASSERT_NE(-1, 336 ASSERT_NE(-1,
338 file_util::WriteFile(sync_file3, nonsense3, strlen(nonsense3))); 337 file_util::WriteFile(sync_file3, nonsense3, strlen(nonsense3)));
339 338
340 StartSyncServiceAndSetInitialSyncEnded(false, false, true, false, true, 339 StartSyncServiceAndSetInitialSyncEnded(false, false, true, false, 1,
341 syncer::STORAGE_ON_DISK); 340 syncer::STORAGE_ON_DISK);
342 EXPECT_FALSE(service_->HasSyncSetupCompleted()); 341 EXPECT_FALSE(service_->HasSyncSetupCompleted());
343 EXPECT_FALSE(service_->sync_initialized()); 342 EXPECT_FALSE(service_->sync_initialized());
344 343
345 // Since we're doing synchronous initialization, backend should be 344 // Since we're doing synchronous initialization, backend should be
346 // initialized by this call. 345 // initialized by this call.
347 IssueTestTokens(); 346 IssueTestTokens();
348 347
349 // Stop the service so we can read the new Sync Data files that were 348 // Stop the service so we can read the new Sync Data files that were
350 // created. 349 // created.
351 service_.reset(); 350 service_.reset();
352 351
353 // This file should have been deleted when the whole directory was nuked. 352 // This file should have been deleted when the whole directory was nuked.
354 ASSERT_FALSE(file_util::PathExists(sync_file3)); 353 ASSERT_FALSE(file_util::PathExists(sync_file3));
355 ASSERT_FALSE(file_util::PathExists(sync_file1)); 354 ASSERT_FALSE(file_util::PathExists(sync_file1));
356 355
357 // This will still exist, but the text should have changed. 356 // This will still exist, but the text should have changed.
358 ASSERT_TRUE(file_util::PathExists(sync_file2)); 357 ASSERT_TRUE(file_util::PathExists(sync_file2));
359 std::string file2text; 358 std::string file2text;
360 ASSERT_TRUE(file_util::ReadFileToString(sync_file2, &file2text)); 359 ASSERT_TRUE(file_util::ReadFileToString(sync_file2, &file2text));
361 ASSERT_NE(file2text.compare(nonsense2), 0); 360 ASSERT_NE(file2text.compare(nonsense2), 0);
362 } 361 }
363 362
364 // Simulates a scenario where a database is corrupted and it is impossible to 363 // Simulates a scenario where a database is corrupted and it is impossible to
365 // recreate it. This test is useful mainly when it is run under valgrind. Its 364 // recreate it. This test is useful mainly when it is run under valgrind. Its
366 // expectations are not very interesting. 365 // expectations are not very interesting.
367 TEST_F(ProfileSyncServiceTest, FailToOpenDatabase) { 366 TEST_F(ProfileSyncServiceTest, FailToOpenDatabase) {
368 StartSyncServiceAndSetInitialSyncEnded(false, true, true, true, false, 367 StartSyncServiceAndSetInitialSyncEnded(false, true, true, true, 0,
369 syncer::STORAGE_INVALID); 368 syncer::STORAGE_INVALID);
370 369
371 // The backend is not ready. Ensure the PSS knows this. 370 // The backend is not ready. Ensure the PSS knows this.
372 EXPECT_FALSE(service_->sync_initialized()); 371 EXPECT_FALSE(service_->sync_initialized());
373 } 372 }
374 373
374 // Register for some IDs with the ProfileSyncService and trigger some
375 // invalidation messages. They should be received by the observer.
376 // Then unregister and trigger the invalidation messages again. Those
377 // shouldn't be received by the observer.
378 TEST_F(ProfileSyncServiceTest, UpdateRegisteredInvalidationIds) {
379 StartSyncService();
380
381 syncer::ObjectIdSet ids;
382 ids.insert(invalidation::ObjectId(1, "id1"));
383 ids.insert(invalidation::ObjectId(2, "id2"));
384 const syncer::ObjectIdPayloadMap& payloads =
385 syncer::ObjectIdSetToPayloadMap(ids, "payload");
386
387 StrictMock<syncer::MockSyncNotifierObserver> observer;
388 EXPECT_CALL(observer, OnNotificationsEnabled());
389 EXPECT_CALL(observer, OnNotificationsDisabled(
390 syncer::TRANSIENT_NOTIFICATION_ERROR));
391 EXPECT_CALL(observer, OnIncomingNotification(
392 payloads, syncer::REMOTE_NOTIFICATION));
393
394 service_->UpdateRegisteredInvalidationIds(&observer, ids);
395
396 SyncBackendHostForProfileSyncTest* const backend =
397 service_->GetBackendForTest();
398
399 backend->EmitOnNotificationsEnabled();
400 backend->EmitOnNotificationsDisabled(syncer::TRANSIENT_NOTIFICATION_ERROR);
401 backend->EmitOnIncomingNotification(payloads, syncer::REMOTE_NOTIFICATION);
402
403 Mock::VerifyAndClearExpectations(&observer);
404
405 service_->UpdateRegisteredInvalidationIds(&observer, syncer::ObjectIdSet());
406
407 backend->EmitOnNotificationsEnabled();
408 backend->EmitOnNotificationsDisabled(syncer::TRANSIENT_NOTIFICATION_ERROR);
409 backend->EmitOnIncomingNotification(payloads, syncer::REMOTE_NOTIFICATION);
410 }
411
412 // Register for some IDs with the ProfileSyncService, restart sync,
413 // and trigger some invalidation messages. They should still be
414 // received by the observer.
415 TEST_F(ProfileSyncServiceTest, UpdateRegisteredInvalidationIdsPersistence) {
416 StartSyncServiceAndSetInitialSyncEnded(true, true, false, true, 2,
417 syncer::STORAGE_IN_MEMORY);
418
419 StrictMock<syncer::MockSyncNotifierObserver> observer;
420 EXPECT_CALL(observer, OnNotificationsEnabled());
421
422 syncer::ObjectIdSet ids;
423 ids.insert(invalidation::ObjectId(3, "id3"));
424 service_->UpdateRegisteredInvalidationIds(&observer, ids);
425
426 service_->StopAndSuppress();
427 service_->UnsuppressAndStart();
428
429 service_->GetBackendForTest()->EmitOnNotificationsEnabled();
430 }
431
375 } // namespace 432 } // namespace
376 } // namespace browser_sync 433 } // namespace browser_sync
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698