power_status_view_unittest.cc revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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/views/controls/label.h" 14 15using power_manager::PowerSupplyProperties; 16 17namespace ash { 18namespace internal { 19 20class PowerStatusViewTest : public test::AshTestBase { 21 public: 22 PowerStatusViewTest() {} 23 virtual ~PowerStatusViewTest() {} 24 25 // Overridden from testing::Test: 26 virtual void SetUp() OVERRIDE { 27 test::AshTestBase::SetUp(); 28 view_.reset(new PowerStatusView(GetViewType(), false)); 29 } 30 31 virtual void TearDown() OVERRIDE { 32 view_.reset(); 33 test::AshTestBase::TearDown(); 34 } 35 36 protected: 37 virtual PowerStatusView::ViewType GetViewType() = 0; 38 PowerStatusView* view() { return view_.get(); } 39 40 void UpdatePowerStatus(const power_manager::PowerSupplyProperties& proto) { 41 PowerStatus::Get()->SetProtoForTesting(proto); 42 view_->OnPowerStatusChanged(); 43 } 44 45 private: 46 scoped_ptr<PowerStatusView> view_; 47 48 DISALLOW_COPY_AND_ASSIGN(PowerStatusViewTest); 49}; 50 51class PowerStatusDefaultViewTest : public PowerStatusViewTest { 52 public: 53 PowerStatusDefaultViewTest() {} 54 virtual ~PowerStatusDefaultViewTest() {} 55 56 protected: 57 virtual PowerStatusView::ViewType GetViewType() OVERRIDE { 58 return PowerStatusView::VIEW_DEFAULT; 59 } 60 61 bool IsPercentageVisible() { 62 return view()->percentage_label_->visible(); 63 } 64 65 bool IsTimeStatusVisible() { 66 return view()->time_status_label_->visible(); 67 } 68 69 base::string16 RemainingTimeInView() { 70 return view()->time_status_label_->text(); 71 } 72 73 private: 74 DISALLOW_COPY_AND_ASSIGN(PowerStatusDefaultViewTest); 75}; 76 77class PowerStatusNotificationViewTest : public PowerStatusViewTest { 78 public: 79 PowerStatusNotificationViewTest() {} 80 virtual ~PowerStatusNotificationViewTest() {} 81 82 protected: 83 virtual PowerStatusView::ViewType GetViewType() OVERRIDE { 84 return PowerStatusView::VIEW_NOTIFICATION; 85 } 86 87 base::string16 StatusInView() { 88 return view()->status_label_->text(); 89 } 90 91 base::string16 RemainingTimeInView() { 92 return view()->time_label_->text(); 93 } 94 95 private: 96 DISALLOW_COPY_AND_ASSIGN(PowerStatusNotificationViewTest); 97}; 98 99TEST_F(PowerStatusDefaultViewTest, Basic) { 100 EXPECT_FALSE(IsPercentageVisible()); 101 EXPECT_TRUE(IsTimeStatusVisible()); 102 103 // Disconnect the power. 104 PowerSupplyProperties prop; 105 prop.set_external_power(PowerSupplyProperties::DISCONNECTED); 106 prop.set_battery_state(PowerSupplyProperties::DISCHARGING); 107 prop.set_battery_percent(99.0); 108 prop.set_battery_time_to_empty_sec(120); 109 prop.set_is_calculating_battery_time(true); 110 UpdatePowerStatus(prop); 111 112 EXPECT_TRUE(IsPercentageVisible()); 113 EXPECT_TRUE(IsTimeStatusVisible()); 114 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING), 115 RemainingTimeInView()); 116 117 prop.set_is_calculating_battery_time(false); 118 UpdatePowerStatus(prop); 119 EXPECT_NE(l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING), 120 RemainingTimeInView()); 121 EXPECT_NE( 122 l10n_util::GetStringUTF16( 123 IDS_ASH_STATUS_TRAY_BATTERY_CHARGING_UNRELIABLE), 124 RemainingTimeInView()); 125 126 prop.set_external_power(PowerSupplyProperties::AC); 127 prop.set_battery_state(PowerSupplyProperties::CHARGING); 128 prop.set_battery_time_to_full_sec(120); 129 UpdatePowerStatus(prop); 130 EXPECT_TRUE(IsPercentageVisible()); 131 EXPECT_TRUE(IsTimeStatusVisible()); 132 EXPECT_NE(l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING), 133 RemainingTimeInView()); 134 EXPECT_NE( 135 l10n_util::GetStringUTF16( 136 IDS_ASH_STATUS_TRAY_BATTERY_CHARGING_UNRELIABLE), 137 RemainingTimeInView()); 138 139 prop.set_external_power(PowerSupplyProperties::USB); 140 UpdatePowerStatus(prop); 141 EXPECT_TRUE(IsPercentageVisible()); 142 EXPECT_TRUE(IsTimeStatusVisible()); 143 EXPECT_EQ( 144 l10n_util::GetStringUTF16( 145 IDS_ASH_STATUS_TRAY_BATTERY_CHARGING_UNRELIABLE), 146 RemainingTimeInView()); 147 148 // Tricky -- connected to non-USB but still discharging. Not likely happening 149 // on production though. 150 prop.set_external_power(PowerSupplyProperties::AC); 151 prop.set_battery_state(PowerSupplyProperties::DISCHARGING); 152 prop.set_battery_time_to_full_sec(120); 153 UpdatePowerStatus(prop); 154 EXPECT_TRUE(IsPercentageVisible()); 155 EXPECT_FALSE(IsTimeStatusVisible()); 156} 157 158TEST_F(PowerStatusNotificationViewTest, Basic) { 159 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BATTERY_FULL), 160 StatusInView()); 161 EXPECT_TRUE(RemainingTimeInView().empty()); 162 163 // Disconnect the power. 164 PowerSupplyProperties prop; 165 prop.set_external_power(PowerSupplyProperties::DISCONNECTED); 166 prop.set_battery_state(PowerSupplyProperties::DISCHARGING); 167 prop.set_battery_percent(99.0); 168 prop.set_battery_time_to_empty_sec(125); 169 prop.set_is_calculating_battery_time(true); 170 UpdatePowerStatus(prop); 171 172 EXPECT_NE(l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BATTERY_FULL), 173 StatusInView()); 174 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BATTERY_CALCULATING), 175 RemainingTimeInView()); 176 177 prop.set_is_calculating_battery_time(false); 178 UpdatePowerStatus(prop); 179 // Low power warning has to be calculated by ui::TimeFormat, but ignore 180 // seconds. 181 EXPECT_EQ(ui::TimeFormat::Simple(ui::TimeFormat::FORMAT_REMAINING, 182 ui::TimeFormat::LENGTH_LONG, 183 base::TimeDelta::FromMinutes(2)), 184 RemainingTimeInView()); 185 186 prop.set_external_power(PowerSupplyProperties::AC); 187 prop.set_battery_state(PowerSupplyProperties::CHARGING); 188 prop.set_battery_time_to_full_sec(120); 189 UpdatePowerStatus(prop); 190 EXPECT_NE(l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_BATTERY_FULL), 191 StatusInView()); 192 // Charging time is somehow using another format? 193 EXPECT_NE(ui::TimeFormat::Simple(ui::TimeFormat::FORMAT_REMAINING, 194 ui::TimeFormat::LENGTH_LONG, 195 base::TimeDelta::FromMinutes(2)), 196 RemainingTimeInView()); 197 198 // Unreliable connection. 199 prop.set_external_power(PowerSupplyProperties::USB); 200 UpdatePowerStatus(prop); 201 EXPECT_EQ( 202 l10n_util::GetStringUTF16( 203 IDS_ASH_STATUS_TRAY_BATTERY_CHARGING_UNRELIABLE), 204 RemainingTimeInView()); 205 206 // Tricky -- connected to non-USB but still discharging. Not likely happening 207 // on production though. 208 prop.set_external_power(PowerSupplyProperties::AC); 209 prop.set_battery_state(PowerSupplyProperties::DISCHARGING); 210 prop.set_battery_time_to_full_sec(120); 211 UpdatePowerStatus(prop); 212 EXPECT_TRUE(RemainingTimeInView().empty()); 213} 214 215} // internal 216} // ash 217