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