Skip to content

Commit

Permalink
Adding *TestAccess Classes to allow access to private Members of test…
Browse files Browse the repository at this point in the history
…ed functions
  • Loading branch information
Alami-Amine committed May 8, 2024
1 parent 304f3e8 commit f3c23e5
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 142 deletions.
11 changes: 9 additions & 2 deletions src/app/ReadHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@
inline constexpr uint16_t kSubscriptionMaxIntervalPublisherLimit = 3600; // seconds (60 minutes)

namespace chip {

// Forward declaration of ReadHandlerTestAccess class to allow it to be friend with the ReadHandler.
// This is not for general API use. It is only to be used for (Unit) Tests to expose private Methods/Members.
namespace Test {
class ReadHandlerTestAccess;
}
namespace app {

//
Expand Down Expand Up @@ -124,14 +130,14 @@ class ReadHandler : public Messaging::ExchangeDelegate
/*
* Called after a subscription has been fully established.
*/
virtual void OnSubscriptionEstablished(ReadHandler & aReadHandler){};
virtual void OnSubscriptionEstablished(ReadHandler & aReadHandler) {};

/*
* Called right before a subscription is about to get terminated. This is only called on subscriptions that were terminated
* after they had been fully established (and therefore had called OnSubscriptionEstablished).
* OnSubscriptionEstablishment().
*/
virtual void OnSubscriptionTerminated(ReadHandler & aReadHandler){};
virtual void OnSubscriptionTerminated(ReadHandler & aReadHandler) {};
};

/*
Expand Down Expand Up @@ -426,6 +432,7 @@ class ReadHandler : public Messaging::ExchangeDelegate
friend class TestReadInteraction;
friend class chip::app::reporting::TestReportingEngine;
friend class chip::app::reporting::TestReportScheduler;
friend class chip::Test::ReadHandlerTestAccess;

//
// The engine needs to be able to Abort/Close a ReadHandler instance upon completion of work for a given read/subscribe
Expand Down
5 changes: 5 additions & 0 deletions src/app/reporting/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
#include <system/TLVPacketBufferBackingStore.h>

namespace chip {
namespace Test {
class EngineTestAccess;

}
namespace app {

class InteractionModelEngine;
Expand Down Expand Up @@ -148,6 +152,7 @@ class Engine

friend class TestReportingEngine;
friend class ::chip::app::TestReadInteraction;
friend class ::chip::Test::EngineTestAccess;

bool IsRunScheduled() const { return mRunScheduled; }

Expand Down
4 changes: 4 additions & 0 deletions src/app/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ chip_test_suite("tests") {

test_sources = [ "TestReportingEngine.cpp" ]

sources = [
"EngineTestAccess.h",
"ReadHandlerTestAccess.h",
]
cflags = [ "-Wconversion" ]

public_deps = [
Expand Down
67 changes: 67 additions & 0 deletions src/app/tests/EngineTestAccess.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
*
* Copyright (c) 2024 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <app/reporting/Engine.h>

using namespace chip::app::reporting;

namespace chip {
namespace Test {

/**
* @brief Class acts as an accessor to private methods of the Engine class without needing to give friend access to
* each individual test.
* This is not a Global API and should only be used for (Unit) Testing.
*/
class EngineTestAccess
{
public:
EngineTestAccess(Engine * aEngine) : pEngine(aEngine) {}

CHIP_ERROR BuildAndSendSingleReportData(chip::app::ReadHandler * apReadHandler)
{
return pEngine->BuildAndSendSingleReportData(apReadHandler);
}

Engine * GetEngine() { return pEngine; }

ObjectPool<Engine::AttributePathParamsWithGeneration, CHIP_IM_SERVER_MAX_NUM_DIRTY_SET, ObjectPoolMem::kInline> &
GetGlobalDirtySet()
{
return pEngine->mGlobalDirtySet;
}

bool MergeOverlappedAttributePath(const chip::app::AttributePathParams & aAttributePath)
{
return pEngine->MergeOverlappedAttributePath(aAttributePath);
}

inline void BumpDirtySetGeneration() { pEngine->BumpDirtySetGeneration(); }

CHIP_ERROR InsertPathIntoDirtySet(const chip::app::AttributePathParams & aAttributePath)
{
return pEngine->InsertPathIntoDirtySet(aAttributePath);
}

private:
Engine * pEngine = nullptr;
};

} // namespace Test
} // namespace chip
51 changes: 51 additions & 0 deletions src/app/tests/ReadHandlerTestAccess.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
*
* Copyright (c) 2024 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <app/ReadHandler.h>

using namespace chip::app;

namespace chip {
namespace Test {

/**
* @brief Class acts as an accessor to private methods of the ReadHandler class without needing to give friend access to
* each individual test.
* This is not a Global API and should only be used for (Unit) Testing.
*/
class ReadHandlerTestAccess
{
public:
ReadHandlerTestAccess(ReadHandler * aReadHandler) : pReadHandler(aReadHandler) {}

void OnInitialRequest(System::PacketBufferHandle && aPayload)
{
if (pReadHandler != nullptr)
{
pReadHandler->OnInitialRequest(std::move(aPayload));
}
}
ReadHandler * GetReadHandler() { return pReadHandler; }

private:
ReadHandler * pReadHandler = nullptr;
};

} // namespace Test
} // namespace chip
Loading

0 comments on commit f3c23e5

Please sign in to comment.