OLD | NEW |
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 "chrome/browser/sync/glue/chrome_sync_notification_bridge.h" | 5 #include "chrome/browser/sync/glue/chrome_sync_notification_bridge.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/memory/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/synchronization/waitable_event.h" | 10 #include "base/synchronization/waitable_event.h" |
(...skipping 30 matching lines...) Expand all Loading... |
41 : public syncer::SyncNotifierObserver { | 41 : public syncer::SyncNotifierObserver { |
42 public: | 42 public: |
43 FakeSyncNotifierObserverIO( | 43 FakeSyncNotifierObserverIO( |
44 ChromeSyncNotificationBridge* bridge, | 44 ChromeSyncNotificationBridge* bridge, |
45 const syncer::ModelTypePayloadMap& expected_payloads) | 45 const syncer::ModelTypePayloadMap& expected_payloads) |
46 : bridge_(bridge), | 46 : bridge_(bridge), |
47 received_improper_notification_(false), | 47 received_improper_notification_(false), |
48 notification_count_(0), | 48 notification_count_(0), |
49 expected_payloads_(expected_payloads) { | 49 expected_payloads_(expected_payloads) { |
50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
51 bridge_->AddObserver(this); | 51 bridge_->UpdateRegisteredIds( |
| 52 this, ModelTypeSetToObjectIdSet( |
| 53 ModelTypePayloadMapToEnumSet(expected_payloads))); |
52 } | 54 } |
53 virtual ~FakeSyncNotifierObserverIO() { | 55 virtual ~FakeSyncNotifierObserverIO() { |
54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
55 bridge_->RemoveObserver(this); | 57 bridge_->UpdateRegisteredIds(this, syncer::ObjectIdSet()); |
56 } | 58 } |
57 | 59 |
58 // SyncNotifierObserver implementation. | 60 // SyncNotifierObserver implementation. |
59 virtual void OnIncomingNotification( | 61 virtual void OnIncomingNotification( |
60 const syncer::ModelTypePayloadMap& type_payloads, | 62 const syncer::ObjectIdPayloadMap& id_payloads, |
61 syncer::IncomingNotificationSource source) OVERRIDE { | 63 syncer::IncomingNotificationSource source) OVERRIDE { |
62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
63 notification_count_++; | 65 notification_count_++; |
64 if (source != syncer::LOCAL_NOTIFICATION) { | 66 if (source != syncer::LOCAL_NOTIFICATION) { |
65 LOG(ERROR) << "Received notification with wrong source."; | 67 LOG(ERROR) << "Received notification with wrong source."; |
66 received_improper_notification_ = true; | 68 received_improper_notification_ = true; |
67 } | 69 } |
68 if (expected_payloads_ != type_payloads) { | 70 const syncer::ModelTypePayloadMap& actual_payloads = |
| 71 syncer::ObjectIdPayloadMapToModelTypePayloadMap(id_payloads); |
| 72 if (expected_payloads_ != actual_payloads) { |
69 LOG(ERROR) << "Received wrong payload."; | 73 LOG(ERROR) << "Received wrong payload."; |
70 received_improper_notification_ = true; | 74 received_improper_notification_ = true; |
71 } | 75 } |
72 } | 76 } |
73 virtual void OnNotificationsEnabled() OVERRIDE { | 77 virtual void OnNotificationsEnabled() OVERRIDE { |
74 NOTREACHED(); | 78 NOTREACHED(); |
75 } | 79 } |
76 virtual void OnNotificationsDisabled( | 80 virtual void OnNotificationsDisabled( |
77 syncer::NotificationsDisabledReason reason) OVERRIDE { | 81 syncer::NotificationsDisabledReason reason) OVERRIDE { |
78 NOTREACHED(); | 82 NOTREACHED(); |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 // Created/used/destroyed on I/O thread. | 187 // Created/used/destroyed on I/O thread. |
184 FakeSyncNotifierObserverIO* io_observer_; | 188 FakeSyncNotifierObserverIO* io_observer_; |
185 bool io_observer_notification_failure_; | 189 bool io_observer_notification_failure_; |
186 ChromeSyncNotificationBridge bridge_; | 190 ChromeSyncNotificationBridge bridge_; |
187 base::WaitableEvent done_; | 191 base::WaitableEvent done_; |
188 }; | 192 }; |
189 | 193 |
190 // Adds an observer on the UI thread, triggers a local refresh notification, and | 194 // Adds an observer on the UI thread, triggers a local refresh notification, and |
191 // ensures the bridge posts a LOCAL_NOTIFICATION with the proper payload to it. | 195 // ensures the bridge posts a LOCAL_NOTIFICATION with the proper payload to it. |
192 TEST_F(ChromeSyncNotificationBridgeTest, LocalNotification) { | 196 TEST_F(ChromeSyncNotificationBridgeTest, LocalNotification) { |
193 syncer::ModelTypePayloadMap payload_map; | 197 syncer::ModelTypeSet models(syncer::SESSIONS); |
194 payload_map[syncer::SESSIONS] = ""; | 198 syncer::ModelTypePayloadMap payload_map = |
| 199 ModelTypePayloadMapFromEnumSet(models, "payload"); |
195 StrictMock<syncer::MockSyncNotifierObserver> observer; | 200 StrictMock<syncer::MockSyncNotifierObserver> observer; |
196 EXPECT_CALL(observer, | 201 EXPECT_CALL(observer, OnIncomingNotification( |
197 OnIncomingNotification(payload_map, | 202 ModelTypePayloadMapToObjectIdPayloadMap(payload_map), |
198 syncer::LOCAL_NOTIFICATION)); | 203 syncer::LOCAL_NOTIFICATION)); |
199 bridge_.AddObserver(&observer); | 204 bridge_.UpdateRegisteredIds(&observer, ModelTypeSetToObjectIdSet(models)); |
200 TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_LOCAL, | 205 TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_LOCAL, |
201 payload_map); | 206 payload_map); |
202 ui_loop_.RunAllPending(); | 207 ui_loop_.RunAllPending(); |
203 Mock::VerifyAndClearExpectations(&observer); | 208 Mock::VerifyAndClearExpectations(&observer); |
204 } | 209 } |
205 | 210 |
206 // Adds an observer on the UI thread, triggers a remote refresh notification, | 211 // Adds an observer on the UI thread, triggers a remote refresh notification, |
207 // and ensures the bridge posts a REMOTE_NOTIFICATION with the proper payload | 212 // and ensures the bridge posts a REMOTE_NOTIFICATION with the proper payload |
208 // to it. | 213 // to it. |
209 TEST_F(ChromeSyncNotificationBridgeTest, RemoteNotification) { | 214 TEST_F(ChromeSyncNotificationBridgeTest, RemoteNotification) { |
210 syncer::ModelTypePayloadMap payload_map; | 215 syncer::ModelTypeSet models(syncer::BOOKMARKS); |
211 payload_map[syncer::BOOKMARKS] = ""; | 216 syncer::ModelTypePayloadMap payload_map = |
| 217 ModelTypePayloadMapFromEnumSet(models, "payload"); |
212 StrictMock<syncer::MockSyncNotifierObserver> observer; | 218 StrictMock<syncer::MockSyncNotifierObserver> observer; |
213 EXPECT_CALL(observer, | 219 EXPECT_CALL(observer, OnIncomingNotification( |
214 OnIncomingNotification(payload_map, | 220 ModelTypePayloadMapToObjectIdPayloadMap(payload_map), |
215 syncer::REMOTE_NOTIFICATION)); | 221 syncer::REMOTE_NOTIFICATION)); |
216 bridge_.AddObserver(&observer); | 222 bridge_.UpdateRegisteredIds(&observer, ModelTypeSetToObjectIdSet(models)); |
217 TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_REMOTE, | 223 TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_REMOTE, |
218 payload_map); | 224 payload_map); |
219 ui_loop_.RunAllPending(); | 225 ui_loop_.RunAllPending(); |
220 Mock::VerifyAndClearExpectations(&observer); | 226 Mock::VerifyAndClearExpectations(&observer); |
221 } | 227 } |
222 | 228 |
223 // Adds an observer on the UI thread, triggers a local refresh notification | 229 // Adds an observer on the UI thread, triggers a local refresh notification |
224 // with empty payload map and ensures the bridge posts a | 230 // with empty payload map and ensures the bridge posts a |
225 // LOCAL_NOTIFICATION with the proper payload to it. | 231 // LOCAL_NOTIFICATION with the proper payload to it. |
226 TEST_F(ChromeSyncNotificationBridgeTest, LocalNotificationEmptyPayloadMap) { | 232 TEST_F(ChromeSyncNotificationBridgeTest, LocalNotificationEmptyPayloadMap) { |
227 const syncer::ModelTypeSet enabled_types( | 233 const syncer::ModelTypeSet enabled_types( |
228 syncer::BOOKMARKS, syncer::PASSWORDS); | 234 syncer::BOOKMARKS, syncer::PASSWORDS); |
229 const syncer::ModelTypePayloadMap enabled_types_payload_map = | 235 const syncer::ModelTypePayloadMap enabled_types_payload_map = |
230 syncer::ModelTypePayloadMapFromEnumSet(enabled_types, std::string()); | 236 syncer::ModelTypePayloadMapFromEnumSet(enabled_types, std::string()); |
231 | 237 |
232 StrictMock<syncer::MockSyncNotifierObserver> observer; | 238 StrictMock<syncer::MockSyncNotifierObserver> observer; |
233 EXPECT_CALL(observer, | 239 EXPECT_CALL(observer, OnIncomingNotification( |
234 OnIncomingNotification(enabled_types_payload_map, | 240 ModelTypePayloadMapToObjectIdPayloadMap(enabled_types_payload_map), |
235 syncer::LOCAL_NOTIFICATION)); | 241 syncer::LOCAL_NOTIFICATION)); |
236 bridge_.AddObserver(&observer); | 242 bridge_.UpdateRegisteredIds(&observer, |
| 243 ModelTypeSetToObjectIdSet(enabled_types)); |
237 // Set enabled types on the bridge. | 244 // Set enabled types on the bridge. |
238 bridge_.UpdateEnabledTypes(enabled_types); | 245 bridge_.UpdateEnabledTypes(enabled_types); |
239 TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_LOCAL, | 246 TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_LOCAL, |
240 syncer::ModelTypePayloadMap()); | 247 syncer::ModelTypePayloadMap()); |
241 ui_loop_.RunAllPending(); | 248 ui_loop_.RunAllPending(); |
242 Mock::VerifyAndClearExpectations(&observer); | 249 Mock::VerifyAndClearExpectations(&observer); |
243 } | 250 } |
244 | 251 |
245 // Adds an observer on the UI thread, triggers a remote refresh notification | 252 // Adds an observer on the UI thread, triggers a remote refresh notification |
246 // with empty payload map and ensures the bridge posts a | 253 // with empty payload map and ensures the bridge posts a |
247 // REMOTE_NOTIFICATION with the proper payload to it. | 254 // REMOTE_NOTIFICATION with the proper payload to it. |
248 TEST_F(ChromeSyncNotificationBridgeTest, RemoteNotificationEmptyPayloadMap) { | 255 TEST_F(ChromeSyncNotificationBridgeTest, RemoteNotificationEmptyPayloadMap) { |
249 const syncer::ModelTypeSet enabled_types( | 256 const syncer::ModelTypeSet enabled_types( |
250 syncer::BOOKMARKS, syncer::TYPED_URLS); | 257 syncer::BOOKMARKS, syncer::TYPED_URLS); |
251 const syncer::ModelTypePayloadMap enabled_types_payload_map = | 258 const syncer::ModelTypePayloadMap enabled_types_payload_map = |
252 syncer::ModelTypePayloadMapFromEnumSet(enabled_types, std::string()); | 259 syncer::ModelTypePayloadMapFromEnumSet(enabled_types, std::string()); |
253 | 260 |
254 StrictMock<syncer::MockSyncNotifierObserver> observer; | 261 StrictMock<syncer::MockSyncNotifierObserver> observer; |
255 EXPECT_CALL(observer, | 262 EXPECT_CALL(observer, OnIncomingNotification( |
256 OnIncomingNotification(enabled_types_payload_map, | 263 ModelTypePayloadMapToObjectIdPayloadMap(enabled_types_payload_map), |
257 syncer::REMOTE_NOTIFICATION)); | 264 syncer::REMOTE_NOTIFICATION)); |
258 bridge_.AddObserver(&observer); | 265 bridge_.UpdateRegisteredIds(&observer, |
| 266 ModelTypeSetToObjectIdSet(enabled_types)); |
259 // Set enabled types on the bridge. | 267 // Set enabled types on the bridge. |
260 bridge_.UpdateEnabledTypes(enabled_types); | 268 bridge_.UpdateEnabledTypes(enabled_types); |
261 TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_REMOTE, | 269 TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_REMOTE, |
262 syncer::ModelTypePayloadMap()); | 270 syncer::ModelTypePayloadMap()); |
263 ui_loop_.RunAllPending(); | 271 ui_loop_.RunAllPending(); |
264 Mock::VerifyAndClearExpectations(&observer); | 272 Mock::VerifyAndClearExpectations(&observer); |
265 } | 273 } |
266 | 274 |
267 // Adds an observer on the I/O thread. Then triggers a refresh notification on | 275 // Adds an observer on the I/O thread. Then triggers a refresh notification on |
268 // the UI thread. We finally verify the proper notification was received by the | 276 // the UI thread. We finally verify the proper notification was received by the |
269 // observer and destroy it on the I/O thread. | 277 // observer and destroy it on the I/O thread. |
270 TEST_F(ChromeSyncNotificationBridgeTest, BasicThreaded) { | 278 TEST_F(ChromeSyncNotificationBridgeTest, BasicThreaded) { |
271 syncer::ModelTypePayloadMap payload_map; | 279 syncer::ModelTypePayloadMap payload_map; |
272 payload_map[syncer::SESSIONS] = ""; | 280 payload_map[syncer::SESSIONS] = ""; |
273 CreateObserverWithExpectedPayload(payload_map); | 281 CreateObserverWithExpectedPayload(payload_map); |
274 TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_LOCAL, | 282 TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_LOCAL, |
275 payload_map); | 283 payload_map); |
276 VerifyAndDestroyObserver(); | 284 VerifyAndDestroyObserver(); |
277 } | 285 } |
278 | 286 |
279 } // namespace | 287 } // namespace |
280 } // namespace browser_sync | 288 } // namespace browser_sync |
OLD | NEW |