Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 1 | #include "dbus_environment.hpp" |
| 2 | #include "helpers.hpp" |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 3 | #include "mocks/clock_mock.hpp" |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 4 | #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 | |
| 11 | using namespace testing; |
| 12 | using namespace std::chrono_literals; |
| 13 | |
| 14 | class 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 Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 25 | std::string triggerId = "MyTrigger"; |
| 26 | std::unique_ptr<NiceMock<ClockMock>> clockMockPtr = |
| 27 | std::make_unique<NiceMock<ClockMock>>(); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 28 | |
| 29 | void SetUp() override |
| 30 | { |
| 31 | std::vector<std::unique_ptr<interfaces::TriggerAction>> actions; |
| 32 | actions.push_back(std::move(actionMockPtr)); |
| 33 | |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 34 | 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 Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 40 | sut = std::make_shared<OnChangeThreshold>( |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 41 | triggerId, |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 42 | utils::convContainer<std::shared_ptr<interfaces::Sensor>>( |
| 43 | sensorMocks), |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 44 | std::move(actions), std::move(clockMockPtr)); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 45 | } |
| 46 | }; |
| 47 | |
| 48 | TEST_F(TestOnChangeThreshold, initializeThresholdExpectAllSensorsAreRegistered) |
| 49 | { |
| 50 | for (auto& sensor : sensorMocks) |
| 51 | { |
| 52 | EXPECT_CALL(*sensor, |
| 53 | registerForUpdates(Truly([sut = sut.get()](const auto& x) { |
Patrick Williams | c7935fa | 2023-10-20 11:19:30 -0500 | [diff] [blame] | 54 | return x.lock().get() == sut; |
| 55 | }))); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | sut->initialize(); |
| 59 | } |
| 60 | |
| 61 | TEST_F(TestOnChangeThreshold, thresholdIsNotInitializeExpectNoActionCommit) |
| 62 | { |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 63 | EXPECT_CALL(actionMock, commit(_, _, _, _, _)).Times(0); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 64 | } |
| 65 | |
Szymon Dompke | 94f71c5 | 2021-12-10 07:16:33 +0100 | [diff] [blame] | 66 | TEST_F(TestOnChangeThreshold, getLabeledParamsReturnsCorrectly) |
| 67 | { |
| 68 | LabeledThresholdParam expected = std::monostate(); |
| 69 | EXPECT_EQ(sut->getThresholdParam(), expected); |
| 70 | } |
| 71 | |
Szymon Dompke | 620c65a | 2022-03-23 21:09:27 +0100 | [diff] [blame] | 72 | TEST_F(TestOnChangeThreshold, firstReadingDoesNoActionCommit) |
| 73 | { |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 74 | EXPECT_CALL(actionMock, commit(_, _, _, _, _)).Times(0); |
Szymon Dompke | 620c65a | 2022-03-23 21:09:27 +0100 | [diff] [blame] | 75 | |
| 76 | sut->initialize(); |
| 77 | sut->sensorUpdated(*sensorMocks.front(), 0ms, 42); |
| 78 | } |
| 79 | |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 80 | struct OnChangeParams |
| 81 | { |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 82 | using UpdateParams = std::tuple<size_t, double>; |
| 83 | using ExpectedParams = std::tuple<size_t, double>; |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 84 | |
| 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 Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 99 | *os << "{ Updates: [ "; |
| 100 | for (const auto& [index, value] : o.updates) |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 101 | { |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 102 | *os << "{ SensorIndex: " << index << ", Value: " << value << " }, "; |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 103 | } |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 104 | *os << " ] Expected: [ "; |
| 105 | for (const auto& [index, value] : o.expected) |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 106 | { |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 107 | *os << "{ SensorIndex: " << index << ", Value: " << value << " }, "; |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 108 | } |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 109 | *os << " ] }"; |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | std::vector<UpdateParams> updates; |
| 113 | std::vector<ExpectedParams> expected; |
| 114 | }; |
| 115 | |
| 116 | class TestOnChangeThresholdUpdates : |
| 117 | public TestOnChangeThreshold, |
| 118 | public WithParamInterface<OnChangeParams> |
| 119 | {}; |
| 120 | |
| 121 | INSTANTIATE_TEST_SUITE_P( |
| 122 | _, TestOnChangeThresholdUpdates, |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 123 | 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 Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 136 | |
| 137 | TEST_P(TestOnChangeThresholdUpdates, senorsIsUpdatedMultipleTimes) |
| 138 | { |
| 139 | InSequence seq; |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 140 | for (const auto& [index, value] : GetParam().expected) |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 141 | { |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 142 | EXPECT_CALL(actionMock, |
| 143 | commit(triggerId, Eq(std::nullopt), sensorNames[index], _, |
| 144 | TriggerValue(value))); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | sut->initialize(); |
Szymon Dompke | 620c65a | 2022-03-23 21:09:27 +0100 | [diff] [blame] | 148 | |
| 149 | // First reading will be skipped |
| 150 | sut->sensorUpdated(*sensorMocks.front(), 0ms, 42); |
| 151 | |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 152 | for (const auto& [index, value] : GetParam().updates) |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 153 | { |
Szymon Dompke | b7b7e1b | 2022-05-19 10:15:48 +0200 | [diff] [blame] | 154 | sut->sensorUpdated(*sensorMocks[index], 42ms, value); |
Szymon Dompke | f763c9e | 2021-03-12 09:19:22 +0100 | [diff] [blame] | 155 | } |
| 156 | } |