OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/sync/notifier/p2p_notifier.h" | 5 #include "chrome/browser/sync/notifier/p2p_notifier.h" |
6 | 6 |
7 #include <cstddef> | 7 #include <cstddef> |
8 | 8 |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 p2p_notifier_->AddObserver(&mock_observer_); | 68 p2p_notifier_->AddObserver(&mock_observer_); |
69 } | 69 } |
70 | 70 |
71 virtual void TearDown() { | 71 virtual void TearDown() { |
72 p2p_notifier_->RemoveObserver(&mock_observer_); | 72 p2p_notifier_->RemoveObserver(&mock_observer_); |
73 p2p_notifier_.reset(); | 73 p2p_notifier_.reset(); |
74 talk_mediator_ = NULL; | 74 talk_mediator_ = NULL; |
75 } | 75 } |
76 | 76 |
77 syncable::ModelTypePayloadMap MakePayloadMap( | 77 syncable::ModelTypePayloadMap MakePayloadMap( |
78 const syncable::ModelTypeSet& types) { | 78 syncable::ModelEnumSet types) { |
79 return syncable::ModelTypePayloadMapFromBitSet( | 79 return syncable::ModelTypePayloadMapFromEnumSet(types, ""); |
80 syncable::ModelTypeBitSetFromSet(types), ""); | |
81 } | 80 } |
82 | 81 |
83 MessageLoop message_loop_; | 82 MessageLoop message_loop_; |
84 // Owned by |p2p_notifier_|. | 83 // Owned by |p2p_notifier_|. |
85 notifier::TalkMediator* talk_mediator_; | 84 notifier::TalkMediator* talk_mediator_; |
86 scoped_ptr<P2PNotifier> p2p_notifier_; | 85 scoped_ptr<P2PNotifier> p2p_notifier_; |
87 StrictMock<MockSyncNotifierObserver> mock_observer_; | 86 StrictMock<MockSyncNotifierObserver> mock_observer_; |
88 }; | 87 }; |
89 | 88 |
90 TEST_F(P2PNotifierTest, P2PNotificationTarget) { | 89 TEST_F(P2PNotifierTest, P2PNotificationTarget) { |
91 for (int i = FIRST_NOTIFICATION_TARGET; | 90 for (int i = FIRST_NOTIFICATION_TARGET; |
92 i <= LAST_NOTIFICATION_TARGET; ++i) { | 91 i <= LAST_NOTIFICATION_TARGET; ++i) { |
93 P2PNotificationTarget target = static_cast<P2PNotificationTarget>(i); | 92 P2PNotificationTarget target = static_cast<P2PNotificationTarget>(i); |
94 const std::string& target_str = P2PNotificationTargetToString(target); | 93 const std::string& target_str = P2PNotificationTargetToString(target); |
95 EXPECT_FALSE(target_str.empty()); | 94 EXPECT_FALSE(target_str.empty()); |
96 EXPECT_EQ(target, P2PNotificationTargetFromString(target_str)); | 95 EXPECT_EQ(target, P2PNotificationTargetFromString(target_str)); |
97 } | 96 } |
98 EXPECT_EQ(NOTIFY_SELF, P2PNotificationTargetFromString("unknown")); | 97 EXPECT_EQ(NOTIFY_SELF, P2PNotificationTargetFromString("unknown")); |
99 } | 98 } |
100 | 99 |
101 TEST_F(P2PNotifierTest, P2PNotificationDataIsTargeted) { | 100 TEST_F(P2PNotifierTest, P2PNotificationDataIsTargeted) { |
102 { | 101 { |
103 const P2PNotificationData notification_data( | 102 const P2PNotificationData notification_data( |
104 "sender", NOTIFY_SELF, syncable::ModelTypeSet()); | 103 "sender", NOTIFY_SELF, syncable::ModelEnumSet()); |
105 EXPECT_TRUE(notification_data.IsTargeted("sender")); | 104 EXPECT_TRUE(notification_data.IsTargeted("sender")); |
106 EXPECT_FALSE(notification_data.IsTargeted("other1")); | 105 EXPECT_FALSE(notification_data.IsTargeted("other1")); |
107 EXPECT_FALSE(notification_data.IsTargeted("other2")); | 106 EXPECT_FALSE(notification_data.IsTargeted("other2")); |
108 } | 107 } |
109 { | 108 { |
110 const P2PNotificationData notification_data( | 109 const P2PNotificationData notification_data( |
111 "sender", NOTIFY_OTHERS, syncable::ModelTypeSet()); | 110 "sender", NOTIFY_OTHERS, syncable::ModelEnumSet()); |
112 EXPECT_FALSE(notification_data.IsTargeted("sender")); | 111 EXPECT_FALSE(notification_data.IsTargeted("sender")); |
113 EXPECT_TRUE(notification_data.IsTargeted("other1")); | 112 EXPECT_TRUE(notification_data.IsTargeted("other1")); |
114 EXPECT_TRUE(notification_data.IsTargeted("other2")); | 113 EXPECT_TRUE(notification_data.IsTargeted("other2")); |
115 } | 114 } |
116 { | 115 { |
117 const P2PNotificationData notification_data( | 116 const P2PNotificationData notification_data( |
118 "sender", NOTIFY_ALL, syncable::ModelTypeSet()); | 117 "sender", NOTIFY_ALL, syncable::ModelEnumSet()); |
119 EXPECT_TRUE(notification_data.IsTargeted("sender")); | 118 EXPECT_TRUE(notification_data.IsTargeted("sender")); |
120 EXPECT_TRUE(notification_data.IsTargeted("other1")); | 119 EXPECT_TRUE(notification_data.IsTargeted("other1")); |
121 EXPECT_TRUE(notification_data.IsTargeted("other2")); | 120 EXPECT_TRUE(notification_data.IsTargeted("other2")); |
122 } | 121 } |
123 } | 122 } |
124 | 123 |
125 TEST_F(P2PNotifierTest, P2PNotificationDataDefault) { | 124 TEST_F(P2PNotifierTest, P2PNotificationDataDefault) { |
126 const P2PNotificationData notification_data; | 125 const P2PNotificationData notification_data; |
127 EXPECT_TRUE(notification_data.IsTargeted("")); | 126 EXPECT_TRUE(notification_data.IsTargeted("")); |
128 EXPECT_FALSE(notification_data.IsTargeted("other1")); | 127 EXPECT_FALSE(notification_data.IsTargeted("other1")); |
129 EXPECT_FALSE(notification_data.IsTargeted("other2")); | 128 EXPECT_FALSE(notification_data.IsTargeted("other2")); |
130 EXPECT_TRUE(notification_data.GetChangedTypes().empty()); | 129 EXPECT_TRUE(notification_data.GetChangedTypes().Empty()); |
131 const std::string& notification_data_str = notification_data.ToString(); | 130 const std::string& notification_data_str = notification_data.ToString(); |
132 EXPECT_EQ( | 131 EXPECT_EQ( |
133 "{\"changedTypes\":[],\"notificationType\":\"notifySelf\"," | 132 "{\"changedTypes\":[],\"notificationType\":\"notifySelf\"," |
134 "\"senderId\":\"\"}", notification_data_str); | 133 "\"senderId\":\"\"}", notification_data_str); |
135 | 134 |
136 P2PNotificationData notification_data_parsed; | 135 P2PNotificationData notification_data_parsed; |
137 EXPECT_TRUE(notification_data_parsed.ResetFromString(notification_data_str)); | 136 EXPECT_TRUE(notification_data_parsed.ResetFromString(notification_data_str)); |
138 EXPECT_TRUE(notification_data.Equals(notification_data_parsed)); | 137 EXPECT_TRUE(notification_data.Equals(notification_data_parsed)); |
139 } | 138 } |
140 | 139 |
141 TEST_F(P2PNotifierTest, P2PNotificationDataNonDefault) { | 140 TEST_F(P2PNotifierTest, P2PNotificationDataNonDefault) { |
142 syncable::ModelTypeSet changed_types; | 141 const syncable::ModelEnumSet changed_types( |
143 changed_types.insert(syncable::BOOKMARKS); | 142 syncable::BOOKMARKS, syncable::THEMES); |
144 changed_types.insert(syncable::THEMES); | |
145 const P2PNotificationData notification_data( | 143 const P2PNotificationData notification_data( |
146 "sender", NOTIFY_ALL, changed_types); | 144 "sender", NOTIFY_ALL, changed_types); |
147 EXPECT_TRUE(notification_data.IsTargeted("sender")); | 145 EXPECT_TRUE(notification_data.IsTargeted("sender")); |
148 EXPECT_TRUE(notification_data.IsTargeted("other1")); | 146 EXPECT_TRUE(notification_data.IsTargeted("other1")); |
149 EXPECT_TRUE(notification_data.IsTargeted("other2")); | 147 EXPECT_TRUE(notification_data.IsTargeted("other2")); |
150 EXPECT_EQ(changed_types, notification_data.GetChangedTypes()); | 148 EXPECT_TRUE(notification_data.GetChangedTypes().Equals(changed_types)); |
151 const std::string& notification_data_str = notification_data.ToString(); | 149 const std::string& notification_data_str = notification_data.ToString(); |
152 EXPECT_EQ( | 150 EXPECT_EQ( |
153 "{\"changedTypes\":[\"Bookmarks\",\"Themes\"]," | 151 "{\"changedTypes\":[\"Bookmarks\",\"Themes\"]," |
154 "\"notificationType\":\"notifyAll\"," | 152 "\"notificationType\":\"notifyAll\"," |
155 "\"senderId\":\"sender\"}", notification_data_str); | 153 "\"senderId\":\"sender\"}", notification_data_str); |
156 | 154 |
157 P2PNotificationData notification_data_parsed; | 155 P2PNotificationData notification_data_parsed; |
158 EXPECT_TRUE(notification_data_parsed.ResetFromString(notification_data_str)); | 156 EXPECT_TRUE(notification_data_parsed.ResetFromString(notification_data_str)); |
159 EXPECT_TRUE(notification_data.Equals(notification_data_parsed)); | 157 EXPECT_TRUE(notification_data.Equals(notification_data_parsed)); |
160 } | 158 } |
161 | 159 |
162 TEST_F(P2PNotifierTest, NotificationsBasic) { | 160 TEST_F(P2PNotifierTest, NotificationsBasic) { |
163 syncable::ModelTypeSet enabled_types; | 161 syncable::ModelEnumSet enabled_types( |
164 enabled_types.insert(syncable::BOOKMARKS); | 162 syncable::BOOKMARKS, syncable::PREFERENCES); |
165 enabled_types.insert(syncable::PREFERENCES); | |
166 | 163 |
167 EXPECT_CALL(mock_observer_, OnNotificationStateChange(true)); | 164 EXPECT_CALL(mock_observer_, OnNotificationStateChange(true)); |
168 EXPECT_CALL(mock_observer_, | 165 EXPECT_CALL(mock_observer_, |
169 OnIncomingNotification(MakePayloadMap(enabled_types))); | 166 OnIncomingNotification(MakePayloadMap(enabled_types))); |
170 | 167 |
171 p2p_notifier_->SetUniqueId("sender"); | 168 p2p_notifier_->SetUniqueId("sender"); |
172 p2p_notifier_->UpdateCredentials("foo@bar.com", "fake_token"); | 169 p2p_notifier_->UpdateCredentials("foo@bar.com", "fake_token"); |
173 p2p_notifier_->UpdateEnabledTypes(enabled_types); | 170 p2p_notifier_->UpdateEnabledTypes(enabled_types); |
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 syncable::ModelEnumSet changed_types( |
178 enabled_types.insert(syncable::THEMES); | 175 syncable::THEMES, syncable::APPS); |
179 enabled_types.insert(syncable::APPS); | |
180 p2p_notifier_->SendNotification(changed_types); | 176 p2p_notifier_->SendNotification(changed_types); |
181 } | 177 } |
182 } | 178 } |
183 | 179 |
184 TEST_F(P2PNotifierTest, SendNotificationData) { | 180 TEST_F(P2PNotifierTest, SendNotificationData) { |
185 syncable::ModelTypeSet enabled_types; | 181 syncable::ModelEnumSet enabled_types( |
186 enabled_types.insert(syncable::BOOKMARKS); | 182 syncable::BOOKMARKS, syncable::PREFERENCES); |
187 enabled_types.insert(syncable::PREFERENCES); | |
188 | 183 |
189 syncable::ModelTypeSet changed_types; | 184 syncable::ModelEnumSet changed_types( |
190 changed_types.insert(syncable::THEMES); | 185 syncable::THEMES, syncable::APPS); |
191 changed_types.insert(syncable::APPS); | |
192 | 186 |
193 const syncable::ModelTypePayloadMap& changed_payload_map = | 187 const syncable::ModelTypePayloadMap& changed_payload_map = |
194 MakePayloadMap(changed_types); | 188 MakePayloadMap(changed_types); |
195 | 189 |
196 EXPECT_CALL(mock_observer_, OnNotificationStateChange(true)); | 190 EXPECT_CALL(mock_observer_, OnNotificationStateChange(true)); |
197 EXPECT_CALL(mock_observer_, | 191 EXPECT_CALL(mock_observer_, |
198 OnIncomingNotification(MakePayloadMap(enabled_types))); | 192 OnIncomingNotification(MakePayloadMap(enabled_types))); |
199 | 193 |
200 p2p_notifier_->SetUniqueId("sender"); | 194 p2p_notifier_->SetUniqueId("sender"); |
201 p2p_notifier_->UpdateCredentials("foo@bar.com", "fake_token"); | 195 p2p_notifier_->UpdateCredentials("foo@bar.com", "fake_token"); |
(...skipping 10 matching lines...) Expand all Loading... |
212 P2PNotificationData("sender", NOTIFY_SELF, changed_types)); | 206 P2PNotificationData("sender", NOTIFY_SELF, changed_types)); |
213 | 207 |
214 // Should be dropped. | 208 // Should be dropped. |
215 Mock::VerifyAndClearExpectations(&mock_observer_); | 209 Mock::VerifyAndClearExpectations(&mock_observer_); |
216 p2p_notifier_->SendNotificationDataForTest( | 210 p2p_notifier_->SendNotificationDataForTest( |
217 P2PNotificationData("sender2", NOTIFY_SELF, changed_types)); | 211 P2PNotificationData("sender2", NOTIFY_SELF, changed_types)); |
218 | 212 |
219 // Should be dropped. | 213 // Should be dropped. |
220 Mock::VerifyAndClearExpectations(&mock_observer_); | 214 Mock::VerifyAndClearExpectations(&mock_observer_); |
221 p2p_notifier_->SendNotificationDataForTest( | 215 p2p_notifier_->SendNotificationDataForTest( |
222 P2PNotificationData("sender", NOTIFY_SELF, syncable::ModelTypeSet())); | 216 P2PNotificationData("sender", NOTIFY_SELF, syncable::ModelEnumSet())); |
223 | 217 |
224 // Should be dropped. | 218 // Should be dropped. |
225 p2p_notifier_->SendNotificationDataForTest( | 219 p2p_notifier_->SendNotificationDataForTest( |
226 P2PNotificationData("sender", NOTIFY_OTHERS, changed_types)); | 220 P2PNotificationData("sender", NOTIFY_OTHERS, changed_types)); |
227 | 221 |
228 // Should be propagated. | 222 // Should be propagated. |
229 Mock::VerifyAndClearExpectations(&mock_observer_); | 223 Mock::VerifyAndClearExpectations(&mock_observer_); |
230 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map)); | 224 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map)); |
231 p2p_notifier_->SendNotificationDataForTest( | 225 p2p_notifier_->SendNotificationDataForTest( |
232 P2PNotificationData("sender2", NOTIFY_OTHERS, changed_types)); | 226 P2PNotificationData("sender2", NOTIFY_OTHERS, changed_types)); |
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("sender2", NOTIFY_OTHERS, syncable::ModelTypeSet())); | 231 P2PNotificationData("sender2", NOTIFY_OTHERS, syncable::ModelEnumSet())); |
238 | 232 |
239 // Should be propagated. | 233 // Should be propagated. |
240 Mock::VerifyAndClearExpectations(&mock_observer_); | 234 Mock::VerifyAndClearExpectations(&mock_observer_); |
241 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map)); | 235 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map)); |
242 p2p_notifier_->SendNotificationDataForTest( | 236 p2p_notifier_->SendNotificationDataForTest( |
243 P2PNotificationData("sender", NOTIFY_ALL, changed_types)); | 237 P2PNotificationData("sender", NOTIFY_ALL, changed_types)); |
244 | 238 |
245 // Should be propagated. | 239 // Should be propagated. |
246 Mock::VerifyAndClearExpectations(&mock_observer_); | 240 Mock::VerifyAndClearExpectations(&mock_observer_); |
247 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map)); | 241 EXPECT_CALL(mock_observer_, OnIncomingNotification(changed_payload_map)); |
248 p2p_notifier_->SendNotificationDataForTest( | 242 p2p_notifier_->SendNotificationDataForTest( |
249 P2PNotificationData("sender2", NOTIFY_ALL, changed_types)); | 243 P2PNotificationData("sender2", NOTIFY_ALL, changed_types)); |
250 | 244 |
251 // Should be dropped. | 245 // Should be dropped. |
252 Mock::VerifyAndClearExpectations(&mock_observer_); | 246 Mock::VerifyAndClearExpectations(&mock_observer_); |
253 p2p_notifier_->SendNotificationDataForTest( | 247 p2p_notifier_->SendNotificationDataForTest( |
254 P2PNotificationData("sender2", NOTIFY_ALL, syncable::ModelTypeSet())); | 248 P2PNotificationData("sender2", NOTIFY_ALL, syncable::ModelEnumSet())); |
255 } | 249 } |
256 | 250 |
257 } // namespace | 251 } // namespace |
258 | 252 |
259 } // namespace sync_notifier | 253 } // namespace sync_notifier |
OLD | NEW |