Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / power / peripheral_battery_observer_browsertest.cc
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/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"
17
18 using ::testing::_;
19 using ::testing::InSequence;
20 using ::testing::Return;
21 using ::testing::SaveArg;
22
23 namespace {
24
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";
28
29 }  // namespace
30
31 namespace chromeos {
32
33 class PeripheralBatteryObserverTest : public InProcessBrowserTest {
34  public:
35   PeripheralBatteryObserverTest() {}
36   virtual ~PeripheralBatteryObserverTest() {}
37
38   virtual void SetUp() override {
39     chromeos::DBusThreadManager::Initialize();
40   }
41
42   virtual void SetUpInProcessBrowserTestFixture() override {
43     InProcessBrowserTest::SetUpInProcessBrowserTestFixture();
44   }
45
46   virtual void SetUpOnMainThread() override {
47     observer_.reset(new PeripheralBatteryObserver());
48   }
49
50   virtual void TearDownOnMainThread() override {
51     observer_.reset();
52   }
53
54   virtual void TearDownInProcessBrowserTestFixture() override {
55     InProcessBrowserTest::TearDownInProcessBrowserTestFixture();
56   }
57
58  protected:
59   scoped_ptr<PeripheralBatteryObserver> observer_;
60
61  private:
62   DISALLOW_COPY_AND_ASSIGN(PeripheralBatteryObserverTest);
63 };
64
65 IN_PROC_BROWSER_TEST_F(PeripheralBatteryObserverTest, Basic) {
66   base::SimpleTestTickClock clock;
67   observer_->set_testing_clock(&clock);
68
69   NotificationUIManager* notification_manager =
70       g_browser_process->notification_ui_manager();
71
72   // Level 50 at time 100, no low-battery notification.
73   clock.Advance(base::TimeDelta::FromSeconds(100));
74   observer_->PeripheralBatteryStatusReceived(kTestBatteryPath,
75                                              kTestDeviceName, 50);
76   EXPECT_EQ(observer_->batteries_.count(kTestBatteryAddress), 1u);
77
78   const PeripheralBatteryObserver::BatteryInfo& info =
79       observer_->batteries_[kTestBatteryAddress];
80
81   EXPECT_EQ(info.name, kTestDeviceName);
82   EXPECT_EQ(info.level, 50);
83   EXPECT_EQ(info.last_notification_timestamp, base::TimeTicks());
84   EXPECT_FALSE(notification_manager->FindById(
85                    kTestBatteryAddress,
86                    NotificationUIManager::GetProfileID(
87                        ProfileManager::GetPrimaryUserProfile())) != NULL);
88
89   // Level 5 at time 110, low-battery notification.
90   clock.Advance(base::TimeDelta::FromSeconds(10));
91   observer_->PeripheralBatteryStatusReceived(kTestBatteryPath,
92                                              kTestDeviceName, 5);
93   EXPECT_EQ(info.level, 5);
94   EXPECT_EQ(info.last_notification_timestamp, clock.NowTicks());
95   EXPECT_TRUE(notification_manager->FindById(
96                   kTestBatteryAddress,
97                   NotificationUIManager::GetProfileID(
98                       ProfileManager::GetPrimaryUserProfile())) != NULL);
99
100   // Level -1 at time 115, cancel previous notification
101   clock.Advance(base::TimeDelta::FromSeconds(5));
102   observer_->PeripheralBatteryStatusReceived(kTestBatteryPath,
103                                              kTestDeviceName, -1);
104   EXPECT_EQ(info.level, 5);
105   EXPECT_EQ(info.last_notification_timestamp,
106             clock.NowTicks() - base::TimeDelta::FromSeconds(5));
107   EXPECT_FALSE(notification_manager->FindById(
108                    kTestBatteryAddress,
109                    NotificationUIManager::GetProfileID(
110                        ProfileManager::GetPrimaryUserProfile())) != NULL);
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->FindById(
120                    kTestBatteryAddress,
121                    NotificationUIManager::GetProfileID(
122                        ProfileManager::GetPrimaryUserProfile())) != NULL);
123
124   // Level 5 at time 130, no low-battery notification (throttling).
125   clock.Advance(base::TimeDelta::FromSeconds(10));
126   observer_->PeripheralBatteryStatusReceived(kTestBatteryPath,
127                                              kTestDeviceName, 5);
128   EXPECT_EQ(info.level, 5);
129   EXPECT_EQ(info.last_notification_timestamp,
130             clock.NowTicks() - base::TimeDelta::FromSeconds(20));
131   EXPECT_FALSE(notification_manager->FindById(
132                    kTestBatteryAddress,
133                    NotificationUIManager::GetProfileID(
134                        ProfileManager::GetPrimaryUserProfile())) != NULL);
135 }
136
137 IN_PROC_BROWSER_TEST_F(PeripheralBatteryObserverTest, InvalidBatteryInfo) {
138   observer_->PeripheralBatteryStatusReceived("invalid-path", kTestDeviceName,
139                                              10);
140   EXPECT_TRUE(observer_->batteries_.empty());
141
142   observer_->PeripheralBatteryStatusReceived(
143       "/sys/class/power_supply/hid-battery", kTestDeviceName, 10);
144   EXPECT_TRUE(observer_->batteries_.empty());
145
146   observer_->PeripheralBatteryStatusReceived(kTestBatteryPath,
147                                              kTestDeviceName, -2);
148   EXPECT_TRUE(observer_->batteries_.empty());
149
150   observer_->PeripheralBatteryStatusReceived(kTestBatteryPath,
151                                              kTestDeviceName, 101);
152   EXPECT_TRUE(observer_->batteries_.empty());
153
154   observer_->PeripheralBatteryStatusReceived(kTestBatteryPath,
155                                              kTestDeviceName, -1);
156   EXPECT_TRUE(observer_->batteries_.empty());
157 }
158
159 IN_PROC_BROWSER_TEST_F(PeripheralBatteryObserverTest, DeviceRemove) {
160   NotificationUIManager* notification_manager =
161       g_browser_process->notification_ui_manager();
162
163   observer_->PeripheralBatteryStatusReceived(kTestBatteryPath,
164                                              kTestDeviceName, 5);
165   EXPECT_EQ(observer_->batteries_.count(kTestBatteryAddress), 1u);
166   EXPECT_TRUE(notification_manager->FindById(
167                   kTestBatteryAddress,
168                   NotificationUIManager::GetProfileID(
169                       ProfileManager::GetPrimaryUserProfile())) != NULL);
170
171   observer_->RemoveBattery(kTestBatteryAddress);
172   EXPECT_FALSE(notification_manager->FindById(
173                    kTestBatteryAddress,
174                    NotificationUIManager::GetProfileID(
175                        ProfileManager::GetPrimaryUserProfile())) != NULL);
176 }
177
178 }  // namespace chromeos