Skip to content

Commit 3062e91

Browse files
[ICD] Convert TestICDMonitoringTable to gTest (#33239)
* Move tests to icd/server/tests * Remove App dependency from the TestICDManager and build it on its own * Convert TestICDMonitoringTable to gtest * Fix build and address review comments * Add define guards in case some builds dont use TCP / UDP endpoints
1 parent 4b58ad2 commit 3062e91

File tree

10 files changed

+514
-528
lines changed

10 files changed

+514
-528
lines changed

src/BUILD.gn

+2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ if (chip_build_tests) {
6666
"${chip_root}/src/protocols/interaction_model/tests",
6767
"${chip_root}/src/protocols/user_directed_commissioning/tests",
6868
"${chip_root}/src/transport/retransmit/tests",
69+
"${chip_root}/src/app/icd/server/tests",
70+
"${chip_root}/src/app/icd/server/tests:tests_nltest",
6971
]
7072

7173
# Skip DNSSD tests for Mbed platform due to flash memory size limitations

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

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright (c) 2024 Project CHIP Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import("//build_overrides/build.gni")
16+
import("//build_overrides/chip.gni")
17+
import("//build_overrides/nlunit_test.gni")
18+
import("//build_overrides/pigweed.gni")
19+
import("${chip_root}/build/chip/chip_test_suite.gni")
20+
import("${chip_root}/src/app/icd/icd.gni")
21+
22+
chip_test_suite_using_nltest("tests_nltest") {
23+
output_name = "libICDServerTestsNL"
24+
25+
test_sources = [ "TestICDManager.cpp" ]
26+
27+
public_deps = [
28+
"${chip_root}/src/app/icd/server:manager",
29+
"${chip_root}/src/app/icd/server:monitoring-table",
30+
"${chip_root}/src/lib/support:test_utils",
31+
"${chip_root}/src/lib/support:testing",
32+
"${chip_root}/src/lib/support:testing_nlunit",
33+
"${chip_root}/src/messaging/tests:helpers",
34+
"${nlunit_test_root}:nlunit-test",
35+
]
36+
}
37+
38+
chip_test_suite("tests") {
39+
output_name = "libICDServerTests"
40+
41+
test_sources = [ "TestICDMonitoringTable.cpp" ]
42+
43+
public_deps = [
44+
"${chip_root}/src/app/icd/server:monitoring-table",
45+
"${chip_root}/src/lib/support:test_utils",
46+
"${chip_root}/src/lib/support:testing",
47+
]
48+
49+
cflags = [ "-Wconversion" ]
50+
}

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

+28-18
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,25 @@
1515
* See the License for the specific language governing permissions and
1616
* limitations under the License.
1717
*/
18-
#include <app/EventManagement.h>
1918
#include <app/SubscriptionsInfoProvider.h>
2019
#include <app/TestEventTriggerDelegate.h>
2120
#include <app/icd/server/ICDConfigurationData.h>
2221
#include <app/icd/server/ICDManager.h>
22+
#include <app/icd/server/ICDMonitoringTable.h>
2323
#include <app/icd/server/ICDNotifier.h>
2424
#include <app/icd/server/ICDStateObserver.h>
25-
#include <app/tests/AppTestContext.h>
25+
#include <crypto/DefaultSessionKeystore.h>
2626
#include <lib/core/DataModelTypes.h>
2727
#include <lib/core/NodeId.h>
2828
#include <lib/support/TestPersistentStorageDelegate.h>
2929
#include <lib/support/TimeUtils.h>
3030
#include <lib/support/UnitTestContext.h>
3131
#include <lib/support/UnitTestExtendedAssertions.h>
3232
#include <lib/support/UnitTestRegistration.h>
33+
#include <messaging/tests/MessagingContext.h>
3334
#include <nlunit-test.h>
3435
#include <system/SystemLayerImpl.h>
3536

36-
#include <crypto/DefaultSessionKeystore.h>
37-
3837
using namespace chip;
3938
using namespace chip::app;
4039
using namespace chip::System;
@@ -119,16 +118,19 @@ class TestSubscriptionsInfoProvider : public SubscriptionsInfoProvider
119118
bool mHasPersistedSubscription = false;
120119
};
121120

