Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / power / peripheral_battery_observer_browsertest.cc
blob1e6e8e974ae4a4ae3874c8280adc5b7311e4ce8b
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/test/base/in_process_browser_test.h"
12 #include "chromeos/dbus/fake_dbus_thread_manager.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/test/test_browser_thread.h"
15 #include "content/public/test/test_utils.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 using ::testing::_;
20 using ::testing::InSequence;
21 using ::testing::Return;
22 using ::testing::SaveArg;
24 namespace {
26 const char kTestBatteryPath[] = "/sys/class/power_supply/hid-AA:BB:CC-battery";
27 const char kTestBatteryAddress[] = "cc:bb:aa";
28 const char kTestDeviceName[] = "test device";
30 } // namespace
32 namespace chromeos {
34 class PeripheralBatteryObserverTest : public InProcessBrowserTest {
35 public:
36 PeripheralBatteryObserverTest () {}
37 virtual ~PeripheralBatteryObserverTest () {}
39 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
40 FakeDBusThreadManager* fake_dbus_thread_manager =
41 new FakeDBusThreadManager;
42 fake_dbus_thread_manager->SetFakeClients();
43 DBusThreadManager::SetInstanceForTesting(fake_dbus_thread_manager);
44 InProcessBrowserTest::SetUpInProcessBrowserTestFixture();
47 virtual void SetUpOnMainThread() OVERRIDE {
48 observer_.reset(new PeripheralBatteryObserver());
51 virtual void CleanUpOnMainThread() OVERRIDE {
52 observer_.reset();
55 virtual void TearDownInProcessBrowserTestFixture() OVERRIDE {
56 InProcessBrowserTest::TearDownInProcessBrowserTestFixture();
59 protected:
60 scoped_ptr<PeripheralBatteryObserver> observer_;
62 private:
63 DISALLOW_COPY_AND_ASSIGN(PeripheralBatteryObserverTest);
66 IN_PROC_BROWSER_TEST_F(PeripheralBatteryObserverTest, Basic) {
67 base::SimpleTestTickClock clock;
68 observer_->set_testing_clock(&clock);
70 NotificationUIManager* notification_manager =
71 g_browser_process->notification_ui_manager();
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);
79 const PeripheralBatteryObserver::BatteryInfo& info =
80 observer_->batteries_[kTestBatteryAddress];
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->FindById(kTestBatteryAddress) != NULL);
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->FindById(kTestBatteryAddress) != NULL);
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->FindById(kTestBatteryAddress) != NULL);
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->FindById(kTestBatteryAddress) != NULL);
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->FindById(kTestBatteryAddress) != NULL);
123 IN_PROC_BROWSER_TEST_F(PeripheralBatteryObserverTest, InvalidBatteryInfo) {
124 observer_->PeripheralBatteryStatusReceived("invalid-path", kTestDeviceName,
125 10);
126 EXPECT_TRUE(observer_->batteries_.empty());
128 observer_->PeripheralBatteryStatusReceived(
129 "/sys/class/power_supply/hid-battery", kTestDeviceName, 10);
130 EXPECT_TRUE(observer_->batteries_.empty());
132 observer_->PeripheralBatteryStatusReceived(kTestBatteryPath,
133 kTestDeviceName, -2);
134 EXPECT_TRUE(observer_->batteries_.empty());
136 observer_->PeripheralBatteryStatusReceived(kTestBatteryPath,
137 kTestDeviceName, 101);
138 EXPECT_TRUE(observer_->batteries_.empty());
140 observer_->PeripheralBatteryStatusReceived(kTestBatteryPath,
141 kTestDeviceName, -1);
142 EXPECT_TRUE(observer_->batteries_.empty());
145 IN_PROC_BROWSER_TEST_F(PeripheralBatteryObserverTest, DeviceRemove) {
146 NotificationUIManager* notification_manager =
147 g_browser_process->notification_ui_manager();
149 observer_->PeripheralBatteryStatusReceived(kTestBatteryPath,
150 kTestDeviceName, 5);
151 EXPECT_EQ(observer_->batteries_.count(kTestBatteryAddress), 1u);
152 EXPECT_TRUE(notification_manager->FindById(kTestBatteryAddress) != NULL);
154 observer_->RemoveBattery(kTestBatteryAddress);
155 EXPECT_FALSE(notification_manager->FindById(kTestBatteryAddress) != NULL);
158 } // namespace chromeos