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