Media Galleries API: Add some CHECKs to try to catch and fix a crash.
[chromium-blink-merge.git] / ui / message_center / message_center_tray_unittest.cc
blob993e18879a9b57a1545fbef207ac1266a3304780
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 virtual ~MockDelegate() {}
26 virtual void OnMessageCenterTrayChanged() OVERRIDE {}
27 virtual bool ShowPopups() OVERRIDE {
28 return show_message_center_success_;
30 virtual void HidePopups() OVERRIDE {}
31 virtual bool ShowMessageCenter() OVERRIDE {
32 return show_popups_success_;
34 virtual void HideMessageCenter() OVERRIDE {}
35 virtual bool ShowNotifierSettings() OVERRIDE {
36 return true;
38 virtual bool IsContextMenuEnabled() const OVERRIDE {
39 return enable_context_menu_;
42 virtual MessageCenterTray* GetMessageCenterTray() OVERRIDE {
43 return NULL;
46 bool show_popups_success_;
47 bool show_message_center_success_;
48 bool enable_context_menu_;
50 private:
51 DISALLOW_COPY_AND_ASSIGN(MockDelegate);
54 } // namespace
56 class MessageCenterTrayTest : public testing::Test {
57 public:
58 MessageCenterTrayTest() {}
59 virtual ~MessageCenterTrayTest() {}
61 virtual void SetUp() {
62 MessageCenter::Initialize();
63 delegate_.reset(new MockDelegate);
64 message_center_ = MessageCenter::Get();
65 message_center_tray_.reset(
66 new MessageCenterTray(delegate_.get(), message_center_));
69 virtual void TearDown() {
70 message_center_tray_.reset();
71 delegate_.reset();
72 message_center_ = NULL;
73 MessageCenter::Shutdown();
76 protected:
77 NotifierId DummyNotifierId() {
78 return NotifierId();
81 void AddNotification(const std::string& id) {
82 scoped_ptr<Notification> notification(
83 new Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
84 id,
85 ASCIIToUTF16("Test Web Notification"),
86 ASCIIToUTF16("Notification message body."),
87 gfx::Image(),
88 ASCIIToUTF16("www.test.org"),
89 DummyNotifierId(),
90 message_center::RichNotificationData(),
91 NULL /* delegate */));
92 message_center_->AddNotification(notification.Pass());
94 scoped_ptr<MockDelegate> delegate_;
95 scoped_ptr<MessageCenterTray> message_center_tray_;
96 MessageCenter* message_center_;
98 private:
99 DISALLOW_COPY_AND_ASSIGN(MessageCenterTrayTest);
102 TEST_F(MessageCenterTrayTest, BasicMessageCenter) {
103 ASSERT_FALSE(message_center_tray_->popups_visible());
104 ASSERT_FALSE(message_center_tray_->message_center_visible());
106 bool shown = message_center_tray_->ShowMessageCenterBubble();
107 EXPECT_TRUE(shown);
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());
117 message_center_tray_->ToggleMessageCenterBubble();
119 ASSERT_FALSE(message_center_tray_->popups_visible());
120 ASSERT_TRUE(message_center_tray_->message_center_visible());
122 message_center_tray_->ToggleMessageCenterBubble();
124 ASSERT_FALSE(message_center_tray_->popups_visible());
125 ASSERT_FALSE(message_center_tray_->message_center_visible());
128 TEST_F(MessageCenterTrayTest, BasicPopup) {
129 ASSERT_FALSE(message_center_tray_->popups_visible());
130 ASSERT_FALSE(message_center_tray_->message_center_visible());
132 message_center_tray_->ShowPopupBubble();
134 ASSERT_FALSE(message_center_tray_->popups_visible());
135 ASSERT_FALSE(message_center_tray_->message_center_visible());
137 AddNotification("BasicPopup");
139 ASSERT_TRUE(message_center_tray_->popups_visible());
140 ASSERT_FALSE(message_center_tray_->message_center_visible());
142 message_center_tray_->HidePopupBubble();
144 ASSERT_FALSE(message_center_tray_->popups_visible());
145 ASSERT_FALSE(message_center_tray_->message_center_visible());
148 TEST_F(MessageCenterTrayTest, MessageCenterClosesPopups) {
149 ASSERT_FALSE(message_center_tray_->popups_visible());
150 ASSERT_FALSE(message_center_tray_->message_center_visible());
152 AddNotification("MessageCenterClosesPopups");
154 ASSERT_TRUE(message_center_tray_->popups_visible());
155 ASSERT_FALSE(message_center_tray_->message_center_visible());
157 bool shown = message_center_tray_->ShowMessageCenterBubble();
158 EXPECT_TRUE(shown);
160 ASSERT_FALSE(message_center_tray_->popups_visible());
161 ASSERT_TRUE(message_center_tray_->message_center_visible());
163 // The notification is queued if it's added when message center is visible.
164 AddNotification("MessageCenterClosesPopups2");
166 message_center_tray_->ShowPopupBubble();
168 ASSERT_FALSE(message_center_tray_->popups_visible());
169 ASSERT_TRUE(message_center_tray_->message_center_visible());
171 message_center_tray_->HideMessageCenterBubble();
173 // The queued notification appears as a popup.
174 ASSERT_TRUE(message_center_tray_->popups_visible());
175 ASSERT_FALSE(message_center_tray_->message_center_visible());
177 message_center_tray_->ShowMessageCenterBubble();
178 message_center_tray_->HideMessageCenterBubble();
179 ASSERT_FALSE(message_center_tray_->popups_visible());
180 ASSERT_FALSE(message_center_tray_->message_center_visible());
183 TEST_F(MessageCenterTrayTest, MessageCenterReopenPopupsForSystemPriority) {
184 ASSERT_FALSE(message_center_tray_->popups_visible());
185 ASSERT_FALSE(message_center_tray_->message_center_visible());
187 scoped_ptr<Notification> notification(
188 new Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
189 "MessageCenterReopnPopupsForSystemPriority",
190 ASCIIToUTF16("Test Web Notification"),
191 ASCIIToUTF16("Notification message body."),
192 gfx::Image(),
193 ASCIIToUTF16("www.test.org"),
194 DummyNotifierId(),
195 message_center::RichNotificationData(),
196 NULL /* delegate */));
197 notification->SetSystemPriority();
198 message_center_->AddNotification(notification.Pass());
200 ASSERT_TRUE(message_center_tray_->popups_visible());
201 ASSERT_FALSE(message_center_tray_->message_center_visible());
203 bool shown = message_center_tray_->ShowMessageCenterBubble();
204 EXPECT_TRUE(shown);
206 ASSERT_FALSE(message_center_tray_->popups_visible());
207 ASSERT_TRUE(message_center_tray_->message_center_visible());
209 message_center_tray_->HideMessageCenterBubble();
211 ASSERT_TRUE(message_center_tray_->popups_visible());
212 ASSERT_FALSE(message_center_tray_->message_center_visible());
215 TEST_F(MessageCenterTrayTest, ShowBubbleFails) {
216 // Now the delegate will signal that it was unable to show a bubble.
217 delegate_->show_popups_success_ = false;
218 delegate_->show_message_center_success_ = false;
220 ASSERT_FALSE(message_center_tray_->popups_visible());
221 ASSERT_FALSE(message_center_tray_->message_center_visible());
223 AddNotification("ShowBubbleFails");
225 message_center_tray_->ShowPopupBubble();
227 ASSERT_FALSE(message_center_tray_->popups_visible());
228 ASSERT_FALSE(message_center_tray_->message_center_visible());
230 bool shown = message_center_tray_->ShowMessageCenterBubble();
231 EXPECT_FALSE(shown);
233 ASSERT_FALSE(message_center_tray_->popups_visible());
234 ASSERT_FALSE(message_center_tray_->message_center_visible());
236 message_center_tray_->HideMessageCenterBubble();
238 ASSERT_FALSE(message_center_tray_->popups_visible());
239 ASSERT_FALSE(message_center_tray_->message_center_visible());
241 message_center_tray_->ToggleMessageCenterBubble();
243 ASSERT_FALSE(message_center_tray_->popups_visible());
244 ASSERT_FALSE(message_center_tray_->message_center_visible());
246 message_center_tray_->HidePopupBubble();
248 ASSERT_FALSE(message_center_tray_->popups_visible());
249 ASSERT_FALSE(message_center_tray_->message_center_visible());
252 TEST_F(MessageCenterTrayTest, ContextMenuTest) {
253 const std::string id1 = "id1";
254 const std::string id2 = "id2";
255 const std::string id3 = "id3";
256 AddNotification(id1);
258 base::string16 display_source = ASCIIToUTF16("www.test.org");
259 NotifierId notifier_id = DummyNotifierId();
261 NotifierId notifier_id2(NotifierId::APPLICATION, "sample-app");
262 scoped_ptr<Notification> notification(
263 new Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
264 id2,
265 ASCIIToUTF16("Test Web Notification"),
266 ASCIIToUTF16("Notification message body."),
267 gfx::Image(),
268 base::string16() /* empty display source */,
269 notifier_id2,
270 message_center::RichNotificationData(),
271 NULL /* delegate */));
272 message_center_->AddNotification(notification.Pass());
274 AddNotification(id3);
276 scoped_ptr<ui::MenuModel> model(
277 message_center_tray_->CreateNotificationMenuModel(
278 notifier_id, display_source));
279 EXPECT_EQ(2, model->GetItemCount());
280 const int second_command = model->GetCommandIdAt(1);
282 // The second item is to open the settings.
283 EXPECT_TRUE(model->IsEnabledAt(0));
284 EXPECT_TRUE(model->IsEnabledAt(1));
285 model->ActivatedAt(1);
286 EXPECT_TRUE(message_center_tray_->message_center_visible());
288 message_center_tray_->HideMessageCenterBubble();
290 // The first item is to disable notifications from the notifier id. It also
291 // removes all notifications from the same notifier, i.e. id1 and id3.
292 model->ActivatedAt(0);
293 NotificationList::Notifications notifications =
294 message_center_->GetVisibleNotifications();
295 EXPECT_EQ(1u, notifications.size());
296 EXPECT_EQ(id2, (*notifications.begin())->id());
298 // Disables the context menu.
299 delegate_->enable_context_menu_ = false;
301 // id2 doesn't have the display source, so it don't have the menu item for
302 // disabling notifications.
303 model = message_center_tray_->CreateNotificationMenuModel(
304 notifier_id2, base::string16());
305 EXPECT_EQ(1, model->GetItemCount());
306 EXPECT_EQ(second_command, model->GetCommandIdAt(0));
308 // The command itself is disabled because delegate disables context menu.
309 EXPECT_FALSE(model->IsEnabledAt(0));
312 } // namespace message_center