fake_power_manager_client_unittest.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright 2015 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 "chromeos/dbus/power/fake_power_manager_client.h"
  5. #include "base/run_loop.h"
  6. #include "base/test/task_environment.h"
  7. #include "testing/gtest/include/gtest/gtest.h"
  8. namespace chromeos {
  9. namespace {
  10. const double kInitialBatteryPercent = 85;
  11. const double kUpdatedBatteryPercent = 70;
  12. const power_manager::PowerSupplyProperties_BatteryState kInitialBatteryState =
  13. power_manager::PowerSupplyProperties_BatteryState_DISCHARGING;
  14. const power_manager::PowerSupplyProperties_ExternalPower kInitialExternalPower =
  15. power_manager::PowerSupplyProperties_ExternalPower_USB;
  16. class TestObserver : public PowerManagerClient::Observer {
  17. public:
  18. TestObserver() : num_power_changed_(0) {}
  19. TestObserver(const TestObserver&) = delete;
  20. TestObserver& operator=(const TestObserver&) = delete;
  21. ~TestObserver() override = default;
  22. const power_manager::PowerSupplyProperties& props() const { return props_; }
  23. int num_power_changed() const { return num_power_changed_; }
  24. void ClearProps() { props_.Clear(); }
  25. void PowerChanged(
  26. const power_manager::PowerSupplyProperties& proto) override {
  27. props_ = proto;
  28. ++num_power_changed_;
  29. }
  30. private:
  31. int num_power_changed_;
  32. power_manager::PowerSupplyProperties props_;
  33. };
  34. void SetTestProperties(power_manager::PowerSupplyProperties* props) {
  35. props->set_battery_percent(kInitialBatteryPercent);
  36. props->set_is_calculating_battery_time(true);
  37. props->set_battery_state(kInitialBatteryState);
  38. props->set_external_power(kInitialExternalPower);
  39. }
  40. } // namespace
  41. TEST(FakePowerManagerClientTest, UpdatePowerPropertiesTest) {
  42. // Checking to verify when UpdatePowerProperties is called,
  43. // |props_| values are updated.
  44. FakePowerManagerClient client;
  45. power_manager::PowerSupplyProperties props;
  46. SetTestProperties(&props);
  47. client.UpdatePowerProperties(props);
  48. EXPECT_EQ(kInitialBatteryPercent, client.GetLastStatus()->battery_percent());
  49. EXPECT_TRUE(client.GetLastStatus()->is_calculating_battery_time());
  50. EXPECT_EQ(kInitialBatteryState, client.GetLastStatus()->battery_state());
  51. EXPECT_EQ(kInitialExternalPower, client.GetLastStatus()->external_power());
  52. // Test if when the values are changed, the correct data is set in the
  53. // FakePowerManagerClient.
  54. props = *client.GetLastStatus();
  55. props.set_battery_percent(kUpdatedBatteryPercent);
  56. client.UpdatePowerProperties(props);
  57. EXPECT_EQ(kUpdatedBatteryPercent, client.GetLastStatus()->battery_percent());
  58. EXPECT_TRUE(client.GetLastStatus()->is_calculating_battery_time());
  59. EXPECT_EQ(kInitialBatteryState, client.GetLastStatus()->battery_state());
  60. EXPECT_EQ(kInitialExternalPower, client.GetLastStatus()->external_power());
  61. }
  62. TEST(FakePowerManagerClientTest, NotifyObserversTest) {
  63. FakePowerManagerClient client;
  64. TestObserver test_observer;
  65. // Test adding observer.
  66. client.AddObserver(&test_observer);
  67. EXPECT_TRUE(client.HasObserver(&test_observer));
  68. // Test if NotifyObservers() sends the correct values to |observer|.
  69. // Check number of times NotifyObservers() is called.
  70. power_manager::PowerSupplyProperties props;
  71. SetTestProperties(&props);
  72. client.UpdatePowerProperties(props);
  73. EXPECT_EQ(kInitialBatteryPercent, test_observer.props().battery_percent());
  74. EXPECT_TRUE(test_observer.props().is_calculating_battery_time());
  75. EXPECT_EQ(kInitialBatteryState, test_observer.props().battery_state());
  76. EXPECT_EQ(kInitialExternalPower, test_observer.props().external_power());
  77. EXPECT_EQ(1, test_observer.num_power_changed());
  78. // Test if RequestStatusUpdate() will propagate the data to the observer.
  79. // Check number of times NotifyObservers is called.
  80. // RequestStatusUpdate posts to the current message loop. This is
  81. // necessary because we want to make sure that NotifyObservers() is
  82. // called as a result of RequestStatusUpdate().
  83. base::test::SingleThreadTaskEnvironment task_environment(
  84. base::test::SingleThreadTaskEnvironment::MainThreadType::UI);
  85. test_observer.ClearProps();
  86. client.RequestStatusUpdate();
  87. base::RunLoop().RunUntilIdle();
  88. EXPECT_EQ(kInitialBatteryPercent, test_observer.props().battery_percent());
  89. EXPECT_TRUE(test_observer.props().is_calculating_battery_time());
  90. EXPECT_EQ(kInitialBatteryState, test_observer.props().battery_state());
  91. EXPECT_EQ(kInitialExternalPower, test_observer.props().external_power());
  92. EXPECT_EQ(2, test_observer.num_power_changed());
  93. // Check when values are changed, the correct values are propagated to the
  94. // observer
  95. props = *client.GetLastStatus();
  96. props.set_battery_percent(kUpdatedBatteryPercent);
  97. props.set_external_power(
  98. power_manager::PowerSupplyProperties_ExternalPower_AC);
  99. client.UpdatePowerProperties(props);
  100. EXPECT_EQ(kUpdatedBatteryPercent, test_observer.props().battery_percent());
  101. EXPECT_TRUE(test_observer.props().is_calculating_battery_time());
  102. EXPECT_EQ(kInitialBatteryState, test_observer.props().battery_state());
  103. EXPECT_EQ(power_manager::PowerSupplyProperties_ExternalPower_AC,
  104. test_observer.props().external_power());
  105. EXPECT_EQ(3, test_observer.num_power_changed());
  106. // Test removing observer.
  107. client.RemoveObserver(&test_observer);
  108. EXPECT_FALSE(client.HasObserver(&test_observer));
  109. }
  110. TEST(FakePowerManagerClientTest, AmbientColorSupport) {
  111. FakePowerManagerClient client;
  112. EXPECT_FALSE(client.SupportsAmbientColor());
  113. client.set_supports_ambient_color(true);
  114. EXPECT_TRUE(client.SupportsAmbientColor());
  115. client.set_supports_ambient_color(false);
  116. EXPECT_FALSE(client.SupportsAmbientColor());
  117. }
  118. } // namespace chromeos