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