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" | |
12 #include "sync/notifier/mock_sync_notifier_observer.h" | 11 #include "sync/notifier/mock_sync_notifier_observer.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
14 | 13 |
15 namespace syncer { | 14 namespace syncer { |
16 | 15 |
17 namespace { | 16 namespace { |
18 | 17 |
19 using ::testing::_; | 18 using ::testing::_; |
20 using ::testing::Mock; | 19 using ::testing::Mock; |
21 using ::testing::StrictMock; | 20 using ::testing::StrictMock; |
22 | 21 |
23 class P2PNotifierTest : public testing::Test { | 22 class P2PNotifierTest : public testing::Test { |
24 protected: | 23 protected: |
25 P2PNotifierTest() | 24 P2PNotifierTest() |
26 : fake_push_client_(new notifier::FakePushClient()), | 25 : fake_push_client_(new notifier::FakePushClient()), |
27 p2p_notifier_( | 26 p2p_notifier_( |
28 scoped_ptr<notifier::PushClient>(fake_push_client_), | 27 scoped_ptr<notifier::PushClient>(fake_push_client_), |
29 NOTIFY_OTHERS), | 28 NOTIFY_OTHERS), |
30 next_sent_notification_to_reflect_(0) { | 29 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_.UpdateRegisteredIds(&mock_observer_, ObjectIdSet()); | 34 p2p_notifier_.RemoveObserver(&mock_observer_); |
35 } | 35 } |
36 | 36 |
37 ModelTypePayloadMap MakePayloadMap(ModelTypeSet types) { | 37 ModelTypePayloadMap MakePayloadMap(ModelTypeSet types) { |
38 return ModelTypePayloadMapFromEnumSet(types, std::string()); | 38 return ModelTypePayloadMapFromEnumSet(types, ""); |
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 | |
148 EXPECT_CALL(mock_observer_, OnNotificationsEnabled()); | 145 EXPECT_CALL(mock_observer_, OnNotificationsEnabled()); |
149 EXPECT_CALL(mock_observer_, OnIncomingNotification( | 146 EXPECT_CALL(mock_observer_, |
150 ModelTypePayloadMapToObjectIdPayloadMap(MakePayloadMap(enabled_types)), | 147 OnIncomingNotification(MakePayloadMap(enabled_types), |
151 REMOTE_NOTIFICATION)); | 148 REMOTE_NOTIFICATION)); |
152 | 149 |
153 p2p_notifier_.SetUniqueId("sender"); | 150 p2p_notifier_.SetUniqueId("sender"); |
154 | 151 |
155 const char kEmail[] = "foo@bar.com"; | 152 const char kEmail[] = "foo@bar.com"; |
156 const char kToken[] = "token"; | 153 const char kToken[] = "token"; |
157 p2p_notifier_.UpdateCredentials(kEmail, kToken); | 154 p2p_notifier_.UpdateCredentials(kEmail, kToken); |
158 { | 155 { |
159 notifier::Subscription expected_subscription; | 156 notifier::Subscription expected_subscription; |
160 expected_subscription.channel = kSyncP2PNotificationChannel; | 157 expected_subscription.channel = kSyncP2PNotificationChannel; |
161 expected_subscription.from = kEmail; | 158 expected_subscription.from = kEmail; |
162 EXPECT_TRUE(notifier::SubscriptionListsEqual( | 159 EXPECT_TRUE(notifier::SubscriptionListsEqual( |
163 fake_push_client_->subscriptions(), | 160 fake_push_client_->subscriptions(), |
164 notifier::SubscriptionList(1, expected_subscription))); | 161 notifier::SubscriptionList(1, expected_subscription))); |
165 } | 162 } |
166 EXPECT_EQ(kEmail, fake_push_client_->email()); | 163 EXPECT_EQ(kEmail, fake_push_client_->email()); |
167 EXPECT_EQ(kToken, fake_push_client_->token()); | 164 EXPECT_EQ(kToken, fake_push_client_->token()); |
168 | 165 |
| 166 p2p_notifier_.UpdateEnabledTypes(enabled_types); |
| 167 |
169 ReflectSentNotifications(); | 168 ReflectSentNotifications(); |
170 fake_push_client_->EnableNotifications(); | 169 fake_push_client_->EnableNotifications(); |
171 | 170 |
172 // Sent with target NOTIFY_OTHERS so should not be propagated to | 171 // Sent with target NOTIFY_OTHERS so should not be propagated to |
173 // |mock_observer_|. | 172 // |mock_observer_|. |
174 { | 173 { |
175 ModelTypeSet changed_types(THEMES, APPS); | 174 ModelTypeSet changed_types(THEMES, APPS); |
176 p2p_notifier_.SendNotification(changed_types); | 175 p2p_notifier_.SendNotification(changed_types); |
177 } | 176 } |
178 | 177 |
179 ReflectSentNotifications(); | 178 ReflectSentNotifications(); |
180 } | 179 } |
181 | 180 |
182 // Set up the P2PNotifier and send out notifications with various | 181 // Set up the P2PNotifier and send out notifications with various |
183 // target settings. The notifications received by the observer should | 182 // target settings. The notifications received by the observer should |
184 // be consistent with the target settings. | 183 // be consistent with the target settings. |
185 TEST_F(P2PNotifierTest, SendNotificationData) { | 184 TEST_F(P2PNotifierTest, SendNotificationData) { |
186 ModelTypeSet enabled_types(BOOKMARKS, PREFERENCES, THEMES); | 185 ModelTypeSet enabled_types(BOOKMARKS, PREFERENCES, THEMES); |
187 ModelTypeSet changed_types(THEMES, APPS); | 186 ModelTypeSet changed_types(THEMES, APPS); |
188 ModelTypeSet expected_types(THEMES); | 187 ModelTypeSet expected_types(THEMES); |
189 | 188 |
190 p2p_notifier_.UpdateRegisteredIds(&mock_observer_, | |
191 ModelTypeSetToObjectIdSet(enabled_types)); | |
192 | |
193 const ModelTypePayloadMap& expected_payload_map = | 189 const ModelTypePayloadMap& expected_payload_map = |
194 MakePayloadMap(expected_types); | 190 MakePayloadMap(expected_types); |
195 | 191 |
196 EXPECT_CALL(mock_observer_, OnNotificationsEnabled()); | 192 EXPECT_CALL(mock_observer_, OnNotificationsEnabled()); |
197 EXPECT_CALL(mock_observer_, | 193 EXPECT_CALL(mock_observer_, |
198 OnIncomingNotification( | 194 OnIncomingNotification(MakePayloadMap(enabled_types), |
199 ModelTypePayloadMapToObjectIdPayloadMap( | 195 REMOTE_NOTIFICATION)); |
200 MakePayloadMap(enabled_types)), | |
201 REMOTE_NOTIFICATION)); | |
202 | 196 |
203 p2p_notifier_.SetUniqueId("sender"); | 197 p2p_notifier_.SetUniqueId("sender"); |
204 p2p_notifier_.UpdateCredentials("foo@bar.com", "fake_token"); | 198 p2p_notifier_.UpdateCredentials("foo@bar.com", "fake_token"); |
| 199 p2p_notifier_.UpdateEnabledTypes(enabled_types); |
205 | 200 |
206 ReflectSentNotifications(); | 201 ReflectSentNotifications(); |
207 fake_push_client_->EnableNotifications(); | 202 fake_push_client_->EnableNotifications(); |
208 | 203 |
209 ReflectSentNotifications(); | 204 ReflectSentNotifications(); |
210 | 205 |
211 // Should be dropped. | 206 // Should be dropped. |
212 Mock::VerifyAndClearExpectations(&mock_observer_); | 207 Mock::VerifyAndClearExpectations(&mock_observer_); |
213 p2p_notifier_.SendNotificationDataForTest(P2PNotificationData()); | 208 p2p_notifier_.SendNotificationDataForTest(P2PNotificationData()); |
214 | 209 |
215 ReflectSentNotifications(); | 210 ReflectSentNotifications(); |
216 | 211 |
217 // Should be propagated. | 212 // Should be propagated. |
218 Mock::VerifyAndClearExpectations(&mock_observer_); | 213 Mock::VerifyAndClearExpectations(&mock_observer_); |
219 EXPECT_CALL(mock_observer_, OnIncomingNotification( | 214 EXPECT_CALL(mock_observer_, OnIncomingNotification(expected_payload_map, |
220 ModelTypePayloadMapToObjectIdPayloadMap(expected_payload_map), | 215 REMOTE_NOTIFICATION)); |
221 REMOTE_NOTIFICATION)); | |
222 p2p_notifier_.SendNotificationDataForTest( | 216 p2p_notifier_.SendNotificationDataForTest( |
223 P2PNotificationData("sender", NOTIFY_SELF, changed_types)); | 217 P2PNotificationData("sender", NOTIFY_SELF, changed_types)); |
224 | 218 |
225 ReflectSentNotifications(); | 219 ReflectSentNotifications(); |
226 | 220 |
227 // Should be dropped. | 221 // Should be dropped. |
228 Mock::VerifyAndClearExpectations(&mock_observer_); | 222 Mock::VerifyAndClearExpectations(&mock_observer_); |
229 p2p_notifier_.SendNotificationDataForTest( | 223 p2p_notifier_.SendNotificationDataForTest( |
230 P2PNotificationData("sender2", NOTIFY_SELF, changed_types)); | 224 P2PNotificationData("sender2", NOTIFY_SELF, changed_types)); |
231 | 225 |
232 ReflectSentNotifications(); | 226 ReflectSentNotifications(); |
233 | 227 |
234 // Should be dropped. | 228 // Should be dropped. |
235 Mock::VerifyAndClearExpectations(&mock_observer_); | 229 Mock::VerifyAndClearExpectations(&mock_observer_); |
236 p2p_notifier_.SendNotificationDataForTest( | 230 p2p_notifier_.SendNotificationDataForTest( |
237 P2PNotificationData("sender", NOTIFY_SELF, ModelTypeSet())); | 231 P2PNotificationData("sender", NOTIFY_SELF, ModelTypeSet())); |
238 | 232 |
239 ReflectSentNotifications(); | 233 ReflectSentNotifications(); |
240 | 234 |
241 // Should be dropped. | 235 // Should be dropped. |
242 p2p_notifier_.SendNotificationDataForTest( | 236 p2p_notifier_.SendNotificationDataForTest( |
243 P2PNotificationData("sender", NOTIFY_OTHERS, changed_types)); | 237 P2PNotificationData("sender", NOTIFY_OTHERS, changed_types)); |
244 | 238 |
245 ReflectSentNotifications(); | 239 ReflectSentNotifications(); |
246 | 240 |
247 // Should be propagated. | 241 // Should be propagated. |
248 Mock::VerifyAndClearExpectations(&mock_observer_); | 242 Mock::VerifyAndClearExpectations(&mock_observer_); |
249 EXPECT_CALL(mock_observer_, OnIncomingNotification( | 243 EXPECT_CALL(mock_observer_, OnIncomingNotification(expected_payload_map, |
250 ModelTypePayloadMapToObjectIdPayloadMap(expected_payload_map), | 244 REMOTE_NOTIFICATION)); |
251 REMOTE_NOTIFICATION)); | |
252 p2p_notifier_.SendNotificationDataForTest( | 245 p2p_notifier_.SendNotificationDataForTest( |
253 P2PNotificationData("sender2", NOTIFY_OTHERS, changed_types)); | 246 P2PNotificationData("sender2", NOTIFY_OTHERS, changed_types)); |
254 | 247 |
255 ReflectSentNotifications(); | 248 ReflectSentNotifications(); |
256 | 249 |
257 // Should be dropped. | 250 // Should be dropped. |
258 Mock::VerifyAndClearExpectations(&mock_observer_); | 251 Mock::VerifyAndClearExpectations(&mock_observer_); |
259 p2p_notifier_.SendNotificationDataForTest( | 252 p2p_notifier_.SendNotificationDataForTest( |
260 P2PNotificationData("sender2", NOTIFY_OTHERS, ModelTypeSet())); | 253 P2PNotificationData("sender2", NOTIFY_OTHERS, ModelTypeSet())); |
261 | 254 |
262 ReflectSentNotifications(); | 255 ReflectSentNotifications(); |
263 | 256 |
264 // Should be propagated. | 257 // Should be propagated. |
265 Mock::VerifyAndClearExpectations(&mock_observer_); | 258 Mock::VerifyAndClearExpectations(&mock_observer_); |
266 EXPECT_CALL(mock_observer_, OnIncomingNotification( | 259 EXPECT_CALL(mock_observer_, OnIncomingNotification(expected_payload_map, |
267 ModelTypePayloadMapToObjectIdPayloadMap(expected_payload_map), | 260 REMOTE_NOTIFICATION)); |
268 REMOTE_NOTIFICATION)); | |
269 p2p_notifier_.SendNotificationDataForTest( | 261 p2p_notifier_.SendNotificationDataForTest( |
270 P2PNotificationData("sender", NOTIFY_ALL, changed_types)); | 262 P2PNotificationData("sender", NOTIFY_ALL, changed_types)); |
271 | 263 |
272 ReflectSentNotifications(); | 264 ReflectSentNotifications(); |
273 | 265 |
274 // Should be propagated. | 266 // Should be propagated. |
275 Mock::VerifyAndClearExpectations(&mock_observer_); | 267 Mock::VerifyAndClearExpectations(&mock_observer_); |
276 EXPECT_CALL(mock_observer_, OnIncomingNotification( | 268 EXPECT_CALL(mock_observer_, OnIncomingNotification(expected_payload_map, |
277 ModelTypePayloadMapToObjectIdPayloadMap(expected_payload_map), | 269 REMOTE_NOTIFICATION)); |
278 REMOTE_NOTIFICATION)); | |
279 p2p_notifier_.SendNotificationDataForTest( | 270 p2p_notifier_.SendNotificationDataForTest( |
280 P2PNotificationData("sender2", NOTIFY_ALL, changed_types)); | 271 P2PNotificationData("sender2", NOTIFY_ALL, changed_types)); |
281 | 272 |
282 ReflectSentNotifications(); | 273 ReflectSentNotifications(); |
283 | 274 |
284 // Should be dropped. | 275 // Should be dropped. |
285 Mock::VerifyAndClearExpectations(&mock_observer_); | 276 Mock::VerifyAndClearExpectations(&mock_observer_); |
286 p2p_notifier_.SendNotificationDataForTest( | 277 p2p_notifier_.SendNotificationDataForTest( |
287 P2PNotificationData("sender2", NOTIFY_ALL, ModelTypeSet())); | 278 P2PNotificationData("sender2", NOTIFY_ALL, ModelTypeSet())); |
288 | 279 |
289 ReflectSentNotifications(); | 280 ReflectSentNotifications(); |
290 } | 281 } |
291 | 282 |
292 } // namespace | 283 } // namespace |
293 | 284 |
294 } // namespace syncer | 285 } // namespace syncer |
OLD | NEW |