1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "ash/system/power/battery_saver_controller.h"
#include <memory>
#include "ash/constants/ash_features.h"
#include "ash/constants/ash_pref_names.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/system/power/battery_notification.h"
#include "ash/system/power/power_status.h"
#include "ash/system/system_notification_controller.h"
#include "ash/system/toast/toast_manager_impl.h"
#include "ash/system/toast/toast_overlay.h"
#include "ash/test/ash_test_base.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "chromeos/dbus/power/fake_power_manager_client.h"
#include "chromeos/dbus/power_manager/power_supply_properties.pb.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/message_center/message_center.h"
namespace ash {
class BatterySaverControllerTest : public AshTestBase {
public:
// AshTestBase:
void SetUp() override {
scoped_feature_list_ = std::make_unique<base::test::ScopedFeatureList>(
features::kBatterySaver);
chromeos::PowerManagerClient::InitializeFake();
AshTestBase::SetUp();
// Let initial PowerStatus call to
// PowerManagerClient::GetBatterySaverModeState to complete.
base::RunLoop run_loop;
run_loop.RunUntilIdle();
}
void TearDown() override {
AshTestBase::TearDown();
chromeos::PowerManagerClient::Shutdown();
scoped_feature_list_.reset();
}
protected:
BatterySaverController* battery_saver_controller() {
return Shell::Get()->battery_saver_controller();
}
void UpdatePowerStatus(double battery_percent,
base::TimeDelta time_to_empty,
bool charging,
bool is_usb_charger = false) {
power_manager::PowerSupplyProperties props;
// Set battery percentage
props.set_battery_percent(battery_percent);
// Determine battery state
auto external_power =
power_manager::PowerSupplyProperties_ExternalPower_DISCONNECTED;
auto battery_state =
power_manager::PowerSupplyProperties_BatteryState_DISCHARGING;
if (is_usb_charger) {
external_power = power_manager::PowerSupplyProperties_ExternalPower_USB;
} else if (charging) { // Otherwise, treat like AC charger.
external_power = power_manager::PowerSupplyProperties_ExternalPower_AC;
}
if (battery_percent >= 100) {
battery_state = power_manager::PowerSupplyProperties_BatteryState_FULL;
} else if (charging) {
battery_state =
power_manager::PowerSupplyProperties_BatteryState_CHARGING;
}
// Set battery state
props.set_external_power(external_power);
props.set_battery_state(battery_state);
// Set time
props.set_is_calculating_battery_time(false);
props.set_battery_time_to_empty_sec(time_to_empty.InSecondsF());
// Flush
base::RunLoop run_loop;
chromeos::FakePowerManagerClient::Get()->UpdatePowerProperties(props);
run_loop.RunUntilIdle();
}
ToastOverlay* GetCurrentToast() {
return Shell::Get()->toast_manager()->GetCurrentOverlayForTesting();
}
void DismissToast() {
Shell::Get()->toast_manager()->CloseAllToastsWithAnimation();
}
bool IsBatterySaverActive() {
return PowerStatus::Get()->IsBatterySaverActive();
}
double GetActivationPercent() {
return features::kBatterySaverActivationChargePercent.Get();
}
constexpr static base::TimeDelta eight_hours_ = base::Hours(8);
std::unique_ptr<base::test::ScopedFeatureList> scoped_feature_list_ = nullptr;
};
class BatterySaverControllerNotificationTest
: public BatterySaverControllerTest,
public testing::WithParamInterface<
features::BatterySaverNotificationBehavior> {
protected:
message_center::Notification* GetCurrentNotification() {
return message_center::MessageCenter::Get()->FindNotificationById(
BatteryNotification::kNotificationId);
}
void DismissNotification() {
message_center::MessageCenter::Get()->RemoveNotification(
BatteryNotification::kNotificationId, false);
EXPECT_EQ(GetCurrentNotification(), nullptr);
}
void NotificationNotPresent() {
EXPECT_EQ(GetCurrentNotification(), nullptr);
}
void NotificationPresent() { EXPECT_NE(GetCurrentNotification(), nullptr); }
void SetExperimentArm(features::BatterySaverNotificationBehavior arm) {
scoped_feature_list_.reset();
base::FieldTrialParams parameters;
parameters[features::kBatterySaverNotificationBehavior.name] =
features::kBatterySaverNotificationBehavior.options[arm].name;
scoped_feature_list_ = std::make_unique<base::test::ScopedFeatureList>();
scoped_feature_list_->InitAndEnableFeatureWithParameters(
features::kBatterySaver, parameters);
base::RunLoop run_loop;
run_loop.RunUntilIdle();
}
};
// Test that the automatic logic to turn battery saver on and off without
// direct user action works.
TEST_F(BatterySaverControllerTest, AutoEnableDisable) {
// Battery near full and charging, no battery saver.
UpdatePowerStatus(80.0, eight_hours_, true);
EXPECT_FALSE(IsBatterySaverActive());
// Battery near full and discharging, still no battery saver.
UpdatePowerStatus(80.0, eight_hours_, false);
EXPECT_FALSE(IsBatterySaverActive());
// Battery discharging but just above the activation %, still no battery
// saver.
UpdatePowerStatus(GetActivationPercent() + 1, eight_hours_, false);
EXPECT_FALSE(IsBatterySaverActive());
// Battery discharging and at activation %, battery saver turns on.
UpdatePowerStatus(GetActivationPercent(), eight_hours_, false);
EXPECT_TRUE(IsBatterySaverActive());
// Discharge more, battery saver remains on.
UpdatePowerStatus(5.0, eight_hours_, false);
EXPECT_TRUE(IsBatterySaverActive());
// Start charging, even with a low battery %, battery saver disables.
UpdatePowerStatus(5.0, eight_hours_, true);
EXPECT_FALSE(IsBatterySaverActive());
}
// Test that we detect charging while asleep, as seen by a sudden jump in charge
// while discharging.
TEST_F(BatterySaverControllerTest, DetectSleepCharging) {
// Battery discharging and below activation %, battery saver turns on.
UpdatePowerStatus(5.0, eight_hours_, false);
EXPECT_TRUE(IsBatterySaverActive());
// Increase charge while still discharging, but stay below activiation %,
// battery saver remains on.
UpdatePowerStatus(GetActivationPercent(), eight_hours_, false);
EXPECT_TRUE(IsBatterySaverActive());
// Increase charge again, but this time above the activation %, battery saver
// turns off.
UpdatePowerStatus(
GetActivationPercent() +
BatterySaverController::kBatterySaverSleepChargeThreshold,
eight_hours_, false);
EXPECT_FALSE(IsBatterySaverActive());
}
TEST_F(BatterySaverControllerTest, EnsureThresholdsCrossed) {
// Start the test with full battery, discharging.
UpdatePowerStatus(100.0, eight_hours_, false);
EXPECT_FALSE(IsBatterySaverActive());
// Enable battery saver mode manually.
battery_saver_controller()->SetState(
true, BatterySaverController::UpdateReason::kSettings);
// Discharge to the percent threshold
UpdatePowerStatus(GetActivationPercent(), eight_hours_, false);
EXPECT_TRUE(IsBatterySaverActive());
// Disable battery saver mode manually.
battery_saver_controller()->SetState(
false, BatterySaverController::UpdateReason::kSettings);
// When we get to percent_threshold-1, it should still be disabled.
UpdatePowerStatus(GetActivationPercent() - 1, eight_hours_, false);
EXPECT_FALSE(IsBatterySaverActive());
}
// Test that Battery Saver remains enabled when charging with a low power USB
// charger.
TEST_F(BatterySaverControllerTest, USBCharging) {
UpdatePowerStatus(GetActivationPercent() - 5, eight_hours_, false);
EXPECT_TRUE(IsBatterySaverActive());
// Attache a low-power charger and slowly charge, Battery Saver remains on.
for (int i = -5; i <= 5; i++) {
UpdatePowerStatus(GetActivationPercent() + i, eight_hours_, true, true);
EXPECT_TRUE(IsBatterySaverActive());
}
}
// Metrics always logged on enable.
void ExpectEnabledMetrics(base::HistogramTester& histogram_tester,
base::HistogramBase::Count32 enabled_count) {
histogram_tester.ExpectTotalCount("Ash.BatterySaver.BatteryPercent.Enabled",
enabled_count);
histogram_tester.ExpectTotalCount("Ash.BatterySaver.TimeToEmpty.Enabled",
enabled_count);
}
// Metrics logged on enable when enabled via settings.
void ExpectSettingsEnabledMetrics(
base::HistogramTester& histogram_tester,
base::HistogramBase::Count32 settings_enabled_count) {
histogram_tester.ExpectTotalCount(
"Ash.BatterySaver.BatteryPercent.EnabledSettings",
settings_enabled_count);
histogram_tester.ExpectTotalCount(
"Ash.BatterySaver.TimeToEmpty.EnabledSettings", settings_enabled_count);
}
// Metrics always logged on disable.
void ExpectDisabledMetrics(base::HistogramTester& histogram_tester,
base::HistogramBase::Count32 disabled_count) {
histogram_tester.ExpectTotalCount("Ash.BatterySaver.BatteryPercent.Disabled",
disabled_count);
histogram_tester.ExpectTotalCount("Ash.BatterySaver.TimeToEmpty.Disabled",
disabled_count);
histogram_tester.ExpectTotalCount("Ash.BatterySaver.Duration",
disabled_count);
}
// Metrics logged on disable when enabled via notification.
void ExpectNotificationEnabledMetricsOnDisable(
base::HistogramTester& histogram_tester,
base::HistogramBase::Count32 notification_enabled_count) {
histogram_tester.ExpectTotalCount(
"Ash.BatterySaver.Duration.EnabledNotification",
notification_enabled_count);
}
// Metrics logged on disable when enabled via settings.
void ExpectSettingsEnabledMetricsOnDisable(
base::HistogramTester& histogram_tester,
base::HistogramBase::Count32 settings_enabled_count) {
histogram_tester.ExpectTotalCount("Ash.BatterySaver.Duration.EnabledSettings",
settings_enabled_count);
}
// Metrics logged on disable when disabled via charging.
void ExpectChargingDisabledMetrics(
base::HistogramTester& histogram_tester,
base::HistogramBase::Count32 charging_disabled_count) {
histogram_tester.ExpectTotalCount(
"Ash.BatterySaver.Duration.DisabledCharging", charging_disabled_count);
}
// Metrics logged on disable when disabled via notification.
void ExpectNotificationDisabledMetrics(
base::HistogramTester& histogram_tester,
base::HistogramBase::Count32 notification_disabled_count) {
histogram_tester.ExpectTotalCount(
"Ash.BatterySaver.Duration.DisabledNotification",
notification_disabled_count);
}
// Metrics logged on disable when disabled via settings.
void ExpectSettingsDisabledMetrics(
base::HistogramTester& histogram_tester,
base::HistogramBase::Count32 settings_disabled_count) {
histogram_tester.ExpectTotalCount(
"Ash.BatterySaver.BatteryPercent.DisabledSettings",
settings_disabled_count);
histogram_tester.ExpectTotalCount(
"Ash.BatterySaver.TimeToEmpty.DisabledSettings", settings_disabled_count);
histogram_tester.ExpectTotalCount(
"Ash.BatterySaver.Duration.DisabledSettings", settings_disabled_count);
}
TEST_F(BatterySaverControllerTest, Metrics) {
base::HistogramTester ht;
// TimeToEmpty should have a value.
UpdatePowerStatus(80.0, eight_hours_, false);
// Enable with settings.
ExpectEnabledMetrics(ht, 0);
ExpectSettingsEnabledMetrics(ht, 0);
battery_saver_controller()->SetState(
true, BatterySaverController::UpdateReason::kSettings);
ExpectEnabledMetrics(ht, 1);
ExpectSettingsEnabledMetrics(ht, 1);
// Disable with settings.
ExpectDisabledMetrics(ht, 0);
ExpectSettingsEnabledMetricsOnDisable(ht, 0);
ExpectSettingsDisabledMetrics(ht, 0);
battery_saver_controller()->SetState(
false, BatterySaverController::UpdateReason::kSettings);
ExpectDisabledMetrics(ht, 1);
ExpectSettingsEnabledMetricsOnDisable(ht, 1);
ExpectSettingsDisabledMetrics(ht, 1);
// Enable with notification.
ExpectEnabledMetrics(ht, 1);
battery_saver_controller()->SetState(
true, BatterySaverController::UpdateReason::kThreshold);
ExpectEnabledMetrics(ht, 2);
// Disable with notification.
ExpectDisabledMetrics(ht, 1);
ExpectNotificationEnabledMetricsOnDisable(ht, 0);
ExpectNotificationDisabledMetrics(ht, 0);
battery_saver_controller()->SetState(
false, BatterySaverController::UpdateReason::kThreshold);
ExpectDisabledMetrics(ht, 2);
ExpectNotificationEnabledMetricsOnDisable(ht, 1);
ExpectNotificationDisabledMetrics(ht, 1);
// Enable again with notifications, just because we need it enabled to test
// disabling with charging.
ExpectEnabledMetrics(ht, 2);
battery_saver_controller()->SetState(
true, BatterySaverController::UpdateReason::kThreshold);
ExpectEnabledMetrics(ht, 3);
// Disable with charging.
ExpectDisabledMetrics(ht, 2);
ExpectNotificationEnabledMetricsOnDisable(ht, 1);
ExpectChargingDisabledMetrics(ht, 0);
battery_saver_controller()->SetState(
false, BatterySaverController::UpdateReason::kCharging);
ExpectDisabledMetrics(ht, 3);
ExpectNotificationEnabledMetricsOnDisable(ht, 2);
ExpectChargingDisabledMetrics(ht, 1);
// Check that we didn't have any spurious metrics logged.
ExpectEnabledMetrics(ht, 3);
ExpectSettingsEnabledMetrics(ht, 1);
ExpectSettingsEnabledMetricsOnDisable(ht, 1);
ExpectNotificationDisabledMetrics(ht, 1);
ExpectSettingsDisabledMetrics(ht, 1);
}
TEST_F(BatterySaverControllerTest, ShowDisableToast) {
// Ensure there is no `ToastOverlay` being displayed at the start of the test.
ToastOverlay* current_toast = GetCurrentToast();
EXPECT_EQ(current_toast, nullptr);
// Test Disabled Toast via Button.
// Enable battery saver mode.
battery_saver_controller()->SetState(
true, BatterySaverController::UpdateReason::kThreshold);
// There should be no `ToastOverlay` displayed when battery saver is enabled.
current_toast = GetCurrentToast();
EXPECT_EQ(current_toast, nullptr);
// Disable battery saver mode via button.
battery_saver_controller()->SetState(
false, BatterySaverController::UpdateReason::kThreshold);
// Check to see if a `ToastOverlay` was displayed, and that it's accurate.
current_toast = GetCurrentToast();
EXPECT_NE(current_toast, nullptr);
EXPECT_EQ(
current_toast->GetText(),
l10n_util::GetStringUTF16(IDS_ASH_BATTERY_SAVER_DISABLED_TOAST_TEXT));
DismissToast();
// Test Disabled Toast via Charging.
// Enable battery saver mode.
battery_saver_controller()->SetState(
true, BatterySaverController::UpdateReason::kThreshold);
// There should be no `ToastOverlay` displayed when battery saver is enabled.
current_toast = GetCurrentToast();
EXPECT_EQ(current_toast, nullptr);
// Disable battery saver mode via Charging.
battery_saver_controller()->SetState(
false, BatterySaverController::UpdateReason::kCharging);
// Check to see if a `ToastOverlay` was displayed, and that it's accurate.
current_toast = GetCurrentToast();
EXPECT_NE(current_toast, nullptr);
EXPECT_EQ(
current_toast->GetText(),
l10n_util::GetStringUTF16(IDS_ASH_BATTERY_SAVER_DISABLED_TOAST_TEXT));
DismissToast();
// Test Disabled Toast via Settings.
// Reenable to test that toast doesn't appear when toggled via Settings.
battery_saver_controller()->SetState(
true, BatterySaverController::UpdateReason::kSettings);
// Disable battery saver mode via Settings toggle.
battery_saver_controller()->SetState(
false, BatterySaverController::UpdateReason::kSettings);
// Check there is still no toast since we disabled via Settings.
current_toast = GetCurrentToast();
EXPECT_EQ(current_toast, nullptr);
}
TEST_F(BatterySaverControllerTest, Allowed) {
// Battery Saver is allowed by default.
EXPECT_TRUE(IsBatterySaverAllowed());
// If pref is managed and false, then Battery Saver is not allowed.
local_state()->SetManagedPref(prefs::kPowerBatterySaver, base::Value(false));
EXPECT_FALSE(IsBatterySaverAllowed());
local_state()->RemoveManagedPref(prefs::kPowerBatterySaver);
// If the experiment is off, Battery Saver is not allowed.
scoped_feature_list_->Reset();
scoped_feature_list_->InitAndDisableFeature(features::kBatterySaver);
EXPECT_FALSE(IsBatterySaverAllowed());
}
TEST_F(BatterySaverControllerNotificationTest,
ShowEnableToastAtCriticalPercentageForAutoEnable) {
SetExperimentArm(features::kBSMAutoEnable);
const int critical_percentage = Shell::Get()
->system_notification_controller()
->power_notification_controller()
->GetCriticalPowerPercentage();
// Ensure there is no `ToastOverlay` being displayed at the start of the test.
ToastOverlay* current_toast = GetCurrentToast();
EXPECT_EQ(current_toast, nullptr);
// Set the battery to a critical percentage.
UpdatePowerStatus(critical_percentage, eight_hours_, /*charging=*/false);
// The enabled toast should be displayed.
current_toast = GetCurrentToast();
EXPECT_NE(current_toast, nullptr);
EXPECT_EQ(
current_toast->GetText(),
l10n_util::GetStringUTF16(IDS_ASH_BATTERY_SAVER_ENABLED_TOAST_TEXT));
DismissToast();
// Plug In AC Charger.
UpdatePowerStatus(critical_percentage, eight_hours_, /*charging=*/true);
// Toast should be 'Disabled' text.
current_toast = GetCurrentToast();
EXPECT_NE(current_toast, nullptr);
EXPECT_EQ(
current_toast->GetText(),
l10n_util::GetStringUTF16(IDS_ASH_BATTERY_SAVER_DISABLED_TOAST_TEXT));
DismissToast();
}
TEST_P(BatterySaverControllerNotificationTest, NotificationStateUpdatesOnUSB) {
SetExperimentArm(features::kBatterySaverNotificationBehavior.Get());
DismissNotification();
// Start test with 100% battery, eight hours time remaining, not charging.
UpdatePowerStatus(100, eight_hours_, /*charging=*/false);
NotificationNotPresent();
DismissNotification();
// Battery read jumps (not smoothly) from above threshold to below threshold.
UpdatePowerStatus(GetActivationPercent() - 1, eight_hours_,
/*charging=*/false);
NotificationPresent();
DismissNotification();
EXPECT_TRUE(IsBatterySaverActive());
// Simulate USB Plug-In, which detects as AC first, then USB.
UpdatePowerStatus(GetActivationPercent() - 1, eight_hours_,
/*charging=*/true);
EXPECT_FALSE(IsBatterySaverActive());
UpdatePowerStatus(GetActivationPercent() - 1, eight_hours_,
/*charging=*/false, /*is_usb_charger=*/true);
EXPECT_EQ(IsBatterySaverActive(),
features::kBatterySaverNotificationBehavior.Get() ==
features::kBSMAutoEnable);
NotificationPresent();
// Disable Battery Saver via Settings toggle.
// This should cause an update in notification state, but not in notification.
battery_saver_controller()->SetState(
false, BatterySaverController::UpdateReason::kSettings);
// Unplug USB Charger.
UpdatePowerStatus(GetActivationPercent() - 1, eight_hours_,
/*charging=*/false, /*is_usb_charger=*/false);
// If notification state was updated, then we should show a generic low power
// notification.
EXPECT_EQ(GetCurrentNotification()->title(),
l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_LOW_BATTERY_TITLE));
EXPECT_EQ(GetCurrentNotification()->buttons().size(), static_cast<ulong>(0));
}
TEST_P(BatterySaverControllerNotificationTest,
ReenableOnUnplugBelowThresholds) {
SetExperimentArm(GetParam());
// Start the test at percent threshold with no charging.
UpdatePowerStatus(GetActivationPercent(), eight_hours_, false);
switch (features::kBatterySaverNotificationBehavior.Get()) {
case features::kBSMAutoEnable:
EXPECT_TRUE(IsBatterySaverActive());
break;
case features::kBSMOptIn:
EXPECT_FALSE(IsBatterySaverActive());
break;
default:
FAIL();
}
// Plug in the charger at percent threshold.
UpdatePowerStatus(GetActivationPercent(), eight_hours_, true);
EXPECT_FALSE(IsBatterySaverActive());
// Unplug the charger, and expect BSM to stay on if we are in an auto-enable
// state.
UpdatePowerStatus(GetActivationPercent(), eight_hours_, false);
switch (features::kBatterySaverNotificationBehavior.Get()) {
case features::kBSMAutoEnable:
EXPECT_TRUE(IsBatterySaverActive());
break;
case features::kBSMOptIn:
EXPECT_FALSE(IsBatterySaverActive());
break;
default:
FAIL();
}
}
TEST_P(BatterySaverControllerNotificationTest,
TestNotificationThresholdsForExperimentArms) {
SetExperimentArm(GetParam());
auto NotificationNotPresent = [&]() {
EXPECT_EQ(GetCurrentNotification(), nullptr);
};
auto NotificationPresent = [&]() {
EXPECT_NE(GetCurrentNotification(), nullptr);
};
// Start test with 100% battery, eight hours time remaining, not charging.
UpdatePowerStatus(100, eight_hours_, true);
DismissNotification();
// Battery discharging but just above the activation %, battery saver should
// be disabled.
UpdatePowerStatus(GetActivationPercent() + 1, eight_hours_, false);
EXPECT_FALSE(IsBatterySaverActive());
NotificationNotPresent();
// Battery read jumps (not smoothly) from above threshold to below threshold.
UpdatePowerStatus(GetActivationPercent() - 1, eight_hours_, false);
switch (features::kBatterySaverNotificationBehavior.Get()) {
case features::kBSMAutoEnable:
EXPECT_TRUE(IsBatterySaverActive());
break;
case features::kBSMOptIn:
EXPECT_FALSE(IsBatterySaverActive());
break;
default:
FAIL();
}
// Notification should appear regardless of battery saver state (so user can
// opt-in/out).
NotificationPresent();
DismissNotification();
// Check to make sure that the notification doesn't reappear after it's been
// dismissed.
UpdatePowerStatus(GetActivationPercent() - 2, eight_hours_, false);
NotificationNotPresent();
}
TEST_P(BatterySaverControllerNotificationTest,
NotificationReappearsAfterChargeThenDischarge) {
SetExperimentArm(GetParam());
// Start test with 100% battery, eight hours time remaining, not charging.
UpdatePowerStatus(100, eight_hours_, true);
DismissNotification();
// Battery read jumps (not smoothly) from above threshold to below threshold.
UpdatePowerStatus(GetActivationPercent() - 1, eight_hours_, false);
// Notification for threshold should appear.
NotificationPresent();
DismissNotification();
// Charging, battery goes above threshold.
UpdatePowerStatus(GetActivationPercent() + 1, eight_hours_, true);
NotificationNotPresent();
// Discharging, battery goes below threshold.
UpdatePowerStatus(GetActivationPercent() - 1, eight_hours_, false);
// Notification should reappear.
NotificationPresent();
DismissNotification();
}
INSTANTIATE_TEST_SUITE_P(
All,
BatterySaverControllerNotificationTest,
testing::Values(features::BatterySaverNotificationBehavior::kBSMAutoEnable,
features::BatterySaverNotificationBehavior::kBSMOptIn));
class BatterySaverControllerInitTest : public testing::Test {
public:
void SetUp() override {
BatterySaverController::RegisterLocalStatePrefs(local_state_.registry());
chromeos::PowerManagerClient::InitializeFake();
power_manager_client_ = chromeos::FakePowerManagerClient::Get();
PowerStatus::Initialize();
}
void TearDown() override {
PowerStatus::Shutdown();
power_manager_client_ = nullptr;
chromeos::PowerManagerClient::Shutdown();
}
protected:
void UpdatePowerPropertiesToDischarging(int battery_percent) {
power_manager::PowerSupplyProperties proto;
proto.set_battery_state(
power_manager::PowerSupplyProperties_BatteryState_DISCHARGING);
proto.set_external_power(
power_manager::PowerSupplyProperties_ExternalPower_DISCONNECTED);
proto.set_battery_percent(battery_percent);
proto.set_is_calculating_battery_time(false);
proto.set_battery_time_to_empty_sec(3600.0);
power_manager_client_->UpdatePowerProperties(proto);
base::RunLoop().RunUntilIdle();
}
base::test::SingleThreadTaskEnvironment task_environment_;
TestingPrefServiceSimple local_state_;
raw_ptr<chromeos::FakePowerManagerClient> power_manager_client_;
};
// Test that Battery Saver active state is preserved across restarts.
TEST_F(BatterySaverControllerInitTest, RestoreState) {
EXPECT_FALSE(power_manager_client_->battery_saver_mode_enabled());
// Disabled by default.
{
local_state_.ClearPref(prefs::kPowerBatterySaver);
std::unique_ptr<BatterySaverController> controller =
std::make_unique<BatterySaverController>(&local_state_);
EXPECT_FALSE(power_manager_client_->battery_saver_mode_enabled());
}
// Restore disabled.
{
local_state_.SetBoolean(prefs::kPowerBatterySaver, false);
std::unique_ptr<BatterySaverController> controller =
std::make_unique<BatterySaverController>(&local_state_);
EXPECT_FALSE(power_manager_client_->battery_saver_mode_enabled());
}
// Restore enabled.
{
local_state_.SetBoolean(prefs::kPowerBatterySaver, true);
std::unique_ptr<BatterySaverController> controller =
std::make_unique<BatterySaverController>(&local_state_);
EXPECT_TRUE(power_manager_client_->battery_saver_mode_enabled());
}
}
// Test that Battery Saver is disabled when charging when shut down.
TEST_F(BatterySaverControllerInitTest, DisableAfterChargingWhenOff) {
base::HistogramTester ht;
const int battery_percent = 80;
local_state_.SetBoolean(prefs::kPowerBatterySaver, true);
local_state_.SetInteger(prefs::kPowerBatterySaverPercent, battery_percent);
// Set battery_percent above saved pref value to trigger charge detection.
UpdatePowerPropertiesToDischarging(
battery_percent +
BatterySaverController::kBatterySaverSleepChargeThreshold);
// Initialize BatterySaverController and check that battery saver is OFF.
std::unique_ptr<BatterySaverController> controller =
std::make_unique<BatterySaverController>(&local_state_);
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(power_manager_client_->battery_saver_mode_enabled());
EXPECT_FALSE(PowerStatus::Get()->IsBatterySaverActive());
ExpectDisabledMetrics(ht, 1);
ExpectChargingDisabledMetrics(ht, 1);
}
// Test that Battery Saver remains on after charging when shut down if the
// battery is low enough that it would be re-enabled.
TEST_F(BatterySaverControllerInitTest, EnableAfterChargingWhenOffAtLowBattery) {
const int battery_percent = 5;
local_state_.SetBoolean(prefs::kPowerBatterySaver, true);
local_state_.SetInteger(prefs::kPowerBatterySaverPercent, battery_percent);
// Set battery_percent above saved pref value to trigger charge detection.
UpdatePowerPropertiesToDischarging(
battery_percent +
BatterySaverController::kBatterySaverSleepChargeThreshold);
// Initialize BatterySaverController and check that battery saver is ON.
std::unique_ptr<BatterySaverController> controller =
std::make_unique<BatterySaverController>(&local_state_);
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(power_manager_client_->battery_saver_mode_enabled());
EXPECT_TRUE(PowerStatus::Get()->IsBatterySaverActive());
}
} // namespace ash
|