Matter SDK Coverage Report
Current view: top level - app/server-cluster/testing - TestEventGenerator.h (source / functions) Coverage Total Hit
Test: SHA:97920baf58e6bf4a18be41e121e08f13676f36b0 Lines: 88.5 % 26 23
Test Date: 2025-08-17 07:11:11 Functions: 85.7 % 7 6

            Line data    Source code
       1              : /*
       2              :  *    Copyright (c) 2025 Project CHIP Authors
       3              :  *    All rights reserved.
       4              :  *
       5              :  *    Licensed under the Apache License, Version 2.0 (the "License");
       6              :  *    you may not use this file except in compliance with the License.
       7              :  *    You may obtain a copy of the License at
       8              :  *
       9              :  *        http://www.apache.org/licenses/LICENSE-2.0
      10              :  *
      11              :  *    Unless required by applicable law or agreed to in writing, software
      12              :  *    distributed under the License is distributed on an "AS IS" BASIS,
      13              :  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      14              :  *    See the License for the specific language governing permissions and
      15              :  *    limitations under the License.
      16              :  */
      17              : #pragma once
      18              : 
      19              : #include <app/EventLoggingDelegate.h>
      20              : #include <app/EventLoggingTypes.h>
      21              : #include <app/data-model-provider/EventsGenerator.h>
      22              : #include <app/data-model/Decode.h>
      23              : 
      24              : namespace chip {
      25              : namespace Test {
      26              : 
      27              : /// Keeps the "last event" in-memory to allow tests to validate
      28              : /// that event writing and encoding worked.
      29              : ///
      30              : /// Provides the ability to get last event information as well as
      31              : /// to decode the last event contents for inspection.
      32              : class LogOnlyEvents : public app::DataModel::EventsGenerator
      33              : {
      34              : public:
      35            0 :     void ScheduleUrgentEventDeliverySync(std::optional<FabricIndex> fabricIndex = std::nullopt) override
      36              :     {
      37            0 :         mUrgentEventDeliveryCount++;
      38            0 :     }
      39              : 
      40            3 :     CHIP_ERROR GenerateEvent(app::EventLoggingDelegate * eventContentWriter, const app::EventOptions & options,
      41              :                              EventNumber & generatedEventNumber) override
      42              :     {
      43            3 :         TLV::TLVWriter writer;
      44              :         TLV::TLVType outerType;
      45            3 :         writer.Init(mLastEventEncodeBuffer);
      46              : 
      47            3 :         ReturnErrorOnFailure(writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, outerType));
      48            3 :         ReturnErrorOnFailure(eventContentWriter->WriteEvent(writer));
      49            3 :         ReturnErrorOnFailure(writer.EndContainer(outerType));
      50            3 :         ReturnErrorOnFailure(writer.Finalize());
      51            3 :         mLastEncodedSpan = ByteSpan(mLastEventEncodeBuffer, writer.GetLengthWritten());
      52              : 
      53            3 :         mLastOptions         = options;
      54            3 :         generatedEventNumber = ++mCurrentEventNumber;
      55              : 
      56            3 :         return CHIP_NO_ERROR;
      57              :     }
      58              : 
      59            3 :     [[nodiscard]] EventNumber CurrentEventNumber() const { return mCurrentEventNumber; }
      60            3 :     [[nodiscard]] const app::EventOptions & LastOptions() const { return mLastOptions; }
      61            2 :     [[nodiscard]] ByteSpan LastWrittenEvent() const { return mLastEncodedSpan; }
      62              :     [[nodiscard]] uint32_t UrgentEventDeliveryCount() const { return mUrgentEventDeliveryCount; }
      63              : 
      64              :     // This relies on the default encoding of events which uses
      65              :     // DataModel::Encode on a EventDataIB::Tag::kData
      66              :     template <typename T>
      67            2 :     CHIP_ERROR DecodeLastEvent(T & dest)
      68              :     {
      69              :         // attempt to decode the last encoded event
      70            2 :         TLV::TLVReader reader;
      71              :         TLV::TLVType outerType;
      72              : 
      73            2 :         reader.Init(LastWrittenEvent());
      74              : 
      75            2 :         ReturnErrorOnFailure(reader.Next());
      76            2 :         ReturnErrorOnFailure(reader.EnterContainer(outerType));
      77              : 
      78            2 :         ReturnErrorOnFailure(reader.Next()); // MUST be positioned on the first element
      79            2 :         ReturnErrorOnFailure(app::DataModel::Decode(reader, dest));
      80              : 
      81            2 :         ReturnErrorOnFailure(reader.ExitContainer(outerType));
      82              : 
      83            2 :         return CHIP_NO_ERROR;
      84              :     }
      85              : 
      86              : private:
      87              :     uint32_t mUrgentEventDeliveryCount = 0;
      88              :     EventNumber mCurrentEventNumber    = 0;
      89              :     app::EventOptions mLastOptions;
      90              :     uint8_t mLastEventEncodeBuffer[128];
      91              :     ByteSpan mLastEncodedSpan;
      92              : };
      93              : 
      94              : } // namespace Test
      95              : } // namespace chip
        

Generated by: LCOV version 2.0-1