Skip to content

Commit 5dd912f

Browse files
authored
pw_unit_test migration: apps-1 and stub-refactoring (#33638)
* Decoupling ember functions + some tests * Fix TearDown issue of some App Tests * adding more tests * Integrating comments * Rename ember-test-compatibility to test-interaction-model-api * adding ember test-utilities source files * restyled patch * integrating comments * restyled
1 parent 8a64366 commit 5dd912f

32 files changed

+2323
-2921
lines changed

src/BUILD.gn

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ if (chip_build_tests) {
9696
if (chip_device_platform != "efr32") {
9797
tests += [
9898
"${chip_root}/src/app/tests",
99+
"${chip_root}/src/app/tests:tests_nltest",
99100
"${chip_root}/src/credentials/tests",
100101
"${chip_root}/src/lib/format/tests",
101102
"${chip_root}/src/lib/support/tests",

src/app/tests/BUILD.gn

+71-25
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import("//build_overrides/build.gni")
1616
import("//build_overrides/chip.gni")
1717
import("//build_overrides/nlunit_test.gni")
18+
import("//build_overrides/pigweed.gni")
1819

1920
import("${chip_root}/build/chip/chip_test_suite.gni")
2021
import("${chip_root}/src/app/icd/icd.gni")
@@ -37,9 +38,10 @@ static_library("helpers") {
3738
"${chip_root}/src/access",
3839
"${chip_root}/src/app",
3940
"${chip_root}/src/lib/support",
40-
"${chip_root}/src/messaging/tests:helpers",
4141
"${chip_root}/src/transport/raw/tests:helpers",
4242
]
43+
44+
public_deps = [ "${chip_root}/src/messaging/tests:helpers" ]
4345
}
4446

4547
source_set("binding-test-srcs") {
@@ -119,47 +121,44 @@ source_set("operational-state-test-srcs") {
119121
]
120122
}
121123

122-
chip_test_suite_using_nltest("tests") {
124+
source_set("app-test-stubs") {
125+
sources = [
126+
"test-ember-api.cpp",
127+
"test-ember-api.h",
128+
"test-interaction-model-api.cpp",
129+
"test-interaction-model-api.h",
130+
]
131+
public_configs = [ "${chip_root}/src/lib/support/pw_log_chip:config" ]
132+
133+
public_deps = [
134+
"${chip_root}/src/app/util/mock:mock_ember",
135+
"${chip_root}/src/lib/core",
136+
"${chip_root}/src/lib/support",
137+
]
138+
}
139+
140+
chip_test_suite("tests") {
123141
output_name = "libAppTests"
124142

125143
test_sources = [
126-
"TestAclAttribute.cpp",
127-
"TestAclEvent.cpp",
128144
"TestAttributeAccessInterfaceCache.cpp",
129145
"TestAttributePathExpandIterator.cpp",
130146
"TestAttributePersistenceProvider.cpp",
131147
"TestAttributeValueDecoder.cpp",
132148
"TestAttributeValueEncoder.cpp",
133-
"TestBasicCommandPathRegistry.cpp",
134149
"TestBindingTable.cpp",
135150
"TestBuilderParser.cpp",
136-
"TestClusterInfo.cpp",
137-
"TestCommandInteraction.cpp",
138-
"TestCommandPathParams.cpp",
139-
"TestConcreteAttributePath.cpp",
140-
"TestDataModelSerialization.cpp",
141-
"TestDefaultOTARequestorStorage.cpp",
142-
"TestEventLoggingNoUTCTime.cpp",
143-
"TestEventOverflow.cpp",
144-
"TestEventPathParams.cpp",
145-
"TestFabricScopedEventLogging.cpp",
146-
"TestInteractionModelEngine.cpp",
147151
"TestMessageDef.cpp",
148152
"TestNullable.cpp",
149153
"TestNumericAttributeTraits.cpp",
150154
"TestOperationalStateClusterObjects.cpp",
151155
"TestPendingNotificationMap.cpp",
152156
"TestPendingResponseTrackerImpl.cpp",
153157
"TestPowerSourceCluster.cpp",
154-
"TestReadInteraction.cpp",
155-
"TestReportScheduler.cpp",
156-
"TestReportingEngine.cpp",
157158
"TestStatusIB.cpp",
158159
"TestStatusResponseMessage.cpp",
159160
"TestTestEventTriggerDelegate.cpp",
160161
"TestTimeSyncDataProvider.cpp",
161-
"TestTimedHandler.cpp",
162-
"TestWriteInteraction.cpp",
163162
]
164163

165164
if (!chip_fake_platform) {
@@ -171,6 +170,56 @@ chip_test_suite_using_nltest("tests") {
171170
test_sources += [ "TestDefaultICDClientStorage.cpp" ]
172171
}
173172

173+
if (chip_persist_subscriptions) {
174+
test_sources += [ "TestSimpleSubscriptionResumptionStorage.cpp" ]
175+
}
176+
177+
cflags = [ "-Wconversion" ]
178+
179+
public_deps = [
180+
":app-test-stubs",
181+
":binding-test-srcs",
182+
":operational-state-test-srcs",
183+
":ota-requestor-test-srcs",
184+
":power-cluster-test-srcs",
185+
":time-sync-data-provider-test-srcs",
186+
"${chip_root}/src/app",
187+
"${chip_root}/src/app/common:cluster-objects",
188+
"${chip_root}/src/app/icd/client:manager",
189+
"${chip_root}/src/app/tests:helpers",
190+
"${chip_root}/src/app/util/mock:mock_ember",
191+
"${chip_root}/src/lib/core",
192+
"${chip_root}/src/lib/core:string-builder-adapters",
193+
"${chip_root}/src/lib/support:test_utils",
194+
"${chip_root}/src/lib/support:testing",
195+
]
196+
}
197+
198+
chip_test_suite_using_nltest("tests_nltest") {
199+
output_name = "libAppTestsNL"
200+
201+
test_sources = [
202+
"TestAclAttribute.cpp",
203+
"TestAclEvent.cpp",
204+
"TestBasicCommandPathRegistry.cpp",
205+
"TestClusterInfo.cpp",
206+
"TestCommandInteraction.cpp",
207+
"TestCommandPathParams.cpp",
208+
"TestConcreteAttributePath.cpp",
209+
"TestDataModelSerialization.cpp",
210+
"TestDefaultOTARequestorStorage.cpp",
211+
"TestEventLoggingNoUTCTime.cpp",
212+
"TestEventOverflow.cpp",
213+
"TestEventPathParams.cpp",
214+
"TestFabricScopedEventLogging.cpp",
215+
"TestInteractionModelEngine.cpp",
216+
"TestReadInteraction.cpp",
217+
"TestReportScheduler.cpp",
218+
"TestReportingEngine.cpp",
219+
"TestTimedHandler.cpp",
220+
"TestWriteInteraction.cpp",
221+
]
222+
174223
#
175224
# On NRF platforms, the allocation of a large number of pbufs in this test
176225
# to exercise chunking causes it to run out of memory. For now, disable it there.
@@ -192,6 +241,7 @@ chip_test_suite_using_nltest("tests") {
192241
cflags = [ "-Wconversion" ]
193242

194243
public_deps = [
244+
":app-test-stubs",
195245
":binding-test-srcs",
196246
":operational-state-test-srcs",
197247
":ota-requestor-test-srcs",
@@ -227,8 +277,4 @@ chip_test_suite_using_nltest("tests") {
227277
"${chip_root}/src/messaging/tests/echo:common",
228278
]
229279
}
230-
231-
if (chip_persist_subscriptions) {
232-
test_sources += [ "TestSimpleSubscriptionResumptionStorage.cpp" ]
233-
}
234280
}

src/app/tests/TestAclAttribute.cpp

+12-31
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <app/MessageDef/EventDataIB.h>
2525
#include <app/reporting/tests/MockReportScheduler.h>
2626
#include <app/tests/AppTestContext.h>
27+
#include <app/tests/test-interaction-model-api.h>
2728
#include <app/util/basic-types.h>
2829
#include <app/util/mock/Constants.h>
2930
#include <app/util/mock/Functions.h>
@@ -46,18 +47,13 @@ namespace {
4647
using namespace chip;
4748
using namespace chip::Access;
4849

49-
chip::ClusterId kTestClusterId = 1;
50-
chip::ClusterId kTestDeniedClusterId1 = 1000;
51-
chip::ClusterId kTestDeniedClusterId2 = 3;
52-
chip::EndpointId kTestEndpointId = 4;
53-
5450
class TestAccessControlDelegate : public AccessControl::Delegate
5551
{
5652
public:
5753
CHIP_ERROR Check(const SubjectDescriptor & subjectDescriptor, const chip::Access::RequestPath & requestPath,
5854
Privilege requestPrivilege) override
5955
{
60-
if (requestPath.cluster == kTestDeniedClusterId2)
56+
if (requestPath.cluster == chip::Test::kTestDeniedClusterId2)
6157
{
6258
return CHIP_ERROR_ACCESS_DENIED;
6359
}
@@ -125,21 +121,6 @@ class MockInteractionModelApp : public chip::app::ReadClient::Callback
125121
namespace chip {
126122
namespace app {
127123

128-
bool ConcreteAttributePathExists(const ConcreteAttributePath & aPath)
129-
{
130-
return aPath.mClusterId != kTestDeniedClusterId1;
131-
}
132-
133-
Protocols::InteractionModel::Status CheckEventSupportStatus(const ConcreteEventPath & aPath)
134-
{
135-
if (aPath.mClusterId == kTestDeniedClusterId1)
136-
{
137-
return Protocols::InteractionModel::Status::UnsupportedCluster;
138-
}
139-
140-
return Protocols::InteractionModel::Status::Success;
141-
}
142-
143124
class TestAclAttribute
144125
{
145126
public:
@@ -166,12 +147,12 @@ void TestAclAttribute::TestACLDeniedAttribute(nlTestSuite * apSuite, void * apCo
166147
chip::app::ReadClient::InteractionType::Subscribe);
167148

168149
chip::app::AttributePathParams attributePathParams[2];
169-
attributePathParams[0].mEndpointId = kTestEndpointId;
170-
attributePathParams[0].mClusterId = kTestDeniedClusterId1;
150+
attributePathParams[0].mEndpointId = chip::Test::kTestEndpointId;
151+
attributePathParams[0].mClusterId = chip::Test::kTestDeniedClusterId1;
171152
attributePathParams[0].mAttributeId = 1;
172153

173-
attributePathParams[1].mEndpointId = kTestEndpointId;
174-
attributePathParams[1].mClusterId = kTestDeniedClusterId1;
154+
attributePathParams[1].mEndpointId = chip::Test::kTestEndpointId;
155+
attributePathParams[1].mClusterId = chip::Test::kTestDeniedClusterId1;
175156
attributePathParams[1].mAttributeId = 2;
176157

177158
ReadPrepareParams readPrepareParams(ctx.GetSessionBobToAlice());
@@ -194,10 +175,10 @@ void TestAclAttribute::TestACLDeniedAttribute(nlTestSuite * apSuite, void * apCo
194175

195176
chip::app::AttributePathParams attributePathParams[2];
196177

197-
attributePathParams[0].mClusterId = kTestDeniedClusterId2;
178+
attributePathParams[0].mClusterId = chip::Test::kTestDeniedClusterId2;
198179
attributePathParams[0].mAttributeId = 1;
199180

200-
attributePathParams[1].mClusterId = kTestDeniedClusterId2;
181+
attributePathParams[1].mClusterId = chip::Test::kTestDeniedClusterId2;
201182
attributePathParams[1].mAttributeId = 2;
202183

203184
ReadPrepareParams readPrepareParams(ctx.GetSessionBobToAlice());
@@ -219,12 +200,12 @@ void TestAclAttribute::TestACLDeniedAttribute(nlTestSuite * apSuite, void * apCo
219200
chip::app::ReadClient::InteractionType::Subscribe);
220201

221202
chip::app::AttributePathParams attributePathParams[2];
222-
attributePathParams[0].mEndpointId = kTestEndpointId;
223-
attributePathParams[0].mClusterId = kTestDeniedClusterId1;
203+
attributePathParams[0].mEndpointId = chip::Test::kTestEndpointId;
204+
attributePathParams[0].mClusterId = chip::Test::kTestDeniedClusterId1;
224205
attributePathParams[0].mAttributeId = 1;
225206

226-
attributePathParams[1].mEndpointId = kTestEndpointId;
227-
attributePathParams[1].mClusterId = kTestClusterId;
207+
attributePathParams[1].mEndpointId = chip::Test::kTestEndpointId;
208+
attributePathParams[1].mClusterId = chip::Test::kTestClusterId;
228209
attributePathParams[1].mAttributeId = 2;
229210

230211
ReadPrepareParams readPrepareParams(ctx.GetSessionBobToAlice());

src/app/tests/TestAttributeAccessInterfaceCache.cpp

+30-58
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818

1919
#include <app/AttributeAccessInterface.h>
2020
#include <app/AttributeAccessInterfaceCache.h>
21-
#include <lib/support/UnitTestRegistration.h>
22-
#include <nlunit-test.h>
21+
#include <lib/core/StringBuilderAdapters.h>
22+
#include <pw_unit_test/framework.h>
2323

2424
using namespace chip;
2525
using namespace chip::app;
2626

2727
namespace {
2828

29-
void TestBasicLifecycle(nlTestSuite * inSuite, void * inContext)
29+
TEST(TestAttributeAccessInterfaceCache, TestBasicLifecycle)
3030
{
3131
using CacheResult = AttributeAccessInterfaceCache::CacheResult;
3232

@@ -44,86 +44,58 @@ void TestBasicLifecycle(nlTestSuite * inSuite, void * inContext)
4444
// Cache can keep track of at least 1 entry,
4545
AttributeAccessInterface * entry = nullptr;
4646

47-
NL_TEST_ASSERT(inSuite, cache.Get(1, 1, &entry) == CacheResult::kCacheMiss);
48-
NL_TEST_ASSERT(inSuite, entry == nullptr);
47+
EXPECT_EQ(cache.Get(1, 1, &entry), CacheResult::kCacheMiss);
48+
EXPECT_EQ(entry, nullptr);
4949
cache.MarkUsed(1, 1, accessor1);
5050

51-
NL_TEST_ASSERT(inSuite, cache.Get(1, 1, &entry) == CacheResult::kDefinitelyUsed);
52-
NL_TEST_ASSERT(inSuite, entry == accessor1);
51+
EXPECT_EQ(cache.Get(1, 1, &entry), CacheResult::kDefinitelyUsed);
52+
EXPECT_EQ(entry, accessor1);
5353

5454
entry = nullptr;
55-
NL_TEST_ASSERT(inSuite, cache.Get(1, 2, &entry) == CacheResult::kCacheMiss);
56-
NL_TEST_ASSERT(inSuite, entry == nullptr);
57-
NL_TEST_ASSERT(inSuite, cache.Get(2, 1, &entry) == CacheResult::kCacheMiss);
58-
NL_TEST_ASSERT(inSuite, entry == nullptr);
55+
EXPECT_EQ(cache.Get(1, 2, &entry), CacheResult::kCacheMiss);
56+
EXPECT_EQ(entry, nullptr);
57+
EXPECT_EQ(cache.Get(2, 1, &entry), CacheResult::kCacheMiss);
58+
EXPECT_EQ(entry, nullptr);
5959

6060
cache.MarkUsed(1, 2, accessor1);
6161

6262
entry = nullptr;
63-
NL_TEST_ASSERT(inSuite, cache.Get(1, 2, &entry) == CacheResult::kDefinitelyUsed);
64-
NL_TEST_ASSERT(inSuite, entry == accessor1);
65-
NL_TEST_ASSERT(inSuite, cache.Get(2, 1, &entry) == CacheResult::kCacheMiss);
63+
EXPECT_EQ(cache.Get(1, 2, &entry), CacheResult::kDefinitelyUsed);
64+
EXPECT_EQ(entry, accessor1);
65+
EXPECT_EQ(cache.Get(2, 1, &entry), CacheResult::kCacheMiss);
6666

6767
cache.MarkUsed(1, 2, accessor2);
6868

6969
entry = nullptr;
70-
NL_TEST_ASSERT(inSuite, cache.Get(1, 2, &entry) == CacheResult::kDefinitelyUsed);
71-
NL_TEST_ASSERT(inSuite, entry == accessor2);
70+
EXPECT_EQ(cache.Get(1, 2, &entry), CacheResult::kDefinitelyUsed);
71+
EXPECT_EQ(entry, accessor2);
7272
// The following should not crash (e.g. output not used if nullptr).
73-
NL_TEST_ASSERT(inSuite, cache.Get(1, 2, nullptr) == CacheResult::kDefinitelyUsed);
73+
EXPECT_EQ(cache.Get(1, 2, nullptr), CacheResult::kDefinitelyUsed);
7474

7575
// Setting used to nullptr == does not mark used.
7676
cache.MarkUsed(1, 2, nullptr);
7777
entry = nullptr;
78-
NL_TEST_ASSERT(inSuite, cache.Get(1, 2, &entry) == CacheResult::kCacheMiss);
79-
NL_TEST_ASSERT(inSuite, entry == nullptr);
78+
EXPECT_EQ(cache.Get(1, 2, &entry), CacheResult::kCacheMiss);
79+
EXPECT_EQ(entry, nullptr);
8080

8181
cache.Invalidate();
82-
NL_TEST_ASSERT(inSuite, cache.Get(1, 1, &entry) == CacheResult::kCacheMiss);
83-
NL_TEST_ASSERT(inSuite, entry == nullptr);
84-
NL_TEST_ASSERT(inSuite, cache.Get(1, 2, &entry) == CacheResult::kCacheMiss);
85-
NL_TEST_ASSERT(inSuite, cache.Get(2, 1, &entry) == CacheResult::kCacheMiss);
82+
EXPECT_EQ(cache.Get(1, 1, &entry), CacheResult::kCacheMiss);
83+
EXPECT_EQ(entry, nullptr);
84+
EXPECT_EQ(cache.Get(1, 2, &entry), CacheResult::kCacheMiss);
85+
EXPECT_EQ(cache.Get(2, 1, &entry), CacheResult::kCacheMiss);
8686

8787
// Marking unused works, keeps single entry, and is invalidated when invalidated fully.
88-
NL_TEST_ASSERT(inSuite, cache.Get(2, 2, nullptr) != CacheResult::kDefinitelyUnused);
89-
NL_TEST_ASSERT(inSuite, cache.Get(3, 3, nullptr) != CacheResult::kDefinitelyUnused);
88+
EXPECT_NE(cache.Get(2, 2, nullptr), CacheResult::kDefinitelyUnused);
89+
EXPECT_NE(cache.Get(3, 3, nullptr), CacheResult::kDefinitelyUnused);
9090
cache.MarkUnused(2, 2);
91-
NL_TEST_ASSERT(inSuite, cache.Get(2, 2, nullptr) == CacheResult::kDefinitelyUnused);
92-
NL_TEST_ASSERT(inSuite, cache.Get(3, 3, nullptr) != CacheResult::kDefinitelyUnused);
91+
EXPECT_EQ(cache.Get(2, 2, nullptr), CacheResult::kDefinitelyUnused);
92+
EXPECT_NE(cache.Get(3, 3, nullptr), CacheResult::kDefinitelyUnused);
9393

9494
cache.MarkUnused(3, 3);
95-
NL_TEST_ASSERT(inSuite, cache.Get(2, 2, nullptr) != CacheResult::kDefinitelyUnused);
96-
NL_TEST_ASSERT(inSuite, cache.Get(3, 3, nullptr) == CacheResult::kDefinitelyUnused);
95+
EXPECT_NE(cache.Get(2, 2, nullptr), CacheResult::kDefinitelyUnused);
96+
EXPECT_EQ(cache.Get(3, 3, nullptr), CacheResult::kDefinitelyUnused);
9797

9898
cache.Invalidate();
99-
NL_TEST_ASSERT(inSuite, cache.Get(3, 3, nullptr) != CacheResult::kDefinitelyUnused);
99+
EXPECT_NE(cache.Get(3, 3, nullptr), CacheResult::kDefinitelyUnused);
100100
}
101-
102-
// clang-format off
103-
const nlTest sTests[] =
104-
{
105-
NL_TEST_DEF("Basic AttributeAccessInterfaceCache lifecycle works", TestBasicLifecycle),
106-
NL_TEST_SENTINEL()
107-
};
108-
// clang-format on
109-
110101
} // namespace
111-
112-
int TestAttributeAccessInterfaceCache()
113-
{
114-
// clang-format off
115-
nlTestSuite theSuite =
116-
{
117-
"Test for AttributeAccessInterface cache utility",
118-
&sTests[0],
119-
nullptr,
120-
nullptr
121-
};
122-
// clang-format on
123-
124-
nlTestRunner(&theSuite, nullptr);
125-
126-
return (nlTestRunnerStats(&theSuite));
127-
}
128-
129-
CHIP_REGISTER_TEST_SUITE(TestAttributeAccessInterfaceCache)

0 commit comments

Comments
 (0)