| 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/android_invalidator_bridge.h" |
| 6 | 6 |
| 7 #include <cstddef> | 7 #include <cstddef> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/location.h" | 12 #include "base/location.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/message_loop.h" | 15 #include "base/message_loop.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 31 | 31 |
| 32 namespace browser_sync { | 32 namespace browser_sync { |
| 33 namespace { | 33 namespace { |
| 34 | 34 |
| 35 using ::testing::NiceMock; | 35 using ::testing::NiceMock; |
| 36 | 36 |
| 37 // Since all the interesting stuff happens on the sync thread, we have | 37 // Since all the interesting stuff happens on the sync thread, we have |
| 38 // to be careful to use GTest/GMock only on the main thread since they | 38 // to be careful to use GTest/GMock only on the main thread since they |
| 39 // are not thread-safe. | 39 // are not thread-safe. |
| 40 | 40 |
| 41 class ChromeSyncNotificationBridgeTest : public testing::Test { | 41 class AndroidInvalidatorBridgeTest : public testing::Test { |
| 42 public: | 42 public: |
| 43 ChromeSyncNotificationBridgeTest() | 43 AndroidInvalidatorBridgeTest() |
| 44 : ui_thread_(content::BrowserThread::UI, &ui_loop_), | 44 : ui_thread_(content::BrowserThread::UI, &ui_loop_), |
| 45 sync_thread_("Sync thread"), | 45 sync_thread_("Sync thread"), |
| 46 sync_handler_notification_success_(false) {} | 46 sync_handler_notification_success_(false) {} |
| 47 | 47 |
| 48 virtual ~ChromeSyncNotificationBridgeTest() {} | 48 virtual ~AndroidInvalidatorBridgeTest() {} |
| 49 | 49 |
| 50 protected: | 50 protected: |
| 51 virtual void SetUp() OVERRIDE { | 51 virtual void SetUp() OVERRIDE { |
| 52 ASSERT_TRUE(sync_thread_.Start()); | 52 ASSERT_TRUE(sync_thread_.Start()); |
| 53 bridge_.reset( | 53 bridge_.reset( |
| 54 new ChromeSyncNotificationBridge( | 54 new AndroidInvalidatorBridge( |
| 55 &mock_profile_, sync_thread_.message_loop_proxy())); | 55 &mock_profile_, sync_thread_.message_loop_proxy())); |
| 56 } | 56 } |
| 57 | 57 |
| 58 virtual void TearDown() OVERRIDE { | 58 virtual void TearDown() OVERRIDE { |
| 59 bridge_->StopForShutdown(); | 59 bridge_->StopForShutdown(); |
| 60 sync_thread_.Stop(); | 60 sync_thread_.Stop(); |
| 61 // Must be reset only after the sync thread is stopped. | 61 // Must be reset only after the sync thread is stopped. |
| 62 bridge_.reset(); | 62 bridge_.reset(); |
| 63 EXPECT_EQ(NULL, sync_handler_.get()); | 63 EXPECT_EQ(NULL, sync_handler_.get()); |
| 64 if (!sync_handler_notification_success_) | 64 if (!sync_handler_notification_success_) |
| 65 ADD_FAILURE() << "Sync handler did not receive proper notification."; | 65 ADD_FAILURE() << "Sync handler did not receive proper notification."; |
| 66 } | 66 } |
| 67 | 67 |
| 68 void VerifyAndDestroyObserver( | 68 void VerifyAndDestroyObserver( |
| 69 const syncer::ModelTypeInvalidationMap& expected_invalidations, | 69 const syncer::ModelTypeInvalidationMap& expected_invalidations, |
| 70 syncer::IncomingInvalidationSource expected_source) { | 70 syncer::IncomingInvalidationSource expected_source) { |
| 71 ASSERT_TRUE(sync_thread_.message_loop_proxy()->PostTask( | 71 ASSERT_TRUE(sync_thread_.message_loop_proxy()->PostTask( |
| 72 FROM_HERE, | 72 FROM_HERE, |
| 73 base::Bind(&ChromeSyncNotificationBridgeTest:: | 73 base::Bind(&AndroidInvalidatorBridgeTest:: |
| 74 VerifyAndDestroyObserverOnSyncThread, | 74 VerifyAndDestroyObserverOnSyncThread, |
| 75 base::Unretained(this), | 75 base::Unretained(this), |
| 76 expected_invalidations, | 76 expected_invalidations, |
| 77 expected_source))); | 77 expected_source))); |
| 78 BlockForSyncThread(); | 78 BlockForSyncThread(); |
| 79 } | 79 } |
| 80 | 80 |
| 81 void CreateObserver() { | 81 void CreateObserver() { |
| 82 ASSERT_TRUE(sync_thread_.message_loop_proxy()->PostTask( | 82 ASSERT_TRUE(sync_thread_.message_loop_proxy()->PostTask( |
| 83 FROM_HERE, | 83 FROM_HERE, |
| 84 base::Bind( | 84 base::Bind( |
| 85 &ChromeSyncNotificationBridgeTest::CreateObserverOnSyncThread, | 85 &AndroidInvalidatorBridgeTest::CreateObserverOnSyncThread, |
| 86 base::Unretained(this)))); | 86 base::Unretained(this)))); |
| 87 BlockForSyncThread(); | 87 BlockForSyncThread(); |
| 88 } | 88 } |
| 89 | 89 |
| 90 void UpdateEnabledTypes(syncer::ModelTypeSet enabled_types) { | 90 void UpdateEnabledTypes(syncer::ModelTypeSet enabled_types) { |
| 91 ASSERT_TRUE(sync_thread_.message_loop_proxy()->PostTask( | 91 ASSERT_TRUE(sync_thread_.message_loop_proxy()->PostTask( |
| 92 FROM_HERE, | 92 FROM_HERE, |
| 93 base::Bind( | 93 base::Bind( |
| 94 &ChromeSyncNotificationBridgeTest:: | 94 &AndroidInvalidatorBridgeTest:: |
| 95 UpdateEnabledTypesOnSyncThread, | 95 UpdateEnabledTypesOnSyncThread, |
| 96 base::Unretained(this), | 96 base::Unretained(this), |
| 97 enabled_types))); | 97 enabled_types))); |
| 98 BlockForSyncThread(); | 98 BlockForSyncThread(); |
| 99 } | 99 } |
| 100 | 100 |
| 101 void TriggerRefreshNotification( | 101 void TriggerRefreshNotification( |
| 102 int type, | 102 int type, |
| 103 const syncer::ModelTypeInvalidationMap& invalidation_map) { | 103 const syncer::ModelTypeInvalidationMap& invalidation_map) { |
| 104 content::NotificationService::current()->Notify( | 104 content::NotificationService::current()->Notify( |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 run_loop.Run(); | 153 run_loop.Run(); |
| 154 } | 154 } |
| 155 | 155 |
| 156 MessageLoop ui_loop_; | 156 MessageLoop ui_loop_; |
| 157 content::TestBrowserThread ui_thread_; | 157 content::TestBrowserThread ui_thread_; |
| 158 base::Thread sync_thread_; | 158 base::Thread sync_thread_; |
| 159 NiceMock<ProfileMock> mock_profile_; | 159 NiceMock<ProfileMock> mock_profile_; |
| 160 // Created/used/destroyed on sync thread. | 160 // Created/used/destroyed on sync thread. |
| 161 scoped_ptr<syncer::FakeInvalidationHandler> sync_handler_; | 161 scoped_ptr<syncer::FakeInvalidationHandler> sync_handler_; |
| 162 bool sync_handler_notification_success_; | 162 bool sync_handler_notification_success_; |
| 163 scoped_ptr<ChromeSyncNotificationBridge> bridge_; | 163 scoped_ptr<AndroidInvalidatorBridge> bridge_; |
| 164 }; | 164 }; |
| 165 | 165 |
| 166 // Adds an observer on the sync thread, triggers a local refresh | |
| 167 // invalidation, and ensures the bridge posts a LOCAL_INVALIDATION | |
| 168 // with the proper state to it. | |
| 169 TEST_F(ChromeSyncNotificationBridgeTest, LocalNotification) { | |
| 170 const syncer::ModelTypeSet types(syncer::SESSIONS); | |
| 171 const syncer::ModelTypeInvalidationMap& invalidation_map = | |
| 172 ModelTypeSetToInvalidationMap(types, std::string()); | |
| 173 CreateObserver(); | |
| 174 UpdateEnabledTypes(syncer::ModelTypeSet(syncer::SESSIONS)); | |
| 175 TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_LOCAL, | |
| 176 invalidation_map); | |
| 177 VerifyAndDestroyObserver(invalidation_map, syncer::LOCAL_INVALIDATION); | |
| 178 } | |
| 179 | |
| 180 // Adds an observer on the sync thread, triggers a remote refresh | 166 // Adds an observer on the sync thread, triggers a remote refresh |
| 181 // invalidation, and ensures the bridge posts a REMOTE_INVALIDATION | 167 // invalidation, and ensures the bridge posts a REMOTE_INVALIDATION |
| 182 // with the proper state to it. | 168 // with the proper state to it. |
| 183 TEST_F(ChromeSyncNotificationBridgeTest, RemoteNotification) { | 169 TEST_F(AndroidInvalidatorBridgeTest, RemoteNotification) { |
| 184 const syncer::ModelTypeSet types(syncer::SESSIONS); | 170 const syncer::ModelTypeSet types(syncer::SESSIONS); |
| 185 const syncer::ModelTypeInvalidationMap& invalidation_map = | 171 const syncer::ModelTypeInvalidationMap& invalidation_map = |
| 186 ModelTypeSetToInvalidationMap(types, std::string()); | 172 ModelTypeSetToInvalidationMap(types, std::string()); |
| 187 CreateObserver(); | 173 CreateObserver(); |
| 188 UpdateEnabledTypes(syncer::ModelTypeSet(syncer::SESSIONS)); | 174 UpdateEnabledTypes(syncer::ModelTypeSet(syncer::SESSIONS)); |
| 189 TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_REMOTE, | 175 TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_REMOTE, |
| 190 invalidation_map); | 176 invalidation_map); |
| 191 VerifyAndDestroyObserver(invalidation_map, syncer::REMOTE_INVALIDATION); | 177 VerifyAndDestroyObserver(invalidation_map, syncer::REMOTE_INVALIDATION); |
| 192 } | 178 } |
| 193 | 179 |
| 194 // Adds an observer on the sync thread, triggers a local refresh | |
| 195 // notification with empty state map and ensures the bridge posts a | |
| 196 // LOCAL_INVALIDATION with the proper state to it. | |
| 197 TEST_F(ChromeSyncNotificationBridgeTest, LocalNotificationEmptyPayloadMap) { | |
| 198 const syncer::ModelTypeSet enabled_types( | |
| 199 syncer::BOOKMARKS, syncer::PASSWORDS); | |
| 200 const syncer::ModelTypeInvalidationMap enabled_types_invalidation_map = | |
| 201 syncer::ModelTypeSetToInvalidationMap(enabled_types, std::string()); | |
| 202 CreateObserver(); | |
| 203 UpdateEnabledTypes(enabled_types); | |
| 204 TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_LOCAL, | |
| 205 syncer::ModelTypeInvalidationMap()); | |
| 206 VerifyAndDestroyObserver( | |
| 207 enabled_types_invalidation_map, syncer::LOCAL_INVALIDATION); | |
| 208 } | |
| 209 | |
| 210 // Adds an observer on the sync thread, triggers a remote refresh | 180 // Adds an observer on the sync thread, triggers a remote refresh |
| 211 // notification with empty state map and ensures the bridge posts a | 181 // notification with empty state map and ensures the bridge posts a |
| 212 // REMOTE_INVALIDATION with the proper state to it. | 182 // REMOTE_INVALIDATION with the proper state to it. |
| 213 TEST_F(ChromeSyncNotificationBridgeTest, RemoteNotificationEmptyPayloadMap) { | 183 TEST_F(AndroidInvalidatorBridgeTest, RemoteNotificationEmptyPayloadMap) { |
| 214 const syncer::ModelTypeSet enabled_types( | 184 const syncer::ModelTypeSet enabled_types( |
| 215 syncer::BOOKMARKS, syncer::TYPED_URLS); | 185 syncer::BOOKMARKS, syncer::TYPED_URLS); |
| 216 const syncer::ModelTypeInvalidationMap enabled_types_invalidation_map = | 186 const syncer::ModelTypeInvalidationMap enabled_types_invalidation_map = |
| 217 syncer::ModelTypeSetToInvalidationMap(enabled_types, std::string()); | 187 syncer::ModelTypeSetToInvalidationMap(enabled_types, std::string()); |
| 218 CreateObserver(); | 188 CreateObserver(); |
| 219 UpdateEnabledTypes(enabled_types); | 189 UpdateEnabledTypes(enabled_types); |
| 220 TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_REMOTE, | 190 TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_REMOTE, |
| 221 syncer::ModelTypeInvalidationMap()); | 191 syncer::ModelTypeInvalidationMap()); |
| 222 VerifyAndDestroyObserver( | 192 VerifyAndDestroyObserver( |
| 223 enabled_types_invalidation_map, syncer::REMOTE_INVALIDATION); | 193 enabled_types_invalidation_map, syncer::REMOTE_INVALIDATION); |
| 224 } | 194 } |
| 225 | 195 |
| 226 } // namespace | 196 } // namespace |
| 227 } // namespace browser_sync | 197 } // namespace browser_sync |
| OLD | NEW |