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 "chrome/browser/invalidation/invalidation_logger.h"
6 #include "chrome/browser/invalidation/invalidation_logger_observer.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 namespace invalidation
{
12 class InvalidationLoggerObserverTest
: public InvalidationLoggerObserver
{
14 InvalidationLoggerObserverTest() { 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
)
29 registered_handlers
= handlers
;
30 registration_change_received
= true;
33 virtual void OnStateChange(const syncer::InvalidatorState
& newState
)
35 state_received
= true;
38 virtual void OnUpdateIds(const std::string
& handler
,
39 const syncer::ObjectIdCountMap
& details
) OVERRIDE
{
40 update_id_received
= true;
41 update_id_replicated
[handler
] = details
;
44 virtual void OnDebugMessage(const base::DictionaryValue
& details
) OVERRIDE
{
45 debug_message_received
= true;
48 virtual void OnInvalidation(
49 const syncer::ObjectIdInvalidationMap
& newInvalidations
) OVERRIDE
{
50 invalidation_received
= true;
53 virtual void OnDetailedStatus(const base::DictionaryValue
& details
) OVERRIDE
{
54 detailed_status_received
= true;
57 bool registration_change_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
;
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
;
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
);
82 observer_test
.ResetStates();
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
);
92 log
.UnregisterObserver(&observer_test
);
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
;
102 log
.RegisterObserver(&observer_test
);
103 log
.UnregisterObserver(&observer_test
);
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
);
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
;
125 log
.RegisterObserver(&observer_test
);
126 EXPECT_FALSE(observer_test
.state_received
);
127 EXPECT_FALSE(observer_test
.update_id_received
);
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
);
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();
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
);
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
);
165 syncer::ObjectIdSet sync_set_A
;
166 syncer::ObjectIdCountMap counted_sync_set_A
;
168 ObjectId
o1(1000, "DataType1");
169 sync_set_A
.insert(o1
);
170 counted_sync_set_A
[o1
] = 0;
172 ObjectId
o2(1000, "DataType2");
173 sync_set_A
.insert(o2
);
174 counted_sync_set_A
[o2
] = 0;
176 syncer::ObjectIdSet sync_set_B
;
177 syncer::ObjectIdCountMap counted_sync_set_B
;
179 ObjectId
o3(1020, "DataTypeA");
180 sync_set_B
.insert(o3
);
181 counted_sync_set_B
[o3
] = 0;
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
;
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
);
192 syncer::ObjectIdSet sync_set_B2
;
193 syncer::ObjectIdCountMap counted_sync_set_B2
;
195 ObjectId
o4(1020, "DataTypeF");
196 sync_set_B2
.insert(o4
);
197 counted_sync_set_B2
[o4
] = 0;
199 ObjectId
o5(1020, "DataTypeG");
200 sync_set_B2
.insert(o5
);
201 counted_sync_set_B2
[o5
] = 0;
203 send_test_map
["TestB"] = sync_set_B2
;
204 expected_received_map
["TestB"] = counted_sync_set_B2
;
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
);
211 // The emit content should return the same map too.
212 observer_test
.ResetStates();
214 EXPECT_EQ(expected_received_map
, observer_test
.update_id_replicated
);
215 log
.UnregisterObserver(&observer_test
);
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
);
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
;
230 ObjectId
o1(1020, "DataTypeA");
232 counted_sync_set
[o1
] = 1;
234 // Generate invalidation for datatype A only.
235 syncer::ObjectIdInvalidationMap fake_invalidations
=
236 syncer::ObjectIdInvalidationMap::InvalidateAll(sync_set
);
238 ObjectId
o2(1040, "DataTypeB");
240 counted_sync_set
[o2
] = 0;
242 // Registed the two objectIds and send an invalidation only for the
244 send_test_map
["Test"] = sync_set
;
245 log
.OnUpdateIds(send_test_map
);
246 log
.OnInvalidation(fake_invalidations
);
248 expected_received_map
["Test"] = counted_sync_set
;
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();
254 EXPECT_EQ(expected_received_map
, observer_test
.update_id_replicated
);
256 log
.UnregisterObserver(&observer_test
);
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
);
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
);
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
);
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
);
283 log
.UnregisterObserver(&observer_test
);
285 } // namespace invalidation