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 "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" | |
| 12 | |
| 13 namespace chromeos { | |
| 14 namespace { | |
| 15 | |
| 16 const double kDefaultBatteryPercent = 85; | |
| 17 const double kDefaultBatteryPercentChanged = 70; | |
|
oshima
2015/07/15 21:46:23
I think following name would be better
kInitialBa
Daniel Erat
2015/07/15 22:02:39
agreed
mozartalouis
2015/07/15 22:10:30
Done.
| |
| 18 | |
| 19 class TestObserver : public PowerManagerClient::Observer { | |
| 20 public: | |
| 21 TestObserver() : num_power_changed_(0) {} | |
| 22 ~TestObserver() override {} | |
| 23 | |
| 24 const power_manager::PowerSupplyProperties& props() const { return props_; } | |
| 25 int num_power_changed() const { return num_power_changed_; } | |
| 26 | |
| 27 void ClearProps() { props_.Clear(); } | |
| 28 | |
| 29 void PowerChanged( | |
| 30 const power_manager::PowerSupplyProperties& proto) override { | |
| 31 props_ = proto; | |
| 32 ++num_power_changed_; | |
| 33 } | |
| 34 | |
| 35 private: | |
| 36 int num_power_changed_; | |
| 37 power_manager::PowerSupplyProperties props_; | |
| 38 | |
| 39 DISALLOW_COPY_AND_ASSIGN(TestObserver); | |
| 40 }; | |
| 41 | |
| 42 void SetTestProperties(power_manager::PowerSupplyProperties* props) { | |
| 43 props->set_battery_percent(kDefaultBatteryPercent); | |
| 44 props->set_is_calculating_battery_time(true); | |
| 45 props->set_battery_state( | |
| 46 power_manager::PowerSupplyProperties_BatteryState_DISCHARGING); | |
| 47 props->set_external_power( | |
| 48 power_manager::PowerSupplyProperties_ExternalPower_USB); | |
| 49 } | |
| 50 | |
| 51 } // namespace | |
| 52 | |
| 53 TEST(FakePowerManagerClientTest, UpdatePowerPropertiesTest) { | |
| 54 // Checking to verify when UpdatePowerProperties is called, | |
| 55 // |props_| values are updated. | |
| 56 FakePowerManagerClient client; | |
| 57 power_manager::PowerSupplyProperties props; | |
| 58 | |
| 59 SetTestProperties(&props); | |
| 60 client.UpdatePowerProperties(props); | |
| 61 | |
| 62 EXPECT_EQ(kDefaultBatteryPercent, client.props().battery_percent()); | |
| 63 EXPECT_TRUE(client.props().is_calculating_battery_time()); | |
| 64 EXPECT_EQ(power_manager::PowerSupplyProperties_BatteryState_DISCHARGING, | |
|
Daniel Erat
2015/07/15 22:02:39
make this be a constant at the top of the file too
mozartalouis
2015/07/15 22:29:20
Done.
| |
| 65 client.props().battery_state()); | |
| 66 EXPECT_EQ(power_manager::PowerSupplyProperties_ExternalPower_USB, | |
|
Daniel Erat
2015/07/15 22:02:39
and this
mozartalouis
2015/07/15 22:29:20
Done.
| |
| 67 client.props().external_power()); | |
| 68 | |
| 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(kDefaultBatteryPercentChanged); | |
| 73 client.UpdatePowerProperties(props); | |
| 74 | |
| 75 EXPECT_EQ(kDefaultBatteryPercentChanged, client.props().battery_percent()); | |
| 76 EXPECT_TRUE(client.props().is_calculating_battery_time()); | |
| 77 EXPECT_EQ(power_manager::PowerSupplyProperties_BatteryState_DISCHARGING, | |
| 78 client.props().battery_state()); | |
| 79 EXPECT_EQ(power_manager::PowerSupplyProperties_ExternalPower_USB, | |
| 80 client.props().external_power()); | |
| 81 }; | |
| 82 | |
| 83 TEST(FakePowerManagerClientTest, NotifyObserversTest) { | |
| 84 FakePowerManagerClient client; | |
| 85 TestObserver test_observer; | |
| 86 | |
| 87 // Test adding observer. | |
| 88 client.AddObserver(&test_observer); | |
| 89 EXPECT_TRUE(client.HasObserver(&test_observer)); | |
| 90 | |
| 91 // Test if NotifyObservers() sends the correct values to |observer|. | |
| 92 // Check number of times NotifyObservers() is called. | |
| 93 power_manager::PowerSupplyProperties props; | |
| 94 SetTestProperties(&props); | |
| 95 client.UpdatePowerProperties(props); | |
| 96 | |
| 97 EXPECT_EQ(kDefaultBatteryPercent, test_observer.props().battery_percent()); | |
| 98 EXPECT_TRUE(test_observer.props().is_calculating_battery_time()); | |
| 99 EXPECT_EQ(power_manager::PowerSupplyProperties_BatteryState_DISCHARGING, | |
| 100 test_observer.props().battery_state()); | |
| 101 EXPECT_EQ(power_manager::PowerSupplyProperties_ExternalPower_USB, | |
| 102 test_observer.props().external_power()); | |
| 103 EXPECT_EQ(1, test_observer.num_power_changed()); | |
| 104 | |
| 105 // Test if RequestStatusUpdate() will propagate the data to the observer. | |
| 106 // Check number of times NotifyObservers is called. | |
| 107 // RequestStatusUpdate posts to the current message loop. This is | |
| 108 // necessary because we want to make sure that NotifyObservers() is | |
| 109 // called as a result of RequestStatusUpdate(). | |
| 110 base::MessageLoopForUI message_loop; | |
| 111 test_observer.ClearProps(); | |
| 112 client.RequestStatusUpdate(); | |
| 113 base::RunLoop().RunUntilIdle(); | |
|
Daniel Erat
2015/07/15 22:02:39
thanks for the explanation. running the message lo
mozartalouis
2015/07/15 22:10:30
Acknowledged.
| |
| 114 | |
| 115 EXPECT_EQ(kDefaultBatteryPercent, test_observer.props().battery_percent()); | |
| 116 EXPECT_TRUE(test_observer.props().is_calculating_battery_time()); | |
| 117 EXPECT_EQ(power_manager::PowerSupplyProperties_BatteryState_DISCHARGING, | |
| 118 test_observer.props().battery_state()); | |
| 119 EXPECT_EQ(power_manager::PowerSupplyProperties_ExternalPower_USB, | |
| 120 test_observer.props().external_power()); | |
| 121 EXPECT_EQ(2, test_observer.num_power_changed()); | |
| 122 | |
| 123 // Check when values are changed, the correct values are propagated to the | |
| 124 // observer | |
| 125 props = client.props(); | |
| 126 props.set_battery_percent(kDefaultBatteryPercentChanged); | |
| 127 props.set_external_power( | |
| 128 power_manager::PowerSupplyProperties_ExternalPower_AC); | |
| 129 client.UpdatePowerProperties(props); | |
| 130 | |
| 131 EXPECT_EQ(kDefaultBatteryPercentChanged, | |
| 132 test_observer.props().battery_percent()); | |
| 133 EXPECT_TRUE(test_observer.props().is_calculating_battery_time()); | |
| 134 EXPECT_EQ(power_manager::PowerSupplyProperties_BatteryState_DISCHARGING, | |
| 135 test_observer.props().battery_state()); | |
| 136 EXPECT_EQ(power_manager::PowerSupplyProperties_ExternalPower_AC, | |
| 137 test_observer.props().external_power()); | |
| 138 EXPECT_EQ(3, test_observer.num_power_changed()); | |
| 139 | |
| 140 // Test removing observer. | |
| 141 client.RemoveObserver(&test_observer); | |
| 142 EXPECT_FALSE(client.HasObserver(&test_observer)); | |
| 143 }; | |
| 144 | |
| 145 } // namespace chromeos | |
| OLD | NEW |