Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "chromeos/dbus/fake_power_manager_client.h" | |
| 6 | |
| 7 #include "testing/gtest/include/gtest/gtest.h" | |
| 8 | |
| 9 namespace chromeos { | |
| 10 namespace { | |
| 11 class TestObserver : public PowerManagerClient::Observer { | |
| 12 public: | |
| 13 TestObserver() : num_power_changed_(0) {} | |
| 14 ~TestObserver() override {} | |
| 15 | |
| 16 const power_manager::PowerSupplyProperties& props() const { return props_; } | |
| 17 int num_power_changed() const { return num_power_changed_; } | |
|
oshima
2015/07/10 22:39:41
new line
mozartalouis
2015/07/14 20:01:29
Done.
| |
| 18 void ClearProps() { props_.Clear(); } | |
| 19 | |
| 20 void PowerChanged( | |
| 21 const power_manager::PowerSupplyProperties& proto) override { | |
| 22 props_ = proto; | |
| 23 ++num_power_changed_; | |
| 24 } | |
| 25 | |
| 26 private: | |
| 27 int num_power_changed_; | |
| 28 power_manager::PowerSupplyProperties props_; | |
| 29 | |
| 30 DISALLOW_COPY_AND_ASSIGN(TestObserver); | |
| 31 }; | |
| 32 | |
| 33 void SetTestProperties(power_manager::PowerSupplyProperties& props) { | |
|
oshima
2015/07/10 22:39:41
out parameter should be pointer per style guide.
mozartalouis
2015/07/14 20:01:29
Done.
| |
| 34 props.set_battery_percent(85); | |
| 35 props.set_is_calculating_battery_time(true); | |
| 36 props.set_battery_state( | |
| 37 power_manager::PowerSupplyProperties_BatteryState_DISCHARGING); | |
| 38 props.set_external_power( | |
| 39 power_manager::PowerSupplyProperties_ExternalPower_USB); | |
| 40 } | |
| 41 | |
| 42 } // namespace | |
| 43 | |
| 44 TEST(FakePowerManagerClientTest, UpdatePowerPropertiesTest) { | |
| 45 // Checking to verify when UpdatePowerProperties is called, | |
| 46 // |props_| values are updated. | |
| 47 FakePowerManagerClient client; | |
| 48 power_manager::PowerSupplyProperties props; | |
| 49 | |
| 50 SetTestProperties(props); | |
| 51 client.UpdatePowerProperties(props); | |
| 52 | |
| 53 EXPECT_EQ(85, client.props().battery_percent()); | |
| 54 EXPECT_TRUE(client.props().is_calculating_battery_time()); | |
| 55 EXPECT_EQ(power_manager::PowerSupplyProperties_BatteryState_DISCHARGING, | |
| 56 client.props().battery_state()); | |
| 57 EXPECT_EQ(power_manager::PowerSupplyProperties_ExternalPower_USB, | |
| 58 client.props().external_power()); | |
| 59 | |
| 60 // Test if when the values are changed, the correct data is set in the | |
| 61 // FakePowerManagerClient. | |
| 62 props = client.props(); | |
| 63 props.set_battery_percent(70); | |
| 64 client.UpdatePowerProperties(props); | |
| 65 | |
| 66 EXPECT_EQ(70, client.props().battery_percent()); | |
| 67 EXPECT_TRUE(client.props().is_calculating_battery_time()); | |
| 68 EXPECT_EQ(power_manager::PowerSupplyProperties_BatteryState_DISCHARGING, | |
| 69 client.props().battery_state()); | |
| 70 EXPECT_EQ(power_manager::PowerSupplyProperties_ExternalPower_USB, | |
| 71 client.props().external_power()); | |
| 72 }; | |
| 73 | |
| 74 TEST(FakePowerManagerClientTest, NotifyObserversTest) { | |
| 75 FakePowerManagerClient client; | |
| 76 power_manager::PowerSupplyProperties props; | |
|
oshima
2015/07/10 22:39:41
move this to line 82 just before it's first used.
mozartalouis
2015/07/14 20:01:29
Done.
| |
| 77 TestObserver test_observer; | |
| 78 | |
| 79 // Test adding observer. | |
| 80 client.AddObserver(&test_observer); | |
| 81 EXPECT_TRUE(client.HasObserver(&test_observer)); | |
| 82 | |
| 83 // Test if NotifyObservers sends the correct values to |observer|. | |
| 84 // Check number of times NotifyObservers is called. | |
| 85 SetTestProperties(props); | |
| 86 client.UpdatePowerProperties(props); | |
| 87 | |
| 88 EXPECT_EQ(85, test_observer.props().battery_percent()); | |
| 89 EXPECT_TRUE(test_observer.props().is_calculating_battery_time()); | |
| 90 EXPECT_EQ(power_manager::PowerSupplyProperties_BatteryState_DISCHARGING, | |
| 91 test_observer.props().battery_state()); | |
| 92 EXPECT_EQ(power_manager::PowerSupplyProperties_ExternalPower_USB, | |
| 93 test_observer.props().external_power()); | |
| 94 EXPECT_EQ(1, test_observer.num_power_changed()); | |
| 95 | |
| 96 // Test if RequestStatusUpdate will propergate the data to the observer. | |
| 97 // Check number of times NotifyObservers is called. | |
| 98 test_observer.ClearProps(); | |
| 99 client.RequestStatusUpdate(); | |
| 100 | |
| 101 EXPECT_EQ(85, test_observer.props().battery_percent()); | |
| 102 EXPECT_TRUE(test_observer.props().is_calculating_battery_time()); | |
| 103 EXPECT_EQ(power_manager::PowerSupplyProperties_BatteryState_DISCHARGING, | |
| 104 test_observer.props().battery_state()); | |
| 105 EXPECT_EQ(power_manager::PowerSupplyProperties_ExternalPower_USB, | |
| 106 test_observer.props().external_power()); | |
| 107 EXPECT_EQ(2, test_observer.num_power_changed()); | |
| 108 | |
| 109 // Check when values are changed, the correct values are propergated to the | |
| 110 // Obserever | |
| 111 props = client.props(); | |
| 112 props.set_battery_percent(90); | |
| 113 props.set_external_power( | |
| 114 power_manager::PowerSupplyProperties_ExternalPower_AC); | |
| 115 client.UpdatePowerProperties(props); | |
| 116 | |
| 117 EXPECT_EQ(90, test_observer.props().battery_percent()); | |
| 118 EXPECT_TRUE(test_observer.props().is_calculating_battery_time()); | |
| 119 EXPECT_EQ(power_manager::PowerSupplyProperties_BatteryState_DISCHARGING, | |
| 120 test_observer.props().battery_state()); | |
| 121 EXPECT_EQ(power_manager::PowerSupplyProperties_ExternalPower_AC, | |
| 122 test_observer.props().external_power()); | |
| 123 EXPECT_EQ(3, test_observer.num_power_changed()); | |
| 124 | |
| 125 // Test removing observer. | |
| 126 client.RemoveObserver(&test_observer); | |
| 127 EXPECT_FALSE(client.HasObserver(&test_observer)); | |
| 128 }; | |
| 129 | |
| 130 } // namespace chromeos | |
| OLD | NEW |