OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <map> | 5 #include <map> |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/gtest_prod_util.h" | 8 #include "base/gtest_prod_util.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "chrome/browser/notifications/notification.h" | 13 #include "chrome/browser/notifications/notification.h" |
14 #include "chrome/browser/notifications/notification_test_util.h" | 14 #include "chrome/browser/notifications/notification_test_util.h" |
15 #include "chrome/browser/notifications/notification_ui_manager.h" | 15 #include "chrome/browser/notifications/notification_ui_manager.h" |
16 #include "chrome/browser/notifications/sync_notifier/chrome_notifier_service.h" | 16 #include "chrome/browser/notifications/sync_notifier/chrome_notifier_service.h" |
17 #include "chrome/browser/notifications/sync_notifier/sync_notifier_test_utils.h" | 17 #include "chrome/browser/notifications/sync_notifier/sync_notifier_test_utils.h" |
18 #include "chrome/browser/notifications/sync_notifier/synced_notification.h" | 18 #include "chrome/browser/notifications/sync_notifier/synced_notification.h" |
19 #include "chrome/browser/notifications/sync_notifier/synced_notification_app_inf o.h" | |
20 #include "chrome/browser/notifications/sync_notifier/synced_notification_app_inf o_service.h" | |
21 #include "chrome/browser/notifications/sync_notifier/synced_notification_app_inf o_service_factory.h" | |
19 #include "chrome/browser/prefs/browser_prefs.h" | 22 #include "chrome/browser/prefs/browser_prefs.h" |
20 #include "chrome/browser/profiles/profile.h" | 23 #include "chrome/browser/profiles/profile.h" |
21 #include "chrome/common/chrome_switches.h" | 24 #include "chrome/common/chrome_switches.h" |
22 #include "chrome/common/pref_names.h" | 25 #include "chrome/common/pref_names.h" |
23 #include "chrome/test/base/testing_pref_service_syncable.h" | 26 #include "chrome/test/base/testing_pref_service_syncable.h" |
24 #include "chrome/test/base/testing_profile.h" | 27 #include "chrome/test/base/testing_profile.h" |
25 #include "components/user_prefs/pref_registry_syncable.h" | 28 #include "components/user_prefs/pref_registry_syncable.h" |
26 #include "content/public/test/test_browser_thread_bundle.h" | 29 #include "content/public/test/test_browser_thread_bundle.h" |
27 #include "sync/api/sync_change.h" | 30 #include "sync/api/sync_change.h" |
28 #include "sync/api/sync_change_processor.h" | 31 #include "sync/api/sync_change_processor.h" |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
117 CommandLine::ForCurrentProcess()->AppendSwitch( | 120 CommandLine::ForCurrentProcess()->AppendSwitch( |
118 switches::kEnableSyncSyncedNotifications); | 121 switches::kEnableSyncSyncedNotifications); |
119 | 122 |
120 // Prevent test code from trying to go to the network. | 123 // Prevent test code from trying to go to the network. |
121 ChromeNotifierService::set_avoid_bitmap_fetching_for_test(true); | 124 ChromeNotifierService::set_avoid_bitmap_fetching_for_test(true); |
122 notification_manager_.reset(new StubNotificationUIManager(GURL( | 125 notification_manager_.reset(new StubNotificationUIManager(GURL( |
123 kSyncedNotificationsWelcomeOrigin))); | 126 kSyncedNotificationsWelcomeOrigin))); |
124 | 127 |
125 // Set up a profile for the unit tests to use. | 128 // Set up a profile for the unit tests to use. |
126 profile_.reset(new TestingProfile()); | 129 profile_.reset(new TestingProfile()); |
130 | |
131 // Set up the testing SyncedNotificationAppInfoService with some test data. | |
132 AddTestingAppInfos(); | |
127 } | 133 } |
128 | 134 |
129 virtual void TearDown() { | 135 virtual void TearDown() { |
130 notification_manager_.reset(); | 136 notification_manager_.reset(); |
131 } | 137 } |
132 | 138 |
133 StubNotificationUIManager* notification_manager() { | 139 StubNotificationUIManager* notification_manager() { |
134 return notification_manager_.get(); | 140 return notification_manager_.get(); |
135 } | 141 } |
136 | 142 |
(...skipping 26 matching lines...) Expand all Loading... | |
163 SyncedNotification* notification) { | 169 SyncedNotification* notification) { |
164 // Take control of the notification to clean it up after we create data | 170 // Take control of the notification to clean it up after we create data |
165 // out of it. | 171 // out of it. |
166 scoped_ptr<SyncedNotification> scoped_notification(notification); | 172 scoped_ptr<SyncedNotification> scoped_notification(notification); |
167 return SyncChange( | 173 return SyncChange( |
168 FROM_HERE, | 174 FROM_HERE, |
169 type, | 175 type, |
170 ChromeNotifierService::CreateSyncDataFromNotification(*notification)); | 176 ChromeNotifierService::CreateSyncDataFromNotification(*notification)); |
171 } | 177 } |
172 | 178 |
179 void AddTestingAppInfos() { | |
180 // Get the SyncedNotificationAppInfoService from the browser object. | |
181 SyncedNotificationAppInfoService* synced_notification_app_info_service = | |
182 SyncedNotificationAppInfoServiceFactory::GetForProfile( | |
183 profile_.get(), Profile::EXPLICIT_ACCESS); | |
184 | |
185 // Create a notification to add. | |
186 // The sending_service_infos_ list will take ownership of this pointer. | |
187 scoped_ptr<SyncedNotificationAppInfo> test_service1( | |
188 new SyncedNotificationAppInfo(profile_.get(), | |
189 std::string(kSendingService1Name), | |
190 synced_notification_app_info_service)); | |
191 | |
192 // Add some App IDs. | |
193 test_service1->AddAppId(kAppId1); | |
194 test_service1->AddAppId(kAppId2); | |
195 | |
196 // Set this icon GURL. | |
dewittj
2014/03/24 17:35:50
fix comment plural
Pete Williamson
2014/03/25 00:08:37
Done.
| |
197 test_service1->SetSettingsURLs(GURL(kTestIconUrl), GURL()); | |
198 | |
199 // Call AddForTest. | |
200 synced_notification_app_info_service->AddForTest(test_service1.Pass()); | |
201 } | |
202 | |
173 protected: | 203 protected: |
174 scoped_ptr<TestingProfile> profile_; | 204 scoped_ptr<TestingProfile> profile_; |
175 | 205 |
176 private: | 206 private: |
177 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_; | 207 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_; |
178 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_wrapper_; | 208 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_wrapper_; |
179 scoped_ptr<StubNotificationUIManager> notification_manager_; | 209 scoped_ptr<StubNotificationUIManager> notification_manager_; |
180 content::TestBrowserThreadBundle thread_bundle_; | 210 content::TestBrowserThreadBundle thread_bundle_; |
181 | 211 |
182 DISALLOW_COPY_AND_ASSIGN(ChromeNotifierServiceTest); | 212 DISALLOW_COPY_AND_ASSIGN(ChromeNotifierServiceTest); |
183 }; | 213 }; |
184 | 214 |
185 // TODO(petewil): Add more tests as I add functionalty. Tests are based on | |
186 // chrome/browser/extensions/app_notification_manager_sync_unittest.cc | |
187 | |
188 // Create a Notification, convert it to SyncData and convert it back. | 215 // Create a Notification, convert it to SyncData and convert it back. |
189 TEST_F(ChromeNotifierServiceTest, NotificationToSyncDataToNotification) { | 216 TEST_F(ChromeNotifierServiceTest, NotificationToSyncDataToNotification) { |
190 scoped_ptr<SyncedNotification> notification1( | 217 scoped_ptr<SyncedNotification> notification1( |
191 CreateNotification(kTitle1, kText1, kIconUrl1, kImageUrl1, kAppId1, | 218 CreateNotification(kTitle1, kText1, kIconUrl1, kImageUrl1, kAppId1, |
192 kKey1, kUnread)); | 219 kKey1, kUnread)); |
193 SyncData sync_data = | 220 SyncData sync_data = |
194 ChromeNotifierService::CreateSyncDataFromNotification(*notification1); | 221 ChromeNotifierService::CreateSyncDataFromNotification(*notification1); |
195 scoped_ptr<SyncedNotification> notification2( | 222 scoped_ptr<SyncedNotification> notification2( |
196 ChromeNotifierService::CreateNotificationFromSyncData(sync_data)); | 223 ChromeNotifierService::CreateNotificationFromSyncData(sync_data)); |
197 EXPECT_TRUE(notification2.get()); | 224 EXPECT_TRUE(notification2.get()); |
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
526 | 553 |
527 // Ensure no new data will be sent to the remote store for notification1. | 554 // Ensure no new data will be sent to the remote store for notification1. |
528 EXPECT_EQ(0U, processor()->change_list_size()); | 555 EXPECT_EQ(0U, processor()->change_list_size()); |
529 EXPECT_FALSE(processor()->ContainsId(kKey1)); | 556 EXPECT_FALSE(processor()->ContainsId(kKey1)); |
530 } | 557 } |
531 | 558 |
532 TEST_F(ChromeNotifierServiceTest, ServiceEnabledTest) { | 559 TEST_F(ChromeNotifierServiceTest, ServiceEnabledTest) { |
533 ChromeNotifierService notifier(profile_.get(), notification_manager()); | 560 ChromeNotifierService notifier(profile_.get(), notification_manager()); |
534 std::set<std::string>::iterator iter; | 561 std::set<std::string>::iterator iter; |
535 std::string first_synced_notification_service_id( | 562 std::string first_synced_notification_service_id( |
536 kFirstSyncedNotificationServiceId); | 563 kSendingService1Name); |
537 | 564 |
538 // Create some local fake data. | 565 // Create some local fake data. |
539 scoped_ptr<SyncedNotification> n1(CreateNotification( | 566 scoped_ptr<SyncedNotification> n1(CreateNotification( |
540 kTitle1, kText1, kIconUrl1, kImageUrl1, kAppId1, kKey1, kUnread)); | 567 kTitle1, kText1, kIconUrl1, kImageUrl1, kAppId1, kKey1, kUnread)); |
541 notifier.AddForTest(n1.Pass()); | 568 notifier.AddForTest(n1.Pass()); |
542 | 569 |
543 // Enable the service and ensure the service is in the list. | 570 // Enable the service and ensure the service is in the list. |
544 // Initially the service starts in the disabled state. | 571 // Initially the service starts in the disabled state. |
545 notifier.OnSyncedNotificationServiceEnabled(kFirstSyncedNotificationServiceId, | 572 notifier.OnSyncedNotificationServiceEnabled(kSendingService1Name, |
546 true); | 573 true); |
547 iter = find(notifier.enabled_sending_services_.begin(), | 574 iter = find(notifier.enabled_sending_services_.begin(), |
548 notifier.enabled_sending_services_.end(), | 575 notifier.enabled_sending_services_.end(), |
549 first_synced_notification_service_id); | 576 first_synced_notification_service_id); |
550 EXPECT_NE(notifier.enabled_sending_services_.end(), iter); | 577 EXPECT_NE(notifier.enabled_sending_services_.end(), iter); |
551 // TODO(petewil): Verify Display gets called too. | 578 // TODO(petewil): Verify Display gets called too. |
552 | 579 |
553 // Disable the service and ensure it is gone from the list and the | 580 // Disable the service and ensure it is gone from the list and the |
554 // notification_manager. | 581 // notification_manager. |
555 notifier.OnSyncedNotificationServiceEnabled(kFirstSyncedNotificationServiceId, | 582 notifier.OnSyncedNotificationServiceEnabled(kSendingService1Name, |
556 false); | 583 false); |
557 iter = find(notifier.enabled_sending_services_.begin(), | 584 iter = find(notifier.enabled_sending_services_.begin(), |
558 notifier.enabled_sending_services_.end(), | 585 notifier.enabled_sending_services_.end(), |
559 first_synced_notification_service_id); | 586 first_synced_notification_service_id); |
560 EXPECT_EQ(notifier.enabled_sending_services_.end(), iter); | 587 EXPECT_EQ(notifier.enabled_sending_services_.end(), iter); |
561 EXPECT_EQ(notification_manager()->dismissed_id(), std::string(kKey1)); | 588 EXPECT_EQ(notification_manager()->dismissed_id(), std::string(kKey1)); |
562 | 589 |
563 } | 590 } |
564 | 591 |
565 // http://crbug.com/341326 | 592 // http://crbug.com/341326 |
(...skipping 13 matching lines...) Expand all Loading... | |
579 EXPECT_EQ(0U, notifier.GetAllSyncData(SYNCED_NOTIFICATIONS).size()); | 606 EXPECT_EQ(0U, notifier.GetAllSyncData(SYNCED_NOTIFICATIONS).size()); |
580 | 607 |
581 // Set up an ADD. | 608 // Set up an ADD. |
582 SyncChangeList changes; | 609 SyncChangeList changes; |
583 changes.push_back( | 610 changes.push_back( |
584 CreateSyncChange(SyncChange::ACTION_ADD, | 611 CreateSyncChange(SyncChange::ACTION_ADD, |
585 CreateNotification(kTitle1, | 612 CreateNotification(kTitle1, |
586 kText1, | 613 kText1, |
587 kIconUrl1, | 614 kIconUrl1, |
588 kImageUrl1, | 615 kImageUrl1, |
589 kFirstSyncedNotificationServiceId, | 616 kAppId1, |
590 kKey1, | 617 kKey1, |
591 kUnread))); | 618 kUnread))); |
592 | 619 |
593 notifier.ProcessSyncChanges(FROM_HERE, changes); | 620 notifier.ProcessSyncChanges(FROM_HERE, changes); |
594 | 621 |
595 EXPECT_EQ(1U, notifier.GetAllSyncData(SYNCED_NOTIFICATIONS).size()); | 622 EXPECT_EQ(1U, notifier.GetAllSyncData(SYNCED_NOTIFICATIONS).size()); |
596 | 623 |
597 // Verify that the first synced notification service is enabled in memory. | 624 // Verify that the first synced notification service is enabled in memory. |
598 std::set<std::string>::iterator iter; | 625 std::set<std::string>::iterator iter; |
599 std::string first_notification_service_id(kFirstSyncedNotificationServiceId); | 626 std::string first_notification_service_id(kSendingService1Name); |
600 iter = find(notifier.enabled_sending_services_.begin(), | 627 iter = find(notifier.enabled_sending_services_.begin(), |
601 notifier.enabled_sending_services_.end(), | 628 notifier.enabled_sending_services_.end(), |
602 first_notification_service_id); | 629 first_notification_service_id); |
603 | 630 |
604 EXPECT_NE(notifier.enabled_sending_services_.end(), iter); | 631 EXPECT_NE(notifier.enabled_sending_services_.end(), iter); |
605 | 632 |
606 // We should have gotten the synced notification and a welcome notification. | 633 // We should have gotten the synced notification and a welcome notification. |
607 EXPECT_EQ(2U, notification_manager()->added_notifications()); | 634 EXPECT_EQ(2U, notification_manager()->added_notifications()); |
608 EXPECT_TRUE(notification_manager()->welcomed()); | 635 EXPECT_TRUE(notification_manager()->welcomed()); |
609 | 636 |
610 changes.clear(); | 637 changes.clear(); |
611 changes.push_back( | 638 changes.push_back( |
612 CreateSyncChange(SyncChange::ACTION_ADD, | 639 CreateSyncChange(SyncChange::ACTION_ADD, |
613 CreateNotification(kTitle2, | 640 CreateNotification(kTitle2, |
614 kText2, | 641 kText2, |
615 kIconUrl2, | 642 kIconUrl2, |
616 kImageUrl2, | 643 kImageUrl2, |
617 kFirstSyncedNotificationServiceId, | 644 kAppId1, |
618 kKey2, | 645 kKey2, |
619 kUnread))); | 646 kUnread))); |
620 notifier.ProcessSyncChanges(FROM_HERE, changes); | 647 notifier.ProcessSyncChanges(FROM_HERE, changes); |
621 | 648 |
622 // But adding another notification should not cause another welcome. | 649 // But adding another notification should not cause another welcome. |
623 EXPECT_EQ(3U, notification_manager()->added_notifications()); | 650 EXPECT_EQ(3U, notification_manager()->added_notifications()); |
624 } | 651 } |
625 | 652 |
626 TEST_F(ChromeNotifierServiceTest, CheckInitializedServicesTest) { | 653 TEST_F(ChromeNotifierServiceTest, CheckInitializedServicesTest) { |
627 // This test will see if we get a new sending service after the first | 654 // This test will see if we get a new sending service after the first |
628 // notification for that service. | 655 // notification for that service. |
629 ChromeNotifierService notifier(profile_.get(), notification_manager()); | 656 ChromeNotifierService notifier(profile_.get(), notification_manager()); |
630 notifier.set_avoid_bitmap_fetching_for_test(true); | 657 notifier.set_avoid_bitmap_fetching_for_test(true); |
631 | 658 |
632 // Initialize but do not enable the sending service. | 659 // Initialize but do not enable the sending service. |
633 notifier.initialized_sending_services_.insert( | 660 notifier.initialized_sending_services_.insert(kSendingService1Name); |
634 kFirstSyncedNotificationServiceId); | |
635 ASSERT_EQ(0U, notifier.enabled_sending_services_.size()); | 661 ASSERT_EQ(0U, notifier.enabled_sending_services_.size()); |
636 ASSERT_EQ(1U, notifier.initialized_sending_services_.size()); | 662 ASSERT_EQ(1U, notifier.initialized_sending_services_.size()); |
637 | 663 |
638 // We initially have no data. | 664 // We initially have no data. |
639 EXPECT_EQ(0U, notifier.enabled_sending_services_.size()); | 665 EXPECT_EQ(0U, notifier.enabled_sending_services_.size()); |
640 EXPECT_EQ(0U, notifier.GetAllSyncData(SYNCED_NOTIFICATIONS).size()); | 666 EXPECT_EQ(0U, notifier.GetAllSyncData(SYNCED_NOTIFICATIONS).size()); |
641 | 667 |
642 // Set up an ADD. | 668 // Set up an ADD. |
643 std::string first_synced_notification_service_id( | 669 std::string first_synced_notification_service_id(kSendingService1Name); |
644 kFirstSyncedNotificationServiceId); | |
645 | 670 |
646 SyncChangeList changes; | 671 SyncChangeList changes; |
647 changes.push_back( | 672 changes.push_back( |
648 CreateSyncChange(SyncChange::ACTION_ADD, | 673 CreateSyncChange(SyncChange::ACTION_ADD, |
649 CreateNotification(kTitle1, | 674 CreateNotification(kTitle1, |
650 kText1, | 675 kText1, |
651 kIconUrl1, | 676 kIconUrl1, |
652 kImageUrl1, | 677 kImageUrl1, |
653 kFirstSyncedNotificationServiceId, | 678 kAppId1, |
654 kKey1, | 679 kKey1, |
655 kUnread))); | 680 kUnread))); |
656 | 681 |
657 notifier.ProcessSyncChanges(FROM_HERE, changes); | 682 notifier.ProcessSyncChanges(FROM_HERE, changes); |
658 | 683 |
659 // Since we added to |initialized_sending_services_| before receiving the | 684 // Since we added to |initialized_sending_services_| before receiving the |
660 // synced notification, we should not have enabled this service while | 685 // synced notification, we should not have enabled this service while |
661 // processing the sync change. | 686 // processing the sync change. |
662 EXPECT_EQ(0U, notifier.enabled_sending_services_.size()); | 687 EXPECT_EQ(0U, notifier.enabled_sending_services_.size()); |
663 EXPECT_EQ(0U, notification_manager()->added_notifications()); | 688 EXPECT_EQ(0U, notification_manager()->added_notifications()); |
664 } | 689 } |
665 | 690 |
666 TEST_F(ChromeNotifierServiceTest, CheckFindAppInfo) { | 691 TEST_F(ChromeNotifierServiceTest, SetAddedAppIdsTest) { |
667 ChromeNotifierService notifier(profile_.get(), notification_manager()); | 692 ChromeNotifierService notifier(profile_.get(), notification_manager()); |
668 notifier.set_avoid_bitmap_fetching_for_test(true); | 693 notifier.set_avoid_bitmap_fetching_for_test(true); |
669 | 694 |
670 SyncedNotificationAppInfoTemp* app_info = | 695 // Add some notifications to our notification list. |
671 notifier.FindAppInfo(kFirstSyncedNotificationServiceId); | 696 scoped_ptr<SyncedNotification> n1(CreateNotification( |
672 EXPECT_TRUE(app_info != NULL); | 697 kTitle1, kText1, kIconUrl1, kImageUrl1, kAppId1, kKey1, kUnread)); |
698 notifier.AddForTest(n1.Pass()); | |
699 | |
700 EXPECT_EQ(static_cast<size_t>(0), | |
701 notification_manager()->added_notifications()); | |
702 | |
703 // Call SetAddedAppIds. | |
704 std::vector<std::string> added_app_ids; | |
705 added_app_ids.push_back(std::string(kAppId1)); | |
706 notifier.SetAddedAppIds(added_app_ids); | |
707 | |
708 // Verify the notification was added by the notification_manager. | |
709 // We see one welcome notification and one new notification. | |
710 EXPECT_EQ(static_cast<size_t>(2), | |
711 notification_manager()->added_notifications()); | |
673 } | 712 } |
674 | 713 |
714 TEST_F(ChromeNotifierServiceTest, SetRemovedAppIdsTest) { | |
715 ChromeNotifierService notifier(profile_.get(), notification_manager()); | |
716 notifier.set_avoid_bitmap_fetching_for_test(true); | |
717 | |
718 // Add some notifications to our notification list. | |
719 scoped_ptr<SyncedNotification> n1(CreateNotification( | |
720 kTitle1, kText1, kIconUrl1, kImageUrl1, kAppId1, kKey1, kUnread)); | |
721 notifier.AddForTest(n1.Pass()); | |
722 | |
723 // Call SetRemovedAppIds. | |
724 std::vector<std::string> removed_app_ids; | |
725 removed_app_ids.push_back(std::string(kAppId1)); | |
726 notifier.SetRemovedAppIds(removed_app_ids); | |
727 | |
728 // Verify the notification was "removed" in the notification manager. | |
729 EXPECT_EQ(std::string(kKey1), notification_manager()->dismissed_id()); | |
730 } | |
731 | |
732 // TODO(petewil): Add a test that we do *not* get a welcome dialog unless we | |
733 // have a valid app info for the notification. | |
734 | |
675 } // namespace notifier | 735 } // namespace notifier |
OLD | NEW |