OLD | NEW |
---|---|
(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) {} | |
dcheng
2013/01/25 22:23:48
Do we need OVERRIDE annotations on virtual methods
Pete Williamson
2013/01/26 02:17:09
as I understand the OVERRIDE keyword, it merely gi
dcheng
2013/01/26 02:26:33
Some platforms enforce that the OVERRIDE keyword i
| |
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_.clear(); | |
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() { | |
172 return static_cast<TestChangeProcessor*>(sync_processor_.get()); | |
173 } | |
174 | |
175 scoped_ptr<SyncChangeProcessor> PassProcessor() { | |
176 return sync_processor_delegate_.Pass(); | |
177 } | |
178 | |
179 SyncedNotification* CreateNotification(const std::string& message, | |
180 const std::string& app_id, | |
181 const std::string& coalescing_key, | |
182 const std::string& external_id) { | |
183 SyncData sync_data = CreateSyncData(message, app_id, coalescing_key, | |
184 external_id); | |
185 // Set enough fields in sync_data, including specifics, for our tests | |
186 // to pass. | |
187 return new SyncedNotification(sync_data); | |
188 } | |
189 | |
190 // Helper to create syncer::SyncChange. | |
191 static SyncChange CreateSyncChange( | |
192 SyncChange::SyncChangeType type, | |
193 SyncedNotification* notification) { | |
194 // Take control of the notification to clean it up after we create data | |
195 // out of it. | |
196 scoped_ptr<SyncedNotification> scoped_notification(notification); | |
197 return SyncChange( | |
198 FROM_HERE, | |
199 type, | |
200 ChromeNotifierService::CreateSyncDataFromNotification(*notification)); | |
201 } | |
202 | |
203 // Helper to create syncer::SyncData. | |
204 static SyncData CreateSyncData(const std::string& message, | |
205 const std::string& app_id, | |
206 const std::string& coalescing_key, | |
207 const std::string& external_id) { | |
208 // CreateLocalData makes a copy of this, so this can safely live | |
209 // on the stack. | |
210 EntitySpecifics entity_specifics; | |
211 | |
212 SyncedNotificationSpecifics* specifics = | |
213 entity_specifics.mutable_synced_notification(); | |
214 | |
215 specifics->mutable_coalesced_notification()-> | |
216 mutable_render_info()-> | |
217 mutable_layout()-> | |
218 set_layout_type( | |
219 SyncedNotificationRenderInfo_Layout_LayoutType_TITLE_AND_SUBTEXT); | |
220 | |
221 specifics->mutable_coalesced_notification()-> | |
222 mutable_id()-> | |
223 set_app_id(app_id); | |
224 | |
225 specifics->mutable_coalesced_notification()-> | |
226 mutable_id()-> | |
227 set_coalescing_key(coalescing_key); | |
228 | |
229 specifics->mutable_coalesced_notification()-> | |
230 mutable_render_info()-> | |
231 mutable_layout()-> | |
232 mutable_title_and_subtext_data()-> | |
233 set_title(message); | |
234 | |
235 specifics->mutable_coalesced_notification()-> | |
236 set_creation_time_msec(kFakeCreationTime); | |
237 | |
238 specifics->mutable_coalesced_notification()-> | |
239 add_notification(); | |
240 specifics->mutable_coalesced_notification()-> | |
241 mutable_notification(0)->set_external_id(external_id); | |
242 | |
243 SyncData sync_data = SyncData::CreateLocalData( | |
244 "syncer::SYNCED_NOTIFICATIONS", | |
245 "ChromeNotifierServiceUnitTest", | |
246 entity_specifics); | |
247 | |
248 return sync_data; | |
249 } | |
250 | |
251 private: | |
252 scoped_ptr<SyncChangeProcessor> sync_processor_; | |
253 scoped_ptr<SyncChangeProcessor> sync_processor_delegate_; | |
254 | |
255 DISALLOW_COPY_AND_ASSIGN(ChromeNotifierServiceTest); | |
256 }; | |
257 | |
258 // Create a Notification, convert it to SyncData and convert it back. | |
259 TEST_F(ChromeNotifierServiceTest, NotificationToSyncDataToNotification) { | |
260 // TODO(petewil): Add more properties to this test. | |
261 scoped_ptr<SyncedNotification> notification1( | |
262 CreateNotification("1", kAppId1, kCoalescingKey1, "11")); | |
263 SyncData sync_data = | |
264 ChromeNotifierService::CreateSyncDataFromNotification(*notification1); | |
265 scoped_ptr<SyncedNotification> notification2( | |
266 ChromeNotifierService::CreateNotificationFromSyncData(sync_data)); | |
267 EXPECT_TRUE(notification2.get()); | |
268 EXPECT_TRUE(notification1->Equals(*notification2)); | |
269 } | |
270 | |
271 // Model assocation: We have no local data, and no remote data. | |
272 TEST_F(ChromeNotifierServiceTest, ModelAssocBothEmpty) { | |
273 StubNotificationUIManager notification_manager; | |
274 ChromeNotifierService notifier(NULL, ¬ification_manager); | |
275 | |
276 notifier.MergeDataAndStartSyncing( | |
277 SYNCED_NOTIFICATIONS, | |
278 SyncDataList(), // Empty. | |
279 PassProcessor(), | |
280 scoped_ptr<SyncErrorFactory>(new SyncErrorFactoryMock())); | |
281 | |
282 EXPECT_EQ(0U, notifier.GetAllSyncData(SYNCED_NOTIFICATIONS).size()); | |
283 EXPECT_EQ(0U, processor()->change_list_size()); | |
284 } | |
285 | |
286 // Process sync changes when there is no local data. | |
287 TEST_F(ChromeNotifierServiceTest, ProcessSyncChangesEmptyModel) { | |
288 // We initially have no data. | |
289 StubNotificationUIManager notification_manager; | |
290 ChromeNotifierService notifier(NULL, ¬ification_manager); | |
291 | |
292 notifier.MergeDataAndStartSyncing( | |
293 SYNCED_NOTIFICATIONS, | |
294 SyncDataList(), | |
295 PassProcessor(), | |
296 scoped_ptr<SyncErrorFactory>(new SyncErrorFactoryMock())); | |
297 | |
298 // Set up a bunch of ADDs. | |
299 SyncChangeList changes; | |
300 changes.push_back(CreateSyncChange( | |
301 SyncChange::ACTION_ADD, CreateNotification( | |
302 "1", kAppId1, kCoalescingKey1, "11"))); | |
303 changes.push_back(CreateSyncChange( | |
304 SyncChange::ACTION_ADD, CreateNotification( | |
305 "2", kAppId2, kCoalescingKey2, "22"))); | |
306 changes.push_back(CreateSyncChange( | |
307 SyncChange::ACTION_ADD, CreateNotification( | |
308 "3", kAppId3, kCoalescingKey3, "33"))); | |
309 | |
310 notifier.ProcessSyncChanges(FROM_HERE, changes); | |
311 | |
312 EXPECT_EQ(3U, notifier.GetAllSyncData(SYNCED_NOTIFICATIONS).size()); | |
313 // TODO(petewil): verify that the list entries have expected values to make | |
314 // this test more robust. | |
315 } | |
316 | |
317 // Model has some notifications, some of them are local only. Sync has some | |
318 // notifications. No items match up. | |
319 TEST_F(ChromeNotifierServiceTest, LocalRemoteBothNonEmptyNoOverlap) { | |
320 StubNotificationUIManager notification_manager; | |
321 ChromeNotifierService notifier(NULL, ¬ification_manager); | |
322 | |
323 // Create some local fake data. | |
324 scoped_ptr<SyncedNotification> n1(CreateNotification( | |
325 "1", kAppId1, kCoalescingKey1, "11")); | |
326 notifier.Add(n1.Pass()); | |
327 scoped_ptr<SyncedNotification> n2(CreateNotification( | |
328 "2", kAppId2, kCoalescingKey2, "22")); | |
329 notifier.Add(n2.Pass()); | |
330 scoped_ptr<SyncedNotification> n3(CreateNotification( | |
331 "3", kAppId3, kCoalescingKey3, "33")); | |
332 notifier.Add(n3.Pass()); | |
333 | |
334 // Create some remote fake data. | |
335 SyncDataList initial_data; | |
336 initial_data.push_back(CreateSyncData("4", kAppId4, kCoalescingKey4, "44")); | |
337 initial_data.push_back(CreateSyncData("5", kAppId5, kCoalescingKey5, "55")); | |
338 initial_data.push_back(CreateSyncData("6", kAppId6, kCoalescingKey6, "66")); | |
339 initial_data.push_back(CreateSyncData("7", kAppId7, kCoalescingKey7, "77")); | |
340 | |
341 // Merge the local and remote data. | |
342 notifier.MergeDataAndStartSyncing( | |
343 SYNCED_NOTIFICATIONS, | |
344 initial_data, | |
345 PassProcessor(), | |
346 scoped_ptr<SyncErrorFactory>(new SyncErrorFactoryMock())); | |
347 | |
348 // Ensure the local store now has all local and remote notifications. | |
349 EXPECT_EQ(7U, notifier.GetAllSyncData(SYNCED_NOTIFICATIONS).size()); | |
350 for (SyncDataList::const_iterator iter = initial_data.begin(); | |
351 iter != initial_data.end(); ++iter) { | |
352 scoped_ptr<SyncedNotification> notification1( | |
353 ChromeNotifierService::CreateNotificationFromSyncData(*iter)); | |
354 // TODO(petewil): Revisit this when we add version info to notifications. | |
355 const std::string& id = notification1->notification_id(); | |
356 const SyncedNotification* notification2 = notifier.FindNotificationById(id); | |
357 EXPECT_TRUE(NULL != notification2); | |
358 EXPECT_TRUE(notification1->Equals(*notification2)); | |
359 } | |
360 EXPECT_TRUE(notifier.FindNotificationById(kNotificationId1)); | |
361 EXPECT_TRUE(notifier.FindNotificationById(kNotificationId2)); | |
362 EXPECT_TRUE(notifier.FindNotificationById(kNotificationId3)); | |
363 | |
364 // Verify the changes made it up to the remote service. | |
365 EXPECT_EQ(3U, processor()->change_list_size()); | |
366 EXPECT_TRUE(processor()->ContainsId(kNotificationId1)); | |
367 EXPECT_EQ(SyncChange::ACTION_ADD, processor()->GetChangeById( | |
368 kNotificationId1).change_type()); | |
369 EXPECT_FALSE(processor()->ContainsId(kNotificationId4)); | |
370 EXPECT_TRUE(processor()->ContainsId(kNotificationId3)); | |
371 EXPECT_EQ(SyncChange::ACTION_ADD, processor()->GetChangeById( | |
372 kNotificationId3).change_type()); | |
373 } | |
374 | |
375 // TODO(petewil): There are more tests to add, such as when an item in | |
376 // the local store matches up with one from the server, with and without | |
377 // merge conflicts. | |
OLD | NEW |