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/views/message_popup_collection.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "ui/gfx/display.h"
14 #include "ui/gfx/rect.h"
15 #include "ui/message_center/fake_message_center.h"
16 #include "ui/message_center/views/toast_contents_view.h"
17 #include "ui/views/test/views_test_base.h"
18 #include "ui/views/widget/widget.h"
20 namespace message_center
{
23 class MessagePopupCollectionTest
: public views::ViewsTestBase
{
25 virtual void SetUp() OVERRIDE
{
26 views::ViewsTestBase::SetUp();
27 MessageCenter::Initialize();
28 collection_
.reset(new MessagePopupCollection(
29 GetContext(), MessageCenter::Get(), NULL
, false));
30 // This size fits test machines resolution and also can keep a few toasts
31 // w/o ill effects of hitting the screen overflow. This allows us to assume
32 // and verify normal layout of the toast stack.
33 collection_
->SetDisplayInfo(gfx::Rect(0, 0, 600, 390),
34 gfx::Rect(0, 0, 600, 400)); // Simulate a
40 virtual void TearDown() OVERRIDE
{
41 collection_
->CloseAllWidgets();
43 MessageCenter::Shutdown();
44 views::ViewsTestBase::TearDown();
48 MessagePopupCollection
* collection() { return collection_
.get(); }
50 size_t GetToastCounts() {
51 return collection_
->toasts_
.size();
54 bool IsToastShown(const std::string
& id
) {
55 views::Widget
* widget
= collection_
->GetWidgetForTest(id
);
56 return widget
&& widget
->IsVisible();
59 views::Widget
* GetWidget(const std::string
& id
) {
60 return collection_
->GetWidgetForTest(id
);
63 gfx::Rect
GetWorkArea() {
64 return collection_
->work_area_
;
67 ToastContentsView
* GetToast(const std::string
& id
) {
68 for (MessagePopupCollection::Toasts::iterator iter
=
69 collection_
->toasts_
.begin();
70 iter
!= collection_
->toasts_
.end(); ++iter
) {
71 if ((*iter
)->id() == id
)
77 std::string
AddNotification() {
78 std::string id
= base::IntToString(id_
++);
79 scoped_ptr
<Notification
> notification(
80 new Notification(NOTIFICATION_TYPE_BASE_FORMAT
,
82 UTF8ToUTF16("test title"),
83 UTF8ToUTF16("test message"),
85 string16() /* display_source */,
86 "" /* extension_id */,
87 message_center::RichNotificationData(),
88 NULL
/* delegate */));
89 MessageCenter::Get()->AddNotification(notification
.Pass());
93 // Assumes there is non-zero pending work.
94 void WaitForTransitionsDone() {
95 collection_
->RunLoopForTest();
98 void CloseAllToastsAndWait() {
99 // Assumes there is at least one toast to close.
100 EXPECT_TRUE(GetToastCounts() > 0);
101 MessageCenter::Get()->RemoveAllNotifications(false);
102 WaitForTransitionsDone();
105 gfx::Rect
GetToastRectAt(size_t index
) {
106 return collection_
->GetToastRectAt(index
);
110 scoped_ptr
<MessagePopupCollection
> collection_
;
114 TEST_F(MessagePopupCollectionTest
, DismissOnClick
) {
115 std::string id1
= AddNotification();
116 std::string id2
= AddNotification();
117 WaitForTransitionsDone();
118 EXPECT_EQ(2u, GetToastCounts());
119 EXPECT_TRUE(IsToastShown(id1
));
120 EXPECT_TRUE(IsToastShown(id2
));
122 MessageCenter::Get()->ClickOnNotification(id2
);
123 WaitForTransitionsDone();
124 EXPECT_EQ(1u, GetToastCounts());
125 EXPECT_TRUE(IsToastShown(id1
));
126 EXPECT_FALSE(IsToastShown(id2
));
128 MessageCenter::Get()->ClickOnNotificationButton(id1
, 0);
129 WaitForTransitionsDone();
130 EXPECT_EQ(0u, GetToastCounts());
131 EXPECT_FALSE(IsToastShown(id1
));
132 EXPECT_FALSE(IsToastShown(id2
));
135 TEST_F(MessagePopupCollectionTest
, ShutdownDuringShowing
) {
136 std::string id1
= AddNotification();
137 std::string id2
= AddNotification();
138 WaitForTransitionsDone();
139 EXPECT_EQ(2u, GetToastCounts());
140 EXPECT_TRUE(IsToastShown(id1
));
141 EXPECT_TRUE(IsToastShown(id2
));
143 // Finish without cleanup of notifications, which may cause use-after-free.
144 // See crbug.com/236448
145 GetWidget(id1
)->CloseNow();
146 collection()->OnMouseExited(GetToast(id2
));
149 TEST_F(MessagePopupCollectionTest
, DefaultPositioning
) {
150 std::string id0
= AddNotification();
151 std::string id1
= AddNotification();
152 std::string id2
= AddNotification();
153 std::string id3
= AddNotification();
154 WaitForTransitionsDone();
156 gfx::Rect r0
= GetToastRectAt(0);
157 gfx::Rect r1
= GetToastRectAt(1);
158 gfx::Rect r2
= GetToastRectAt(2);
159 gfx::Rect r3
= GetToastRectAt(3);
161 // 3 toasts are shown, equal size, vertical stack.
162 EXPECT_TRUE(IsToastShown(id0
));
163 EXPECT_TRUE(IsToastShown(id1
));
164 EXPECT_TRUE(IsToastShown(id2
));
166 EXPECT_EQ(r0
.width(), r1
.width());
167 EXPECT_EQ(r1
.width(), r2
.width());
169 EXPECT_EQ(r0
.height(), r1
.height());
170 EXPECT_EQ(r1
.height(), r2
.height());
172 EXPECT_GT(r0
.y(), r1
.y());
173 EXPECT_GT(r1
.y(), r2
.y());
175 EXPECT_EQ(r0
.x(), r1
.x());
176 EXPECT_EQ(r1
.x(), r2
.x());
178 // The 4th toast is not shown yet.
179 EXPECT_FALSE(IsToastShown(id3
));
180 EXPECT_EQ(0, r3
.width());
181 EXPECT_EQ(0, r3
.height());
183 CloseAllToastsAndWait();
184 EXPECT_EQ(0u, GetToastCounts());
187 TEST_F(MessagePopupCollectionTest
, DefaultPositioningWithRightTaskbar
) {
188 // If taskbar is on the right we show the toasts bottom to top as usual.
190 // Simulate a taskbar at the right.
191 collection()->SetDisplayInfo(gfx::Rect(0, 0, 590, 400), // Work-area.
192 gfx::Rect(0, 0, 600, 400)); // Display-bounds.
193 std::string id0
= AddNotification();
194 std::string id1
= AddNotification();
195 WaitForTransitionsDone();
197 gfx::Rect r0
= GetToastRectAt(0);
198 gfx::Rect r1
= GetToastRectAt(1);
200 // 2 toasts are shown, equal size, vertical stack.
201 EXPECT_TRUE(IsToastShown(id0
));
202 EXPECT_TRUE(IsToastShown(id1
));
204 EXPECT_EQ(r0
.width(), r1
.width());
205 EXPECT_EQ(r0
.height(), r1
.height());
206 EXPECT_GT(r0
.y(), r1
.y());
207 EXPECT_EQ(r0
.x(), r1
.x());
209 CloseAllToastsAndWait();
210 EXPECT_EQ(0u, GetToastCounts());
212 // Restore simulated taskbar position to bottom.
213 collection()->SetDisplayInfo(gfx::Rect(0, 0, 600, 390), // Work-area.
214 gfx::Rect(0, 0, 600, 400)); // Display-bounds.
217 TEST_F(MessagePopupCollectionTest
, TopDownPositioningWithTopTaskbar
) {
218 // Simulate a taskbar at the top.
219 collection()->SetDisplayInfo(gfx::Rect(0, 10, 600, 390), // Work-area.
220 gfx::Rect(0, 0, 600, 400)); // Display-bounds.
221 std::string id0
= AddNotification();
222 std::string id1
= AddNotification();
223 WaitForTransitionsDone();
225 gfx::Rect r0
= GetToastRectAt(0);
226 gfx::Rect r1
= GetToastRectAt(1);
228 // 2 toasts are shown, equal size, vertical stack.
229 EXPECT_TRUE(IsToastShown(id0
));
230 EXPECT_TRUE(IsToastShown(id1
));
232 EXPECT_EQ(r0
.width(), r1
.width());
233 EXPECT_EQ(r0
.height(), r1
.height());
234 EXPECT_LT(r0
.y(), r1
.y());
235 EXPECT_EQ(r0
.x(), r1
.x());
237 CloseAllToastsAndWait();
238 EXPECT_EQ(0u, GetToastCounts());
240 // Restore simulated taskbar position to bottom.
241 collection()->SetDisplayInfo(gfx::Rect(0, 0, 600, 390), // Work-area.
242 gfx::Rect(0, 0, 600, 400)); // Display-bounds.
245 TEST_F(MessagePopupCollectionTest
, TopDownPositioningWithLeftAndTopTaskbar
) {
246 // If there "seems" to be a taskbar on left and top (like in Unity), it is
247 // assumed that the actual taskbar is the top one.
249 // Simulate a taskbar at the top and left.
250 collection()->SetDisplayInfo(gfx::Rect(10, 10, 590, 390), // Work-area.
251 gfx::Rect(0, 0, 600, 400)); // Display-bounds.
252 std::string id0
= AddNotification();
253 std::string id1
= AddNotification();
254 WaitForTransitionsDone();
256 gfx::Rect r0
= GetToastRectAt(0);
257 gfx::Rect r1
= GetToastRectAt(1);
259 // 2 toasts are shown, equal size, vertical stack.
260 EXPECT_TRUE(IsToastShown(id0
));
261 EXPECT_TRUE(IsToastShown(id1
));
263 EXPECT_EQ(r0
.width(), r1
.width());
264 EXPECT_EQ(r0
.height(), r1
.height());
265 EXPECT_LT(r0
.y(), r1
.y());
266 EXPECT_EQ(r0
.x(), r1
.x());
268 CloseAllToastsAndWait();
269 EXPECT_EQ(0u, GetToastCounts());
271 // Restore simulated taskbar position to bottom.
272 collection()->SetDisplayInfo(gfx::Rect(0, 0, 600, 390), // Work-area.
273 gfx::Rect(0, 0, 600, 400)); // Display-bounds.
276 TEST_F(MessagePopupCollectionTest
, TopDownPositioningWithBottomAndTopTaskbar
) {
277 // If there "seems" to be a taskbar on bottom and top (like in Gnome), it is
278 // assumed that the actual taskbar is the top one.
280 // Simulate a taskbar at the top and bottom.
281 collection()->SetDisplayInfo(gfx::Rect(0, 10, 580, 400), // Work-area.
282 gfx::Rect(0, 0, 600, 400)); // Display-bounds.
283 std::string id0
= AddNotification();
284 std::string id1
= AddNotification();
285 WaitForTransitionsDone();
287 gfx::Rect r0
= GetToastRectAt(0);
288 gfx::Rect r1
= GetToastRectAt(1);
290 // 2 toasts are shown, equal size, vertical stack.
291 EXPECT_TRUE(IsToastShown(id0
));
292 EXPECT_TRUE(IsToastShown(id1
));
294 EXPECT_EQ(r0
.width(), r1
.width());
295 EXPECT_EQ(r0
.height(), r1
.height());
296 EXPECT_LT(r0
.y(), r1
.y());
297 EXPECT_EQ(r0
.x(), r1
.x());
299 CloseAllToastsAndWait();
300 EXPECT_EQ(0u, GetToastCounts());
302 // Restore simulated taskbar position to bottom.
303 collection()->SetDisplayInfo(gfx::Rect(0, 0, 600, 390), // Work-area.
304 gfx::Rect(0, 0, 600, 400)); // Display-bounds.
307 TEST_F(MessagePopupCollectionTest
, LeftPositioningWithLeftTaskbar
) {
308 // Simulate a taskbar at the left.
309 collection()->SetDisplayInfo(gfx::Rect(10, 0, 590, 400), // Work-area.
310 gfx::Rect(0, 0, 600, 400)); // Display-bounds.
311 std::string id0
= AddNotification();
312 std::string id1
= AddNotification();
313 WaitForTransitionsDone();
315 gfx::Rect r0
= GetToastRectAt(0);
316 gfx::Rect r1
= GetToastRectAt(1);
318 // 2 toasts are shown, equal size, vertical stack.
319 EXPECT_TRUE(IsToastShown(id0
));
320 EXPECT_TRUE(IsToastShown(id1
));
322 EXPECT_EQ(r0
.width(), r1
.width());
323 EXPECT_EQ(r0
.height(), r1
.height());
324 EXPECT_GT(r0
.y(), r1
.y());
325 EXPECT_EQ(r0
.x(), r1
.x());
327 // Ensure that toasts are on the left.
328 EXPECT_LT(r1
.x(), GetWorkArea().CenterPoint().x());
330 CloseAllToastsAndWait();
331 EXPECT_EQ(0u, GetToastCounts());
333 // Restore simulated taskbar position to bottom.
334 collection()->SetDisplayInfo(gfx::Rect(0, 0, 600, 390), // Work-area.
335 gfx::Rect(0, 0, 600, 400)); // Display-bounds.
338 // TODO(dimich): Test repositioning - both normal one and when user is closing
342 } // namespace message_center