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