| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/system/chromeos/power/tray_power.h" | 5 #include "ash/system/chromeos/power/tray_power.h" |
| 6 | 6 |
| 7 #include "ash/ash_switches.h" | 7 #include "ash/ash_switches.h" |
| 8 #include "ash/test/ash_test_base.h" | 8 #include "ash/test/ash_test_base.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "chromeos/dbus/power_supply_status.h" |
| 10 #include "ui/message_center/fake_message_center.h" | 11 #include "ui/message_center/fake_message_center.h" |
| 11 | 12 |
| 12 using chromeos::PowerSupplyStatus; | 13 using chromeos::PowerSupplyStatus; |
| 13 using message_center::Notification; | 14 using message_center::Notification; |
| 14 | 15 |
| 15 namespace { | 16 namespace { |
| 16 | 17 |
| 17 class MockMessageCenter : public message_center::FakeMessageCenter { | 18 class MockMessageCenter : public message_center::FakeMessageCenter { |
| 18 public: | 19 public: |
| 19 MockMessageCenter() : add_count_(0), remove_count_(0) {} | 20 MockMessageCenter() : add_count_(0), remove_count_(0) {} |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 virtual void TearDown() OVERRIDE { | 62 virtual void TearDown() OVERRIDE { |
| 62 tray_power_.reset(); | 63 tray_power_.reset(); |
| 63 message_center_.reset(); | 64 message_center_.reset(); |
| 64 test::AshTestBase::TearDown(); | 65 test::AshTestBase::TearDown(); |
| 65 } | 66 } |
| 66 | 67 |
| 67 TrayPower::NotificationState notification_state() const { | 68 TrayPower::NotificationState notification_state() const { |
| 68 return tray_power_->notification_state_; | 69 return tray_power_->notification_state_; |
| 69 } | 70 } |
| 70 | 71 |
| 71 bool MaybeShowUsbChargerNotification(const PowerSupplyStatus& old_status, | 72 bool MaybeShowUsbChargerNotification(const PowerSupplyStatus& status) { |
| 72 const PowerSupplyStatus& new_status) { | 73 PowerStatus::Get()->set_status_for_testing(status); |
| 73 return tray_power_->MaybeShowUsbChargerNotification(old_status, new_status); | 74 return tray_power_->MaybeShowUsbChargerNotification(); |
| 74 } | 75 } |
| 75 | 76 |
| 76 bool UpdateNotificationState(const PowerSupplyStatus& status) { | 77 bool UpdateNotificationState(const PowerSupplyStatus& status) { |
| 77 return tray_power_->UpdateNotificationState(status); | 78 PowerStatus::Get()->set_status_for_testing(status); |
| 79 return tray_power_->UpdateNotificationState(); |
| 78 } | 80 } |
| 79 | 81 |
| 80 void SetLastPowerStatus(const PowerSupplyStatus& status) { | 82 void SetUsbChargerConnected(bool connected) { |
| 81 tray_power_->last_power_supply_status_ = status; | 83 tray_power_->usb_charger_was_connected_ = connected; |
| 82 } | 84 } |
| 83 | 85 |
| 84 // Returns a discharging PowerSupplyStatus more appropriate for testing. | 86 // Returns a discharging PowerSupplyStatus more appropriate for testing. |
| 85 static PowerSupplyStatus DefaultPowerSupplyStatus() { | 87 static PowerSupplyStatus DefaultPowerSupplyStatus() { |
| 86 PowerSupplyStatus status; | 88 PowerSupplyStatus status; |
| 87 status.line_power_on = false; | 89 status.line_power_on = false; |
| 88 status.battery_is_present = true; | 90 status.battery_is_present = true; |
| 89 status.battery_is_full = false; | 91 status.battery_is_full = false; |
| 90 status.battery_seconds_to_empty = 3 * 60 * 60; | 92 status.battery_seconds_to_empty = 3 * 60 * 60; |
| 91 status.battery_seconds_to_full = 2 * 60 * 60; | 93 status.battery_seconds_to_full = 2 * 60 * 60; |
| 92 status.battery_percentage = 50.0; | 94 status.battery_percentage = 50.0; |
| 93 status.is_calculating_battery_time = false; | 95 status.is_calculating_battery_time = false; |
| 94 status.battery_state = PowerSupplyStatus::DISCHARGING; | 96 status.battery_state = PowerSupplyStatus::DISCHARGING; |
| 95 return status; | 97 return status; |
| 96 } | 98 } |
| 97 | 99 |
| 98 private: | 100 private: |
| 99 scoped_ptr<MockMessageCenter> message_center_; | 101 scoped_ptr<MockMessageCenter> message_center_; |
| 100 scoped_ptr<TrayPower> tray_power_; | 102 scoped_ptr<TrayPower> tray_power_; |
| 101 | 103 |
| 102 DISALLOW_COPY_AND_ASSIGN(TrayPowerTest); | 104 DISALLOW_COPY_AND_ASSIGN(TrayPowerTest); |
| 103 }; | 105 }; |
| 104 | 106 |
| 105 TEST_F(TrayPowerTest, MaybeShowUsbChargerNotification) { | 107 TEST_F(TrayPowerTest, MaybeShowUsbChargerNotification) { |
| 108 PowerSupplyStatus discharging = DefaultPowerSupplyStatus(); |
| 109 EXPECT_FALSE(MaybeShowUsbChargerNotification(discharging)); |
| 110 EXPECT_EQ(0, message_center()->add_count()); |
| 111 EXPECT_EQ(0, message_center()->remove_count()); |
| 112 |
| 106 // Notification shows when connecting a USB charger. | 113 // Notification shows when connecting a USB charger. |
| 107 PowerSupplyStatus discharging = DefaultPowerSupplyStatus(); | |
| 108 PowerSupplyStatus usb_connected = DefaultPowerSupplyStatus(); | 114 PowerSupplyStatus usb_connected = DefaultPowerSupplyStatus(); |
| 109 usb_connected.line_power_on = true; | 115 usb_connected.line_power_on = true; |
| 110 usb_connected.battery_state = PowerSupplyStatus::CONNECTED_TO_USB; | 116 usb_connected.battery_state = PowerSupplyStatus::CONNECTED_TO_USB; |
| 111 EXPECT_TRUE(MaybeShowUsbChargerNotification(discharging, usb_connected)); | 117 EXPECT_TRUE(MaybeShowUsbChargerNotification(usb_connected)); |
| 112 EXPECT_EQ(1, message_center()->add_count()); | 118 EXPECT_EQ(1, message_center()->add_count()); |
| 119 EXPECT_EQ(0, message_center()->remove_count()); |
| 113 | 120 |
| 114 // Change in charge does not trigger the notification again. | 121 // Change in charge does not trigger the notification again. |
| 115 PowerSupplyStatus more_charge = DefaultPowerSupplyStatus(); | 122 PowerSupplyStatus more_charge = DefaultPowerSupplyStatus(); |
| 116 more_charge.line_power_on = true; | 123 more_charge.line_power_on = true; |
| 117 more_charge.battery_seconds_to_full = 60 * 60; | 124 more_charge.battery_seconds_to_full = 60 * 60; |
| 118 more_charge.battery_percentage = 75.0; | 125 more_charge.battery_percentage = 75.0; |
| 119 more_charge.battery_state = PowerSupplyStatus::CONNECTED_TO_USB; | 126 more_charge.battery_state = PowerSupplyStatus::CONNECTED_TO_USB; |
| 120 EXPECT_FALSE(MaybeShowUsbChargerNotification(usb_connected, more_charge)); | 127 SetUsbChargerConnected(true); |
| 128 EXPECT_FALSE(MaybeShowUsbChargerNotification(more_charge)); |
| 121 EXPECT_EQ(1, message_center()->add_count()); | 129 EXPECT_EQ(1, message_center()->add_count()); |
| 122 EXPECT_EQ(0, message_center()->remove_count()); | 130 EXPECT_EQ(0, message_center()->remove_count()); |
| 123 | 131 |
| 124 // Disconnecting a USB charger with the notification showing should close | 132 // Disconnecting a USB charger with the notification showing should close |
| 125 // the notification. | 133 // the notification. |
| 126 EXPECT_TRUE(MaybeShowUsbChargerNotification(usb_connected, discharging)); | 134 EXPECT_TRUE(MaybeShowUsbChargerNotification(discharging)); |
| 135 EXPECT_EQ(1, message_center()->add_count()); |
| 127 EXPECT_EQ(1, message_center()->remove_count()); | 136 EXPECT_EQ(1, message_center()->remove_count()); |
| 128 } | 137 } |
| 129 | 138 |
| 130 TEST_F(TrayPowerTest, UpdateNotificationState) { | 139 TEST_F(TrayPowerTest, UpdateNotificationState) { |
| 131 // No notifications when no battery present. | 140 // No notifications when no battery present. |
| 132 PowerSupplyStatus no_battery = DefaultPowerSupplyStatus(); | 141 PowerSupplyStatus no_battery = DefaultPowerSupplyStatus(); |
| 133 no_battery.battery_is_present = false; | 142 no_battery.battery_is_present = false; |
| 134 EXPECT_FALSE(UpdateNotificationState(no_battery)); | 143 EXPECT_FALSE(UpdateNotificationState(no_battery)); |
| 135 EXPECT_EQ(TrayPower::NOTIFICATION_NONE, notification_state()); | 144 EXPECT_EQ(TrayPower::NOTIFICATION_NONE, notification_state()); |
| 136 | 145 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 150 // Critical low battery notification. | 159 // Critical low battery notification. |
| 151 PowerSupplyStatus critical = DefaultPowerSupplyStatus(); | 160 PowerSupplyStatus critical = DefaultPowerSupplyStatus(); |
| 152 critical.battery_seconds_to_empty = 60; | 161 critical.battery_seconds_to_empty = 60; |
| 153 critical.battery_percentage = 2.0; | 162 critical.battery_percentage = 2.0; |
| 154 EXPECT_TRUE(UpdateNotificationState(critical)); | 163 EXPECT_TRUE(UpdateNotificationState(critical)); |
| 155 EXPECT_EQ(TrayPower::NOTIFICATION_CRITICAL, notification_state()); | 164 EXPECT_EQ(TrayPower::NOTIFICATION_CRITICAL, notification_state()); |
| 156 } | 165 } |
| 157 | 166 |
| 158 } // namespace internal | 167 } // namespace internal |
| 159 } // namespace ash | 168 } // namespace ash |
| OLD | NEW |