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

Side by Side Diff: chrome/browser/notifier/chrome_notifier_service_unittest.cc

Issue 11745024: Synced Notification Sync Change Processor (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Lint fixes 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
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 <map>
6
7 #include "base/memory/scoped_ptr.h"
8 #include "chrome/browser/notifications/notification_ui_manager.h"
9 #include "chrome/browser/notifier/chrome_notifier_service.h"
10 #include "chrome/browser/notifier/synced_notification.h"
11 #include "sync/api/sync_change.h"
12 #include "sync/api/sync_change_processor.h"
13 #include "sync/api/sync_error_factory.h"
14 #include "sync/api/sync_error_factory_mock.h"
15 #include "sync/protocol/sync.pb.h"
16 #include "sync/protocol/synced_notification_specifics.pb.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 using sync_pb::SyncedNotificationSpecifics;
20 using sync_pb::EntitySpecifics;
21 using sync_pb::SyncedNotificationRenderInfo_Layout_LayoutType_TITLE_AND_SUBTEXT;
22 using syncer::SyncData;
23 using syncer::SyncChange;
24 using syncer::SyncChangeList;
25 using syncer::SyncDataList;
26 using syncer::SYNCED_NOTIFICATIONS;
27 using notifier::SyncedNotification;
28 using notifier::ChromeNotifierService;
29
30 namespace {
31
32 const char kAppId1[] = "fboilmbenheemaomgaeehigklolhkhnf";
33 const char kAppId2[] = "fbcmoldooppoahjhfflnmljoanccekpf";
34 const char kAppId3[] = "fbcmoldooppoahjhfflnmljoanccek33";
35 const char kAppId4[] = "fbcmoldooppoahjhfflnmljoanccek44";
36 const char kAppId5[] = "fbcmoldooppoahjhfflnmljoanccek55";
37 const char kAppId6[] = "fbcmoldooppoahjhfflnmljoanccek66";
38 const char kAppId7[] = "fbcmoldooppoahjhfflnmljoanccek77";
39 const char kCoalescingKey1[] = "foo";
40 const char kCoalescingKey2[] = "bar";
41 const char kCoalescingKey3[] = "bat";
42 const char kCoalescingKey4[] = "baz";
43 const char kCoalescingKey5[] = "foobar";
44 const char kCoalescingKey6[] = "fu";
45 const char kCoalescingKey7[] = "meta";
46 const char kNotificationId1[] = "fboilmbenheemaomgaeehigklolhkhnf/foo";
47 const char kNotificationId2[] = "fbcmoldooppoahjhfflnmljoanccekpf/bar";
48 const char kNotificationId3[] = "fbcmoldooppoahjhfflnmljoanccek33/bat";
49 const char kNotificationId4[] = "fbcmoldooppoahjhfflnmljoanccek44/baz";
50 const char kNotificationId5[] = "fbcmoldooppoahjhfflnmljoanccek55/foobar";
51 const char kNotificationId6[] = "fbcmoldooppoahjhfflnmljoanccek66/fu";
52 const char kNotificationId7[] = "fbcmoldooppoahjhfflnmljoanccek77/meta";
53
54 const int64 kFakeCreationTime = 42;
55
56 // Extract notification id from syncer::SyncData.
57 std::string GetNotificationId(const SyncData& sync_data) {
58 SyncedNotificationSpecifics specifics = sync_data.GetSpecifics().
59 synced_notification();
60 std::string notification_id = specifics.
61 coalesced_notification().id().app_id();
62 notification_id += "/";
63 notification_id += specifics.coalesced_notification().id().coalescing_key();
64 return notification_id;
65 }
66
67 // Stub out the NotificationUIManager for unit testing.
68 class StubNotificationUIManager : public NotificationUIManager {
69 public:
70 StubNotificationUIManager() {}
71 virtual ~StubNotificationUIManager() {}
72
73 // Adds a notification to be displayed. Virtual for unit test override.
74 virtual void Add(const Notification& notification, Profile* profile)
75 OVERRIDE {}
76
77 // Removes any notifications matching the supplied ID, either currently
78 // displayed or in the queue. Returns true if anything was removed.
79 virtual bool CancelById(const std::string& notification_id) OVERRIDE {
80 return false;
81 }
82
83 // Removes notifications matching the |source_origin| (which could be an
84 // extension ID). Returns true if anything was removed.
85 virtual bool CancelAllBySourceOrigin(const GURL& source_origin) OVERRIDE {
86 return false;
87 }
88
89 // Removes notifications matching |profile|. Returns true if any were removed.
90 virtual bool CancelAllByProfile(Profile* profile) OVERRIDE {
91 return false;
92 }
93
94 // Cancels all pending notifications and closes anything currently showing.
95 // Used when the app is terminating.
96 virtual void CancelAll() OVERRIDE {}
97
98 private:
99 DISALLOW_COPY_AND_ASSIGN(StubNotificationUIManager);
100 };
101
102 // Dummy SyncChangeProcessor used to help review what SyncChanges are pushed
103 // back up to Sync.
104 class TestChangeProcessor : public syncer::SyncChangeProcessor {
105 public:
106 TestChangeProcessor() { }
107 virtual ~TestChangeProcessor() { }
108
109 // Store a copy of all the changes passed in so we can examine them later.
110 virtual syncer::SyncError ProcessSyncChanges(
111 const tracked_objects::Location& from_here,
112 const SyncChangeList& change_list) OVERRIDE {
113 change_map_.clear();
114 for (SyncChangeList::const_iterator iter = change_list.begin();
115 iter != change_list.end(); ++iter) {
116 // Put the data into the change tracking map.
117 change_map_[GetNotificationId(iter->sync_data())] = *iter;
118 }
119
120 return syncer::SyncError();
121 }
122
123 size_t change_list_size() { return change_map_.size(); }
124
125 bool ContainsId(const std::string& id) {
126 return change_map_.find(id) != change_map_.end();
127 }
128
129 SyncChange GetChangeById(const std::string& id) {
130 EXPECT_TRUE(ContainsId(id));
131 return change_map_[id];
132 }
133
134 private:
135 // Track the changes received in ProcessSyncChanges.
136 std::map<std::string, SyncChange> change_map_;
137
138 DISALLOW_COPY_AND_ASSIGN(TestChangeProcessor);
139 };
140
141 class SyncChangeProcessorDelegate : public syncer::SyncChangeProcessor {
142 public:
143 explicit SyncChangeProcessorDelegate(SyncChangeProcessor* recipient)
144 : recipient_(recipient) {
145 EXPECT_TRUE(recipient_);
146 }
147 virtual ~SyncChangeProcessorDelegate() {}
148
149 // syncer::SyncChangeProcessor implementation.
150 virtual syncer::SyncError ProcessSyncChanges(
151 const tracked_objects::Location& from_here,
152 const SyncChangeList& change_list) OVERRIDE {
153 return recipient_->ProcessSyncChanges(from_here, change_list);
154 }
155
156 private:
157 // The recipient of all sync changes.
158 SyncChangeProcessor* recipient_;
159
160 DISALLOW_COPY_AND_ASSIGN(SyncChangeProcessorDelegate);
161 };
162
163 } // namespace
164
165 class ChromeNotifierServiceTest : public testing::Test {
166 public:
167 ChromeNotifierServiceTest()
168 : sync_processor_(new TestChangeProcessor),
169 sync_processor_delegate_(new SyncChangeProcessorDelegate(
170 sync_processor_.get())) {}
171 ~ChromeNotifierServiceTest() {}
172
173 // Methods from testing::Test.
174 virtual void SetUp() {}
175 virtual void TearDown() {}
176
177 TestChangeProcessor* processor() {
178 return static_cast<TestChangeProcessor*>(sync_processor_.get());
179 }
180
181 scoped_ptr<syncer::SyncChangeProcessor> PassProcessor() {
182 return sync_processor_delegate_.Pass();
183 }
184
185 SyncedNotification* CreateNotification(const std::string& message,
186 const std::string& app_id,
187 const std::string& coalescing_key,
188 const std::string& external_id) {
189 SyncData sync_data = CreateSyncData(message, app_id, coalescing_key,
190 external_id);
191 // Set enough fields in sync_data, including specifics, for our tests
192 // to pass.
193 return new SyncedNotification(sync_data);
194 }
195
196 // Helper to create syncer::SyncChange.
197 static SyncChange CreateSyncChange(
198 SyncChange::SyncChangeType type,
199 SyncedNotification* notification) {
200 // Take control of the notification to clean it up after we create data
201 // out of it.
202 scoped_ptr<SyncedNotification> scoped_notification(notification);
203 return SyncChange(
204 FROM_HERE,
205 type,
206 ChromeNotifierService::CreateSyncDataFromNotification(*notification));
207 }
208
209 // Helper to create syncer::SyncData.
210 static SyncData CreateSyncData(const std::string& message,
211 const std::string& app_id,
212 const std::string& coalescing_key,
213 const std::string& external_id) {
214 // CreateLocalData makes a copy of this, so this can safely live
215 // on the stack.
216 EntitySpecifics entity_specifics;
217
218 SyncedNotificationSpecifics* specifics =
219 entity_specifics.mutable_synced_notification();
220
221 specifics->mutable_coalesced_notification()->
222 mutable_render_info()->
223 mutable_layout()->
224 set_layout_type(
225 SyncedNotificationRenderInfo_Layout_LayoutType_TITLE_AND_SUBTEXT);
226
227 specifics->mutable_coalesced_notification()->
228 mutable_id()->
229 set_app_id(app_id);
230
231 specifics->mutable_coalesced_notification()->
232 mutable_id()->
233 set_coalescing_key(coalescing_key);
234
235 specifics->mutable_coalesced_notification()->
236 mutable_render_info()->
237 mutable_layout()->
238 mutable_title_and_subtext_data()->
239 set_title(message);
240
241 specifics->mutable_coalesced_notification()->
242 set_creation_time_msec(kFakeCreationTime);
243
244 specifics->mutable_coalesced_notification()->
245 add_notification();
246 specifics->mutable_coalesced_notification()->
247 mutable_notification(0)->set_external_id(external_id);
248
249 SyncData sync_data = SyncData::CreateLocalData(
250 "syncer::SYNCED_NOTIFICATIONS",
251 "ChromeNotifierServiceUnitTest",
252 entity_specifics);
253
254 return sync_data;
255 }
256
257 private:
258 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_;
259 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_delegate_;
260
261 DISALLOW_COPY_AND_ASSIGN(ChromeNotifierServiceTest);
262 };
263
264 // Create a Notification, convert it to SyncData and convert it back.
265 TEST_F(ChromeNotifierServiceTest, NotificationToSyncDataToNotification) {
266 // TODO(petewil): Add more properties to this test.
267 scoped_ptr<SyncedNotification> notification1(
268 CreateNotification("1", kAppId1, kCoalescingKey1, "11"));
269 SyncData sync_data =
270 ChromeNotifierService::CreateSyncDataFromNotification(*notification1);
271 scoped_ptr<SyncedNotification> notification2(
272 ChromeNotifierService::CreateNotificationFromSyncData(sync_data));
273 EXPECT_TRUE(notification2.get());
274 EXPECT_TRUE(notification1->Equals(*notification2));
275 }
276
277 // Model assocation: We have no local data, and no remote data.
278 TEST_F(ChromeNotifierServiceTest, ModelAssocBothEmpty) {
279 StubNotificationUIManager notification_manager;
280 ChromeNotifierService notifier(NULL, &notification_manager);
281
282 notifier.MergeDataAndStartSyncing(
283 SYNCED_NOTIFICATIONS,
284 SyncDataList(), // Empty.
285 PassProcessor(),
286 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock()));
287
288 EXPECT_EQ(0U, notifier.GetAllSyncData(SYNCED_NOTIFICATIONS).size());
289 EXPECT_EQ(0U, processor()->change_list_size());
290 }
291
292 // Process sync changes when there is no local data.
293 TEST_F(ChromeNotifierServiceTest, ProcessSyncChangesEmptyModel) {
294 // We initially have no data.
295 StubNotificationUIManager notification_manager;
296 ChromeNotifierService notifier(NULL, &notification_manager);
297
298 notifier.MergeDataAndStartSyncing(
299 SYNCED_NOTIFICATIONS,
300 SyncDataList(),
301 PassProcessor(),
302 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock()));
303
304 // Set up a bunch of ADDs.
305 SyncChangeList changes;
306 changes.push_back(CreateSyncChange(
307 SyncChange::ACTION_ADD, CreateNotification(
308 "1", kAppId1, kCoalescingKey1, "11")));
309 changes.push_back(CreateSyncChange(
310 SyncChange::ACTION_ADD, CreateNotification(
311 "2", kAppId2, kCoalescingKey2, "22")));
312 changes.push_back(CreateSyncChange(
313 SyncChange::ACTION_ADD, CreateNotification(
314 "3", kAppId3, kCoalescingKey3, "33")));
315
316 notifier.ProcessSyncChanges(FROM_HERE, changes);
317
318 EXPECT_EQ(3U, notifier.GetAllSyncData(SYNCED_NOTIFICATIONS).size());
319 // TODO(petewil): verify that the list entries have expected values to make
320 // this test more robust.
321 }
322
323 // Model has some notifications, some of them are local only. Sync has some
324 // notifications. No items match up.
325 TEST_F(ChromeNotifierServiceTest, LocalRemoteBothNonEmptyNoOverlap) {
326 StubNotificationUIManager notification_manager;
327 ChromeNotifierService notifier(NULL, &notification_manager);
328
329 // Create some local fake data.
330 scoped_ptr<SyncedNotification> n1(CreateNotification(
331 "1", kAppId1, kCoalescingKey1, "11"));
332 notifier.AddForTest(n1.Pass());
333 scoped_ptr<SyncedNotification> n2(CreateNotification(
334 "2", kAppId2, kCoalescingKey2, "22"));
335 notifier.AddForTest(n2.Pass());
336 scoped_ptr<SyncedNotification> n3(CreateNotification(
337 "3", kAppId3, kCoalescingKey3, "33"));
338 notifier.AddForTest(n3.Pass());
339
340 // Create some remote fake data.
341 SyncDataList initial_data;
342 initial_data.push_back(CreateSyncData("4", kAppId4, kCoalescingKey4, "44"));
343 initial_data.push_back(CreateSyncData("5", kAppId5, kCoalescingKey5, "55"));
344 initial_data.push_back(CreateSyncData("6", kAppId6, kCoalescingKey6, "66"));
345 initial_data.push_back(CreateSyncData("7", kAppId7, kCoalescingKey7, "77"));
346
347 // Merge the local and remote data.
348 notifier.MergeDataAndStartSyncing(
349 SYNCED_NOTIFICATIONS,
350 initial_data,
351 PassProcessor(),
352 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock()));
353
354 // Ensure the local store now has all local and remote notifications.
355 EXPECT_EQ(7U, notifier.GetAllSyncData(SYNCED_NOTIFICATIONS).size());
356 for (SyncDataList::const_iterator iter = initial_data.begin();
357 iter != initial_data.end(); ++iter) {
358 scoped_ptr<SyncedNotification> notification1(
359 ChromeNotifierService::CreateNotificationFromSyncData(*iter));
360 // TODO(petewil): Revisit this when we add version info to notifications.
361 const std::string& id = notification1->notification_id();
362 const SyncedNotification* notification2 = notifier.FindNotificationById(id);
363 EXPECT_TRUE(NULL != notification2);
364 EXPECT_TRUE(notification1->Equals(*notification2));
365 }
366 EXPECT_TRUE(notifier.FindNotificationById(kNotificationId1));
367 EXPECT_TRUE(notifier.FindNotificationById(kNotificationId2));
368 EXPECT_TRUE(notifier.FindNotificationById(kNotificationId3));
369
370 // Verify the changes made it up to the remote service.
371 EXPECT_EQ(3U, processor()->change_list_size());
372 EXPECT_TRUE(processor()->ContainsId(kNotificationId1));
373 EXPECT_EQ(SyncChange::ACTION_ADD, processor()->GetChangeById(
374 kNotificationId1).change_type());
375 EXPECT_FALSE(processor()->ContainsId(kNotificationId4));
376 EXPECT_TRUE(processor()->ContainsId(kNotificationId3));
377 EXPECT_EQ(SyncChange::ACTION_ADD, processor()->GetChangeById(
378 kNotificationId3).change_type());
379 }
380
381 // TODO(petewil): There are more tests to add, such as when an item in
382 // the local store matches up with one from the server, with and without
383 // merge conflicts.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698