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"
19 using ::testing::InSequence
;
20 using ::testing::Return
;
21 using ::testing::SaveArg
;
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";
33 class PeripheralBatteryObserverTest
: public InProcessBrowserTest
{
35 PeripheralBatteryObserverTest() {}
36 ~PeripheralBatteryObserverTest() override
{}
38 void SetUp() override
{ chromeos::DBusThreadManager::Initialize(); }
40 void SetUpInProcessBrowserTestFixture() override
{
41 InProcessBrowserTest::SetUpInProcessBrowserTestFixture();
44 void SetUpOnMainThread() override
{
45 observer_
.reset(new PeripheralBatteryObserver());
48 void TearDownOnMainThread() override
{ observer_
.reset(); }
50 void TearDownInProcessBrowserTestFixture() override
{
51 InProcessBrowserTest::TearDownInProcessBrowserTestFixture();
55 scoped_ptr
<PeripheralBatteryObserver
> observer_
;
58 DISALLOW_COPY_AND_ASSIGN(PeripheralBatteryObserverTest
);
61 IN_PROC_BROWSER_TEST_F(PeripheralBatteryObserverTest
, Basic
) {
62 base::SimpleTestTickClock clock
;
63 observer_
->set_testing_clock(&clock
);
65 NotificationUIManager
* notification_manager
=
66 g_browser_process
->notification_ui_manager();
68 // Level 50 at time 100, no low-battery notification.
69 clock
.Advance(base::TimeDelta::FromSeconds(100));
70 observer_
->PeripheralBatteryStatusReceived(kTestBatteryPath
,
72 EXPECT_EQ(observer_
->batteries_
.count(kTestBatteryAddress
), 1u);
74 const PeripheralBatteryObserver::BatteryInfo
& info
=
75 observer_
->batteries_
[kTestBatteryAddress
];
77 EXPECT_EQ(info
.name
, kTestDeviceName
);
78 EXPECT_EQ(info
.level
, 50);
79 EXPECT_EQ(info
.last_notification_timestamp
, base::TimeTicks());
80 EXPECT_FALSE(notification_manager
->FindById(
82 NotificationUIManager::GetProfileID(
83 ProfileManager::GetPrimaryUserProfile())) != NULL
);
85 // Level 5 at time 110, low-battery notification.
86 clock
.Advance(base::TimeDelta::FromSeconds(10));
87 observer_
->PeripheralBatteryStatusReceived(kTestBatteryPath
,
89 EXPECT_EQ(info
.level
, 5);
90 EXPECT_EQ(info
.last_notification_timestamp
, clock
.NowTicks());
91 EXPECT_TRUE(notification_manager
->FindById(
93 NotificationUIManager::GetProfileID(
94 ProfileManager::GetPrimaryUserProfile())) != NULL
);
96 // Level -1 at time 115, cancel previous notification
97 clock
.Advance(base::TimeDelta::FromSeconds(5));
98 observer_
->PeripheralBatteryStatusReceived(kTestBatteryPath
,
100 EXPECT_EQ(info
.level
, 5);
101 EXPECT_EQ(info
.last_notification_timestamp
,
102 clock
.NowTicks() - base::TimeDelta::FromSeconds(5));
103 EXPECT_FALSE(notification_manager
->FindById(
105 NotificationUIManager::GetProfileID(
106 ProfileManager::GetPrimaryUserProfile())) != NULL
);
108 // Level 50 at time 120, no low-battery notification.
109 clock
.Advance(base::TimeDelta::FromSeconds(5));
110 observer_
->PeripheralBatteryStatusReceived(kTestBatteryPath
,
111 kTestDeviceName
, 50);
112 EXPECT_EQ(info
.level
, 50);
113 EXPECT_EQ(info
.last_notification_timestamp
,
114 clock
.NowTicks() - base::TimeDelta::FromSeconds(10));
115 EXPECT_FALSE(notification_manager
->FindById(
117 NotificationUIManager::GetProfileID(
118 ProfileManager::GetPrimaryUserProfile())) != NULL
);
120 // Level 5 at time 130, no low-battery notification (throttling).
121 clock
.Advance(base::TimeDelta::FromSeconds(10));
122 observer_
->PeripheralBatteryStatusReceived(kTestBatteryPath
,
124 EXPECT_EQ(info
.level
, 5);
125 EXPECT_EQ(info
.last_notification_timestamp
,
126 clock
.NowTicks() - base::TimeDelta::FromSeconds(20));
127 EXPECT_FALSE(notification_manager
->FindById(
129 NotificationUIManager::GetProfileID(
130 ProfileManager::GetPrimaryUserProfile())) != NULL
);
133 IN_PROC_BROWSER_TEST_F(PeripheralBatteryObserverTest
, InvalidBatteryInfo
) {
134 observer_
->PeripheralBatteryStatusReceived("invalid-path", kTestDeviceName
,
136 EXPECT_TRUE(observer_
->batteries_
.empty());
138 observer_
->PeripheralBatteryStatusReceived(
139 "/sys/class/power_supply/hid-battery", kTestDeviceName
, 10);
140 EXPECT_TRUE(observer_
->batteries_
.empty());
142 observer_
->PeripheralBatteryStatusReceived(kTestBatteryPath
,
143 kTestDeviceName
, -2);
144 EXPECT_TRUE(observer_
->batteries_
.empty());
146 observer_
->PeripheralBatteryStatusReceived(kTestBatteryPath
,
147 kTestDeviceName
, 101);
148 EXPECT_TRUE(observer_
->batteries_
.empty());
150 observer_
->PeripheralBatteryStatusReceived(kTestBatteryPath
,
151 kTestDeviceName
, -1);
152 EXPECT_TRUE(observer_
->batteries_
.empty());
155 IN_PROC_BROWSER_TEST_F(PeripheralBatteryObserverTest
, DeviceRemove
) {
156 NotificationUIManager
* notification_manager
=
157 g_browser_process
->notification_ui_manager();
159 observer_
->PeripheralBatteryStatusReceived(kTestBatteryPath
,
161 EXPECT_EQ(observer_
->batteries_
.count(kTestBatteryAddress
), 1u);
162 EXPECT_TRUE(notification_manager
->FindById(
164 NotificationUIManager::GetProfileID(
165 ProfileManager::GetPrimaryUserProfile())) != NULL
);
167 observer_
->RemoveBattery(kTestBatteryAddress
);
168 EXPECT_FALSE(notification_manager
->FindById(
170 NotificationUIManager::GetProfileID(
171 ProfileManager::GetPrimaryUserProfile())) != NULL
);
174 } // namespace chromeos