|
| 1 | +/******************************************************************************** |
| 2 | + * Copyright (c) 2026 Contributors to the Eclipse Foundation |
| 3 | + * |
| 4 | + * See the NOTICE file(s) distributed with this work for additional |
| 5 | + * information regarding copyright ownership. |
| 6 | + * |
| 7 | + * This program and the accompanying materials are made available under the |
| 8 | + * terms of the Apache License Version 2.0 which is available at |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * SPDX-License-Identifier: Apache-2.0 |
| 12 | + ********************************************************************************/ |
| 13 | + |
| 14 | +#include "score/mw/com/impl/bindings/lola/skeleton_event_common.h" |
| 15 | +#include "score/mw/com/impl/bindings/lola/test/skeleton_event_test_resources.h" |
| 16 | + |
| 17 | +#include <gmock/gmock.h> |
| 18 | +#include <gtest/gtest.h> |
| 19 | + |
| 20 | +namespace score::mw::com::impl::lola |
| 21 | +{ |
| 22 | +namespace |
| 23 | +{ |
| 24 | + |
| 25 | +using ::testing::_; |
| 26 | +using ::testing::AnyNumber; |
| 27 | +using ::testing::Return; |
| 28 | +using ::testing::ReturnRef; |
| 29 | + |
| 30 | +class SkeletonEventCommonFixture : public SkeletonEventFixture |
| 31 | +{ |
| 32 | + public: |
| 33 | + SkeletonEventCommonFixture() |
| 34 | + { |
| 35 | + ON_CALL(runtime_mock_, GetServiceDiscovery()).WillByDefault(ReturnRef(service_discovery_mock_)); |
| 36 | + } |
| 37 | + |
| 38 | + void InitialiseSkeletonEventCommon(const ElementFqId element_fq_id, |
| 39 | + const std::string& service_element_name, |
| 40 | + const std::size_t max_samples, |
| 41 | + const std::uint8_t max_subscribers, |
| 42 | + const bool enforce_max_samples, |
| 43 | + const QualityType quality_type, |
| 44 | + impl::tracing::SkeletonEventTracingData skeleton_event_tracing_data) |
| 45 | + { |
| 46 | + // We defer initialisation of the Skeleton to InitialiseSkeletonEventCommon to allow test fixtures to set any |
| 47 | + // mocked expectations before creating the skeleton. |
| 48 | + InitialiseSkeleton(GetValidInstanceIdentifierForEventCommon(quality_type)); |
| 49 | + |
| 50 | + SkeletonBinding::SkeletonEventBindings events{}; |
| 51 | + SkeletonBinding::SkeletonFieldBindings fields{}; |
| 52 | + std::optional<SkeletonBinding::RegisterShmObjectTraceCallback> register_shm_object_trace_callback{}; |
| 53 | + |
| 54 | + std::ignore = skeleton_->PrepareOffer(events, fields, std::move(register_shm_object_trace_callback)); |
| 55 | + |
| 56 | + skeleton_event_ = std::make_unique<SkeletonEvent<test::TestSampleType>>( |
| 57 | + *skeleton_, |
| 58 | + element_fq_id, |
| 59 | + service_element_name, |
| 60 | + SkeletonEventProperties{max_samples, max_subscribers, enforce_max_samples}, |
| 61 | + skeleton_event_tracing_data); |
| 62 | + |
| 63 | + // Call PrepareOffer on the skeleton event to trigger Skeleton::Register, which populates |
| 64 | + // the event_controls_ maps in ServiceDataControl for both QM and (if ASIL-B) ASIL-B. |
| 65 | + std::ignore = skeleton_event_->PrepareOffer(); |
| 66 | + |
| 67 | + skeleton_event_common_ = |
| 68 | + std::make_unique<score::mw::com::impl::lola::SkeletonEventCommon<test::TestSampleType>>( |
| 69 | + *skeleton_, |
| 70 | + event_name_, |
| 71 | + SkeletonEventProperties{max_samples, max_subscribers, enforce_max_samples}, |
| 72 | + element_fq_id, |
| 73 | + skeleton_event_tracing_data); |
| 74 | + } |
| 75 | + |
| 76 | + protected: |
| 77 | + std::string_view event_name_{"test_event"}; |
| 78 | + std::unique_ptr<SkeletonEventCommon<test::TestSampleType>> skeleton_event_common_; |
| 79 | + |
| 80 | + InstanceIdentifier GetValidInstanceIdentifierForEventCommon(QualityType quality_type) const |
| 81 | + { |
| 82 | + if (quality_type == QualityType::kASIL_B) |
| 83 | + { |
| 84 | + return make_InstanceIdentifier(valid_asil_instance_deployment_, valid_type_deployment_); |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + return make_InstanceIdentifier(valid_qm_instance_deployment_, valid_type_deployment_); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private: |
| 93 | + ServiceInstanceDeployment valid_qm_instance_deployment_{make_ServiceIdentifierType(service_type_name_), |
| 94 | + binding_info_, |
| 95 | + QualityType::kASIL_QM, |
| 96 | + instance_specifier_}; |
| 97 | +}; |
| 98 | + |
| 99 | +TEST_F(SkeletonEventCommonFixture, RegisterEventNotificationCallbacksForAsilBTriggersMessagePassingRegistration) |
| 100 | +{ |
| 101 | + const bool enforce_max_samples{true}; |
| 102 | + InitialiseSkeletonEventCommon(fake_element_fq_id_, |
| 103 | + fake_event_name_, |
| 104 | + max_samples_, |
| 105 | + max_subscribers_, |
| 106 | + enforce_max_samples, |
| 107 | + QualityType::kASIL_B, |
| 108 | + {}); |
| 109 | + |
| 110 | + auto* const event_control_qm = GetEventControl(fake_element_fq_id_, QualityType::kASIL_QM); |
| 111 | + auto* const event_control_asil_b = GetEventControl(fake_element_fq_id_, QualityType::kASIL_B); |
| 112 | + ASSERT_NE(event_control_qm, nullptr); |
| 113 | + ASSERT_NE(event_control_asil_b, nullptr); |
| 114 | + |
| 115 | + // Expect that RegisterEventNotificationExistenceChangedCallback is called once per quality type with correct ASIL |
| 116 | + // level and element ID |
| 117 | + EXPECT_CALL(message_passing_mock_, |
| 118 | + RegisterEventNotificationExistenceChangedCallback(impl::QualityType::kASIL_B, fake_element_fq_id_, _)) |
| 119 | + .Times(1); |
| 120 | + EXPECT_CALL(message_passing_mock_, |
| 121 | + RegisterEventNotificationExistenceChangedCallback(impl::QualityType::kASIL_QM, fake_element_fq_id_, _)) |
| 122 | + .Times(1); |
| 123 | + // ... when PrepareOfferCommon is called |
| 124 | + skeleton_event_common_->PrepareOfferCommon(*event_control_qm, event_control_asil_b); |
| 125 | +} |
| 126 | + |
| 127 | +TEST_F(SkeletonEventCommonFixture, UnregisterEventNotificationCallbacksForAsilBTriggersMessagePassingUnregistration) |
| 128 | +{ |
| 129 | + const bool enforce_max_samples{true}; |
| 130 | + InitialiseSkeletonEventCommon(fake_element_fq_id_, |
| 131 | + fake_event_name_, |
| 132 | + max_samples_, |
| 133 | + max_subscribers_, |
| 134 | + enforce_max_samples, |
| 135 | + QualityType::kASIL_B, |
| 136 | + {}); |
| 137 | + |
| 138 | + auto* const event_control_qm = GetEventControl(fake_element_fq_id_, QualityType::kASIL_QM); |
| 139 | + auto* const event_control_asil_b = GetEventControl(fake_element_fq_id_, QualityType::kASIL_B); |
| 140 | + ASSERT_NE(event_control_qm, nullptr); |
| 141 | + ASSERT_NE(event_control_asil_b, nullptr); |
| 142 | + |
| 143 | + // Expect that RegisterEventNotificationExistenceChangedCallback is called once per quality type with correct ASIL |
| 144 | + // level and element ID |
| 145 | + EXPECT_CALL(message_passing_mock_, |
| 146 | + UnregisterEventNotificationExistenceChangedCallback(impl::QualityType::kASIL_B, fake_element_fq_id_)) |
| 147 | + .Times(1); |
| 148 | + EXPECT_CALL(message_passing_mock_, |
| 149 | + UnregisterEventNotificationExistenceChangedCallback(impl::QualityType::kASIL_QM, fake_element_fq_id_)) |
| 150 | + .Times(1); |
| 151 | + // ... when PrepareStopOfferCommon is called |
| 152 | + skeleton_event_common_->PrepareStopOfferCommon(); |
| 153 | +} |
| 154 | + |
| 155 | +} // namespace |
| 156 | + |
| 157 | +} // namespace score::mw::com::impl::lola |
0 commit comments