Skip to content

Commit d43b087

Browse files
committed
Modifed all tests that use LoopbackMessagingContext or AppContext. Moved LoopbackMessagingContext data to heap.
1 parent 1980820 commit d43b087

39 files changed

+1705
-2405
lines changed

docs/testing/unit_testing.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ 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
221+
type alias: `using YourTestContext = Test::AppContext;` instead of defining
222222
your own text context class.
223223

224224
## Best practices

src/app/icd/server/tests/BUILD.gn

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ chip_test_suite("tests") {
3131
public_deps = [
3232
"${chip_root}/src/app/icd/server:manager",
3333
"${chip_root}/src/app/icd/server:monitoring-table",
34+
"${chip_root}/src/app/tests:helpers",
35+
"${chip_root}/src/controller/data_model",
3436
"${chip_root}/src/lib/support:test_utils",
3537
"${chip_root}/src/lib/support:testing",
3638
"${chip_root}/src/messaging/tests:helpers",

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

+17-33
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>
@@ -23,13 +25,12 @@
2325
#include <app/icd/server/ICDNotifier.h>
2426
#include <app/icd/server/ICDStateObserver.h>
2527
#include <app/icd/server/tests/ICDConfigurationDataTestAccess.h>
28+
#include <app/tests/AppTestContext.h>
2629
#include <crypto/DefaultSessionKeystore.h>
27-
#include <gtest/gtest.h>
2830
#include <lib/core/DataModelTypes.h>
2931
#include <lib/core/NodeId.h>
3032
#include <lib/support/TestPersistentStorageDelegate.h>
3133
#include <lib/support/TimeUtils.h>
32-
#include <messaging/tests/MessagingContext.h>
3334
#include <system/SystemLayerImpl.h>
3435

3536
using namespace chip;
@@ -122,18 +123,16 @@ class TestSubscriptionsInfoProvider : public SubscriptionsInfoProvider
122123
bool mHasPersistedSubscription = false;
123124
};
124125

125-
System::Clock::Internal::MockClock * pMockClock = nullptr;
126-
System::Clock::ClockBase * pRealClock = nullptr;
127-
chip::Test::LoopbackMessagingContext * pMessagingContext = nullptr;
126+
System::Clock::Internal::MockClock * pMockClock = nullptr;
127+
System::Clock::ClockBase * pRealClock = nullptr;
128128

129129
} // namespace
130130

131131
namespace chip {
132132
namespace app {
133133

134-
class TestICDManager : public ::testing::Test
134+
class TestICDManager : public Test::AppContext
135135
{
136-
137136
public:
138137
/*
139138
* Advance the test Mock clock time by the amout passed in argument
@@ -145,7 +144,7 @@ class TestICDManager : public ::testing::Test
145144
static void AdvanceClockAndRunEventLoop(Milliseconds64 time)
146145
{
147146
pMockClock->AdvanceMonotonic(time);
148-
pMessagingContext->GetIOContext().DriveIO();
147+
GetIOContext().DriveIO();
149148
}
150149

151150
// Performs shared setup for all tests in the test suite
@@ -157,16 +156,10 @@ class TestICDManager : public ::testing::Test
157156
ASSERT_NE(pMockClock, nullptr);
158157
}
159158

160-
if (pMessagingContext == nullptr)
161-
{
162-
pMessagingContext = new LoopbackMessagingContext();
163-
ASSERT_NE(pMessagingContext, nullptr);
164-
}
165-
166-
pMessagingContext->SetUpTestSuite();
167-
ASSERT_EQ(chip::DeviceLayer::PlatformMgr().InitChipStack(), CHIP_NO_ERROR);
159+
AppContext::SetUpTestSuite();
160+
VerifyOrReturn(!HasFailure());
168161

169-
DeviceLayer::SetSystemLayerForTesting(&(pMessagingContext->GetSystemLayer()));
162+
DeviceLayer::SetSystemLayerForTesting(&GetSystemLayer());
170163
pRealClock = &SystemClock();
171164
Clock::Internal::SetSystemClockForTesting(pMockClock);
172165
}
@@ -177,40 +170,33 @@ class TestICDManager : public ::testing::Test
177170
Clock::Internal::SetSystemClockForTesting(pRealClock);
178171
DeviceLayer::SetSystemLayerForTesting(nullptr);
179172

180-
DeviceLayer::PlatformMgr().Shutdown();
181-
pMessagingContext->TearDownTestSuite();
173+
AppContext::TearDownTestSuite();
182174

183175
if (pMockClock != nullptr)
184176
{
185177
delete pMockClock;
186178
pMockClock = nullptr;
187179
}
188180

189-
if (pMessagingContext != nullptr)
190-
{
191-
delete pMessagingContext;
192-
pMessagingContext = nullptr;
193-
}
194-
195181
pRealClock = nullptr;
196182
}
197183

198184
// Performs setup for each individual test in the test suite
199185
void SetUp() override
200186
{
201-
pMessagingContext->SetUp();
187+
AppContext::SetUp();
188+
VerifyOrReturn(!HasFailure());
202189

203190
mICDStateObserver.ResetAll();
204191
mICDManager.RegisterObserver(&mICDStateObserver);
205-
mICDManager.Init(&testStorage, &(pMessagingContext->GetFabricTable()), &mKeystore,
206-
&(pMessagingContext->GetExchangeManager()), &mSubInfoProvider);
192+
mICDManager.Init(&testStorage, &GetFabricTable(), &mKeystore, &GetExchangeManager(), &mSubInfoProvider);
207193
}
208194

209195
// Performs teardown for each individual test in the test suite
210196
void TearDown() override
211197
{
212198
mICDManager.Shutdown();
213-
pMessagingContext->TearDown();
199+
AppContext::TearDown();
214200
}
215201

216202
TestSessionKeystoreImpl mKeystore;
@@ -574,8 +560,7 @@ TEST_F(TestICDManager, TestICDCounter)
574560

575561
// Shut down and reinit ICDManager to increment counter
576562
mICDManager.Shutdown();
577-
mICDManager.Init(&(testStorage), &(pMessagingContext->GetFabricTable()), &(mKeystore),
578-
&(pMessagingContext->GetExchangeManager()), &(mSubInfoProvider));
563+
mICDManager.Init(&(testStorage), &GetFabricTable(), &(mKeystore), &GetExchangeManager(), &(mSubInfoProvider));
579564
mICDManager.RegisterObserver(&(mICDStateObserver));
580565

581566
EXPECT_EQ(counter + ICDConfigurationData::kICDCounterPersistenceIncrement,
@@ -980,8 +965,7 @@ TEST_F(TestICDManager, TestICDStateObserverOnICDModeChangeOnInit)
980965
// Shut down and reinit ICDManager - We should go to LIT mode since we have a registration
981966
mICDManager.Shutdown();
982967
mICDManager.RegisterObserver(&(mICDStateObserver));
983-
mICDManager.Init(&testStorage, &(pMessagingContext->GetFabricTable()), &mKeystore, &(pMessagingContext->GetExchangeManager()),
984-
&mSubInfoProvider);
968+
mICDManager.Init(&testStorage, &GetFabricTable(), &mKeystore, &GetExchangeManager(), &mSubInfoProvider);
985969

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

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 chip::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+
chip::Test::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)