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.
5 #include "ash/system/chromeos/power/power_status_view.h"
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/views/controls/label.h"
15 using power_manager::PowerSupplyProperties
;
19 class PowerStatusViewTest
: public test::AshTestBase
{
21 PowerStatusViewTest() {}
22 ~PowerStatusViewTest() override
{}
24 // Overridden from testing::Test:
25 void SetUp() override
{
26 test::AshTestBase::SetUp();
27 view_
.reset(new PowerStatusView(false));
30 void TearDown() override
{
32 test::AshTestBase::TearDown();
36 void UpdatePowerStatus(const power_manager::PowerSupplyProperties
& proto
) {
37 PowerStatus::Get()->SetProtoForTesting(proto
);
38 view_
->OnPowerStatusChanged();
41 bool IsPercentageVisible() const {
42 return view_
->percentage_label_
->visible();
45 bool IsTimeStatusVisible() const {
46 return view_
->time_status_label_
->visible();
49 base::string16
RemainingTimeInView() const {
50 return view_
->time_status_label_
->text();
54 scoped_ptr
<PowerStatusView
> view_
;
56 DISALLOW_COPY_AND_ASSIGN(PowerStatusViewTest
);
59 TEST_F(PowerStatusViewTest
, Basic
) {
60 EXPECT_FALSE(IsPercentageVisible());
61 EXPECT_TRUE(IsTimeStatusVisible());
63 // Disconnect the power.
64 PowerSupplyProperties prop
;
65 prop
.set_external_power(PowerSupplyProperties::DISCONNECTED
);
66 prop
.set_battery_state(PowerSupplyProperties::DISCHARGING
);
67 prop
.set_battery_percent(99.0);
68 prop
.set_battery_time_to_empty_sec(120);
69 prop
.set_is_calculating_battery_time(true);
70 UpdatePowerStatus(prop
);
72 EXPECT_TRUE(IsPercentageVisible());
73 EXPECT_TRUE(IsTimeStatusVisible());
74 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING
),
75 RemainingTimeInView());
77 prop
.set_is_calculating_battery_time(false);
78 UpdatePowerStatus(prop
);
79 EXPECT_NE(l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING
),
80 RemainingTimeInView());
82 l10n_util::GetStringUTF16(
83 IDS_ASH_STATUS_TRAY_BATTERY_CHARGING_UNRELIABLE
),
84 RemainingTimeInView());
86 prop
.set_external_power(PowerSupplyProperties::AC
);
87 prop
.set_battery_state(PowerSupplyProperties::CHARGING
);
88 prop
.set_battery_time_to_full_sec(120);
89 UpdatePowerStatus(prop
);
90 EXPECT_TRUE(IsPercentageVisible());
91 EXPECT_TRUE(IsTimeStatusVisible());
92 EXPECT_NE(l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING
),
93 RemainingTimeInView());
95 l10n_util::GetStringUTF16(
96 IDS_ASH_STATUS_TRAY_BATTERY_CHARGING_UNRELIABLE
),
97 RemainingTimeInView());
99 prop
.set_external_power(PowerSupplyProperties::USB
);
100 UpdatePowerStatus(prop
);
101 EXPECT_TRUE(IsPercentageVisible());
102 EXPECT_TRUE(IsTimeStatusVisible());
104 l10n_util::GetStringUTF16(
105 IDS_ASH_STATUS_TRAY_BATTERY_CHARGING_UNRELIABLE
),
106 RemainingTimeInView());
108 // Tricky -- connected to non-USB but still discharging. Not likely happening
109 // on production though.
110 prop
.set_external_power(PowerSupplyProperties::AC
);
111 prop
.set_battery_state(PowerSupplyProperties::DISCHARGING
);
112 prop
.set_battery_time_to_full_sec(120);
113 UpdatePowerStatus(prop
);
114 EXPECT_TRUE(IsPercentageVisible());
115 EXPECT_FALSE(IsTimeStatusVisible());