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 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 | 119 |
120 virtual void TearDown() OVERRIDE { | 120 virtual void TearDown() OVERRIDE { |
121 sync_thread_.Stop(); | 121 sync_thread_.Stop(); |
122 // Must be reset only after the sync thread is stopped. | 122 // Must be reset only after the sync thread is stopped. |
123 bridge_.reset(); | 123 bridge_.reset(); |
124 EXPECT_EQ(NULL, sync_observer_); | 124 EXPECT_EQ(NULL, sync_observer_); |
125 if (sync_observer_notification_failure_) | 125 if (sync_observer_notification_failure_) |
126 ADD_FAILURE() << "Sync Observer did not receive proper notification."; | 126 ADD_FAILURE() << "Sync Observer did not receive proper notification."; |
127 } | 127 } |
128 | 128 |
| 129 void VerifyAndDestroyObserver() { |
| 130 ASSERT_TRUE(sync_thread_.message_loop_proxy()->PostTask( |
| 131 FROM_HERE, |
| 132 base::Bind(&ChromeSyncNotificationBridgeTest:: |
| 133 VerifyAndDestroyObserverOnSyncThread, |
| 134 base::Unretained(this)))); |
| 135 BlockForSyncThread(); |
| 136 } |
| 137 |
| 138 void CreateObserverWithExpectations( |
| 139 syncer::ModelTypePayloadMap expected_payloads, |
| 140 syncer::IncomingNotificationSource expected_source) { |
| 141 ASSERT_TRUE(sync_thread_.message_loop_proxy()->PostTask( |
| 142 FROM_HERE, |
| 143 base::Bind( |
| 144 &ChromeSyncNotificationBridgeTest::CreateObserverOnSyncThread, |
| 145 base::Unretained(this), |
| 146 expected_payloads, |
| 147 expected_source))); |
| 148 BlockForSyncThread(); |
| 149 } |
| 150 |
| 151 void UpdateBridgeEnabledTypes(syncer::ModelTypeSet enabled_types) { |
| 152 ASSERT_TRUE(sync_thread_.message_loop_proxy()->PostTask( |
| 153 FROM_HERE, |
| 154 base::Bind( |
| 155 &ChromeSyncNotificationBridgeTest:: |
| 156 UpdateBridgeEnabledTypesOnSyncThread, |
| 157 base::Unretained(this), |
| 158 enabled_types))); |
| 159 BlockForSyncThread(); |
| 160 } |
| 161 |
| 162 void TriggerRefreshNotification( |
| 163 int type, |
| 164 const syncer::ModelTypePayloadMap& payload_map) { |
| 165 content::NotificationService::current()->Notify( |
| 166 type, |
| 167 content::Source<Profile>(&mock_profile_), |
| 168 content::Details<const syncer::ModelTypePayloadMap>(&payload_map)); |
| 169 } |
| 170 |
| 171 private: |
129 void VerifyAndDestroyObserverOnSyncThread() { | 172 void VerifyAndDestroyObserverOnSyncThread() { |
130 DCHECK(sync_thread_.message_loop_proxy()->RunsTasksOnCurrentThread()); | 173 DCHECK(sync_thread_.message_loop_proxy()->RunsTasksOnCurrentThread()); |
131 if (!sync_observer_) { | 174 if (!sync_observer_) { |
132 sync_observer_notification_failure_ = true; | 175 sync_observer_notification_failure_ = true; |
133 } else { | 176 } else { |
134 sync_observer_notification_failure_ = | 177 sync_observer_notification_failure_ = |
135 !sync_observer_->ReceivedProperNotification(); | 178 !sync_observer_->ReceivedProperNotification(); |
136 delete sync_observer_; | 179 delete sync_observer_; |
137 sync_observer_ = NULL; | 180 sync_observer_ = NULL; |
138 } | 181 } |
139 } | 182 } |
140 | 183 |
141 void VerifyAndDestroyObserver() { | |
142 ASSERT_TRUE(sync_thread_.message_loop_proxy()->PostTask( | |
143 FROM_HERE, | |
144 base::Bind(&ChromeSyncNotificationBridgeTest:: | |
145 VerifyAndDestroyObserverOnSyncThread, | |
146 base::Unretained(this)))); | |
147 BlockForSyncThread(); | |
148 } | |
149 | |
150 void CreateObserverOnSyncThread( | 184 void CreateObserverOnSyncThread( |
151 syncer::ModelTypePayloadMap expected_payloads, | 185 syncer::ModelTypePayloadMap expected_payloads, |
152 syncer::IncomingNotificationSource expected_source) { | 186 syncer::IncomingNotificationSource expected_source) { |
153 DCHECK(sync_thread_.message_loop_proxy()->RunsTasksOnCurrentThread()); | 187 DCHECK(sync_thread_.message_loop_proxy()->RunsTasksOnCurrentThread()); |
154 sync_observer_ = new FakeSyncNotifierObserver( | 188 sync_observer_ = new FakeSyncNotifierObserver( |
155 sync_thread_.message_loop_proxy(), | 189 sync_thread_.message_loop_proxy(), |
156 bridge_.get(), | 190 bridge_.get(), |
157 expected_payloads, | 191 expected_payloads, |
158 expected_source); | 192 expected_source); |
159 } | 193 } |
160 | 194 |
161 void CreateObserverWithExpectations( | 195 void UpdateBridgeEnabledTypesOnSyncThread( |
162 syncer::ModelTypePayloadMap expected_payloads, | 196 syncer::ModelTypeSet enabled_types) { |
163 syncer::IncomingNotificationSource expected_source) { | 197 DCHECK(sync_thread_.message_loop_proxy()->RunsTasksOnCurrentThread()); |
164 ASSERT_TRUE(sync_thread_.message_loop_proxy()->PostTask( | 198 bridge_->UpdateEnabledTypes(enabled_types); |
165 FROM_HERE, | |
166 base::Bind( | |
167 &ChromeSyncNotificationBridgeTest::CreateObserverOnSyncThread, | |
168 base::Unretained(this), | |
169 expected_payloads, | |
170 expected_source))); | |
171 BlockForSyncThread(); | |
172 } | 199 } |
173 | 200 |
174 void SignalOnSyncThread() { | 201 void SignalOnSyncThread() { |
175 DCHECK(sync_thread_.message_loop_proxy()->RunsTasksOnCurrentThread()); | 202 DCHECK(sync_thread_.message_loop_proxy()->RunsTasksOnCurrentThread()); |
176 done_.Signal(); | 203 done_.Signal(); |
177 } | 204 } |
178 | 205 |
179 void BlockForSyncThread() { | 206 void BlockForSyncThread() { |
180 done_.Reset(); | 207 done_.Reset(); |
181 ASSERT_TRUE(sync_thread_.message_loop_proxy()->PostTask( | 208 ASSERT_TRUE(sync_thread_.message_loop_proxy()->PostTask( |
182 FROM_HERE, | 209 FROM_HERE, |
183 base::Bind(&ChromeSyncNotificationBridgeTest::SignalOnSyncThread, | 210 base::Bind(&ChromeSyncNotificationBridgeTest::SignalOnSyncThread, |
184 base::Unretained(this)))); | 211 base::Unretained(this)))); |
185 done_.TimedWait(TestTimeouts::action_timeout()); | 212 done_.TimedWait(TestTimeouts::action_timeout()); |
186 if (!done_.IsSignaled()) | 213 if (!done_.IsSignaled()) |
187 ADD_FAILURE() << "Timed out waiting for IO thread."; | 214 ADD_FAILURE() << "Timed out waiting for sync thread."; |
188 } | 215 } |
189 | 216 |
190 void TriggerRefreshNotification( | |
191 int type, | |
192 const syncer::ModelTypePayloadMap& payload_map) { | |
193 content::NotificationService::current()->Notify( | |
194 type, | |
195 content::Source<Profile>(&mock_profile_), | |
196 content::Details<const syncer::ModelTypePayloadMap>(&payload_map)); | |
197 } | |
198 | |
199 private: | |
200 content::TestBrowserThread ui_thread_; | 217 content::TestBrowserThread ui_thread_; |
201 base::Thread sync_thread_; | 218 base::Thread sync_thread_; |
202 NiceMock<ProfileMock> mock_profile_; | 219 NiceMock<ProfileMock> mock_profile_; |
203 // Created/used/destroyed on sync thread. | 220 // Created/used/destroyed on sync thread. |
204 FakeSyncNotifierObserver* sync_observer_; | 221 FakeSyncNotifierObserver* sync_observer_; |
205 bool sync_observer_notification_failure_; | 222 bool sync_observer_notification_failure_; |
206 scoped_ptr<ChromeSyncNotificationBridge> bridge_; | 223 scoped_ptr<ChromeSyncNotificationBridge> bridge_; |
207 base::WaitableEvent done_; | 224 base::WaitableEvent done_; |
208 }; | 225 }; |
209 | 226 |
(...skipping 24 matching lines...) Expand all Loading... |
234 // Adds an observer on the sync thread, triggers a local refresh | 251 // Adds an observer on the sync thread, triggers a local refresh |
235 // notification with empty payload map and ensures the bridge posts a | 252 // notification with empty payload map and ensures the bridge posts a |
236 // LOCAL_NOTIFICATION with the proper payload to it. | 253 // LOCAL_NOTIFICATION with the proper payload to it. |
237 TEST_F(ChromeSyncNotificationBridgeTest, LocalNotificationEmptyPayloadMap) { | 254 TEST_F(ChromeSyncNotificationBridgeTest, LocalNotificationEmptyPayloadMap) { |
238 const syncer::ModelTypeSet enabled_types( | 255 const syncer::ModelTypeSet enabled_types( |
239 syncer::BOOKMARKS, syncer::PASSWORDS); | 256 syncer::BOOKMARKS, syncer::PASSWORDS); |
240 const syncer::ModelTypePayloadMap enabled_types_payload_map = | 257 const syncer::ModelTypePayloadMap enabled_types_payload_map = |
241 syncer::ModelTypePayloadMapFromEnumSet(enabled_types, std::string()); | 258 syncer::ModelTypePayloadMapFromEnumSet(enabled_types, std::string()); |
242 CreateObserverWithExpectations( | 259 CreateObserverWithExpectations( |
243 enabled_types_payload_map, syncer::LOCAL_NOTIFICATION); | 260 enabled_types_payload_map, syncer::LOCAL_NOTIFICATION); |
| 261 UpdateBridgeEnabledTypes(enabled_types); |
244 TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_LOCAL, | 262 TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_LOCAL, |
245 enabled_types_payload_map); | 263 syncer::ModelTypePayloadMap()); |
246 VerifyAndDestroyObserver(); | 264 VerifyAndDestroyObserver(); |
247 } | 265 } |
248 | 266 |
249 // Adds an observer on the sync thread, triggers a remote refresh | 267 // Adds an observer on the sync thread, triggers a remote refresh |
250 // notification with empty payload map and ensures the bridge posts a | 268 // notification with empty payload map and ensures the bridge posts a |
251 // REMOTE_NOTIFICATION with the proper payload to it. | 269 // REMOTE_NOTIFICATION with the proper payload to it. |
252 TEST_F(ChromeSyncNotificationBridgeTest, RemoteNotificationEmptyPayloadMap) { | 270 TEST_F(ChromeSyncNotificationBridgeTest, RemoteNotificationEmptyPayloadMap) { |
253 const syncer::ModelTypeSet enabled_types( | 271 const syncer::ModelTypeSet enabled_types( |
254 syncer::BOOKMARKS, syncer::TYPED_URLS); | 272 syncer::BOOKMARKS, syncer::TYPED_URLS); |
255 const syncer::ModelTypePayloadMap enabled_types_payload_map = | 273 const syncer::ModelTypePayloadMap enabled_types_payload_map = |
256 syncer::ModelTypePayloadMapFromEnumSet(enabled_types, std::string()); | 274 syncer::ModelTypePayloadMapFromEnumSet(enabled_types, std::string()); |
257 CreateObserverWithExpectations( | 275 CreateObserverWithExpectations( |
258 enabled_types_payload_map, syncer::REMOTE_NOTIFICATION); | 276 enabled_types_payload_map, syncer::REMOTE_NOTIFICATION); |
| 277 UpdateBridgeEnabledTypes(enabled_types); |
259 TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_REMOTE, | 278 TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_REMOTE, |
260 enabled_types_payload_map); | 279 syncer::ModelTypePayloadMap()); |
261 VerifyAndDestroyObserver(); | 280 VerifyAndDestroyObserver(); |
262 } | 281 } |
263 | 282 |
264 } // namespace | 283 } // namespace |
265 } // namespace browser_sync | 284 } // namespace browser_sync |
OLD | NEW |