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

Side by Side Diff: chrome/browser/sync/glue/chrome_sync_notification_bridge_unittest.cc

Issue 10696087: [Sync] Move ModelType and related classes to 'syncer' namespace (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: sort headers, update copyrights Created 8 years, 5 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
OLDNEW
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 24 matching lines...) Expand all
35 // if the observer has received a notification with the proper source and 35 // if the observer has received a notification with the proper source and
36 // payload. 36 // payload.
37 // Note: Because this object lives on the IO thread, we use a fake (vs a mock) 37 // Note: Because this object lives on the IO thread, we use a fake (vs a mock)
38 // so we don't have to worry about possible thread safety issues within 38 // so we don't have to worry about possible thread safety issues within
39 // GTest/GMock. 39 // GTest/GMock.
40 class FakeSyncNotifierObserverIO 40 class FakeSyncNotifierObserverIO
41 : public syncer::SyncNotifierObserver { 41 : public syncer::SyncNotifierObserver {
42 public: 42 public:
43 FakeSyncNotifierObserverIO( 43 FakeSyncNotifierObserverIO(
44 ChromeSyncNotificationBridge* bridge, 44 ChromeSyncNotificationBridge* bridge,
45 const syncable::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_->AddObserver(this);
52 } 52 }
53 virtual ~FakeSyncNotifierObserverIO() { 53 virtual ~FakeSyncNotifierObserverIO() {
54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
55 bridge_->RemoveObserver(this); 55 bridge_->RemoveObserver(this);
56 } 56 }
57 57
58 // SyncNotifierObserver implementation. 58 // SyncNotifierObserver implementation.
59 virtual void OnIncomingNotification( 59 virtual void OnIncomingNotification(
60 const syncable::ModelTypePayloadMap& type_payloads, 60 const syncer::ModelTypePayloadMap& type_payloads,
61 syncer::IncomingNotificationSource source) OVERRIDE { 61 syncer::IncomingNotificationSource source) OVERRIDE {
62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
63 notification_count_++; 63 notification_count_++;
64 if (source != syncer::LOCAL_NOTIFICATION) { 64 if (source != syncer::LOCAL_NOTIFICATION) {
65 LOG(ERROR) << "Received notification with wrong source."; 65 LOG(ERROR) << "Received notification with wrong source.";
66 received_improper_notification_ = true; 66 received_improper_notification_ = true;
67 } 67 }
68 if (expected_payloads_ != type_payloads) { 68 if (expected_payloads_ != type_payloads) {
69 LOG(ERROR) << "Received wrong payload."; 69 LOG(ERROR) << "Received wrong payload.";
70 received_improper_notification_ = true; 70 received_improper_notification_ = true;
71 } 71 }
72 } 72 }
73 virtual void OnNotificationsEnabled() OVERRIDE { 73 virtual void OnNotificationsEnabled() OVERRIDE {
74 NOTREACHED(); 74 NOTREACHED();
75 } 75 }
76 virtual void OnNotificationsDisabled( 76 virtual void OnNotificationsDisabled(
77 syncer::NotificationsDisabledReason reason) OVERRIDE { 77 syncer::NotificationsDisabledReason reason) OVERRIDE {
78 NOTREACHED(); 78 NOTREACHED();
79 } 79 }
80 80
81 bool ReceivedProperNotification() const { 81 bool ReceivedProperNotification() const {
82 return (notification_count_ == 1) && !received_improper_notification_; 82 return (notification_count_ == 1) && !received_improper_notification_;
83 } 83 }
84 84
85 private: 85 private:
86 ChromeSyncNotificationBridge* bridge_; 86 ChromeSyncNotificationBridge* bridge_;
87 bool received_improper_notification_; 87 bool received_improper_notification_;
88 size_t notification_count_; 88 size_t notification_count_;
89 syncable::ModelTypePayloadMap expected_payloads_; 89 syncer::ModelTypePayloadMap expected_payloads_;
90 }; 90 };
91 91
92 class ChromeSyncNotificationBridgeTest : public testing::Test { 92 class ChromeSyncNotificationBridgeTest : public testing::Test {
93 public: 93 public:
94 ChromeSyncNotificationBridgeTest() 94 ChromeSyncNotificationBridgeTest()
95 : ui_thread_(BrowserThread::UI, &ui_loop_), 95 : ui_thread_(BrowserThread::UI, &ui_loop_),
96 io_thread_(BrowserThread::IO), 96 io_thread_(BrowserThread::IO),
97 io_observer_(NULL), 97 io_observer_(NULL),
98 io_observer_notification_failure_(false), 98 io_observer_notification_failure_(false),
99 bridge_(&mock_profile_), 99 bridge_(&mock_profile_),
(...skipping 26 matching lines...) Expand all
126 void VerifyAndDestroyObserver() { 126 void VerifyAndDestroyObserver() {
127 ASSERT_TRUE(BrowserThread::PostTask( 127 ASSERT_TRUE(BrowserThread::PostTask(
128 BrowserThread::IO, 128 BrowserThread::IO,
129 FROM_HERE, 129 FROM_HERE,
130 base::Bind(&ChromeSyncNotificationBridgeTest:: 130 base::Bind(&ChromeSyncNotificationBridgeTest::
131 VerifyAndDestroyObserverOnIOThread, 131 VerifyAndDestroyObserverOnIOThread,
132 base::Unretained(this)))); 132 base::Unretained(this))));
133 } 133 }
134 134
135 void CreateObserverOnIOThread( 135 void CreateObserverOnIOThread(
136 syncable::ModelTypePayloadMap expected_payloads) { 136 syncer::ModelTypePayloadMap expected_payloads) {
137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
138 io_observer_ = new FakeSyncNotifierObserverIO(&bridge_, 138 io_observer_ = new FakeSyncNotifierObserverIO(&bridge_,
139 expected_payloads); 139 expected_payloads);
140 } 140 }
141 141
142 void CreateObserverWithExpectedPayload( 142 void CreateObserverWithExpectedPayload(
143 syncable::ModelTypePayloadMap expected_payloads) { 143 syncer::ModelTypePayloadMap expected_payloads) {
144 ASSERT_TRUE(BrowserThread::PostTask( 144 ASSERT_TRUE(BrowserThread::PostTask(
145 BrowserThread::IO, 145 BrowserThread::IO,
146 FROM_HERE, 146 FROM_HERE,
147 base::Bind(&ChromeSyncNotificationBridgeTest::CreateObserverOnIOThread, 147 base::Bind(&ChromeSyncNotificationBridgeTest::CreateObserverOnIOThread,
148 base::Unretained(this), 148 base::Unretained(this),
149 expected_payloads))); 149 expected_payloads)));
150 BlockForIOThread(); 150 BlockForIOThread();
151 } 151 }
152 152
153 void SignalOnIOThread() { 153 void SignalOnIOThread() {
154 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 154 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
155 done_.Signal(); 155 done_.Signal();
156 } 156 }
157 157
158 void BlockForIOThread() { 158 void BlockForIOThread() {
159 done_.Reset(); 159 done_.Reset();
160 BrowserThread::PostTask( 160 BrowserThread::PostTask(
161 BrowserThread::IO, 161 BrowserThread::IO,
162 FROM_HERE, 162 FROM_HERE,
163 base::Bind(&ChromeSyncNotificationBridgeTest::SignalOnIOThread, 163 base::Bind(&ChromeSyncNotificationBridgeTest::SignalOnIOThread,
164 base::Unretained(this))); 164 base::Unretained(this)));
165 done_.TimedWait(TestTimeouts::action_timeout()); 165 done_.TimedWait(TestTimeouts::action_timeout());
166 if (!done_.IsSignaled()) 166 if (!done_.IsSignaled())
167 ADD_FAILURE() << "Timed out waiting for IO thread."; 167 ADD_FAILURE() << "Timed out waiting for IO thread.";
168 } 168 }
169 169
170 void TriggerRefreshNotification( 170 void TriggerRefreshNotification(
171 int type, 171 int type,
172 const syncable::ModelTypePayloadMap& payload_map) { 172 const syncer::ModelTypePayloadMap& payload_map) {
173 content::NotificationService::current()->Notify( 173 content::NotificationService::current()->Notify(
174 type, 174 type,
175 content::Source<Profile>(&mock_profile_), 175 content::Source<Profile>(&mock_profile_),
176 content::Details<const syncable::ModelTypePayloadMap>(&payload_map)); 176 content::Details<const syncer::ModelTypePayloadMap>(&payload_map));
177 } 177 }
178 178
179 MessageLoop ui_loop_; 179 MessageLoop ui_loop_;
180 content::TestBrowserThread ui_thread_; 180 content::TestBrowserThread ui_thread_;
181 content::TestBrowserThread io_thread_; 181 content::TestBrowserThread io_thread_;
182 NiceMock<ProfileMock> mock_profile_; 182 NiceMock<ProfileMock> mock_profile_;
183 // Created/used/destroyed on I/O thread. 183 // Created/used/destroyed on I/O thread.
184 FakeSyncNotifierObserverIO* io_observer_; 184 FakeSyncNotifierObserverIO* io_observer_;
185 bool io_observer_notification_failure_; 185 bool io_observer_notification_failure_;
186 ChromeSyncNotificationBridge bridge_; 186 ChromeSyncNotificationBridge bridge_;
187 base::WaitableEvent done_; 187 base::WaitableEvent done_;
188 }; 188 };
189 189
190 // Adds an observer on the UI thread, triggers a local refresh notification, and 190 // 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. 191 // ensures the bridge posts a LOCAL_NOTIFICATION with the proper payload to it.
192 TEST_F(ChromeSyncNotificationBridgeTest, LocalNotification) { 192 TEST_F(ChromeSyncNotificationBridgeTest, LocalNotification) {
193 syncable::ModelTypePayloadMap payload_map; 193 syncer::ModelTypePayloadMap payload_map;
194 payload_map[syncable::SESSIONS] = ""; 194 payload_map[syncer::SESSIONS] = "";
195 StrictMock<syncer::MockSyncNotifierObserver> observer; 195 StrictMock<syncer::MockSyncNotifierObserver> observer;
196 EXPECT_CALL(observer, 196 EXPECT_CALL(observer,
197 OnIncomingNotification(payload_map, 197 OnIncomingNotification(payload_map,
198 syncer::LOCAL_NOTIFICATION)); 198 syncer::LOCAL_NOTIFICATION));
199 bridge_.AddObserver(&observer); 199 bridge_.AddObserver(&observer);
200 TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_LOCAL, 200 TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_LOCAL,
201 payload_map); 201 payload_map);
202 ui_loop_.RunAllPending(); 202 ui_loop_.RunAllPending();
203 Mock::VerifyAndClearExpectations(&observer); 203 Mock::VerifyAndClearExpectations(&observer);
204 } 204 }
205 205
206 // Adds an observer on the UI thread, triggers a remote refresh notification, 206 // 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 207 // and ensures the bridge posts a REMOTE_NOTIFICATION with the proper payload
208 // to it. 208 // to it.
209 TEST_F(ChromeSyncNotificationBridgeTest, RemoteNotification) { 209 TEST_F(ChromeSyncNotificationBridgeTest, RemoteNotification) {
210 syncable::ModelTypePayloadMap payload_map; 210 syncer::ModelTypePayloadMap payload_map;
211 payload_map[syncable::BOOKMARKS] = ""; 211 payload_map[syncer::BOOKMARKS] = "";
212 StrictMock<syncer::MockSyncNotifierObserver> observer; 212 StrictMock<syncer::MockSyncNotifierObserver> observer;
213 EXPECT_CALL(observer, 213 EXPECT_CALL(observer,
214 OnIncomingNotification(payload_map, 214 OnIncomingNotification(payload_map,
215 syncer::REMOTE_NOTIFICATION)); 215 syncer::REMOTE_NOTIFICATION));
216 bridge_.AddObserver(&observer); 216 bridge_.AddObserver(&observer);
217 TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_REMOTE, 217 TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_REMOTE,
218 payload_map); 218 payload_map);
219 ui_loop_.RunAllPending(); 219 ui_loop_.RunAllPending();
220 Mock::VerifyAndClearExpectations(&observer); 220 Mock::VerifyAndClearExpectations(&observer);
221 } 221 }
222 222
223 // Adds an observer on the UI thread, triggers a local refresh notification 223 // Adds an observer on the UI thread, triggers a local refresh notification
224 // with empty payload map and ensures the bridge posts a 224 // with empty payload map and ensures the bridge posts a
225 // LOCAL_NOTIFICATION with the proper payload to it. 225 // LOCAL_NOTIFICATION with the proper payload to it.
226 TEST_F(ChromeSyncNotificationBridgeTest, LocalNotificationEmptyPayloadMap) { 226 TEST_F(ChromeSyncNotificationBridgeTest, LocalNotificationEmptyPayloadMap) {
227 const syncable::ModelTypeSet enabled_types( 227 const syncer::ModelTypeSet enabled_types(
228 syncable::BOOKMARKS, syncable::PASSWORDS); 228 syncer::BOOKMARKS, syncer::PASSWORDS);
229 const syncable::ModelTypePayloadMap enabled_types_payload_map = 229 const syncer::ModelTypePayloadMap enabled_types_payload_map =
230 syncable::ModelTypePayloadMapFromEnumSet(enabled_types, std::string()); 230 syncer::ModelTypePayloadMapFromEnumSet(enabled_types, std::string());
231 231
232 StrictMock<syncer::MockSyncNotifierObserver> observer; 232 StrictMock<syncer::MockSyncNotifierObserver> observer;
233 EXPECT_CALL(observer, 233 EXPECT_CALL(observer,
234 OnIncomingNotification(enabled_types_payload_map, 234 OnIncomingNotification(enabled_types_payload_map,
235 syncer::LOCAL_NOTIFICATION)); 235 syncer::LOCAL_NOTIFICATION));
236 bridge_.AddObserver(&observer); 236 bridge_.AddObserver(&observer);
237 // Set enabled types on the bridge. 237 // Set enabled types on the bridge.
238 bridge_.UpdateEnabledTypes(enabled_types); 238 bridge_.UpdateEnabledTypes(enabled_types);
239 TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_LOCAL, 239 TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_LOCAL,
240 syncable::ModelTypePayloadMap()); 240 syncer::ModelTypePayloadMap());
241 ui_loop_.RunAllPending(); 241 ui_loop_.RunAllPending();
242 Mock::VerifyAndClearExpectations(&observer); 242 Mock::VerifyAndClearExpectations(&observer);
243 } 243 }
244 244
245 // Adds an observer on the UI thread, triggers a remote refresh notification 245 // Adds an observer on the UI thread, triggers a remote refresh notification
246 // with empty payload map and ensures the bridge posts a 246 // with empty payload map and ensures the bridge posts a
247 // REMOTE_NOTIFICATION with the proper payload to it. 247 // REMOTE_NOTIFICATION with the proper payload to it.
248 TEST_F(ChromeSyncNotificationBridgeTest, RemoteNotificationEmptyPayloadMap) { 248 TEST_F(ChromeSyncNotificationBridgeTest, RemoteNotificationEmptyPayloadMap) {
249 const syncable::ModelTypeSet enabled_types( 249 const syncer::ModelTypeSet enabled_types(
250 syncable::BOOKMARKS, syncable::TYPED_URLS); 250 syncer::BOOKMARKS, syncer::TYPED_URLS);
251 const syncable::ModelTypePayloadMap enabled_types_payload_map = 251 const syncer::ModelTypePayloadMap enabled_types_payload_map =
252 syncable::ModelTypePayloadMapFromEnumSet(enabled_types, std::string()); 252 syncer::ModelTypePayloadMapFromEnumSet(enabled_types, std::string());
253 253
254 StrictMock<syncer::MockSyncNotifierObserver> observer; 254 StrictMock<syncer::MockSyncNotifierObserver> observer;
255 EXPECT_CALL(observer, 255 EXPECT_CALL(observer,
256 OnIncomingNotification(enabled_types_payload_map, 256 OnIncomingNotification(enabled_types_payload_map,
257 syncer::REMOTE_NOTIFICATION)); 257 syncer::REMOTE_NOTIFICATION));
258 bridge_.AddObserver(&observer); 258 bridge_.AddObserver(&observer);
259 // Set enabled types on the bridge. 259 // Set enabled types on the bridge.
260 bridge_.UpdateEnabledTypes(enabled_types); 260 bridge_.UpdateEnabledTypes(enabled_types);
261 TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_REMOTE, 261 TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_REMOTE,
262 syncable::ModelTypePayloadMap()); 262 syncer::ModelTypePayloadMap());
263 ui_loop_.RunAllPending(); 263 ui_loop_.RunAllPending();
264 Mock::VerifyAndClearExpectations(&observer); 264 Mock::VerifyAndClearExpectations(&observer);
265 } 265 }
266 266
267 // Adds an observer on the I/O thread. Then triggers a refresh notification on 267 // 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 268 // the UI thread. We finally verify the proper notification was received by the
269 // observer and destroy it on the I/O thread. 269 // observer and destroy it on the I/O thread.
270 TEST_F(ChromeSyncNotificationBridgeTest, BasicThreaded) { 270 TEST_F(ChromeSyncNotificationBridgeTest, BasicThreaded) {
271 syncable::ModelTypePayloadMap payload_map; 271 syncer::ModelTypePayloadMap payload_map;
272 payload_map[syncable::SESSIONS] = ""; 272 payload_map[syncer::SESSIONS] = "";
273 CreateObserverWithExpectedPayload(payload_map); 273 CreateObserverWithExpectedPayload(payload_map);
274 TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_LOCAL, 274 TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_LOCAL,
275 payload_map); 275 payload_map);
276 VerifyAndDestroyObserver(); 276 VerifyAndDestroyObserver();
277 } 277 }
278 278
279 } // namespace 279 } // namespace
280 } // namespace browser_sync 280 } // namespace browser_sync
OLDNEW
« no previous file with comments | « chrome/browser/sync/glue/chrome_sync_notification_bridge.cc ('k') | chrome/browser/sync/glue/data_type_controller.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698