Skip to content

Commit 018ef92

Browse files
pw_unit_test migration: apps final batch (#33772)
* pw_unit_test migration: apps 2nd batch * Restyled by clang-format * removing some NL_unit components * Adding RunEventLoop to testcases in TestCommissioningWindowManager * removing NL_test from OpenIoT SDK * integrating comments * Integrating Comments * Restyled by clang-format * removing pointless nullptr check from TestContext * Restyled by clang-format * remove commented-out code * Refactoring macro to test-ineraction-model-api * Restyled by clang-format --------- Co-authored-by: Restyled.io <commits@restyled.io>
1 parent 61a2561 commit 018ef92

26 files changed

+4914
-6178
lines changed

src/BUILD.gn

-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ if (chip_build_tests) {
9797
if (chip_device_platform != "efr32") {
9898
tests += [
9999
"${chip_root}/src/app/tests",
100-
"${chip_root}/src/app/tests:tests_nltest",
101100
"${chip_root}/src/credentials/tests",
102101
"${chip_root}/src/lib/format/tests",
103102
"${chip_root}/src/lib/support/tests",

src/app/tests/BUILD.gn

+33-60
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ chip_test_suite("tests") {
141141
output_name = "libAppTests"
142142

143143
test_sources = [
144+
"TestAclAttribute.cpp",
145+
"TestAclEvent.cpp",
144146
"TestAttributeAccessInterfaceCache.cpp",
145147
"TestAttributePathExpandIterator.cpp",
146148
"TestAttributePathParams.cpp",
@@ -150,37 +152,34 @@ chip_test_suite("tests") {
150152
"TestBasicCommandPathRegistry.cpp",
151153
"TestBindingTable.cpp",
152154
"TestBuilderParser.cpp",
155+
"TestCommandInteraction.cpp",
153156
"TestCommandPathParams.cpp",
154157
"TestConcreteAttributePath.cpp",
155158
"TestDataModelSerialization.cpp",
156159
"TestDefaultOTARequestorStorage.cpp",
160+
"TestEventLoggingNoUTCTime.cpp",
161+
"TestEventOverflow.cpp",
157162
"TestEventPathParams.cpp",
163+
"TestFabricScopedEventLogging.cpp",
164+
"TestInteractionModelEngine.cpp",
158165
"TestMessageDef.cpp",
159166
"TestNullable.cpp",
160167
"TestNumericAttributeTraits.cpp",
161168
"TestOperationalStateClusterObjects.cpp",
162169
"TestPendingNotificationMap.cpp",
163170
"TestPendingResponseTrackerImpl.cpp",
164171
"TestPowerSourceCluster.cpp",
172+
"TestReadInteraction.cpp",
173+
"TestReportScheduler.cpp",
174+
"TestReportingEngine.cpp",
165175
"TestStatusIB.cpp",
166176
"TestStatusResponseMessage.cpp",
167177
"TestTestEventTriggerDelegate.cpp",
168178
"TestTimeSyncDataProvider.cpp",
179+
"TestTimedHandler.cpp",
180+
"TestWriteInteraction.cpp",
169181
]
170182

171-
if (!chip_fake_platform) {
172-
test_sources += [ "TestFailSafeContext.cpp" ]
173-
}
174-
175-
# DefaultICDClientStorage assumes that raw AES key is used by the application
176-
if (chip_crypto != "psa") {
177-
test_sources += [ "TestDefaultICDClientStorage.cpp" ]
178-
}
179-
180-
if (chip_persist_subscriptions) {
181-
test_sources += [ "TestSimpleSubscriptionResumptionStorage.cpp" ]
182-
}
183-
184183
cflags = [ "-Wconversion" ]
185184

186185
public_deps = [
@@ -199,6 +198,7 @@ chip_test_suite("tests") {
199198
"${chip_root}/src/lib/core:string-builder-adapters",
200199
"${chip_root}/src/lib/support:test_utils",
201200
"${chip_root}/src/lib/support:testing",
201+
"${chip_root}/src/lib/support/tests:pw-test-macros",
202202
]
203203

204204
if (chip_device_platform != "android") {
@@ -211,27 +211,29 @@ chip_test_suite("tests") {
211211
":scenes-table-test-srcs",
212212
]
213213
}
214-
}
215214

216-
chip_test_suite_using_nltest("tests_nltest") {
217-
output_name = "libAppTestsNL"
215+
if (chip_config_network_layer_ble &&
216+
(chip_device_platform == "linux" || chip_device_platform == "darwin")) {
217+
test_sources += [ "TestCommissioningWindowManager.cpp" ]
218+
public_deps += [
219+
"${chip_root}/src/app/server",
220+
"${chip_root}/src/messaging/tests/echo:common",
221+
]
222+
}
218223

219-
test_sources = [
220-
"TestAclAttribute.cpp",
221-
"TestAclEvent.cpp",
222-
"TestCommandInteraction.cpp",
223-
"TestEventLoggingNoUTCTime.cpp",
224-
"TestEventOverflow.cpp",
225-
"TestFabricScopedEventLogging.cpp",
226-
"TestInteractionModelEngine.cpp",
227-
"TestReadInteraction.cpp",
228-
"TestReportScheduler.cpp",
229-
"TestReportingEngine.cpp",
230-
"TestTimedHandler.cpp",
231-
"TestWriteInteraction.cpp",
232-
]
224+
if (!chip_fake_platform) {
225+
test_sources += [ "TestFailSafeContext.cpp" ]
226+
}
227+
228+
# DefaultICDClientStorage assumes that raw AES key is used by the application
229+
if (chip_crypto != "psa") {
230+
test_sources += [ "TestDefaultICDClientStorage.cpp" ]
231+
}
232+
233+
if (chip_persist_subscriptions) {
234+
test_sources += [ "TestSimpleSubscriptionResumptionStorage.cpp" ]
235+
}
233236

234-
#
235237
# On NRF platforms, the allocation of a large number of pbufs in this test
236238
# to exercise chunking causes it to run out of memory. For now, disable it there.
237239
#
@@ -248,33 +250,4 @@ chip_test_suite_using_nltest("tests_nltest") {
248250
chip_device_platform != "openiotsdk" && chip_device_platform != "fake") {
249251
test_sources += [ "TestEventLogging.cpp" ]
250252
}
251-
252-
cflags = [ "-Wconversion" ]
253-
254-
public_deps = [
255-
":app-test-stubs",
256-
":binding-test-srcs",
257-
":operational-state-test-srcs",
258-
":ota-requestor-test-srcs",
259-
":time-sync-data-provider-test-srcs",
260-
"${chip_root}/src/app",
261-
"${chip_root}/src/app/common:cluster-objects",
262-
"${chip_root}/src/app/icd/client:manager",
263-
"${chip_root}/src/app/tests:helpers",
264-
"${chip_root}/src/app/util/mock:mock_ember",
265-
"${chip_root}/src/lib/core",
266-
"${chip_root}/src/lib/support:test_utils",
267-
"${chip_root}/src/lib/support:testing",
268-
"${chip_root}/src/lib/support:testing_nlunit",
269-
"${nlunit_test_root}:nlunit-test",
270-
]
271-
272-
if (chip_config_network_layer_ble &&
273-
(chip_device_platform == "linux" || chip_device_platform == "darwin")) {
274-
test_sources += [ "TestCommissionManager.cpp" ]
275-
public_deps += [
276-
"${chip_root}/src/app/server",
277-
"${chip_root}/src/messaging/tests/echo:common",
278-
]
279-
}
280253
}

src/app/tests/TestAclAttribute.cpp

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

19+
#include <lib/core/StringBuilderAdapters.h>
20+
#include <pw_unit_test/framework.h>
21+
1922
#include <access/examples/PermissiveAccessControlDelegate.h>
2023
#include <app/ConcreteAttributePath.h>
2124
#include <app/ConcreteEventPath.h>
@@ -34,11 +37,8 @@
3437
#include <lib/core/TLVDebug.h>
3538
#include <lib/core/TLVUtilities.h>
3639
#include <lib/support/CHIPCounter.h>
37-
#include <lib/support/UnitTestContext.h>
38-
#include <lib/support/UnitTestRegistration.h>
3940
#include <messaging/ExchangeContext.h>
4041
#include <messaging/Flags.h>
41-
#include <nlunit-test.h>
4242
#include <protocols/interaction_model/Constants.h>
4343

4444
#include <type_traits>
@@ -73,18 +73,6 @@ class TestDeviceTypeResolver : public AccessControl::DeviceTypeResolver
7373
bool IsDeviceTypeOnEndpoint(DeviceTypeId deviceType, EndpointId endpoint) override { return false; }
7474
} gDeviceTypeResolver;
7575

76-
class TestAccessContext : public chip::Test::AppContext
77-
{
78-
public:
79-
// Performs setup for each individual test in the test suite
80-
void SetUp() override
81-
{
82-
chip::Test::AppContext::SetUp();
83-
Access::GetAccessControl().Finish();
84-
Access::GetAccessControl().Init(GetTestAccessControlDelegate(), gDeviceTypeResolver);
85-
}
86-
};
87-
8876
class MockInteractionModelApp : public chip::app::ReadClient::Callback
8977
{
9078
public:
@@ -121,29 +109,50 @@ class MockInteractionModelApp : public chip::app::ReadClient::Callback
121109
namespace chip {
122110
namespace app {
123111

124-
class TestAclAttribute
112+
class TestAclAttribute : public ::testing::Test
125113
{
126114
public:
127-
static void TestACLDeniedAttribute(nlTestSuite * apSuite, void * apContext);
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+
127+
void SetUp() override
128+
{
129+
mpTestContext->SetUp();
130+
131+
Access::GetAccessControl().Finish();
132+
Access::GetAccessControl().Init(GetTestAccessControlDelegate(), gDeviceTypeResolver);
133+
}
134+
void TearDown() override { mpTestContext->TearDown(); }
135+
136+
static chip::Test::AppContext * mpTestContext;
128137
};
129138

139+
chip::Test::AppContext * TestAclAttribute::mpTestContext = nullptr;
140+
130141
// Read Client sends a malformed subscribe request, interaction model engine fails to parse the request and generates a status
131142
// report to client, and client is closed.
132-
void TestAclAttribute::TestACLDeniedAttribute(nlTestSuite * apSuite, void * apContext)
143+
TEST_F(TestAclAttribute, TestACLDeniedAttribute)
133144
{
134-
TestAccessContext & ctx = *static_cast<TestAccessContext *>(apContext);
135-
CHIP_ERROR err = CHIP_NO_ERROR;
136-
137-
Messaging::ReliableMessageMgr * rm = ctx.GetExchangeManager().GetReliableMessageMgr();
138-
NL_TEST_ASSERT(apSuite, rm->TestGetCountRetransTable() == 0);
145+
Messaging::ReliableMessageMgr * rm = mpTestContext->GetExchangeManager().GetReliableMessageMgr();
146+
EXPECT_EQ(rm->TestGetCountRetransTable(), 0);
139147

140148
MockInteractionModelApp delegate;
141149
auto * engine = chip::app::InteractionModelEngine::GetInstance();
142-
err = engine->Init(&ctx.GetExchangeManager(), &ctx.GetFabricTable(), app::reporting::GetDefaultReportScheduler());
143-
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
150+
EXPECT_EQ(engine->Init(&mpTestContext->GetExchangeManager(), &mpTestContext->GetFabricTable(),
151+
app::reporting::GetDefaultReportScheduler()),
152+
CHIP_NO_ERROR);
144153

145154
{
146-
app::ReadClient readClient(chip::app::InteractionModelEngine::GetInstance(), &ctx.GetExchangeManager(), delegate,
155+
app::ReadClient readClient(chip::app::InteractionModelEngine::GetInstance(), &mpTestContext->GetExchangeManager(), delegate,
147156
chip::app::ReadClient::InteractionType::Subscribe);
148157

149158
chip::app::AttributePathParams attributePathParams[2];
@@ -155,22 +164,21 @@ void TestAclAttribute::TestACLDeniedAttribute(nlTestSuite * apSuite, void * apCo
155164
attributePathParams[1].mClusterId = chip::Test::kTestDeniedClusterId1;
156165
attributePathParams[1].mAttributeId = 2;
157166

158-
ReadPrepareParams readPrepareParams(ctx.GetSessionBobToAlice());
167+
ReadPrepareParams readPrepareParams(mpTestContext->GetSessionBobToAlice());
159168
readPrepareParams.mpAttributePathParamsList = attributePathParams;
160169
readPrepareParams.mAttributePathParamsListSize = 2;
161170

162-
err = readClient.SendRequest(readPrepareParams);
163-
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
171+
EXPECT_EQ(readClient.SendRequest(readPrepareParams), CHIP_NO_ERROR);
164172

165-
ctx.DrainAndServiceIO();
166-
NL_TEST_ASSERT(apSuite, delegate.mError == CHIP_IM_GLOBAL_STATUS(InvalidAction));
167-
NL_TEST_ASSERT(apSuite, !delegate.mGotReport);
173+
mpTestContext->DrainAndServiceIO();
174+
EXPECT_EQ(delegate.mError, CHIP_IM_GLOBAL_STATUS(InvalidAction));
175+
EXPECT_FALSE(delegate.mGotReport);
168176
delegate.mError = CHIP_NO_ERROR;
169177
delegate.mGotReport = false;
170178
}
171179

172180
{
173-
app::ReadClient readClient(chip::app::InteractionModelEngine::GetInstance(), &ctx.GetExchangeManager(), delegate,
181+
app::ReadClient readClient(chip::app::InteractionModelEngine::GetInstance(), &mpTestContext->GetExchangeManager(), delegate,
174182
chip::app::ReadClient::InteractionType::Subscribe);
175183

176184
chip::app::AttributePathParams attributePathParams[2];
@@ -181,22 +189,21 @@ void TestAclAttribute::TestACLDeniedAttribute(nlTestSuite * apSuite, void * apCo
181189
attributePathParams[1].mClusterId = chip::Test::kTestDeniedClusterId2;
182190
attributePathParams[1].mAttributeId = 2;
183191

184-
ReadPrepareParams readPrepareParams(ctx.GetSessionBobToAlice());
192+
ReadPrepareParams readPrepareParams(mpTestContext->GetSessionBobToAlice());
185193
readPrepareParams.mpAttributePathParamsList = attributePathParams;
186194
readPrepareParams.mAttributePathParamsListSize = 2;
187195

188-
err = readClient.SendRequest(readPrepareParams);
189-
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
196+
EXPECT_EQ(readClient.SendRequest(readPrepareParams), CHIP_NO_ERROR);
190197

191-
ctx.DrainAndServiceIO();
192-
NL_TEST_ASSERT(apSuite, delegate.mError == CHIP_IM_GLOBAL_STATUS(InvalidAction));
193-
NL_TEST_ASSERT(apSuite, !delegate.mGotReport);
198+
mpTestContext->DrainAndServiceIO();
199+
EXPECT_EQ(delegate.mError, CHIP_IM_GLOBAL_STATUS(InvalidAction));
200+
EXPECT_FALSE(delegate.mGotReport);
194201
delegate.mError = CHIP_NO_ERROR;
195202
delegate.mGotReport = false;
196203
}
197204

198205
{
199-
app::ReadClient readClient(chip::app::InteractionModelEngine::GetInstance(), &ctx.GetExchangeManager(), delegate,
206+
app::ReadClient readClient(chip::app::InteractionModelEngine::GetInstance(), &mpTestContext->GetExchangeManager(), delegate,
200207
chip::app::ReadClient::InteractionType::Subscribe);
201208

202209
chip::app::AttributePathParams attributePathParams[2];
@@ -208,49 +215,23 @@ void TestAclAttribute::TestACLDeniedAttribute(nlTestSuite * apSuite, void * apCo
208215
attributePathParams[1].mClusterId = chip::Test::kTestClusterId;
209216
attributePathParams[1].mAttributeId = 2;
210217

211-
ReadPrepareParams readPrepareParams(ctx.GetSessionBobToAlice());
218+
ReadPrepareParams readPrepareParams(mpTestContext->GetSessionBobToAlice());
212219
readPrepareParams.mpAttributePathParamsList = attributePathParams;
213220
readPrepareParams.mAttributePathParamsListSize = 2;
214221

215-
err = readClient.SendRequest(readPrepareParams);
216-
NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
222+
EXPECT_EQ(readClient.SendRequest(readPrepareParams), CHIP_NO_ERROR);
217223

218-
ctx.DrainAndServiceIO();
219-
NL_TEST_ASSERT(apSuite, delegate.mError == CHIP_NO_ERROR);
220-
NL_TEST_ASSERT(apSuite, delegate.mGotReport);
221-
NL_TEST_ASSERT(apSuite, engine->GetNumActiveReadHandlers(ReadHandler::InteractionType::Subscribe) == 1);
224+
mpTestContext->DrainAndServiceIO();
225+
EXPECT_EQ(delegate.mError, CHIP_NO_ERROR);
226+
EXPECT_TRUE(delegate.mGotReport);
227+
EXPECT_EQ(engine->GetNumActiveReadHandlers(ReadHandler::InteractionType::Subscribe), 1u);
222228
delegate.mError = CHIP_NO_ERROR;
223229
delegate.mGotReport = false;
224230
}
225231

226-
NL_TEST_ASSERT(apSuite, engine->GetNumActiveReadClients() == 0);
232+
EXPECT_EQ(engine->GetNumActiveReadClients(), 0u);
227233
engine->Shutdown();
228-
NL_TEST_ASSERT(apSuite, ctx.GetExchangeManager().GetNumActiveExchanges() == 0);
234+
EXPECT_EQ(mpTestContext->GetExchangeManager().GetNumActiveExchanges(), 0u);
229235
}
230236
} // namespace app
231237
} // namespace chip
232-
233-
namespace {
234-
235-
const nlTest sTests[] = {
236-
NL_TEST_DEF("TestACLDeniedAttribute", chip::app::TestAclAttribute::TestACLDeniedAttribute),
237-
NL_TEST_SENTINEL(),
238-
};
239-
240-
nlTestSuite sSuite = {
241-
"TestAclAttribute",
242-
&sTests[0],
243-
NL_TEST_WRAP_FUNCTION(TestAccessContext::SetUpTestSuite),
244-
NL_TEST_WRAP_FUNCTION(TestAccessContext::TearDownTestSuite),
245-
NL_TEST_WRAP_METHOD(TestAccessContext, SetUp),
246-
NL_TEST_WRAP_METHOD(TestAccessContext, TearDown),
247-
};
248-
249-
} // namespace
250-
251-
int TestAclAttribute()
252-
{
253-
return chip::ExecuteTestsWithContext<TestAccessContext>(&sSuite);
254-
}
255-
256-
CHIP_REGISTER_TEST_SUITE(TestAclAttribute)

0 commit comments

Comments
 (0)