1 // Copyright (c) 2013 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.
5 #include "ui/message_center/message_center_tray.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "ui/base/models/menu_model.h"
10 #include "ui/message_center/message_center.h"
11 #include "ui/message_center/notification.h"
12 #include "ui/message_center/notification_types.h"
14 using base::ASCIIToUTF16
;
16 namespace message_center
{
19 class MockDelegate
: public MessageCenterTrayDelegate
{
22 : show_popups_success_(true),
23 show_message_center_success_(true),
24 enable_context_menu_(true) {}
25 ~MockDelegate() override
{}
26 void OnMessageCenterTrayChanged() override
{}
27 bool ShowPopups() override
{ return show_message_center_success_
; }
28 void HidePopups() override
{}
29 bool ShowMessageCenter() override
{ return show_popups_success_
; }
30 void HideMessageCenter() override
{}
31 bool ShowNotifierSettings() override
{ return true; }
32 bool IsContextMenuEnabled() const override
{ return enable_context_menu_
; }
34 MessageCenterTray
* GetMessageCenterTray() override
{ return NULL
; }
36 bool show_popups_success_
;
37 bool show_message_center_success_
;
38 bool enable_context_menu_
;
41 DISALLOW_COPY_AND_ASSIGN(MockDelegate
);
46 class MessageCenterTrayTest
: public testing::Test
{
48 MessageCenterTrayTest() {}
49 ~MessageCenterTrayTest() override
{}
51 void SetUp() override
{
52 MessageCenter::Initialize();
53 delegate_
.reset(new MockDelegate
);
54 message_center_
= MessageCenter::Get();
55 message_center_tray_
.reset(
56 new MessageCenterTray(delegate_
.get(), message_center_
));
59 void TearDown() override
{
60 message_center_tray_
.reset();
62 message_center_
= NULL
;
63 MessageCenter::Shutdown();
67 NotifierId
DummyNotifierId() {
71 void AddNotification(const std::string
& id
) {
72 scoped_ptr
<Notification
> notification(
73 new Notification(message_center::NOTIFICATION_TYPE_SIMPLE
,
75 ASCIIToUTF16("Test Web Notification"),
76 ASCIIToUTF16("Notification message body."),
78 ASCIIToUTF16("www.test.org"),
80 message_center::RichNotificationData(),
81 NULL
/* delegate */));
82 message_center_
->AddNotification(notification
.Pass());
84 scoped_ptr
<MockDelegate
> delegate_
;
85 scoped_ptr
<MessageCenterTray
> message_center_tray_
;
86 MessageCenter
* message_center_
;
89 DISALLOW_COPY_AND_ASSIGN(MessageCenterTrayTest
);
92 TEST_F(MessageCenterTrayTest
, BasicMessageCenter
) {
93 ASSERT_FALSE(message_center_tray_
->popups_visible());
94 ASSERT_FALSE(message_center_tray_
->message_center_visible());
96 bool shown
= message_center_tray_
->ShowMessageCenterBubble();
99 ASSERT_FALSE(message_center_tray_
->popups_visible());
100 ASSERT_TRUE(message_center_tray_
->message_center_visible());
102 message_center_tray_
->HideMessageCenterBubble();
104 ASSERT_FALSE(message_center_tray_
->popups_visible());
105 ASSERT_FALSE(message_center_tray_
->message_center_visible());
107 message_center_tray_
->ToggleMessageCenterBubble();
109 ASSERT_FALSE(message_center_tray_
->popups_visible());
110 ASSERT_TRUE(message_center_tray_
->message_center_visible());
112 message_center_tray_
->ToggleMessageCenterBubble();
114 ASSERT_FALSE(message_center_tray_
->popups_visible());
115 ASSERT_FALSE(message_center_tray_
->message_center_visible());
118 TEST_F(MessageCenterTrayTest
, BasicPopup
) {
119 ASSERT_FALSE(message_center_tray_
->popups_visible());
120 ASSERT_FALSE(message_center_tray_
->message_center_visible());
122 message_center_tray_
->ShowPopupBubble();
124 ASSERT_FALSE(message_center_tray_
->popups_visible());
125 ASSERT_FALSE(message_center_tray_
->message_center_visible());
127 AddNotification("BasicPopup");
129 ASSERT_TRUE(message_center_tray_
->popups_visible());
130 ASSERT_FALSE(message_center_tray_
->message_center_visible());
132 message_center_tray_
->HidePopupBubble();
134 ASSERT_FALSE(message_center_tray_
->popups_visible());
135 ASSERT_FALSE(message_center_tray_
->message_center_visible());
138 TEST_F(MessageCenterTrayTest
, MessageCenterClosesPopups
) {
139 ASSERT_FALSE(message_center_tray_
->popups_visible());
140 ASSERT_FALSE(message_center_tray_
->message_center_visible());
142 AddNotification("MessageCenterClosesPopups");
144 ASSERT_TRUE(message_center_tray_
->popups_visible());
145 ASSERT_FALSE(message_center_tray_
->message_center_visible());
147 bool shown
= message_center_tray_
->ShowMessageCenterBubble();
150 ASSERT_FALSE(message_center_tray_
->popups_visible());
151 ASSERT_TRUE(message_center_tray_
->message_center_visible());
153 // The notification is queued if it's added when message center is visible.
154 AddNotification("MessageCenterClosesPopups2");
156 message_center_tray_
->ShowPopupBubble();
158 ASSERT_FALSE(message_center_tray_
->popups_visible());
159 ASSERT_TRUE(message_center_tray_
->message_center_visible());
161 message_center_tray_
->HideMessageCenterBubble();
163 // The queued notification appears as a popup.
164 ASSERT_TRUE(message_center_tray_
->popups_visible());
165 ASSERT_FALSE(message_center_tray_
->message_center_visible());
167 message_center_tray_
->ShowMessageCenterBubble();
168 message_center_tray_
->HideMessageCenterBubble();
169 ASSERT_FALSE(message_center_tray_
->popups_visible());
170 ASSERT_FALSE(message_center_tray_
->message_center_visible());
173 TEST_F(MessageCenterTrayTest
, MessageCenterReopenPopupsForSystemPriority
) {
174 ASSERT_FALSE(message_center_tray_
->popups_visible());
175 ASSERT_FALSE(message_center_tray_
->message_center_visible());
177 scoped_ptr
<Notification
> notification(
178 new Notification(message_center::NOTIFICATION_TYPE_SIMPLE
,
179 "MessageCenterReopnPopupsForSystemPriority",
180 ASCIIToUTF16("Test Web Notification"),
181 ASCIIToUTF16("Notification message body."),
183 ASCIIToUTF16("www.test.org"),
185 message_center::RichNotificationData(),
186 NULL
/* delegate */));
187 notification
->SetSystemPriority();
188 message_center_
->AddNotification(notification
.Pass());
190 ASSERT_TRUE(message_center_tray_
->popups_visible());
191 ASSERT_FALSE(message_center_tray_
->message_center_visible());
193 bool shown
= message_center_tray_
->ShowMessageCenterBubble();
196 ASSERT_FALSE(message_center_tray_
->popups_visible());
197 ASSERT_TRUE(message_center_tray_
->message_center_visible());
199 message_center_tray_
->HideMessageCenterBubble();
201 ASSERT_TRUE(message_center_tray_
->popups_visible());
202 ASSERT_FALSE(message_center_tray_
->message_center_visible());
205 TEST_F(MessageCenterTrayTest
, ShowBubbleFails
) {
206 // Now the delegate will signal that it was unable to show a bubble.
207 delegate_
->show_popups_success_
= false;
208 delegate_
->show_message_center_success_
= false;
210 ASSERT_FALSE(message_center_tray_
->popups_visible());
211 ASSERT_FALSE(message_center_tray_
->message_center_visible());
213 AddNotification("ShowBubbleFails");
215 message_center_tray_
->ShowPopupBubble();
217 ASSERT_FALSE(message_center_tray_
->popups_visible());
218 ASSERT_FALSE(message_center_tray_
->message_center_visible());
220 bool shown
= message_center_tray_
->ShowMessageCenterBubble();
223 ASSERT_FALSE(message_center_tray_
->popups_visible());
224 ASSERT_FALSE(message_center_tray_
->message_center_visible());
226 message_center_tray_
->HideMessageCenterBubble();
228 ASSERT_FALSE(message_center_tray_
->popups_visible());
229 ASSERT_FALSE(message_center_tray_
->message_center_visible());
231 message_center_tray_
->ToggleMessageCenterBubble();
233 ASSERT_FALSE(message_center_tray_
->popups_visible());
234 ASSERT_FALSE(message_center_tray_
->message_center_visible());
236 message_center_tray_
->HidePopupBubble();
238 ASSERT_FALSE(message_center_tray_
->popups_visible());
239 ASSERT_FALSE(message_center_tray_
->message_center_visible());
242 TEST_F(MessageCenterTrayTest
, ContextMenuTest
) {
243 const std::string id1
= "id1";
244 const std::string id2
= "id2";
245 const std::string id3
= "id3";
246 AddNotification(id1
);
248 base::string16 display_source
= ASCIIToUTF16("www.test.org");
249 NotifierId notifier_id
= DummyNotifierId();
251 NotifierId
notifier_id2(NotifierId::APPLICATION
, "sample-app");
252 scoped_ptr
<Notification
> notification(
253 new Notification(message_center::NOTIFICATION_TYPE_SIMPLE
,
255 ASCIIToUTF16("Test Web Notification"),
256 ASCIIToUTF16("Notification message body."),
258 base::string16() /* empty display source */,
260 message_center::RichNotificationData(),
261 NULL
/* delegate */));
262 message_center_
->AddNotification(notification
.Pass());
264 AddNotification(id3
);
266 scoped_ptr
<ui::MenuModel
> model(
267 message_center_tray_
->CreateNotificationMenuModel(
268 notifier_id
, display_source
));
269 EXPECT_EQ(2, model
->GetItemCount());
270 const int second_command
= model
->GetCommandIdAt(1);
272 // The second item is to open the settings.
273 EXPECT_TRUE(model
->IsEnabledAt(0));
274 EXPECT_TRUE(model
->IsEnabledAt(1));
275 model
->ActivatedAt(1);
276 EXPECT_TRUE(message_center_tray_
->message_center_visible());
278 message_center_tray_
->HideMessageCenterBubble();
280 // The first item is to disable notifications from the notifier id. It also
281 // removes all notifications from the same notifier, i.e. id1 and id3.
282 model
->ActivatedAt(0);
283 NotificationList::Notifications notifications
=
284 message_center_
->GetVisibleNotifications();
285 EXPECT_EQ(1u, notifications
.size());
286 EXPECT_EQ(id2
, (*notifications
.begin())->id());
288 // Disables the context menu.
289 delegate_
->enable_context_menu_
= false;
291 // id2 doesn't have the display source, so it don't have the menu item for
292 // disabling notifications.
293 model
= message_center_tray_
->CreateNotificationMenuModel(
294 notifier_id2
, base::string16());
295 EXPECT_EQ(1, model
->GetItemCount());
296 EXPECT_EQ(second_command
, model
->GetCommandIdAt(0));
298 // The command itself is disabled because delegate disables context menu.
299 EXPECT_FALSE(model
->IsEnabledAt(0));
302 } // namespace message_center