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
3 // found in the LICENSE file.
5 #include "sync/notifier/p2p_notifier.h"
9 #include "jingle/notifier/listener/fake_push_client.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"
13 #include "testing/gtest/include/gtest/gtest.h"
20 using ::testing::Mock
;
21 using ::testing::StrictMock
;
23 class P2PNotifierTest
: public testing::Test
{
26 : fake_push_client_(new notifier::FakePushClient()),
28 scoped_ptr
<notifier::PushClient
>(fake_push_client_
),
30 next_sent_notification_to_reflect_(0) {
31 p2p_notifier_
.RegisterHandler(&mock_observer_
);
34 virtual ~P2PNotifierTest() {
35 p2p_notifier_
.UnregisterHandler(&mock_observer_
);
38 ModelTypePayloadMap
MakePayloadMap(ModelTypeSet types
) {
39 return ModelTypePayloadMapFromEnumSet(types
, std::string());
42 // Simulate receiving all the notifications we sent out since last
43 // time this was called.
44 void ReflectSentNotifications() {
45 const std::vector
<notifier::Notification
>& sent_notifications
=
46 fake_push_client_
->sent_notifications();
47 for(size_t i
= next_sent_notification_to_reflect_
;
48 i
< sent_notifications
.size(); ++i
) {
49 p2p_notifier_
.OnIncomingNotification(sent_notifications
[i
]);
51 next_sent_notification_to_reflect_
= sent_notifications
.size();
54 // Owned by |p2p_notifier_|.
55 notifier::FakePushClient
* fake_push_client_
;
56 P2PNotifier p2p_notifier_
;
57 StrictMock
<MockSyncNotifierObserver
> mock_observer_
;
60 size_t next_sent_notification_to_reflect_
;
63 // Make sure the P2PNotificationTarget <-> string conversions work.
64 TEST_F(P2PNotifierTest
, P2PNotificationTarget
) {
65 for (int i
= FIRST_NOTIFICATION_TARGET
;
66 i
<= LAST_NOTIFICATION_TARGET
; ++i
) {
67 P2PNotificationTarget target
= static_cast<P2PNotificationTarget
>(i
);
68 const std::string
& target_str
= P2PNotificationTargetToString(target
);
69 EXPECT_FALSE(target_str
.empty());
70 EXPECT_EQ(target
, P2PNotificationTargetFromString(target_str
));
72 EXPECT_EQ(NOTIFY_SELF
, P2PNotificationTargetFromString("unknown"));
75 // Make sure notification targeting works correctly.
76 TEST_F(P2PNotifierTest
, P2PNotificationDataIsTargeted
) {
78 const P2PNotificationData
notification_data(
79 "sender", NOTIFY_SELF
, ModelTypeSet());
80 EXPECT_TRUE(notification_data
.IsTargeted("sender"));
81 EXPECT_FALSE(notification_data
.IsTargeted("other1"));
82 EXPECT_FALSE(notification_data
.IsTargeted("other2"));
85 const P2PNotificationData
notification_data(
86 "sender", NOTIFY_OTHERS
, ModelTypeSet());
87 EXPECT_FALSE(notification_data
.IsTargeted("sender"));
88 EXPECT_TRUE(notification_data
.IsTargeted("other1"));
89 EXPECT_TRUE(notification_data
.IsTargeted("other2"));
92 const P2PNotificationData
notification_data(
93 "sender", NOTIFY_ALL
, ModelTypeSet());
94 EXPECT_TRUE(notification_data
.IsTargeted("sender"));
95 EXPECT_TRUE(notification_data
.IsTargeted("other1"));
96 EXPECT_TRUE(notification_data
.IsTargeted("other2"));
100 // Make sure the P2PNotificationData <-> string conversions work for a
101 // default-constructed P2PNotificationData.
102 TEST_F(P2PNotifierTest
, P2PNotificationDataDefault
) {
103 const P2PNotificationData notification_data
;
104 EXPECT_TRUE(notification_data
.IsTargeted(""));
105 EXPECT_FALSE(notification_data
.IsTargeted("other1"));
106 EXPECT_FALSE(notification_data
.IsTargeted("other2"));
107 EXPECT_TRUE(notification_data
.GetChangedTypes().Empty());
108 const std::string
& notification_data_str
= notification_data
.ToString();
110 "{\"changedTypes\":[],\"notificationType\":\"notifySelf\","
111 "\"senderId\":\"\"}", notification_data_str
);
113 P2PNotificationData notification_data_parsed
;
114 EXPECT_TRUE(notification_data_parsed
.ResetFromString(notification_data_str
));
115 EXPECT_TRUE(notification_data
.Equals(notification_data_parsed
));
118 // Make sure the P2PNotificationData <-> string conversions work for a
119 // non-default-constructed P2PNotificationData.
120 TEST_F(P2PNotifierTest
, P2PNotificationDataNonDefault
) {
121 const ModelTypeSet
changed_types(BOOKMARKS
, THEMES
);
122 const P2PNotificationData
notification_data(
123 "sender", NOTIFY_ALL
, changed_types
);
124 EXPECT_TRUE(notification_data
.IsTargeted("sender"));
125 EXPECT_TRUE(notification_data
.IsTargeted("other1"));
126 EXPECT_TRUE(notification_data
.IsTargeted("other2"));
127 EXPECT_TRUE(notification_data
.GetChangedTypes().Equals(changed_types
));
128 const std::string
& notification_data_str
= notification_data
.ToString();
130 "{\"changedTypes\":[\"Bookmarks\",\"Themes\"],"
131 "\"notificationType\":\"notifyAll\","
132 "\"senderId\":\"sender\"}", notification_data_str
);
134 P2PNotificationData notification_data_parsed
;
135 EXPECT_TRUE(notification_data_parsed
.ResetFromString(notification_data_str
));
136 EXPECT_TRUE(notification_data
.Equals(notification_data_parsed
));
139 // Set up the P2PNotifier, simulate a successful connection, and send
140 // a notification with the default target (NOTIFY_OTHERS). The
141 // observer should receive only a notification from the call to
142 // UpdateEnabledTypes().
143 TEST_F(P2PNotifierTest
, NotificationsBasic
) {
144 const ModelTypeSet
enabled_types(BOOKMARKS
, PREFERENCES
);
146 EXPECT_CALL(mock_observer_
, OnNotificationsEnabled());
147 EXPECT_CALL(mock_observer_
, OnIncomingNotification(
148 ModelTypePayloadMapToObjectIdPayloadMap(MakePayloadMap(enabled_types
)),
149 REMOTE_NOTIFICATION
));
151 p2p_notifier_
.UpdateRegisteredIds(&mock_observer_
,
152 ModelTypeSetToObjectIdSet(enabled_types
));
154 p2p_notifier_
.SetUniqueId("sender");
156 const char kEmail
[] = "foo@bar.com";
157 const char kToken
[] = "token";
158 p2p_notifier_
.UpdateCredentials(kEmail
, kToken
);
160 notifier::Subscription expected_subscription
;
161 expected_subscription
.channel
= kSyncP2PNotificationChannel
;
162 expected_subscription
.from
= kEmail
;
163 EXPECT_TRUE(notifier::SubscriptionListsEqual(
164 fake_push_client_
->subscriptions(),
165 notifier::SubscriptionList(1, expected_subscription
)));
167 EXPECT_EQ(kEmail
, fake_push_client_
->email());
168 EXPECT_EQ(kToken
, fake_push_client_
->token());
170 ReflectSentNotifications();
171 fake_push_client_
->EnableNotifications();
173 // Sent with target NOTIFY_OTHERS so should not be propagated to
176 ModelTypeSet
changed_types(THEMES
, APPS
);
177 p2p_notifier_
.SendNotification(changed_types
);
180 ReflectSentNotifications();
183 // Set up the P2PNotifier and send out notifications with various
184 // target settings. The notifications received by the observer should
185 // be consistent with the target settings.
186 TEST_F(P2PNotifierTest
, SendNotificationData
) {
187 const ModelTypeSet
enabled_types(BOOKMARKS
, PREFERENCES
, THEMES
);
188 const ModelTypeSet
changed_types(THEMES
, APPS
);
189 const ModelTypeSet
expected_types(THEMES
);
191 EXPECT_CALL(mock_observer_
, OnNotificationsEnabled());
192 EXPECT_CALL(mock_observer_
,
193 OnIncomingNotification(
194 ModelTypePayloadMapToObjectIdPayloadMap(
195 MakePayloadMap(enabled_types
)),
196 REMOTE_NOTIFICATION
));
198 p2p_notifier_
.UpdateRegisteredIds(&mock_observer_
,
199 ModelTypeSetToObjectIdSet(enabled_types
));
201 const ModelTypePayloadMap
& expected_payload_map
=
202 MakePayloadMap(expected_types
);
204 p2p_notifier_
.SetUniqueId("sender");
205 p2p_notifier_
.UpdateCredentials("foo@bar.com", "fake_token");
207 ReflectSentNotifications();
208 fake_push_client_
->EnableNotifications();
210 ReflectSentNotifications();
212 // Should be dropped.
213 Mock::VerifyAndClearExpectations(&mock_observer_
);
214 p2p_notifier_
.SendNotificationDataForTest(P2PNotificationData());
216 ReflectSentNotifications();
218 // Should be propagated.
219 Mock::VerifyAndClearExpectations(&mock_observer_
);
220 EXPECT_CALL(mock_observer_
, OnIncomingNotification(
221 ModelTypePayloadMapToObjectIdPayloadMap(expected_payload_map
),
222 REMOTE_NOTIFICATION
));
223 p2p_notifier_
.SendNotificationDataForTest(
224 P2PNotificationData("sender", NOTIFY_SELF
, changed_types
));
226 ReflectSentNotifications();
228 // Should be dropped.
229 Mock::VerifyAndClearExpectations(&mock_observer_
);
230 p2p_notifier_
.SendNotificationDataForTest(
231 P2PNotificationData("sender2", NOTIFY_SELF
, changed_types
));
233 ReflectSentNotifications();
235 // Should be dropped.
236 Mock::VerifyAndClearExpectations(&mock_observer_
);
237 p2p_notifier_
.SendNotificationDataForTest(
238 P2PNotificationData("sender", NOTIFY_SELF
, ModelTypeSet()));
240 ReflectSentNotifications();
242 // Should be dropped.
243 p2p_notifier_
.SendNotificationDataForTest(
244 P2PNotificationData("sender", NOTIFY_OTHERS
, changed_types
));
246 ReflectSentNotifications();
248 // Should be propagated.
249 Mock::VerifyAndClearExpectations(&mock_observer_
);
250 EXPECT_CALL(mock_observer_
, OnIncomingNotification(
251 ModelTypePayloadMapToObjectIdPayloadMap(expected_payload_map
),
252 REMOTE_NOTIFICATION
));
253 p2p_notifier_
.SendNotificationDataForTest(
254 P2PNotificationData("sender2", NOTIFY_OTHERS
, changed_types
));
256 ReflectSentNotifications();
258 // Should be dropped.
259 Mock::VerifyAndClearExpectations(&mock_observer_
);
260 p2p_notifier_
.SendNotificationDataForTest(
261 P2PNotificationData("sender2", NOTIFY_OTHERS
, ModelTypeSet()));
263 ReflectSentNotifications();
265 // Should be propagated.
266 Mock::VerifyAndClearExpectations(&mock_observer_
);
267 EXPECT_CALL(mock_observer_
, OnIncomingNotification(
268 ModelTypePayloadMapToObjectIdPayloadMap(expected_payload_map
),
269 REMOTE_NOTIFICATION
));
270 p2p_notifier_
.SendNotificationDataForTest(
271 P2PNotificationData("sender", NOTIFY_ALL
, changed_types
));
273 ReflectSentNotifications();
275 // Should be propagated.
276 Mock::VerifyAndClearExpectations(&mock_observer_
);
277 EXPECT_CALL(mock_observer_
, OnIncomingNotification(
278 ModelTypePayloadMapToObjectIdPayloadMap(expected_payload_map
),
279 REMOTE_NOTIFICATION
));
280 p2p_notifier_
.SendNotificationDataForTest(
281 P2PNotificationData("sender2", NOTIFY_ALL
, changed_types
));
283 ReflectSentNotifications();
285 // Should be dropped.
286 Mock::VerifyAndClearExpectations(&mock_observer_
);
287 p2p_notifier_
.SendNotificationDataForTest(
288 P2PNotificationData("sender2", NOTIFY_ALL
, ModelTypeSet()));
290 ReflectSentNotifications();
295 } // namespace syncer