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 const syncer::ObjectIdSet& ids = syncer::ModelTypeSetToObjectIdSet( |
akalin
2012/07/21 01:09:47
not necessary for this CL, but surely we should ha
dcheng
2012/07/21 14:06:53
Done.
| |
57 syncer::ModelTypePayloadMapToEnumSet( | |
58 syncer::ObjectIdPayloadMapToModelTypePayloadMap( | |
59 expected_payloads))); | |
60 bridge_->UpdateRegisteredIds(this, ids); | |
57 } | 61 } |
58 | 62 |
59 virtual ~FakeSyncNotifierObserver() { | 63 virtual ~FakeSyncNotifierObserver() { |
60 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | 64 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
61 bridge_->RemoveObserver(this); | 65 bridge_->UpdateRegisteredIds(this, syncer::ObjectIdSet()); |
62 } | 66 } |
63 | 67 |
64 // SyncNotifierObserver implementation. | 68 // SyncNotifierObserver implementation. |
65 virtual void OnIncomingNotification( | 69 virtual void OnIncomingNotification( |
66 const syncer::ModelTypePayloadMap& type_payloads, | 70 const syncer::ObjectIdPayloadMap& id_payloads, |
67 syncer::IncomingNotificationSource source) OVERRIDE { | 71 syncer::IncomingNotificationSource source) OVERRIDE { |
68 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | 72 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
69 notification_count_++; | 73 notification_count_++; |
70 if (source != expected_source_) { | 74 if (source != expected_source_) { |
71 LOG(ERROR) << "Received notification with wrong source"; | 75 LOG(ERROR) << "Received notification with wrong source"; |
72 received_improper_notification_ = true; | 76 received_improper_notification_ = true; |
73 } | 77 } |
74 if (expected_payloads_ != type_payloads) { | 78 if (expected_payloads_ != id_payloads) { |
75 LOG(ERROR) << "Received wrong payload"; | 79 LOG(ERROR) << "Received wrong payload"; |
76 received_improper_notification_ = true; | 80 received_improper_notification_ = true; |
77 } | 81 } |
78 } | 82 } |
79 virtual void OnNotificationsEnabled() OVERRIDE { | 83 virtual void OnNotificationsEnabled() OVERRIDE { |
80 NOTREACHED(); | 84 NOTREACHED(); |
81 } | 85 } |
82 virtual void OnNotificationsDisabled( | 86 virtual void OnNotificationsDisabled( |
83 syncer::NotificationsDisabledReason reason) OVERRIDE { | 87 syncer::NotificationsDisabledReason reason) OVERRIDE { |
84 NOTREACHED(); | 88 NOTREACHED(); |
85 } | 89 } |
86 | 90 |
87 bool ReceivedProperNotification() const { | 91 bool ReceivedProperNotification() const { |
88 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); | 92 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); |
89 return (notification_count_ == 1) && !received_improper_notification_; | 93 return (notification_count_ == 1) && !received_improper_notification_; |
90 } | 94 } |
91 | 95 |
92 private: | 96 private: |
93 const scoped_refptr<base::SequencedTaskRunner> sync_task_runner_; | 97 const scoped_refptr<base::SequencedTaskRunner> sync_task_runner_; |
94 ChromeSyncNotificationBridge* const bridge_; | 98 ChromeSyncNotificationBridge* const bridge_; |
95 bool received_improper_notification_; | 99 bool received_improper_notification_; |
96 size_t notification_count_; | 100 size_t notification_count_; |
97 const syncer::ModelTypePayloadMap expected_payloads_; | 101 const syncer::ObjectIdPayloadMap expected_payloads_; |
98 const syncer::IncomingNotificationSource expected_source_; | 102 const syncer::IncomingNotificationSource expected_source_; |
99 }; | 103 }; |
100 | 104 |
101 class ChromeSyncNotificationBridgeTest : public testing::Test { | 105 class ChromeSyncNotificationBridgeTest : public testing::Test { |
102 public: | 106 public: |
103 ChromeSyncNotificationBridgeTest() | 107 ChromeSyncNotificationBridgeTest() |
104 : ui_thread_(BrowserThread::UI), | 108 : ui_thread_(BrowserThread::UI), |
105 sync_thread_("Sync thread"), | 109 sync_thread_("Sync thread"), |
106 sync_observer_(NULL), | 110 sync_observer_(NULL), |
107 sync_observer_notification_failure_(false), | 111 sync_observer_notification_failure_(false), |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
141 void VerifyAndDestroyObserver() { | 145 void VerifyAndDestroyObserver() { |
142 ASSERT_TRUE(sync_thread_.message_loop_proxy()->PostTask( | 146 ASSERT_TRUE(sync_thread_.message_loop_proxy()->PostTask( |
143 FROM_HERE, | 147 FROM_HERE, |
144 base::Bind(&ChromeSyncNotificationBridgeTest:: | 148 base::Bind(&ChromeSyncNotificationBridgeTest:: |
145 VerifyAndDestroyObserverOnSyncThread, | 149 VerifyAndDestroyObserverOnSyncThread, |
146 base::Unretained(this)))); | 150 base::Unretained(this)))); |
147 BlockForSyncThread(); | 151 BlockForSyncThread(); |
148 } | 152 } |
149 | 153 |
150 void CreateObserverOnSyncThread( | 154 void CreateObserverOnSyncThread( |
151 syncer::ModelTypePayloadMap expected_payloads, | 155 const syncer::ObjectIdPayloadMap& expected_payloads, |
152 syncer::IncomingNotificationSource expected_source) { | 156 syncer::IncomingNotificationSource expected_source) { |
153 DCHECK(sync_thread_.message_loop_proxy()->RunsTasksOnCurrentThread()); | 157 DCHECK(sync_thread_.message_loop_proxy()->RunsTasksOnCurrentThread()); |
154 sync_observer_ = new FakeSyncNotifierObserver( | 158 sync_observer_ = new FakeSyncNotifierObserver( |
155 sync_thread_.message_loop_proxy(), | 159 sync_thread_.message_loop_proxy(), |
156 bridge_.get(), | 160 bridge_.get(), |
157 expected_payloads, | 161 expected_payloads, |
158 expected_source); | 162 expected_source); |
159 } | 163 } |
160 | 164 |
161 void CreateObserverWithExpectations( | 165 void CreateObserverWithExpectations( |
162 syncer::ModelTypePayloadMap expected_payloads, | 166 const syncer::ModelTypePayloadMap& expected_payloads, |
163 syncer::IncomingNotificationSource expected_source) { | 167 syncer::IncomingNotificationSource expected_source) { |
168 const syncer::ObjectIdPayloadMap& expected_id_payloads = | |
169 syncer::ModelTypePayloadMapToObjectIdPayloadMap(expected_payloads); | |
164 ASSERT_TRUE(sync_thread_.message_loop_proxy()->PostTask( | 170 ASSERT_TRUE(sync_thread_.message_loop_proxy()->PostTask( |
165 FROM_HERE, | 171 FROM_HERE, |
166 base::Bind( | 172 base::Bind( |
167 &ChromeSyncNotificationBridgeTest::CreateObserverOnSyncThread, | 173 &ChromeSyncNotificationBridgeTest::CreateObserverOnSyncThread, |
168 base::Unretained(this), | 174 base::Unretained(this), |
169 expected_payloads, | 175 expected_id_payloads, |
170 expected_source))); | 176 expected_source))); |
171 BlockForSyncThread(); | 177 BlockForSyncThread(); |
172 } | 178 } |
173 | 179 |
174 void SignalOnSyncThread() { | 180 void SignalOnSyncThread() { |
175 DCHECK(sync_thread_.message_loop_proxy()->RunsTasksOnCurrentThread()); | 181 DCHECK(sync_thread_.message_loop_proxy()->RunsTasksOnCurrentThread()); |
176 done_.Signal(); | 182 done_.Signal(); |
177 } | 183 } |
178 | 184 |
179 void BlockForSyncThread() { | 185 void BlockForSyncThread() { |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
256 syncer::ModelTypePayloadMapFromEnumSet(enabled_types, std::string()); | 262 syncer::ModelTypePayloadMapFromEnumSet(enabled_types, std::string()); |
257 CreateObserverWithExpectations( | 263 CreateObserverWithExpectations( |
258 enabled_types_payload_map, syncer::REMOTE_NOTIFICATION); | 264 enabled_types_payload_map, syncer::REMOTE_NOTIFICATION); |
259 TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_REMOTE, | 265 TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_REMOTE, |
260 enabled_types_payload_map); | 266 enabled_types_payload_map); |
261 VerifyAndDestroyObserver(); | 267 VerifyAndDestroyObserver(); |
262 } | 268 } |
263 | 269 |
264 } // namespace | 270 } // namespace |
265 } // namespace browser_sync | 271 } // namespace browser_sync |
OLD | NEW |