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 "chrome/browser/sync/glue/sync_backend_host.h" | 5 #include "chrome/browser/sync/glue/sync_backend_host.h" |
6 | 6 |
7 #include <cstddef> | 7 #include <cstddef> |
8 | 8 |
| 9 #include "base/location.h" |
9 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
10 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
11 #include "base/synchronization/waitable_event.h" | 12 #include "base/synchronization/waitable_event.h" |
12 #include "base/test/test_timeouts.h" | 13 #include "base/test/test_timeouts.h" |
13 #include "chrome/browser/sync/invalidations/invalidator_storage.h" | 14 #include "chrome/browser/sync/invalidations/invalidator_storage.h" |
14 #include "chrome/browser/sync/sync_prefs.h" | 15 #include "chrome/browser/sync/sync_prefs.h" |
15 #include "chrome/test/base/testing_profile.h" | 16 #include "chrome/test/base/testing_profile.h" |
16 #include "content/public/test/test_browser_thread.h" | 17 #include "content/public/test/test_browser_thread.h" |
| 18 #include "google/cacheinvalidation/include/types.h" |
17 #include "googleurl/src/gurl.h" | 19 #include "googleurl/src/gurl.h" |
18 #include "net/url_request/test_url_fetcher_factory.h" | 20 #include "net/url_request/test_url_fetcher_factory.h" |
19 #include "sync/internal_api/public/base/model_type.h" | 21 #include "sync/internal_api/public/base/model_type.h" |
20 #include "sync/internal_api/public/engine/model_safe_worker.h" | 22 #include "sync/internal_api/public/engine/model_safe_worker.h" |
21 #include "sync/internal_api/public/sync_manager_factory.h" | 23 #include "sync/internal_api/public/sync_manager_factory.h" |
22 #include "sync/internal_api/public/test/fake_sync_manager.h" | 24 #include "sync/internal_api/public/test/fake_sync_manager.h" |
23 #include "sync/internal_api/public/util/experiments.h" | 25 #include "sync/internal_api/public/util/experiments.h" |
| 26 #include "sync/notifier/mock_sync_notifier_observer.h" |
| 27 #include "sync/notifier/notifications_disabled_reason.h" |
24 #include "sync/protocol/encryption.pb.h" | 28 #include "sync/protocol/encryption.pb.h" |
25 #include "sync/protocol/sync_protocol_error.h" | 29 #include "sync/protocol/sync_protocol_error.h" |
26 #include "sync/util/test_unrecoverable_error_handler.h" | 30 #include "sync/util/test_unrecoverable_error_handler.h" |
27 #include "testing/gmock/include/gmock/gmock.h" | 31 #include "testing/gmock/include/gmock/gmock.h" |
28 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
29 | 33 |
30 using content::BrowserThread; | 34 using content::BrowserThread; |
31 using syncer::FakeSyncManager; | 35 using syncer::FakeSyncManager; |
32 using syncer::SyncManager; | 36 using syncer::SyncManager; |
33 using ::testing::InvokeWithoutArgs; | 37 using ::testing::InvokeWithoutArgs; |
| 38 using ::testing::StrictMock; |
34 using ::testing::_; | 39 using ::testing::_; |
35 | 40 |
36 namespace browser_sync { | 41 namespace browser_sync { |
37 | 42 |
38 namespace { | 43 namespace { |
39 | 44 |
40 ACTION_P(Signal, event) { | 45 ACTION_P(Signal, event) { |
41 event->Signal(); | 46 event->Signal(); |
42 } | 47 } |
43 | 48 |
44 void SignalEvent(base::WaitableEvent* event) { | 49 void SignalEvent(base::WaitableEvent* event) { |
45 event->Signal(); | 50 event->Signal(); |
46 } | 51 } |
47 | 52 |
48 static void QuitMessageLoop() { | 53 static void QuitMessageLoop() { |
49 MessageLoop::current()->Quit(); | 54 MessageLoop::current()->Quit(); |
50 } | 55 } |
51 | 56 |
52 class MockSyncFrontend : public SyncFrontend { | 57 class MockSyncFrontend : public SyncFrontend { |
53 public: | 58 public: |
54 virtual ~MockSyncFrontend() {} | 59 virtual ~MockSyncFrontend() {} |
55 | 60 |
| 61 MOCK_METHOD0(OnNotificationsEnabled, void()); |
| 62 MOCK_METHOD1(OnNotificationsDisabled, |
| 63 void(syncer::NotificationsDisabledReason)); |
| 64 MOCK_METHOD2(OnIncomingNotification, |
| 65 void(const syncer::ObjectIdPayloadMap&, |
| 66 syncer::IncomingNotificationSource)); |
56 MOCK_METHOD2(OnBackendInitialized, | 67 MOCK_METHOD2(OnBackendInitialized, |
57 void(const syncer::WeakHandle<syncer::JsBackend>&, bool)); | 68 void(const syncer::WeakHandle<syncer::JsBackend>&, bool)); |
58 MOCK_METHOD0(OnSyncCycleCompleted, void()); | 69 MOCK_METHOD0(OnSyncCycleCompleted, void()); |
59 MOCK_METHOD1(OnConnectionStatusChange, | 70 MOCK_METHOD1(OnConnectionStatusChange, |
60 void(syncer::ConnectionStatus status)); | 71 void(syncer::ConnectionStatus status)); |
61 MOCK_METHOD0(OnStopSyncingPermanently, void()); | 72 MOCK_METHOD0(OnStopSyncingPermanently, void()); |
62 MOCK_METHOD0(OnClearServerDataSucceeded, void()); | 73 MOCK_METHOD0(OnClearServerDataSucceeded, void()); |
63 MOCK_METHOD0(OnClearServerDataFailed, void()); | 74 MOCK_METHOD0(OnClearServerDataFailed, void()); |
64 MOCK_METHOD2(OnPassphraseRequired, | 75 MOCK_METHOD2(OnPassphraseRequired, |
65 void(syncer::PassphraseRequiredReason, | 76 void(syncer::PassphraseRequiredReason, |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 enabled_types_.Put(syncer::AUTOFILL); | 144 enabled_types_.Put(syncer::AUTOFILL); |
134 } | 145 } |
135 | 146 |
136 virtual void TearDown() OVERRIDE { | 147 virtual void TearDown() OVERRIDE { |
137 backend_->StopSyncingForShutdown(); | 148 backend_->StopSyncingForShutdown(); |
138 backend_->Shutdown(false); | 149 backend_->Shutdown(false); |
139 backend_.reset(); | 150 backend_.reset(); |
140 sync_prefs_.reset(); | 151 sync_prefs_.reset(); |
141 invalidator_storage_.reset(); | 152 invalidator_storage_.reset(); |
142 profile_.reset(); | 153 profile_.reset(); |
143 // Pump messages posted by the sync core thread (which may end up | 154 // Pump messages posted by the sync thread (which may end up |
144 // posting on the IO thread). | 155 // posting on the IO thread). |
145 ui_loop_.RunAllPending(); | 156 ui_loop_.RunAllPending(); |
146 io_thread_.Stop(); | 157 io_thread_.Stop(); |
147 // Pump any messages posted by the IO thread. | 158 // Pump any messages posted by the IO thread. |
148 ui_loop_.RunAllPending(); | 159 ui_loop_.RunAllPending(); |
149 } | 160 } |
150 | 161 |
151 // Synchronously initializes the backend. | 162 // Synchronously initializes the backend. |
152 void InitializeBackend() { | 163 void InitializeBackend() { |
153 EXPECT_CALL(mock_frontend_, OnBackendInitialized(_, true)). | 164 EXPECT_CALL(mock_frontend_, OnBackendInitialized(_, true)). |
(...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
533 EXPECT_TRUE(fake_manager_->GetAndResetDownloadedTypes().Equals( | 544 EXPECT_TRUE(fake_manager_->GetAndResetDownloadedTypes().Equals( |
534 Union(new_types, partial_types))); | 545 Union(new_types, partial_types))); |
535 EXPECT_TRUE(Intersection(fake_manager_->GetAndResetCleanedTypes(), | 546 EXPECT_TRUE(Intersection(fake_manager_->GetAndResetCleanedTypes(), |
536 enabled_types_).Empty()); | 547 enabled_types_).Empty()); |
537 EXPECT_TRUE(fake_manager_->InitialSyncEndedTypes().Equals(enabled_types_)); | 548 EXPECT_TRUE(fake_manager_->InitialSyncEndedTypes().Equals(enabled_types_)); |
538 EXPECT_TRUE(fake_manager_->GetAndResetEnabledTypes().Equals(enabled_types_)); | 549 EXPECT_TRUE(fake_manager_->GetAndResetEnabledTypes().Equals(enabled_types_)); |
539 EXPECT_TRUE(fake_manager_->GetTypesWithEmptyProgressMarkerToken( | 550 EXPECT_TRUE(fake_manager_->GetTypesWithEmptyProgressMarkerToken( |
540 enabled_types_).Empty()); | 551 enabled_types_).Empty()); |
541 } | 552 } |
542 | 553 |
| 554 // Register for some IDs and trigger an invalidation. This should |
| 555 // propagate all the way to the frontend. |
| 556 TEST_F(SyncBackendHostTest, Invalidate) { |
| 557 InitializeBackend(); |
| 558 |
| 559 syncer::ObjectIdSet ids; |
| 560 ids.insert(invalidation::ObjectId(1, "id1")); |
| 561 ids.insert(invalidation::ObjectId(2, "id2")); |
| 562 const syncer::ObjectIdPayloadMap& id_payloads = |
| 563 syncer::ObjectIdSetToPayloadMap(ids, "payload"); |
| 564 |
| 565 EXPECT_CALL( |
| 566 mock_frontend_, |
| 567 OnIncomingNotification(id_payloads, syncer::REMOTE_NOTIFICATION)) |
| 568 .WillOnce(InvokeWithoutArgs(QuitMessageLoop)); |
| 569 |
| 570 backend_->UpdateRegisteredInvalidationIds(ids); |
| 571 fake_manager_->Invalidate(id_payloads, syncer::REMOTE_NOTIFICATION); |
| 572 ui_loop_.PostDelayedTask( |
| 573 FROM_HERE, ui_loop_.QuitClosure(), TestTimeouts::action_timeout()); |
| 574 ui_loop_.Run(); |
| 575 } |
| 576 |
| 577 // Register for some IDs and turn on notifications. This should |
| 578 // propagate all the way to the frontend. |
| 579 TEST_F(SyncBackendHostTest, EnableNotifications) { |
| 580 InitializeBackend(); |
| 581 |
| 582 EXPECT_CALL(mock_frontend_, OnNotificationsEnabled()) |
| 583 .WillOnce(InvokeWithoutArgs(QuitMessageLoop)); |
| 584 |
| 585 syncer::ObjectIdSet ids; |
| 586 ids.insert(invalidation::ObjectId(3, "id3")); |
| 587 backend_->UpdateRegisteredInvalidationIds(ids); |
| 588 fake_manager_->EnableNotifications(); |
| 589 ui_loop_.PostDelayedTask( |
| 590 FROM_HERE, ui_loop_.QuitClosure(), TestTimeouts::action_timeout()); |
| 591 ui_loop_.Run(); |
| 592 } |
| 593 |
| 594 // Register for some IDs and turn off notifications. This should |
| 595 // propagate all the way to the frontend. |
| 596 TEST_F(SyncBackendHostTest, DisableNotifications) { |
| 597 InitializeBackend(); |
| 598 |
| 599 EXPECT_CALL(mock_frontend_, |
| 600 OnNotificationsDisabled(syncer::TRANSIENT_NOTIFICATION_ERROR)) |
| 601 .WillOnce(InvokeWithoutArgs(QuitMessageLoop)); |
| 602 |
| 603 syncer::ObjectIdSet ids; |
| 604 ids.insert(invalidation::ObjectId(4, "id4")); |
| 605 backend_->UpdateRegisteredInvalidationIds(ids); |
| 606 fake_manager_->DisableNotifications(syncer::TRANSIENT_NOTIFICATION_ERROR); |
| 607 ui_loop_.PostDelayedTask( |
| 608 FROM_HERE, ui_loop_.QuitClosure(), TestTimeouts::action_timeout()); |
| 609 ui_loop_.Run(); |
| 610 } |
| 611 |
543 } // namespace | 612 } // namespace |
544 | 613 |
545 } // namespace browser_sync | 614 } // namespace browser_sync |
OLD | NEW |