blob: b4a6fd32cebb56f73cfa8e19b677e5cb4650ad44 [file] [log] [blame]
Szymon Dompkef763c9e2021-03-12 09:19:22 +01001#include "dbus_environment.hpp"
2#include "helpers.hpp"
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +02003#include "mocks/clock_mock.hpp"
Szymon Dompkef763c9e2021-03-12 09:19:22 +01004#include "mocks/sensor_mock.hpp"
5#include "mocks/trigger_action_mock.hpp"
6#include "on_change_threshold.hpp"
7#include "utils/conv_container.hpp"
8
9#include <gmock/gmock.h>
10
11using namespace testing;
12using namespace std::chrono_literals;
13
14class TestOnChangeThreshold : public Test
15{
16 public:
17 std::vector<std::shared_ptr<SensorMock>> sensorMocks = {
18 std::make_shared<NiceMock<SensorMock>>(),
19 std::make_shared<NiceMock<SensorMock>>()};
20 std::vector<std::string> sensorNames = {"Sensor1", "Sensor2"};
21 std::unique_ptr<TriggerActionMock> actionMockPtr =
22 std::make_unique<StrictMock<TriggerActionMock>>();
23 TriggerActionMock& actionMock = *actionMockPtr;
24 std::shared_ptr<OnChangeThreshold> sut;
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020025 std::string triggerId = "MyTrigger";
26 std::unique_ptr<NiceMock<ClockMock>> clockMockPtr =
27 std::make_unique<NiceMock<ClockMock>>();
Szymon Dompkef763c9e2021-03-12 09:19:22 +010028
29 void SetUp() override
30 {
31 std::vector<std::unique_ptr<interfaces::TriggerAction>> actions;
32 actions.push_back(std::move(actionMockPtr));
33
Szymon Dompke94f71c52021-12-10 07:16:33 +010034 for (size_t idx = 0; idx < sensorMocks.size(); idx++)
35 {
36 ON_CALL(*sensorMocks.at(idx), getName())
37 .WillByDefault(Return(sensorNames[idx]));
38 }
39
Szymon Dompkef763c9e2021-03-12 09:19:22 +010040 sut = std::make_shared<OnChangeThreshold>(
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020041 triggerId,
Szymon Dompkef763c9e2021-03-12 09:19:22 +010042 utils::convContainer<std::shared_ptr<interfaces::Sensor>>(
43 sensorMocks),
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020044 std::move(actions), std::move(clockMockPtr));
Szymon Dompkef763c9e2021-03-12 09:19:22 +010045 }
46};
47
48TEST_F(TestOnChangeThreshold, initializeThresholdExpectAllSensorsAreRegistered)
49{
50 for (auto& sensor : sensorMocks)
51 {
52 EXPECT_CALL(*sensor,
53 registerForUpdates(Truly([sut = sut.get()](const auto& x) {
54 return x.lock().get() == sut;
55 })));
56 }
57
58 sut->initialize();
59}
60
61TEST_F(TestOnChangeThreshold, thresholdIsNotInitializeExpectNoActionCommit)
62{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020063 EXPECT_CALL(actionMock, commit(_, _, _, _, _)).Times(0);
Szymon Dompkef763c9e2021-03-12 09:19:22 +010064}
65
Szymon Dompke94f71c52021-12-10 07:16:33 +010066TEST_F(TestOnChangeThreshold, getLabeledParamsReturnsCorrectly)
67{
68 LabeledThresholdParam expected = std::monostate();
69 EXPECT_EQ(sut->getThresholdParam(), expected);
70}
71
Szymon Dompke620c65a2022-03-23 21:09:27 +010072TEST_F(TestOnChangeThreshold, firstReadingDoesNoActionCommit)
73{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020074 EXPECT_CALL(actionMock, commit(_, _, _, _, _)).Times(0);
Szymon Dompke620c65a2022-03-23 21:09:27 +010075
76 sut->initialize();
77 sut->sensorUpdated(*sensorMocks.front(), 0ms, 42);
78}
79
Szymon Dompkef763c9e2021-03-12 09:19:22 +010080struct OnChangeParams
81{
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020082 using UpdateParams = std::tuple<size_t, double>;
83 using ExpectedParams = std::tuple<size_t, double>;
Szymon Dompkef763c9e2021-03-12 09:19:22 +010084
85 OnChangeParams& Updates(std::vector<UpdateParams> val)
86 {
87 updates = std::move(val);
88 return *this;
89 }
90
91 OnChangeParams& Expected(std::vector<ExpectedParams> val)
92 {
93 expected = std::move(val);
94 return *this;
95 }
96
97 friend void PrintTo(const OnChangeParams& o, std::ostream* os)
98 {
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +020099 *os << "{ Updates: [ ";
100 for (const auto& [index, value] : o.updates)
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100101 {
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200102 *os << "{ SensorIndex: " << index << ", Value: " << value << " }, ";
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100103 }
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200104 *os << " ] Expected: [ ";
105 for (const auto& [index, value] : o.expected)
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100106 {
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200107 *os << "{ SensorIndex: " << index << ", Value: " << value << " }, ";
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100108 }
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200109 *os << " ] }";
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100110 }
111
112 std::vector<UpdateParams> updates;
113 std::vector<ExpectedParams> expected;
114};
115
116class TestOnChangeThresholdUpdates :
117 public TestOnChangeThreshold,
118 public WithParamInterface<OnChangeParams>
119{};
120
121INSTANTIATE_TEST_SUITE_P(
122 _, TestOnChangeThresholdUpdates,
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200123 Values(OnChangeParams().Updates({{0, 80.0}}).Expected({{0, 80.0}}),
124 OnChangeParams()
125 .Updates({{0, 80.0}, {1, 81.0}})
126 .Expected({{0, 80.0}, {1, 81.0}}),
127 OnChangeParams()
128 .Updates({{0, 80.0}, {0, 90.0}})
129 .Expected({{0, 80.0}, {0, 90.0}}),
130 OnChangeParams()
131 .Updates({{0, 80.0}, {1, 90.0}, {0, 90.0}})
132 .Expected({{0, 80.0}, {1, 90.0}, {0, 90.0}}),
133 OnChangeParams()
134 .Updates({{0, 80.0}, {1, 80.0}, {1, 90.0}, {0, 90.0}})
135 .Expected({{0, 80.0}, {1, 80.0}, {1, 90.0}, {0, 90.0}})));
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100136
137TEST_P(TestOnChangeThresholdUpdates, senorsIsUpdatedMultipleTimes)
138{
139 InSequence seq;
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200140 for (const auto& [index, value] : GetParam().expected)
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100141 {
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200142 EXPECT_CALL(actionMock,
143 commit(triggerId, Eq(std::nullopt), sensorNames[index], _,
144 TriggerValue(value)));
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100145 }
146
147 sut->initialize();
Szymon Dompke620c65a2022-03-23 21:09:27 +0100148
149 // First reading will be skipped
150 sut->sensorUpdated(*sensorMocks.front(), 0ms, 42);
151
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200152 for (const auto& [index, value] : GetParam().updates)
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100153 {
Szymon Dompkeb7b7e1b2022-05-19 10:15:48 +0200154 sut->sensorUpdated(*sensorMocks[index], 42ms, value);
Szymon Dompkef763c9e2021-03-12 09:19:22 +0100155 }
156}