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