Skip to content

Commit f3bf109

Browse files
committed
Modified MessagingContext and its subclasses as well as AppContext to reflect PW migration.
1 parent fef9ee3 commit f3bf109

File tree

6 files changed

+197
-270
lines changed

6 files changed

+197
-270
lines changed

src/app/tests/AppTestContext.cpp

+12-14
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
#include <app/tests/AppTestContext.h>
17+
#include "AppTestContext.h"
1818

1919
#include <access/AccessControl.h>
2020
#include <access/examples/PermissiveAccessControlDelegate.h>
@@ -40,11 +40,10 @@ namespace Test {
4040

4141
void AppContext::SetUpTestSuite()
4242
{
43-
CHIP_ERROR err = CHIP_NO_ERROR;
4443
LoopbackMessagingContext::SetUpTestSuite();
45-
// TODO: use ASSERT_EQ, once transition to pw_unit_test is complete
46-
VerifyOrDieWithMsg((err = chip::DeviceLayer::PlatformMgr().InitChipStack()) == CHIP_NO_ERROR, AppServer,
47-
"Init CHIP stack failed: %" CHIP_ERROR_FORMAT, err.Format());
44+
VerifyOrReturn(!HasFailure()); // Stop if parent had a failure.
45+
46+
ASSERT_EQ(chip::DeviceLayer::PlatformMgr().InitChipStack(), CHIP_NO_ERROR);
4847
}
4948

5049
void AppContext::TearDownTestSuite()
@@ -55,24 +54,23 @@ void AppContext::TearDownTestSuite()
5554

5655
void AppContext::SetUp()
5756
{
58-
CHIP_ERROR err = CHIP_NO_ERROR;
5957
LoopbackMessagingContext::SetUp();
60-
// TODO: use ASSERT_EQ, once transition to pw_unit_test is complete
61-
VerifyOrDieWithMsg((err = app::InteractionModelEngine::GetInstance()->Init(&GetExchangeManager(), &GetFabricTable(),
62-
app::reporting::GetDefaultReportScheduler())) ==
63-
CHIP_NO_ERROR,
64-
AppServer, "Init InteractionModelEngine failed: %" CHIP_ERROR_FORMAT, err.Format());
58+
VerifyOrReturn(!HasFailure()); // Stop if parent had a failure.
59+
60+
ASSERT_EQ(app::InteractionModelEngine::GetInstance()->Init(&GetExchangeManager(), &GetFabricTable(),
61+
app::reporting::GetDefaultReportScheduler()),
62+
CHIP_NO_ERROR);
6563
Access::SetAccessControl(gPermissiveAccessControl);
66-
VerifyOrDieWithMsg((err = Access::GetAccessControl().Init(chip::Access::Examples::GetPermissiveAccessControlDelegate(),
67-
gDeviceTypeResolver)) == CHIP_NO_ERROR,
68-
AppServer, "Init AccessControl failed: %" CHIP_ERROR_FORMAT, err.Format());
64+
ASSERT_EQ(Access::GetAccessControl().Init(chip::Access::Examples::GetPermissiveAccessControlDelegate(), gDeviceTypeResolver),
65+
CHIP_NO_ERROR);
6966
}
7067

7168
void AppContext::TearDown()
7269
{
7370
Access::GetAccessControl().Finish();
7471
Access::ResetAccessControlToDefault();
7572
chip::app::InteractionModelEngine::GetInstance()->Shutdown();
73+
7674
LoopbackMessagingContext::TearDown();
7775
}
7876

src/app/tests/AppTestContext.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ class AppContext : public LoopbackMessagingContext
3232
// Performs shared teardown for all tests in the test suite
3333
static void TearDownTestSuite();
3434
// Performs setup for each individual test in the test suite
35-
void SetUp();
35+
virtual void SetUp();
3636
// Performs teardown for each individual test in the test suite
37-
void TearDown();
37+
virtual void TearDown();
3838
};
3939

4040
} // namespace Test

src/controller/tests/TestEventCaching.cpp

+22-55
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
* limitations under the License.
1717
*/
1818

19-
#include <gtest/gtest.h>
20-
2119
#include "app-common/zap-generated/ids/Attributes.h"
2220
#include "app-common/zap-generated/ids/Clusters.h"
2321
#include "app/ClusterStateCache.h"
@@ -37,7 +35,6 @@
3735
#include <lib/support/TimeUtils.h>
3836
#include <lib/support/UnitTestUtils.h>
3937
#include <lib/support/logging/CHIPLogging.h>
40-
#include <messaging/tests/MessagingContext.h>
4138

