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

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

Issue 10696087: [Sync] Move ModelType and related classes to 'syncer' namespace (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: sort headers, update copyrights 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
« no previous file with comments | « sync/notifier/p2p_notifier.cc ('k') | sync/notifier/sync_notifier.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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"
(...skipping 16 matching lines...) Expand all
27 scoped_ptr<notifier::PushClient>(fake_push_client_), 27 scoped_ptr<notifier::PushClient>(fake_push_client_),
28 NOTIFY_OTHERS), 28 NOTIFY_OTHERS),
29 next_sent_notification_to_reflect_(0) { 29 next_sent_notification_to_reflect_(0) {
30 p2p_notifier_.AddObserver(&mock_observer_); 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_.RemoveObserver(&mock_observer_);
35 } 35 }
36 36
37 syncable::ModelTypePayloadMap MakePayloadMap( 37 syncer::ModelTypePayloadMap MakePayloadMap(syncer::ModelTypeSet types) {
38 syncable::ModelTypeSet types) { 38 return syncer::ModelTypePayloadMapFromEnumSet(types, "");
39 return syncable::ModelTypePayloadMapFromEnumSet(types, "");
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 =
46 fake_push_client_->sent_notifications(); 45 fake_push_client_->sent_notifications();
47 for(size_t i = next_sent_notification_to_reflect_; 46 for(size_t i = next_sent_notification_to_reflect_;
48 i < sent_notifications.size(); ++i) { 47 i < sent_notifications.size(); ++i) {
49 p2p_notifier_.OnIncomingNotification(sent_notifications[i]); 48 p2p_notifier_.OnIncomingNotification(sent_notifications[i]);
(...skipping 19 matching lines...) Expand all
69 EXPECT_FALSE(target_str.empty()); 68 EXPECT_FALSE(target_str.empty());
70 EXPECT_EQ(target, P2PNotificationTargetFromString(target_str)); 69 EXPECT_EQ(target, P2PNotificationTargetFromString(target_str));
71 } 70 }
72 EXPECT_EQ(NOTIFY_SELF, P2PNotificationTargetFromString("unknown")); 71 EXPECT_EQ(NOTIFY_SELF, P2PNotificationTargetFromString("unknown"));
73 } 72 }
74 73
75 // Make sure notification targeting works correctly. 74 // Make sure notification targeting works correctly.
76 TEST_F(P2PNotifierTest, P2PNotificationDataIsTargeted) { 75 TEST_F(P2PNotifierTest, P2PNotificationDataIsTargeted) {
77 { 76 {
78 const P2PNotificationData notification_data( 77 const P2PNotificationData notification_data(
79 "sender", NOTIFY_SELF, syncable::ModelTypeSet()); 78 "sender", NOTIFY_SELF, syncer::ModelTypeSet());
80 EXPECT_TRUE(notification_data.IsTargeted("sender")); 79 EXPECT_TRUE(notification_data.IsTargeted("sender"));
81 EXPECT_FALSE(notification_data.IsTargeted("other1")); 80 EXPECT_FALSE(notification_data.IsTargeted("other1"));
82 EXPECT_FALSE(notification_data.IsTargeted("other2")); 81 EXPECT_FALSE(notification_data.IsTargeted("other2"));
83 } 82 }
84 { 83 {
85 const P2PNotificationData notification_data( 84 const P2PNotificationData notification_data(
86 "sender", NOTIFY_OTHERS, syncable::ModelTypeSet()); 85 "sender", NOTIFY_OTHERS, syncer::ModelTypeSet());
87 EXPECT_FALSE(notification_data.IsTargeted("sender")); 86 EXPECT_FALSE(notification_data.IsTargeted("sender"));
88 EXPECT_TRUE(notification_data.IsTargeted("other1")); 87 EXPECT_TRUE(notification_data.IsTargeted("other1"));
89 EXPECT_TRUE(notification_data.IsTargeted("other2")); 88 EXPECT_TRUE(notification_data.IsTargeted("other2"));
90 } 89 }
91 { 90 {
92 const P2PNotificationData notification_data( 91 const P2PNotificationData notification_data(
93 "sender", NOTIFY_ALL, syncable::ModelTypeSet()); 92 "sender", NOTIFY_ALL, syncer::ModelTypeSet());
94 EXPECT_TRUE(notification_data.IsTargeted("sender")); 93 EXPECT_TRUE(notification_data.IsTargeted("sender"));
95 EXPECT_TRUE(notification_data.IsTargeted("other1")); 94 EXPECT_TRUE(notification_data.IsTargeted("other1"));
96 EXPECT_TRUE(notification_data.IsTargeted("other2")); 95 EXPECT_TRUE(notification_data.IsTargeted("other2"));
97 } 96 }
98 } 97 }
99 98
100 // Make sure the P2PNotificationData <-> string conversions work for a 99 // Make sure the P2PNotificationData <-> string conversions work for a
101 // default-constructed P2PNotificationData. 100 // default-constructed P2PNotificationData.
102 TEST_F(P2PNotifierTest, P2PNotificationDataDefault) { 101 TEST_F(P2PNotifierTest, P2PNotificationDataDefault) {
103 const P2PNotificationData notification_data; 102 const P2PNotificationData notification_data;
104 EXPECT_TRUE(notification_data.IsTargeted("")); 103 EXPECT_TRUE(notification_data.IsTargeted(""));
105 EXPECT_FALSE(notification_data.IsTargeted("other1")); 104 EXPECT_FALSE(notification_data.IsTargeted("other1"));
106 EXPECT_FALSE(notification_data.IsTargeted("other2")); 105 EXPECT_FALSE(notification_data.IsTargeted("other2"));
107 EXPECT_TRUE(notification_data.GetChangedTypes().Empty()); 106 EXPECT_TRUE(notification_data.GetChangedTypes().Empty());
108 const std::string& notification_data_str = notification_data.ToString(); 107 const std::string& notification_data_str = notification_data.ToString();
109 EXPECT_EQ( 108 EXPECT_EQ(
110 "{\"changedTypes\":[],\"notificationType\":\"notifySelf\"," 109 "{\"changedTypes\":[],\"notificationType\":\"notifySelf\","
111 "\"senderId\":\"\"}", notification_data_str); 110 "\"senderId\":\"\"}", notification_data_str);
112 111
113 P2PNotificationData notification_data_parsed; 112 P2PNotificationData notification_data_parsed;
114 EXPECT_TRUE(notification_data_parsed.ResetFromString(notification_data_str)); 113 EXPECT_TRUE(notification_data_parsed.ResetFromString(notification_data_str));
115 EXPECT_TRUE(notification_data.Equals(notification_data_parsed)); 114 EXPECT_TRUE(notification_data.Equals(notification_data_parsed));
116 } 115 }
117 116
118 // Make sure the P2PNotificationData <-> string conversions work for a 117 // Make sure the P2PNotificationData <-> string conversions work for a
119 // non-default-constructed P2PNotificationData. 118 // non-default-constructed P2PNotificationData.
120 TEST_F(P2PNotifierTest, P2PNotificationDataNonDefault) { 119 TEST_F(P2PNotifierTest, P2PNotificationDataNonDefault) {
121 const syncable::ModelTypeSet changed_types( 120 const syncer::ModelTypeSet changed_types(syncer::BOOKMARKS, syncer::THEMES);
122 syncable::BOOKMARKS, syncable::THEMES);
123 const P2PNotificationData notification_data( 121 const P2PNotificationData notification_data(
124 "sender", NOTIFY_ALL, changed_types); 122 "sender", NOTIFY_ALL, changed_types);
125 EXPECT_TRUE(notification_data.IsTargeted("sender")); 123 EXPECT_TRUE(notification_data.IsTargeted("sender"));
126 EXPECT_TRUE(notification_data.IsTargeted("other1")); 124 EXPECT_TRUE(notification_data.IsTargeted("other1"));
127 EXPECT_TRUE(notification_data.IsTargeted("other2")); 125 EXPECT_TRUE(notification_data.IsTargeted("other2"));
128 EXPECT_TRUE(notification_data.GetChangedTypes().Equals(changed_types)); 126 EXPECT_TRUE(notification_data.GetChangedTypes().Equals(changed_types));
129 const std::string& notification_data_str = notification_data.ToString(); 127 const std::string& notification_data_str = notification_data.ToString();
130 EXPECT_EQ( 128 EXPECT_EQ(
131 "{\"changedTypes\":[\"Bookmarks\",\"Themes\"]," 129 "{\"changedTypes\":[\"Bookmarks\",\"Themes\"],"
132 "\"notificationType\":\"notifyAll\"," 130 "\"notificationType\":\"notifyAll\","
133 "\"senderId\":\"sender\"}", notification_data_str); 131 "\"senderId\":\"sender\"}", notification_data_str);
134 132
135 P2PNotificationData notification_data_parsed; 133 P2PNotificationData notification_data_parsed;
136 EXPECT_TRUE(notification_data_parsed.ResetFromString(notification_data_str)); 134 EXPECT_TRUE(notification_data_parsed.ResetFromString(notification_data_str));
137 EXPECT_TRUE(notification_data.Equals(notification_data_parsed)); 135 EXPECT_TRUE(notification_data.Equals(notification_data_parsed));
138 } 136 }
139 137
140 // Set up the P2PNotifier, simulate a successful connection, and send 138 // Set up the P2PNotifier, simulate a successful connection, and send
141 // a notification with the default target (NOTIFY_OTHERS). The 139 // a notification with the default target (NOTIFY_OTHERS). The
142 // observer should receive only a notification from the call to 140 // observer should receive only a notification from the call to
143 // UpdateEnabledTypes(). 141 // UpdateEnabledTypes().
144 TEST_F(P2PNotifierTest, NotificationsBasic) { 142 TEST_F(P2PNotifierTest, NotificationsBasic) {
145 syncable::ModelTypeSet enabled_types( 143 syncer::ModelTypeSet enabled_types(syncer::BOOKMARKS, syncer::PREFERENCES);
146 syncable::BOOKMARKS, syncable::PREFERENCES);
147 144
148 EXPECT_CALL(mock_observer_, OnNotificationsEnabled()); 145 EXPECT_CALL(mock_observer_, OnNotificationsEnabled());
149 EXPECT_CALL(mock_observer_, 146 EXPECT_CALL(mock_observer_,
150 OnIncomingNotification(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";
(...skipping 10 matching lines...) Expand all
167 EXPECT_EQ(kToken, fake_push_client_->token()); 164 EXPECT_EQ(kToken, fake_push_client_->token());
168 165
169 p2p_notifier_.UpdateEnabledTypes(enabled_types); 166 p2p_notifier_.UpdateEnabledTypes(enabled_types);
170 167
171 ReflectSentNotifications(); 168 ReflectSentNotifications();
172 fake_push_client_->EnableNotifications(); 169 fake_push_client_->EnableNotifications();
173 170
174 // Sent with target NOTIFY_OTHERS so should not be propagated to 171 // Sent with target NOTIFY_OTHERS so should not be propagated to
175 // |mock_observer_|. 172 // |mock_observer_|.
176 { 173 {
177 syncable::ModelTypeSet changed_types( 174 syncer::ModelTypeSet changed_types(syncer::THEMES, syncer::APPS);
178 syncable::THEMES, syncable::APPS);
179 p2p_notifier_.SendNotification(changed_types); 175 p2p_notifier_.SendNotification(changed_types);
180 } 176 }
181 177
182 ReflectSentNotifications(); 178 ReflectSentNotifications();
183 } 179 }
184 180
185 // Set up the P2PNotifier and send out notifications with various 181 // Set up the P2PNotifier and send out notifications with various
186 // target settings. The notifications received by the observer should 182 // target settings. The notifications received by the observer should
187 // be consistent with the target settings. 183 // be consistent with the target settings.
188 TEST_F(P2PNotifierTest, SendNotificationData) { 184 TEST_F(P2PNotifierTest, SendNotificationData) {
189 syncable::ModelTypeSet enabled_types( 185 syncer::ModelTypeSet enabled_types(syncer::BOOKMARKS, syncer::PREFERENCES);
190 syncable::BOOKMARKS, syncable::PREFERENCES);
191 186
192 syncable::ModelTypeSet changed_types( 187 syncer::ModelTypeSet changed_types(syncer::THEMES, syncer::APPS);
193 syncable::THEMES, syncable::APPS);
194 188
195 const syncable::ModelTypePayloadMap& changed_payload_map = 189 const syncer::ModelTypePayloadMap& changed_payload_map =
196 MakePayloadMap(changed_types); 190 MakePayloadMap(changed_types);
197 191
198 EXPECT_CALL(mock_observer_, OnNotificationsEnabled()); 192 EXPECT_CALL(mock_observer_, OnNotificationsEnabled());
199 EXPECT_CALL(mock_observer_, 193 EXPECT_CALL(mock_observer_,
200 OnIncomingNotification(MakePayloadMap(enabled_types), 194 OnIncomingNotification(MakePayloadMap(enabled_types),
201 REMOTE_NOTIFICATION)); 195 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");
205 p2p_notifier_.UpdateEnabledTypes(enabled_types); 199 p2p_notifier_.UpdateEnabledTypes(enabled_types);
(...skipping 21 matching lines...) Expand all
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, syncable::ModelTypeSet())); 231 P2PNotificationData("sender", NOTIFY_SELF, syncer::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(changed_payload_map, 243 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map,
250 REMOTE_NOTIFICATION)); 244 REMOTE_NOTIFICATION));
251 p2p_notifier_.SendNotificationDataForTest( 245 p2p_notifier_.SendNotificationDataForTest(
252 P2PNotificationData("sender2", NOTIFY_OTHERS, changed_types)); 246 P2PNotificationData("sender2", NOTIFY_OTHERS, changed_types));
253 247
254 ReflectSentNotifications(); 248 ReflectSentNotifications();
255 249
256 // Should be dropped. 250 // Should be dropped.
257 Mock::VerifyAndClearExpectations(&mock_observer_); 251 Mock::VerifyAndClearExpectations(&mock_observer_);
258 p2p_notifier_.SendNotificationDataForTest( 252 p2p_notifier_.SendNotificationDataForTest(
259 P2PNotificationData("sender2", NOTIFY_OTHERS, syncable::ModelTypeSet())); 253 P2PNotificationData("sender2", NOTIFY_OTHERS, syncer::ModelTypeSet()));
260 254
261 ReflectSentNotifications(); 255 ReflectSentNotifications();
262 256
263 // Should be propagated. 257 // Should be propagated.
264 Mock::VerifyAndClearExpectations(&mock_observer_); 258 Mock::VerifyAndClearExpectations(&mock_observer_);
265 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map, 259 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map,
266 REMOTE_NOTIFICATION)); 260 REMOTE_NOTIFICATION));
267 p2p_notifier_.SendNotificationDataForTest( 261 p2p_notifier_.SendNotificationDataForTest(
268 P2PNotificationData("sender", NOTIFY_ALL, changed_types)); 262 P2PNotificationData("sender", NOTIFY_ALL, changed_types));
269 263
270 ReflectSentNotifications(); 264 ReflectSentNotifications();
271 265
272 // Should be propagated. 266 // Should be propagated.
273 Mock::VerifyAndClearExpectations(&mock_observer_); 267 Mock::VerifyAndClearExpectations(&mock_observer_);
274 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map, 268 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map,
275 REMOTE_NOTIFICATION)); 269 REMOTE_NOTIFICATION));
276 p2p_notifier_.SendNotificationDataForTest( 270 p2p_notifier_.SendNotificationDataForTest(
277 P2PNotificationData("sender2", NOTIFY_ALL, changed_types)); 271 P2PNotificationData("sender2", NOTIFY_ALL, changed_types));
278 272
279 ReflectSentNotifications(); 273 ReflectSentNotifications();
280 274
281 // Should be dropped. 275 // Should be dropped.
282 Mock::VerifyAndClearExpectations(&mock_observer_); 276 Mock::VerifyAndClearExpectations(&mock_observer_);
283 p2p_notifier_.SendNotificationDataForTest( 277 p2p_notifier_.SendNotificationDataForTest(
284 P2PNotificationData("sender2", NOTIFY_ALL, syncable::ModelTypeSet())); 278 P2PNotificationData("sender2", NOTIFY_ALL, syncer::ModelTypeSet()));
285 279
286 ReflectSentNotifications(); 280 ReflectSentNotifications();
287 } 281 }
288 282
289 } // namespace 283 } // namespace
290 284
291 } // namespace syncer 285 } // namespace syncer
OLDNEW
« no previous file with comments | « sync/notifier/p2p_notifier.cc ('k') | sync/notifier/sync_notifier.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698