OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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_view.h" | 5 #include "ash/common/system/chromeos/power/power_status_view.h" |
6 | 6 |
7 #include "ash/common/material_design/material_design_controller.h" | |
8 #include "ash/common/system/chromeos/power/power_status.h" | 7 #include "ash/common/system/chromeos/power/power_status.h" |
9 #include "ash/test/ash_md_test_base.h" | 8 #include "ash/test/ash_test_base.h" |
10 #include "chromeos/dbus/power_manager/power_supply_properties.pb.h" | 9 #include "chromeos/dbus/power_manager/power_supply_properties.pb.h" |
11 #include "grit/ash_strings.h" | 10 #include "grit/ash_strings.h" |
12 #include "ui/base/l10n/l10n_util.h" | 11 #include "ui/base/l10n/l10n_util.h" |
13 #include "ui/base/l10n/time_format.h" | 12 #include "ui/base/l10n/time_format.h" |
14 #include "ui/gfx/image/image_skia.h" | 13 #include "ui/gfx/image/image_skia.h" |
15 #include "ui/views/controls/image_view.h" | 14 #include "ui/views/controls/image_view.h" |
16 #include "ui/views/controls/label.h" | 15 #include "ui/views/controls/label.h" |
17 | 16 |
18 using power_manager::PowerSupplyProperties; | 17 using power_manager::PowerSupplyProperties; |
19 | 18 |
20 namespace ash { | 19 namespace ash { |
21 | 20 |
22 class PowerStatusViewTest : public test::AshMDTestBase { | 21 class PowerStatusViewTest : public test::AshTestBase { |
23 public: | 22 public: |
24 PowerStatusViewTest() {} | 23 PowerStatusViewTest() {} |
25 ~PowerStatusViewTest() override {} | 24 ~PowerStatusViewTest() override {} |
26 | 25 |
27 // Overridden from testing::Test: | 26 // Overridden from testing::Test: |
28 void SetUp() override { | 27 void SetUp() override { |
29 test::AshMDTestBase::SetUp(); | 28 test::AshTestBase::SetUp(); |
30 view_.reset(new PowerStatusView(false)); | 29 view_.reset(new PowerStatusView(false)); |
31 } | 30 } |
32 | 31 |
33 void TearDown() override { | 32 void TearDown() override { |
34 view_.reset(); | 33 view_.reset(); |
35 test::AshMDTestBase::TearDown(); | 34 test::AshTestBase::TearDown(); |
36 } | 35 } |
37 | 36 |
38 protected: | 37 protected: |
39 void UpdatePowerStatus(const power_manager::PowerSupplyProperties& proto) { | 38 void UpdatePowerStatus(const power_manager::PowerSupplyProperties& proto) { |
40 PowerStatus::Get()->SetProtoForTesting(proto); | 39 PowerStatus::Get()->SetProtoForTesting(proto); |
41 view_->OnPowerStatusChanged(); | 40 view_->OnPowerStatusChanged(); |
42 } | 41 } |
43 | 42 |
44 bool IsPercentageVisible() const { | 43 bool IsPercentageVisible() const { |
45 return view_->percentage_label_->visible(); | 44 return view_->percentage_label_->visible(); |
46 } | 45 } |
47 | 46 |
48 bool IsTimeStatusVisible() const { | 47 bool IsTimeStatusVisible() const { |
49 return view_->time_status_label_->visible(); | 48 return view_->time_status_label_->visible(); |
50 } | 49 } |
51 | 50 |
52 base::string16 RemainingTimeInView() const { | 51 base::string16 RemainingTimeInView() const { |
53 return view_->time_status_label_->text(); | 52 return view_->time_status_label_->text(); |
54 } | 53 } |
55 | 54 |
56 gfx::ImageSkia GetBatteryImage() const { return view_->icon_->GetImage(); } | 55 gfx::ImageSkia GetBatteryImage() const { return view_->icon_->GetImage(); } |
57 | 56 |
58 private: | 57 private: |
59 std::unique_ptr<PowerStatusView> view_; | 58 std::unique_ptr<PowerStatusView> view_; |
60 | 59 |
61 DISALLOW_COPY_AND_ASSIGN(PowerStatusViewTest); | 60 DISALLOW_COPY_AND_ASSIGN(PowerStatusViewTest); |
62 }; | 61 }; |
63 | 62 |
64 INSTANTIATE_TEST_CASE_P( | 63 TEST_F(PowerStatusViewTest, Basic) { |
65 /* prefix intentionally left blank due to only one parameterization */, | |
66 PowerStatusViewTest, | |
67 testing::Values(MaterialDesignController::NON_MATERIAL, | |
68 MaterialDesignController::MATERIAL_NORMAL, | |
69 MaterialDesignController::MATERIAL_EXPERIMENTAL)); | |
70 | |
71 TEST_P(PowerStatusViewTest, Basic) { | |
72 EXPECT_FALSE(IsPercentageVisible()); | 64 EXPECT_FALSE(IsPercentageVisible()); |
73 EXPECT_TRUE(IsTimeStatusVisible()); | 65 EXPECT_TRUE(IsTimeStatusVisible()); |
74 | 66 |
75 // Disconnect the power. | 67 // Disconnect the power. |
76 PowerSupplyProperties prop; | 68 PowerSupplyProperties prop; |
77 prop.set_external_power(PowerSupplyProperties::DISCONNECTED); | 69 prop.set_external_power(PowerSupplyProperties::DISCONNECTED); |
78 prop.set_battery_state(PowerSupplyProperties::DISCHARGING); | 70 prop.set_battery_state(PowerSupplyProperties::DISCHARGING); |
79 prop.set_battery_percent(99.0); | 71 prop.set_battery_percent(99.0); |
80 prop.set_battery_time_to_empty_sec(120); | 72 prop.set_battery_time_to_empty_sec(120); |
81 prop.set_is_calculating_battery_time(true); | 73 prop.set_is_calculating_battery_time(true); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 // Tricky -- connected to non-USB but still discharging. Not likely happening | 109 // Tricky -- connected to non-USB but still discharging. Not likely happening |
118 // on production though. | 110 // on production though. |
119 prop.set_external_power(PowerSupplyProperties::AC); | 111 prop.set_external_power(PowerSupplyProperties::AC); |
120 prop.set_battery_state(PowerSupplyProperties::DISCHARGING); | 112 prop.set_battery_state(PowerSupplyProperties::DISCHARGING); |
121 prop.set_battery_time_to_full_sec(120); | 113 prop.set_battery_time_to_full_sec(120); |
122 UpdatePowerStatus(prop); | 114 UpdatePowerStatus(prop); |
123 EXPECT_TRUE(IsPercentageVisible()); | 115 EXPECT_TRUE(IsPercentageVisible()); |
124 EXPECT_FALSE(IsTimeStatusVisible()); | 116 EXPECT_FALSE(IsTimeStatusVisible()); |
125 } | 117 } |
126 | 118 |
127 TEST_P(PowerStatusViewTest, AvoidNeedlessBatteryImageUpdates) { | |
128 // No battery icon is shown in the material design system menu. | |
129 if (ash::MaterialDesignController::UseMaterialDesignSystemIcons()) | |
130 return; | |
131 | |
132 PowerSupplyProperties prop; | |
133 prop.set_external_power(PowerSupplyProperties::AC); | |
134 prop.set_battery_state(PowerSupplyProperties::CHARGING); | |
135 prop.set_battery_percent(50.0); | |
136 UpdatePowerStatus(prop); | |
137 | |
138 // Create a copy of the view's ImageSkia (backed by the same bitmap). We hang | |
139 // onto this to ensure that the original bitmap's memory doesn't get recycled | |
140 // for a new bitmap, ensuring that we can safely compare bitmap addresses | |
141 // later to check if the image that's being displayed has changed. | |
142 const gfx::ImageSkia original_image = GetBatteryImage(); | |
143 | |
144 // Send a no-op update. The old image should still be used. | |
145 UpdatePowerStatus(prop); | |
146 EXPECT_EQ(original_image.bitmap(), GetBatteryImage().bitmap()); | |
147 | |
148 // Make a big change to the percentage and check that a new image is used. | |
149 prop.set_battery_percent(100.0); | |
150 UpdatePowerStatus(prop); | |
151 EXPECT_NE(original_image.bitmap(), GetBatteryImage().bitmap()); | |
152 } | |
153 | |
154 } // namespace ash | 119 } // namespace ash |
OLD | NEW |