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

Side by Side Diff: ui/message_center/message_center_impl_unittest.cc

Issue 288033014: Remove Notifications Associated with an Extension When the Extension is Unchecked from the Message … (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix Unit Test That Left Dangling Pointer Created 6 years, 7 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 | Annotate | Revision Log
« no previous file with comments | « ui/message_center/message_center_impl.cc ('k') | ui/message_center/notifier_settings.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "ui/message_center/message_center_impl.h" 5 #include "ui/message_center/message_center_impl.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
11 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "ui/gfx/canvas.h" 12 #include "ui/gfx/canvas.h"
13 #include "ui/gfx/size.h" 13 #include "ui/gfx/size.h"
14 #include "ui/message_center/message_center.h" 14 #include "ui/message_center/message_center.h"
15 #include "ui/message_center/message_center_types.h" 15 #include "ui/message_center/message_center_types.h"
16 #include "ui/message_center/notification_blocker.h" 16 #include "ui/message_center/notification_blocker.h"
17 #include "ui/message_center/notification_types.h" 17 #include "ui/message_center/notification_types.h"
18 #include "ui/message_center/notifier_settings.h"
18 19
19 using base::UTF8ToUTF16; 20 using base::UTF8ToUTF16;
20 21
21 namespace message_center { 22 namespace message_center {
22 namespace { 23 namespace {
23 24
24 class MessageCenterImplTest : public testing::Test, 25 class MessageCenterImplTest : public testing::Test,
25 public MessageCenterObserver { 26 public MessageCenterObserver {
26 public: 27 public:
27 MessageCenterImplTest() {} 28 MessageCenterImplTest() {}
28 29
29 virtual void SetUp() OVERRIDE { 30 virtual void SetUp() OVERRIDE {
30 MessageCenter::Initialize(); 31 MessageCenter::Initialize();
31 message_center_ = MessageCenter::Get(); 32 message_center_ = MessageCenter::Get();
32 loop_.reset(new base::MessageLoop); 33 loop_.reset(new base::MessageLoop);
33 run_loop_.reset(new base::RunLoop()); 34 run_loop_.reset(new base::RunLoop());
34 closure_ = run_loop_->QuitClosure(); 35 closure_ = run_loop_->QuitClosure();
35 } 36 }
36 37
37 virtual void TearDown() OVERRIDE { 38 virtual void TearDown() OVERRIDE {
38 run_loop_.reset(); 39 run_loop_.reset();
39 loop_.reset(); 40 loop_.reset();
40 message_center_ = NULL; 41 message_center_ = NULL;
41 MessageCenter::Shutdown(); 42 MessageCenter::Shutdown();
42 } 43 }
43 44
44 MessageCenter* message_center() const { return message_center_; } 45 MessageCenter* message_center() const { return message_center_; }
46 NotifierSettingsObserver* notifier_settings_observer() const {
47 return static_cast<NotifierSettingsObserver*>(message_center_impl());
48 }
49 MessageCenterImpl* message_center_impl() const {
50 return reinterpret_cast<MessageCenterImpl*>(message_center_);
51 }
52
45 base::RunLoop* run_loop() const { return run_loop_.get(); } 53 base::RunLoop* run_loop() const { return run_loop_.get(); }
46 base::Closure closure() const { return closure_; } 54 base::Closure closure() const { return closure_; }
47 55
48 protected: 56 protected:
49 Notification* CreateSimpleNotification(const std::string& id) { 57 Notification* CreateSimpleNotification(const std::string& id) {
50 return CreateNotification(id, NOTIFICATION_TYPE_SIMPLE); 58 return CreateNotificationWithNotifierId(
59 id,
60 "app1",
61 NOTIFICATION_TYPE_SIMPLE);
62 }
63
64 Notification* CreateSimpleNotificationWithNotifierId(
65 const std::string& id, const std::string& notifier_id) {
66 return CreateNotificationWithNotifierId(
67 id,
68 notifier_id,
69 NOTIFICATION_TYPE_SIMPLE);
51 } 70 }
52 71
53 Notification* CreateNotification(const std::string& id, 72 Notification* CreateNotification(const std::string& id,
54 message_center::NotificationType type) { 73 message_center::NotificationType type) {
74 return CreateNotificationWithNotifierId(id, "app1", type);
75 }
76
77 Notification* CreateNotificationWithNotifierId(
78 const std::string& id,
79 const std::string& notifier_id,
80 message_center::NotificationType type) {
55 RichNotificationData optional_fields; 81 RichNotificationData optional_fields;
56 optional_fields.buttons.push_back(ButtonInfo(UTF8ToUTF16("foo"))); 82 optional_fields.buttons.push_back(ButtonInfo(UTF8ToUTF16("foo")));
57 optional_fields.buttons.push_back(ButtonInfo(UTF8ToUTF16("foo"))); 83 optional_fields.buttons.push_back(ButtonInfo(UTF8ToUTF16("foo")));
58 return new Notification(type, 84 return new Notification(type,
59 id, 85 id,
60 UTF8ToUTF16("title"), 86 UTF8ToUTF16("title"),
61 UTF8ToUTF16(id), 87 UTF8ToUTF16(id),
62 gfx::Image() /* icon */, 88 gfx::Image() /* icon */,
63 base::string16() /* display_source */, 89 base::string16() /* display_source */,
64 NotifierId(NotifierId::APPLICATION, "app1"), 90 NotifierId(NotifierId::APPLICATION, notifier_id),
65 optional_fields, 91 optional_fields,
66 NULL); 92 NULL);
67 } 93 }
68 94
95
69 private: 96 private:
70 MessageCenter* message_center_; 97 MessageCenter* message_center_;
71 scoped_ptr<base::MessageLoop> loop_; 98 scoped_ptr<base::MessageLoop> loop_;
72 scoped_ptr<base::RunLoop> run_loop_; 99 scoped_ptr<base::RunLoop> run_loop_;
73 base::Closure closure_; 100 base::Closure closure_;
74 101
75 DISALLOW_COPY_AND_ASSIGN(MessageCenterImplTest); 102 DISALLOW_COPY_AND_ASSIGN(MessageCenterImplTest);
76 }; 103 };
77 104
78 class ToggledNotificationBlocker : public NotificationBlocker { 105 class ToggledNotificationBlocker : public NotificationBlocker {
(...skipping 675 matching lines...) Expand 10 before | Expand all | Expand 10 after
754 notifications.begin(); iter != notifications.end(); ++iter) { 781 notifications.begin(); iter != notifications.end(); ++iter) {
755 message_center()->MarkSinglePopupAsShown((*iter)->id(), false); 782 message_center()->MarkSinglePopupAsShown((*iter)->id(), false);
756 } 783 }
757 EXPECT_EQ(3u, message_center()->UnreadNotificationCount()); 784 EXPECT_EQ(3u, message_center()->UnreadNotificationCount());
758 785
759 // Opening the message center will reset the unread count. 786 // Opening the message center will reset the unread count.
760 message_center()->SetVisibility(VISIBILITY_MESSAGE_CENTER); 787 message_center()->SetVisibility(VISIBILITY_MESSAGE_CENTER);
761 EXPECT_EQ(0u, message_center()->UnreadNotificationCount()); 788 EXPECT_EQ(0u, message_center()->UnreadNotificationCount());
762 } 789 }
763 790
791 TEST_F(MessageCenterImplTest, DisableNotificationsByNotifier) {
792 ASSERT_EQ(0u, message_center()->NotificationCount());
793 message_center()->AddNotification(
794 scoped_ptr<Notification>(
795 CreateSimpleNotificationWithNotifierId("id1-1", "app1")));
796 message_center()->AddNotification(
797 scoped_ptr<Notification>(
798 CreateSimpleNotificationWithNotifierId("id1-2", "app1")));
799 message_center()->AddNotification(
800 scoped_ptr<Notification>(
801 CreateSimpleNotificationWithNotifierId("id2-1", "app2")));
802 message_center()->AddNotification(
803 scoped_ptr<Notification>(
804 CreateSimpleNotificationWithNotifierId("id2-2", "app2")));
805 message_center()->AddNotification(
806 scoped_ptr<Notification>(
807 CreateSimpleNotificationWithNotifierId("id2-3", "app2")));
808 ASSERT_EQ(5u, message_center()->NotificationCount());
809
810 // Removing all of app1's notifications should only leave app2's.
811 message_center()->DisableNotificationsByNotifier(
812 NotifierId(NotifierId::APPLICATION, "app1"));
813 ASSERT_EQ(3u, message_center()->NotificationCount());
814
815 // Now we remove the remaining notifications.
816 message_center()->DisableNotificationsByNotifier(
817 NotifierId(NotifierId::APPLICATION, "app2"));
818 ASSERT_EQ(0u, message_center()->NotificationCount());
819 }
820
821 TEST_F(MessageCenterImplTest, NotifierEnabledChanged) {
822 ASSERT_EQ(0u, message_center()->NotificationCount());
823 message_center()->AddNotification(
824 scoped_ptr<Notification>(
825 CreateSimpleNotificationWithNotifierId("id1-1", "app1")));
826 message_center()->AddNotification(
827 scoped_ptr<Notification>(
828 CreateSimpleNotificationWithNotifierId("id1-2", "app1")));
829 message_center()->AddNotification(
830 scoped_ptr<Notification>(
831 CreateSimpleNotificationWithNotifierId("id1-3", "app1")));
832 message_center()->AddNotification(
833 scoped_ptr<Notification>(
834 CreateSimpleNotificationWithNotifierId("id2-1", "app2")));
835 message_center()->AddNotification(
836 scoped_ptr<Notification>(
837 CreateSimpleNotificationWithNotifierId("id2-2", "app2")));
838 message_center()->AddNotification(
839 scoped_ptr<Notification>(
840 CreateSimpleNotificationWithNotifierId("id2-3", "app2")));
841 message_center()->AddNotification(
842 scoped_ptr<Notification>(
843 CreateSimpleNotificationWithNotifierId("id2-4", "app2")));
844 message_center()->AddNotification(
845 scoped_ptr<Notification>(
846 CreateSimpleNotificationWithNotifierId("id2-5", "app2")));
847 ASSERT_EQ(8u, message_center()->NotificationCount());
848
849 // Enabling an extension should have no effect on the count.
850 notifier_settings_observer()->NotifierEnabledChanged(
851 NotifierId(NotifierId::APPLICATION, "app1"), true);
852 ASSERT_EQ(8u, message_center()->NotificationCount());
853
854 // Removing all of app2's notifications should only leave app1's.
855 notifier_settings_observer()->NotifierEnabledChanged(
856 NotifierId(NotifierId::APPLICATION, "app2"), false);
857 ASSERT_EQ(3u, message_center()->NotificationCount());
858
859 // Removal operations should be idempotent.
860 notifier_settings_observer()->NotifierEnabledChanged(
861 NotifierId(NotifierId::APPLICATION, "app2"), false);
862 ASSERT_EQ(3u, message_center()->NotificationCount());
863
864 // Now we remove the remaining notifications.
865 notifier_settings_observer()->NotifierEnabledChanged(
866 NotifierId(NotifierId::APPLICATION, "app1"), false);
867 ASSERT_EQ(0u, message_center()->NotificationCount());
868 }
869
764 } // namespace internal 870 } // namespace internal
765 } // namespace message_center 871 } // namespace message_center
OLDNEW
« no previous file with comments | « ui/message_center/message_center_impl.cc ('k') | ui/message_center/notifier_settings.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698