OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "sync/notifier/p2p_notifier.h" | 5 #include "sync/notifier/p2p_notifier.h" |
6 | 6 |
7 #include <cstddef> | 7 #include <cstddef> |
8 | 8 |
9 #include "jingle/notifier/listener/fake_push_client.h" | 9 #include "jingle/notifier/listener/fake_push_client.h" |
10 #include "sync/internal_api/public/base/model_type.h" | 10 #include "sync/internal_api/public/base/model_type.h" |
| 11 #include "sync/internal_api/public/base/model_type_payload_map.h" |
11 #include "sync/notifier/mock_sync_notifier_observer.h" | 12 #include "sync/notifier/mock_sync_notifier_observer.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
13 | 14 |
14 namespace syncer { | 15 namespace syncer { |
15 | 16 |
16 namespace { | 17 namespace { |
17 | 18 |
18 using ::testing::_; | 19 using ::testing::_; |
19 using ::testing::Mock; | 20 using ::testing::Mock; |
| 21 using ::testing::Return; |
20 using ::testing::StrictMock; | 22 using ::testing::StrictMock; |
21 | 23 |
22 class P2PNotifierTest : public testing::Test { | 24 class P2PNotifierTest : public testing::Test { |
23 protected: | 25 protected: |
24 P2PNotifierTest() | 26 P2PNotifierTest() |
25 : fake_push_client_(new notifier::FakePushClient()), | 27 : fake_push_client_(new notifier::FakePushClient()), |
26 p2p_notifier_( | 28 p2p_notifier_( |
27 scoped_ptr<notifier::PushClient>(fake_push_client_), | 29 scoped_ptr<notifier::PushClient>(fake_push_client_), |
28 NOTIFY_OTHERS), | 30 NOTIFY_OTHERS), |
29 next_sent_notification_to_reflect_(0) { | 31 next_sent_notification_to_reflect_(0) { |
30 p2p_notifier_.AddObserver(&mock_observer_); | 32 p2p_notifier_.AddHandler(&mock_observer_); |
31 } | 33 } |
32 | 34 |
33 virtual ~P2PNotifierTest() { | 35 virtual ~P2PNotifierTest() { |
34 p2p_notifier_.RemoveObserver(&mock_observer_); | 36 p2p_notifier_.RemoveHandler(&mock_observer_); |
35 } | 37 } |
36 | 38 |
37 syncer::ModelTypePayloadMap MakePayloadMap(syncer::ModelTypeSet types) { | 39 syncer::ModelTypePayloadMap MakePayloadMap(syncer::ModelTypeSet types) { |
38 return syncer::ModelTypePayloadMapFromEnumSet(types, ""); | 40 return syncer::ModelTypePayloadMapFromEnumSet(types, ""); |
39 } | 41 } |
40 | 42 |
41 // Simulate receiving all the notifications we sent out since last | 43 // Simulate receiving all the notifications we sent out since last |
42 // time this was called. | 44 // time this was called. |
43 void ReflectSentNotifications() { | 45 void ReflectSentNotifications() { |
44 const std::vector<notifier::Notification>& sent_notifications = | 46 const std::vector<notifier::Notification>& sent_notifications = |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 EXPECT_TRUE(notification_data.Equals(notification_data_parsed)); | 137 EXPECT_TRUE(notification_data.Equals(notification_data_parsed)); |
136 } | 138 } |
137 | 139 |
138 // Set up the P2PNotifier, simulate a successful connection, and send | 140 // Set up the P2PNotifier, simulate a successful connection, and send |
139 // a notification with the default target (NOTIFY_OTHERS). The | 141 // a notification with the default target (NOTIFY_OTHERS). The |
140 // observer should receive only a notification from the call to | 142 // observer should receive only a notification from the call to |
141 // UpdateEnabledTypes(). | 143 // UpdateEnabledTypes(). |
142 TEST_F(P2PNotifierTest, NotificationsBasic) { | 144 TEST_F(P2PNotifierTest, NotificationsBasic) { |
143 syncer::ModelTypeSet enabled_types(syncer::BOOKMARKS, syncer::PREFERENCES); | 145 syncer::ModelTypeSet enabled_types(syncer::BOOKMARKS, syncer::PREFERENCES); |
144 | 146 |
| 147 EXPECT_CALL(mock_observer_, GetHandledIds()) |
| 148 .WillOnce(Return(ModelTypeSetToObjectIdSet(enabled_types))); |
| 149 p2p_notifier_.ReloadHandlers(); |
| 150 |
145 EXPECT_CALL(mock_observer_, OnNotificationsEnabled()); | 151 EXPECT_CALL(mock_observer_, OnNotificationsEnabled()); |
146 EXPECT_CALL(mock_observer_, | 152 EXPECT_CALL(mock_observer_, OnIncomingNotification( |
147 OnIncomingNotification(MakePayloadMap(enabled_types), | 153 ModelTypePayloadMapToObjectIdPayloadMap(MakePayloadMap(enabled_types)), |
148 REMOTE_NOTIFICATION)); | 154 REMOTE_NOTIFICATION)); |
149 | 155 |
150 p2p_notifier_.SetUniqueId("sender"); | 156 p2p_notifier_.SetUniqueId("sender"); |
151 | 157 |
152 const char kEmail[] = "foo@bar.com"; | 158 const char kEmail[] = "foo@bar.com"; |
153 const char kToken[] = "token"; | 159 const char kToken[] = "token"; |
154 p2p_notifier_.UpdateCredentials(kEmail, kToken); | 160 p2p_notifier_.UpdateCredentials(kEmail, kToken); |
155 { | 161 { |
156 notifier::Subscription expected_subscription; | 162 notifier::Subscription expected_subscription; |
157 expected_subscription.channel = kSyncP2PNotificationChannel; | 163 expected_subscription.channel = kSyncP2PNotificationChannel; |
158 expected_subscription.from = kEmail; | 164 expected_subscription.from = kEmail; |
159 EXPECT_TRUE(notifier::SubscriptionListsEqual( | 165 EXPECT_TRUE(notifier::SubscriptionListsEqual( |
160 fake_push_client_->subscriptions(), | 166 fake_push_client_->subscriptions(), |
161 notifier::SubscriptionList(1, expected_subscription))); | 167 notifier::SubscriptionList(1, expected_subscription))); |
162 } | 168 } |
163 EXPECT_EQ(kEmail, fake_push_client_->email()); | 169 EXPECT_EQ(kEmail, fake_push_client_->email()); |
164 EXPECT_EQ(kToken, fake_push_client_->token()); | 170 EXPECT_EQ(kToken, fake_push_client_->token()); |
165 | 171 |
166 p2p_notifier_.UpdateEnabledTypes(enabled_types); | |
167 | |
168 ReflectSentNotifications(); | 172 ReflectSentNotifications(); |
169 fake_push_client_->EnableNotifications(); | 173 fake_push_client_->EnableNotifications(); |
170 | 174 |
171 // Sent with target NOTIFY_OTHERS so should not be propagated to | 175 // Sent with target NOTIFY_OTHERS so should not be propagated to |
172 // |mock_observer_|. | 176 // |mock_observer_|. |
173 { | 177 { |
174 syncer::ModelTypeSet changed_types(syncer::THEMES, syncer::APPS); | 178 syncer::ModelTypeSet changed_types(syncer::THEMES, syncer::APPS); |
175 p2p_notifier_.SendNotification(changed_types); | 179 p2p_notifier_.SendNotification(changed_types); |
176 } | 180 } |
177 | 181 |
178 ReflectSentNotifications(); | 182 ReflectSentNotifications(); |
179 } | 183 } |
180 | 184 |
181 // Set up the P2PNotifier and send out notifications with various | 185 // Set up the P2PNotifier and send out notifications with various |
182 // target settings. The notifications received by the observer should | 186 // target settings. The notifications received by the observer should |
183 // be consistent with the target settings. | 187 // be consistent with the target settings. |
184 TEST_F(P2PNotifierTest, SendNotificationData) { | 188 TEST_F(P2PNotifierTest, SendNotificationData) { |
185 syncer::ModelTypeSet enabled_types(syncer::BOOKMARKS, syncer::PREFERENCES); | 189 syncer::ModelTypeSet enabled_types(syncer::BOOKMARKS, syncer::PREFERENCES); |
186 | 190 |
187 syncer::ModelTypeSet changed_types(syncer::THEMES, syncer::APPS); | 191 EXPECT_CALL(mock_observer_, GetHandledIds()) |
| 192 .WillOnce(Return(ModelTypeSetToObjectIdSet(enabled_types))); |
| 193 p2p_notifier_.ReloadHandlers(); |
188 | 194 |
189 const syncer::ModelTypePayloadMap& changed_payload_map = | 195 syncer::ModelTypeSet changed_types(syncer::BOOKMARKS, syncer::APPS); |
190 MakePayloadMap(changed_types); | 196 syncer::ModelTypeSet expected_changed_types(syncer::BOOKMARKS); |
| 197 |
| 198 const syncer::ModelTypePayloadMap& expected_changed_payload_map = |
| 199 MakePayloadMap(expected_changed_types); |
191 | 200 |
192 EXPECT_CALL(mock_observer_, OnNotificationsEnabled()); | 201 EXPECT_CALL(mock_observer_, OnNotificationsEnabled()); |
193 EXPECT_CALL(mock_observer_, | 202 EXPECT_CALL(mock_observer_, |
194 OnIncomingNotification(MakePayloadMap(enabled_types), | 203 OnIncomingNotification( |
195 REMOTE_NOTIFICATION)); | 204 ModelTypePayloadMapToObjectIdPayloadMap( |
| 205 MakePayloadMap(enabled_types)), |
| 206 REMOTE_NOTIFICATION)); |
196 | 207 |
197 p2p_notifier_.SetUniqueId("sender"); | 208 p2p_notifier_.SetUniqueId("sender"); |
198 p2p_notifier_.UpdateCredentials("foo@bar.com", "fake_token"); | 209 p2p_notifier_.UpdateCredentials("foo@bar.com", "fake_token"); |
199 p2p_notifier_.UpdateEnabledTypes(enabled_types); | |
200 | 210 |
201 ReflectSentNotifications(); | 211 ReflectSentNotifications(); |
202 fake_push_client_->EnableNotifications(); | 212 fake_push_client_->EnableNotifications(); |
203 | 213 |
204 ReflectSentNotifications(); | 214 ReflectSentNotifications(); |
205 | 215 |
206 // Should be dropped. | 216 // Should be dropped. |
207 Mock::VerifyAndClearExpectations(&mock_observer_); | 217 Mock::VerifyAndClearExpectations(&mock_observer_); |
208 p2p_notifier_.SendNotificationDataForTest(P2PNotificationData()); | 218 p2p_notifier_.SendNotificationDataForTest(P2PNotificationData()); |
209 | 219 |
210 ReflectSentNotifications(); | 220 ReflectSentNotifications(); |
211 | 221 |
212 // Should be propagated. | 222 // Should be propagated. |
213 Mock::VerifyAndClearExpectations(&mock_observer_); | 223 Mock::VerifyAndClearExpectations(&mock_observer_); |
214 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map, | 224 EXPECT_CALL(mock_observer_, OnIncomingNotification( |
215 REMOTE_NOTIFICATION)); | 225 ModelTypePayloadMapToObjectIdPayloadMap(expected_changed_payload_map), |
| 226 REMOTE_NOTIFICATION)); |
216 p2p_notifier_.SendNotificationDataForTest( | 227 p2p_notifier_.SendNotificationDataForTest( |
217 P2PNotificationData("sender", NOTIFY_SELF, changed_types)); | 228 P2PNotificationData("sender", NOTIFY_SELF, changed_types)); |
218 | 229 |
219 ReflectSentNotifications(); | 230 ReflectSentNotifications(); |
220 | 231 |
221 // Should be dropped. | 232 // Should be dropped. |
222 Mock::VerifyAndClearExpectations(&mock_observer_); | 233 Mock::VerifyAndClearExpectations(&mock_observer_); |
223 p2p_notifier_.SendNotificationDataForTest( | 234 p2p_notifier_.SendNotificationDataForTest( |
224 P2PNotificationData("sender2", NOTIFY_SELF, changed_types)); | 235 P2PNotificationData("sender2", NOTIFY_SELF, changed_types)); |
225 | 236 |
226 ReflectSentNotifications(); | 237 ReflectSentNotifications(); |
227 | 238 |
228 // Should be dropped. | 239 // Should be dropped. |
229 Mock::VerifyAndClearExpectations(&mock_observer_); | 240 Mock::VerifyAndClearExpectations(&mock_observer_); |
230 p2p_notifier_.SendNotificationDataForTest( | 241 p2p_notifier_.SendNotificationDataForTest( |
231 P2PNotificationData("sender", NOTIFY_SELF, syncer::ModelTypeSet())); | 242 P2PNotificationData("sender", NOTIFY_SELF, syncer::ModelTypeSet())); |
232 | 243 |
233 ReflectSentNotifications(); | 244 ReflectSentNotifications(); |
234 | 245 |
235 // Should be dropped. | 246 // Should be dropped. |
236 p2p_notifier_.SendNotificationDataForTest( | 247 p2p_notifier_.SendNotificationDataForTest( |
237 P2PNotificationData("sender", NOTIFY_OTHERS, changed_types)); | 248 P2PNotificationData("sender", NOTIFY_OTHERS, changed_types)); |
238 | 249 |
239 ReflectSentNotifications(); | 250 ReflectSentNotifications(); |
240 | 251 |
241 // Should be propagated. | 252 // Should be propagated. |
242 Mock::VerifyAndClearExpectations(&mock_observer_); | 253 Mock::VerifyAndClearExpectations(&mock_observer_); |
243 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map, | 254 EXPECT_CALL(mock_observer_, OnIncomingNotification( |
244 REMOTE_NOTIFICATION)); | 255 ModelTypePayloadMapToObjectIdPayloadMap(expected_changed_payload_map), |
| 256 REMOTE_NOTIFICATION)); |
245 p2p_notifier_.SendNotificationDataForTest( | 257 p2p_notifier_.SendNotificationDataForTest( |
246 P2PNotificationData("sender2", NOTIFY_OTHERS, changed_types)); | 258 P2PNotificationData("sender2", NOTIFY_OTHERS, changed_types)); |
247 | 259 |
248 ReflectSentNotifications(); | 260 ReflectSentNotifications(); |
249 | 261 |
250 // Should be dropped. | 262 // Should be dropped. |
251 Mock::VerifyAndClearExpectations(&mock_observer_); | 263 Mock::VerifyAndClearExpectations(&mock_observer_); |
252 p2p_notifier_.SendNotificationDataForTest( | 264 p2p_notifier_.SendNotificationDataForTest( |
253 P2PNotificationData("sender2", NOTIFY_OTHERS, syncer::ModelTypeSet())); | 265 P2PNotificationData("sender2", NOTIFY_OTHERS, syncer::ModelTypeSet())); |
254 | 266 |
255 ReflectSentNotifications(); | 267 ReflectSentNotifications(); |
256 | 268 |
257 // Should be propagated. | 269 // Should be propagated. |
258 Mock::VerifyAndClearExpectations(&mock_observer_); | 270 Mock::VerifyAndClearExpectations(&mock_observer_); |
259 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map, | 271 EXPECT_CALL(mock_observer_, OnIncomingNotification( |
260 REMOTE_NOTIFICATION)); | 272 ModelTypePayloadMapToObjectIdPayloadMap(expected_changed_payload_map), |
| 273 REMOTE_NOTIFICATION)); |
261 p2p_notifier_.SendNotificationDataForTest( | 274 p2p_notifier_.SendNotificationDataForTest( |
262 P2PNotificationData("sender", NOTIFY_ALL, changed_types)); | 275 P2PNotificationData("sender", NOTIFY_ALL, changed_types)); |
263 | 276 |
264 ReflectSentNotifications(); | 277 ReflectSentNotifications(); |
265 | 278 |
266 // Should be propagated. | 279 // Should be propagated. |
267 Mock::VerifyAndClearExpectations(&mock_observer_); | 280 Mock::VerifyAndClearExpectations(&mock_observer_); |
268 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map, | 281 EXPECT_CALL(mock_observer_, OnIncomingNotification( |
269 REMOTE_NOTIFICATION)); | 282 ModelTypePayloadMapToObjectIdPayloadMap(expected_changed_payload_map), |
| 283 REMOTE_NOTIFICATION)); |
270 p2p_notifier_.SendNotificationDataForTest( | 284 p2p_notifier_.SendNotificationDataForTest( |
271 P2PNotificationData("sender2", NOTIFY_ALL, changed_types)); | 285 P2PNotificationData("sender2", NOTIFY_ALL, changed_types)); |
272 | 286 |
273 ReflectSentNotifications(); | 287 ReflectSentNotifications(); |
274 | 288 |
275 // Should be dropped. | 289 // Should be dropped. |
276 Mock::VerifyAndClearExpectations(&mock_observer_); | 290 Mock::VerifyAndClearExpectations(&mock_observer_); |
277 p2p_notifier_.SendNotificationDataForTest( | 291 p2p_notifier_.SendNotificationDataForTest( |
278 P2PNotificationData("sender2", NOTIFY_ALL, syncer::ModelTypeSet())); | 292 P2PNotificationData("sender2", NOTIFY_ALL, syncer::ModelTypeSet())); |
279 | 293 |
280 ReflectSentNotifications(); | 294 ReflectSentNotifications(); |
281 } | 295 } |
282 | 296 |
283 } // namespace | 297 } // namespace |
284 | 298 |
285 } // namespace syncer | 299 } // namespace syncer |
OLD | NEW |