Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / chromeos / dbus / fake_power_manager_client_unittest.cc
blob021459e21abebe8748537531b4d3a608386323de
1 // Copyright 2015 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 "chromeos/dbus/fake_power_manager_client.h"
7 #include "base/basictypes.h"
8 #include "base/macros.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace chromeos {
14 namespace {
16 const double kInitialBatteryPercent = 85;
17 const double kUpdatedBatteryPercent = 70;
18 const power_manager::PowerSupplyProperties_BatteryState kInitialBatteryState =
19 power_manager::PowerSupplyProperties_BatteryState_DISCHARGING;
20 const power_manager::PowerSupplyProperties_ExternalPower kInitialExternalPower =
21 power_manager::PowerSupplyProperties_ExternalPower_USB;
23 class TestObserver : public PowerManagerClient::Observer {
24 public:
25 TestObserver() : num_power_changed_(0) {}
26 ~TestObserver() override {}
28 const power_manager::PowerSupplyProperties& props() const { return props_; }
29 int num_power_changed() const { return num_power_changed_; }
31 void ClearProps() { props_.Clear(); }
33 void PowerChanged(
34 const power_manager::PowerSupplyProperties& proto) override {
35 props_ = proto;
36 ++num_power_changed_;
39 private:
40 int num_power_changed_;
41 power_manager::PowerSupplyProperties props_;
43 DISALLOW_COPY_AND_ASSIGN(TestObserver);
46 void SetTestProperties(power_manager::PowerSupplyProperties* props) {
47 props->set_battery_percent(kInitialBatteryPercent);
48 props->set_is_calculating_battery_time(true);
49 props->set_battery_state(kInitialBatteryState);
50 props->set_external_power(kInitialExternalPower);
53 } // namespace
55 TEST(FakePowerManagerClientTest, UpdatePowerPropertiesTest) {
56 // Checking to verify when UpdatePowerProperties is called,
57 // |props_| values are updated.
58 FakePowerManagerClient client;
59 power_manager::PowerSupplyProperties props;
61 SetTestProperties(&props);
62 client.UpdatePowerProperties(props);
64 EXPECT_EQ(kInitialBatteryPercent, client.props().battery_percent());
65 EXPECT_TRUE(client.props().is_calculating_battery_time());
66 EXPECT_EQ(kInitialBatteryState, client.props().battery_state());
67 EXPECT_EQ(kInitialExternalPower, client.props().external_power());
69 // Test if when the values are changed, the correct data is set in the
70 // FakePowerManagerClient.
71 props = client.props();
72 props.set_battery_percent(kUpdatedBatteryPercent);
73 client.UpdatePowerProperties(props);
75 EXPECT_EQ(kUpdatedBatteryPercent, client.props().battery_percent());
76 EXPECT_TRUE(client.props().is_calculating_battery_time());
77 EXPECT_EQ(kInitialBatteryState, client.props().battery_state());
78 EXPECT_EQ(kInitialExternalPower, client.props().external_power());
81 TEST(FakePowerManagerClientTest, NotifyObserversTest) {
82 FakePowerManagerClient client;
83 TestObserver test_observer;
85 // Test adding observer.
86 client.AddObserver(&test_observer);
87 EXPECT_TRUE(client.HasObserver(&test_observer));
89 // Test if NotifyObservers() sends the correct values to |observer|.
90 // Check number of times NotifyObservers() is called.
91 power_manager::PowerSupplyProperties props;
92 SetTestProperties(&props);
93 client.UpdatePowerProperties(props);
95 EXPECT_EQ(kInitialBatteryPercent, test_observer.props().battery_percent());
96 EXPECT_TRUE(test_observer.props().is_calculating_battery_time());
97 EXPECT_EQ(kInitialBatteryState, test_observer.props().battery_state());
98 EXPECT_EQ(kInitialExternalPower, test_observer.props().external_power());
99 EXPECT_EQ(1, test_observer.num_power_changed());
101 // Test if RequestStatusUpdate() will propagate the data to the observer.
102 // Check number of times NotifyObservers is called.
103 // RequestStatusUpdate posts to the current message loop. This is
104 // necessary because we want to make sure that NotifyObservers() is
105 // called as a result of RequestStatusUpdate().
106 base::MessageLoopForUI message_loop;
107 test_observer.ClearProps();
108 client.RequestStatusUpdate();
109 base::RunLoop().RunUntilIdle();
111 EXPECT_EQ(kInitialBatteryPercent, test_observer.props().battery_percent());
112 EXPECT_TRUE(test_observer.props().is_calculating_battery_time());
113 EXPECT_EQ(kInitialBatteryState, test_observer.props().battery_state());
114 EXPECT_EQ(kInitialExternalPower, test_observer.props().external_power());
115 EXPECT_EQ(2, test_observer.num_power_changed());
117 // Check when values are changed, the correct values are propagated to the
118 // observer
119 props = client.props();
120 props.set_battery_percent(kUpdatedBatteryPercent);
121 props.set_external_power(
122 power_manager::PowerSupplyProperties_ExternalPower_AC);
123 client.UpdatePowerProperties(props);
125 EXPECT_EQ(kUpdatedBatteryPercent, test_observer.props().battery_percent());
126 EXPECT_TRUE(test_observer.props().is_calculating_battery_time());
127 EXPECT_EQ(kInitialBatteryState, test_observer.props().battery_state());
128 EXPECT_EQ(power_manager::PowerSupplyProperties_ExternalPower_AC,
129 test_observer.props().external_power());
130 EXPECT_EQ(3, test_observer.num_power_changed());
132 // Test removing observer.
133 client.RemoveObserver(&test_observer);
134 EXPECT_FALSE(client.HasObserver(&test_observer));
137 } // namespace chromeos