Chromium Code Reviews| Index: chromeos/dbus/fake_power_manager_client_unittest.cc |
| diff --git a/chromeos/dbus/fake_power_manager_client_unittest.cc b/chromeos/dbus/fake_power_manager_client_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9cb0e589b81632d0233f75af62a7d6c5a1effb7d |
| --- /dev/null |
| +++ b/chromeos/dbus/fake_power_manager_client_unittest.cc |
| @@ -0,0 +1,96 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chromeos/dbus/fake_power_manager_client.h" |
| + |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace chromeos { |
| + |
| +class FakePowerManagerClientTest : public testing::Test { |
| + public: |
| + // These are based on the PowerSupplyProperties enums for BatteryState |
| + // and ExternalPower. |
| + enum BatteryState { FULL, CHARGING, DISCHARGING }; |
|
Daniel Erat
2015/07/06 14:28:15
this will break; someone will update the proto's e
mozartalouis
2015/07/06 20:49:42
Done.
|
| + enum ExternalPower { AC, USB, DISCONNECTED }; |
| + |
| + FakePowerManagerClientTest() { |
| + // Test the default values that are assigned when the |
| + // FakePowerManagerClient::Init() is called. |
| + client_.Init(nullptr); |
| + |
| + EXPECT_EQ(50, client_.props().battery_percent()); |
|
Daniel Erat
2015/07/06 14:28:14
putting expectations in the base class doesn't mak
mozartalouis
2015/07/06 20:49:42
Done.
|
| + EXPECT_FALSE(client_.props().is_calculating_battery_time()); |
| + EXPECT_EQ(power_manager::PowerSupplyProperties_BatteryState_DISCHARGING, |
| + client_.props().battery_state()); |
| + EXPECT_EQ(power_manager::PowerSupplyProperties_ExternalPower_DISCONNECTED, |
| + client_.props().external_power()); |
| + } |
| + |
| + void UpdatePowerProperties(int percentage, |
| + BatteryState state, |
| + ExternalPower power) { |
| + client_.UpdatePowerProperties( |
| + percentage, false, |
| + static_cast<power_manager::PowerSupplyProperties_BatteryState>(state), |
| + static_cast<power_manager::PowerSupplyProperties_ExternalPower>(power)); |
| + } |
| + |
| + FakePowerManagerClient& client() { return client_; } |
|
Daniel Erat
2015/07/06 14:28:15
we don't use non-const references (but see below;
mozartalouis
2015/07/06 20:49:42
Done.
|
| + |
| + private: |
| + FakePowerManagerClient client_; |
|
Daniel Erat
2015/07/06 14:28:15
put this in a "protected:" section; then derived c
mozartalouis
2015/07/06 20:49:42
Done.
|
| + |
| + DISALLOW_COPY_AND_ASSIGN(FakePowerManagerClientTest); |
| +}; |
| + |
| +TEST_F(FakePowerManagerClientTest, BatteryStateTest) { |
| + // Test if setting battery_percent to 100 and battery_state to |
| + // CHARGING will reset the battery_state to FULL. |
| + UpdatePowerProperties(100, BatteryState::CHARGING, ExternalPower::AC); |
| + EXPECT_EQ(100, client().props().battery_percent()); |
| + EXPECT_EQ(power_manager::PowerSupplyProperties_BatteryState_FULL, |
| + client().props().battery_state()); |
| + |
| + // Test if setting battery_percent less than 100 and |
| + // battery_state to FULL will reset battery_state to |
| + // DISCHARGING. |
| + UpdatePowerProperties(80, BatteryState::FULL, ExternalPower::DISCONNECTED); |
| + EXPECT_EQ(80, client().props().battery_percent()); |
| + EXPECT_EQ(power_manager::PowerSupplyProperties_BatteryState_DISCHARGING, |
| + client().props().battery_state()); |
| +} |
| + |
| +TEST_F(FakePowerManagerClientTest, ExternalPowerTest) { |
| + // Test if setting battery_percent less than 100 and |
| + // battery_state to CHARGING while external_power is set to |
| + // USB will set external_power to USB. |
| + UpdatePowerProperties(80, BatteryState::CHARGING, ExternalPower::USB); |
| + EXPECT_EQ(power_manager::PowerSupplyProperties_ExternalPower_USB, |
| + client().props().external_power()); |
| + |
| + // Test if setting battery_percent less than 100 and |
| + // battery_state to DISCHARGING while external_power is set to |
| + // USB will set external_power to USB. |
| + UpdatePowerProperties(80, BatteryState::DISCHARGING, ExternalPower::USB); |
| + EXPECT_EQ(power_manager::PowerSupplyProperties_ExternalPower_USB, |
| + client().props().external_power()); |
| + |
| + // Test if setting battery_percent less than 100 and |
| + // battery_state to CHARGING while external_power is set to |
| + // DISCONNECTED will set external_power to AC. |
| + UpdatePowerProperties(80, BatteryState::CHARGING, |
| + ExternalPower::DISCONNECTED); |
| + EXPECT_EQ(power_manager::PowerSupplyProperties_ExternalPower_AC, |
| + client().props().external_power()); |
| + |
| + // Test if setting battery_percent less than 100 and |
| + // battery_state to CHARGING while external_power is set to |
| + // AC will set external_power to DISCONNECTED. |
| + UpdatePowerProperties(80, BatteryState::DISCHARGING, ExternalPower::AC); |
| + EXPECT_EQ(power_manager::PowerSupplyProperties_ExternalPower_DISCONNECTED, |
| + client().props().external_power()); |
| +} |
| + |
| +} // namespace chromeos |