4239
using namespace chip;
4340
using namespace chip::app;
@@ -50,42 +47,17 @@ static uint8_t gInfoEventBuffer[4096];
5047
static uint8_t gCritEventBuffer[4096];
5148
static chip::app::CircularEventBuffer gCircularEventBuffer[3];
5249

53-
using TestContext = chip::Test::AppContext;
54-
5550
//
5651
// The generated endpoint_config for the controller app has Endpoint 1
5752
// already used in the fixed endpoint set of size 1. Consequently, let's use the next
5853
// number higher than that for our dynamic test endpoint.
5954
//
6055
constexpr EndpointId kTestEndpointId = 2;
6156

62-
class TestEventCaching : public ::testing::Test
57+
class TestEventCaching : public Test::AppContext
6358
{
64-
public:
65-
// Performs shared setup for all tests in the test suite
66-
static void SetUpTestSuite()
67-
{
68-
if (mpContext == nullptr)
69-
{
70-
mpContext = new TestContext();
71-
ASSERT_NE(mpContext, nullptr);
72-
}
73-
mpContext->SetUpTestSuite();
74-
}
75-
76-
// Performs shared teardown for all tests in the test suite
77-
static void TearDownTestSuite()
78-
{
79-
mpContext->TearDownTestSuite();
80-
if (mpContext != nullptr)
81-
{
82-
delete mpContext;
83-
mpContext = nullptr;
84-
}
85-
}
86-
8759
protected:
88-
// Performs setup for each test in the suite
60+
// Performs setup for each test in the suite. Run once for each test function.
8961
void SetUp()
9062
{
9163
const chip::app::LogStorageResources logStorageResources[] = {
@@ -94,29 +66,24 @@ class TestEventCaching : public ::testing::Test
9466
{ &gCritEventBuffer[0], sizeof(gCritEventBuffer), chip::app::PriorityLevel::Critical },
9567
};
9668

97-
mpContext->SetUp();
69+
AppContext::SetUp(); // Call parent.
70+
VerifyOrReturn(!HasFailure()); // Stop if parent had a failure.
9871

99-
CHIP_ERROR err = CHIP_NO_ERROR;
100-
// TODO: use ASSERT_EQ, once transition to pw_unit_test is complete
101-
VerifyOrDieWithMsg((err = mEventCounter.Init(0)) == CHIP_NO_ERROR, AppServer,
102-
"Init EventCounter failed: %" CHIP_ERROR_FORMAT, err.Format());
103-
chip::app::EventManagement::CreateEventManagement(&mpContext->GetExchangeManager(), ArraySize(logStorageResources),
72+
ASSERT_EQ(mEventCounter.Init(0), CHIP_NO_ERROR);
73+
chip::app::EventManagement::CreateEventManagement(&GetExchangeManager(), ArraySize(logStorageResources),
10474
gCircularEventBuffer, logStorageResources, &mEventCounter);
10575
}
10676

107-
// Performs teardown for each test in the suite
77+
// Performs teardown for each test in the suite. Run once for each test function.
10878
void TearDown()
10979
{
11080
chip::app::EventManagement::DestroyEventManagement();
111-
mpContext->TearDown();
81+
AppContext::TearDown(); // Call parent.
11282
}
11383

114-
static TestContext * mpContext;
115-
11684
private:
11785
MonotonicallyIncreasingCounter<EventNumber> mEventCounter;
11886
};
119-
TestContext * TestEventCaching::mpContext = nullptr;
12087

12188
//clang-format off
12289
DECLARE_DYNAMIC_ATTRIBUTE_LIST_BEGIN(testClusterAttrs)
@@ -179,7 +146,7 @@ void GenerateEvents(chip::EventNumber & firstEventNumber, chip::EventNumber & la
179146
*/
180147
TEST_F(TestEventCaching, TestBasicCaching)
181148
{
182-
auto sessionHandle = mpContext->GetSessionBobToAlice();
149+
auto sessionHandle = GetSessionBobToAlice();
183150
app::InteractionModelEngine * engine = app::InteractionModelEngine::GetInstance();
184151

185152
// Initialize the ember side server logic
@@ -207,12 +174,12 @@ TEST_F(TestEventCaching, TestBasicCaching)
207174
TestReadCallback readCallback;
208175

209176
{
210-
app::ReadClient readClient(engine, &mpContext->GetExchangeManager(),
211-
readCallback.mClusterCacheAdapter.GetBufferedCallback(), app::ReadClient::InteractionType::Read);
177+
app::ReadClient readClient(engine, &GetExchangeManager(), readCallback.mClusterCacheAdapter.GetBufferedCallback(),
178+
app::ReadClient::InteractionType::Read);
212179

213180
EXPECT_EQ(readClient.SendRequest(readParams), CHIP_NO_ERROR);
214181

215-
mpContext->DrainAndServiceIO();
182+
DrainAndServiceIO();
216183

217184
uint8_t generationCount = 0;
218185
readCallback.mClusterCacheAdapter.ForEachEventData(
@@ -340,12 +307,12 @@ TEST_F(TestEventCaching, TestBasicCaching)
340307
GenerateEvents(firstEventNumber, lastEventNumber);
341308

342309
{
343-
app::ReadClient readClient(engine, &mpContext->GetExchangeManager(),
344-
readCallback.mClusterCacheAdapter.GetBufferedCallback(), app::ReadClient::InteractionType::Read);
310+
app::ReadClient readClient(engine, &GetExchangeManager(), readCallback.mClusterCacheAdapter.GetBufferedCallback(),
311+
app::ReadClient::InteractionType::Read);
345312

346313
EXPECT_EQ(readClient.SendRequest(readParams), CHIP_NO_ERROR);
347314

348-
mpContext->DrainAndServiceIO();
315+
DrainAndServiceIO();
349316

350317
//
351318
// Validate that we still have all 5 of the old events we received, as well as the new ones that just got generated.
@@ -392,8 +359,8 @@ TEST_F(TestEventCaching, TestBasicCaching)
392359
// we don't receive events lower than that value.
393360
//
394361
{
395-
app::ReadClient readClient(engine, &mpContext->GetExchangeManager(),
396-
readCallback.mClusterCacheAdapter.GetBufferedCallback(), app::ReadClient::InteractionType::Read);
362+
app::ReadClient readClient(engine, &GetExchangeManager(), readCallback.mClusterCacheAdapter.GetBufferedCallback(),
363+
app::ReadClient::InteractionType::Read);
397364

398365
readCallback.mClusterCacheAdapter.ClearEventCache();
399366
constexpr EventNumber kLastSeenEventNumber = 3;
@@ -405,7 +372,7 @@ TEST_F(TestEventCaching, TestBasicCaching)
405372

406373
EXPECT_EQ(readClient.SendRequest(readParams), CHIP_NO_ERROR);
407374

408-
mpContext->DrainAndServiceIO();
375+
DrainAndServiceIO();
409376

410377
// We should only get events with event numbers larger than kHighestEventNumberSeen.
411378
EXPECT_EQ(readCallback.mEventsSeen, lastEventNumber - kLastSeenEventNumber);
@@ -441,12 +408,12 @@ TEST_F(TestEventCaching, TestBasicCaching)
441408

442409
{
443410
readParams.mEventNumber.SetValue(5);
444-
app::ReadClient readClient(engine, &mpContext->GetExchangeManager(),
445-
readCallback.mClusterCacheAdapter.GetBufferedCallback(), app::ReadClient::InteractionType::Read);
411+
app::ReadClient readClient(engine, &GetExchangeManager(), readCallback.mClusterCacheAdapter.GetBufferedCallback(),
412+
app::ReadClient::InteractionType::Read);
446413
readCallback.mClusterCacheAdapter.ClearEventCache(true);
447414
EXPECT_EQ(readClient.SendRequest(readParams), CHIP_NO_ERROR);
448415

449-
mpContext->DrainAndServiceIO();
416+
DrainAndServiceIO();
450417

451418
//
452419
// Validate that we would receive 5 events
@@ -474,7 +441,7 @@ TEST_F(TestEventCaching, TestBasicCaching)
474441
EXPECT_TRUE(highestEventNumber.HasValue() && highestEventNumber.Value() == 9);
475442
}
476443

477-
EXPECT_EQ(mpContext->GetExchangeManager().GetNumActiveExchanges(), 0u);
444+
EXPECT_EQ(GetExchangeManager().GetNumActiveExchanges(), 0u);
478445

479446
emberAfClearDynamicEndpoint(0);
480447
}

0 commit comments

Comments
 (0)