Skip to content

Commit 444dfd5

Browse files
committed
Add unit test for factory reset event
Signed-off-by: Adrian Gielniewski <adrian.gielniewski@nordicsemi.no>
1 parent 882b55c commit 444dfd5

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

src/app/tests/BUILD.gn

+1
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ chip_test_suite("tests") {
241241
"TestReadInteraction.cpp",
242242
"TestReportScheduler.cpp",
243243
"TestReportingEngine.cpp",
244+
"TestServer.cpp",
244245
"TestStatusIB.cpp",
245246
"TestStatusResponseMessage.cpp",
246247
"TestTestEventTriggerDelegate.cpp",

src/app/tests/TestServer.cpp

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
*
3+
* Copyright (c) 2025 Project CHIP Authors
4+
* All rights reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#include <pw_unit_test/framework.h>
20+
21+
#include <app/server/Server.h>
22+
#include <platform/CHIPDeviceLayer.h>
23+
24+
namespace chip {
25+
26+
using namespace chip::DeviceLayer;
27+
28+
namespace app {
29+
namespace server {
30+
31+
class TestServer : public ::testing::Test
32+
{
33+
public:
34+
static void SetUpTestSuite()
35+
{
36+
ASSERT_EQ(Platform::MemoryInit(), CHIP_NO_ERROR);
37+
ASSERT_EQ(PlatformMgr().InitChipStack(), CHIP_NO_ERROR);
38+
}
39+
static void TearDownTestSuite()
40+
{
41+
Platform::MemoryShutdown();
42+
PlatformMgr().Shutdown();
43+
}
44+
};
45+
46+
class TestEventHandler
47+
{
48+
public:
49+
ChipDeviceEvent mEvent{};
50+
51+
static void EventHandler(const ChipDeviceEvent * event, intptr_t arg)
52+
{
53+
reinterpret_cast<TestEventHandler *>(arg)->mEvent = *event;
54+
}
55+
};
56+
57+
TEST_F(TestServer, TestFactoryResetEvent)
58+
{
59+
TestEventHandler handler;
60+
PlatformMgr().AddEventHandler(TestEventHandler::EventHandler, reinterpret_cast<intptr_t>(&handler));
61+
62+
auto & server = Server::GetInstance();
63+
64+
server.ScheduleFactoryReset();
65+
66+
PlatformMgr().ScheduleWork([](intptr_t) -> void { PlatformMgr().StopEventLoopTask(); }, (intptr_t) nullptr);
67+
PlatformMgr().RunEventLoop();
68+
69+
EXPECT_EQ(handler.mEvent.Type, DeviceEventType::kFactoryReset);
70+
71+
PlatformMgr().RemoveEventHandler(TestEventHandler::EventHandler, reinterpret_cast<intptr_t>(&handler));
72+
}
73+
74+
} // namespace server
75+
} // namespace app
76+
} // namespace chip

src/test_driver/nrfconnect/prj.conf

+4
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,7 @@ CONFIG_CHIP_ENABLE_READ_CLIENT=y
8383
CONFIG_CHIP_DEVICE_VENDOR_ID=65521
8484
CONFIG_CHIP_DEVICE_PRODUCT_ID=32768
8585
CONFIG_CHIP_PROJECT_CONFIG="main/include/CHIPProjectConfig.h"
86+
87+
# Don't erase all settings as it deinitializes NVS/ZMS and causes issues with
88+
# testing Server::ScheduleFactoryReset
89+
CONFIG_CHIP_FACTORY_RESET_ERASE_SETTINGS=n

0 commit comments

Comments
 (0)