| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ash/common/system/chromeos/power/power_status.h" | 5 #include "ash/common/system/chromeos/power/power_status.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "ash/common/material_design/material_design_controller.h" | |
| 10 #include "ash/common/test/material_design_controller_test_api.h" | |
| 11 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 12 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 13 #include "chromeos/dbus/dbus_thread_manager.h" | 11 #include "chromeos/dbus/dbus_thread_manager.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "third_party/cros_system_api/dbus/service_constants.h" | 13 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 16 | 14 |
| 17 using power_manager::PowerSupplyProperties; | 15 using power_manager::PowerSupplyProperties; |
| 18 | 16 |
| 19 namespace ash { | 17 namespace ash { |
| 20 namespace { | 18 namespace { |
| 21 | 19 |
| 22 class TestObserver : public PowerStatus::Observer { | 20 class TestObserver : public PowerStatus::Observer { |
| 23 public: | 21 public: |
| 24 TestObserver() : power_changed_count_(0) {} | 22 TestObserver() : power_changed_count_(0) {} |
| 25 ~TestObserver() override {} | 23 ~TestObserver() override {} |
| 26 | 24 |
| 27 int power_changed_count() const { return power_changed_count_; } | 25 int power_changed_count() const { return power_changed_count_; } |
| 28 | 26 |
| 29 // PowerStatus::Observer overrides: | 27 // PowerStatus::Observer overrides: |
| 30 void OnPowerStatusChanged() override { ++power_changed_count_; } | 28 void OnPowerStatusChanged() override { ++power_changed_count_; } |
| 31 | 29 |
| 32 private: | 30 private: |
| 33 int power_changed_count_; | 31 int power_changed_count_; |
| 34 | 32 |
| 35 DISALLOW_COPY_AND_ASSIGN(TestObserver); | 33 DISALLOW_COPY_AND_ASSIGN(TestObserver); |
| 36 }; | 34 }; |
| 37 | 35 |
| 38 } // namespace | 36 } // namespace |
| 39 | 37 |
| 40 class PowerStatusTest | 38 class PowerStatusTest : public testing::Test { |
| 41 : public testing::Test, | |
| 42 public testing::WithParamInterface<MaterialDesignController::Mode> { | |
| 43 public: | 39 public: |
| 44 PowerStatusTest() : power_status_(NULL) {} | 40 PowerStatusTest() : power_status_(NULL) {} |
| 45 ~PowerStatusTest() override {} | 41 ~PowerStatusTest() override {} |
| 46 | 42 |
| 47 void SetUp() override { | 43 void SetUp() override { |
| 48 chromeos::DBusThreadManager::Initialize(); | 44 chromeos::DBusThreadManager::Initialize(); |
| 49 PowerStatus::Initialize(); | 45 PowerStatus::Initialize(); |
| 50 power_status_ = PowerStatus::Get(); | 46 power_status_ = PowerStatus::Get(); |
| 51 test_observer_.reset(new TestObserver); | 47 test_observer_.reset(new TestObserver); |
| 52 power_status_->AddObserver(test_observer_.get()); | 48 power_status_->AddObserver(test_observer_.get()); |
| 53 material_design_state_.reset( | |
| 54 new test::MaterialDesignControllerTestAPI(GetParam())); | |
| 55 } | 49 } |
| 56 | 50 |
| 57 void TearDown() override { | 51 void TearDown() override { |
| 58 material_design_state_.reset(); | |
| 59 power_status_->RemoveObserver(test_observer_.get()); | 52 power_status_->RemoveObserver(test_observer_.get()); |
| 60 test_observer_.reset(); | 53 test_observer_.reset(); |
| 61 PowerStatus::Shutdown(); | 54 PowerStatus::Shutdown(); |
| 62 chromeos::DBusThreadManager::Shutdown(); | 55 chromeos::DBusThreadManager::Shutdown(); |
| 63 } | 56 } |
| 64 | 57 |
| 65 protected: | 58 protected: |
| 66 base::MessageLoopForUI message_loop_; | 59 base::MessageLoopForUI message_loop_; |
| 67 PowerStatus* power_status_; // Not owned. | 60 PowerStatus* power_status_; // Not owned. |
| 68 std::unique_ptr<TestObserver> test_observer_; | 61 std::unique_ptr<TestObserver> test_observer_; |
| 69 | 62 |
| 70 private: | 63 private: |
| 71 std::unique_ptr<test::MaterialDesignControllerTestAPI> material_design_state_; | |
| 72 | |
| 73 DISALLOW_COPY_AND_ASSIGN(PowerStatusTest); | 64 DISALLOW_COPY_AND_ASSIGN(PowerStatusTest); |
| 74 }; | 65 }; |
| 75 | 66 |
| 76 // The prefix has intentionally been left blank since there is only one | 67 TEST_F(PowerStatusTest, InitializeAndUpdate) { |
| 77 // parameterization of this test fixture. | |
| 78 INSTANTIATE_TEST_CASE_P( | |
| 79 /* prefix intentionally left blank */, | |
| 80 PowerStatusTest, | |
| 81 testing::Values(MaterialDesignController::NON_MATERIAL, | |
| 82 MaterialDesignController::MATERIAL_NORMAL, | |
| 83 MaterialDesignController::MATERIAL_EXPERIMENTAL)); | |
| 84 | |
| 85 TEST_P(PowerStatusTest, InitializeAndUpdate) { | |
| 86 // Test that the initial power supply state should be acquired after | 68 // Test that the initial power supply state should be acquired after |
| 87 // PowerStatus is instantiated. This depends on | 69 // PowerStatus is instantiated. This depends on |
| 88 // PowerManagerClientStubImpl, which responds to power status update | 70 // PowerManagerClientStubImpl, which responds to power status update |
| 89 // requests, pretends there is a battery present, and generates some valid | 71 // requests, pretends there is a battery present, and generates some valid |
| 90 // power supply status data. | 72 // power supply status data. |
| 91 base::RunLoop().RunUntilIdle(); | 73 base::RunLoop().RunUntilIdle(); |
| 92 EXPECT_EQ(1, test_observer_->power_changed_count()); | 74 EXPECT_EQ(1, test_observer_->power_changed_count()); |
| 93 | 75 |
| 94 // Test RequestUpdate, test_obsever_ should be notified for power suuply | 76 // Test RequestUpdate, test_obsever_ should be notified for power suuply |
| 95 // status change. | 77 // status change. |
| 96 power_status_->RequestStatusUpdate(); | 78 power_status_->RequestStatusUpdate(); |
| 97 base::RunLoop().RunUntilIdle(); | 79 base::RunLoop().RunUntilIdle(); |
| 98 EXPECT_EQ(2, test_observer_->power_changed_count()); | 80 EXPECT_EQ(2, test_observer_->power_changed_count()); |
| 99 } | 81 } |
| 100 | 82 |
| 101 TEST_P(PowerStatusTest, ShouldDisplayBatteryTime) { | 83 TEST_F(PowerStatusTest, ShouldDisplayBatteryTime) { |
| 102 EXPECT_FALSE( | 84 EXPECT_FALSE( |
| 103 PowerStatus::ShouldDisplayBatteryTime(base::TimeDelta::FromSeconds(-1))); | 85 PowerStatus::ShouldDisplayBatteryTime(base::TimeDelta::FromSeconds(-1))); |
| 104 EXPECT_FALSE( | 86 EXPECT_FALSE( |
| 105 PowerStatus::ShouldDisplayBatteryTime(base::TimeDelta::FromSeconds(0))); | 87 PowerStatus::ShouldDisplayBatteryTime(base::TimeDelta::FromSeconds(0))); |
| 106 EXPECT_FALSE( | 88 EXPECT_FALSE( |
| 107 PowerStatus::ShouldDisplayBatteryTime(base::TimeDelta::FromSeconds(59))); | 89 PowerStatus::ShouldDisplayBatteryTime(base::TimeDelta::FromSeconds(59))); |
| 108 EXPECT_TRUE( | 90 EXPECT_TRUE( |
| 109 PowerStatus::ShouldDisplayBatteryTime(base::TimeDelta::FromSeconds(60))); | 91 PowerStatus::ShouldDisplayBatteryTime(base::TimeDelta::FromSeconds(60))); |
| 110 EXPECT_TRUE( | 92 EXPECT_TRUE( |
| 111 PowerStatus::ShouldDisplayBatteryTime(base::TimeDelta::FromSeconds(600))); | 93 PowerStatus::ShouldDisplayBatteryTime(base::TimeDelta::FromSeconds(600))); |
| 112 EXPECT_TRUE(PowerStatus::ShouldDisplayBatteryTime( | 94 EXPECT_TRUE(PowerStatus::ShouldDisplayBatteryTime( |
| 113 base::TimeDelta::FromSeconds(3600))); | 95 base::TimeDelta::FromSeconds(3600))); |
| 114 EXPECT_TRUE(PowerStatus::ShouldDisplayBatteryTime( | 96 EXPECT_TRUE(PowerStatus::ShouldDisplayBatteryTime( |
| 115 base::TimeDelta::FromSeconds(PowerStatus::kMaxBatteryTimeToDisplaySec))); | 97 base::TimeDelta::FromSeconds(PowerStatus::kMaxBatteryTimeToDisplaySec))); |
| 116 EXPECT_FALSE( | 98 EXPECT_FALSE( |
| 117 PowerStatus::ShouldDisplayBatteryTime(base::TimeDelta::FromSeconds( | 99 PowerStatus::ShouldDisplayBatteryTime(base::TimeDelta::FromSeconds( |
| 118 PowerStatus::kMaxBatteryTimeToDisplaySec + 1))); | 100 PowerStatus::kMaxBatteryTimeToDisplaySec + 1))); |
| 119 } | 101 } |
| 120 | 102 |
| 121 TEST_P(PowerStatusTest, SplitTimeIntoHoursAndMinutes) { | 103 TEST_F(PowerStatusTest, SplitTimeIntoHoursAndMinutes) { |
| 122 int hours = 0, minutes = 0; | 104 int hours = 0, minutes = 0; |
| 123 PowerStatus::SplitTimeIntoHoursAndMinutes(base::TimeDelta::FromSeconds(0), | 105 PowerStatus::SplitTimeIntoHoursAndMinutes(base::TimeDelta::FromSeconds(0), |
| 124 &hours, &minutes); | 106 &hours, &minutes); |
| 125 EXPECT_EQ(0, hours); | 107 EXPECT_EQ(0, hours); |
| 126 EXPECT_EQ(0, minutes); | 108 EXPECT_EQ(0, minutes); |
| 127 | 109 |
| 128 PowerStatus::SplitTimeIntoHoursAndMinutes(base::TimeDelta::FromSeconds(60), | 110 PowerStatus::SplitTimeIntoHoursAndMinutes(base::TimeDelta::FromSeconds(60), |
| 129 &hours, &minutes); | 111 &hours, &minutes); |
| 130 EXPECT_EQ(0, hours); | 112 EXPECT_EQ(0, hours); |
| 131 EXPECT_EQ(1, minutes); | 113 EXPECT_EQ(1, minutes); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 162 base::TimeDelta::FromSecondsD(3599.9), &hours, &minutes); | 144 base::TimeDelta::FromSecondsD(3599.9), &hours, &minutes); |
| 163 EXPECT_EQ(1, hours); | 145 EXPECT_EQ(1, hours); |
| 164 EXPECT_EQ(0, minutes); | 146 EXPECT_EQ(0, minutes); |
| 165 | 147 |
| 166 PowerStatus::SplitTimeIntoHoursAndMinutes( | 148 PowerStatus::SplitTimeIntoHoursAndMinutes( |
| 167 base::TimeDelta::FromSecondsD(3600.1), &hours, &minutes); | 149 base::TimeDelta::FromSecondsD(3600.1), &hours, &minutes); |
| 168 EXPECT_EQ(1, hours); | 150 EXPECT_EQ(1, hours); |
| 169 EXPECT_EQ(0, minutes); | 151 EXPECT_EQ(0, minutes); |
| 170 } | 152 } |
| 171 | 153 |
| 172 TEST_P(PowerStatusTest, GetBatteryImageInfo) { | 154 TEST_F(PowerStatusTest, GetBatteryImageInfo) { |
| 173 const bool use_md_icon = | |
| 174 ash::MaterialDesignController::UseMaterialDesignSystemIcons(); | |
| 175 | |
| 176 PowerSupplyProperties prop; | 155 PowerSupplyProperties prop; |
| 177 prop.set_external_power(PowerSupplyProperties::AC); | 156 prop.set_external_power(PowerSupplyProperties::AC); |
| 178 prop.set_battery_state(PowerSupplyProperties::CHARGING); | 157 prop.set_battery_state(PowerSupplyProperties::CHARGING); |
| 179 prop.set_battery_percent(98.0); | 158 prop.set_battery_percent(98.0); |
| 180 power_status_->SetProtoForTesting(prop); | 159 power_status_->SetProtoForTesting(prop); |
| 181 const PowerStatus::BatteryImageInfo info_charging_98 = | 160 const PowerStatus::BatteryImageInfo info_charging_98 = |
| 182 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT); | 161 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT); |
| 183 | 162 |
| 184 // 99% should use the same icon as 98%. | 163 // 99% should use the same icon as 98%. |
| 185 prop.set_battery_percent(99.0); | 164 prop.set_battery_percent(99.0); |
| 186 power_status_->SetProtoForTesting(prop); | 165 power_status_->SetProtoForTesting(prop); |
| 187 EXPECT_EQ(info_charging_98, | 166 EXPECT_EQ(info_charging_98, |
| 188 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT)); | 167 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT)); |
| 189 | 168 |
| 190 // The dark icon set should use a different image for non-MD, but the | 169 // The dark icon set should use a different image for non-MD, but the |
| 191 // same image for MD. | 170 // same image for MD. |
| 192 prop.set_battery_percent(98.0); | 171 prop.set_battery_percent(98.0); |
| 193 if (use_md_icon) { | 172 EXPECT_EQ(info_charging_98, |
| 194 EXPECT_EQ(info_charging_98, | 173 power_status_->GetBatteryImageInfo(PowerStatus::ICON_DARK)); |
| 195 power_status_->GetBatteryImageInfo(PowerStatus::ICON_DARK)); | |
| 196 } else { | |
| 197 EXPECT_NE(info_charging_98, | |
| 198 power_status_->GetBatteryImageInfo(PowerStatus::ICON_DARK)); | |
| 199 } | |
| 200 | 174 |
| 201 // A different icon should be used when the battery is full, too. | 175 // A different icon should be used when the battery is full, too. |
| 202 prop.set_battery_state(PowerSupplyProperties::FULL); | 176 prop.set_battery_state(PowerSupplyProperties::FULL); |
| 203 prop.set_battery_percent(100.0); | 177 prop.set_battery_percent(100.0); |
| 204 power_status_->SetProtoForTesting(prop); | 178 power_status_->SetProtoForTesting(prop); |
| 205 EXPECT_NE(info_charging_98, | 179 EXPECT_NE(info_charging_98, |
| 206 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT)); | 180 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT)); |
| 207 | 181 |
| 208 // A much-lower battery level should use a different icon. | 182 // A much-lower battery level should use a different icon. |
| 209 prop.set_battery_state(PowerSupplyProperties::CHARGING); | 183 prop.set_battery_state(PowerSupplyProperties::CHARGING); |
| 210 prop.set_battery_percent(20.0); | 184 prop.set_battery_percent(20.0); |
| 211 power_status_->SetProtoForTesting(prop); | 185 power_status_->SetProtoForTesting(prop); |
| 212 EXPECT_NE(info_charging_98, | 186 EXPECT_NE(info_charging_98, |
| 213 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT)); | 187 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT)); |
| 214 | 188 |
| 215 // Ditto for 98%, but on USB instead of AC. | 189 // Ditto for 98%, but on USB instead of AC. |
| 216 prop.set_external_power(PowerSupplyProperties::USB); | 190 prop.set_external_power(PowerSupplyProperties::USB); |
| 217 prop.set_battery_percent(98.0); | 191 prop.set_battery_percent(98.0); |
| 218 power_status_->SetProtoForTesting(prop); | 192 power_status_->SetProtoForTesting(prop); |
| 219 EXPECT_NE(info_charging_98, | 193 EXPECT_NE(info_charging_98, |
| 220 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT)); | 194 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT)); |
| 221 } | 195 } |
| 222 | 196 |
| 223 // Tests that the |icon_badge| member of BatteryImageInfo is set correctly | 197 // Tests that the |icon_badge| member of BatteryImageInfo is set correctly |
| 224 // with various power supply property values. | 198 // with various power supply property values. |
| 225 TEST_P(PowerStatusTest, BatteryImageInfoIconBadge) { | 199 TEST_F(PowerStatusTest, BatteryImageInfoIconBadge) { |
| 226 // The |icon_badge| member is only populated for the material design | |
| 227 // battery icon. | |
| 228 if (!ash::MaterialDesignController::UseMaterialDesignSystemIcons()) | |
| 229 return; | |
| 230 | |
| 231 PowerSupplyProperties prop; | 200 PowerSupplyProperties prop; |
| 232 | 201 |
| 233 // A charging battery connected to AC power should have an ICON_BADGE_BOLT. | 202 // A charging battery connected to AC power should have an ICON_BADGE_BOLT. |
| 234 prop.set_external_power(PowerSupplyProperties::AC); | 203 prop.set_external_power(PowerSupplyProperties::AC); |
| 235 prop.set_battery_state(PowerSupplyProperties::CHARGING); | 204 prop.set_battery_state(PowerSupplyProperties::CHARGING); |
| 236 prop.set_battery_percent(98.0); | 205 prop.set_battery_percent(98.0); |
| 237 power_status_->SetProtoForTesting(prop); | 206 power_status_->SetProtoForTesting(prop); |
| 238 EXPECT_EQ( | 207 EXPECT_EQ( |
| 239 PowerStatus::ICON_BADGE_BOLT, | 208 PowerStatus::ICON_BADGE_BOLT, |
| 240 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT).icon_badge); | 209 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT).icon_badge); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 287 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT).icon_badge); | 256 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT).icon_badge); |
| 288 prop.set_battery_percent(PowerStatus::kCriticalBatteryChargePercentageMd - 1); | 257 prop.set_battery_percent(PowerStatus::kCriticalBatteryChargePercentageMd - 1); |
| 289 power_status_->SetProtoForTesting(prop); | 258 power_status_->SetProtoForTesting(prop); |
| 290 EXPECT_EQ( | 259 EXPECT_EQ( |
| 291 PowerStatus::ICON_BADGE_ALERT, | 260 PowerStatus::ICON_BADGE_ALERT, |
| 292 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT).icon_badge); | 261 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT).icon_badge); |
| 293 } | 262 } |
| 294 | 263 |
| 295 // Tests that the |charge_level| member of BatteryImageInfo is set correctly | 264 // Tests that the |charge_level| member of BatteryImageInfo is set correctly |
| 296 // with various power supply property values. | 265 // with various power supply property values. |
| 297 TEST_P(PowerStatusTest, BatteryImageInfoChargeLevel) { | 266 TEST_F(PowerStatusTest, BatteryImageInfoChargeLevel) { |
| 298 // The |charge_level| member is only populated for the material design | |
| 299 // battery icon. | |
| 300 if (!ash::MaterialDesignController::UseMaterialDesignSystemIcons()) | |
| 301 return; | |
| 302 | |
| 303 PowerSupplyProperties prop; | 267 PowerSupplyProperties prop; |
| 304 | 268 |
| 305 // No charge level is drawn when the battery is not present. | 269 // No charge level is drawn when the battery is not present. |
| 306 prop.set_external_power(PowerSupplyProperties::DISCONNECTED); | 270 prop.set_external_power(PowerSupplyProperties::DISCONNECTED); |
| 307 prop.set_battery_state(PowerSupplyProperties::NOT_PRESENT); | 271 prop.set_battery_state(PowerSupplyProperties::NOT_PRESENT); |
| 308 power_status_->SetProtoForTesting(prop); | 272 power_status_->SetProtoForTesting(prop); |
| 309 EXPECT_EQ( | 273 EXPECT_EQ( |
| 310 0, | 274 0, |
| 311 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT).charge_level); | 275 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT).charge_level); |
| 312 | 276 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 346 11, | 310 11, |
| 347 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT).charge_level); | 311 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT).charge_level); |
| 348 prop.set_battery_percent(100.0); | 312 prop.set_battery_percent(100.0); |
| 349 power_status_->SetProtoForTesting(prop); | 313 power_status_->SetProtoForTesting(prop); |
| 350 EXPECT_EQ( | 314 EXPECT_EQ( |
| 351 12, | 315 12, |
| 352 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT).charge_level); | 316 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT).charge_level); |
| 353 } | 317 } |
| 354 | 318 |
| 355 } // namespace ash | 319 } // namespace ash |
| OLD | NEW |