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/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
(...skipping 26 matching lines...) Expand all Loading... |
37 // if the observer has received a notification with the proper source and | 37 // if the observer has received a notification with the proper source and |
38 // payload. | 38 // payload. |
39 // Note: Because this object lives on the sync thread, we use a fake | 39 // Note: Because this object lives on the sync thread, we use a fake |
40 // (vs a mock) so we don't have to worry about possible thread safety | 40 // (vs a mock) so we don't have to worry about possible thread safety |
41 // issues within GTest/GMock. | 41 // issues within GTest/GMock. |
42 class FakeSyncNotifierObserver : public syncer::SyncNotifierObserver { | 42 class FakeSyncNotifierObserver : public syncer::SyncNotifierObserver { |
43 public: | 43 public: |
44 FakeSyncNotifierObserver( | 44 FakeSyncNotifierObserver( |
45 const scoped_refptr<base::SequencedTaskRunner>& sync_task_runner, | 45 const scoped_refptr<base::SequencedTaskRunner>& sync_task_runner, |
46 ChromeSyncNotificationBridge* bridge, | 46 ChromeSyncNotificationBridge* bridge, |
47 const syncer::ModelTypePayloadMap& expected_payloads, | 47 const syncer::ObjectIdPayloadMap& expected_payloads, |
48 syncer::IncomingNotificationSource expected_source) | 48 syncer::IncomingNotificationSource expected_source) |
49 : sync_task_runner_(sync_task_runner), | 49 : sync_task_runner_(sync_task_runner), |
50 bridge_(bridge), | 50 bridge_(bridge), |
51 received_improper_notification_(false), | 51 received_improper_notification_(false), |
52 notification_count_(0), | 52 notification_count_(0), |
53 expected_payloads_(expected_payloads), | 53 expected_payloads_(expected_payloads), |
54 expected_source_(expected_source) { | 54 expected_source_(expected_source) { |
55 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | 55 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
56 bridge_->AddObserver(this); | 56 // TODO(dcheng): We might want a function to go from ObjectIdPayloadMap -> |
| 57 // ObjectIdSet to avoid this rather long incantation... |
| 58 const syncer::ObjectIdSet& ids = syncer::ModelTypeSetToObjectIdSet( |
| 59 syncer::ModelTypePayloadMapToEnumSet( |
| 60 syncer::ObjectIdPayloadMapToModelTypePayloadMap( |
| 61 expected_payloads))); |
| 62 bridge_->UpdateRegisteredIds(this, ids); |
57 } | 63 } |
58 | 64 |
59 virtual ~FakeSyncNotifierObserver() { | 65 virtual ~FakeSyncNotifierObserver() { |
60 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | 66 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
61 bridge_->RemoveObserver(this); | 67 bridge_->UpdateRegisteredIds(this, syncer::ObjectIdSet()); |
62 } | 68 } |
63 | 69 |
64 // SyncNotifierObserver implementation. | 70 // SyncNotifierObserver implementation. |
65 virtual void OnIncomingNotification( | 71 virtual void OnIncomingNotification( |
66 const syncer::ModelTypePayloadMap& type_payloads, | 72 const syncer::ObjectIdPayloadMap& id_payloads, |
67 syncer::IncomingNotificationSource source) OVERRIDE { | 73 syncer::IncomingNotificationSource source) OVERRIDE { |
68 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | 74 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
69 notification_count_++; | 75 notification_count_++; |
70 if (source != expected_source_) { | 76 if (source != expected_source_) { |
71 LOG(ERROR) << "Received notification with wrong source"; | 77 LOG(ERROR) << "Received notification with wrong source"; |
72 received_improper_notification_ = true; | 78 received_improper_notification_ = true; |
73 } | 79 } |
74 if (expected_payloads_ != type_payloads) { | 80 if (expected_payloads_ != id_payloads) { |
75 LOG(ERROR) << "Received wrong payload"; | 81 LOG(ERROR) << "Received wrong payload"; |
76 received_improper_notification_ = true; | 82 received_improper_notification_ = true; |
77 } | 83 } |
78 } | 84 } |
79 virtual void OnNotificationsEnabled() OVERRIDE { | 85 virtual void OnNotificationsEnabled() OVERRIDE { |
80 NOTREACHED(); | 86 NOTREACHED(); |
81 } | 87 } |
82 virtual void OnNotificationsDisabled( | 88 virtual void OnNotificationsDisabled( |
83 syncer::NotificationsDisabledReason reason) OVERRIDE { | 89 syncer::NotificationsDisabledReason reason) OVERRIDE { |
84 NOTREACHED(); | 90 NOTREACHED(); |
85 } | 91 } |
86 | 92 |
87 bool ReceivedProperNotification() const { | 93 bool ReceivedProperNotification() const { |
88 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | 94 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
89 return (notification_count_ == 1) && !received_improper_notification_; | 95 return (notification_count_ == 1) && !received_improper_notification_; |
90 } | 96 } |
91 | 97 |
92 private: | 98 private: |
93 const scoped_refptr<base::SequencedTaskRunner> sync_task_runner_; | 99 const scoped_refptr<base::SequencedTaskRunner> sync_task_runner_; |
94 ChromeSyncNotificationBridge* const bridge_; | 100 ChromeSyncNotificationBridge* const bridge_; |
95 bool received_improper_notification_; | 101 bool received_improper_notification_; |
96 size_t notification_count_; | 102 size_t notification_count_; |
97 const syncer::ModelTypePayloadMap expected_payloads_; | 103 const syncer::ObjectIdPayloadMap expected_payloads_; |
98 const syncer::IncomingNotificationSource expected_source_; | 104 const syncer::IncomingNotificationSource expected_source_; |
99 }; | 105 }; |
100 | 106 |
101 class ChromeSyncNotificationBridgeTest : public testing::Test { | 107 class ChromeSyncNotificationBridgeTest : public testing::Test { |
102 public: | 108 public: |
103 ChromeSyncNotificationBridgeTest() | 109 ChromeSyncNotificationBridgeTest() |
104 : ui_thread_(BrowserThread::UI), | 110 : ui_thread_(BrowserThread::UI), |
105 sync_thread_("Sync thread"), | 111 sync_thread_("Sync thread"), |
106 sync_observer_(NULL), | 112 sync_observer_(NULL), |
107 sync_observer_notification_failure_(false), | 113 sync_observer_notification_failure_(false), |
(...skipping 21 matching lines...) Expand all Loading... |
129 void VerifyAndDestroyObserver() { | 135 void VerifyAndDestroyObserver() { |
130 ASSERT_TRUE(sync_thread_.message_loop_proxy()->PostTask( | 136 ASSERT_TRUE(sync_thread_.message_loop_proxy()->PostTask( |
131 FROM_HERE, | 137 FROM_HERE, |
132 base::Bind(&ChromeSyncNotificationBridgeTest:: | 138 base::Bind(&ChromeSyncNotificationBridgeTest:: |
133 VerifyAndDestroyObserverOnSyncThread, | 139 VerifyAndDestroyObserverOnSyncThread, |
134 base::Unretained(this)))); | 140 base::Unretained(this)))); |
135 BlockForSyncThread(); | 141 BlockForSyncThread(); |
136 } | 142 } |
137 | 143 |
138 void CreateObserverWithExpectations( | 144 void CreateObserverWithExpectations( |
139 syncer::ModelTypePayloadMap expected_payloads, | 145 const syncer::ModelTypePayloadMap& expected_payloads, |
140 syncer::IncomingNotificationSource expected_source) { | 146 syncer::IncomingNotificationSource expected_source) { |
| 147 const syncer::ObjectIdPayloadMap& expected_id_payloads = |
| 148 syncer::ModelTypePayloadMapToObjectIdPayloadMap(expected_payloads); |
141 ASSERT_TRUE(sync_thread_.message_loop_proxy()->PostTask( | 149 ASSERT_TRUE(sync_thread_.message_loop_proxy()->PostTask( |
142 FROM_HERE, | 150 FROM_HERE, |
143 base::Bind( | 151 base::Bind( |
144 &ChromeSyncNotificationBridgeTest::CreateObserverOnSyncThread, | 152 &ChromeSyncNotificationBridgeTest::CreateObserverOnSyncThread, |
145 base::Unretained(this), | 153 base::Unretained(this), |
146 expected_payloads, | 154 expected_id_payloads, |
147 expected_source))); | 155 expected_source))); |
148 BlockForSyncThread(); | 156 BlockForSyncThread(); |
149 } | 157 } |
150 | 158 |
151 void UpdateBridgeEnabledTypes(syncer::ModelTypeSet enabled_types) { | 159 void UpdateBridgeEnabledTypes(syncer::ModelTypeSet enabled_types) { |
152 ASSERT_TRUE(sync_thread_.message_loop_proxy()->PostTask( | 160 ASSERT_TRUE(sync_thread_.message_loop_proxy()->PostTask( |
153 FROM_HERE, | 161 FROM_HERE, |
154 base::Bind( | 162 base::Bind( |
155 &ChromeSyncNotificationBridgeTest:: | 163 &ChromeSyncNotificationBridgeTest:: |
156 UpdateBridgeEnabledTypesOnSyncThread, | 164 UpdateBridgeEnabledTypesOnSyncThread, |
(...skipping 18 matching lines...) Expand all Loading... |
175 sync_observer_notification_failure_ = true; | 183 sync_observer_notification_failure_ = true; |
176 } else { | 184 } else { |
177 sync_observer_notification_failure_ = | 185 sync_observer_notification_failure_ = |
178 !sync_observer_->ReceivedProperNotification(); | 186 !sync_observer_->ReceivedProperNotification(); |
179 delete sync_observer_; | 187 delete sync_observer_; |
180 sync_observer_ = NULL; | 188 sync_observer_ = NULL; |
181 } | 189 } |
182 } | 190 } |
183 | 191 |
184 void CreateObserverOnSyncThread( | 192 void CreateObserverOnSyncThread( |
185 syncer::ModelTypePayloadMap expected_payloads, | 193 const syncer::ObjectIdPayloadMap& expected_payloads, |
186 syncer::IncomingNotificationSource expected_source) { | 194 syncer::IncomingNotificationSource expected_source) { |
187 DCHECK(sync_thread_.message_loop_proxy()->RunsTasksOnCurrentThread()); | 195 DCHECK(sync_thread_.message_loop_proxy()->RunsTasksOnCurrentThread()); |
188 sync_observer_ = new FakeSyncNotifierObserver( | 196 sync_observer_ = new FakeSyncNotifierObserver( |
189 sync_thread_.message_loop_proxy(), | 197 sync_thread_.message_loop_proxy(), |
190 bridge_.get(), | 198 bridge_.get(), |
191 expected_payloads, | 199 expected_payloads, |
192 expected_source); | 200 expected_source); |
193 } | 201 } |
194 | 202 |
195 void UpdateBridgeEnabledTypesOnSyncThread( | 203 void UpdateBridgeEnabledTypesOnSyncThread( |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 CreateObserverWithExpectations( | 283 CreateObserverWithExpectations( |
276 enabled_types_payload_map, syncer::REMOTE_NOTIFICATION); | 284 enabled_types_payload_map, syncer::REMOTE_NOTIFICATION); |
277 UpdateBridgeEnabledTypes(enabled_types); | 285 UpdateBridgeEnabledTypes(enabled_types); |
278 TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_REMOTE, | 286 TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_REMOTE, |
279 syncer::ModelTypePayloadMap()); | 287 syncer::ModelTypePayloadMap()); |
280 VerifyAndDestroyObserver(); | 288 VerifyAndDestroyObserver(); |
281 } | 289 } |
282 | 290 |
283 } // namespace | 291 } // namespace |
284 } // namespace browser_sync | 292 } // namespace browser_sync |
OLD | NEW |