power_status_unittest.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // Copyright (c) 2014 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/power_status.h"
  5. #include <memory>
  6. #include "ash/resources/vector_icons/vector_icons.h"
  7. #include "ash/test/ash_test_base.h"
  8. #include "base/run_loop.h"
  9. #include "third_party/cros_system_api/dbus/service_constants.h"
  10. #include "ui/gfx/image/image.h"
  11. #include "ui/gfx/image/image_unittest_util.h"
  12. using power_manager::PowerSupplyProperties;
  13. namespace ash {
  14. namespace {
  15. class TestObserver : public PowerStatus::Observer {
  16. public:
  17. TestObserver() : power_changed_count_(0) {}
  18. TestObserver(const TestObserver&) = delete;
  19. TestObserver& operator=(const TestObserver&) = delete;
  20. ~TestObserver() override = default;
  21. int power_changed_count() const { return power_changed_count_; }
  22. // PowerStatus::Observer overrides:
  23. void OnPowerStatusChanged() override { ++power_changed_count_; }
  24. private:
  25. int power_changed_count_;
  26. };
  27. } // namespace
  28. class PowerStatusTest : public AshTestBase {
  29. public:
  30. PowerStatusTest() = default;
  31. PowerStatusTest(const PowerStatusTest&) = delete;
  32. PowerStatusTest& operator=(const PowerStatusTest&) = delete;
  33. ~PowerStatusTest() override = default;
  34. void SetUp() override {
  35. AshTestBase::SetUp();
  36. power_status_ = PowerStatus::Get();
  37. test_observer_ = std::make_unique<TestObserver>();
  38. power_status_->AddObserver(test_observer_.get());
  39. }
  40. void TearDown() override {
  41. power_status_->RemoveObserver(test_observer_.get());
  42. test_observer_.reset();
  43. AshTestBase::TearDown();
  44. }
  45. protected:
  46. PowerStatus* power_status_ = nullptr; // Not owned.
  47. std::unique_ptr<TestObserver> test_observer_;
  48. };
  49. TEST_F(PowerStatusTest, InitializeAndUpdate) {
  50. // Test that the initial power supply state should be acquired after
  51. // PowerStatus is instantiated. This depends on
  52. // PowerManagerClientStubImpl, which responds to power status update
  53. // requests, pretends there is a battery present, and generates some valid
  54. // power supply status data.
  55. base::RunLoop().RunUntilIdle();
  56. EXPECT_EQ(1, test_observer_->power_changed_count());
  57. // Test RequestUpdate, test_obsever_ should be notified for power suuply
  58. // status change.
  59. power_status_->RequestStatusUpdate();
  60. base::RunLoop().RunUntilIdle();
  61. EXPECT_EQ(2, test_observer_->power_changed_count());
  62. }
  63. TEST_F(PowerStatusTest, GetBatteryImageInfo) {
  64. PowerSupplyProperties prop;
  65. prop.set_external_power(PowerSupplyProperties::AC);
  66. prop.set_battery_state(PowerSupplyProperties::CHARGING);
  67. prop.set_battery_percent(98.0);
  68. power_status_->SetProtoForTesting(prop);
  69. const PowerStatus::BatteryImageInfo info_charging_98 =
  70. power_status_->GetBatteryImageInfo();
  71. // 99% should use the same icon as 98%.
  72. prop.set_battery_percent(99.0);
  73. power_status_->SetProtoForTesting(prop);
  74. EXPECT_TRUE(info_charging_98.ApproximatelyEqual(
  75. power_status_->GetBatteryImageInfo()));
  76. // A different icon should be used when the battery is full.
  77. prop.set_battery_state(PowerSupplyProperties::FULL);
  78. prop.set_battery_percent(100.0);
  79. power_status_->SetProtoForTesting(prop);
  80. EXPECT_FALSE(info_charging_98.ApproximatelyEqual(
  81. power_status_->GetBatteryImageInfo()));
  82. // A much-lower battery level should use a different icon.
  83. prop.set_battery_state(PowerSupplyProperties::CHARGING);
  84. prop.set_battery_percent(20.0);
  85. power_status_->SetProtoForTesting(prop);
  86. EXPECT_FALSE(info_charging_98.ApproximatelyEqual(
  87. power_status_->GetBatteryImageInfo()));
  88. // Ditto for 98%, but on USB instead of AC.
  89. prop.set_external_power(PowerSupplyProperties::USB);
  90. prop.set_battery_percent(98.0);
  91. power_status_->SetProtoForTesting(prop);
  92. EXPECT_FALSE(info_charging_98.ApproximatelyEqual(
  93. power_status_->GetBatteryImageInfo()));
  94. }
  95. // Tests that the |icon_badge| member of BatteryImageInfo is set correctly
  96. // with various power supply property values.
  97. TEST_F(PowerStatusTest, BatteryImageInfoIconBadge) {
  98. PowerSupplyProperties prop;
  99. // A charging battery connected to AC power should have a bolt badge.
  100. prop.set_external_power(PowerSupplyProperties::AC);
  101. prop.set_battery_state(PowerSupplyProperties::CHARGING);
  102. prop.set_battery_percent(98.0);
  103. power_status_->SetProtoForTesting(prop);
  104. const gfx::VectorIcon* bolt_icon =
  105. power_status_->GetBatteryImageInfo().icon_badge;
  106. EXPECT_TRUE(bolt_icon);
  107. // A discharging battery connected to AC should also have a bolt badge.
  108. prop.set_battery_state(PowerSupplyProperties::DISCHARGING);
  109. power_status_->SetProtoForTesting(prop);
  110. EXPECT_EQ(bolt_icon, power_status_->GetBatteryImageInfo().icon_badge);
  111. // A charging battery connected to USB power should have an
  112. // unreliable badge.
  113. prop.set_external_power(PowerSupplyProperties::USB);
  114. prop.set_battery_state(PowerSupplyProperties::CHARGING);
  115. power_status_->SetProtoForTesting(prop);
  116. const gfx::VectorIcon* unreliable_icon =
  117. power_status_->GetBatteryImageInfo().icon_badge;
  118. EXPECT_NE(unreliable_icon, bolt_icon);
  119. EXPECT_TRUE(unreliable_icon);
  120. // A discharging battery connected to USB power should also have an
  121. // unreliable badge.
  122. prop.set_battery_state(PowerSupplyProperties::DISCHARGING);
  123. power_status_->SetProtoForTesting(prop);
  124. EXPECT_EQ(unreliable_icon, power_status_->GetBatteryImageInfo().icon_badge);
  125. // Show the right icon when no battery is present.
  126. prop.set_external_power(PowerSupplyProperties::DISCONNECTED);
  127. prop.set_battery_state(PowerSupplyProperties::NOT_PRESENT);
  128. power_status_->SetProtoForTesting(prop);
  129. const gfx::VectorIcon* x_icon =
  130. power_status_->GetBatteryImageInfo().icon_badge;
  131. EXPECT_TRUE(x_icon);
  132. EXPECT_NE(bolt_icon, x_icon);
  133. EXPECT_NE(unreliable_icon, x_icon);
  134. // Do not show a badge when the battery is discharging.
  135. prop.set_battery_state(PowerSupplyProperties::DISCHARGING);
  136. power_status_->SetProtoForTesting(prop);
  137. EXPECT_FALSE(power_status_->GetBatteryImageInfo().icon_badge);
  138. // Show the right icon for a discharging battery when it falls below
  139. // a charge level of PowerStatus::kCriticalBatteryChargePercentage.
  140. prop.set_battery_percent(PowerStatus::kCriticalBatteryChargePercentage);
  141. power_status_->SetProtoForTesting(prop);
  142. EXPECT_FALSE(power_status_->GetBatteryImageInfo().icon_badge);
  143. prop.set_battery_percent(PowerStatus::kCriticalBatteryChargePercentage - 1);
  144. power_status_->SetProtoForTesting(prop);
  145. const gfx::VectorIcon* alert_icon =
  146. power_status_->GetBatteryImageInfo().icon_badge;
  147. EXPECT_TRUE(alert_icon);
  148. EXPECT_NE(bolt_icon, alert_icon);
  149. EXPECT_NE(unreliable_icon, alert_icon);
  150. EXPECT_NE(x_icon, alert_icon);
  151. }
  152. // Tests that the battery image changes appropriately with various power supply
  153. // property values.
  154. TEST_F(PowerStatusTest, BatteryImageInfoChargeLevel) {
  155. PowerSupplyProperties prop;
  156. // No charge level is drawn when the battery is not present.
  157. prop.set_external_power(PowerSupplyProperties::DISCONNECTED);
  158. prop.set_battery_state(PowerSupplyProperties::NOT_PRESENT);
  159. power_status_->SetProtoForTesting(prop);
  160. EXPECT_EQ(0, power_status_->GetBatteryImageInfo().charge_percent);
  161. PowerStatus* power_status = power_status_;
  162. auto get_battery_image = [&power_status]() {
  163. return gfx::Image(
  164. power_status->GetBatteryImage(power_status->GetBatteryImageInfo(), 16,
  165. SK_ColorYELLOW, SK_ColorGREEN));
  166. };
  167. prop.set_external_power(PowerSupplyProperties::AC);
  168. prop.set_battery_state(PowerSupplyProperties::CHARGING);
  169. prop.set_battery_percent(0.0);
  170. EXPECT_EQ(0, power_status_->GetBatteryImageInfo().charge_percent);
  171. gfx::Image empty_image = get_battery_image();
  172. // 10% and 20% look different.
  173. prop.set_battery_percent(10.0);
  174. power_status_->SetProtoForTesting(prop);
  175. EXPECT_EQ(10.0, power_status_->GetBatteryImageInfo().charge_percent);
  176. gfx::Image image_10 = get_battery_image();
  177. EXPECT_FALSE(gfx::test::AreImagesEqual(empty_image, image_10));
  178. prop.set_battery_percent(20.0);
  179. power_status_->SetProtoForTesting(prop);
  180. EXPECT_EQ(20.0, power_status_->GetBatteryImageInfo().charge_percent);
  181. gfx::Image image_20 = get_battery_image();
  182. EXPECT_FALSE(gfx::test::AreImagesEqual(image_10, image_20));
  183. // 99% and 100% look different.
  184. prop.set_battery_percent(99.0);
  185. power_status_->SetProtoForTesting(prop);
  186. EXPECT_EQ(99, power_status_->GetBatteryImageInfo().charge_percent);
  187. gfx::Image image_99 = get_battery_image();
  188. prop.set_battery_percent(100.0);
  189. power_status_->SetProtoForTesting(prop);
  190. EXPECT_EQ(100, power_status_->GetBatteryImageInfo().charge_percent);
  191. gfx::Image image_100 = get_battery_image();
  192. EXPECT_FALSE(gfx::test::AreImagesEqual(image_99, image_100));
  193. }
  194. // Tests that positive time-to-full and time-to-empty estimates are honored.
  195. TEST_F(PowerStatusTest, PositiveBatteryTimeEstimates) {
  196. constexpr auto kTime = base::Seconds(120);
  197. PowerSupplyProperties prop;
  198. prop.set_external_power(PowerSupplyProperties::AC);
  199. prop.set_battery_state(PowerSupplyProperties::CHARGING);
  200. prop.set_battery_time_to_full_sec(kTime.InSeconds());
  201. power_status_->SetProtoForTesting(prop);
  202. absl::optional<base::TimeDelta> time = power_status_->GetBatteryTimeToFull();
  203. ASSERT_TRUE(time);
  204. EXPECT_EQ(kTime, *time);
  205. prop.Clear();
  206. prop.set_external_power(PowerSupplyProperties::DISCONNECTED);
  207. prop.set_battery_state(PowerSupplyProperties::DISCHARGING);
  208. prop.set_battery_time_to_empty_sec(kTime.InSeconds());
  209. power_status_->SetProtoForTesting(prop);
  210. time = power_status_->GetBatteryTimeToEmpty();
  211. ASSERT_TRUE(time);
  212. EXPECT_EQ(kTime, *time);
  213. }
  214. // Tests that missing time-to-full and time-to-empty estimates (which powerd
  215. // sends when no battery is present) and negative ones (which powerd sends when
  216. // the battery current is close to zero) are disregarded:
  217. // https://crbug.com/930358
  218. TEST_F(PowerStatusTest, MissingBatteryTimeEstimates) {
  219. // No battery.
  220. PowerSupplyProperties prop;
  221. prop.set_external_power(PowerSupplyProperties::AC);
  222. prop.set_battery_state(PowerSupplyProperties::NOT_PRESENT);
  223. power_status_->SetProtoForTesting(prop);
  224. absl::optional<base::TimeDelta> time = power_status_->GetBatteryTimeToFull();
  225. EXPECT_FALSE(time) << *time << " returned despite missing battery";
  226. time = power_status_->GetBatteryTimeToEmpty();
  227. EXPECT_FALSE(time) << *time << " returned despite missing battery";
  228. // Battery is charging, but negative estimate provided.
  229. prop.set_battery_state(PowerSupplyProperties::CHARGING);
  230. prop.set_battery_time_to_full_sec(-1);
  231. power_status_->SetProtoForTesting(prop);
  232. time = power_status_->GetBatteryTimeToFull();
  233. EXPECT_FALSE(time) << *time << " returned despite negative estimate";
  234. // Battery is discharging, but negative estimate provided.
  235. prop.Clear();
  236. prop.set_external_power(PowerSupplyProperties::DISCONNECTED);
  237. prop.set_battery_state(PowerSupplyProperties::DISCHARGING);
  238. prop.set_battery_time_to_empty_sec(-1);
  239. power_status_->SetProtoForTesting(prop);
  240. time = power_status_->GetBatteryTimeToEmpty();
  241. EXPECT_FALSE(time) << *time << " returned despite negative estimate";
  242. }
  243. TEST_F(PowerStatusTest, PreferredMinimumExternalPower) {
  244. PowerSupplyProperties prop;
  245. prop.set_external_power(PowerSupplyProperties::USB);
  246. prop.set_battery_state(PowerSupplyProperties::NOT_PRESENT);
  247. prop.set_preferred_minimum_external_power(23.45);
  248. power_status_->SetProtoForTesting(prop);
  249. EXPECT_EQ(23.45, power_status_->GetPreferredMinimumPower());
  250. }
  251. } // namespace ash