Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / chromeos / power / peripheral_battery_observer_browsertest.cc
blob8c08e0d1f417ad39c7bb383b4f1dcd04040e9a97
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.
5 #include "chrome/browser/chromeos/power/peripheral_battery_observer.h"
7 #include "base/command_line.h"
8 #include "base/message_loop/message_loop.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/notifications/notification_ui_manager.h"
11 #include "chrome/browser/profiles/profile_manager.h"
12 #include "chrome/test/base/in_process_browser_test.h"
13 #include "chromeos/dbus/dbus_thread_manager.h"
14 #include "content/public/test/test_utils.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 using ::testing::_;
19 using ::testing::InSequence;
20 using ::testing::Return;
21 using ::testing::SaveArg;
23 namespace {
25 const char kTestBatteryPath[] = "/sys/class/power_supply/hid-AA:BB:CC-battery";
26 const char kTestBatteryAddress[] = "cc:bb:aa";
27 const char kTestDeviceName[] = "test device";
29 } // namespace
31 namespace chromeos {
33 class PeripheralBatteryObserverTest : public InProcessBrowserTest {
34 public:
35 PeripheralBatteryObserverTest() {}
36 ~PeripheralBatteryObserverTest() override {}
38 void SetUp() override {
39 InProcessBrowserTest::SetUp();
40 chromeos::DBusThreadManager::Initialize();
43 void SetUpInProcessBrowserTestFixture() override {
44 InProcessBrowserTest::SetUpInProcessBrowserTestFixture();
47 void SetUpOnMainThread() override {
48 observer_.reset(new PeripheralBatteryObserver());
51 void TearDownOnMainThread() override { observer_.reset(); }
53 void TearDownInProcessBrowserTestFixture() override {
54 InProcessBrowserTest::TearDownInProcessBrowserTestFixture();
57 protected:
58 scoped_ptr<PeripheralBatteryObserver> observer_;
60 private:
61 DISALLOW_COPY_AND_ASSIGN(PeripheralBatteryObserverTest);
64 IN_PROC_BROWSER_TEST_F(PeripheralBatteryObserverTest, Basic) {
65 base::SimpleTestTickClock clock;
66 observer_->set_testing_clock(&clock);
68 NotificationUIManager* notification_manager =
69 g_browser_process->notification_ui_manager();
71 // Level 50 at time 100, no low-battery notification.
72 clock.Advance(base::TimeDelta::FromSeconds(100));
73 observer_->PeripheralBatteryStatusReceived(kTestBatteryPath,
74 kTestDeviceName, 50);
75 EXPECT_EQ(observer_->batteries_.count(kTestBatteryAddress), 1u);
77 const PeripheralBatteryObserver::BatteryInfo& info =
78 observer_->batteries_[kTestBatteryAddress];
80 EXPECT_EQ(info.name, kTestDeviceName);
81 EXPECT_EQ(info.level, 50);
82 EXPECT_EQ(info.last_notification_timestamp, base::TimeTicks());
83 EXPECT_FALSE(notification_manager->FindById(
84 kTestBatteryAddress,
85 NotificationUIManager::GetProfileID(
86 ProfileManager::GetPrimaryUserProfile())) != NULL);
88 // Level 5 at time 110, low-battery notification.
89 clock.Advance(base::TimeDelta::FromSeconds(10));
90 observer_->PeripheralBatteryStatusReceived(kTestBatteryPath,
91 kTestDeviceName, 5);
92 EXPECT_EQ(info.level, 5);
93 EXPECT_EQ(info.last_notification_timestamp, clock.NowTicks());
94 EXPECT_TRUE(notification_manager->FindById(
95 kTestBatteryAddress,
96 NotificationUIManager::GetProfileID(
97 ProfileManager::GetPrimaryUserProfile())) != NULL);
99 // Level -1 at time 115, cancel previous notification
100 clock.Advance(base::TimeDelta::FromSeconds(5));
101 observer_->PeripheralBatteryStatusReceived(kTestBatteryPath,
102 kTestDeviceName, -1);
103 EXPECT_EQ(info.level, 5);
104 EXPECT_EQ(info.last_notification_timestamp,
105 clock.NowTicks() - base::TimeDelta::FromSeconds(5));
106 EXPECT_FALSE(notification_manager->FindById(
107 kTestBatteryAddress,
108 NotificationUIManager::GetProfileID(
109 ProfileManager::GetPrimaryUserProfile())) != NULL);
111 // Level 50 at time 120, no low-battery notification.
112 clock.Advance(base::TimeDelta::FromSeconds(5));
113 observer_->PeripheralBatteryStatusReceived(kTestBatteryPath,
114 kTestDeviceName, 50);
115 EXPECT_EQ(info.level, 50);
116 EXPECT_EQ(info.last_notification_timestamp,
117 clock.NowTicks() - base::TimeDelta::FromSeconds(10));
118 EXPECT_FALSE(notification_manager->FindById(
119 kTestBatteryAddress,
120 NotificationUIManager::GetProfileID(
121 ProfileManager::GetPrimaryUserProfile())) != NULL);
123 // Level 5 at time 130, no low-battery notification (throttling).
124 clock.Advance(base::TimeDelta::FromSeconds(10));
125 observer_->PeripheralBatteryStatusReceived(kTestBatteryPath,
126 kTestDeviceName, 5);
127 EXPECT_EQ(info.level, 5);
128 EXPECT_EQ(info.last_notification_timestamp,
129 clock.NowTicks() - base::TimeDelta::FromSeconds(20));
130 EXPECT_FALSE(notification_manager->FindById(
131 kTestBatteryAddress,
132 NotificationUIManager::GetProfileID(
133 ProfileManager::GetPrimaryUserProfile())) != NULL);
136 IN_PROC_BROWSER_TEST_F(PeripheralBatteryObserverTest, InvalidBatteryInfo) {
137 observer_->PeripheralBatteryStatusReceived("invalid-path", kTestDeviceName,
138 10);
139 EXPECT_TRUE(observer_->batteries_.empty());
141 observer_->PeripheralBatteryStatusReceived(
142 "/sys/class/power_supply/hid-battery", kTestDeviceName, 10);
143 EXPECT_TRUE(observer_->batteries_.empty());
145 observer_->PeripheralBatteryStatusReceived(kTestBatteryPath,
146 kTestDeviceName, -2);
147 EXPECT_TRUE(observer_->batteries_.empty());
149 observer_->PeripheralBatteryStatusReceived(kTestBatteryPath,
150 kTestDeviceName, 101);
151 EXPECT_TRUE(observer_->batteries_.empty());
153 observer_->PeripheralBatteryStatusReceived(kTestBatteryPath,
154 kTestDeviceName, -1);
155 EXPECT_TRUE(observer_->batteries_.empty());
158 IN_PROC_BROWSER_TEST_F(PeripheralBatteryObserverTest, DeviceRemove) {
159 NotificationUIManager* notification_manager =
160 g_browser_process->notification_ui_manager();
162 observer_->PeripheralBatteryStatusReceived(kTestBatteryPath,
163 kTestDeviceName, 5);
164 EXPECT_EQ(observer_->batteries_.count(kTestBatteryAddress), 1u);
165 EXPECT_TRUE(notification_manager->FindById(
166 kTestBatteryAddress,
167 NotificationUIManager::GetProfileID(
168 ProfileManager::GetPrimaryUserProfile())) != NULL);
170 observer_->RemoveBattery(kTestBatteryAddress);
171 EXPECT_FALSE(notification_manager->FindById(
172 kTestBatteryAddress,
173 NotificationUIManager::GetProfileID(
174 ProfileManager::GetPrimaryUserProfile())) != NULL);
177 } // namespace chromeos