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