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/notifier/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" |
11 #include "base/test/test_timeouts.h" | 11 #include "base/test/test_timeouts.h" |
12 #include "base/threading/thread.h" | 12 #include "base/threading/thread.h" |
13 #include "chrome/browser/sync/syncable/model_type.h" | 13 #include "chrome/browser/sync/syncable/model_type.h" |
14 #include "chrome/browser/sync/syncable/model_type_payload_map.h" | 14 #include "chrome/browser/sync/syncable/model_type_payload_map.h" |
15 #include "chrome/browser/sync/notifier/mock_sync_notifier_observer.h" | 15 #include "chrome/browser/sync/notifier/mock_sync_notifier_observer.h" |
16 #include "chrome/browser/sync/notifier/sync_notifier_observer.h" | 16 #include "chrome/browser/sync/notifier/sync_notifier_observer.h" |
17 #include "chrome/common/chrome_notification_types.h" | 17 #include "chrome/common/chrome_notification_types.h" |
18 #include "chrome/test/base/profile_mock.h" | 18 #include "chrome/test/base/profile_mock.h" |
19 #include "content/public/browser/notification_service.h" | 19 #include "content/public/browser/notification_service.h" |
20 #include "content/public/browser/notification_details.h" | 20 #include "content/public/browser/notification_details.h" |
21 #include "content/test/test_browser_thread.h" | 21 #include "content/test/test_browser_thread.h" |
22 #include "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
24 | 24 |
25 namespace sync_notifier { | 25 namespace browser_sync { |
26 namespace { | 26 namespace { |
27 | 27 |
28 using ::testing::Mock; | 28 using ::testing::Mock; |
29 using ::testing::NiceMock; | 29 using ::testing::NiceMock; |
30 using ::testing::StrictMock; | 30 using ::testing::StrictMock; |
31 using content::BrowserThread; | 31 using content::BrowserThread; |
32 | 32 |
33 // Receives a ChromeSyncNotificationBridge to register to, and an expected | 33 // Receives a ChromeSyncNotificationBridge to register to, and an expected |
34 // ModelTypePayloadMap. ReceivedProperNotification() will return true only | 34 // ModelTypePayloadMap. ReceivedProperNotification() will return true only |
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 : public SyncNotifierObserver { | 40 class FakeSyncNotifierObserverIO |
| 41 : public sync_notifier::SyncNotifierObserver { |
41 public: | 42 public: |
42 FakeSyncNotifierObserverIO( | 43 FakeSyncNotifierObserverIO( |
43 ChromeSyncNotificationBridge* bridge, | 44 ChromeSyncNotificationBridge* bridge, |
44 const syncable::ModelTypePayloadMap& expected_payloads) | 45 const syncable::ModelTypePayloadMap& expected_payloads) |
45 : bridge_(bridge), | 46 : bridge_(bridge), |
46 received_improper_notification_(false), | 47 received_improper_notification_(false), |
47 notification_count_(0), | 48 notification_count_(0), |
48 expected_payloads_(expected_payloads) { | 49 expected_payloads_(expected_payloads) { |
49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
50 bridge_->AddObserver(this); | 51 bridge_->AddObserver(this); |
51 } | 52 } |
52 virtual ~FakeSyncNotifierObserverIO() { | 53 virtual ~FakeSyncNotifierObserverIO() { |
53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
54 bridge_->RemoveObserver(this); | 55 bridge_->RemoveObserver(this); |
55 } | 56 } |
56 | 57 |
57 // SyncNotifierObserver implementation. | 58 // SyncNotifierObserver implementation. |
58 virtual void OnIncomingNotification( | 59 virtual void OnIncomingNotification( |
59 const syncable::ModelTypePayloadMap& type_payloads, | 60 const syncable::ModelTypePayloadMap& type_payloads, |
60 IncomingNotificationSource source) OVERRIDE { | 61 sync_notifier::IncomingNotificationSource source) OVERRIDE { |
61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
62 notification_count_++; | 63 notification_count_++; |
63 if (source != LOCAL_NOTIFICATION) { | 64 if (source != sync_notifier::LOCAL_NOTIFICATION) { |
64 LOG(ERROR) << "Received notification with wrong source."; | 65 LOG(ERROR) << "Received notification with wrong source."; |
65 received_improper_notification_ = true; | 66 received_improper_notification_ = true; |
66 } | 67 } |
67 if (expected_payloads_ != type_payloads) { | 68 if (expected_payloads_ != type_payloads) { |
68 LOG(ERROR) << "Received wrong payload."; | 69 LOG(ERROR) << "Received wrong payload."; |
69 received_improper_notification_ = true; | 70 received_improper_notification_ = true; |
70 } | 71 } |
71 } | 72 } |
72 virtual void OnNotificationStateChange(bool notifications_enabled) { | 73 virtual void OnNotificationStateChange(bool notifications_enabled) { |
73 NOTREACHED(); | 74 NOTREACHED(); |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 bool io_observer_notification_failure_; | 183 bool io_observer_notification_failure_; |
183 ChromeSyncNotificationBridge bridge_; | 184 ChromeSyncNotificationBridge bridge_; |
184 base::WaitableEvent done_; | 185 base::WaitableEvent done_; |
185 }; | 186 }; |
186 | 187 |
187 // Adds an observer on the UI thread, triggers a refresh notification, and | 188 // Adds an observer on the UI thread, triggers a refresh notification, and |
188 // ensures the bridge posts a LOCAL_NOTIFICATION with the proper payload to it. | 189 // ensures the bridge posts a LOCAL_NOTIFICATION with the proper payload to it. |
189 TEST_F(ChromeSyncNotificationBridgeTest, Basic) { | 190 TEST_F(ChromeSyncNotificationBridgeTest, Basic) { |
190 syncable::ModelTypePayloadMap payload_map; | 191 syncable::ModelTypePayloadMap payload_map; |
191 payload_map[syncable::SESSIONS] = ""; | 192 payload_map[syncable::SESSIONS] = ""; |
192 StrictMock<MockSyncNotifierObserver> observer; | 193 StrictMock<sync_notifier::MockSyncNotifierObserver> observer; |
193 EXPECT_CALL(observer, | 194 EXPECT_CALL(observer, |
194 OnIncomingNotification(payload_map, LOCAL_NOTIFICATION)); | 195 OnIncomingNotification(payload_map, |
| 196 sync_notifier::LOCAL_NOTIFICATION)); |
195 bridge_.AddObserver(&observer); | 197 bridge_.AddObserver(&observer); |
196 TriggerRefreshNotification(); | 198 TriggerRefreshNotification(); |
197 ui_loop_.RunAllPending(); | 199 ui_loop_.RunAllPending(); |
198 Mock::VerifyAndClearExpectations(&observer); | 200 Mock::VerifyAndClearExpectations(&observer); |
199 } | 201 } |
200 | 202 |
201 // Adds an observer on the I/O thread. Then triggers a refresh notification on | 203 // Adds an observer on the I/O thread. Then triggers a refresh notification on |
202 // the UI thread. We finally verify the proper notification was received by the | 204 // the UI thread. We finally verify the proper notification was received by the |
203 // observer and destroy it on the I/O thread. | 205 // observer and destroy it on the I/O thread. |
204 TEST_F(ChromeSyncNotificationBridgeTest, BasicThreaded) { | 206 TEST_F(ChromeSyncNotificationBridgeTest, BasicThreaded) { |
205 syncable::ModelTypePayloadMap payload_map; | 207 syncable::ModelTypePayloadMap payload_map; |
206 payload_map[syncable::SESSIONS] = ""; | 208 payload_map[syncable::SESSIONS] = ""; |
207 CreateObserverWithExpectedPayload(payload_map); | 209 CreateObserverWithExpectedPayload(payload_map); |
208 TriggerRefreshNotification(); | 210 TriggerRefreshNotification(); |
209 VerifyAndDestroyObserver(); | 211 VerifyAndDestroyObserver(); |
210 } | 212 } |
211 | 213 |
212 } // namespace | 214 } // namespace |
213 } // namespace sync_notifier | 215 } // namespace browser_sync |
OLD | NEW |