Skip to content

Commit f213189

Browse files
feasel0restyled-commits
authored andcommitted
Updated AppContext, LoopbackMessagingContext, and UDPMessagingContext to serve as PW test fixture classes. (project-chip#34036)
* Modified MessagingContext and its subclasses as well as AppContext to reflect PW migration. * Modifed all tests that use LoopbackMessagingContext or AppContext. Moved LoopbackMessagingContext data to heap. * Removed unneeded scope from call to parent setup/teardown * Made some helper functions into class methods. * Restyled by prettier-markdown * Reverted TestICDManager back to using LoopbackMessagingContext * fixing merge conflicts * fixing merge conflicts * Fixed duplicate code and missing semicolon * Trying to update TestReadHandler_DataVersionFiltersTruncated * Restyled by whitespace * Restyled by clang-format * fixed problem with the new TestReadHandler_DataVersionFiltersTruncated * fix in tracing * Changes to MessagingContext member variable initialization in response to code review * Used std::unique_ptr for MessagingContext.mpData --------- Co-authored-by: Restyled.io <commits@restyled.io>
1 parent 3c43488 commit f213189

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1953
-2713
lines changed

docs/testing/unit_testing.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,8 @@ your overriding function make sure to check `HasFailure()` and return if the
218218
parent function failed.
219219

220220
If you don't override any of the setup/teardown functions, you can simply make a
221-
type alias: `using YourTestContext = Test::AppContextPW;` instead of defining
222-
your own text context class.
221+
type alias: `using YourTestContext = Test::AppContext;` instead of defining your
222+
own text context class.
223223

224224
## Best practices
225225

src/app/icd/server/tests/TestICDManager.cpp

+17-29
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* See the License for the specific language governing permissions and
1616
* limitations under the License.
1717
*/
18+
#include <pw_unit_test/framework.h>
19+
1820
#include <app/SubscriptionsInfoProvider.h>
1921
#include <app/TestEventTriggerDelegate.h>
2022
#include <app/icd/server/ICDConfigurationData.h>
@@ -24,7 +26,6 @@
2426
#include <app/icd/server/ICDStateObserver.h>
2527
#include <app/icd/server/tests/ICDConfigurationDataTestAccess.h>
2628
#include <crypto/DefaultSessionKeystore.h>
27-
#include <gtest/gtest.h>
2829
#include <lib/address_resolve/AddressResolve.h>
2930
#include <lib/core/DataModelTypes.h>
3031
#include <lib/core/NodeId.h>
@@ -124,18 +125,16 @@ class TestSubscriptionsInfoProvider : public SubscriptionsInfoProvider
124125
bool mHasPersistedSubscription = false;
125126
};
126127

127-
System::Clock::Internal::MockClock * pMockClock = nullptr;
128-
System::Clock::ClockBase * pRealClock = nullptr;
129-
chip::Test::LoopbackMessagingContext * pMessagingContext = nullptr;
128+
System::Clock::Internal::MockClock * pMockClock = nullptr;
129+
System::Clock::ClockBase * pRealClock = nullptr;
130130

131131
} // namespace
132132

