notification_icons_controller_unittest.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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_icons_controller.h"
  5. #include "ash/constants/ash_features.h"
  6. #include "ash/constants/notifier_catalogs.h"
  7. #include "ash/public/cpp/notification_utils.h"
  8. #include "ash/public/cpp/vm_camera_mic_constants.h"
  9. #include "ash/system/tray/tray_item_view.h"
  10. #include "ash/system/unified/notification_counter_view.h"
  11. #include "ash/system/unified/unified_system_tray.h"
  12. #include "ash/test/ash_test_base.h"
  13. #include "base/test/scoped_feature_list.h"
  14. #include "ui/message_center/message_center.h"
  15. #include "ui/message_center/public/cpp/notification.h"
  16. namespace ash {
  17. namespace {
  18. const char kCapsLockNotifierId[] = "ash.caps-lock";
  19. const char kBatteryNotificationNotifierId[] = "ash.battery";
  20. const char kUsbNotificationNotifierId[] = "ash.power";
  21. } // namespace
  22. class NotificationIconsControllerTest
  23. : public AshTestBase,
  24. public testing::WithParamInterface<bool> {
  25. public:
  26. NotificationIconsControllerTest() = default;
  27. ~NotificationIconsControllerTest() override = default;
  28. // AshTestBase:
  29. void SetUp() override {
  30. AshTestBase::SetUp();
  31. scoped_feature_list_.InitWithFeatureState(features::kScalableStatusArea,
  32. IsScalableStatusAreaEnabled());
  33. tray_ = std::make_unique<UnifiedSystemTray>(GetPrimaryShelf());
  34. notification_icons_controller_ =
  35. std::make_unique<NotificationIconsController>(tray_.get());
  36. notification_icons_controller_->AddNotificationTrayItems(
  37. tray_->tray_container());
  38. }
  39. bool IsScalableStatusAreaEnabled() { return GetParam(); }
  40. void TearDown() override {
  41. notification_icons_controller_.reset();
  42. tray_.reset();
  43. AshTestBase::TearDown();
  44. }
  45. TrayItemView* separator() {
  46. return notification_icons_controller_->separator_;
  47. }
  48. std::string AddNotification(bool is_pinned,
  49. bool is_critical_warning,
  50. const std::string& app_id = "app") {
  51. std::string id = base::NumberToString(notification_id_++);
  52. auto warning_level =
  53. is_critical_warning
  54. ? message_center::SystemNotificationWarningLevel::CRITICAL_WARNING
  55. : message_center::SystemNotificationWarningLevel::NORMAL;
  56. message_center::RichNotificationData rich_notification_data;
  57. rich_notification_data.pinned = is_pinned;
  58. message_center::MessageCenter::Get()->AddNotification(
  59. CreateSystemNotification(
  60. message_center::NOTIFICATION_TYPE_SIMPLE, id, u"test_title",
  61. u"test message", std::u16string() /*display_source */,
  62. GURL() /* origin_url */,
  63. message_center::NotifierId(
  64. message_center::NotifierType::SYSTEM_COMPONENT, app_id,
  65. NotificationCatalogName::kTestCatalogName),
  66. rich_notification_data, nullptr /* delegate */, gfx::VectorIcon(),
  67. warning_level));
  68. notification_id_++;
  69. return id;
  70. }
  71. protected:
  72. int notification_id_ = 0;
  73. base::test::ScopedFeatureList scoped_feature_list_;
  74. std::unique_ptr<UnifiedSystemTray> tray_;
  75. std::unique_ptr<NotificationIconsController> notification_icons_controller_;
  76. };
  77. INSTANTIATE_TEST_SUITE_P(All,
  78. NotificationIconsControllerTest,
  79. testing::Bool() /* IsScalableStatusAreaEnabled() */);
  80. TEST_P(NotificationIconsControllerTest, DisplayChanged) {
  81. AddNotification(true /* is_pinned */, false /* is_critical_warning */);
  82. AddNotification(false /* is_pinned */, false /* is_critical_warning */);
  83. // Icons get added from RTL, so we check the end of the vector first.
  84. // Notification icons should be shown in medium screen size.
  85. UpdateDisplay("800x700");
  86. EXPECT_EQ(IsScalableStatusAreaEnabled(),
  87. notification_icons_controller_->tray_items().back()->GetVisible());
  88. EXPECT_EQ(IsScalableStatusAreaEnabled(), separator()->GetVisible());
  89. // Notification icons should not be shown in small screen size.
  90. UpdateDisplay("600x500");
  91. EXPECT_FALSE(
  92. notification_icons_controller_->tray_items().back()->GetVisible());
  93. EXPECT_FALSE(separator()->GetVisible());
  94. // Notification icons should be shown in large screen size.
  95. UpdateDisplay("1680x800");
  96. EXPECT_EQ(IsScalableStatusAreaEnabled(),
  97. notification_icons_controller_->tray_items().back()->GetVisible());
  98. EXPECT_EQ(IsScalableStatusAreaEnabled(), separator()->GetVisible());
  99. }
  100. TEST_P(NotificationIconsControllerTest, ShowNotificationIcons) {
  101. UpdateDisplay("800x700");
  102. // Icons get added from RTL, so we check the end of the vector first.
  103. const int end = notification_icons_controller_->tray_items().size() - 1;
  104. // Ensure that the indexes that will be accessed exist.
  105. ASSERT_TRUE(notification_icons_controller_->tray_items().size() >= 2);
  106. // If there's no notification, no notification icons should be shown.
  107. EXPECT_FALSE(notification_icons_controller_->tray_items()[end]->GetVisible());
  108. EXPECT_FALSE(
  109. notification_icons_controller_->tray_items()[end - 1]->GetVisible());
  110. EXPECT_FALSE(separator()->GetVisible());
  111. // Same case for non pinned or non critical warning notification.
  112. AddNotification(false /* is_pinned */, false /* is_critical_warning */);
  113. EXPECT_FALSE(notification_icons_controller_->tray_items()[end]->GetVisible());
  114. EXPECT_FALSE(
  115. notification_icons_controller_->tray_items()[end - 1]->GetVisible());
  116. EXPECT_FALSE(separator()->GetVisible());
  117. // Notification icons should be shown when pinned or critical warning
  118. // notification is added.
  119. std::string id0 =
  120. AddNotification(true /* is_pinned */, false /* is_critical_warning */);
  121. EXPECT_EQ(IsScalableStatusAreaEnabled(),
  122. notification_icons_controller_->tray_items()[end]->GetVisible());
  123. EXPECT_FALSE(
  124. notification_icons_controller_->tray_items()[end - 1]->GetVisible());
  125. EXPECT_EQ(IsScalableStatusAreaEnabled(), separator()->GetVisible());
  126. std::string id1 =
  127. AddNotification(false /* is_pinned */, true /* is_critical_warning */);
  128. EXPECT_EQ(IsScalableStatusAreaEnabled(),
  129. notification_icons_controller_->tray_items()[end]->GetVisible());
  130. EXPECT_EQ(
  131. IsScalableStatusAreaEnabled(),
  132. notification_icons_controller_->tray_items()[end - 1]->GetVisible());
  133. EXPECT_EQ(IsScalableStatusAreaEnabled(), separator()->GetVisible());
  134. // Remove the critical warning notification should make the tray show only one
  135. // icon.
  136. message_center::MessageCenter::Get()->RemoveNotification(id1,
  137. false /* by_user */);
  138. EXPECT_EQ(IsScalableStatusAreaEnabled(),
  139. notification_icons_controller_->tray_items()[end]->GetVisible());
  140. EXPECT_FALSE(
  141. notification_icons_controller_->tray_items()[end - 1]->GetVisible());
  142. EXPECT_EQ(IsScalableStatusAreaEnabled(), separator()->GetVisible());
  143. // Remove the pinned notification, no icon is shown.
  144. message_center::MessageCenter::Get()->RemoveNotification(id0,
  145. false /* by_user */);
  146. EXPECT_FALSE(notification_icons_controller_->tray_items()[end]->GetVisible());
  147. EXPECT_FALSE(
  148. notification_icons_controller_->tray_items()[end - 1]->GetVisible());
  149. EXPECT_FALSE(separator()->GetVisible());
  150. }
  151. TEST_P(NotificationIconsControllerTest, NotShowNotificationIcons) {
  152. UpdateDisplay("800x700");
  153. // Icons get added from RTL, so we check the end of the vector first.
  154. EXPECT_FALSE(
  155. notification_icons_controller_->tray_items().back()->GetVisible());
  156. AddNotification(true /* is_pinned */, false /* is_critical_warning */,
  157. kBatteryNotificationNotifierId);
  158. // Battery notification should not be shown.
  159. EXPECT_FALSE(
  160. notification_icons_controller_->tray_items().back()->GetVisible());
  161. EXPECT_FALSE(separator()->GetVisible());
  162. // Notification count does update for this notification.
  163. notification_icons_controller_->notification_counter_view()->Update();
  164. EXPECT_EQ(1, notification_icons_controller_->notification_counter_view()
  165. ->count_for_display_for_testing());
  166. AddNotification(true /* is_pinned */, false /* is_critical_warning */,
  167. kUsbNotificationNotifierId);
  168. // Usb charging notification should not be shown.
  169. EXPECT_FALSE(
  170. notification_icons_controller_->tray_items().back()->GetVisible());
  171. EXPECT_FALSE(separator()->GetVisible());
  172. // Notification count does update for this notification.
  173. notification_icons_controller_->notification_counter_view()->Update();
  174. EXPECT_EQ(2, notification_icons_controller_->notification_counter_view()
  175. ->count_for_display_for_testing());
  176. AddNotification(true /* is_pinned */, false /* is_critical_warning */,
  177. kVmCameraMicNotifierId);
  178. // VM camera/mic notification should not be shown.
  179. EXPECT_FALSE(
  180. notification_icons_controller_->tray_items().back()->GetVisible());
  181. EXPECT_FALSE(separator()->GetVisible());
  182. // Notification count does not update for this notification (since there's
  183. // another tray item for this).
  184. notification_icons_controller_->notification_counter_view()->Update();
  185. EXPECT_EQ(2, notification_icons_controller_->notification_counter_view()
  186. ->count_for_display_for_testing());
  187. }
  188. TEST_P(NotificationIconsControllerTest, NotificationItemInQuietMode) {
  189. UpdateDisplay("800x700");
  190. message_center::MessageCenter::Get()->SetQuietMode(true);
  191. // Icons get added from RTL, so we check the end of the vector first. At
  192. // first, no icons should be shown.
  193. EXPECT_FALSE(
  194. notification_icons_controller_->tray_items().back()->GetVisible());
  195. // In quiet mode, notification other than capslock notification should not
  196. // show an item in the tray.
  197. auto id1 = AddNotification(/*is_pinned=*/true, /*is_critical_warning=*/false);
  198. EXPECT_FALSE(
  199. notification_icons_controller_->tray_items().back()->GetVisible());
  200. auto id2 = AddNotification(/*is_pinned=*/true, /*is_critical_warning=*/false,
  201. kCapsLockNotifierId);
  202. EXPECT_EQ(IsScalableStatusAreaEnabled(),
  203. notification_icons_controller_->tray_items().back()->GetVisible());
  204. if (IsScalableStatusAreaEnabled()) {
  205. EXPECT_EQ(id2, notification_icons_controller_->tray_items()
  206. .back()
  207. ->GetNotificationId());
  208. }
  209. message_center::MessageCenter::Get()->RemoveNotification(id2,
  210. /*by_user=*/false);
  211. EXPECT_FALSE(
  212. notification_icons_controller_->tray_items().back()->GetVisible());
  213. }
  214. } // namespace ash