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

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

Issue 12022041: Separate local and remote sync invalidations (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 7 years, 10 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/sync/glue/chrome_sync_notification_bridge.h"
6
7 #include <cstddef>
8
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/compiler_specific.h"
12 #include "base/location.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/message_loop.h"
16 #include "base/message_loop_proxy.h"
17 #include "base/run_loop.h"
18 #include "base/sequenced_task_runner.h"
19 #include "base/threading/thread.h"
20 #include "chrome/common/chrome_notification_types.h"
21 #include "chrome/test/base/profile_mock.h"
22 #include "content/public/browser/notification_details.h"
23 #include "content/public/browser/notification_service.h"
24 #include "content/public/test/test_browser_thread.h"
25 #include "sync/internal_api/public/base/model_type.h"
26 #include "sync/internal_api/public/base/model_type_invalidation_map.h"
27 #include "sync/notifier/fake_invalidation_handler.h"
28 #include "sync/notifier/object_id_invalidation_map_test_util.h"
29 #include "testing/gmock/include/gmock/gmock.h"
30 #include "testing/gtest/include/gtest/gtest.h"
31
32 namespace browser_sync {
33 namespace {
34
35 using ::testing::NiceMock;
36
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
39 // are not thread-safe.
40
41 class ChromeSyncNotificationBridgeTest : public testing::Test {
42 public:
43 ChromeSyncNotificationBridgeTest()
44 : ui_thread_(content::BrowserThread::UI, &ui_loop_),
45 sync_thread_("Sync thread"),
46 sync_handler_notification_success_(false) {}
47
48 virtual ~ChromeSyncNotificationBridgeTest() {}
49
50 protected:
51 virtual void SetUp() OVERRIDE {
52 ASSERT_TRUE(sync_thread_.Start());
53 bridge_.reset(
54 new ChromeSyncNotificationBridge(
55 &mock_profile_, sync_thread_.message_loop_proxy()));
56 }
57
58 virtual void TearDown() OVERRIDE {
59 bridge_->StopForShutdown();
60 sync_thread_.Stop();
61 // Must be reset only after the sync thread is stopped.
62 bridge_.reset();
63 EXPECT_EQ(NULL, sync_handler_.get());
64 if (!sync_handler_notification_success_)
65 ADD_FAILURE() << "Sync handler did not receive proper notification.";
66 }
67
68 void VerifyAndDestroyObserver(
69 const syncer::ModelTypeInvalidationMap& expected_invalidations,
70 syncer::IncomingInvalidationSource expected_source) {
71 ASSERT_TRUE(sync_thread_.message_loop_proxy()->PostTask(
72 FROM_HERE,
73 base::Bind(&ChromeSyncNotificationBridgeTest::
74 VerifyAndDestroyObserverOnSyncThread,
75 base::Unretained(this),
76 expected_invalidations,
77 expected_source)));
78 BlockForSyncThread();
79 }
80
81 void CreateObserver() {
82 ASSERT_TRUE(sync_thread_.message_loop_proxy()->PostTask(
83 FROM_HERE,
84 base::Bind(
85 &ChromeSyncNotificationBridgeTest::CreateObserverOnSyncThread,
86 base::Unretained(this))));
87 BlockForSyncThread();
88 }
89
90 void UpdateEnabledTypes(syncer::ModelTypeSet enabled_types) {
91 ASSERT_TRUE(sync_thread_.message_loop_proxy()->PostTask(
92 FROM_HERE,
93 base::Bind(
94 &ChromeSyncNotificationBridgeTest::
95 UpdateEnabledTypesOnSyncThread,
96 base::Unretained(this),
97 enabled_types)));
98 BlockForSyncThread();
99 }
100
101 void TriggerRefreshNotification(
102 int type,
103 const syncer::ModelTypeInvalidationMap& invalidation_map) {
104 content::NotificationService::current()->Notify(
105 type,
106 content::Source<Profile>(&mock_profile_),
107 content::Details<const syncer::ModelTypeInvalidationMap>(
108 &invalidation_map));
109 BlockForSyncThread();
110 }
111
112 private:
113 void VerifyAndDestroyObserverOnSyncThread(
114 const syncer::ModelTypeInvalidationMap& expected_invalidations,
115 syncer::IncomingInvalidationSource expected_source) {
116 DCHECK(sync_thread_.message_loop_proxy()->RunsTasksOnCurrentThread());
117 if (sync_handler_.get()) {
118 sync_handler_notification_success_ =
119 (sync_handler_->GetInvalidationCount() == 1) &&
120 ObjectIdInvalidationMapEquals(
121 sync_handler_->GetLastInvalidationMap(),
122 syncer::ModelTypeInvalidationMapToObjectIdInvalidationMap(
123 expected_invalidations)) &&
124 (sync_handler_->GetLastInvalidationSource() == expected_source);
125 bridge_->UnregisterHandler(sync_handler_.get());
126 } else {
127 sync_handler_notification_success_ = false;
128 }
129 sync_handler_.reset();
130 }
131
132 void CreateObserverOnSyncThread() {
133 DCHECK(sync_thread_.message_loop_proxy()->RunsTasksOnCurrentThread());
134 sync_handler_.reset(new syncer::FakeInvalidationHandler());
135 bridge_->RegisterHandler(sync_handler_.get());
136 }
137
138 void UpdateEnabledTypesOnSyncThread(
139 syncer::ModelTypeSet enabled_types) {
140 DCHECK(sync_thread_.message_loop_proxy()->RunsTasksOnCurrentThread());
141 bridge_->UpdateRegisteredIds(
142 sync_handler_.get(), ModelTypeSetToObjectIdSet(enabled_types));
143 }
144
145 void BlockForSyncThread() {
146 // Post a task to the sync thread's message loop and block until
147 // it runs.
148 base::RunLoop run_loop;
149 ASSERT_TRUE(sync_thread_.message_loop_proxy()->PostTaskAndReply(
150 FROM_HERE,
151 base::Bind(&base::DoNothing),
152 run_loop.QuitClosure()));
153 run_loop.Run();
154 }
155
156 MessageLoop ui_loop_;
157 content::TestBrowserThread ui_thread_;
158 base::Thread sync_thread_;
159 NiceMock<ProfileMock> mock_profile_;
160 // Created/used/destroyed on sync thread.
161 scoped_ptr<syncer::FakeInvalidationHandler> sync_handler_;
162 bool sync_handler_notification_success_;
163 scoped_ptr<ChromeSyncNotificationBridge> bridge_;
164 };
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
181 // invalidation, and ensures the bridge posts a REMOTE_INVALIDATION
182 // with the proper state to it.
183 TEST_F(ChromeSyncNotificationBridgeTest, RemoteNotification) {
184 const syncer::ModelTypeSet types(syncer::SESSIONS);
185 const syncer::ModelTypeInvalidationMap& invalidation_map =
186 ModelTypeSetToInvalidationMap(types, std::string());
187 CreateObserver();
188 UpdateEnabledTypes(syncer::ModelTypeSet(syncer::SESSIONS));
189 TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_REMOTE,
190 invalidation_map);
191 VerifyAndDestroyObserver(invalidation_map, syncer::REMOTE_INVALIDATION);
192 }
193
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
211 // notification with empty state map and ensures the bridge posts a
212 // REMOTE_INVALIDATION with the proper state to it.
213 TEST_F(ChromeSyncNotificationBridgeTest, RemoteNotificationEmptyPayloadMap) {
214 const syncer::ModelTypeSet enabled_types(
215 syncer::BOOKMARKS, syncer::TYPED_URLS);
216 const syncer::ModelTypeInvalidationMap enabled_types_invalidation_map =
217 syncer::ModelTypeSetToInvalidationMap(enabled_types, std::string());
218 CreateObserver();
219 UpdateEnabledTypes(enabled_types);
220 TriggerRefreshNotification(chrome::NOTIFICATION_SYNC_REFRESH_REMOTE,
221 syncer::ModelTypeInvalidationMap());
222 VerifyAndDestroyObserver(
223 enabled_types_invalidation_map, syncer::REMOTE_INVALIDATION);
224 }
225
226 } // namespace
227 } // namespace browser_sync
OLDNEW
« no previous file with comments | « chrome/browser/sync/glue/chrome_sync_notification_bridge.cc ('k') | chrome/browser/sync/glue/sync_backend_host.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698