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"),
74 "" /* extension id */,
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 AddNotification("MessageCenterClosesPopups2");
152 message_center_tray_
->ShowPopupBubble();
154 ASSERT_FALSE(message_center_tray_
->popups_visible());
155 ASSERT_TRUE(message_center_tray_
->message_center_visible());
157 message_center_tray_
->HideMessageCenterBubble();
159 ASSERT_FALSE(message_center_tray_
->popups_visible());
160 ASSERT_FALSE(message_center_tray_
->message_center_visible());
163 TEST_F(MessageCenterTrayTest
, MessageCenterReopenPopupsForSystemPriority
) {
164 ASSERT_FALSE(message_center_tray_
->popups_visible());
165 ASSERT_FALSE(message_center_tray_
->message_center_visible());
167 scoped_ptr
<Notification
> notification(
168 new Notification(message_center::NOTIFICATION_TYPE_SIMPLE
,
169 "MessageCenterReopnPopupsForSystemPriority",
170 ASCIIToUTF16("Test Web Notification"),
171 ASCIIToUTF16("Notification message body."),
173 ASCIIToUTF16("www.test.org"),
174 "" /* extension id */,
175 message_center::RichNotificationData(),
176 NULL
/* delegate */));
177 notification
->SetSystemPriority();
178 message_center_
->AddNotification(notification
.Pass());
180 ASSERT_TRUE(message_center_tray_
->popups_visible());
181 ASSERT_FALSE(message_center_tray_
->message_center_visible());
183 bool shown
= message_center_tray_
->ShowMessageCenterBubble();
186 ASSERT_FALSE(message_center_tray_
->popups_visible());
187 ASSERT_TRUE(message_center_tray_
->message_center_visible());
189 message_center_tray_
->HideMessageCenterBubble();
191 ASSERT_TRUE(message_center_tray_
->popups_visible());
192 ASSERT_FALSE(message_center_tray_
->message_center_visible());
195 TEST_F(MessageCenterTrayTest
, ShowBubbleFails
) {
196 // Now the delegate will signal that it was unable to show a bubble.
197 delegate_
->show_popups_success_
= false;
198 delegate_
->show_message_center_success_
= false;
200 ASSERT_FALSE(message_center_tray_
->popups_visible());
201 ASSERT_FALSE(message_center_tray_
->message_center_visible());
203 AddNotification("ShowBubbleFails");
205 message_center_tray_
->ShowPopupBubble();
207 ASSERT_FALSE(message_center_tray_
->popups_visible());
208 ASSERT_FALSE(message_center_tray_
->message_center_visible());
210 bool shown
= message_center_tray_
->ShowMessageCenterBubble();
213 ASSERT_FALSE(message_center_tray_
->popups_visible());
214 ASSERT_FALSE(message_center_tray_
->message_center_visible());
216 message_center_tray_
->HideMessageCenterBubble();
218 ASSERT_FALSE(message_center_tray_
->popups_visible());
219 ASSERT_FALSE(message_center_tray_
->message_center_visible());
221 message_center_tray_
->ToggleMessageCenterBubble();
223 ASSERT_FALSE(message_center_tray_
->popups_visible());
224 ASSERT_FALSE(message_center_tray_
->message_center_visible());
226 message_center_tray_
->HidePopupBubble();
228 ASSERT_FALSE(message_center_tray_
->popups_visible());
229 ASSERT_FALSE(message_center_tray_
->message_center_visible());
232 } // namespace message_center