Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(819)

Side by Side Diff: sync/notifier/p2p_notifier_unittest.cc

Issue 10702074: Refactor sync-specific parts out of SyncNotifier/SyncNotifierObserver (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: FOR_THE_HORDE Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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); 186 ModelTypeSet enabled_types(BOOKMARKS, PREFERENCES);
186 187
187 ModelTypeSet changed_types(THEMES, APPS); 188 p2p_notifier_.UpdateRegisteredIds(&mock_observer_,
189 ModelTypeSetToObjectIdSet(enabled_types));
188 190
189 const ModelTypePayloadMap& changed_payload_map = 191 ModelTypeSet changed_types(BOOKMARKS, APPS);
190 MakePayloadMap(changed_types); 192 ModelTypeSet expected_changed_types(BOOKMARKS);
193
194 const ModelTypePayloadMap& expected_changed_payload_map =
195 MakePayloadMap(expected_changed_types);
191 196
192 EXPECT_CALL(mock_observer_, OnNotificationsEnabled()); 197 EXPECT_CALL(mock_observer_, OnNotificationsEnabled());
193 EXPECT_CALL(mock_observer_, 198 EXPECT_CALL(mock_observer_,
194 OnIncomingNotification(MakePayloadMap(enabled_types), 199 OnIncomingNotification(
195 REMOTE_NOTIFICATION)); 200 ModelTypePayloadMapToObjectIdPayloadMap(
201 MakePayloadMap(enabled_types)),
202 REMOTE_NOTIFICATION));
196 203
197 p2p_notifier_.SetUniqueId("sender"); 204 p2p_notifier_.SetUniqueId("sender");
198 p2p_notifier_.UpdateCredentials("foo@bar.com", "fake_token"); 205 p2p_notifier_.UpdateCredentials("foo@bar.com", "fake_token");
199 p2p_notifier_.UpdateEnabledTypes(enabled_types);
200 206
201 ReflectSentNotifications(); 207 ReflectSentNotifications();
202 fake_push_client_->EnableNotifications(); 208 fake_push_client_->EnableNotifications();
203 209
204 ReflectSentNotifications(); 210 ReflectSentNotifications();
205 211
206 // Should be dropped. 212 // Should be dropped.
207 Mock::VerifyAndClearExpectations(&mock_observer_); 213 Mock::VerifyAndClearExpectations(&mock_observer_);
208 p2p_notifier_.SendNotificationDataForTest(P2PNotificationData()); 214 p2p_notifier_.SendNotificationDataForTest(P2PNotificationData());
209 215
210 ReflectSentNotifications(); 216 ReflectSentNotifications();
211 217
212 // Should be propagated. 218 // Should be propagated.
213 Mock::VerifyAndClearExpectations(&mock_observer_); 219 Mock::VerifyAndClearExpectations(&mock_observer_);
214 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map, 220 EXPECT_CALL(mock_observer_, OnIncomingNotification(
215 REMOTE_NOTIFICATION)); 221 ModelTypePayloadMapToObjectIdPayloadMap(expected_changed_payload_map),
222 REMOTE_NOTIFICATION));
216 p2p_notifier_.SendNotificationDataForTest( 223 p2p_notifier_.SendNotificationDataForTest(
217 P2PNotificationData("sender", NOTIFY_SELF, changed_types)); 224 P2PNotificationData("sender", NOTIFY_SELF, changed_types));
218 225
219 ReflectSentNotifications(); 226 ReflectSentNotifications();
220 227
221 // Should be dropped. 228 // Should be dropped.
222 Mock::VerifyAndClearExpectations(&mock_observer_); 229 Mock::VerifyAndClearExpectations(&mock_observer_);
223 p2p_notifier_.SendNotificationDataForTest( 230 p2p_notifier_.SendNotificationDataForTest(
224 P2PNotificationData("sender2", NOTIFY_SELF, changed_types)); 231 P2PNotificationData("sender2", NOTIFY_SELF, changed_types));
225 232
226 ReflectSentNotifications(); 233 ReflectSentNotifications();
227 234
228 // Should be dropped. 235 // Should be dropped.
229 Mock::VerifyAndClearExpectations(&mock_observer_); 236 Mock::VerifyAndClearExpectations(&mock_observer_);
230 p2p_notifier_.SendNotificationDataForTest( 237 p2p_notifier_.SendNotificationDataForTest(
231 P2PNotificationData("sender", NOTIFY_SELF, ModelTypeSet())); 238 P2PNotificationData("sender", NOTIFY_SELF, ModelTypeSet()));
232 239
233 ReflectSentNotifications(); 240 ReflectSentNotifications();
234 241
235 // Should be dropped. 242 // Should be dropped.
236 p2p_notifier_.SendNotificationDataForTest( 243 p2p_notifier_.SendNotificationDataForTest(
237 P2PNotificationData("sender", NOTIFY_OTHERS, changed_types)); 244 P2PNotificationData("sender", NOTIFY_OTHERS, changed_types));
238 245
239 ReflectSentNotifications(); 246 ReflectSentNotifications();
240 247
241 // Should be propagated. 248 // Should be propagated.
242 Mock::VerifyAndClearExpectations(&mock_observer_); 249 Mock::VerifyAndClearExpectations(&mock_observer_);
243 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map, 250 EXPECT_CALL(mock_observer_, OnIncomingNotification(
244 REMOTE_NOTIFICATION)); 251 ModelTypePayloadMapToObjectIdPayloadMap(expected_changed_payload_map),
252 REMOTE_NOTIFICATION));
245 p2p_notifier_.SendNotificationDataForTest( 253 p2p_notifier_.SendNotificationDataForTest(
246 P2PNotificationData("sender2", NOTIFY_OTHERS, changed_types)); 254 P2PNotificationData("sender2", NOTIFY_OTHERS, changed_types));
247 255
248 ReflectSentNotifications(); 256 ReflectSentNotifications();
249 257
250 // Should be dropped. 258 // Should be dropped.
251 Mock::VerifyAndClearExpectations(&mock_observer_); 259 Mock::VerifyAndClearExpectations(&mock_observer_);
252 p2p_notifier_.SendNotificationDataForTest( 260 p2p_notifier_.SendNotificationDataForTest(
253 P2PNotificationData("sender2", NOTIFY_OTHERS, ModelTypeSet())); 261 P2PNotificationData("sender2", NOTIFY_OTHERS, ModelTypeSet()));
254 262
255 ReflectSentNotifications(); 263 ReflectSentNotifications();
256 264
257 // Should be propagated. 265 // Should be propagated.
258 Mock::VerifyAndClearExpectations(&mock_observer_); 266 Mock::VerifyAndClearExpectations(&mock_observer_);
259 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map, 267 EXPECT_CALL(mock_observer_, OnIncomingNotification(
260 REMOTE_NOTIFICATION)); 268 ModelTypePayloadMapToObjectIdPayloadMap(expected_changed_payload_map),
269 REMOTE_NOTIFICATION));
261 p2p_notifier_.SendNotificationDataForTest( 270 p2p_notifier_.SendNotificationDataForTest(
262 P2PNotificationData("sender", NOTIFY_ALL, changed_types)); 271 P2PNotificationData("sender", NOTIFY_ALL, changed_types));
263 272
264 ReflectSentNotifications(); 273 ReflectSentNotifications();
265 274
266 // Should be propagated. 275 // Should be propagated.
267 Mock::VerifyAndClearExpectations(&mock_observer_); 276 Mock::VerifyAndClearExpectations(&mock_observer_);
268 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map, 277 EXPECT_CALL(mock_observer_, OnIncomingNotification(
269 REMOTE_NOTIFICATION)); 278 ModelTypePayloadMapToObjectIdPayloadMap(expected_changed_payload_map),
279 REMOTE_NOTIFICATION));
270 p2p_notifier_.SendNotificationDataForTest( 280 p2p_notifier_.SendNotificationDataForTest(
271 P2PNotificationData("sender2", NOTIFY_ALL, changed_types)); 281 P2PNotificationData("sender2", NOTIFY_ALL, changed_types));
272 282
273 ReflectSentNotifications(); 283 ReflectSentNotifications();
274 284
275 // Should be dropped. 285 // Should be dropped.
276 Mock::VerifyAndClearExpectations(&mock_observer_); 286 Mock::VerifyAndClearExpectations(&mock_observer_);
277 p2p_notifier_.SendNotificationDataForTest( 287 p2p_notifier_.SendNotificationDataForTest(
278 P2PNotificationData("sender2", NOTIFY_ALL, ModelTypeSet())); 288 P2PNotificationData("sender2", NOTIFY_ALL, ModelTypeSet()));
279 289
280 ReflectSentNotifications(); 290 ReflectSentNotifications();
281 } 291 }
282 292
283 } // namespace 293 } // namespace
284 294
285 } // namespace syncer 295 } // namespace syncer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698