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

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

Powered by Google App Engine
This is Rietveld 408576698