Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / message_center / message_center_tray_unittest.cc
blobe12bd094c3fb7d91ce0380e61e06b1a11a22c124
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 AddNotification(id, DummyNotifierId());
75 void AddNotification(const std::string& id, NotifierId notifier_id) {
76 scoped_ptr<Notification> notification(new Notification(
77 message_center::NOTIFICATION_TYPE_SIMPLE, id,
78 ASCIIToUTF16("Test Web Notification"),
79 ASCIIToUTF16("Notification message body."), gfx::Image(),
80 ASCIIToUTF16("www.test.org"), GURL(), notifier_id,
81 message_center::RichNotificationData(), NULL /* delegate */));
82 message_center_->AddNotification(notification.Pass());
84 scoped_ptr<MockDelegate> delegate_;
85 scoped_ptr<MessageCenterTray> message_center_tray_;
86 MessageCenter* message_center_;
88 private:
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();
97 EXPECT_TRUE(shown);
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_->ShowMessageCenterBubble();
109 ASSERT_FALSE(message_center_tray_->popups_visible());
110 ASSERT_TRUE(message_center_tray_->message_center_visible());
112 message_center_tray_->HideMessageCenterBubble();
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();
148 EXPECT_TRUE(shown);
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(new Notification(
178 message_center::NOTIFICATION_TYPE_SIMPLE,
179 "MessageCenterReopnPopupsForSystemPriority",
180 ASCIIToUTF16("Test Web Notification"),
181 ASCIIToUTF16("Notification message body."), gfx::Image(),
182 ASCIIToUTF16("www.test.org"), GURL(), DummyNotifierId(),
183 message_center::RichNotificationData(), 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();
191 EXPECT_TRUE(shown);
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();
218 EXPECT_FALSE(shown);
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_->ShowMessageCenterBubble();
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 #ifdef OS_CHROMEOS
240 TEST_F(MessageCenterTrayTest, ContextMenuTestWithMessageCenter) {
241 const std::string id1 = "id1";
242 const std::string id2 = "id2";
243 const std::string id3 = "id3";
244 AddNotification(id1);
246 base::string16 display_source = ASCIIToUTF16("www.test.org");
247 NotifierId notifier_id = DummyNotifierId();
249 NotifierId notifier_id2(NotifierId::APPLICATION, "sample-app");
250 scoped_ptr<Notification> notification(new Notification(
251 message_center::NOTIFICATION_TYPE_SIMPLE, id2,
252 ASCIIToUTF16("Test Web Notification"),
253 ASCIIToUTF16("Notification message body."), gfx::Image(),
254 base::string16() /* empty display source */, GURL(), notifier_id2,
255 message_center::RichNotificationData(), NULL /* delegate */));
256 message_center_->AddNotification(notification.Pass());
258 AddNotification(id3);
260 scoped_ptr<ui::MenuModel> model(
261 message_center_tray_->CreateNotificationMenuModel(
262 notifier_id, display_source));
263 EXPECT_EQ(2, model->GetItemCount());
264 const int second_command = model->GetCommandIdAt(1);
266 // The second item is to open the settings.
267 EXPECT_TRUE(model->IsEnabledAt(0));
268 EXPECT_TRUE(model->IsEnabledAt(1));
269 model->ActivatedAt(1);
270 EXPECT_TRUE(message_center_tray_->message_center_visible());
272 message_center_tray_->HideMessageCenterBubble();
274 // The first item is to disable notifications from the notifier id. It also
275 // removes all notifications from the same notifier, i.e. id1 and id3.
276 model->ActivatedAt(0);
277 NotificationList::Notifications notifications =
278 message_center_->GetVisibleNotifications();
279 EXPECT_EQ(1u, notifications.size());
280 EXPECT_EQ(id2, (*notifications.begin())->id());
282 // Disables the context menu.
283 delegate_->enable_context_menu_ = false;
285 // id2 doesn't have the display source, so it don't have the menu item for
286 // disabling notifications.
287 model = message_center_tray_->CreateNotificationMenuModel(
288 notifier_id2, base::string16());
289 EXPECT_EQ(1, model->GetItemCount());
290 EXPECT_EQ(second_command, model->GetCommandIdAt(0));
292 // The command itself is disabled because delegate disables context menu.
293 EXPECT_FALSE(model->IsEnabledAt(0));
296 #else
297 TEST_F(MessageCenterTrayTest, ContextMenuTestPopupsOnly) {
298 const std::string id1 = "id1";
299 const std::string id2 = "id2";
300 const std::string id3 = "id3";
302 base::string16 display_source = ASCIIToUTF16("https://www.test.org");
303 NotifierId notifier_id(GURL("https://www.test.org"));
305 AddNotification(id1, notifier_id);
307 NotifierId notifier_id2(NotifierId::APPLICATION, "sample-app");
308 scoped_ptr<Notification> notification(new Notification(
309 message_center::NOTIFICATION_TYPE_SIMPLE, id2,
310 ASCIIToUTF16("Test Web Notification"),
311 ASCIIToUTF16("Notification message body."), gfx::Image(),
312 base::string16() /* empty display source */, GURL(), notifier_id2,
313 message_center::RichNotificationData(), NULL /* delegate */));
314 message_center_->AddNotification(notification.Pass());
316 AddNotification(id3, notifier_id);
318 // The dummy notifier is SYSTEM_COMPONENT so no context menu is visible.
319 scoped_ptr<ui::MenuModel> model(
320 message_center_tray_->CreateNotificationMenuModel(DummyNotifierId(),
321 display_source));
322 EXPECT_EQ(nullptr, model.get());
324 model = message_center_tray_->CreateNotificationMenuModel(notifier_id,
325 display_source);
326 EXPECT_EQ(1, model->GetItemCount());
328 // The first item is to disable notifications from the notifier id. It also
329 // removes all notifications from the same notifier, i.e. id1 and id3.
330 EXPECT_TRUE(model->IsEnabledAt(0));
331 model->ActivatedAt(0);
333 NotificationList::Notifications notifications =
334 message_center_->GetVisibleNotifications();
335 EXPECT_EQ(1u, notifications.size());
336 EXPECT_EQ(id2, (*notifications.begin())->id());
338 // Disables the context menu.
339 delegate_->enable_context_menu_ = false;
341 // id2 doesn't have the display source, so it don't have the menu item for
342 // disabling notifications.
343 EXPECT_EQ(nullptr, message_center_tray_->CreateNotificationMenuModel(
344 notifier_id2, base::string16()));
346 #endif
348 TEST_F(MessageCenterTrayTest, DelegateDisabledContextMenu) {
349 const std::string id1 = "id1";
350 base::string16 display_source = ASCIIToUTF16("www.test.org");
351 AddNotification(id1);
352 NotifierId notifier_id(GURL("www.test.org"));
354 delegate_->enable_context_menu_ = false;
355 // id2 doesn't have the display source, so it don't have the menu item for
356 // disabling notifications.
357 scoped_ptr<ui::MenuModel> model(
358 message_center_tray_->CreateNotificationMenuModel(notifier_id,
359 display_source));
361 // The commands are disabled because delegate disables context menu.
362 #ifndef OS_CHROMEOS
363 EXPECT_EQ(1, model->GetItemCount());
364 #else
365 EXPECT_EQ(2, model->GetItemCount());
366 EXPECT_FALSE(model->IsEnabledAt(1));
367 #endif
369 EXPECT_FALSE(model->IsEnabledAt(0));
372 } // namespace message_center