Get foreground tab on Android
[chromium-blink-merge.git] / ui / message_center / message_center_tray_unittest.cc
blob6a68b2b989f4037d2b3f222ebe00a17320fb75bd
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 {
13 namespace {
15 class MockDelegate : public MessageCenterTrayDelegate {
16 public:
17 MockDelegate()
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 {
31 return false;
34 virtual MessageCenterTray* GetMessageCenterTray() OVERRIDE {
35 return NULL;
38 bool show_popups_success_;
39 bool show_message_center_success_;
41 private:
42 DISALLOW_COPY_AND_ASSIGN(MockDelegate);
45 class MessageCenterTrayTest : public testing::Test {
46 public:
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();
60 delegate_.reset();
61 message_center_ = NULL;
62 MessageCenter::Shutdown();
65 protected:
66 void AddNotification(const std::string& id) {
67 scoped_ptr<Notification> notification(
68 new Notification(message_center::NOTIFICATION_TYPE_SIMPLE,
69 id,
70 ASCIIToUTF16("Test Web Notification"),
71 ASCIIToUTF16("Notification message body."),
72 gfx::Image(),
73 ASCIIToUTF16("www.test.org"),
74 NotifierId(),
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_;
83 private:
84 DISALLOW_COPY_AND_ASSIGN(MessageCenterTrayTest);
87 } // namespace
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();
94 EXPECT_TRUE(shown);
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();
145 EXPECT_TRUE(shown);
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."),
179 gfx::Image(),
180 ASCIIToUTF16("www.test.org"),
181 NotifierId(),
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();
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_->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