| 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 DBusThreadManager::InitializeForTesting(mock_dbus_thread_manager); |
| 43 CrosInProcessBrowserTest::SetUpInProcessBrowserTestFixture(); |
| 44 } |
| 45 |
| 46 virtual void SetUpOnMainThread() OVERRIDE { |
| 47 observer_.reset(new PeripheralBatteryObserver()); |
| 48 } |
| 49 |
| 50 virtual void CleanUpOnMainThread() OVERRIDE { |
| 51 observer_.reset(); |
| 52 } |
| 53 |
| 54 virtual void TearDownInProcessBrowserTestFixture() OVERRIDE { |
| 55 CrosInProcessBrowserTest::TearDownInProcessBrowserTestFixture(); |
| 56 DBusThreadManager::Shutdown(); |
| 57 } |
| 58 |
| 59 protected: |
| 60 scoped_ptr<PeripheralBatteryObserver> observer_; |
| 61 |
| 62 private: |
| 63 DISALLOW_COPY_AND_ASSIGN(PeripheralBatteryObserverTest); |
| 64 }; |
| 65 |
| 66 IN_PROC_BROWSER_TEST_F(PeripheralBatteryObserverTest, Basic) { |
| 67 base::SimpleTestTickClock clock; |
| 68 observer_->set_testing_clock(&clock); |
| 69 |
| 70 NotificationUIManager* notification_manager = |
| 71 g_browser_process->notification_ui_manager(); |
| 72 |
| 73 // Level 50 at time 100, no low-battery notification. |
| 74 clock.Advance(base::TimeDelta::FromSeconds(100)); |
| 75 observer_->PeripheralBatteryStatusReceived(kTestBatteryPath, |
| 76 kTestDeviceName, 50); |
| 77 EXPECT_EQ(observer_->batteries_.count(kTestBatteryAddress), 1u); |
| 78 |
| 79 const PeripheralBatteryObserver::BatteryInfo& info = |
| 80 observer_->batteries_[kTestBatteryAddress]; |
| 81 |
| 82 EXPECT_EQ(info.name, kTestDeviceName); |
| 83 EXPECT_EQ(info.level, 50); |
| 84 EXPECT_EQ(info.last_notification_timestamp, base::TimeTicks()); |
| 85 EXPECT_FALSE(notification_manager->DoesIdExist(kTestBatteryAddress)); |
| 86 |
| 87 // Level 5 at time 110, low-battery notification. |
| 88 clock.Advance(base::TimeDelta::FromSeconds(10)); |
| 89 observer_->PeripheralBatteryStatusReceived(kTestBatteryPath, |
| 90 kTestDeviceName, 5); |
| 91 EXPECT_EQ(info.level, 5); |
| 92 EXPECT_EQ(info.last_notification_timestamp, clock.NowTicks()); |
| 93 EXPECT_TRUE(notification_manager->DoesIdExist(kTestBatteryAddress)); |
| 94 |
| 95 // Level -1 at time 115, cancel previous notification |
| 96 clock.Advance(base::TimeDelta::FromSeconds(5)); |
| 97 observer_->PeripheralBatteryStatusReceived(kTestBatteryPath, |
| 98 kTestDeviceName, -1); |
| 99 EXPECT_EQ(info.level, 5); |
| 100 EXPECT_EQ(info.last_notification_timestamp, |
| 101 clock.NowTicks() - base::TimeDelta::FromSeconds(5)); |
| 102 EXPECT_FALSE(notification_manager->DoesIdExist(kTestBatteryAddress)); |
| 103 |
| 104 // Level 50 at time 120, no low-battery notification. |
| 105 clock.Advance(base::TimeDelta::FromSeconds(5)); |
| 106 observer_->PeripheralBatteryStatusReceived(kTestBatteryPath, |
| 107 kTestDeviceName, 50); |
| 108 EXPECT_EQ(info.level, 50); |
| 109 EXPECT_EQ(info.last_notification_timestamp, |
| 110 clock.NowTicks() - base::TimeDelta::FromSeconds(10)); |
| 111 EXPECT_FALSE(notification_manager->DoesIdExist(kTestBatteryAddress)); |
| 112 |
| 113 // Level 5 at time 130, no low-battery notification (throttling). |
| 114 clock.Advance(base::TimeDelta::FromSeconds(10)); |
| 115 observer_->PeripheralBatteryStatusReceived(kTestBatteryPath, |
| 116 kTestDeviceName, 5); |
| 117 EXPECT_EQ(info.level, 5); |
| 118 EXPECT_EQ(info.last_notification_timestamp, |
| 119 clock.NowTicks() - base::TimeDelta::FromSeconds(20)); |
| 120 EXPECT_FALSE(notification_manager->DoesIdExist(kTestBatteryAddress)); |
| 121 } |
| 122 |
| 123 IN_PROC_BROWSER_TEST_F(PeripheralBatteryObserverTest, InvalidBatteryInfo) { |
| 124 observer_->PeripheralBatteryStatusReceived("invalid-path", kTestDeviceName, |
| 125 10); |
| 126 EXPECT_TRUE(observer_->batteries_.empty()); |
| 127 |
| 128 observer_->PeripheralBatteryStatusReceived( |
| 129 "/sys/class/power_supply/hid-battery", kTestDeviceName, 10); |
| 130 EXPECT_TRUE(observer_->batteries_.empty()); |
| 131 |
| 132 observer_->PeripheralBatteryStatusReceived(kTestBatteryPath, |
| 133 kTestDeviceName, -2); |
| 134 EXPECT_TRUE(observer_->batteries_.empty()); |
| 135 |
| 136 observer_->PeripheralBatteryStatusReceived(kTestBatteryPath, |
| 137 kTestDeviceName, 101); |
| 138 EXPECT_TRUE(observer_->batteries_.empty()); |
| 139 |
| 140 observer_->PeripheralBatteryStatusReceived(kTestBatteryPath, |
| 141 kTestDeviceName, -1); |
| 142 EXPECT_TRUE(observer_->batteries_.empty()); |
| 143 } |
| 144 |
| 145 IN_PROC_BROWSER_TEST_F(PeripheralBatteryObserverTest, DeviceRemove) { |
| 146 NotificationUIManager* notification_manager = |
| 147 g_browser_process->notification_ui_manager(); |
| 148 |
| 149 observer_->PeripheralBatteryStatusReceived(kTestBatteryPath, |
| 150 kTestDeviceName, 5); |
| 151 EXPECT_EQ(observer_->batteries_.count(kTestBatteryAddress), 1u); |
| 152 EXPECT_TRUE(notification_manager->DoesIdExist(kTestBatteryAddress)); |
| 153 |
| 154 observer_->RemoveBattery(kTestBatteryAddress); |
| 155 EXPECT_FALSE(notification_manager->DoesIdExist(kTestBatteryAddress)); |
| 156 } |
| 157 |
| 158 } // namespace chromeos |
| OLD | NEW |