notification_overflow_view_unittest.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2018 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.
  4. #include "ash/app_menu/notification_overflow_view.h"
  5. #include <string>
  6. #include "ash/public/cpp/app_menu_constants.h"
  7. #include "base/strings/string_number_conversions.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. #include "ui/message_center/views/proportional_image_view.h"
  10. #include "ui/views/test/views_test_base.h"
  11. namespace ash {
  12. class NotificationOverflowViewTest : public views::ViewsTestBase {
  13. public:
  14. NotificationOverflowViewTest() {}
  15. NotificationOverflowViewTest(const NotificationOverflowViewTest&) = delete;
  16. NotificationOverflowViewTest& operator=(const NotificationOverflowViewTest&) =
  17. delete;
  18. ~NotificationOverflowViewTest() override = default;
  19. // views::ViewsTestBase:
  20. void SetUp() override {
  21. views::ViewsTestBase::SetUp();
  22. notification_overflow_view_ = std::make_unique<NotificationOverflowView>();
  23. }
  24. // Adds a notification and returns the string identifier.
  25. std::string AddNotification() {
  26. message_center::ProportionalImageView image_view(gfx::Size(16, 16));
  27. std::string notification_id =
  28. base::NumberToString(notification_identifier_++);
  29. notification_overflow_view_->AddIcon(image_view, notification_id);
  30. return notification_id;
  31. }
  32. // Checks whether |expected_notification_icons| notification icons are being
  33. // shown in the overflow. Does not include the overflow icon.
  34. void CheckNumberOfNotificationIcons(int expected_notification_icons) {
  35. int actual_notification_icons = 0;
  36. for (auto* v : notification_overflow_view_->GetChildrenInZOrder()) {
  37. if (!v->GetVisible() || v->GetID() != kNotificationOverflowIconId)
  38. continue;
  39. actual_notification_icons++;
  40. }
  41. DCHECK_EQ(expected_notification_icons, actual_notification_icons);
  42. }
  43. // Returns whether the overflow icon is being shown.
  44. bool HasOverflowIcon() {
  45. for (auto* v : notification_overflow_view_->GetChildrenInZOrder()) {
  46. if (v->GetID() != kOverflowIconId)
  47. continue;
  48. return v->GetVisible();
  49. }
  50. return false;
  51. }
  52. NotificationOverflowView* notification_overflow_view() {
  53. return notification_overflow_view_.get();
  54. }
  55. private:
  56. int notification_identifier_ = 0;
  57. std::unique_ptr<NotificationOverflowView> notification_overflow_view_;
  58. };
  59. // Tests that icons get added and removed when notifications come and go.
  60. TEST_F(NotificationOverflowViewTest, Basic) {
  61. // Initially no notification icons should be shown.
  62. CheckNumberOfNotificationIcons(0);
  63. EXPECT_FALSE(HasOverflowIcon());
  64. // Add a notification, an icon should be created.
  65. std::string id = AddNotification();
  66. CheckNumberOfNotificationIcons(1);
  67. EXPECT_FALSE(HasOverflowIcon());
  68. // Try to remove a notification that doesn't exist. Nothing should change.
  69. notification_overflow_view()->RemoveIcon("non-existent-notification-id");
  70. CheckNumberOfNotificationIcons(1);
  71. EXPECT_FALSE(HasOverflowIcon());
  72. // Remove the notification that was added earlier, the icon should be removed.
  73. notification_overflow_view()->RemoveIcon(id);
  74. CheckNumberOfNotificationIcons(0);
  75. EXPECT_FALSE(HasOverflowIcon());
  76. }
  77. // Tests that the overflow icon gets added when more than |kMaxOverflowIcons|
  78. // icons are added.
  79. TEST_F(NotificationOverflowViewTest, OverflowIcon) {
  80. // Add notifications until just before overflow.
  81. for (int i = 0; i < kMaxOverflowIcons; ++i) {
  82. AddNotification();
  83. CheckNumberOfNotificationIcons(i + 1);
  84. EXPECT_FALSE(HasOverflowIcon());
  85. }
  86. // Add one more notification, the overflow icon should appear instead of that
  87. // new notification.
  88. AddNotification();
  89. CheckNumberOfNotificationIcons(kMaxOverflowIcons);
  90. EXPECT_TRUE(HasOverflowIcon());
  91. // Remove any notification that was added. The overflow icon should dissapear.
  92. notification_overflow_view()->RemoveIcon(base::NumberToString(0));
  93. CheckNumberOfNotificationIcons(kMaxOverflowIcons);
  94. EXPECT_FALSE(HasOverflowIcon());
  95. // Add a few more (over |kMaxOverflowIcons|) notifications to show the
  96. // overflow icon.
  97. AddNotification();
  98. AddNotification();
  99. AddNotification();
  100. CheckNumberOfNotificationIcons(kMaxOverflowIcons);
  101. EXPECT_TRUE(HasOverflowIcon());
  102. }
  103. } // namespace ash