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" |
(...skipping 10 matching lines...) Expand all Loading... |
21 using ::testing::StrictMock; | 21 using ::testing::StrictMock; |
22 | 22 |
23 class P2PNotifierTest : public testing::Test { | 23 class P2PNotifierTest : public testing::Test { |
24 protected: | 24 protected: |
25 P2PNotifierTest() | 25 P2PNotifierTest() |
26 : fake_push_client_(new notifier::FakePushClient()), | 26 : fake_push_client_(new notifier::FakePushClient()), |
27 p2p_notifier_( | 27 p2p_notifier_( |
28 scoped_ptr<notifier::PushClient>(fake_push_client_), | 28 scoped_ptr<notifier::PushClient>(fake_push_client_), |
29 NOTIFY_OTHERS), | 29 NOTIFY_OTHERS), |
30 next_sent_notification_to_reflect_(0) { | 30 next_sent_notification_to_reflect_(0) { |
31 p2p_notifier_.RegisterHandler(&mock_observer_); | |
32 } | 31 } |
33 | 32 |
34 virtual ~P2PNotifierTest() { | 33 virtual ~P2PNotifierTest() { |
35 p2p_notifier_.UnregisterHandler(&mock_observer_); | 34 p2p_notifier_.UpdateRegisteredIds(&mock_observer_, ObjectIdSet()); |
36 } | 35 } |
37 | 36 |
38 ModelTypePayloadMap MakePayloadMap(ModelTypeSet types) { | 37 ModelTypePayloadMap MakePayloadMap(ModelTypeSet types) { |
39 return ModelTypePayloadMapFromEnumSet(types, std::string()); | 38 return ModelTypePayloadMapFromEnumSet(types, std::string()); |
40 } | 39 } |
41 | 40 |
42 // Simulate receiving all the notifications we sent out since last | 41 // Simulate receiving all the notifications we sent out since last |
43 // time this was called. | 42 // time this was called. |
44 void ReflectSentNotifications() { | 43 void ReflectSentNotifications() { |
45 const std::vector<notifier::Notification>& sent_notifications = | 44 const std::vector<notifier::Notification>& sent_notifications = |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 P2PNotificationData notification_data_parsed; | 133 P2PNotificationData notification_data_parsed; |
135 EXPECT_TRUE(notification_data_parsed.ResetFromString(notification_data_str)); | 134 EXPECT_TRUE(notification_data_parsed.ResetFromString(notification_data_str)); |
136 EXPECT_TRUE(notification_data.Equals(notification_data_parsed)); | 135 EXPECT_TRUE(notification_data.Equals(notification_data_parsed)); |
137 } | 136 } |
138 | 137 |
139 // Set up the P2PNotifier, simulate a successful connection, and send | 138 // Set up the P2PNotifier, simulate a successful connection, and send |
140 // a notification with the default target (NOTIFY_OTHERS). The | 139 // a notification with the default target (NOTIFY_OTHERS). The |
141 // observer should receive only a notification from the call to | 140 // observer should receive only a notification from the call to |
142 // UpdateEnabledTypes(). | 141 // UpdateEnabledTypes(). |
143 TEST_F(P2PNotifierTest, NotificationsBasic) { | 142 TEST_F(P2PNotifierTest, NotificationsBasic) { |
144 const ModelTypeSet enabled_types(BOOKMARKS, PREFERENCES); | 143 ModelTypeSet enabled_types(BOOKMARKS, PREFERENCES); |
| 144 |
| 145 p2p_notifier_.UpdateRegisteredIds(&mock_observer_, |
| 146 ModelTypeSetToObjectIdSet(enabled_types)); |
145 | 147 |
146 EXPECT_CALL(mock_observer_, OnNotificationsEnabled()); | 148 EXPECT_CALL(mock_observer_, OnNotificationsEnabled()); |
147 EXPECT_CALL(mock_observer_, OnIncomingNotification( | 149 EXPECT_CALL(mock_observer_, OnIncomingNotification( |
148 ModelTypePayloadMapToObjectIdPayloadMap(MakePayloadMap(enabled_types)), | 150 ModelTypePayloadMapToObjectIdPayloadMap(MakePayloadMap(enabled_types)), |
149 REMOTE_NOTIFICATION)); | 151 REMOTE_NOTIFICATION)); |
150 | 152 |
151 p2p_notifier_.UpdateRegisteredIds(&mock_observer_, | |
152 ModelTypeSetToObjectIdSet(enabled_types)); | |
153 | |
154 p2p_notifier_.SetUniqueId("sender"); | 153 p2p_notifier_.SetUniqueId("sender"); |
155 | 154 |
156 const char kEmail[] = "foo@bar.com"; | 155 const char kEmail[] = "foo@bar.com"; |
157 const char kToken[] = "token"; | 156 const char kToken[] = "token"; |
158 p2p_notifier_.UpdateCredentials(kEmail, kToken); | 157 p2p_notifier_.UpdateCredentials(kEmail, kToken); |
159 { | 158 { |
160 notifier::Subscription expected_subscription; | 159 notifier::Subscription expected_subscription; |
161 expected_subscription.channel = kSyncP2PNotificationChannel; | 160 expected_subscription.channel = kSyncP2PNotificationChannel; |
162 expected_subscription.from = kEmail; | 161 expected_subscription.from = kEmail; |
163 EXPECT_TRUE(notifier::SubscriptionListsEqual( | 162 EXPECT_TRUE(notifier::SubscriptionListsEqual( |
(...skipping 13 matching lines...) Expand all Loading... |
177 p2p_notifier_.SendNotification(changed_types); | 176 p2p_notifier_.SendNotification(changed_types); |
178 } | 177 } |
179 | 178 |
180 ReflectSentNotifications(); | 179 ReflectSentNotifications(); |
181 } | 180 } |
182 | 181 |
183 // Set up the P2PNotifier and send out notifications with various | 182 // Set up the P2PNotifier and send out notifications with various |
184 // target settings. The notifications received by the observer should | 183 // target settings. The notifications received by the observer should |
185 // be consistent with the target settings. | 184 // be consistent with the target settings. |
186 TEST_F(P2PNotifierTest, SendNotificationData) { | 185 TEST_F(P2PNotifierTest, SendNotificationData) { |
187 const ModelTypeSet enabled_types(BOOKMARKS, PREFERENCES, THEMES); | 186 ModelTypeSet enabled_types(BOOKMARKS, PREFERENCES, THEMES); |
188 const ModelTypeSet changed_types(THEMES, APPS); | 187 ModelTypeSet changed_types(THEMES, APPS); |
189 const ModelTypeSet expected_types(THEMES); | 188 ModelTypeSet expected_types(THEMES); |
| 189 |
| 190 p2p_notifier_.UpdateRegisteredIds(&mock_observer_, |
| 191 ModelTypeSetToObjectIdSet(enabled_types)); |
| 192 |
| 193 const ModelTypePayloadMap& expected_payload_map = |
| 194 MakePayloadMap(expected_types); |
190 | 195 |
191 EXPECT_CALL(mock_observer_, OnNotificationsEnabled()); | 196 EXPECT_CALL(mock_observer_, OnNotificationsEnabled()); |
192 EXPECT_CALL(mock_observer_, | 197 EXPECT_CALL(mock_observer_, |
193 OnIncomingNotification( | 198 OnIncomingNotification( |
194 ModelTypePayloadMapToObjectIdPayloadMap( | 199 ModelTypePayloadMapToObjectIdPayloadMap( |
195 MakePayloadMap(enabled_types)), | 200 MakePayloadMap(enabled_types)), |
196 REMOTE_NOTIFICATION)); | 201 REMOTE_NOTIFICATION)); |
197 | 202 |
198 p2p_notifier_.UpdateRegisteredIds(&mock_observer_, | |
199 ModelTypeSetToObjectIdSet(enabled_types)); | |
200 | |
201 const ModelTypePayloadMap& expected_payload_map = | |
202 MakePayloadMap(expected_types); | |
203 | |
204 p2p_notifier_.SetUniqueId("sender"); | 203 p2p_notifier_.SetUniqueId("sender"); |
205 p2p_notifier_.UpdateCredentials("foo@bar.com", "fake_token"); | 204 p2p_notifier_.UpdateCredentials("foo@bar.com", "fake_token"); |
206 | 205 |
207 ReflectSentNotifications(); | 206 ReflectSentNotifications(); |
208 fake_push_client_->EnableNotifications(); | 207 fake_push_client_->EnableNotifications(); |
209 | 208 |
210 ReflectSentNotifications(); | 209 ReflectSentNotifications(); |
211 | 210 |
212 // Should be dropped. | 211 // Should be dropped. |
213 Mock::VerifyAndClearExpectations(&mock_observer_); | 212 Mock::VerifyAndClearExpectations(&mock_observer_); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 Mock::VerifyAndClearExpectations(&mock_observer_); | 285 Mock::VerifyAndClearExpectations(&mock_observer_); |
287 p2p_notifier_.SendNotificationDataForTest( | 286 p2p_notifier_.SendNotificationDataForTest( |
288 P2PNotificationData("sender2", NOTIFY_ALL, ModelTypeSet())); | 287 P2PNotificationData("sender2", NOTIFY_ALL, ModelTypeSet())); |
289 | 288 |
290 ReflectSentNotifications(); | 289 ReflectSentNotifications(); |
291 } | 290 } |
292 | 291 |
293 } // namespace | 292 } // namespace |
294 | 293 |
295 } // namespace syncer | 294 } // namespace syncer |
OLD | NEW |