notification_counter_view_unittest.cc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Copyright 2020 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/system/unified/notification_counter_view.h"
  5. #include "ash/constants/ash_features.h"
  6. #include "ash/system/unified/notification_icons_controller.h"
  7. #include "ash/system/unified/unified_system_tray.h"
  8. #include "ash/test/ash_test_base.h"
  9. #include "base/strings/string_number_conversions.h"
  10. #include "base/test/scoped_feature_list.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. #include "ui/gfx/image/image.h"
  13. #include "ui/message_center/message_center.h"
  14. #include "ui/message_center/public/cpp/notification.h"
  15. #include "ui/message_center/public/cpp/notification_types.h"
  16. #include "ui/message_center/public/cpp/notifier_id.h"
  17. #include "url/gurl.h"
  18. namespace ash {
  19. namespace {
  20. void AddNotification(const std::string& notification_id,
  21. bool is_pinned = false) {
  22. message_center::RichNotificationData rich_notification_data;
  23. rich_notification_data.pinned = is_pinned;
  24. message_center::MessageCenter::Get()->AddNotification(
  25. std::make_unique<message_center::Notification>(
  26. message_center::NOTIFICATION_TYPE_SIMPLE, notification_id,
  27. u"test_title", u"test message", ui::ImageModel(),
  28. /*display_source=*/std::u16string(), GURL(),
  29. message_center::NotifierId(message_center::NotifierType::APPLICATION,
  30. "app"),
  31. rich_notification_data, new message_center::NotificationDelegate()));
  32. }
  33. } // namespace
  34. class NotificationCounterViewTest : public AshTestBase,
  35. public testing::WithParamInterface<bool> {
  36. public:
  37. NotificationCounterViewTest() = default;
  38. NotificationCounterViewTest(const NotificationCounterViewTest&) = delete;
  39. NotificationCounterViewTest& operator=(const NotificationCounterViewTest&) =
  40. delete;
  41. ~NotificationCounterViewTest() override = default;
  42. // AshTestBase:
  43. void SetUp() override {
  44. AshTestBase::SetUp();
  45. scoped_feature_list_.InitWithFeatureState(features::kScalableStatusArea,
  46. IsScalableStatusAreaEnabled());
  47. tray_ = std::make_unique<UnifiedSystemTray>(GetPrimaryShelf());
  48. notification_icons_controller_ =
  49. std::make_unique<NotificationIconsController>(tray_.get());
  50. notification_icons_controller_->AddNotificationTrayItems(
  51. tray_->tray_container());
  52. notification_counter_view_ =
  53. notification_icons_controller_->notification_counter_view();
  54. }
  55. bool IsScalableStatusAreaEnabled() { return GetParam(); }
  56. void TearDown() override {
  57. notification_icons_controller_.reset();
  58. tray_.reset();
  59. AshTestBase::TearDown();
  60. }
  61. protected:
  62. NotificationCounterView* notification_counter_view() {
  63. return notification_counter_view_;
  64. }
  65. private:
  66. base::test::ScopedFeatureList scoped_feature_list_;
  67. std::unique_ptr<UnifiedSystemTray> tray_;
  68. std::unique_ptr<NotificationIconsController> notification_icons_controller_;
  69. NotificationCounterView* notification_counter_view_;
  70. };
  71. INSTANTIATE_TEST_SUITE_P(All,
  72. NotificationCounterViewTest,
  73. testing::Bool() /* IsScalableStatusAreaEnabled() */);
  74. TEST_P(NotificationCounterViewTest, CountForDisplay) {
  75. // Not visible when count == 0.
  76. notification_counter_view()->Update();
  77. EXPECT_EQ(0, notification_counter_view()->count_for_display_for_testing());
  78. EXPECT_FALSE(notification_counter_view()->GetVisible());
  79. // Count is visible and updates between 1..max+1.
  80. int max = static_cast<int>(kTrayNotificationMaxCount);
  81. for (int i = 1; i <= max + 1; i++) {
  82. AddNotification(base::NumberToString(i));
  83. notification_counter_view()->Update();
  84. EXPECT_EQ(i, notification_counter_view()->count_for_display_for_testing());
  85. EXPECT_TRUE(notification_counter_view()->GetVisible());
  86. }
  87. // Count does not change after max+1.
  88. AddNotification(base::NumberToString(max + 2));
  89. notification_counter_view()->Update();
  90. EXPECT_EQ(max + 1,
  91. notification_counter_view()->count_for_display_for_testing());
  92. EXPECT_TRUE(notification_counter_view()->GetVisible());
  93. }
  94. TEST_P(NotificationCounterViewTest, HiddenNotificationCount) {
  95. // Not visible when count == 0.
  96. notification_counter_view()->Update();
  97. EXPECT_EQ(0, notification_counter_view()->count_for_display_for_testing());
  98. EXPECT_FALSE(notification_counter_view()->GetVisible());
  99. // Added a pinned notification, counter should not be visible when the feature
  100. // is enabled.
  101. AddNotification("1", true /* is_pinned */);
  102. notification_counter_view()->Update();
  103. EXPECT_EQ(IsScalableStatusAreaEnabled(),
  104. !notification_counter_view()->GetVisible());
  105. // Added a normal notification.
  106. AddNotification("2");
  107. notification_counter_view()->Update();
  108. int expected_count = IsScalableStatusAreaEnabled() ? 1 : 2;
  109. EXPECT_TRUE(notification_counter_view()->GetVisible());
  110. EXPECT_EQ(expected_count,
  111. notification_counter_view()->count_for_display_for_testing());
  112. // Added another pinned.
  113. AddNotification("3", true /* is_pinned */);
  114. notification_counter_view()->Update();
  115. expected_count = IsScalableStatusAreaEnabled() ? 1 : 3;
  116. EXPECT_TRUE(notification_counter_view()->GetVisible());
  117. EXPECT_EQ(expected_count,
  118. notification_counter_view()->count_for_display_for_testing());
  119. message_center::MessageCenter::Get()->RemoveNotification("1",
  120. false /* by_user */);
  121. message_center::MessageCenter::Get()->RemoveNotification("3",
  122. false /* by_user */);
  123. notification_counter_view()->Update();
  124. EXPECT_EQ(1, notification_counter_view()->count_for_display_for_testing());
  125. }
  126. TEST_P(NotificationCounterViewTest, DisplayChanged) {
  127. AddNotification("1", true /* is_pinned */);
  128. notification_counter_view()->Update();
  129. // In medium size screen, the counter should not be displayed since pinned
  130. // notification icon is shown (if the feature is enabled).
  131. UpdateDisplay("800x700");
  132. EXPECT_EQ(IsScalableStatusAreaEnabled(),
  133. !notification_counter_view()->GetVisible());
  134. // The counter should not be shown when we remove the pinned notification.
  135. message_center::MessageCenter::Get()->RemoveNotification("1",
  136. false /* by_user */);
  137. notification_counter_view()->Update();
  138. EXPECT_FALSE(notification_counter_view()->GetVisible());
  139. AddNotification("1", true /* is_pinned */);
  140. notification_counter_view()->Update();
  141. // In small display, the counter show be shown with pinned notification.
  142. UpdateDisplay("600x500");
  143. EXPECT_TRUE(notification_counter_view()->GetVisible());
  144. // In large screen size, expected the same behavior like medium screen size.
  145. UpdateDisplay("1680x800");
  146. EXPECT_EQ(IsScalableStatusAreaEnabled(),
  147. !notification_counter_view()->GetVisible());
  148. message_center::MessageCenter::Get()->RemoveNotification("1",
  149. false /* by_user */);
  150. notification_counter_view()->Update();
  151. EXPECT_FALSE(notification_counter_view()->GetVisible());
  152. }
  153. } // namespace ash