OLD | NEW |
(Empty) | |
| 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 "base/memory/scoped_ptr.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "ui/message_center/fake_message_center.h" |
| 11 |
| 12 using chromeos::PowerSupplyStatus; |
| 13 using message_center::Notification; |
| 14 |
| 15 namespace { |
| 16 |
| 17 class MockMessageCenter : public message_center::FakeMessageCenter { |
| 18 public: |
| 19 MockMessageCenter() : add_count_(0), remove_count_(0) {} |
| 20 virtual ~MockMessageCenter() {} |
| 21 |
| 22 int add_count() const { return add_count_; } |
| 23 int remove_count() const { return remove_count_; } |
| 24 |
| 25 // message_center::FakeMessageCenter overrides: |
| 26 virtual void AddNotification(scoped_ptr<Notification> notification) OVERRIDE { |
| 27 add_count_++; |
| 28 } |
| 29 virtual void RemoveNotification(const std::string& id, bool by_user) |
| 30 OVERRIDE { |
| 31 remove_count_++; |
| 32 } |
| 33 |
| 34 private: |
| 35 int add_count_; |
| 36 int remove_count_; |
| 37 |
| 38 DISALLOW_COPY_AND_ASSIGN(MockMessageCenter); |
| 39 }; |
| 40 |
| 41 } // namespace |
| 42 |
| 43 namespace ash { |
| 44 namespace internal { |
| 45 |
| 46 class TrayPowerTest : public testing::Test { |
| 47 public: |
| 48 TrayPowerTest() {} |
| 49 virtual ~TrayPowerTest() {} |
| 50 |
| 51 MockMessageCenter* message_center() { return message_center_.get(); } |
| 52 TrayPower* tray_power() { return tray_power_.get(); } |
| 53 |
| 54 // testing::Test overrides: |
| 55 virtual void SetUp() OVERRIDE { |
| 56 message_center_.reset(new MockMessageCenter()); |
| 57 tray_power_.reset(new TrayPower(NULL, message_center_.get())); |
| 58 } |
| 59 |
| 60 TrayPower::NotificationState notification_state() const { |
| 61 return tray_power_->notification_state_; |
| 62 } |
| 63 |
| 64 bool MaybeShowUsbChargerNotification(const PowerSupplyStatus& old_status, |
| 65 const PowerSupplyStatus& new_status) { |
| 66 return tray_power_->MaybeShowUsbChargerNotification(old_status, new_status); |
| 67 } |
| 68 |
| 69 bool UpdateNotificationState(const PowerSupplyStatus& status) { |
| 70 return tray_power_->UpdateNotificationState(status); |
| 71 } |
| 72 |
| 73 void SetLastPowerStatus(const PowerSupplyStatus& status) { |
| 74 tray_power_->last_power_supply_status_ = status; |
| 75 } |
| 76 |
| 77 // Returns a discharging PowerSupplyStatus more appropriate for testing. |
| 78 static PowerSupplyStatus DefaultPowerSupplyStatus() { |
| 79 PowerSupplyStatus status; |
| 80 status.line_power_on = false; |
| 81 status.battery_is_present = true; |
| 82 status.battery_is_full = false; |
| 83 status.battery_seconds_to_empty = 3 * 60 * 60; |
| 84 status.battery_seconds_to_full = 2 * 60 * 60; |
| 85 status.battery_percentage = 50.0; |
| 86 status.is_calculating_battery_time = false; |
| 87 status.battery_state = PowerSupplyStatus::DISCHARGING; |
| 88 return status; |
| 89 } |
| 90 |
| 91 private: |
| 92 scoped_ptr<MockMessageCenter> message_center_; |
| 93 scoped_ptr<TrayPower> tray_power_; |
| 94 |
| 95 DISALLOW_COPY_AND_ASSIGN(TrayPowerTest); |
| 96 }; |
| 97 |
| 98 TEST_F(TrayPowerTest, MaybeShowUsbChargerNotification) { |
| 99 // Notification shows when connecting a USB charger. |
| 100 PowerSupplyStatus discharging = DefaultPowerSupplyStatus(); |
| 101 PowerSupplyStatus usb_connected = DefaultPowerSupplyStatus(); |
| 102 usb_connected.line_power_on = true; |
| 103 usb_connected.battery_state = PowerSupplyStatus::CONNECTED_TO_USB; |
| 104 EXPECT_TRUE(MaybeShowUsbChargerNotification(discharging, usb_connected)); |
| 105 EXPECT_EQ(1, message_center()->add_count()); |
| 106 |
| 107 // Change in charge does not trigger the notification again. |
| 108 PowerSupplyStatus more_charge = DefaultPowerSupplyStatus(); |
| 109 more_charge.line_power_on = true; |
| 110 more_charge.battery_seconds_to_full = 60 * 60; |
| 111 more_charge.battery_percentage = 75.0; |
| 112 more_charge.battery_state = PowerSupplyStatus::CONNECTED_TO_USB; |
| 113 EXPECT_FALSE(MaybeShowUsbChargerNotification(usb_connected, more_charge)); |
| 114 EXPECT_EQ(1, message_center()->add_count()); |
| 115 EXPECT_EQ(0, message_center()->remove_count()); |
| 116 |
| 117 // Disconnecting a USB charger with the notification showing should close |
| 118 // the notification. |
| 119 EXPECT_TRUE(MaybeShowUsbChargerNotification(usb_connected, discharging)); |
| 120 EXPECT_EQ(1, message_center()->remove_count()); |
| 121 } |
| 122 |
| 123 TEST_F(TrayPowerTest, UpdateNotificationState) { |
| 124 // No notifications when no battery present. |
| 125 PowerSupplyStatus no_battery = DefaultPowerSupplyStatus(); |
| 126 no_battery.battery_is_present = false; |
| 127 EXPECT_FALSE(UpdateNotificationState(no_battery)); |
| 128 EXPECT_EQ(TrayPower::NOTIFICATION_NONE, notification_state()); |
| 129 |
| 130 // No notification when calculating remaining battery time. |
| 131 PowerSupplyStatus calculating = DefaultPowerSupplyStatus(); |
| 132 calculating.is_calculating_battery_time = true; |
| 133 EXPECT_FALSE(UpdateNotificationState(calculating)); |
| 134 EXPECT_EQ(TrayPower::NOTIFICATION_NONE, notification_state()); |
| 135 |
| 136 // No notification when charging. |
| 137 PowerSupplyStatus charging = DefaultPowerSupplyStatus(); |
| 138 charging.line_power_on = true; |
| 139 charging.battery_state = PowerSupplyStatus::CHARGING; |
| 140 EXPECT_FALSE(UpdateNotificationState(charging)); |
| 141 EXPECT_EQ(TrayPower::NOTIFICATION_NONE, notification_state()); |
| 142 |
| 143 // Critical low battery notification. |
| 144 PowerSupplyStatus critical = DefaultPowerSupplyStatus(); |
| 145 critical.battery_seconds_to_empty = 60; |
| 146 critical.battery_percentage = 2.0; |
| 147 EXPECT_TRUE(UpdateNotificationState(critical)); |
| 148 EXPECT_EQ(TrayPower::NOTIFICATION_CRITICAL, notification_state()); |
| 149 } |
| 150 |
| 151 } // namespace internal |
| 152 } // namespace ash |
OLD | NEW |