| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ash/system/chromeos/power/power_status.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 | |
| 9 #include "base/message_loop/message_loop.h" | |
| 10 #include "chromeos/dbus/dbus_thread_manager.h" | |
| 11 #include "testing/gtest/include/gtest/gtest.h" | |
| 12 #include "third_party/cros_system_api/dbus/service_constants.h" | |
| 13 | |
| 14 using power_manager::PowerSupplyProperties; | |
| 15 | |
| 16 namespace ash { | |
| 17 namespace { | |
| 18 | |
| 19 class TestObserver : public PowerStatus::Observer { | |
| 20 public: | |
| 21 TestObserver() : power_changed_count_(0) {} | |
| 22 ~TestObserver() override {} | |
| 23 | |
| 24 int power_changed_count() const { return power_changed_count_; } | |
| 25 | |
| 26 // PowerStatus::Observer overrides: | |
| 27 void OnPowerStatusChanged() override { ++power_changed_count_; } | |
| 28 | |
| 29 private: | |
| 30 int power_changed_count_; | |
| 31 | |
| 32 DISALLOW_COPY_AND_ASSIGN(TestObserver); | |
| 33 }; | |
| 34 | |
| 35 } // namespace | |
| 36 | |
| 37 class PowerStatusTest : public testing::Test { | |
| 38 public: | |
| 39 PowerStatusTest() : power_status_(NULL) {} | |
| 40 ~PowerStatusTest() override {} | |
| 41 | |
| 42 void SetUp() override { | |
| 43 chromeos::DBusThreadManager::Initialize(); | |
| 44 PowerStatus::Initialize(); | |
| 45 power_status_ = PowerStatus::Get(); | |
| 46 test_observer_.reset(new TestObserver); | |
| 47 power_status_->AddObserver(test_observer_.get()); | |
| 48 } | |
| 49 | |
| 50 void TearDown() override { | |
| 51 power_status_->RemoveObserver(test_observer_.get()); | |
| 52 test_observer_.reset(); | |
| 53 PowerStatus::Shutdown(); | |
| 54 chromeos::DBusThreadManager::Shutdown(); | |
| 55 } | |
| 56 | |
| 57 protected: | |
| 58 base::MessageLoopForUI message_loop_; | |
| 59 PowerStatus* power_status_; // Not owned. | |
| 60 std::unique_ptr<TestObserver> test_observer_; | |
| 61 | |
| 62 private: | |
| 63 DISALLOW_COPY_AND_ASSIGN(PowerStatusTest); | |
| 64 }; | |
| 65 | |
| 66 TEST_F(PowerStatusTest, InitializeAndUpdate) { | |
| 67 // Test that the initial power supply state should be acquired after | |
| 68 // PowerStatus is instantiated. This depends on | |
| 69 // PowerManagerClientStubImpl, which responds to power status update | |
| 70 // requests, pretends there is a battery present, and generates some valid | |
| 71 // power supply status data. | |
| 72 message_loop_.RunUntilIdle(); | |
| 73 EXPECT_EQ(1, test_observer_->power_changed_count()); | |
| 74 | |
| 75 // Test RequestUpdate, test_obsever_ should be notified for power suuply | |
| 76 // status change. | |
| 77 power_status_->RequestStatusUpdate(); | |
| 78 message_loop_.RunUntilIdle(); | |
| 79 EXPECT_EQ(2, test_observer_->power_changed_count()); | |
| 80 } | |
| 81 | |
| 82 TEST_F(PowerStatusTest, ShouldDisplayBatteryTime) { | |
| 83 EXPECT_FALSE(PowerStatus::ShouldDisplayBatteryTime( | |
| 84 base::TimeDelta::FromSeconds(-1))); | |
| 85 EXPECT_FALSE(PowerStatus::ShouldDisplayBatteryTime( | |
| 86 base::TimeDelta::FromSeconds(0))); | |
| 87 EXPECT_FALSE(PowerStatus::ShouldDisplayBatteryTime( | |
| 88 base::TimeDelta::FromSeconds(59))); | |
| 89 EXPECT_TRUE(PowerStatus::ShouldDisplayBatteryTime( | |
| 90 base::TimeDelta::FromSeconds(60))); | |
| 91 EXPECT_TRUE(PowerStatus::ShouldDisplayBatteryTime( | |
| 92 base::TimeDelta::FromSeconds(600))); | |
| 93 EXPECT_TRUE(PowerStatus::ShouldDisplayBatteryTime( | |
| 94 base::TimeDelta::FromSeconds(3600))); | |
| 95 EXPECT_TRUE(PowerStatus::ShouldDisplayBatteryTime( | |
| 96 base::TimeDelta::FromSeconds( | |
| 97 PowerStatus::kMaxBatteryTimeToDisplaySec))); | |
| 98 EXPECT_FALSE(PowerStatus::ShouldDisplayBatteryTime( | |
| 99 base::TimeDelta::FromSeconds( | |
| 100 PowerStatus::kMaxBatteryTimeToDisplaySec + 1))); | |
| 101 } | |
| 102 | |
| 103 TEST_F(PowerStatusTest, SplitTimeIntoHoursAndMinutes) { | |
| 104 int hours = 0, minutes = 0; | |
| 105 PowerStatus::SplitTimeIntoHoursAndMinutes( | |
| 106 base::TimeDelta::FromSeconds(0), &hours, &minutes); | |
| 107 EXPECT_EQ(0, hours); | |
| 108 EXPECT_EQ(0, minutes); | |
| 109 | |
| 110 PowerStatus::SplitTimeIntoHoursAndMinutes( | |
| 111 base::TimeDelta::FromSeconds(60), &hours, &minutes); | |
| 112 EXPECT_EQ(0, hours); | |
| 113 EXPECT_EQ(1, minutes); | |
| 114 | |
| 115 PowerStatus::SplitTimeIntoHoursAndMinutes( | |
| 116 base::TimeDelta::FromSeconds(3600), &hours, &minutes); | |
| 117 EXPECT_EQ(1, hours); | |
| 118 EXPECT_EQ(0, minutes); | |
| 119 | |
| 120 PowerStatus::SplitTimeIntoHoursAndMinutes( | |
| 121 base::TimeDelta::FromSeconds(3600 + 60), &hours, &minutes); | |
| 122 EXPECT_EQ(1, hours); | |
| 123 EXPECT_EQ(1, minutes); | |
| 124 | |
| 125 PowerStatus::SplitTimeIntoHoursAndMinutes( | |
| 126 base::TimeDelta::FromSeconds(7 * 3600 + 23 * 60), &hours, &minutes); | |
| 127 EXPECT_EQ(7, hours); | |
| 128 EXPECT_EQ(23, minutes); | |
| 129 | |
| 130 // Check that minutes are rounded. | |
| 131 PowerStatus::SplitTimeIntoHoursAndMinutes( | |
| 132 base::TimeDelta::FromSeconds(2 * 3600 + 3 * 60 + 30), &hours, &minutes); | |
| 133 EXPECT_EQ(2, hours); | |
| 134 EXPECT_EQ(4, minutes); | |
| 135 | |
| 136 PowerStatus::SplitTimeIntoHoursAndMinutes( | |
| 137 base::TimeDelta::FromSeconds(2 * 3600 + 3 * 60 + 29), &hours, &minutes); | |
| 138 EXPECT_EQ(2, hours); | |
| 139 EXPECT_EQ(3, minutes); | |
| 140 | |
| 141 // Check that times close to hour boundaries aren't incorrectly rounded such | |
| 142 // that they display 60 minutes: http://crbug.com/368261 | |
| 143 PowerStatus::SplitTimeIntoHoursAndMinutes( | |
| 144 base::TimeDelta::FromSecondsD(3599.9), &hours, &minutes); | |
| 145 EXPECT_EQ(1, hours); | |
| 146 EXPECT_EQ(0, minutes); | |
| 147 | |
| 148 PowerStatus::SplitTimeIntoHoursAndMinutes( | |
| 149 base::TimeDelta::FromSecondsD(3600.1), &hours, &minutes); | |
| 150 EXPECT_EQ(1, hours); | |
| 151 EXPECT_EQ(0, minutes); | |
| 152 } | |
| 153 | |
| 154 TEST_F(PowerStatusTest, GetBatteryImageInfo) { | |
| 155 PowerSupplyProperties prop; | |
| 156 prop.set_external_power(PowerSupplyProperties::AC); | |
| 157 prop.set_battery_state(PowerSupplyProperties::CHARGING); | |
| 158 prop.set_battery_percent(98.0); | |
| 159 power_status_->SetProtoForTesting(prop); | |
| 160 const PowerStatus::BatteryImageInfo info_charging_98 = | |
| 161 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT); | |
| 162 | |
| 163 // 99% should use the same icon as 98%. | |
| 164 prop.set_battery_percent(99.0); | |
| 165 power_status_->SetProtoForTesting(prop); | |
| 166 EXPECT_EQ(info_charging_98, | |
| 167 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT)); | |
| 168 | |
| 169 // The dark icon set should use a different image. | |
| 170 prop.set_battery_percent(98.0); | |
| 171 EXPECT_NE(info_charging_98, | |
| 172 power_status_->GetBatteryImageInfo(PowerStatus::ICON_DARK)); | |
| 173 | |
| 174 // A different icon should be used when the battery is full, too. | |
| 175 prop.set_battery_state(PowerSupplyProperties::FULL); | |
| 176 prop.set_battery_percent(100.0); | |
| 177 power_status_->SetProtoForTesting(prop); | |
| 178 EXPECT_NE(info_charging_98, | |
| 179 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT)); | |
| 180 | |
| 181 // A much-lower battery level should use a different icon. | |
| 182 prop.set_battery_state(PowerSupplyProperties::CHARGING); | |
| 183 prop.set_battery_percent(20.0); | |
| 184 power_status_->SetProtoForTesting(prop); | |
| 185 EXPECT_NE(info_charging_98, | |
| 186 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT)); | |
| 187 | |
| 188 // Ditto for 98%, but on USB instead of AC. | |
| 189 prop.set_external_power(PowerSupplyProperties::USB); | |
| 190 prop.set_battery_percent(98.0); | |
| 191 power_status_->SetProtoForTesting(prop); | |
| 192 EXPECT_NE(info_charging_98, | |
| 193 power_status_->GetBatteryImageInfo(PowerStatus::ICON_LIGHT)); | |
| 194 } | |
| 195 | |
| 196 } // namespace ash | |
| OLD | NEW |