133133
namespace chip {
134134
namespace app {
135135

136-
class TestICDManager : public ::testing::Test
136+
class TestICDManager : public Test::LoopbackMessagingContext
137137
{
138-
139138
public:
140139
/*
141140
* Advance the test Mock clock time by the amout passed in argument
@@ -147,7 +146,7 @@ class TestICDManager : public ::testing::Test
147146
static void AdvanceClockAndRunEventLoop(Milliseconds64 time)
148147
{
149148
pMockClock->AdvanceMonotonic(time);
150-
pMessagingContext->GetIOContext().DriveIO();
149+
GetIOContext().DriveIO();
151150
}
152151

153152
// Performs shared setup for all tests in the test suite
@@ -159,16 +158,12 @@ class TestICDManager : public ::testing::Test
159158
ASSERT_NE(pMockClock, nullptr);
160159
}
161160

162-
if (pMessagingContext == nullptr)
163-
{
164-
pMessagingContext = new LoopbackMessagingContext();
165-
ASSERT_NE(pMessagingContext, nullptr);
166-
}
161+
LoopbackMessagingContext::SetUpTestSuite();
162+
VerifyOrReturn(!HasFailure());
167163

168-
pMessagingContext->SetUpTestSuite();
169164
ASSERT_EQ(chip::DeviceLayer::PlatformMgr().InitChipStack(), CHIP_NO_ERROR);
170165

171-
DeviceLayer::SetSystemLayerForTesting(&(pMessagingContext->GetSystemLayer()));
166+
DeviceLayer::SetSystemLayerForTesting(&GetSystemLayer());
172167
pRealClock = &SystemClock();
173168
Clock::Internal::SetSystemClockForTesting(pMockClock);
174169
}
@@ -180,39 +175,34 @@ class TestICDManager : public ::testing::Test
180175
DeviceLayer::SetSystemLayerForTesting(nullptr);
181176

182177
DeviceLayer::PlatformMgr().Shutdown();
183-
pMessagingContext->TearDownTestSuite();
178+
179+
LoopbackMessagingContext::TearDownTestSuite();
184180

185181
if (pMockClock != nullptr)
186182
{
187183
delete pMockClock;
188184
pMockClock = nullptr;
189185
}
190186

191-
if (pMessagingContext != nullptr)
192-
{
193-
delete pMessagingContext;
194-
pMessagingContext = nullptr;
195-
}
196-
197187
pRealClock = nullptr;
198188
}
199189

200190
// Performs setup for each individual test in the test suite
201191
void SetUp() override
202192
{
203-
pMessagingContext->SetUp();
193+
LoopbackMessagingContext::SetUp();
194+
VerifyOrReturn(!HasFailure());
204195

205196
mICDStateObserver.ResetAll();
206197
mICDManager.RegisterObserver(&mICDStateObserver);
207-
mICDManager.Init(&testStorage, &(pMessagingContext->GetFabricTable()), &mKeystore,
208-
&(pMessagingContext->GetExchangeManager()), &mSubInfoProvider);
198+
mICDManager.Init(&testStorage, &GetFabricTable(), &mKeystore, &GetExchangeManager(), &mSubInfoProvider);
209199
}
210200

211201
// Performs teardown for each individual test in the test suite
212202
void TearDown() override
213203
{
214204
mICDManager.Shutdown();
215-
pMessagingContext->TearDown();
205+
LoopbackMessagingContext::TearDown();
216206
}
217207

218208
TestSessionKeystoreImpl mKeystore;
@@ -576,8 +566,7 @@ TEST_F(TestICDManager, TestICDCounter)
576566

577567
// Shut down and reinit ICDManager to increment counter
578568
mICDManager.Shutdown();
579-
mICDManager.Init(&(testStorage), &(pMessagingContext->GetFabricTable()), &(mKeystore),
580-
&(pMessagingContext->GetExchangeManager()), &(mSubInfoProvider));
569+
mICDManager.Init(&(testStorage), &GetFabricTable(), &(mKeystore), &GetExchangeManager(), &(mSubInfoProvider));
581570
mICDManager.RegisterObserver(&(mICDStateObserver));
582571

583572
EXPECT_EQ(counter + ICDConfigurationData::kICDCounterPersistenceIncrement,
@@ -982,8 +971,7 @@ TEST_F(TestICDManager, TestICDStateObserverOnICDModeChangeOnInit)
982971
// Shut down and reinit ICDManager - We should go to LIT mode since we have a registration
983972
mICDManager.Shutdown();
984973
mICDManager.RegisterObserver(&(mICDStateObserver));
985-
mICDManager.Init(&testStorage, &(pMessagingContext->GetFabricTable()), &mKeystore, &(pMessagingContext->GetExchangeManager()),
986-
&mSubInfoProvider);
974+
mICDManager.Init(&testStorage, &GetFabricTable(), &mKeystore, &GetExchangeManager(), &mSubInfoProvider);
987975

988976
// We have a registration, transition to LIT mode
989977
EXPECT_TRUE(mICDStateObserver.mOnICDModeChangeCalled);

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()
@@ -67,24 +66,23 @@ void AppContext::TearDownTestSuite()
6766

6867
void AppContext::SetUp()
6968
{
70-
CHIP_ERROR err = CHIP_NO_ERROR;
7169
LoopbackMessagingContext::SetUp();
72-
// TODO: use ASSERT_EQ, once transition to pw_unit_test is complete
73-
VerifyOrDieWithMsg((err = app::InteractionModelEngine::GetInstance()->Init(&GetExchangeManager(), &GetFabricTable(),
74-
app::reporting::GetDefaultReportScheduler())) ==
75-
CHIP_NO_ERROR,
76-
AppServer, "Init InteractionModelEngine failed: %" CHIP_ERROR_FORMAT, err.Format());
70+
VerifyOrReturn(!HasFailure()); // Stop if parent had a failure.
71+
72+
ASSERT_EQ(app::InteractionModelEngine::GetInstance()->Init(&GetExchangeManager(), &GetFabricTable(),
73+
app::reporting::GetDefaultReportScheduler()),
74+
CHIP_NO_ERROR);
7775
Access::SetAccessControl(gPermissiveAccessControl);
78-
VerifyOrDieWithMsg((err = Access::GetAccessControl().Init(chip::Access::Examples::GetPermissiveAccessControlDelegate(),
79-
gDeviceTypeResolver)) == CHIP_NO_ERROR,
80-
AppServer, "Init AccessControl failed: %" CHIP_ERROR_FORMAT, err.Format());
76+
ASSERT_EQ(Access::GetAccessControl().Init(chip::Access::Examples::GetPermissiveAccessControlDelegate(), gDeviceTypeResolver),
77+
CHIP_NO_ERROR);
8178
}
8279

8380
void AppContext::TearDown()
8481
{
8582
Access::GetAccessControl().Finish();
8683
Access::ResetAccessControlToDefault();
8784
chip::app::InteractionModelEngine::GetInstance()->Shutdown();
85+
8886
LoopbackMessagingContext::TearDown();
8987
}
9088

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/app/tests/BUILD.gn

+4-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ static_library("helpers") {
4040
"${chip_root}/src/transport/raw/tests:helpers",
4141
]
4242

43-
public_deps = [ "${chip_root}/src/messaging/tests:helpers" ]
43+
public_deps = [
44+
"${chip_root}/src/lib/support/tests:pw-test-macros",
45+
"${chip_root}/src/messaging/tests:helpers",
46+
]
4447
}
4548

4649
source_set("binding-test-srcs") {

src/app/tests/TestAclAttribute.cpp

+14-33
Original file line numberDiff line numberDiff line change
@@ -109,50 +109,31 @@ class MockInteractionModelApp : public chip::app::ReadClient::Callback
109109
namespace chip {
110110
namespace app {
111111

112-
class TestAclAttribute : public ::testing::Test
112+
class TestAclAttribute : public Test::AppContext
113113
{
114114
public:
115-
static void SetUpTestSuite()
116-
{
117-
118-
mpTestContext = new chip::Test::AppContext;
119-
mpTestContext->SetUpTestSuite();
120-
}
121-
static void TearDownTestSuite()
122-
{
123-
mpTestContext->TearDownTestSuite();
124-
delete mpTestContext;
125-
}
126-
127115
void SetUp() override
128116
{
129-
mpTestContext->SetUp();
117+
AppContext::SetUp();
130118

131119
Access::GetAccessControl().Finish();
132120
Access::GetAccessControl().Init(GetTestAccessControlDelegate(), gDeviceTypeResolver);
133121
}
134-
void TearDown() override { mpTestContext->TearDown(); }
135-
136-
static chip::Test::AppContext * mpTestContext;
137122
};
138123

139-
chip::Test::AppContext * TestAclAttribute::mpTestContext = nullptr;
140-
141124
// Read Client sends a malformed subscribe request, interaction model engine fails to parse the request and generates a status
142125
// report to client, and client is closed.
143126
TEST_F(TestAclAttribute, TestACLDeniedAttribute)
144127
{
145-
Messaging::ReliableMessageMgr * rm = mpTestContext->GetExchangeManager().GetReliableMessageMgr();
128+
Messaging::ReliableMessageMgr * rm = GetExchangeManager().GetReliableMessageMgr();
146129
EXPECT_EQ(rm->TestGetCountRetransTable(), 0);
147130

148131
MockInteractionModelApp delegate;
149132
auto * engine = chip::app::InteractionModelEngine::GetInstance();
150-
EXPECT_EQ(engine->Init(&mpTestContext->GetExchangeManager(), &mpTestContext->GetFabricTable(),
151-
app::reporting::GetDefaultReportScheduler()),
152-
CHIP_NO_ERROR);
133+
EXPECT_EQ(engine->Init(&GetExchangeManager(), &GetFabricTable(), app::reporting::GetDefaultReportScheduler()), CHIP_NO_ERROR);
153134

154135
{
155-
app::ReadClient readClient(chip::app::InteractionModelEngine::GetInstance(), &mpTestContext->GetExchangeManager(), delegate,
136+
app::ReadClient readClient(chip::app::InteractionModelEngine::GetInstance(), &GetExchangeManager(), delegate,
156137
chip::app::ReadClient::InteractionType::Subscribe);
157138

158139
chip::app::AttributePathParams attributePathParams[2];
@@ -164,21 +145,21 @@ TEST_F(TestAclAttribute, TestACLDeniedAttribute)
164145
attributePathParams[1].mClusterId = chip::Test::kTestDeniedClusterId1;
165146
attributePathParams[1].mAttributeId = 2;
166147

167-
ReadPrepareParams readPrepareParams(mpTestContext->GetSessionBobToAlice());
148+
ReadPrepareParams readPrepareParams(GetSessionBobToAlice());
168149
readPrepareParams.mpAttributePathParamsList = attributePathParams;
169150
readPrepareParams.mAttributePathParamsListSize = 2;
170151

171152
EXPECT_EQ(readClient.SendRequest(readPrepareParams), CHIP_NO_ERROR);
172153

173-
mpTestContext->DrainAndServiceIO();
154+
DrainAndServiceIO();
174155
EXPECT_EQ(delegate.mError, CHIP_IM_GLOBAL_STATUS(InvalidAction));
175156
EXPECT_FALSE(delegate.mGotReport);
176157
delegate.mError = CHIP_NO_ERROR;
177158
delegate.mGotReport = false;
178159
}
179160

180161
{
181-
app::ReadClient readClient(chip::app::InteractionModelEngine::GetInstance(), &mpTestContext->GetExchangeManager(), delegate,
162+
app::ReadClient readClient(chip::app::InteractionModelEngine::GetInstance(), &GetExchangeManager(), delegate,
182163
chip::app::ReadClient::InteractionType::Subscribe);
183164

184165
chip::app::AttributePathParams attributePathParams[2];
@@ -189,21 +170,21 @@ TEST_F(TestAclAttribute, TestACLDeniedAttribute)
189170
attributePathParams[1].mClusterId = chip::Test::kTestDeniedClusterId2;
190171
attributePathParams[1].mAttributeId = 2;
191172

192-
ReadPrepareParams readPrepareParams(mpTestContext->GetSessionBobToAlice());
173+
ReadPrepareParams readPrepareParams(GetSessionBobToAlice());
193174
readPrepareParams.mpAttributePathParamsList = attributePathParams;
194175
readPrepareParams.mAttributePathParamsListSize = 2;
195176

196177
EXPECT_EQ(readClient.SendRequest(readPrepareParams), CHIP_NO_ERROR);
197178

198-
mpTestContext->DrainAndServiceIO();
179+
DrainAndServiceIO();
199180
EXPECT_EQ(delegate.mError, CHIP_IM_GLOBAL_STATUS(InvalidAction));
200181
EXPECT_FALSE(delegate.mGotReport);
201182
delegate.mError = CHIP_NO_ERROR;
202183
delegate.mGotReport = false;
203184
}
204185

205186
{
206-
app::ReadClient readClient(chip::app::InteractionModelEngine::GetInstance(), &mpTestContext->GetExchangeManager(), delegate,
187+
app::ReadClient readClient(chip::app::InteractionModelEngine::GetInstance(), &GetExchangeManager(), delegate,
207188
chip::app::ReadClient::InteractionType::Subscribe);
208189

209190
chip::app::AttributePathParams attributePathParams[2];
@@ -215,13 +196,13 @@ TEST_F(TestAclAttribute, TestACLDeniedAttribute)
215196
attributePathParams[1].mClusterId = chip::Test::kTestClusterId;
216197
attributePathParams[1].mAttributeId = 2;
217198

218-
ReadPrepareParams readPrepareParams(mpTestContext->GetSessionBobToAlice());
199+
ReadPrepareParams readPrepareParams(GetSessionBobToAlice());
219200
readPrepareParams.mpAttributePathParamsList = attributePathParams;
220201
readPrepareParams.mAttributePathParamsListSize = 2;
221202

222203
EXPECT_EQ(readClient.SendRequest(readPrepareParams), CHIP_NO_ERROR);
223204

224-
mpTestContext->DrainAndServiceIO();
205+
DrainAndServiceIO();
225206
EXPECT_EQ(delegate.mError, CHIP_NO_ERROR);
226207
EXPECT_TRUE(delegate.mGotReport);
227208
EXPECT_EQ(engine->GetNumActiveReadHandlers(ReadHandler::InteractionType::Subscribe), 1u);
@@ -231,7 +212,7 @@ TEST_F(TestAclAttribute, TestACLDeniedAttribute)
231212

232213
EXPECT_EQ(engine->GetNumActiveReadClients(), 0u);
233214
engine->Shutdown();
234-
EXPECT_EQ(mpTestContext->GetExchangeManager().GetNumActiveExchanges(), 0u);
215+
EXPECT_EQ(GetExchangeManager().GetNumActiveExchanges(), 0u);
235216
}
236217
} // namespace app
237218
} // namespace chip

0 commit comments

Comments
 (0)