| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/sync/glue/bridged_invalidator.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/compiler_specific.h" | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "base/memory/weak_ptr.h" | |
| 12 #include "base/threading/thread.h" | |
| 13 #include "chrome/browser/sync/glue/chrome_sync_notification_bridge.h" | |
| 14 #include "chrome/test/base/profile_mock.h" | |
| 15 #include "content/public/test/test_browser_thread.h" | |
| 16 #include "google/cacheinvalidation/include/types.h" | |
| 17 #include "sync/internal_api/public/base/model_type.h" | |
| 18 #include "sync/internal_api/public/base/model_type_test_util.h" | |
| 19 #include "sync/notifier/fake_invalidation_handler.h" | |
| 20 #include "sync/notifier/fake_invalidator.h" | |
| 21 #include "sync/notifier/invalidator_test_template.h" | |
| 22 #include "sync/notifier/object_id_invalidation_map_test_util.h" | |
| 23 #include "testing/gmock/include/gmock/gmock.h" | |
| 24 #include "testing/gtest/include/gtest/gtest.h" | |
| 25 | |
| 26 namespace syncer { | |
| 27 class InvalidationStateTracker; | |
| 28 } // namespace syncer | |
| 29 | |
| 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_.RunUntilIdle(); | |
| 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( | |
| 56 &bridge_, fake_delegate_, syncer::DEFAULT_INVALIDATION_ERROR)); | |
| 57 } | |
| 58 | |
| 59 BridgedInvalidator* GetInvalidator() { | |
| 60 return invalidator_.get(); | |
| 61 } | |
| 62 | |
| 63 ChromeSyncNotificationBridge* GetBridge() { | |
| 64 return &bridge_; | |
| 65 } | |
| 66 | |
| 67 syncer::FakeInvalidator* GetFakeDelegate() { | |
| 68 return fake_delegate_; | |
| 69 } | |
| 70 | |
| 71 void DestroyInvalidator() { | |
| 72 invalidator_.reset(); | |
| 73 fake_delegate_ = NULL; | |
| 74 bridge_.StopForShutdown(); | |
| 75 ui_loop_.RunUntilIdle(); | |
| 76 } | |
| 77 | |
| 78 void WaitForInvalidator() { | |
| 79 // Do nothing. | |
| 80 } | |
| 81 | |
| 82 void TriggerOnInvalidatorStateChange(syncer::InvalidatorState state) { | |
| 83 fake_delegate_->EmitOnInvalidatorStateChange(state); | |
| 84 } | |
| 85 | |
| 86 void TriggerOnIncomingInvalidation( | |
| 87 const syncer::ObjectIdInvalidationMap& invalidation_map, | |
| 88 syncer::IncomingInvalidationSource source) { | |
| 89 fake_delegate_->EmitOnIncomingInvalidation(invalidation_map, source); | |
| 90 } | |
| 91 | |
| 92 static bool InvalidatorHandlesDeprecatedState() { | |
| 93 return false; | |
| 94 } | |
| 95 | |
| 96 private: | |
| 97 MessageLoop ui_loop_; | |
| 98 content::TestBrowserThread ui_thread_; | |
| 99 ::testing::NiceMock<ProfileMock> mock_profile_; | |
| 100 ChromeSyncNotificationBridge bridge_; | |
| 101 // Owned by |invalidator_|. | |
| 102 syncer::FakeInvalidator* fake_delegate_; | |
| 103 scoped_ptr<BridgedInvalidator> invalidator_; | |
| 104 }; | |
| 105 | |
| 106 namespace { | |
| 107 | |
| 108 // All tests just verify that each call is passed through to the delegate, with | |
| 109 // the exception of RegisterHandler, UnregisterHandler, and | |
| 110 // UpdateRegisteredIds, which also verifies the call is forwarded to the | |
| 111 // bridge. | |
| 112 class BridgedInvalidatorTest : public testing::Test { | |
| 113 public: | |
| 114 BridgedInvalidatorTest() { | |
| 115 delegate_.CreateInvalidator( | |
| 116 "fake_state", base::WeakPtr<syncer::InvalidationStateTracker>()); | |
| 117 } | |
| 118 | |
| 119 virtual ~BridgedInvalidatorTest() {} | |
| 120 | |
| 121 protected: | |
| 122 BridgedInvalidatorTestDelegate delegate_; | |
| 123 }; | |
| 124 | |
| 125 TEST_F(BridgedInvalidatorTest, HandlerMethods) { | |
| 126 syncer::ObjectIdSet ids; | |
| 127 ids.insert(invalidation::ObjectId(1, "id1")); | |
| 128 | |
| 129 syncer::FakeInvalidationHandler handler; | |
| 130 | |
| 131 delegate_.GetInvalidator()->RegisterHandler(&handler); | |
| 132 EXPECT_TRUE(delegate_.GetFakeDelegate()->IsHandlerRegistered(&handler)); | |
| 133 EXPECT_TRUE(delegate_.GetBridge()->IsHandlerRegisteredForTest(&handler)); | |
| 134 | |
| 135 delegate_.GetInvalidator()->UpdateRegisteredIds(&handler, ids); | |
| 136 EXPECT_EQ(ids, delegate_.GetFakeDelegate()->GetRegisteredIds(&handler)); | |
| 137 EXPECT_EQ(ids, delegate_.GetBridge()->GetRegisteredIdsForTest(&handler)); | |
| 138 | |
| 139 delegate_.GetInvalidator()->UnregisterHandler(&handler); | |
| 140 EXPECT_FALSE(delegate_.GetFakeDelegate()->IsHandlerRegistered(&handler)); | |
| 141 EXPECT_FALSE(delegate_.GetBridge()->IsHandlerRegisteredForTest(&handler)); | |
| 142 } | |
| 143 | |
| 144 TEST_F(BridgedInvalidatorTest, GetInvalidatorState) { | |
| 145 EXPECT_EQ(syncer::DEFAULT_INVALIDATION_ERROR, | |
| 146 delegate_.GetInvalidator()->GetInvalidatorState()); | |
| 147 } | |
| 148 | |
| 149 TEST_F(BridgedInvalidatorTest, SetUniqueId) { | |
| 150 const std::string& unique_id = "unique id"; | |
| 151 delegate_.GetInvalidator()->SetUniqueId(unique_id); | |
| 152 EXPECT_EQ(unique_id, delegate_.GetFakeDelegate()->GetUniqueId()); | |
| 153 } | |
| 154 | |
| 155 TEST_F(BridgedInvalidatorTest, SetStateDeprecated) { | |
| 156 const std::string& state = "state"; | |
| 157 delegate_.GetInvalidator()->SetStateDeprecated(state); | |
| 158 EXPECT_EQ(state, delegate_.GetFakeDelegate()->GetStateDeprecated()); | |
| 159 } | |
| 160 | |
| 161 TEST_F(BridgedInvalidatorTest, UpdateCredentials) { | |
| 162 const std::string& email = "email"; | |
| 163 const std::string& token = "token"; | |
| 164 delegate_.GetInvalidator()->UpdateCredentials(email, token); | |
| 165 EXPECT_EQ(email, delegate_.GetFakeDelegate()->GetCredentialsEmail()); | |
| 166 EXPECT_EQ(token, delegate_.GetFakeDelegate()->GetCredentialsToken()); | |
| 167 } | |
| 168 | |
| 169 TEST_F(BridgedInvalidatorTest, SendInvalidation) { | |
| 170 syncer::ObjectIdSet ids; | |
| 171 ids.insert(invalidation::ObjectId(1, "id1")); | |
| 172 ids.insert(invalidation::ObjectId(2, "id2")); | |
| 173 const syncer::ObjectIdInvalidationMap& invalidation_map = | |
| 174 syncer::ObjectIdSetToInvalidationMap(ids, "payload"); | |
| 175 delegate_.GetInvalidator()->SendInvalidation(invalidation_map); | |
| 176 EXPECT_THAT(invalidation_map, | |
| 177 Eq(delegate_.GetFakeDelegate()->GetLastSentInvalidationMap())); | |
| 178 } | |
| 179 | |
| 180 } // namespace | |
| 181 } // namespace browser_sync | |
| 182 | |
| 183 namespace syncer { | |
| 184 namespace { | |
| 185 | |
| 186 INSTANTIATE_TYPED_TEST_CASE_P( | |
| 187 BridgedInvalidatorTest, InvalidatorTest, | |
| 188 ::browser_sync::BridgedInvalidatorTestDelegate); | |
| 189 | |
| 190 } // namespace | |
| 191 } // namespace syncer | |
| OLD | NEW |