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 "testing/gtest/include/gtest/gtest.h" | |
| 11 | |
| 12 namespace chromeos { | |
| 13 namespace { | |
| 14 class TestObserver : public PowerManagerClient::Observer { | |
| 15 public: | |
| 16 TestObserver() : num_power_changed_(0) {} | |
| 17 ~TestObserver() override {} | |
| 18 | |
| 19 const power_manager::PowerSupplyProperties& props() const { return props_; } | |
| 20 int num_power_changed() const { return num_power_changed_; } | |
| 21 | |
| 22 void ClearProps() { props_.Clear(); } | |
| 23 | |
| 24 void PowerChanged( | |
| 25 const power_manager::PowerSupplyProperties& proto) override { | |
| 26 props_ = proto; | |
| 27 ++num_power_changed_; | |
| 28 } | |
| 29 | |
| 30 private: | |
| 31 int num_power_changed_; | |
| 32 power_manager::PowerSupplyProperties props_; | |
| 33 | |
| 34 DISALLOW_COPY_AND_ASSIGN(TestObserver); | |
| 35 }; | |
| 36 | |
| 37 void SetTestProperties(power_manager::PowerSupplyProperties* props) { | |
| 38 props->set_battery_percent(85); | |
| 39 props->set_is_calculating_battery_time(true); | |
| 40 props->set_battery_state( | |
| 41 power_manager::PowerSupplyProperties_BatteryState_DISCHARGING); | |
| 42 props->set_external_power( | |
| 43 power_manager::PowerSupplyProperties_ExternalPower_USB); | |
| 44 } | |
| 45 | |
| 46 } // namespace | |
| 47 | |
| 48 TEST(FakePowerManagerClientTest, UpdatePowerPropertiesTest) { | |
| 49 // Checking to verify when UpdatePowerProperties is called, | |
| 50 // |props_| values are updated. | |
| 51 FakePowerManagerClient client; | |
| 52 power_manager::PowerSupplyProperties props; | |
| 53 | |
| 54 SetTestProperties(&props); | |
| 55 client.UpdatePowerProperties(props); | |
| 56 | |
| 57 EXPECT_EQ(85, client.props().battery_percent()); | |
|
Daniel Erat
2015/07/15 19:05:47
there are a bunch of constants (85, _DISCHARGING,
oshima
2015/07/15 19:59:01
I prefer tests to use constants or actual value ra
Daniel Erat
2015/07/15 20:02:07
constants are fine if they're also shared by SetTe
mozartalouis
2015/07/15 21:26:38
Done.
| |
| 58 EXPECT_TRUE(client.props().is_calculating_battery_time()); | |
| 59 EXPECT_EQ(power_manager::PowerSupplyProperties_BatteryState_DISCHARGING, | |
| 60 client.props().battery_state()); | |
| 61 EXPECT_EQ(power_manager::PowerSupplyProperties_ExternalPower_USB, | |
| 62 client.props().external_power()); | |
| 63 | |
| 64 // Test if when the values are changed, the correct data is set in the | |
| 65 // FakePowerManagerClient. | |
| 66 props = client.props(); | |
| 67 props.set_battery_percent(70); | |
| 68 client.UpdatePowerProperties(props); | |
| 69 | |
| 70 EXPECT_EQ(70, client.props().battery_percent()); | |
| 71 EXPECT_TRUE(client.props().is_calculating_battery_time()); | |
| 72 EXPECT_EQ(power_manager::PowerSupplyProperties_BatteryState_DISCHARGING, | |
| 73 client.props().battery_state()); | |
| 74 EXPECT_EQ(power_manager::PowerSupplyProperties_ExternalPower_USB, | |
| 75 client.props().external_power()); | |
| 76 }; | |
| 77 | |
| 78 TEST(FakePowerManagerClientTest, NotifyObserversTest) { | |
| 79 FakePowerManagerClient client; | |
| 80 TestObserver test_observer; | |
| 81 | |
| 82 // Test adding observer. | |
| 83 client.AddObserver(&test_observer); | |
| 84 EXPECT_TRUE(client.HasObserver(&test_observer)); | |
| 85 | |
| 86 // Test if NotifyObservers() sends the correct values to |observer|. | |
| 87 // Check number of times NotifyObservers() is called. | |
| 88 power_manager::PowerSupplyProperties props; | |
| 89 SetTestProperties(&props); | |
| 90 client.UpdatePowerProperties(props); | |
| 91 | |
| 92 EXPECT_EQ(85, test_observer.props().battery_percent()); | |
| 93 EXPECT_TRUE(test_observer.props().is_calculating_battery_time()); | |
| 94 EXPECT_EQ(power_manager::PowerSupplyProperties_BatteryState_DISCHARGING, | |
| 95 test_observer.props().battery_state()); | |
| 96 EXPECT_EQ(power_manager::PowerSupplyProperties_ExternalPower_USB, | |
| 97 test_observer.props().external_power()); | |
| 98 EXPECT_EQ(1, test_observer.num_power_changed()); | |
| 99 | |
| 100 // Test if RequestStatusUpdate() will propergate the data to the observer. | |
|
Daniel Erat
2015/07/15 19:05:46
sp: propagate
mozartalouis
2015/07/15 21:26:38
Done.
| |
| 101 // Check number of times NotifyObservers is called. | |
| 102 // RequestStatusUpdate posts to the current message loop. This is | |
| 103 // nessesary becuase we want to make sure that NotifyObservers() is | |
| 104 // called as a result of RequestStatusUpdate(). | |
|
Daniel Erat
2015/07/15 19:05:47
i don't think i understand why you need to post a
mozartalouis
2015/07/15 21:26:38
From the PowerStatusViewTest.Basic the test fails:
| |
| 105 base::MessageLoopForUI message_loop; | |
|
oshima
2015/07/15 18:46:50
can you use base::RunLoop instead?
mozartalouis
2015/07/15 21:26:38
Done.
| |
| 106 test_observer.ClearProps(); | |
| 107 client.RequestStatusUpdate(); | |
| 108 message_loop.RunUntilIdle(); | |
| 109 | |
| 110 EXPECT_EQ(85, test_observer.props().battery_percent()); | |
| 111 EXPECT_TRUE(test_observer.props().is_calculating_battery_time()); | |
| 112 EXPECT_EQ(power_manager::PowerSupplyProperties_BatteryState_DISCHARGING, | |
| 113 test_observer.props().battery_state()); | |
| 114 EXPECT_EQ(power_manager::PowerSupplyProperties_ExternalPower_USB, | |
| 115 test_observer.props().external_power()); | |
| 116 EXPECT_EQ(2, test_observer.num_power_changed()); | |
| 117 | |
| 118 // Check when values are changed, the correct values are propergated to the | |
|
Daniel Erat
2015/07/15 19:05:47
sp: propagated
mozartalouis
2015/07/15 21:26:38
Done.
| |
| 119 // Obserever | |
|
Daniel Erat
2015/07/15 19:05:47
sp: observer
mozartalouis
2015/07/15 21:26:38
Done.
| |
| 120 props = client.props(); | |
| 121 props.set_battery_percent(90); | |
| 122 props.set_external_power( | |
| 123 power_manager::PowerSupplyProperties_ExternalPower_AC); | |
| 124 client.UpdatePowerProperties(props); | |
| 125 | |
| 126 EXPECT_EQ(90, test_observer.props().battery_percent()); | |
| 127 EXPECT_TRUE(test_observer.props().is_calculating_battery_time()); | |
| 128 EXPECT_EQ(power_manager::PowerSupplyProperties_BatteryState_DISCHARGING, | |
| 129 test_observer.props().battery_state()); | |
| 130 EXPECT_EQ(power_manager::PowerSupplyProperties_ExternalPower_AC, | |
| 131 test_observer.props().external_power()); | |
| 132 EXPECT_EQ(3, test_observer.num_power_changed()); | |
| 133 | |
| 134 // Test removing observer. | |
| 135 client.RemoveObserver(&test_observer); | |
| 136 EXPECT_FALSE(client.HasObserver(&test_observer)); | |
| 137 }; | |
| 138 | |
| 139 } // namespace chromeos | |
| OLD | NEW |