| 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/bridged_invalidator.h" | 5 #include "chrome/browser/sync/glue/bridged_invalidator.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/memory/weak_ptr.h" |
| 11 #include "base/threading/thread.h" | 12 #include "base/threading/thread.h" |
| 12 #include "chrome/browser/sync/glue/chrome_sync_notification_bridge.h" | 13 #include "chrome/browser/sync/glue/chrome_sync_notification_bridge.h" |
| 13 #include "chrome/test/base/profile_mock.h" | 14 #include "chrome/test/base/profile_mock.h" |
| 14 #include "content/public/test/test_browser_thread.h" | 15 #include "content/public/test/test_browser_thread.h" |
| 16 #include "google/cacheinvalidation/include/types.h" |
| 15 #include "sync/internal_api/public/base/model_type.h" | 17 #include "sync/internal_api/public/base/model_type.h" |
| 16 #include "sync/internal_api/public/base/model_type_test_util.h" | 18 #include "sync/internal_api/public/base/model_type_test_util.h" |
| 17 #include "sync/notifier/fake_invalidation_handler.h" | 19 #include "sync/notifier/fake_invalidation_handler.h" |
| 18 #include "sync/notifier/fake_invalidator.h" | 20 #include "sync/notifier/fake_invalidator.h" |
| 19 #include "sync/notifier/invalidator.h" | 21 #include "sync/notifier/invalidator_test_template.h" |
| 22 #include "sync/notifier/object_id_state_map_test_util.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" | 23 #include "testing/gmock/include/gmock/gmock.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 25 |
| 26 namespace syncer { |
| 27 class InvalidationStateTracker; |
| 28 } // namespace syncer |
| 29 |
| 23 namespace browser_sync { | 30 namespace browser_sync { |
| 31 |
| 32 // BridgedInvalidatorTestDelegate has to be visible from the syncer |
| 33 // namespace (where InvalidatorTest lives). |
| 34 class BridgedInvalidatorTestDelegate { |
| 35 public: |
| 36 BridgedInvalidatorTestDelegate() |
| 37 : ui_thread_(content::BrowserThread::UI, &ui_loop_), |
| 38 bridge_(&mock_profile_, ui_loop_.message_loop_proxy()), |
| 39 fake_delegate_(NULL) { |
| 40 // Pump |ui_loop_| to fully initialize |bridge_|. |
| 41 ui_loop_.RunAllPending(); |
| 42 } |
| 43 |
| 44 ~BridgedInvalidatorTestDelegate() { |
| 45 DestroyInvalidator(); |
| 46 } |
| 47 |
| 48 void CreateInvalidator( |
| 49 const std::string& /* initial_state (unused) */, |
| 50 const base::WeakPtr<syncer::InvalidationStateTracker>& |
| 51 /* invalidation_state_tracker (unused) */) { |
| 52 DCHECK(!fake_delegate_); |
| 53 DCHECK(!invalidator_.get()); |
| 54 fake_delegate_ = new syncer::FakeInvalidator(); |
| 55 invalidator_.reset(new BridgedInvalidator(&bridge_, fake_delegate_)); |
| 56 } |
| 57 |
| 58 BridgedInvalidator* GetInvalidator() { |
| 59 return invalidator_.get(); |
| 60 } |
| 61 |
| 62 ChromeSyncNotificationBridge* GetBridge() { |
| 63 return &bridge_; |
| 64 } |
| 65 |
| 66 syncer::FakeInvalidator* GetFakeDelegate() { |
| 67 return fake_delegate_; |
| 68 } |
| 69 |
| 70 void DestroyInvalidator() { |
| 71 invalidator_.reset(); |
| 72 fake_delegate_ = NULL; |
| 73 bridge_.StopForShutdown(); |
| 74 ui_loop_.RunAllPending(); |
| 75 } |
| 76 |
| 77 void WaitForInvalidator() { |
| 78 // Do nothing. |
| 79 } |
| 80 |
| 81 void TriggerOnNotificationsEnabled() { |
| 82 fake_delegate_->EmitOnNotificationsEnabled(); |
| 83 } |
| 84 |
| 85 void TriggerOnIncomingNotification( |
| 86 const syncer::ObjectIdStateMap& id_state_map, |
| 87 syncer::IncomingNotificationSource source) { |
| 88 fake_delegate_->EmitOnIncomingNotification(id_state_map, source); |
| 89 } |
| 90 |
| 91 void TriggerOnNotificationsDisabled( |
| 92 syncer::NotificationsDisabledReason reason) { |
| 93 fake_delegate_->EmitOnNotificationsDisabled(reason); |
| 94 } |
| 95 |
| 96 static bool InvalidatorHandlesDeprecatedState() { |
| 97 return false; |
| 98 } |
| 99 |
| 100 private: |
| 101 MessageLoop ui_loop_; |
| 102 content::TestBrowserThread ui_thread_; |
| 103 ::testing::NiceMock<ProfileMock> mock_profile_; |
| 104 ChromeSyncNotificationBridge bridge_; |
| 105 // Owned by |invalidator_|. |
| 106 syncer::FakeInvalidator* fake_delegate_; |
| 107 scoped_ptr<BridgedInvalidator> invalidator_; |
| 108 }; |
| 109 |
| 24 namespace { | 110 namespace { |
| 25 | 111 |
| 26 using ::testing::NiceMock; | |
| 27 using ::testing::StrictMock; | |
| 28 using content::BrowserThread; | |
| 29 using syncer::HasModelTypes; | |
| 30 | |
| 31 class MockChromeSyncNotificationBridge : public ChromeSyncNotificationBridge { | |
| 32 public: | |
| 33 MockChromeSyncNotificationBridge( | |
| 34 const Profile* profile, | |
| 35 const scoped_refptr<base::SequencedTaskRunner>& sync_task_runner) | |
| 36 : ChromeSyncNotificationBridge(profile, sync_task_runner) {} | |
| 37 virtual ~MockChromeSyncNotificationBridge() {} | |
| 38 | |
| 39 MOCK_METHOD1(RegisterHandler, void(syncer::InvalidationHandler*)); | |
| 40 MOCK_METHOD2(UpdateRegisteredIds, | |
| 41 void(syncer::InvalidationHandler*, | |
| 42 const syncer::ObjectIdSet&)); | |
| 43 MOCK_METHOD1(UnregisterHandler, void(syncer::InvalidationHandler*)); | |
| 44 }; | |
| 45 | |
| 46 // All tests just verify that each call is passed through to the delegate, with | 112 // All tests just verify that each call is passed through to the delegate, with |
| 47 // the exception of RegisterHandler, UnregisterHandler, and | 113 // the exception of RegisterHandler, UnregisterHandler, and |
| 48 // UpdateRegisteredIds, which also verifies the call is forwarded to the | 114 // UpdateRegisteredIds, which also verifies the call is forwarded to the |
| 49 // bridge. | 115 // bridge. |
| 50 class BridgedInvalidatorTest : public testing::Test { | 116 class BridgedInvalidatorTest : public testing::Test { |
| 51 public: | 117 public: |
| 52 BridgedInvalidatorTest() | 118 BridgedInvalidatorTest() { |
| 53 : ui_thread_(BrowserThread::UI, &ui_loop_), | 119 delegate_.CreateInvalidator( |
| 54 mock_bridge_(&mock_profile_, ui_loop_.message_loop_proxy()), | 120 "fake_state", base::WeakPtr<syncer::InvalidationStateTracker>()); |
| 55 // Owned by bridged_invalidator_. | 121 } |
| 56 fake_delegate_(new syncer::FakeInvalidator()), | 122 |
| 57 bridged_invalidator_(&mock_bridge_, fake_delegate_) {} | |
| 58 virtual ~BridgedInvalidatorTest() {} | 123 virtual ~BridgedInvalidatorTest() {} |
| 59 | 124 |
| 60 protected: | 125 protected: |
| 61 MessageLoop ui_loop_; | 126 BridgedInvalidatorTestDelegate delegate_; |
| 62 content::TestBrowserThread ui_thread_; | |
| 63 NiceMock<ProfileMock> mock_profile_; | |
| 64 StrictMock<MockChromeSyncNotificationBridge> mock_bridge_; | |
| 65 syncer::FakeInvalidator* fake_delegate_; | |
| 66 BridgedInvalidator bridged_invalidator_; | |
| 67 }; | 127 }; |
| 68 | 128 |
| 69 TEST_F(BridgedInvalidatorTest, RegisterHandler) { | 129 TEST_F(BridgedInvalidatorTest, RegisterHandler) { |
| 70 const syncer::ObjectIdSet& ids = | 130 syncer::ObjectIdSet ids; |
| 71 syncer::ModelTypeSetToObjectIdSet( | 131 ids.insert(invalidation::ObjectId(1, "id1")); |
| 72 syncer::ModelTypeSet(syncer::APPS, syncer::PREFERENCES)); | |
| 73 | 132 |
| 74 syncer::FakeInvalidationHandler handler; | 133 syncer::FakeInvalidationHandler handler; |
| 75 EXPECT_CALL(mock_bridge_, RegisterHandler(&handler)); | |
| 76 EXPECT_CALL(mock_bridge_, UpdateRegisteredIds(&handler, ids)); | |
| 77 EXPECT_CALL(mock_bridge_, UnregisterHandler(&handler)); | |
| 78 | 134 |
| 79 bridged_invalidator_.RegisterHandler(&handler); | 135 delegate_.GetInvalidator()->RegisterHandler(&handler); |
| 80 EXPECT_TRUE(fake_delegate_->IsHandlerRegistered(&handler)); | 136 EXPECT_TRUE(delegate_.GetFakeDelegate()->IsHandlerRegistered(&handler)); |
| 137 EXPECT_TRUE(delegate_.GetBridge()->IsHandlerRegisteredForTest(&handler)); |
| 81 | 138 |
| 82 bridged_invalidator_.UpdateRegisteredIds(&handler, ids); | 139 delegate_.GetInvalidator()->UpdateRegisteredIds(&handler, ids); |
| 83 EXPECT_EQ(ids, fake_delegate_->GetRegisteredIds(&handler)); | 140 EXPECT_EQ(ids, delegate_.GetFakeDelegate()->GetRegisteredIds(&handler)); |
| 141 EXPECT_EQ(ids, delegate_.GetBridge()->GetRegisteredIdsForTest(&handler)); |
| 84 | 142 |
| 85 bridged_invalidator_.UnregisterHandler(&handler); | 143 delegate_.GetInvalidator()->UnregisterHandler(&handler); |
| 86 EXPECT_FALSE(fake_delegate_->IsHandlerRegistered(&handler)); | 144 EXPECT_FALSE(delegate_.GetFakeDelegate()->IsHandlerRegistered(&handler)); |
| 145 EXPECT_FALSE(delegate_.GetBridge()->IsHandlerRegisteredForTest(&handler)); |
| 87 } | 146 } |
| 88 | 147 |
| 89 TEST_F(BridgedInvalidatorTest, SetUniqueId) { | 148 TEST_F(BridgedInvalidatorTest, SetUniqueId) { |
| 90 const std::string& unique_id = "unique id"; | 149 const std::string& unique_id = "unique id"; |
| 91 bridged_invalidator_.SetUniqueId(unique_id); | 150 delegate_.GetInvalidator()->SetUniqueId(unique_id); |
| 92 EXPECT_EQ(unique_id, fake_delegate_->GetUniqueId()); | 151 EXPECT_EQ(unique_id, delegate_.GetFakeDelegate()->GetUniqueId()); |
| 93 } | 152 } |
| 94 | 153 |
| 95 TEST_F(BridgedInvalidatorTest, SetStateDeprecated) { | 154 TEST_F(BridgedInvalidatorTest, SetStateDeprecated) { |
| 96 const std::string& state = "state"; | 155 const std::string& state = "state"; |
| 97 bridged_invalidator_.SetStateDeprecated(state); | 156 delegate_.GetInvalidator()->SetStateDeprecated(state); |
| 98 EXPECT_EQ(state, fake_delegate_->GetStateDeprecated()); | 157 EXPECT_EQ(state, delegate_.GetFakeDelegate()->GetStateDeprecated()); |
| 99 } | 158 } |
| 100 | 159 |
| 101 TEST_F(BridgedInvalidatorTest, UpdateCredentials) { | 160 TEST_F(BridgedInvalidatorTest, UpdateCredentials) { |
| 102 const std::string& email = "email"; | 161 const std::string& email = "email"; |
| 103 const std::string& token = "token"; | 162 const std::string& token = "token"; |
| 104 bridged_invalidator_.UpdateCredentials(email, token); | 163 delegate_.GetInvalidator()->UpdateCredentials(email, token); |
| 105 EXPECT_EQ(email, fake_delegate_->GetCredentialsEmail()); | 164 EXPECT_EQ(email, delegate_.GetFakeDelegate()->GetCredentialsEmail()); |
| 106 EXPECT_EQ(token, fake_delegate_->GetCredentialsToken()); | 165 EXPECT_EQ(token, delegate_.GetFakeDelegate()->GetCredentialsToken()); |
| 107 } | 166 } |
| 108 | 167 |
| 109 TEST_F(BridgedInvalidatorTest, SendNotification) { | 168 TEST_F(BridgedInvalidatorTest, SendNotification) { |
| 110 const syncer::ModelTypeSet changed_types( | 169 syncer::ObjectIdSet ids; |
| 111 syncer::SESSIONS, syncer::EXTENSIONS); | 170 ids.insert(invalidation::ObjectId(1, "id1")); |
| 112 bridged_invalidator_.SendNotification(changed_types); | 171 ids.insert(invalidation::ObjectId(2, "id2")); |
| 113 EXPECT_TRUE(fake_delegate_->GetLastChangedTypes().Equals(changed_types)); | 172 const syncer::ObjectIdStateMap& id_state_map = |
| 173 syncer::ObjectIdSetToStateMap(ids, "payload"); |
| 174 delegate_.GetInvalidator()->SendNotification(id_state_map); |
| 175 EXPECT_THAT(id_state_map, |
| 176 Eq(delegate_.GetFakeDelegate()->GetLastSentIdStateMap())); |
| 114 } | 177 } |
| 115 | 178 |
| 116 } // namespace | 179 } // namespace |
| 117 } // namespace browser_sync | 180 } // namespace browser_sync |
| 181 |
| 182 namespace syncer { |
| 183 namespace { |
| 184 |
| 185 INSTANTIATE_TYPED_TEST_CASE_P( |
| 186 BridgedInvalidatorTest, InvalidatorTest, |
| 187 ::browser_sync::BridgedInvalidatorTestDelegate); |
| 188 |
| 189 } // namespace |
| 190 } // namespace syncer |
| OLD | NEW |