Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / ui / message_center / message_center_tray_unittest.cc
blob1562156695a486d5afcfd08c034e400f24943640
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 {
17 namespace {
19 class MockDelegate : public MessageCenterTrayDelegate {
20 public:
21 MockDelegate()
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_;
40 private:
41 DISALLOW_COPY_AND_ASSIGN(MockDelegate);
44 } // namespace
46 class MessageCenterTrayTest : public testing::Test {
47 public:
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();
61 delegate_.reset();
62 message_center_ = NULL;
63 MessageCenter::Shutdown();
66 protected:
67 NotifierId DummyNotifierId() {
68 return NotifierId();
71 void AddNotification(const std::string& id) {
72 scoped_ptr<Notification> notification(new Notification(
73 message_center::NOTIFICATION_TYPE_SIMPLE, id,
74 ASCIIToUTF16("Test Web Notification"),
75 ASCIIToUTF16("Notification message body."), gfx::Image(),
76 ASCIIToUTF16("www.test.org"), GURL(), DummyNotifierId(),
77 message_center::RichNotificationData(), NULL /* delegate */));
78 message_center_->AddNotification(notification.Pass());
80 scoped_ptr<MockDelegate> delegate_;
81 scoped_ptr<MessageCenterTray> message_center_tray_;
82 MessageCenter* message_center_;
84 private:
85 DISALLOW_COPY_AND_ASSIGN(MessageCenterTrayTest);
88 TEST_F(MessageCenterTrayTest, BasicMessageCenter) {
89 ASSERT_FALSE(message_center_tray_->popups_visible());
90 ASSERT_FALSE(message_center_tray_->message_center_visible());
92 bool shown = message_center_tray_->ShowMessageCenterBubble();
93 EXPECT_TRUE(shown);
95 ASSERT_FALSE(message_center_tray_->popups_visible());
96 ASSERT_TRUE(message_center_tray_->message_center_visible());
98 message_center_tray_->HideMessageCenterBubble();
100 ASSERT_FALSE(message_center_tray_->popups_visible());
101 ASSERT_FALSE(message_center_tray_->message_center_visible());
103 message_center_tray_->ToggleMessageCenterBubble();
105 ASSERT_FALSE(message_center_tray_->popups_visible());
106 ASSERT_TRUE(message_center_tray_->message_center_visible());
108 message_center_tray_->ToggleMessageCenterBubble();
110 ASSERT_FALSE(message_center_tray_->popups_visible());
111 ASSERT_FALSE(message_center_tray_->message_center_visible());
114 TEST_F(MessageCenterTrayTest, BasicPopup) {
115 ASSERT_FALSE(message_center_tray_->popups_visible());
116 ASSERT_FALSE(message_center_tray_->message_center_visible());
118 message_center_tray_->ShowPopupBubble();
120 ASSERT_FALSE(message_center_tray_->popups_visible());
121 ASSERT_FALSE(message_center_tray_->message_center_visible());
123 AddNotification("BasicPopup");
125 ASSERT_TRUE(message_center_tray_->popups_visible());
126 ASSERT_FALSE(message_center_tray_->message_center_visible());
128 message_center_tray_->HidePopupBubble();
130 ASSERT_FALSE(message_center_tray_->popups_visible());
131 ASSERT_FALSE(message_center_tray_->message_center_visible());
134 TEST_F(MessageCenterTrayTest, MessageCenterClosesPopups) {
135 ASSERT_FALSE(message_center_tray_->popups_visible());
136 ASSERT_FALSE(message_center_tray_->message_center_visible());
138 AddNotification("MessageCenterClosesPopups");
140 ASSERT_TRUE(message_center_tray_->popups_visible());
141 ASSERT_FALSE(message_center_tray_->message_center_visible());
143 bool shown = message_center_tray_->ShowMessageCenterBubble();
144 EXPECT_TRUE(shown);
146 ASSERT_FALSE(message_center_tray_->popups_visible());
147 ASSERT_TRUE(message_center_tray_->message_center_visible());
149 // The notification is queued if it's added when message center is 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 // The queued notification appears as a popup.
160 ASSERT_TRUE(message_center_tray_->popups_visible());
161 ASSERT_FALSE(message_center_tray_->message_center_visible());
163 message_center_tray_->ShowMessageCenterBubble();
164 message_center_tray_->HideMessageCenterBubble();
165 ASSERT_FALSE(message_center_tray_->popups_visible());
166 ASSERT_FALSE(message_center_tray_->message_center_visible());
169 TEST_F(MessageCenterTrayTest, MessageCenterReopenPopupsForSystemPriority) {
170 ASSERT_FALSE(message_center_tray_->popups_visible());
171 ASSERT_FALSE(message_center_tray_->message_center_visible());
173 scoped_ptr<Notification> notification(new Notification(
174 message_center::NOTIFICATION_TYPE_SIMPLE,
175 "MessageCenterReopnPopupsForSystemPriority",
176 ASCIIToUTF16("Test Web Notification"),
177 ASCIIToUTF16("Notification message body."), gfx::Image(),
178 ASCIIToUTF16("www.test.org"), GURL(), DummyNotifierId(),
179 message_center::RichNotificationData(), NULL /* delegate */));
180 notification->SetSystemPriority();
181 message_center_->AddNotification(notification.Pass());
183 ASSERT_TRUE(message_center_tray_->popups_visible());
184 ASSERT_FALSE(message_center_tray_->message_center_visible());
186 bool shown = message_center_tray_->ShowMessageCenterBubble();
187 EXPECT_TRUE(shown);
189 ASSERT_FALSE(message_center_tray_->popups_visible());
190 ASSERT_TRUE(message_center_tray_->message_center_visible());
192 message_center_tray_->HideMessageCenterBubble();
194 ASSERT_TRUE(message_center_tray_->popups_visible());
195 ASSERT_FALSE(message_center_tray_->message_center_visible());
198 TEST_F(MessageCenterTrayTest, ShowBubbleFails) {
199 // Now the delegate will signal that it was unable to show a bubble.
200 delegate_->show_popups_success_ = false;
201 delegate_->show_message_center_success_ = false;
203 ASSERT_FALSE(message_center_tray_->popups_visible());
204 ASSERT_FALSE(message_center_tray_->message_center_visible());
206 AddNotification("ShowBubbleFails");
208 message_center_tray_->ShowPopupBubble();
210 ASSERT_FALSE(message_center_tray_->popups_visible());
211 ASSERT_FALSE(message_center_tray_->message_center_visible());
213 bool shown = message_center_tray_->ShowMessageCenterBubble();
214 EXPECT_FALSE(shown);
216 ASSERT_FALSE(message_center_tray_->popups_visible());
217 ASSERT_FALSE(message_center_tray_->message_center_visible());
219 message_center_tray_->HideMessageCenterBubble();
221 ASSERT_FALSE(message_center_tray_->popups_visible());
222 ASSERT_FALSE(message_center_tray_->message_center_visible());
224 message_center_tray_->ToggleMessageCenterBubble();
226 ASSERT_FALSE(message_center_tray_->popups_visible());
227 ASSERT_FALSE(message_center_tray_->message_center_visible());
229 message_center_tray_->HidePopupBubble();
231 ASSERT_FALSE(message_center_tray_->popups_visible());
232 ASSERT_FALSE(message_center_tray_->message_center_visible());
235 TEST_F(MessageCenterTrayTest, ContextMenuTest) {
236 const std::string id1 = "id1";
237 const std::string id2 = "id2";
238 const std::string id3 = "id3";
239 AddNotification(id1);
241 base::string16 display_source = ASCIIToUTF16("www.test.org");
242 NotifierId notifier_id = DummyNotifierId();
244 NotifierId notifier_id2(NotifierId::APPLICATION, "sample-app");
245 scoped_ptr<Notification> notification(new Notification(
246 message_center::NOTIFICATION_TYPE_SIMPLE, id2,
247 ASCIIToUTF16("Test Web Notification"),
248 ASCIIToUTF16("Notification message body."), gfx::Image(),
249 base::string16() /* empty display source */, GURL(), notifier_id2,
250 message_center::RichNotificationData(), NULL /* delegate */));
251 message_center_->AddNotification(notification.Pass());
253 AddNotification(id3);
255 scoped_ptr<ui::MenuModel> model(
256 message_center_tray_->CreateNotificationMenuModel(
257 notifier_id, display_source));
258 EXPECT_EQ(2, model->GetItemCount());
259 const int second_command = model->GetCommandIdAt(1);
261 // The second item is to open the settings.
262 EXPECT_TRUE(model->IsEnabledAt(0));
263 EXPECT_TRUE(model->IsEnabledAt(1));
264 model->ActivatedAt(1);
265 EXPECT_TRUE(message_center_tray_->message_center_visible());
267 message_center_tray_->HideMessageCenterBubble();
269 // The first item is to disable notifications from the notifier id. It also
270 // removes all notifications from the same notifier, i.e. id1 and id3.
271 model->ActivatedAt(0);
272 NotificationList::Notifications notifications =
273 message_center_->GetVisibleNotifications();
274 EXPECT_EQ(1u, notifications.size());
275 EXPECT_EQ(id2, (*notifications.begin())->id());
277 // Disables the context menu.
278 delegate_->enable_context_menu_ = false;
280 // id2 doesn't have the display source, so it don't have the menu item for
281 // disabling notifications.
282 model = message_center_tray_->CreateNotificationMenuModel(
283 notifier_id2, base::string16());
284 EXPECT_EQ(1, model->GetItemCount());
285 EXPECT_EQ(second_command, model->GetCommandIdAt(0));
287 // The command itself is disabled because delegate disables context menu.
288 EXPECT_FALSE(model->IsEnabledAt(0));
291 } // namespace message_center