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/message_center/message_center.h"
10 #include "ui/message_center/notification_types.h"
12 namespace message_center
{
15 class MockDelegate
: public MessageCenterTrayDelegate
{
18 : show_popups_success_(true),
19 show_message_center_success_(true) {}
20 virtual ~MockDelegate() {}
21 virtual void OnMessageCenterTrayChanged() OVERRIDE
{}
22 virtual bool ShowPopups() OVERRIDE
{
23 return show_message_center_success_
;
25 virtual void HidePopups() OVERRIDE
{}
26 virtual bool ShowMessageCenter() OVERRIDE
{
27 return show_popups_success_
;
29 virtual void HideMessageCenter() OVERRIDE
{}
30 virtual bool ShowNotifierSettings() OVERRIDE
{
34 virtual MessageCenterTray
* GetMessageCenterTray() OVERRIDE
{
38 bool show_popups_success_
;
39 bool show_message_center_success_
;
42 DISALLOW_COPY_AND_ASSIGN(MockDelegate
);
45 class MessageCenterTrayTest
: public testing::Test
{
47 MessageCenterTrayTest() {}
48 virtual ~MessageCenterTrayTest() {}
50 virtual void SetUp() {
51 MessageCenter::Initialize();
52 delegate_
.reset(new MockDelegate
);
53 message_center_
= MessageCenter::Get();
54 message_center_tray_
.reset(
55 new MessageCenterTray(delegate_
.get(), message_center_
));
58 virtual void TearDown() {
59 message_center_tray_
.reset();
61 message_center_
= NULL
;
62 MessageCenter::Shutdown();
66 void AddNotification(const std::string
& id
) {
67 scoped_ptr
<Notification
> notification(
68 new Notification(message_center::NOTIFICATION_TYPE_SIMPLE
,
70 ASCIIToUTF16("Test Web Notification"),
71 ASCIIToUTF16("Notification message body."),
73 ASCIIToUTF16("www.test.org"),
75 message_center::RichNotificationData(),
76 NULL
/* delegate */));
77 message_center_
->AddNotification(notification
.Pass());
79 scoped_ptr
<MockDelegate
> delegate_
;
80 scoped_ptr
<MessageCenterTray
> message_center_tray_
;
81 MessageCenter
* message_center_
;
84 DISALLOW_COPY_AND_ASSIGN(MessageCenterTrayTest
);
89 TEST_F(MessageCenterTrayTest
, BasicMessageCenter
) {
90 ASSERT_FALSE(message_center_tray_
->popups_visible());
91 ASSERT_FALSE(message_center_tray_
->message_center_visible());
93 bool shown
= message_center_tray_
->ShowMessageCenterBubble();
96 ASSERT_FALSE(message_center_tray_
->popups_visible());
97 ASSERT_TRUE(message_center_tray_
->message_center_visible());
99 message_center_tray_
->HideMessageCenterBubble();
101 ASSERT_FALSE(message_center_tray_
->popups_visible());
102 ASSERT_FALSE(message_center_tray_
->message_center_visible());
104 message_center_tray_
->ToggleMessageCenterBubble();
106 ASSERT_FALSE(message_center_tray_
->popups_visible());
107 ASSERT_TRUE(message_center_tray_
->message_center_visible());
109 message_center_tray_
->ToggleMessageCenterBubble();
111 ASSERT_FALSE(message_center_tray_
->popups_visible());
112 ASSERT_FALSE(message_center_tray_
->message_center_visible());
115 TEST_F(MessageCenterTrayTest
, BasicPopup
) {
116 ASSERT_FALSE(message_center_tray_
->popups_visible());
117 ASSERT_FALSE(message_center_tray_
->message_center_visible());
119 message_center_tray_
->ShowPopupBubble();
121 ASSERT_FALSE(message_center_tray_
->popups_visible());
122 ASSERT_FALSE(message_center_tray_
->message_center_visible());
124 AddNotification("BasicPopup");
126 ASSERT_TRUE(message_center_tray_
->popups_visible());
127 ASSERT_FALSE(message_center_tray_
->message_center_visible());
129 message_center_tray_
->HidePopupBubble();
131 ASSERT_FALSE(message_center_tray_
->popups_visible());
132 ASSERT_FALSE(message_center_tray_
->message_center_visible());
135 TEST_F(MessageCenterTrayTest
, MessageCenterClosesPopups
) {
136 ASSERT_FALSE(message_center_tray_
->popups_visible());
137 ASSERT_FALSE(message_center_tray_
->message_center_visible());
139 AddNotification("MessageCenterClosesPopups");
141 ASSERT_TRUE(message_center_tray_
->popups_visible());
142 ASSERT_FALSE(message_center_tray_
->message_center_visible());
144 bool shown
= message_center_tray_
->ShowMessageCenterBubble();
147 ASSERT_FALSE(message_center_tray_
->popups_visible());
148 ASSERT_TRUE(message_center_tray_
->message_center_visible());
150 // The notification is queued if it's added when message center is visible.
151 AddNotification("MessageCenterClosesPopups2");
153 message_center_tray_
->ShowPopupBubble();
155 ASSERT_FALSE(message_center_tray_
->popups_visible());
156 ASSERT_TRUE(message_center_tray_
->message_center_visible());
158 message_center_tray_
->HideMessageCenterBubble();
160 // The queued notification appears as a popup.
161 ASSERT_TRUE(message_center_tray_
->popups_visible());
162 ASSERT_FALSE(message_center_tray_
->message_center_visible());
164 message_center_tray_
->ShowMessageCenterBubble();
165 message_center_tray_
->HideMessageCenterBubble();
166 ASSERT_FALSE(message_center_tray_
->popups_visible());
167 ASSERT_FALSE(message_center_tray_
->message_center_visible());
170 TEST_F(MessageCenterTrayTest
, MessageCenterReopenPopupsForSystemPriority
) {
171 ASSERT_FALSE(message_center_tray_
->popups_visible());
172 ASSERT_FALSE(message_center_tray_
->message_center_visible());
174 scoped_ptr
<Notification
> notification(
175 new Notification(message_center::NOTIFICATION_TYPE_SIMPLE
,
176 "MessageCenterReopnPopupsForSystemPriority",
177 ASCIIToUTF16("Test Web Notification"),
178 ASCIIToUTF16("Notification message body."),
180 ASCIIToUTF16("www.test.org"),
182 message_center::RichNotificationData(),
183 NULL
/* delegate */));
184 notification
->SetSystemPriority();
185 message_center_
->AddNotification(notification
.Pass());
187 ASSERT_TRUE(message_center_tray_
->popups_visible());
188 ASSERT_FALSE(message_center_tray_
->message_center_visible());
190 bool shown
= message_center_tray_
->ShowMessageCenterBubble();
193 ASSERT_FALSE(message_center_tray_
->popups_visible());
194 ASSERT_TRUE(message_center_tray_
->message_center_visible());
196 message_center_tray_
->HideMessageCenterBubble();
198 ASSERT_TRUE(message_center_tray_
->popups_visible());
199 ASSERT_FALSE(message_center_tray_
->message_center_visible());
202 TEST_F(MessageCenterTrayTest
, ShowBubbleFails
) {
203 // Now the delegate will signal that it was unable to show a bubble.
204 delegate_
->show_popups_success_
= false;
205 delegate_
->show_message_center_success_
= false;
207 ASSERT_FALSE(message_center_tray_
->popups_visible());
208 ASSERT_FALSE(message_center_tray_
->message_center_visible());
210 AddNotification("ShowBubbleFails");
212 message_center_tray_
->ShowPopupBubble();
214 ASSERT_FALSE(message_center_tray_
->popups_visible());
215 ASSERT_FALSE(message_center_tray_
->message_center_visible());
217 bool shown
= message_center_tray_
->ShowMessageCenterBubble();
220 ASSERT_FALSE(message_center_tray_
->popups_visible());
221 ASSERT_FALSE(message_center_tray_
->message_center_visible());
223 message_center_tray_
->HideMessageCenterBubble();
225 ASSERT_FALSE(message_center_tray_
->popups_visible());
226 ASSERT_FALSE(message_center_tray_
->message_center_visible());
228 message_center_tray_
->ToggleMessageCenterBubble();
230 ASSERT_FALSE(message_center_tray_
->popups_visible());
231 ASSERT_FALSE(message_center_tray_
->message_center_visible());
233 message_center_tray_
->HidePopupBubble();
235 ASSERT_FALSE(message_center_tray_
->popups_visible());
236 ASSERT_FALSE(message_center_tray_
->message_center_visible());
239 } // namespace message_center