tray_power_unittest.cc revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
1// Copyright 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/tray_power.h"
6
7#include "ash/ash_switches.h"
8#include "ash/test/ash_test_base.h"
9#include "base/memory/scoped_ptr.h"
10#include "chromeos/dbus/power_manager/power_supply_properties.pb.h"
11#include "ui/message_center/fake_message_center.h"
12
13using message_center::Notification;
14using power_manager::PowerSupplyProperties;
15
16namespace {
17
18class MockMessageCenter : public message_center::FakeMessageCenter {
19 public:
20  MockMessageCenter() : add_count_(0), remove_count_(0) {}
21  virtual ~MockMessageCenter() {}
22
23  int add_count() const { return add_count_; }
24  int remove_count() const { return remove_count_; }
25
26  // message_center::FakeMessageCenter overrides:
27  virtual void AddNotification(scoped_ptr<Notification> notification) OVERRIDE {
28    add_count_++;
29  }
30  virtual void RemoveNotification(const std::string& id, bool by_user)
31      OVERRIDE {
32    remove_count_++;
33  }
34
35 private:
36  int add_count_;
37  int remove_count_;
38
39  DISALLOW_COPY_AND_ASSIGN(MockMessageCenter);
40};
41
42}  // namespace
43
44namespace ash {
45namespace internal {
46
47class TrayPowerTest : public test::AshTestBase {
48 public:
49  TrayPowerTest() {}
50  virtual ~TrayPowerTest() {}
51
52  MockMessageCenter* message_center() { return message_center_.get(); }
53  TrayPower* tray_power() { return tray_power_.get(); }
54
55  // test::AshTestBase::SetUp() overrides:
56  virtual void SetUp() OVERRIDE {
57    test::AshTestBase::SetUp();
58    message_center_.reset(new MockMessageCenter());
59    tray_power_.reset(new TrayPower(NULL, message_center_.get()));
60  }
61
62  virtual void TearDown() OVERRIDE {
63    tray_power_.reset();
64    message_center_.reset();
65    test::AshTestBase::TearDown();
66  }
67
68  TrayPower::NotificationState notification_state() const {
69    return tray_power_->notification_state_;
70  }
71
72  bool MaybeShowUsbChargerNotification(const PowerSupplyProperties& proto) {
73    PowerStatus::Get()->SetProtoForTesting(proto);
74    return tray_power_->MaybeShowUsbChargerNotification();
75  }
76
77  bool UpdateNotificationState(const PowerSupplyProperties& proto) {
78    PowerStatus::Get()->SetProtoForTesting(proto);
79    return tray_power_->UpdateNotificationState();
80  }
81
82  void SetUsbChargerConnected(bool connected) {
83    tray_power_->usb_charger_was_connected_ = connected;
84   }
85
86  // Returns a discharging PowerSupplyProperties more appropriate for testing.
87  static PowerSupplyProperties DefaultPowerSupplyProperties() {
88    PowerSupplyProperties proto;
89    proto.set_external_power(
90        power_manager::PowerSupplyProperties_ExternalPower_DISCONNECTED);
91    proto.set_battery_state(
92        power_manager::PowerSupplyProperties_BatteryState_DISCHARGING);
93    proto.set_battery_percent(50.0);
94    proto.set_battery_time_to_empty_sec(3 * 60 * 60);
95    proto.set_battery_time_to_full_sec(2 * 60 * 60);
96    proto.set_is_calculating_battery_time(false);
97    return proto;
98  }
99
100 private:
101  scoped_ptr<MockMessageCenter> message_center_;
102  scoped_ptr<TrayPower> tray_power_;
103
104  DISALLOW_COPY_AND_ASSIGN(TrayPowerTest);
105};
106
107TEST_F(TrayPowerTest, MaybeShowUsbChargerNotification) {
108  PowerSupplyProperties discharging = DefaultPowerSupplyProperties();
109  EXPECT_FALSE(MaybeShowUsbChargerNotification(discharging));
110  EXPECT_EQ(0, message_center()->add_count());
111  EXPECT_EQ(0, message_center()->remove_count());
112
113  // Notification shows when connecting a USB charger.
114  PowerSupplyProperties usb_connected = DefaultPowerSupplyProperties();
115  usb_connected.set_external_power(
116      power_manager::PowerSupplyProperties_ExternalPower_USB);
117  EXPECT_TRUE(MaybeShowUsbChargerNotification(usb_connected));
118  EXPECT_EQ(1, message_center()->add_count());
119  EXPECT_EQ(0, message_center()->remove_count());
120
121  // Change in charge does not trigger the notification again.
122  PowerSupplyProperties more_charge = DefaultPowerSupplyProperties();
123  more_charge.set_external_power(
124      power_manager::PowerSupplyProperties_ExternalPower_USB);
125  more_charge.set_battery_time_to_full_sec(60 * 60);
126  more_charge.set_battery_percent(75.0);
127  SetUsbChargerConnected(true);
128  EXPECT_FALSE(MaybeShowUsbChargerNotification(more_charge));
129  EXPECT_EQ(1, message_center()->add_count());
130  EXPECT_EQ(0, message_center()->remove_count());
131
132  // Disconnecting a USB charger with the notification showing should close
133  // the notification.
134  EXPECT_TRUE(MaybeShowUsbChargerNotification(discharging));
135  EXPECT_EQ(1, message_center()->add_count());
136  EXPECT_EQ(1, message_center()->remove_count());
137}
138
139TEST_F(TrayPowerTest, UpdateNotificationState) {
140  // No notifications when no battery present.
141  PowerSupplyProperties no_battery = DefaultPowerSupplyProperties();
142  no_battery.set_external_power(
143      power_manager::PowerSupplyProperties_ExternalPower_AC);
144  no_battery.set_battery_state(
145      power_manager::PowerSupplyProperties_BatteryState_NOT_PRESENT);
146  EXPECT_FALSE(UpdateNotificationState(no_battery));
147  EXPECT_EQ(TrayPower::NOTIFICATION_NONE, notification_state());
148
149  // No notification when calculating remaining battery time.
150  PowerSupplyProperties calculating = DefaultPowerSupplyProperties();
151  calculating.set_is_calculating_battery_time(true);
152  EXPECT_FALSE(UpdateNotificationState(calculating));
153  EXPECT_EQ(TrayPower::NOTIFICATION_NONE, notification_state());
154
155  // No notification when charging.
156  PowerSupplyProperties charging = DefaultPowerSupplyProperties();
157  charging.set_external_power(
158      power_manager::PowerSupplyProperties_ExternalPower_AC);
159  charging.set_battery_state(
160      power_manager::PowerSupplyProperties_BatteryState_CHARGING);
161  EXPECT_FALSE(UpdateNotificationState(charging));
162  EXPECT_EQ(TrayPower::NOTIFICATION_NONE, notification_state());
163
164  // Critical low battery notification.
165  PowerSupplyProperties critical = DefaultPowerSupplyProperties();
166  critical.set_battery_time_to_empty_sec(60);
167  critical.set_battery_percent(2.0);
168  EXPECT_TRUE(UpdateNotificationState(critical));
169  EXPECT_EQ(TrayPower::NOTIFICATION_CRITICAL, notification_state());
170}
171
172}  // namespace internal
173}  // namespace ash
174