| OLD | NEW |
| (Empty) |
| 1 // Copyright 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 | |
| 5 #include "ash/system/chromeos/power/power_status_view.h" | |
| 6 | |
| 7 #include "ash/system/chromeos/power/power_status.h" | |
| 8 #include "ash/test/ash_test_base.h" | |
| 9 #include "chromeos/dbus/power_manager/power_supply_properties.pb.h" | |
| 10 #include "grit/ash_strings.h" | |
| 11 #include "ui/base/l10n/l10n_util.h" | |
| 12 #include "ui/base/l10n/time_format.h" | |
| 13 #include "ui/gfx/image/image_skia.h" | |
| 14 #include "ui/views/controls/image_view.h" | |
| 15 #include "ui/views/controls/label.h" | |
| 16 | |
| 17 using power_manager::PowerSupplyProperties; | |
| 18 | |
| 19 namespace ash { | |
| 20 | |
| 21 class PowerStatusViewTest : public test::AshTestBase { | |
| 22 public: | |
| 23 PowerStatusViewTest() {} | |
| 24 ~PowerStatusViewTest() override {} | |
| 25 | |
| 26 // Overridden from testing::Test: | |
| 27 void SetUp() override { | |
| 28 test::AshTestBase::SetUp(); | |
| 29 view_.reset(new PowerStatusView(false)); | |
| 30 } | |
| 31 | |
| 32 void TearDown() override { | |
| 33 view_.reset(); | |
| 34 test::AshTestBase::TearDown(); | |
| 35 } | |
| 36 | |
| 37 protected: | |
| 38 void UpdatePowerStatus(const power_manager::PowerSupplyProperties& proto) { | |
| 39 PowerStatus::Get()->SetProtoForTesting(proto); | |
| 40 view_->OnPowerStatusChanged(); | |
| 41 } | |
| 42 | |
| 43 bool IsPercentageVisible() const { | |
| 44 return view_->percentage_label_->visible(); | |
| 45 } | |
| 46 | |
| 47 bool IsTimeStatusVisible() const { | |
| 48 return view_->time_status_label_->visible(); | |
| 49 } | |
| 50 | |
| 51 base::string16 RemainingTimeInView() const { | |
| 52 return view_->time_status_label_->text(); | |
| 53 } | |
| 54 | |
| 55 gfx::ImageSkia GetBatteryImage() const { return view_->icon_->GetImage(); } | |
| 56 | |
| 57 private: | |
| 58 std::unique_ptr<PowerStatusView> view_; | |
| 59 | |
| 60 DISALLOW_COPY_AND_ASSIGN(PowerStatusViewTest); | |
| 61 }; | |
| 62 | |
| 63 TEST_F(PowerStatusViewTest, Basic) { | |
| 64 EXPECT_FALSE(IsPercentageVisible()); | |
| 65 EXPECT_TRUE(IsTimeStatusVisible()); | |
| 66 | |
| 67 // Disconnect the power. | |
| 68 PowerSupplyProperties prop; | |
| 69 prop.set_external_power(PowerSupplyProperties::DISCONNECTED); | |
| 70 prop.set_battery_state(PowerSupplyProperties::DISCHARGING); | |
| 71 prop.set_battery_percent(99.0); | |
| 72 prop.set_battery_time_to_empty_sec(120); | |
| 73 prop.set_is_calculating_battery_time(true); | |
| 74 UpdatePowerStatus(prop); | |
| 75 | |
| 76 EXPECT_TRUE(IsPercentageVisible()); | |
| 77 EXPECT_TRUE(IsTimeStatusVisible()); | |
| 78 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING), | |
| 79 RemainingTimeInView()); | |
| 80 | |
| 81 prop.set_is_calculating_battery_time(false); | |
| 82 UpdatePowerStatus(prop); | |
| 83 EXPECT_NE(l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING), | |
| 84 RemainingTimeInView()); | |
| 85 EXPECT_NE( | |
| 86 l10n_util::GetStringUTF16( | |
| 87 IDS_ASH_STATUS_TRAY_BATTERY_CHARGING_UNRELIABLE), | |
| 88 RemainingTimeInView()); | |
| 89 | |
| 90 prop.set_external_power(PowerSupplyProperties::AC); | |
| 91 prop.set_battery_state(PowerSupplyProperties::CHARGING); | |
| 92 prop.set_battery_time_to_full_sec(120); | |
| 93 UpdatePowerStatus(prop); | |
| 94 EXPECT_TRUE(IsPercentageVisible()); | |
| 95 EXPECT_TRUE(IsTimeStatusVisible()); | |
| 96 EXPECT_NE(l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING), | |
| 97 RemainingTimeInView()); | |
| 98 EXPECT_NE( | |
| 99 l10n_util::GetStringUTF16( | |
| 100 IDS_ASH_STATUS_TRAY_BATTERY_CHARGING_UNRELIABLE), | |
| 101 RemainingTimeInView()); | |
| 102 | |
| 103 prop.set_external_power(PowerSupplyProperties::USB); | |
| 104 UpdatePowerStatus(prop); | |
| 105 EXPECT_TRUE(IsPercentageVisible()); | |
| 106 EXPECT_TRUE(IsTimeStatusVisible()); | |
| 107 EXPECT_EQ( | |
| 108 l10n_util::GetStringUTF16( | |
| 109 IDS_ASH_STATUS_TRAY_BATTERY_CHARGING_UNRELIABLE), | |
| 110 RemainingTimeInView()); | |
| 111 | |
| 112 // Tricky -- connected to non-USB but still discharging. Not likely happening | |
| 113 // on production though. | |
| 114 prop.set_external_power(PowerSupplyProperties::AC); | |
| 115 prop.set_battery_state(PowerSupplyProperties::DISCHARGING); | |
| 116 prop.set_battery_time_to_full_sec(120); | |
| 117 UpdatePowerStatus(prop); | |
| 118 EXPECT_TRUE(IsPercentageVisible()); | |
| 119 EXPECT_FALSE(IsTimeStatusVisible()); | |
| 120 } | |
| 121 | |
| 122 TEST_F(PowerStatusViewTest, AvoidNeedlessBatteryImageUpdates) { | |
| 123 PowerSupplyProperties prop; | |
| 124 prop.set_external_power(PowerSupplyProperties::AC); | |
| 125 prop.set_battery_state(PowerSupplyProperties::CHARGING); | |
| 126 prop.set_battery_percent(50.0); | |
| 127 UpdatePowerStatus(prop); | |
| 128 | |
| 129 // Create a copy of the view's ImageSkia (backed by the same bitmap). We hang | |
| 130 // onto this to ensure that the original bitmap's memory doesn't get recycled | |
| 131 // for a new bitmap, ensuring that we can safely compare bitmap addresses | |
| 132 // later to check if the image that's being displayed has changed. | |
| 133 const gfx::ImageSkia original_image = GetBatteryImage(); | |
| 134 | |
| 135 // Send a no-op update. The old image should still be used. | |
| 136 UpdatePowerStatus(prop); | |
| 137 EXPECT_EQ(original_image.bitmap(), GetBatteryImage().bitmap()); | |
| 138 | |
| 139 // Make a big change to the percentage and check that a new image is used. | |
| 140 prop.set_battery_percent(100.0); | |
| 141 UpdatePowerStatus(prop); | |
| 142 EXPECT_NE(original_image.bitmap(), GetBatteryImage().bitmap()); | |
| 143 } | |
| 144 | |
| 145 } // namespace ash | |
| OLD | NEW |