| 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 "chrome/browser/chromeos/power/peripheral_battery_observer.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/message_loop.h" |
| 9 #include "chrome/browser/browser_process.h" |
| 10 #include "chrome/browser/chromeos/cros/cros_in_process_browser_test.h" |
| 11 #include "chrome/browser/notifications/notification_ui_manager.h" |
| 12 #include "chromeos/dbus/mock_dbus_thread_manager.h" |
| 13 #include "chromeos/dbus/mock_update_engine_client.h" |
| 14 #include "content/public/browser/browser_thread.h" |
| 15 #include "content/public/test/test_browser_thread.h" |
| 16 #include "content/public/test/test_utils.h" |
| 17 #include "testing/gmock/include/gmock/gmock.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 |
| 20 using ::testing::_; |
| 21 using ::testing::InSequence; |
| 22 using ::testing::Return; |
| 23 using ::testing::SaveArg; |
| 24 |
| 25 namespace { |
| 26 |
| 27 const char kTestBatteryPath[] = "/sys/class/power_supply/hid-AA:BB:CC-battery"; |
| 28 const char kTestBatteryAddress[] = "cc:bb:aa"; |
| 29 const char kTestDeviceName[] = "test device"; |
| 30 |
| 31 } // namespace |
| 32 |
| 33 namespace chromeos { |
| 34 |
| 35 class PeripheralBatteryObserverTest : public CrosInProcessBrowserTest { |
| 36 public: |
| 37 PeripheralBatteryObserverTest () {} |
| 38 virtual ~PeripheralBatteryObserverTest () {} |
| 39 |
| 40 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { |
| 41 MockDBusThreadManager* mock_dbus_thread_manager = new MockDBusThreadManager; |
| 42 |
| 43 EXPECT_CALL(*mock_dbus_thread_manager, GetSystemBus()) |
| 44 .WillRepeatedly(Return(reinterpret_cast<dbus::Bus*>(NULL))); |
| 45 EXPECT_CALL(*mock_dbus_thread_manager->mock_update_engine_client(), |
| 46 GetLastStatus()) |
| 47 .Times(1) |
| 48 .WillOnce(Return(MockUpdateEngineClient::Status())); |
| 49 |
| 50 DBusThreadManager::InitializeForTesting(mock_dbus_thread_manager); |
| 51 CrosInProcessBrowserTest::SetUpInProcessBrowserTestFixture(); |
| 52 } |
| 53 |
| 54 virtual void SetUpOnMainThread() OVERRIDE { |
| 55 observer_.reset(new PeripheralBatteryObserver()); |
| 56 } |
| 57 |
| 58 virtual void CleanUpOnMainThread() OVERRIDE { |
| 59 observer_.reset(); |
| 60 } |
| 61 |
| 62 virtual void TearDownInProcessBrowserTestFixture() OVERRIDE { |
| 63 CrosInProcessBrowserTest::TearDownInProcessBrowserTestFixture(); |
| 64 DBusThreadManager::Shutdown(); |
| 65 } |
| 66 |
| 67 protected: |
| 68 scoped_ptr<PeripheralBatteryObserver> observer_; |
| 69 |
| 70 private: |
| 71 DISALLOW_COPY_AND_ASSIGN(PeripheralBatteryObserverTest); |
| 72 }; |
| 73 |
| 74 IN_PROC_BROWSER_TEST_F(PeripheralBatteryObserverTest, Basic) { |
| 75 base::SimpleTestTickClock clock; |
| 76 observer_->set_testing_clock(&clock); |
| 77 |
| 78 NotificationUIManager* notification_manager = |
| 79 g_browser_process->notification_ui_manager(); |
| 80 |
| 81 // Level 50 at time 100, no low-battery notification. |
| 82 clock.Advance(base::TimeDelta::FromSeconds(100)); |
| 83 observer_->PeripheralBatteryStatusReceived(kTestBatteryPath, |
| 84 kTestDeviceName, 50); |
| 85 EXPECT_EQ(observer_->batteries_.count(kTestBatteryAddress), 1u); |
| 86 |
| 87 const PeripheralBatteryObserver::BatteryInfo& info = |
| 88 observer_->batteries_[kTestBatteryAddress]; |
| 89 |
| 90 EXPECT_EQ(info.name, kTestDeviceName); |
| 91 EXPECT_EQ(info.level, 50); |
| 92 EXPECT_EQ(info.last_notification_timestamp, base::TimeTicks()); |
| 93 EXPECT_FALSE(notification_manager->DoesIdExist(kTestBatteryAddress)); |
| 94 |
| 95 // Level 5 at time 110, low-battery notification. |
| 96 clock.Advance(base::TimeDelta::FromSeconds(10)); |
| 97 observer_->PeripheralBatteryStatusReceived(kTestBatteryPath, |
| 98 kTestDeviceName, 5); |
| 99 EXPECT_EQ(info.level, 5); |
| 100 EXPECT_EQ(info.last_notification_timestamp, clock.NowTicks()); |
| 101 EXPECT_TRUE(notification_manager->DoesIdExist(kTestBatteryAddress)); |
| 102 |
| 103 // Level -1 at time 115, cancel previous notification |
| 104 clock.Advance(base::TimeDelta::FromSeconds(5)); |
| 105 observer_->PeripheralBatteryStatusReceived(kTestBatteryPath, |
| 106 kTestDeviceName, -1); |
| 107 EXPECT_EQ(info.level, 5); |
| 108 EXPECT_EQ(info.last_notification_timestamp, |
| 109 clock.NowTicks() - base::TimeDelta::FromSeconds(5)); |
| 110 EXPECT_FALSE(notification_manager->DoesIdExist(kTestBatteryAddress)); |
| 111 |
| 112 // Level 50 at time 120, no low-battery notification. |
| 113 clock.Advance(base::TimeDelta::FromSeconds(5)); |
| 114 observer_->PeripheralBatteryStatusReceived(kTestBatteryPath, |
| 115 kTestDeviceName, 50); |
| 116 EXPECT_EQ(info.level, 50); |
| 117 EXPECT_EQ(info.last_notification_timestamp, |
| 118 clock.NowTicks() - base::TimeDelta::FromSeconds(10)); |
| 119 EXPECT_FALSE(notification_manager->DoesIdExist(kTestBatteryAddress)); |
| 120 |
| 121 // Level 5 at time 130, no low-battery notification (throttling). |
| 122 clock.Advance(base::TimeDelta::FromSeconds(10)); |
| 123 observer_->PeripheralBatteryStatusReceived(kTestBatteryPath, |
| 124 kTestDeviceName, 5); |
| 125 EXPECT_EQ(info.level, 5); |
| 126 EXPECT_EQ(info.last_notification_timestamp, |
| 127 clock.NowTicks() - base::TimeDelta::FromSeconds(20)); |
| 128 EXPECT_FALSE(notification_manager->DoesIdExist(kTestBatteryAddress)); |
| 129 } |
| 130 |
| 131 IN_PROC_BROWSER_TEST_F(PeripheralBatteryObserverTest, InvalidBatteryInfo) { |
| 132 observer_->PeripheralBatteryStatusReceived("invalid-path", kTestDeviceName, |
| 133 10); |
| 134 EXPECT_TRUE(observer_->batteries_.empty()); |
| 135 |
| 136 observer_->PeripheralBatteryStatusReceived( |
| 137 "/sys/class/power_supply/hid-battery", kTestDeviceName, 10); |
| 138 EXPECT_TRUE(observer_->batteries_.empty()); |
| 139 |
| 140 observer_->PeripheralBatteryStatusReceived(kTestBatteryPath, |
| 141 kTestDeviceName, -2); |
| 142 EXPECT_TRUE(observer_->batteries_.empty()); |
| 143 |
| 144 observer_->PeripheralBatteryStatusReceived(kTestBatteryPath, |
| 145 kTestDeviceName, 101); |
| 146 EXPECT_TRUE(observer_->batteries_.empty()); |
| 147 |
| 148 observer_->PeripheralBatteryStatusReceived(kTestBatteryPath, |
| 149 kTestDeviceName, -1); |
| 150 EXPECT_TRUE(observer_->batteries_.empty()); |
| 151 } |
| 152 |
| 153 IN_PROC_BROWSER_TEST_F(PeripheralBatteryObserverTest, DeviceRemove) { |
| 154 NotificationUIManager* notification_manager = |
| 155 g_browser_process->notification_ui_manager(); |
| 156 |
| 157 observer_->PeripheralBatteryStatusReceived(kTestBatteryPath, |
| 158 kTestDeviceName, 5); |
| 159 EXPECT_EQ(observer_->batteries_.count(kTestBatteryAddress), 1u); |
| 160 EXPECT_TRUE(notification_manager->DoesIdExist(kTestBatteryAddress)); |
| 161 |
| 162 observer_->RemoveBattery(kTestBatteryAddress); |
| 163 EXPECT_FALSE(notification_manager->DoesIdExist(kTestBatteryAddress)); |
| 164 } |
| 165 |
| 166 } // namespace chromeos |
| OLD | NEW |