adaptive_charging_notification_controller_unittest.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright 2022 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/power/adaptive_charging_notification_controller.h"
  5. #include <string>
  6. #include "ash/constants/ash_pref_names.h"
  7. #include "ash/session/session_controller_impl.h"
  8. #include "ash/shell.h"
  9. #include "ash/test/ash_test_base.h"
  10. #include "base/i18n/rtl.h"
  11. #include "base/run_loop.h"
  12. #include "base/test/icu_test_util.h"
  13. #include "base/time/time.h"
  14. #include "base/time/time_override.h"
  15. #include "ui/message_center/message_center.h"
  16. namespace ash {
  17. namespace {
  18. const base::Time::Exploded kTestDateTimeExploded = {
  19. 2022, 4, 5, 29, // Fri, Apr 29, 2022
  20. 2, 42, 7, 0 // 2:42:07.000 in UTC = 12:42:07 in Australia AEST.
  21. };
  22. // Enables or disables the user pref for the entire feature.
  23. void SetAdaptiveChargingPref(bool enabled) {
  24. Shell::Get()->session_controller()->GetActivePrefService()->SetBoolean(
  25. prefs::kPowerAdaptiveChargingEnabled, enabled);
  26. base::RunLoop().RunUntilIdle();
  27. }
  28. // Returns the number of (popup or non-popup) notifications that are currently
  29. // visible in the message center queue.
  30. size_t VisibleNotificationCount() {
  31. return message_center::MessageCenter::Get()->GetVisibleNotifications().size();
  32. }
  33. } // namespace
  34. class AdaptiveChargingNotificationControllerTest : public AshTestBase {
  35. public:
  36. AdaptiveChargingNotificationControllerTest() = default;
  37. AdaptiveChargingNotificationControllerTest(
  38. const AdaptiveChargingNotificationControllerTest&) = delete;
  39. AdaptiveChargingNotificationControllerTest& operator=(
  40. const AdaptiveChargingNotificationControllerTest&) = delete;
  41. ~AdaptiveChargingNotificationControllerTest() override = default;
  42. // AshTestBase:
  43. void SetUp() override {
  44. AshTestBase::SetUp();
  45. controller_ = std::make_unique<AdaptiveChargingNotificationController>();
  46. }
  47. AdaptiveChargingNotificationController* GetController() {
  48. return controller_.get();
  49. }
  50. void SimulateClick(int button_index) {
  51. message_center::Notification* notification =
  52. message_center::MessageCenter::Get()->FindVisibleNotificationById(
  53. /*id=*/"adaptive-charging-notify-info");
  54. notification->delegate()->Click(button_index, absl::nullopt);
  55. }
  56. private:
  57. std::unique_ptr<AdaptiveChargingNotificationController> controller_;
  58. };
  59. TEST_F(AdaptiveChargingNotificationControllerTest, ShouldntShowNotification) {
  60. SetAdaptiveChargingPref(false);
  61. GetController()->ShowAdaptiveChargingNotification();
  62. GetController()->ShowAdaptiveChargingNotification(5);
  63. EXPECT_EQ(VisibleNotificationCount(), 0u);
  64. }
  65. TEST_F(AdaptiveChargingNotificationControllerTest, ShowNotificationWithHour) {
  66. SetAdaptiveChargingPref(true);
  67. GetController()->ShowAdaptiveChargingNotification(5);
  68. EXPECT_EQ(VisibleNotificationCount(), 1u);
  69. }
  70. TEST_F(AdaptiveChargingNotificationControllerTest,
  71. ShowNotificationWithoutHour) {
  72. SetAdaptiveChargingPref(true);
  73. GetController()->ShowAdaptiveChargingNotification();
  74. EXPECT_EQ(VisibleNotificationCount(), 1u);
  75. }
  76. TEST_F(AdaptiveChargingNotificationControllerTest, HaveTimeInNotification) {
  77. // Set default locale.
  78. base::test::ScopedRestoreICUDefaultLocale restore_locale;
  79. base::i18n::SetICUDefaultLocale("en_AU");
  80. base::test::ScopedRestoreDefaultTimezone sydney_time("Australia/Sydney");
  81. // Override time for testing.
  82. base::subtle::ScopedTimeClockOverrides time_override(
  83. []() {
  84. base::Time time;
  85. EXPECT_TRUE(base::Time::FromUTCExploded(kTestDateTimeExploded, &time));
  86. return time;
  87. },
  88. /*time_ticks_override=*/nullptr,
  89. /*thread_ticks_override=*/nullptr);
  90. SetAdaptiveChargingPref(true);
  91. GetController()->ShowAdaptiveChargingNotification(5);
  92. const message_center::Notification* notification =
  93. message_center::MessageCenter::Get()->FindPopupNotificationById(
  94. "adaptive-charging-notify-info");
  95. ASSERT_TRUE(notification);
  96. // Current local time is 12:42 pm, so 5 hours after should be 5:30pm (rounding
  97. // from 5:42pm).
  98. EXPECT_NE(notification->message().find(u"5:30 pm"), std::u16string::npos);
  99. }
  100. TEST_F(AdaptiveChargingNotificationControllerTest, TimeRoundingUpTest) {
  101. // Set default locale.
  102. base::test::ScopedRestoreICUDefaultLocale restore_locale;
  103. base::i18n::SetICUDefaultLocale("en_AU");
  104. base::test::ScopedRestoreDefaultTimezone sydney_time("Australia/Sydney");
  105. // Override time for testing.
  106. base::subtle::ScopedTimeClockOverrides time_override(
  107. []() {
  108. base::Time time;
  109. EXPECT_TRUE(base::Time::FromUTCExploded(kTestDateTimeExploded, &time));
  110. return time + base::Minutes(3); // Local time is 12:45pm.
  111. },
  112. /*time_ticks_override=*/nullptr,
  113. /*thread_ticks_override=*/nullptr);
  114. SetAdaptiveChargingPref(true);
  115. GetController()->ShowAdaptiveChargingNotification(5);
  116. const message_center::Notification* notification =
  117. message_center::MessageCenter::Get()->FindPopupNotificationById(
  118. "adaptive-charging-notify-info");
  119. ASSERT_TRUE(notification);
  120. // Current local time is 12:45 pm, so 5 hours after should be 6:00pm (rounding
  121. // from 5:45pm).
  122. EXPECT_NE(notification->message().find(u"6:00 pm"), std::u16string::npos);
  123. }
  124. TEST_F(AdaptiveChargingNotificationControllerTest,
  125. ClickButtonMakesNotificationDisappear) {
  126. SetAdaptiveChargingPref(true);
  127. GetController()->ShowAdaptiveChargingNotification(5);
  128. EXPECT_EQ(VisibleNotificationCount(), 1u);
  129. // Notification should disappear after click.
  130. SimulateClick(/*button_index=*/0);
  131. EXPECT_EQ(VisibleNotificationCount(), 0u);
  132. }
  133. } // namespace ash