Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / components / invalidation / invalidation_logger_unittest.cc
blob2ee0f8d7afa32822e7b2cefdc2cafcc580b16e73
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.
5 #include "components/invalidation/invalidation_logger.h"
6 #include "components/invalidation/invalidation_logger_observer.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 namespace invalidation {
12 class InvalidationLoggerObserverTest : public InvalidationLoggerObserver {
13 public:
14 InvalidationLoggerObserverTest() { ResetStates(); }
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>();
27 virtual void OnRegistrationChange(const std::multiset<std::string>& handlers)
28 OVERRIDE {
29 registered_handlers = handlers;
30 registration_change_received = true;
33 virtual void OnStateChange(const syncer::InvalidatorState& new_state,
34 const base::Time& last_change_timestamp)
35 OVERRIDE {
36 state_received = true;
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;
45 virtual void OnDebugMessage(const base::DictionaryValue& details) OVERRIDE {
46 debug_message_received = true;
49 virtual void OnInvalidation(
50 const syncer::ObjectIdInvalidationMap& new_invalidations) OVERRIDE {
51 invalidation_received = true;
54 virtual void OnDetailedStatus(const base::DictionaryValue& details) OVERRIDE {
55 detailed_status_received = true;
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;
68 // Test that the callbacks are actually being called when observers are
69 // registered and don't produce any other callback in the meantime.
70 TEST(InvalidationLoggerTest, TestCallbacks) {
71 InvalidationLogger log;
72 InvalidationLoggerObserverTest observer_test;
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);
83 observer_test.ResetStates();
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);
93 log.UnregisterObserver(&observer_test);
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)
99 TEST(InvalidationLoggerTest, TestReleaseOfObserver) {
100 InvalidationLogger log;
101 InvalidationLoggerObserverTest observer_test;
103 log.RegisterObserver(&observer_test);
104 log.UnregisterObserver(&observer_test);
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);
120 // Test the EmitContet in InvalidationLogger is actually
121 // sending state and updateIds notifications.
122 TEST(InvalidationLoggerTest, TestEmitContent) {
123 InvalidationLogger log;
124 InvalidationLoggerObserverTest observer_test;
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);
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();
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);
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.
159 TEST(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);
166 syncer::ObjectIdSet sync_set_A;
167 syncer::ObjectIdCountMap counted_sync_set_A;
169 ObjectId o1(1000, "DataType1");
170 sync_set_A.insert(o1);
171 counted_sync_set_A[o1] = 0;
173 ObjectId o2(1000, "DataType2");
174 sync_set_A.insert(o2);
175 counted_sync_set_A[o2] = 0;
177 syncer::ObjectIdSet sync_set_B;
178 syncer::ObjectIdCountMap counted_sync_set_B;
180 ObjectId o3(1020, "DataTypeA");
181 sync_set_B.insert(o3);
182 counted_sync_set_B[o3] = 0;
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;
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);
193 syncer::ObjectIdSet sync_set_B2;
194 syncer::ObjectIdCountMap counted_sync_set_B2;
196 ObjectId o4(1020, "DataTypeF");
197 sync_set_B2.insert(o4);
198 counted_sync_set_B2[o4] = 0;
200 ObjectId o5(1020, "DataTypeG");
201 sync_set_B2.insert(o5);
202 counted_sync_set_B2[o5] = 0;
204 send_test_map["TestB"] = sync_set_B2;
205 expected_received_map["TestB"] = counted_sync_set_B2;
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);
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);
219 // Test that the invalidation notification changes the total count
220 // of invalidations received for that datatype.
221 TEST(InvalidationLoggerTest, TestInvalidtionsTotalCount) {
222 InvalidationLogger log;
223 InvalidationLoggerObserverTest observer_test;
224 log.RegisterObserver(&observer_test);
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;
231 ObjectId o1(1020, "DataTypeA");
232 sync_set.insert(o1);
233 counted_sync_set[o1] = 1;
235 // Generate invalidation for datatype A only.
236 syncer::ObjectIdInvalidationMap fake_invalidations =
237 syncer::ObjectIdInvalidationMap::InvalidateAll(sync_set);
239 ObjectId o2(1040, "DataTypeB");
240 sync_set.insert(o2);
241 counted_sync_set[o2] = 0;
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);
249 expected_received_map["Test"] = counted_sync_set;
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);
257 log.UnregisterObserver(&observer_test);
260 // Test that registered handlers are being sent to the observers.
261 TEST(InvalidationLoggerTest, TestRegisteredHandlers) {
262 InvalidationLogger log;
263 InvalidationLoggerObserverTest observer_test;
264 log.RegisterObserver(&observer_test);
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);
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);
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);
284 log.UnregisterObserver(&observer_test);
286 } // namespace invalidation