Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(680)

Side by Side Diff: ui/message_center/views/message_popup_collection_unittest.cc

Issue 1913433004: Supporting shrinking/enlarging for notifications (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: review, test for many notifications Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/message_center/views/message_popup_collection.h" 5 #include "ui/message_center/views/message_popup_collection.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <list> 9 #include <list>
10 #include <utility> 10 #include <utility>
11 11
12 #include "base/message_loop/message_loop.h" 12 #include "base/message_loop/message_loop.h"
13 #include "base/strings/string_number_conversions.h" 13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/utf_string_conversions.h" 14 #include "base/strings/utf_string_conversions.h"
15 #include "build/build_config.h" 15 #include "build/build_config.h"
16 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "ui/events/event.h" 17 #include "ui/events/event.h"
18 #include "ui/events/event_constants.h" 18 #include "ui/events/event_constants.h"
19 #include "ui/events/event_utils.h" 19 #include "ui/events/event_utils.h"
20 #include "ui/gfx/display.h" 20 #include "ui/gfx/display.h"
21 #include "ui/gfx/geometry/rect.h" 21 #include "ui/gfx/geometry/rect.h"
22 #include "ui/message_center/fake_message_center.h" 22 #include "ui/message_center/fake_message_center.h"
23 #include "ui/message_center/views/desktop_popup_alignment_delegate.h" 23 #include "ui/message_center/views/desktop_popup_alignment_delegate.h"
24 #include "ui/message_center/views/toast_contents_view.h" 24 #include "ui/message_center/views/toast_contents_view.h"
25 #include "ui/views/test/views_test_base.h" 25 #include "ui/views/test/views_test_base.h"
26 #include "ui/views/widget/widget.h" 26 #include "ui/views/widget/widget.h"
27 #include "ui/views/widget/widget_delegate.h" 27 #include "ui/views/widget/widget_delegate.h"
28 28
29 namespace {
30
31 std::unique_ptr<message_center::Notification> CreateTestNotification(
32 std::string id,
33 std::string text) {
34 return base::WrapUnique(new message_center::Notification(
35 message_center::NOTIFICATION_TYPE_BASE_FORMAT, id,
36 base::UTF8ToUTF16("test title"), base::ASCIIToUTF16(text), gfx::Image(),
37 base::string16() /* display_source */, GURL(),
38 message_center::NotifierId(message_center::NotifierId::APPLICATION, id),
39 message_center::RichNotificationData(),
40 new message_center::NotificationDelegate()));
41 }
42
43 } // namespace
44
29 namespace message_center { 45 namespace message_center {
30 namespace test { 46 namespace test {
31 47
32 class MessagePopupCollectionTest : public views::ViewsTestBase { 48 class MessagePopupCollectionTest : public views::ViewsTestBase {
33 public: 49 public:
34 void SetUp() override { 50 void SetUp() override {
35 views::ViewsTestBase::SetUp(); 51 views::ViewsTestBase::SetUp();
36 MessageCenter::Initialize(); 52 MessageCenter::Initialize();
37 MessageCenter::Get()->DisableTimersForTest(); 53 MessageCenter::Get()->DisableTimersForTest();
38 alignment_delegate_.reset(new DesktopPopupAlignmentDelegate); 54 alignment_delegate_.reset(new DesktopPopupAlignmentDelegate);
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after
259 EXPECT_TRUE(IsToastShown(id0)); 275 EXPECT_TRUE(IsToastShown(id0));
260 EXPECT_TRUE(IsToastShown(id1)); 276 EXPECT_TRUE(IsToastShown(id1));
261 277
262 EXPECT_EQ(r0.width(), r1.width()); 278 EXPECT_EQ(r0.width(), r1.width());
263 EXPECT_EQ(r0.height(), r1.height()); 279 EXPECT_EQ(r0.height(), r1.height());
264 EXPECT_GT(r0.y(), r1.y()); 280 EXPECT_GT(r0.y(), r1.y());
265 EXPECT_EQ(r0.x(), r1.x()); 281 EXPECT_EQ(r0.x(), r1.x());
266 282
267 CloseAllToasts(); 283 CloseAllToasts();
268 EXPECT_EQ(0u, GetToastCounts()); 284 EXPECT_EQ(0u, GetToastCounts());
269
270 // Restore simulated taskbar position to bottom.
271 SetDisplayInfo(gfx::Rect(0, 0, 600, 390), // Work-area.
272 gfx::Rect(0, 0, 600, 400)); // Display-bounds.
273
274 WaitForTransitionsDone(); 285 WaitForTransitionsDone();
275 } 286 }
276 287
277 TEST_F(MessagePopupCollectionTest, TopDownPositioningWithTopTaskbar) { 288 TEST_F(MessagePopupCollectionTest, TopDownPositioningWithTopTaskbar) {
278 // Simulate a taskbar at the top. 289 // Simulate a taskbar at the top.
279 SetDisplayInfo(gfx::Rect(0, 10, 600, 390), // Work-area. 290 SetDisplayInfo(gfx::Rect(0, 10, 600, 390), // Work-area.
280 gfx::Rect(0, 0, 600, 400)); // Display-bounds. 291 gfx::Rect(0, 0, 600, 400)); // Display-bounds.
281 std::string id0 = AddNotification(); 292 std::string id0 = AddNotification();
282 std::string id1 = AddNotification(); 293 std::string id1 = AddNotification();
283 WaitForTransitionsDone(); 294 WaitForTransitionsDone();
284 295
285 gfx::Rect r0 = GetToastRectAt(0); 296 gfx::Rect r0 = GetToastRectAt(0);
286 gfx::Rect r1 = GetToastRectAt(1); 297 gfx::Rect r1 = GetToastRectAt(1);
287 298
288 // 2 toasts are shown, equal size, vertical stack. 299 // 2 toasts are shown, equal size, vertical stack.
289 EXPECT_TRUE(IsToastShown(id0)); 300 EXPECT_TRUE(IsToastShown(id0));
290 EXPECT_TRUE(IsToastShown(id1)); 301 EXPECT_TRUE(IsToastShown(id1));
291 302
292 EXPECT_EQ(r0.width(), r1.width()); 303 EXPECT_EQ(r0.width(), r1.width());
293 EXPECT_EQ(r0.height(), r1.height()); 304 EXPECT_EQ(r0.height(), r1.height());
294 EXPECT_LT(r0.y(), r1.y()); 305 EXPECT_LT(r0.y(), r1.y());
295 EXPECT_EQ(r0.x(), r1.x()); 306 EXPECT_EQ(r0.x(), r1.x());
296 307
297 CloseAllToasts(); 308 CloseAllToasts();
298 EXPECT_EQ(0u, GetToastCounts()); 309 EXPECT_EQ(0u, GetToastCounts());
299 WaitForTransitionsDone(); 310 WaitForTransitionsDone();
300
301 // Restore simulated taskbar position to bottom.
302 SetDisplayInfo(gfx::Rect(0, 0, 600, 390), // Work-area.
303 gfx::Rect(0, 0, 600, 400)); // Display-bounds.
304 } 311 }
305 312
306 TEST_F(MessagePopupCollectionTest, TopDownPositioningWithLeftAndTopTaskbar) { 313 TEST_F(MessagePopupCollectionTest, TopDownPositioningWithLeftAndTopTaskbar) {
307 // If there "seems" to be a taskbar on left and top (like in Unity), it is 314 // If there "seems" to be a taskbar on left and top (like in Unity), it is
308 // assumed that the actual taskbar is the top one. 315 // assumed that the actual taskbar is the top one.
309 316
310 // Simulate a taskbar at the top and left. 317 // Simulate a taskbar at the top and left.
311 SetDisplayInfo(gfx::Rect(10, 10, 590, 390), // Work-area. 318 SetDisplayInfo(gfx::Rect(10, 10, 590, 390), // Work-area.
312 gfx::Rect(0, 0, 600, 400)); // Display-bounds. 319 gfx::Rect(0, 0, 600, 400)); // Display-bounds.
313 std::string id0 = AddNotification(); 320 std::string id0 = AddNotification();
314 std::string id1 = AddNotification(); 321 std::string id1 = AddNotification();
315 WaitForTransitionsDone(); 322 WaitForTransitionsDone();
316 323
317 gfx::Rect r0 = GetToastRectAt(0); 324 gfx::Rect r0 = GetToastRectAt(0);
318 gfx::Rect r1 = GetToastRectAt(1); 325 gfx::Rect r1 = GetToastRectAt(1);
319 326
320 // 2 toasts are shown, equal size, vertical stack. 327 // 2 toasts are shown, equal size, vertical stack.
321 EXPECT_TRUE(IsToastShown(id0)); 328 EXPECT_TRUE(IsToastShown(id0));
322 EXPECT_TRUE(IsToastShown(id1)); 329 EXPECT_TRUE(IsToastShown(id1));
323 330
324 EXPECT_EQ(r0.width(), r1.width()); 331 EXPECT_EQ(r0.width(), r1.width());
325 EXPECT_EQ(r0.height(), r1.height()); 332 EXPECT_EQ(r0.height(), r1.height());
326 EXPECT_LT(r0.y(), r1.y()); 333 EXPECT_LT(r0.y(), r1.y());
327 EXPECT_EQ(r0.x(), r1.x()); 334 EXPECT_EQ(r0.x(), r1.x());
328 335
329 CloseAllToasts(); 336 CloseAllToasts();
330 EXPECT_EQ(0u, GetToastCounts()); 337 EXPECT_EQ(0u, GetToastCounts());
331 WaitForTransitionsDone(); 338 WaitForTransitionsDone();
332
333 // Restore simulated taskbar position to bottom.
334 SetDisplayInfo(gfx::Rect(0, 0, 600, 390), // Work-area.
335 gfx::Rect(0, 0, 600, 400)); // Display-bounds.
336 } 339 }
337 340
338 TEST_F(MessagePopupCollectionTest, TopDownPositioningWithBottomAndTopTaskbar) { 341 TEST_F(MessagePopupCollectionTest, TopDownPositioningWithBottomAndTopTaskbar) {
339 // If there "seems" to be a taskbar on bottom and top (like in Gnome), it is 342 // If there "seems" to be a taskbar on bottom and top (like in Gnome), it is
340 // assumed that the actual taskbar is the top one. 343 // assumed that the actual taskbar is the top one.
341 344
342 // Simulate a taskbar at the top and bottom. 345 // Simulate a taskbar at the top and bottom.
343 SetDisplayInfo(gfx::Rect(0, 10, 580, 400), // Work-area. 346 SetDisplayInfo(gfx::Rect(0, 10, 580, 400), // Work-area.
344 gfx::Rect(0, 0, 600, 400)); // Display-bounds. 347 gfx::Rect(0, 0, 600, 400)); // Display-bounds.
345 std::string id0 = AddNotification(); 348 std::string id0 = AddNotification();
346 std::string id1 = AddNotification(); 349 std::string id1 = AddNotification();
347 WaitForTransitionsDone(); 350 WaitForTransitionsDone();
348 351
349 gfx::Rect r0 = GetToastRectAt(0); 352 gfx::Rect r0 = GetToastRectAt(0);
350 gfx::Rect r1 = GetToastRectAt(1); 353 gfx::Rect r1 = GetToastRectAt(1);
351 354
352 // 2 toasts are shown, equal size, vertical stack. 355 // 2 toasts are shown, equal size, vertical stack.
353 EXPECT_TRUE(IsToastShown(id0)); 356 EXPECT_TRUE(IsToastShown(id0));
354 EXPECT_TRUE(IsToastShown(id1)); 357 EXPECT_TRUE(IsToastShown(id1));
355 358
356 EXPECT_EQ(r0.width(), r1.width()); 359 EXPECT_EQ(r0.width(), r1.width());
357 EXPECT_EQ(r0.height(), r1.height()); 360 EXPECT_EQ(r0.height(), r1.height());
358 EXPECT_LT(r0.y(), r1.y()); 361 EXPECT_LT(r0.y(), r1.y());
359 EXPECT_EQ(r0.x(), r1.x()); 362 EXPECT_EQ(r0.x(), r1.x());
360 363
361 CloseAllToasts(); 364 CloseAllToasts();
362 EXPECT_EQ(0u, GetToastCounts()); 365 EXPECT_EQ(0u, GetToastCounts());
363 WaitForTransitionsDone(); 366 WaitForTransitionsDone();
364
365 // Restore simulated taskbar position to bottom.
366 SetDisplayInfo(gfx::Rect(0, 0, 600, 390), // Work-area.
367 gfx::Rect(0, 0, 600, 400)); // Display-bounds.
368 } 367 }
369 368
370 TEST_F(MessagePopupCollectionTest, LeftPositioningWithLeftTaskbar) { 369 TEST_F(MessagePopupCollectionTest, LeftPositioningWithLeftTaskbar) {
371 // Simulate a taskbar at the left. 370 // Simulate a taskbar at the left.
372 SetDisplayInfo(gfx::Rect(10, 0, 590, 400), // Work-area. 371 SetDisplayInfo(gfx::Rect(10, 0, 590, 400), // Work-area.
373 gfx::Rect(0, 0, 600, 400)); // Display-bounds. 372 gfx::Rect(0, 0, 600, 400)); // Display-bounds.
374 std::string id0 = AddNotification(); 373 std::string id0 = AddNotification();
375 std::string id1 = AddNotification(); 374 std::string id1 = AddNotification();
376 WaitForTransitionsDone(); 375 WaitForTransitionsDone();
377 376
378 gfx::Rect r0 = GetToastRectAt(0); 377 gfx::Rect r0 = GetToastRectAt(0);
379 gfx::Rect r1 = GetToastRectAt(1); 378 gfx::Rect r1 = GetToastRectAt(1);
380 379
381 // 2 toasts are shown, equal size, vertical stack. 380 // 2 toasts are shown, equal size, vertical stack.
382 EXPECT_TRUE(IsToastShown(id0)); 381 EXPECT_TRUE(IsToastShown(id0));
383 EXPECT_TRUE(IsToastShown(id1)); 382 EXPECT_TRUE(IsToastShown(id1));
384 383
385 EXPECT_EQ(r0.width(), r1.width()); 384 EXPECT_EQ(r0.width(), r1.width());
386 EXPECT_EQ(r0.height(), r1.height()); 385 EXPECT_EQ(r0.height(), r1.height());
387 EXPECT_GT(r0.y(), r1.y()); 386 EXPECT_GT(r0.y(), r1.y());
388 EXPECT_EQ(r0.x(), r1.x()); 387 EXPECT_EQ(r0.x(), r1.x());
389 388
390 // Ensure that toasts are on the left. 389 // Ensure that toasts are on the left.
391 EXPECT_LT(r1.x(), GetWorkArea().CenterPoint().x()); 390 EXPECT_LT(r1.x(), GetWorkArea().CenterPoint().x());
392 391
393 CloseAllToasts(); 392 CloseAllToasts();
394 EXPECT_EQ(0u, GetToastCounts()); 393 EXPECT_EQ(0u, GetToastCounts());
395 WaitForTransitionsDone(); 394 WaitForTransitionsDone();
396
397 // Restore simulated taskbar position to bottom.
398 SetDisplayInfo(gfx::Rect(0, 0, 600, 390), // Work-area.
399 gfx::Rect(0, 0, 600, 400)); // Display-bounds.
400 } 395 }
401 396
402 TEST_F(MessagePopupCollectionTest, DetectMouseHover) { 397 TEST_F(MessagePopupCollectionTest, DetectMouseHover) {
403 std::string id0 = AddNotification(); 398 std::string id0 = AddNotification();
404 std::string id1 = AddNotification(); 399 std::string id1 = AddNotification();
405 WaitForTransitionsDone(); 400 WaitForTransitionsDone();
406 401
407 views::WidgetDelegateView* toast0 = GetToast(id0); 402 views::WidgetDelegateView* toast0 = GetToast(id0);
408 EXPECT_TRUE(toast0 != NULL); 403 EXPECT_TRUE(toast0 != NULL);
409 views::WidgetDelegateView* toast1 = GetToast(id1); 404 views::WidgetDelegateView* toast1 = GetToast(id1);
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
513 WaitForTransitionsDone(); 508 WaitForTransitionsDone();
514 509
515 // Confirms that there is no toast. 510 // Confirms that there is no toast.
516 EXPECT_EQ(0u, GetToastCounts()); 511 EXPECT_EQ(0u, GetToastCounts());
517 // But the notification still exists. 512 // But the notification still exists.
518 EXPECT_EQ(1u, MessageCenter::Get()->NotificationCount()); 513 EXPECT_EQ(1u, MessageCenter::Get()->NotificationCount());
519 } 514 }
520 515
521 #endif // defined(OS_CHROMEOS) 516 #endif // defined(OS_CHROMEOS)
522 517
518 // When notifications were displayed on top, change of notification
519 // size didn't affect corresponding widget.
520 TEST_F(MessagePopupCollectionTest, ChangingNotificationSize) {
521 // Simulate a taskbar at the top.
522 SetDisplayInfo(gfx::Rect(0, 10, 600, 390), // Work-area.
523 gfx::Rect(0, 0, 600, 400)); // Display-bounds.
524
525 struct {
526 std::string name;
527 std::string text;
528 } updates[] = {
529 {"shrinking", ""},
530 {"enlarging", "abc\ndef\nghk\n"},
531 {"restoring", "abc\n"},
532 };
533
534 std::vector<std::string> notification_ids;
535 // adding notifications
536 {
537 constexpr int max_visible_popup_notifications = 3;
538 notification_ids.reserve(max_visible_popup_notifications);
539 std::generate_n(std::back_inserter(notification_ids),
540 max_visible_popup_notifications,
541 [this] { return AddNotification(); });
542 }
543
544 // helper to check, that notifications borders have the same size
545 // as their widgets at any time
546 auto check_notifications = [this, &notification_ids](const std::string& msg) {
547 for (const std::string& id : notification_ids) {
548 auto* toast = GetToast(id);
549 ASSERT_TRUE(toast) << "no toast for: " << id << "\ntest case: " << msg;
550 auto* widget = GetWidget(id);
551 ASSERT_TRUE(widget) << "no widget for: " << id << "\ntest case: " << msg;
552 EXPECT_TRUE(toast->bounds().height() ==
553 widget->GetWindowBoundsInScreen().height() &&
554 toast->bounds().width() ==
555 widget->GetWindowBoundsInScreen().width())
Jun Mukai 2016/05/05 18:01:51 nit: EXPECT_EQ(toast->bounds().size(), widget->Get
556 << "size of toast contents doesn't match widget's size"
557 << "\nfor id: " << id << "\ntest case: " << msg
558 << "\ntoast width: " << toast->bounds().width()
559 << "\nwidget width: " << widget->GetWindowBoundsInScreen().width()
560 << "\ntoast height: " << toast->bounds().height()
561 << "\nwidget height: " << widget->GetWindowBoundsInScreen().height();
Jun Mukai 2016/05/05 18:01:51 In case you have to keep EXPECT_TRUE, you may want
562 }
563 };
564
565 // checks not updated notifications
566 {
567 WaitForTransitionsDone();
568 ASSERT_NO_FATAL_FAILURE(
569 check_notifications("before updating, animation is done"));
570 }
571
572 // updating notifications one by one
573 for (const std::string& id : notification_ids) {
574 for (const auto& update : updates) {
575 MessageCenter::Get()->UpdateNotification(
576 id, CreateTestNotification(id, update.text));
577 ASSERT_NO_FATAL_FAILURE(check_notifications(id + " " + update.name +
578 " animation in progress"));
579 WaitForTransitionsDone();
580 ASSERT_NO_FATAL_FAILURE(
581 check_notifications(id + " " + update.name + " animation is done"));
582 }
583 }
584
585 CloseAllToasts();
586 WaitForTransitionsDone();
587 }
588
523 } // namespace test 589 } // namespace test
524 } // namespace message_center 590 } // namespace message_center
OLDNEW
« no previous file with comments | « no previous file | ui/message_center/views/toast_contents_view.h » ('j') | ui/message_center/views/toast_contents_view.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698