122-
class TestContext : public chip::Test::AppContext
121+
class TestContext : public chip::Test::LoopbackMessagingContext
123122
{
124123
public:
125124
// Performs shared setup for all tests in the test suite
126125
CHIP_ERROR SetUpTestSuite() override
127126
{
128-
ReturnErrorOnFailure(chip::Test::AppContext::SetUpTestSuite());
127+
ReturnErrorOnFailure(LoopbackMessagingContext::SetUpTestSuite());
128+
ReturnErrorOnFailure(chip::DeviceLayer::PlatformMgr().InitChipStack());
129+
129130
DeviceLayer::SetSystemLayerForTesting(&GetSystemLayer());
130131
mRealClock = &chip::System::SystemClock();
131132
System::Clock::Internal::SetSystemClockForTesting(&mMockClock);
133+
132134
return CHIP_NO_ERROR;
133135
}
134136

@@ -137,24 +139,28 @@ class TestContext : public chip::Test::AppContext
137139
{
138140
System::Clock::Internal::SetSystemClockForTesting(mRealClock);
139141
DeviceLayer::SetSystemLayerForTesting(nullptr);
140-
chip::Test::AppContext::TearDownTestSuite();
142+
143+
chip::DeviceLayer::PlatformMgr().Shutdown();
144+
LoopbackMessagingContext::TearDownTestSuite();
141145
}
142146

143147
// Performs setup for each individual test in the test suite
144148
CHIP_ERROR SetUp() override
145149
{
146-
ReturnErrorOnFailure(chip::Test::AppContext::SetUp());
150+
ReturnErrorOnFailure(LoopbackMessagingContext::SetUp());
151+
147152
mICDStateObserver.ResetAll();
148153
mICDManager.RegisterObserver(&mICDStateObserver);
149154
mICDManager.Init(&testStorage, &GetFabricTable(), &mKeystore, &GetExchangeManager(), &mSubInfoProvider);
155+
150156
return CHIP_NO_ERROR;
151157
}
152158

153159
// Performs teardown for each individual test in the test suite
154160
void TearDown() override
155161
{
156162
mICDManager.Shutdown();
157-
chip::Test::AppContext::TearDown();
163+
LoopbackMessagingContext::TearDown();
158164
}
159165

160166
System::Clock::Internal::MockClock mMockClock;
@@ -653,6 +659,7 @@ class TestICDManager
653659
NL_TEST_ASSERT(aSuite, stayActivePromisedMs == 20000);
654660
}
655661

662+
#if CHIP_CONFIG_ENABLE_ICD_CIP
656663
#if CHIP_CONFIG_PERSIST_SUBSCRIPTIONS
657664
#if CHIP_CONFIG_SUBSCRIPTION_TIMEOUT_RESUMPTION
658665
static void TestShouldCheckInMsgsBeSentAtActiveModeFunction(nlTestSuite * aSuite, void * aContext)
@@ -723,6 +730,7 @@ class TestICDManager
723730
NL_TEST_ASSERT(aSuite, ctx->mICDManager.ShouldCheckInMsgsBeSentAtActiveModeFunction(kTestFabricIndex1, kClientNodeId11));
724731
}
725732
#endif // CHIP_CONFIG_PERSIST_SUBSCRIPTIONS
733+
#endif // CHIP_CONFIG_ENABLE_ICD_CIP
726734

