Matter SDK Coverage Report
Current view: top level - app/server-cluster/testing - TestEventGenerator.h (source / functions) Coverage Total Hit
Test: SHA:db08debc068562b264a2df3a7f3a8cc1d0b3aba1 Lines: 88.5 % 26 23
Test Date: 2025-10-02 07:10:30 Functions: 87.5 % 8 7

            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           13 :     CHIP_ERROR GenerateEvent(app::EventLoggingDelegate * eventContentWriter, const app::EventOptions & options,
      41              :                              EventNumber & generatedEventNumber) override
      42              :     {
      43           13 :         TLV::TLVWriter writer;
      44              :         TLV::TLVType outerType;
      45           13 :         writer.Init(mLastEventEncodeBuffer);
      46              : 
      47           13 :         ReturnErrorOnFailure(writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Structure, outerType));
      48           13 :         ReturnErrorOnFailure(eventContentWriter->WriteEvent(writer));
      49           13 :         ReturnErrorOnFailure(writer.EndContainer(outerType));
      50           13 :         ReturnErrorOnFailure(writer.Finalize());
      51           13 :         mLastEncodedSpan = ByteSpan(mLastEventEncodeBuffer, writer.GetLengthWritten());
      52              : 
      53           13 :         mLastOptions         = options;
      54           13 :         generatedEventNumber = ++mCurrentEventNumber;
      55              : 
      56           13 :         return CHIP_NO_ERROR;
      57              :     }
      58              : 
      59           17 :     [[nodiscard]] EventNumber CurrentEventNumber() const { return mCurrentEventNumber; }
      60            7 :     [[nodiscard]] const app::EventOptions & LastOptions() const { return mLastOptions; }
      61            6 :     [[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            6 :     CHIP_ERROR DecodeLastEvent(T & dest)
      68              :     {
      69              :         // attempt to decode the last encoded event
      70            6 :         TLV::TLVReader reader;
      71              :         TLV::TLVType outerType;
      72              : 
      73            6 :         reader.Init(LastWrittenEvent());
      74              : 
      75            6 :         ReturnErrorOnFailure(reader.Next());
      76            6 :         ReturnErrorOnFailure(reader.EnterContainer(outerType));
      77              : 
      78            6 :         ReturnErrorOnFailure(reader.Next()); // MUST be positioned on the first element
      79            6 :         ReturnErrorOnFailure(app::DataModel::Decode(reader, dest));
      80              : 
      81            6 :         ReturnErrorOnFailure(reader.ExitContainer(outerType));
      82              : 
      83            6 :         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