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

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

Issue 10875064: Rename SyncNotifier->Invalidator and SyncNotifierObserver->InvalidationHandler. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merge to ToT for landing Created 8 years, 3 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
(Empty)
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.
4
5 #include "sync/notifier/p2p_notifier.h"
6
7 #include <cstddef>
8
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_state_map.h"
12 #include "sync/notifier/fake_sync_notifier_observer.h"
13 #include "sync/notifier/object_id_state_map_test_util.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace syncer {
17
18 namespace {
19
20 class P2PNotifierTest : public testing::Test {
21 protected:
22 P2PNotifierTest()
23 : fake_push_client_(new notifier::FakePushClient()),
24 p2p_notifier_(
25 scoped_ptr<notifier::PushClient>(fake_push_client_),
26 NOTIFY_OTHERS),
27 next_sent_notification_to_reflect_(0) {
28 p2p_notifier_.RegisterHandler(&fake_observer_);
29 }
30
31 virtual ~P2PNotifierTest() {
32 p2p_notifier_.UnregisterHandler(&fake_observer_);
33 }
34
35 ModelTypeStateMap MakeStateMap(ModelTypeSet types) {
36 return ModelTypeSetToStateMap(types, std::string());
37 }
38
39 // Simulate receiving all the notifications we sent out since last
40 // time this was called.
41 void ReflectSentNotifications() {
42 const std::vector<notifier::Notification>& sent_notifications =
43 fake_push_client_->sent_notifications();
44 for(size_t i = next_sent_notification_to_reflect_;
45 i < sent_notifications.size(); ++i) {
46 p2p_notifier_.OnIncomingNotification(sent_notifications[i]);
47 }
48 next_sent_notification_to_reflect_ = sent_notifications.size();
49 }
50
51 // Owned by |p2p_notifier_|.
52 notifier::FakePushClient* fake_push_client_;
53 P2PNotifier p2p_notifier_;
54 FakeSyncNotifierObserver fake_observer_;
55
56 private:
57 size_t next_sent_notification_to_reflect_;
58 };
59
60 // Make sure the P2PNotificationTarget <-> string conversions work.
61 TEST_F(P2PNotifierTest, P2PNotificationTarget) {
62 for (int i = FIRST_NOTIFICATION_TARGET;
63 i <= LAST_NOTIFICATION_TARGET; ++i) {
64 P2PNotificationTarget target = static_cast<P2PNotificationTarget>(i);
65 const std::string& target_str = P2PNotificationTargetToString(target);
66 EXPECT_FALSE(target_str.empty());
67 EXPECT_EQ(target, P2PNotificationTargetFromString(target_str));
68 }
69 EXPECT_EQ(NOTIFY_SELF, P2PNotificationTargetFromString("unknown"));
70 }
71
72 // Make sure notification targeting works correctly.
73 TEST_F(P2PNotifierTest, P2PNotificationDataIsTargeted) {
74 {
75 const P2PNotificationData notification_data(
76 "sender", NOTIFY_SELF, ModelTypeSet());
77 EXPECT_TRUE(notification_data.IsTargeted("sender"));
78 EXPECT_FALSE(notification_data.IsTargeted("other1"));
79 EXPECT_FALSE(notification_data.IsTargeted("other2"));
80 }
81 {
82 const P2PNotificationData notification_data(
83 "sender", NOTIFY_OTHERS, ModelTypeSet());
84 EXPECT_FALSE(notification_data.IsTargeted("sender"));
85 EXPECT_TRUE(notification_data.IsTargeted("other1"));
86 EXPECT_TRUE(notification_data.IsTargeted("other2"));
87 }
88 {
89 const P2PNotificationData notification_data(
90 "sender", NOTIFY_ALL, ModelTypeSet());
91 EXPECT_TRUE(notification_data.IsTargeted("sender"));
92 EXPECT_TRUE(notification_data.IsTargeted("other1"));
93 EXPECT_TRUE(notification_data.IsTargeted("other2"));
94 }
95 }
96
97 // Make sure the P2PNotificationData <-> string conversions work for a
98 // default-constructed P2PNotificationData.
99 TEST_F(P2PNotifierTest, P2PNotificationDataDefault) {
100 const P2PNotificationData notification_data;
101 EXPECT_TRUE(notification_data.IsTargeted(""));
102 EXPECT_FALSE(notification_data.IsTargeted("other1"));
103 EXPECT_FALSE(notification_data.IsTargeted("other2"));
104 EXPECT_TRUE(notification_data.GetChangedTypes().Empty());
105 const std::string& notification_data_str = notification_data.ToString();
106 EXPECT_EQ(
107 "{\"changedTypes\":[],\"notificationType\":\"notifySelf\","
108 "\"senderId\":\"\"}", notification_data_str);
109
110 P2PNotificationData notification_data_parsed;
111 EXPECT_TRUE(notification_data_parsed.ResetFromString(notification_data_str));
112 EXPECT_TRUE(notification_data.Equals(notification_data_parsed));
113 }
114
115 // Make sure the P2PNotificationData <-> string conversions work for a
116 // non-default-constructed P2PNotificationData.
117 TEST_F(P2PNotifierTest, P2PNotificationDataNonDefault) {
118 const ModelTypeSet changed_types(BOOKMARKS, THEMES);
119 const P2PNotificationData notification_data(
120 "sender", NOTIFY_ALL, changed_types);
121 EXPECT_TRUE(notification_data.IsTargeted("sender"));
122 EXPECT_TRUE(notification_data.IsTargeted("other1"));
123 EXPECT_TRUE(notification_data.IsTargeted("other2"));
124 EXPECT_TRUE(notification_data.GetChangedTypes().Equals(changed_types));
125 const std::string& notification_data_str = notification_data.ToString();
126 EXPECT_EQ(
127 "{\"changedTypes\":[\"Bookmarks\",\"Themes\"],"
128 "\"notificationType\":\"notifyAll\","
129 "\"senderId\":\"sender\"}", notification_data_str);
130
131 P2PNotificationData notification_data_parsed;
132 EXPECT_TRUE(notification_data_parsed.ResetFromString(notification_data_str));
133 EXPECT_TRUE(notification_data.Equals(notification_data_parsed));
134 }
135
136 // Set up the P2PNotifier, simulate a successful connection, and send
137 // a notification with the default target (NOTIFY_OTHERS). The
138 // observer should receive only a notification from the call to
139 // UpdateEnabledTypes().
140 TEST_F(P2PNotifierTest, NotificationsBasic) {
141 const ModelTypeSet enabled_types(BOOKMARKS, PREFERENCES);
142
143 p2p_notifier_.UpdateRegisteredIds(&fake_observer_,
144 ModelTypeSetToObjectIdSet(enabled_types));
145
146 p2p_notifier_.SetUniqueId("sender");
147
148 const char kEmail[] = "foo@bar.com";
149 const char kToken[] = "token";
150 p2p_notifier_.UpdateCredentials(kEmail, kToken);
151 {
152 notifier::Subscription expected_subscription;
153 expected_subscription.channel = kSyncP2PNotificationChannel;
154 expected_subscription.from = kEmail;
155 EXPECT_TRUE(notifier::SubscriptionListsEqual(
156 fake_push_client_->subscriptions(),
157 notifier::SubscriptionList(1, expected_subscription)));
158 }
159 EXPECT_EQ(kEmail, fake_push_client_->email());
160 EXPECT_EQ(kToken, fake_push_client_->token());
161
162 ReflectSentNotifications();
163 fake_push_client_->EnableNotifications();
164 EXPECT_EQ(NO_NOTIFICATION_ERROR,
165 fake_observer_.GetNotificationsDisabledReason());
166
167 ReflectSentNotifications();
168 EXPECT_EQ(1, fake_observer_.GetNotificationCount());
169 EXPECT_THAT(
170 ModelTypeStateMapToObjectIdStateMap(MakeStateMap(enabled_types)),
171 Eq(fake_observer_.GetLastNotificationIdStateMap()));
172 EXPECT_EQ(REMOTE_NOTIFICATION, fake_observer_.GetLastNotificationSource());
173
174 // Sent with target NOTIFY_OTHERS so should not be propagated to
175 // |fake_observer_|.
176 {
177 ModelTypeSet changed_types(THEMES, APPS);
178 p2p_notifier_.SendNotification(changed_types);
179 }
180
181 ReflectSentNotifications();
182 EXPECT_EQ(1, fake_observer_.GetNotificationCount());
183 }
184
185 // Set up the P2PNotifier and send out notifications with various
186 // target settings. The notifications received by the observer should
187 // be consistent with the target settings.
188 TEST_F(P2PNotifierTest, SendNotificationData) {
189 const ModelTypeSet enabled_types(BOOKMARKS, PREFERENCES, THEMES);
190 const ModelTypeSet changed_types(THEMES, APPS);
191 const ModelTypeSet expected_types(THEMES);
192
193 p2p_notifier_.UpdateRegisteredIds(&fake_observer_,
194 ModelTypeSetToObjectIdSet(enabled_types));
195
196 p2p_notifier_.SetUniqueId("sender");
197 p2p_notifier_.UpdateCredentials("foo@bar.com", "fake_token");
198
199 ReflectSentNotifications();
200 fake_push_client_->EnableNotifications();
201 EXPECT_EQ(NO_NOTIFICATION_ERROR,
202 fake_observer_.GetNotificationsDisabledReason());
203
204 ReflectSentNotifications();
205 EXPECT_EQ(1, fake_observer_.GetNotificationCount());
206 EXPECT_THAT(
207 ModelTypeStateMapToObjectIdStateMap(MakeStateMap(enabled_types)),
208 Eq(fake_observer_.GetLastNotificationIdStateMap()));
209 EXPECT_EQ(REMOTE_NOTIFICATION, fake_observer_.GetLastNotificationSource());
210
211 // Should be dropped.
212 p2p_notifier_.SendNotificationDataForTest(P2PNotificationData());
213 ReflectSentNotifications();
214 EXPECT_EQ(1, fake_observer_.GetNotificationCount());
215
216 const ObjectIdStateMap& expected_ids =
217 ModelTypeStateMapToObjectIdStateMap(MakeStateMap(expected_types));
218
219 // Should be propagated.
220 p2p_notifier_.SendNotificationDataForTest(
221 P2PNotificationData("sender", NOTIFY_SELF, changed_types));
222 ReflectSentNotifications();
223 EXPECT_EQ(2, fake_observer_.GetNotificationCount());
224 EXPECT_THAT(
225 expected_ids,
226 Eq(fake_observer_.GetLastNotificationIdStateMap()));
227
228 // Should be dropped.
229 p2p_notifier_.SendNotificationDataForTest(
230 P2PNotificationData("sender2", NOTIFY_SELF, changed_types));
231 ReflectSentNotifications();
232 EXPECT_EQ(2, fake_observer_.GetNotificationCount());
233
234 // Should be dropped.
235 p2p_notifier_.SendNotificationDataForTest(
236 P2PNotificationData("sender", NOTIFY_SELF, ModelTypeSet()));
237 ReflectSentNotifications();
238 EXPECT_EQ(2, fake_observer_.GetNotificationCount());
239
240 // Should be dropped.
241 p2p_notifier_.SendNotificationDataForTest(
242 P2PNotificationData("sender", NOTIFY_OTHERS, changed_types));
243 ReflectSentNotifications();
244 EXPECT_EQ(2, fake_observer_.GetNotificationCount());
245
246 // Should be propagated.
247 p2p_notifier_.SendNotificationDataForTest(
248 P2PNotificationData("sender2", NOTIFY_OTHERS, changed_types));
249 ReflectSentNotifications();
250 EXPECT_EQ(3, fake_observer_.GetNotificationCount());
251 EXPECT_THAT(
252 expected_ids,
253 Eq(fake_observer_.GetLastNotificationIdStateMap()));
254
255 // Should be dropped.
256 p2p_notifier_.SendNotificationDataForTest(
257 P2PNotificationData("sender2", NOTIFY_OTHERS, ModelTypeSet()));
258 ReflectSentNotifications();
259 EXPECT_EQ(3, fake_observer_.GetNotificationCount());
260
261 // Should be propagated.
262 p2p_notifier_.SendNotificationDataForTest(
263 P2PNotificationData("sender", NOTIFY_ALL, changed_types));
264 ReflectSentNotifications();
265 EXPECT_EQ(4, fake_observer_.GetNotificationCount());
266 EXPECT_THAT(
267 expected_ids,
268 Eq(fake_observer_.GetLastNotificationIdStateMap()));
269
270 // Should be propagated.
271 p2p_notifier_.SendNotificationDataForTest(
272 P2PNotificationData("sender2", NOTIFY_ALL, changed_types));
273 ReflectSentNotifications();
274 EXPECT_EQ(5, fake_observer_.GetNotificationCount());
275 EXPECT_THAT(
276 expected_ids,
277 Eq(fake_observer_.GetLastNotificationIdStateMap()));
278
279 // Should be dropped.
280 p2p_notifier_.SendNotificationDataForTest(
281 P2PNotificationData("sender2", NOTIFY_ALL, ModelTypeSet()));
282 ReflectSentNotifications();
283 EXPECT_EQ(5, fake_observer_.GetNotificationCount());
284 }
285
286 } // namespace
287
288 } // 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