1// Copyright 2014 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 "components/invalidation/invalidation_logger.h" 6#include "components/invalidation/invalidation_logger_observer.h" 7 8#include "testing/gtest/include/gtest/gtest.h" 9 10namespace invalidation { 11 12class InvalidationLoggerObserverTest : public InvalidationLoggerObserver { 13 public: 14 InvalidationLoggerObserverTest() { ResetStates(); } 15 16 void ResetStates() { 17 registration_change_received = false; 18 state_received = false; 19 update_id_received = false; 20 debug_message_received = false; 21 invalidation_received = false; 22 detailed_status_received = false; 23 update_id_replicated = std::map<std::string, syncer::ObjectIdCountMap>(); 24 registered_handlers = std::multiset<std::string>(); 25 } 26 27 virtual void OnRegistrationChange(const std::multiset<std::string>& handlers) 28 OVERRIDE { 29 registered_handlers = handlers; 30 registration_change_received = true; 31 } 32 33 virtual void OnStateChange(const syncer::InvalidatorState& new_state, 34 const base::Time& last_change_timestamp) 35 OVERRIDE { 36 state_received = true; 37 } 38 39 virtual void OnUpdateIds(const std::string& handler, 40 const syncer::ObjectIdCountMap& details) OVERRIDE { 41 update_id_received = true; 42 update_id_replicated[handler] = details; 43 } 44 45 virtual void OnDebugMessage(const base::DictionaryValue& details) OVERRIDE { 46 debug_message_received = true; 47 } 48 49 virtual void OnInvalidation( 50 const syncer::ObjectIdInvalidationMap& new_invalidations) OVERRIDE { 51 invalidation_received = true; 52 } 53 54 virtual void OnDetailedStatus(const base::DictionaryValue& details) OVERRIDE { 55 detailed_status_received = true; 56 } 57 58 bool registration_change_received; 59 bool state_received; 60 bool update_id_received; 61 bool debug_message_received; 62 bool invalidation_received; 63 bool detailed_status_received; 64 std::map<std::string, syncer::ObjectIdCountMap> update_id_replicated; 65 std::multiset<std::string> registered_handlers; 66}; 67 68// Test that the callbacks are actually being called when observers are 69// registered and don't produce any other callback in the meantime. 70TEST(InvalidationLoggerTest, TestCallbacks) { 71 InvalidationLogger log; 72 InvalidationLoggerObserverTest observer_test; 73 74 log.RegisterObserver(&observer_test); 75 log.OnStateChange(syncer::INVALIDATIONS_ENABLED); 76 EXPECT_TRUE(observer_test.state_received); 77 EXPECT_FALSE(observer_test.update_id_received); 78 EXPECT_FALSE(observer_test.registration_change_received); 79 EXPECT_FALSE(observer_test.invalidation_received); 80 EXPECT_FALSE(observer_test.debug_message_received); 81 EXPECT_FALSE(observer_test.detailed_status_received); 82 83 observer_test.ResetStates(); 84 85 log.OnInvalidation(syncer::ObjectIdInvalidationMap()); 86 EXPECT_TRUE(observer_test.invalidation_received); 87 EXPECT_FALSE(observer_test.state_received); 88 EXPECT_FALSE(observer_test.update_id_received); 89 EXPECT_FALSE(observer_test.registration_change_received); 90 EXPECT_FALSE(observer_test.debug_message_received); 91 EXPECT_FALSE(observer_test.detailed_status_received); 92 93 log.UnregisterObserver(&observer_test); 94} 95 96// Test that after registering an observer and then unregistering it 97// no callbacks regarding that observer are called. 98// (i.e. the observer is cleanly removed) 99TEST(InvalidationLoggerTest, TestReleaseOfObserver) { 100 InvalidationLogger log; 101 InvalidationLoggerObserverTest observer_test; 102 103 log.RegisterObserver(&observer_test); 104 log.UnregisterObserver(&observer_test); 105 106 log.OnInvalidation(syncer::ObjectIdInvalidationMap()); 107 log.OnStateChange(syncer::INVALIDATIONS_ENABLED); 108 log.OnRegistration(std::string()); 109 log.OnUnregistration(std::string()); 110 log.OnDebugMessage(base::DictionaryValue()); 111 log.OnUpdateIds(std::map<std::string, syncer::ObjectIdSet>()); 112 EXPECT_FALSE(observer_test.registration_change_received); 113 EXPECT_FALSE(observer_test.update_id_received); 114 EXPECT_FALSE(observer_test.invalidation_received); 115 EXPECT_FALSE(observer_test.state_received); 116 EXPECT_FALSE(observer_test.debug_message_received); 117 EXPECT_FALSE(observer_test.detailed_status_received); 118} 119 120// Test the EmitContet in InvalidationLogger is actually 121// sending state and updateIds notifications. 122TEST(InvalidationLoggerTest, TestEmitContent) { 123 InvalidationLogger log; 124 InvalidationLoggerObserverTest observer_test; 125 126 log.RegisterObserver(&observer_test); 127 EXPECT_FALSE(observer_test.state_received); 128 EXPECT_FALSE(observer_test.update_id_received); 129 log.EmitContent(); 130 // Expect state and registered handlers only because no Ids were registered. 131 EXPECT_TRUE(observer_test.state_received); 132 EXPECT_TRUE(observer_test.registration_change_received); 133 EXPECT_FALSE(observer_test.update_id_received); 134 EXPECT_FALSE(observer_test.invalidation_received); 135 EXPECT_FALSE(observer_test.debug_message_received); 136 EXPECT_FALSE(observer_test.detailed_status_received); 137 138 observer_test.ResetStates(); 139 std::map<std::string, syncer::ObjectIdSet> test_map; 140 test_map["Test"] = syncer::ObjectIdSet(); 141 log.OnUpdateIds(test_map); 142 EXPECT_TRUE(observer_test.update_id_received); 143 observer_test.ResetStates(); 144 145 log.EmitContent(); 146 // Expect now state, ids and registered handlers change. 147 EXPECT_TRUE(observer_test.state_received); 148 EXPECT_TRUE(observer_test.update_id_received); 149 EXPECT_TRUE(observer_test.registration_change_received); 150 EXPECT_FALSE(observer_test.invalidation_received); 151 EXPECT_FALSE(observer_test.debug_message_received); 152 EXPECT_FALSE(observer_test.detailed_status_received); 153 log.UnregisterObserver(&observer_test); 154} 155 156// Test that the updateId notification actually sends the same ObjectId that 157// was sent to the Observer. 158// The ObserverTest rebuilds the map that was sent in pieces by the logger. 159TEST(InvalidationLoggerTest, TestUpdateIdsMap) { 160 InvalidationLogger log; 161 InvalidationLoggerObserverTest observer_test; 162 std::map<std::string, syncer::ObjectIdSet> send_test_map; 163 std::map<std::string, syncer::ObjectIdCountMap> expected_received_map; 164 log.RegisterObserver(&observer_test); 165 166 syncer::ObjectIdSet sync_set_A; 167 syncer::ObjectIdCountMap counted_sync_set_A; 168 169 ObjectId o1(1000, "DataType1"); 170 sync_set_A.insert(o1); 171 counted_sync_set_A[o1] = 0; 172 173 ObjectId o2(1000, "DataType2"); 174 sync_set_A.insert(o2); 175 counted_sync_set_A[o2] = 0; 176 177 syncer::ObjectIdSet sync_set_B; 178 syncer::ObjectIdCountMap counted_sync_set_B; 179 180 ObjectId o3(1020, "DataTypeA"); 181 sync_set_B.insert(o3); 182 counted_sync_set_B[o3] = 0; 183 184 send_test_map["TestA"] = sync_set_A; 185 send_test_map["TestB"] = sync_set_B; 186 expected_received_map["TestA"] = counted_sync_set_A; 187 expected_received_map["TestB"] = counted_sync_set_B; 188 189 // Send the objects ids registered for the two different handler name. 190 log.OnUpdateIds(send_test_map); 191 EXPECT_EQ(expected_received_map, observer_test.update_id_replicated); 192 193 syncer::ObjectIdSet sync_set_B2; 194 syncer::ObjectIdCountMap counted_sync_set_B2; 195 196 ObjectId o4(1020, "DataTypeF"); 197 sync_set_B2.insert(o4); 198 counted_sync_set_B2[o4] = 0; 199 200 ObjectId o5(1020, "DataTypeG"); 201 sync_set_B2.insert(o5); 202 counted_sync_set_B2[o5] = 0; 203 204 send_test_map["TestB"] = sync_set_B2; 205 expected_received_map["TestB"] = counted_sync_set_B2; 206 207 // Test now that if we replace the registered datatypes for TestB, the 208 // original don't show up again. 209 log.OnUpdateIds(send_test_map); 210 EXPECT_EQ(expected_received_map, observer_test.update_id_replicated); 211 212 // The emit content should return the same map too. 213 observer_test.ResetStates(); 214 log.EmitContent(); 215 EXPECT_EQ(expected_received_map, observer_test.update_id_replicated); 216 log.UnregisterObserver(&observer_test); 217} 218 219// Test that the invalidation notification changes the total count 220// of invalidations received for that datatype. 221TEST(InvalidationLoggerTest, TestInvalidtionsTotalCount) { 222 InvalidationLogger log; 223 InvalidationLoggerObserverTest observer_test; 224 log.RegisterObserver(&observer_test); 225 226 std::map<std::string, syncer::ObjectIdSet> send_test_map; 227 std::map<std::string, syncer::ObjectIdCountMap> expected_received_map; 228 syncer::ObjectIdSet sync_set; 229 syncer::ObjectIdCountMap counted_sync_set; 230 231 ObjectId o1(1020, "DataTypeA"); 232 sync_set.insert(o1); 233 counted_sync_set[o1] = 1; 234 235 // Generate invalidation for datatype A only. 236 syncer::ObjectIdInvalidationMap fake_invalidations = 237 syncer::ObjectIdInvalidationMap::InvalidateAll(sync_set); 238 239 ObjectId o2(1040, "DataTypeB"); 240 sync_set.insert(o2); 241 counted_sync_set[o2] = 0; 242 243 // Registed the two objectIds and send an invalidation only for the 244 // Datatype A. 245 send_test_map["Test"] = sync_set; 246 log.OnUpdateIds(send_test_map); 247 log.OnInvalidation(fake_invalidations); 248 249 expected_received_map["Test"] = counted_sync_set; 250 251 // Reset the state of the observer to receive the ObjectIds with the 252 // count of invalidations received (1 and 0). 253 observer_test.ResetStates(); 254 log.EmitContent(); 255 EXPECT_EQ(expected_received_map, observer_test.update_id_replicated); 256 257 log.UnregisterObserver(&observer_test); 258} 259 260// Test that registered handlers are being sent to the observers. 261TEST(InvalidationLoggerTest, TestRegisteredHandlers) { 262 InvalidationLogger log; 263 InvalidationLoggerObserverTest observer_test; 264 log.RegisterObserver(&observer_test); 265 266 log.OnRegistration(std::string("FakeHandler1")); 267 std::multiset<std::string> test_multiset; 268 test_multiset.insert("FakeHandler1"); 269 EXPECT_TRUE(observer_test.registration_change_received); 270 EXPECT_EQ(observer_test.registered_handlers, test_multiset); 271 272 observer_test.ResetStates(); 273 log.OnRegistration(std::string("FakeHandler2")); 274 test_multiset.insert("FakeHandler2"); 275 EXPECT_TRUE(observer_test.registration_change_received); 276 EXPECT_EQ(observer_test.registered_handlers, test_multiset); 277 278 observer_test.ResetStates(); 279 log.OnUnregistration(std::string("FakeHandler2")); 280 test_multiset.erase("FakeHandler2"); 281 EXPECT_TRUE(observer_test.registration_change_received); 282 EXPECT_EQ(observer_test.registered_handlers, test_multiset); 283 284 log.UnregisterObserver(&observer_test); 285} 286} // namespace invalidation 287