727735
static void TestHandleTestEventTriggerActiveModeReq(nlTestSuite * aSuite, void * aContext)
728736
{
@@ -1117,27 +1125,29 @@ namespace {
11171125
static const nlTest sTests[] = {
11181126
NL_TEST_DEF("TestICDModeDurations", TestICDManager::TestICDModeDurations),
11191127
NL_TEST_DEF("TestOnSubscriptionReport", TestICDManager::TestOnSubscriptionReport),
1120-
NL_TEST_DEF("TestICDModeDurationsWith0ActiveModeDurationWithoutActiveSub",
1121-
TestICDManager::TestICDModeDurationsWith0ActiveModeDurationWithoutActiveSub),
1122-
NL_TEST_DEF("TestICDModeDurationsWith0ActiveModeDurationWithActiveSub",
1123-
TestICDManager::TestICDModeDurationsWith0ActiveModeDurationWithActiveSub),
11241128
NL_TEST_DEF("TestKeepActivemodeRequests", TestICDManager::TestKeepActivemodeRequests),
1125-
NL_TEST_DEF("TestICDMRegisterUnregisterEvents", TestICDManager::TestICDMRegisterUnregisterEvents),
1126-
NL_TEST_DEF("TestICDCounter", TestICDManager::TestICDCounter),
11271129
NL_TEST_DEF("TestICDStayActive", TestICDManager::TestICDMStayActive),
1130+
#if CHIP_CONFIG_ENABLE_ICD_CIP
1131+
NL_TEST_DEF("TestICDCounter", TestICDManager::TestICDCounter),
1132+
NL_TEST_DEF("TestICDMRegisterUnregisterEvents", TestICDManager::TestICDMRegisterUnregisterEvents),
1133+
NL_TEST_DEF("TestICDModeDurationsWith0ActiveModeDurationWithActiveSub",
1134+
TestICDManager::TestICDModeDurationsWith0ActiveModeDurationWithActiveSub),
1135+
NL_TEST_DEF("TestICDModeDurationsWith0ActiveModeDurationWithoutActiveSub",
1136+
TestICDManager::TestICDModeDurationsWith0ActiveModeDurationWithoutActiveSub),
11281137
NL_TEST_DEF("TestShouldCheckInMsgsBeSentAtActiveModeFunction", TestICDManager::TestShouldCheckInMsgsBeSentAtActiveModeFunction),
1129-
NL_TEST_DEF("TestHandleTestEventTriggerActiveModeReq", TestICDManager::TestHandleTestEventTriggerActiveModeReq),
11301138
NL_TEST_DEF("TestHandleTestEventTriggerInvalidateHalfCounterValues",
11311139
TestICDManager::TestHandleTestEventTriggerInvalidateHalfCounterValues),
11321140
NL_TEST_DEF("TestHandleTestEventTriggerInvalidateAllCounterValues",
11331141
TestICDManager::TestHandleTestEventTriggerInvalidateAllCounterValues),
1142+
NL_TEST_DEF("TestICDStateObserverOnICDModeChange", TestICDManager::TestICDStateObserverOnICDModeChange),
1143+
NL_TEST_DEF("TestICDStateObserverOnICDModeChangeOnInit", TestICDManager::TestICDStateObserverOnICDModeChangeOnInit),
1144+
#endif // CHIP_CONFIG_ENABLE_ICD_CIP
1145+
NL_TEST_DEF("TestHandleTestEventTriggerActiveModeReq", TestICDManager::TestHandleTestEventTriggerActiveModeReq),
11341146
NL_TEST_DEF("TestICDStateObserverOnEnterIdleModeActiveModeDuration",
11351147
TestICDManager::TestICDStateObserverOnEnterIdleModeActiveModeDuration),
11361148
NL_TEST_DEF("TestICDStateObserverOnEnterIdleModeActiveModeThreshold",
11371149
TestICDManager::TestICDStateObserverOnEnterIdleModeActiveModeThreshold),
11381150
NL_TEST_DEF("TestICDStateObserverOnEnterActiveMode", TestICDManager::TestICDStateObserverOnEnterActiveMode),
1139-
NL_TEST_DEF("TestICDStateObserverOnICDModeChange", TestICDManager::TestICDStateObserverOnICDModeChange),
1140-
NL_TEST_DEF("TestICDStateObserverOnICDModeChangeOnInit", TestICDManager::TestICDStateObserverOnICDModeChangeOnInit),
11411151
NL_TEST_DEF("TestICDStateObserverOnTransitionToIdleModeGreaterActiveModeDuration",
11421152
TestICDManager::TestICDStateObserverOnTransitionToIdleModeGreaterActiveModeDuration),
11431153
NL_TEST_DEF("TestICDStateObserverOnTransitionToIdleModeEqualActiveModeDuration",

0 commit comments

Comments
 (0)