-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpower_supply_training_condition_test.cc
223 lines (190 loc) · 8.52 KB
/
power_supply_training_condition_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright 2022 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include <string>
#include <unordered_map>
#include <utility>
#include <base/check.h>
#include <base/functional/callback.h>
#include <base/memory/ref_counted.h>
#include <dbus/message.h>
#include <dbus/mock_bus.h>
#include <dbus/mock_object_proxy.h>
#include <dbus/power_manager/dbus-constants.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "federated/power_supply_training_condition.h"
#include "power_manager/proto_bindings/battery_saver.pb.h"
#include "power_manager/proto_bindings/power_supply_properties.pb.h"
namespace federated {
namespace {
using ::power_manager::kBatterySaverModeStateChanged;
using ::power_manager::kGetBatterySaverModeState;
using ::power_manager::kPowerManagerInterface;
using ::power_manager::kPowerManagerServiceName;
using ::power_manager::kPowerManagerServicePath;
using ::power_manager::kPowerSupplyPollSignal;
using ::testing::_;
using ::testing::Invoke;
using ::testing::SaveArg;
using ::testing::StrictMock;
// Generates a PowerSupplyProperties proto with the given battery percent and
// state.
power_manager::PowerSupplyProperties GeneratePowerSupplyProto(
const double battery_percent,
power_manager::PowerSupplyProperties::BatteryState battery_state) {
power_manager::PowerSupplyProperties power_supply_proto;
power_supply_proto.set_battery_percent(battery_percent);
power_supply_proto.set_battery_state(battery_state);
return power_supply_proto;
}
} // namespace
class PowerSupplyTrainingConditionTest : public ::testing::Test {
public:
PowerSupplyTrainingConditionTest()
: mock_dbus_(new StrictMock<dbus::MockBus>(dbus::Bus::Options())),
dbus_object_proxy_(new StrictMock<dbus::MockObjectProxy>(
mock_dbus_.get(),
kPowerManagerServiceName,
dbus::ObjectPath(kPowerManagerServicePath))) {}
PowerSupplyTrainingConditionTest(const PowerSupplyTrainingConditionTest&) =
delete;
PowerSupplyTrainingConditionTest& operator=(
const PowerSupplyTrainingConditionTest&) = delete;
void SetUp() override {
EXPECT_CALL(*mock_dbus_,
GetObjectProxy(kPowerManagerServiceName,
dbus::ObjectPath(kPowerManagerServicePath)))
.WillOnce(Return(dbus_object_proxy_.get()));
EXPECT_CALL(
*dbus_object_proxy_,
DoConnectToSignal(kPowerManagerInterface, kPowerSupplyPollSignal, _, _))
.WillOnce(SaveArg<2>(&on_signal_callbacks_[kPowerSupplyPollSignal]));
EXPECT_CALL(*dbus_object_proxy_,
DoConnectToSignal(kPowerManagerInterface,
kBatterySaverModeStateChanged, _, _))
.WillOnce(
SaveArg<2>(&on_signal_callbacks_[kBatterySaverModeStateChanged]));
EXPECT_CALL(*dbus_object_proxy_, DoWaitForServiceToBeAvailable(_))
.WillOnce(Invoke(
[](dbus::MockObjectProxy::WaitForServiceToBeAvailableCallback*
callback) { std::move(*callback).Run(true); }));
EXPECT_CALL(*dbus_object_proxy_, CallMethodAndBlock(_, _))
.WillOnce(Invoke(
this,
&PowerSupplyTrainingConditionTest::RespondGetBatterySaverMode));
power_supply_training_condition_ =
std::make_unique<PowerSupplyTrainingCondition>(mock_dbus_.get());
}
// Creates a dbus signal with the given `signal_name`, writes the given string
// (a serialized proto or "") to it and invokes.
void CreateSignalAndInvoke(const std::string& serialized_proto,
const std::string& signal_name) {
dbus::Signal signal(kPowerManagerInterface, signal_name);
dbus::MessageWriter writer(&signal);
writer.AppendArrayOfBytes(
reinterpret_cast<const uint8_t*>(serialized_proto.data()),
serialized_proto.size());
auto callback_iter = on_signal_callbacks_.find(signal_name);
ASSERT_NE(callback_iter, on_signal_callbacks_.end());
callback_iter->second.Run(&signal);
}
// Initialises the battery saver mode as disabled.
std::unique_ptr<dbus::Response> RespondGetBatterySaverMode(
dbus::MethodCall* method_call, int timeout_msec) {
EXPECT_EQ(method_call->GetInterface(), kPowerManagerInterface);
EXPECT_EQ(method_call->GetMember(), kGetBatterySaverModeState);
method_call->SetSerial(1); // Needs to be non-zero or it fails.
std::unique_ptr<dbus::Response> response =
dbus::Response::FromMethodCall(method_call);
dbus::MessageWriter writer(response.get());
power_manager::BatterySaverModeState battery_saver_mode_state;
battery_saver_mode_state.set_enabled(false);
writer.AppendProtoAsArrayOfBytes(battery_saver_mode_state);
return response;
}
PowerSupplyTrainingCondition* power_supply_training_condition() const {
DCHECK(power_supply_training_condition_);
return power_supply_training_condition_.get();
}
private:
scoped_refptr<StrictMock<dbus::MockBus>> mock_dbus_;
scoped_refptr<StrictMock<dbus::MockObjectProxy>> dbus_object_proxy_;
std::unique_ptr<PowerSupplyTrainingCondition>
power_supply_training_condition_;
std::unordered_map<std::string,
base::RepeatingCallback<void(dbus::Signal* signal)>>
on_signal_callbacks_;
};
// Tests PowerSupply is initialized correctly and can handle unexpected
// empty proto messages.
TEST_F(PowerSupplyTrainingConditionTest, EmptyPowerSupplyPollSignal) {
// Initialized as false;
EXPECT_FALSE(
power_supply_training_condition()->IsTrainingConditionSatisfied());
// Invoke kPowerSupplyPollSignal with an empty proto message.
CreateSignalAndInvoke("", kPowerSupplyPollSignal);
EXPECT_FALSE(
power_supply_training_condition()->IsTrainingConditionSatisfied());
}
TEST_F(PowerSupplyTrainingConditionTest, PowerSupplySatisfied) {
// Note: here we must create new signals rather than write to the old one,
// because the WriteSerializedProtoToSignal is effectively "append", only the
// first written string can be read by the DeviceStatusMonitor.
// battery = 95, state = discharging, satisfied.
CreateSignalAndInvoke(
GeneratePowerSupplyProto(
95.0, power_manager::PowerSupplyProperties::DISCHARGING)
.SerializeAsString(),
kPowerSupplyPollSignal);
EXPECT_TRUE(
power_supply_training_condition()->IsTrainingConditionSatisfied());
// battery = 50, state = charging, satisfied.
CreateSignalAndInvoke(
GeneratePowerSupplyProto(50.0,
power_manager::PowerSupplyProperties::CHARGING)
.SerializeAsString(),
kPowerSupplyPollSignal);
EXPECT_TRUE(
power_supply_training_condition()->IsTrainingConditionSatisfied());
// battery = 100, state = full, satisfied.
CreateSignalAndInvoke(GeneratePowerSupplyProto(
100.0, power_manager::PowerSupplyProperties::FULL)
.SerializeAsString(),
kPowerSupplyPollSignal);
EXPECT_TRUE(
power_supply_training_condition()->IsTrainingConditionSatisfied());
}
TEST_F(PowerSupplyTrainingConditionTest, PowerSupplyNotSatisfied) {
// battery = 80, state = discharging, unsatisfied.
CreateSignalAndInvoke(
GeneratePowerSupplyProto(
80.0, power_manager::PowerSupplyProperties::DISCHARGING)
.SerializeAsString(),
kPowerSupplyPollSignal);
EXPECT_FALSE(
power_supply_training_condition()->IsTrainingConditionSatisfied());
}
TEST_F(PowerSupplyTrainingConditionTest, RespectBatterySaverMode) {
// Meets the power supply requirements.
CreateSignalAndInvoke(GeneratePowerSupplyProto(
100.0, power_manager::PowerSupplyProperties::FULL)
.SerializeAsString(),
kPowerSupplyPollSignal);
EXPECT_TRUE(
power_supply_training_condition()->IsTrainingConditionSatisfied());
// When battery_saver is enabled, power supply conditions are not satisfied.
power_manager::BatterySaverModeState battery_saver_mode_state;
battery_saver_mode_state.set_enabled(true);
CreateSignalAndInvoke(battery_saver_mode_state.SerializeAsString(),
kBatterySaverModeStateChanged);
EXPECT_FALSE(
power_supply_training_condition()->IsTrainingConditionSatisfied());
battery_saver_mode_state.set_enabled(false);
CreateSignalAndInvoke(battery_saver_mode_state.SerializeAsString(),
kBatterySaverModeStateChanged);
EXPECT_TRUE(
power_supply_training_condition()->IsTrainingConditionSatisfied());
}
} // namespace federated