Skip to content

Commit

Permalink
[ICD] Convert TestICDMonitoringTable to gTest (#33239)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mkardous-silabs authored Apr 30, 2024
1 parent 4b58ad2 commit 3062e91
Show file tree
Hide file tree
Showing 10 changed files with 514 additions and 528 deletions.
2 changes: 2 additions & 0 deletions src/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ if (chip_build_tests) {
"${chip_root}/src/protocols/interaction_model/tests",
"${chip_root}/src/protocols/user_directed_commissioning/tests",
"${chip_root}/src/transport/retransmit/tests",
"${chip_root}/src/app/icd/server/tests",
"${chip_root}/src/app/icd/server/tests:tests_nltest",
]

# Skip DNSSD tests for Mbed platform due to flash memory size limitations
Expand Down
50 changes: 50 additions & 0 deletions src/app/icd/server/tests/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright (c) 2024 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("//build_overrides/build.gni")
import("//build_overrides/chip.gni")
import("//build_overrides/nlunit_test.gni")
import("//build_overrides/pigweed.gni")
import("${chip_root}/build/chip/chip_test_suite.gni")
import("${chip_root}/src/app/icd/icd.gni")

chip_test_suite_using_nltest("tests_nltest") {
output_name = "libICDServerTestsNL"

test_sources = [ "TestICDManager.cpp" ]

public_deps = [
"${chip_root}/src/app/icd/server:manager",
"${chip_root}/src/app/icd/server:monitoring-table",
"${chip_root}/src/lib/support:test_utils",
"${chip_root}/src/lib/support:testing",
"${chip_root}/src/lib/support:testing_nlunit",
"${chip_root}/src/messaging/tests:helpers",
"${nlunit_test_root}:nlunit-test",
]
}

chip_test_suite("tests") {
output_name = "libICDServerTests"

test_sources = [ "TestICDMonitoringTable.cpp" ]

public_deps = [
"${chip_root}/src/app/icd/server:monitoring-table",
"${chip_root}/src/lib/support:test_utils",
"${chip_root}/src/lib/support:testing",
]

cflags = [ "-Wconversion" ]
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,25 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <app/EventManagement.h>
#include <app/SubscriptionsInfoProvider.h>
#include <app/TestEventTriggerDelegate.h>
#include <app/icd/server/ICDConfigurationData.h>
#include <app/icd/server/ICDManager.h>
#include <app/icd/server/ICDMonitoringTable.h>
#include <app/icd/server/ICDNotifier.h>
#include <app/icd/server/ICDStateObserver.h>
#include <app/tests/AppTestContext.h>
#include <crypto/DefaultSessionKeystore.h>
#include <lib/core/DataModelTypes.h>
#include <lib/core/NodeId.h>
#include <lib/support/TestPersistentStorageDelegate.h>
#include <lib/support/TimeUtils.h>
#include <lib/support/UnitTestContext.h>
#include <lib/support/UnitTestExtendedAssertions.h>
#include <lib/support/UnitTestRegistration.h>
#include <messaging/tests/MessagingContext.h>
#include <nlunit-test.h>
#include <system/SystemLayerImpl.h>

#include <crypto/DefaultSessionKeystore.h>

using namespace chip;
using namespace chip::app;
using namespace chip::System;
Expand Down Expand Up @@ -119,16 +118,19 @@ class TestSubscriptionsInfoProvider : public SubscriptionsInfoProvider
bool mHasPersistedSubscription = false;
};

class TestContext : public chip::Test::AppContext
class TestContext : public chip::Test::LoopbackMessagingContext
{
public:
// Performs shared setup for all tests in the test suite
CHIP_ERROR SetUpTestSuite() override
{
ReturnErrorOnFailure(chip::Test::AppContext::SetUpTestSuite());
ReturnErrorOnFailure(LoopbackMessagingContext::SetUpTestSuite());
ReturnErrorOnFailure(chip::DeviceLayer::PlatformMgr().InitChipStack());

DeviceLayer::SetSystemLayerForTesting(&GetSystemLayer());
mRealClock = &chip::System::SystemClock();
System::Clock::Internal::SetSystemClockForTesting(&mMockClock);

return CHIP_NO_ERROR;
}

Expand All @@ -137,24 +139,28 @@ class TestContext : public chip::Test::AppContext
{
System::Clock::Internal::SetSystemClockForTesting(mRealClock);
DeviceLayer::SetSystemLayerForTesting(nullptr);
chip::Test::AppContext::TearDownTestSuite();

chip::DeviceLayer::PlatformMgr().Shutdown();
LoopbackMessagingContext::TearDownTestSuite();
}

// Performs setup for each individual test in the test suite
CHIP_ERROR SetUp() override
{
ReturnErrorOnFailure(chip::Test::AppContext::SetUp());
ReturnErrorOnFailure(LoopbackMessagingContext::SetUp());

mICDStateObserver.ResetAll();
mICDManager.RegisterObserver(&mICDStateObserver);
mICDManager.Init(&testStorage, &GetFabricTable(), &mKeystore, &GetExchangeManager(), &mSubInfoProvider);

return CHIP_NO_ERROR;
}

// Performs teardown for each individual test in the test suite
void TearDown() override
{
mICDManager.Shutdown();
chip::Test::AppContext::TearDown();
LoopbackMessagingContext::TearDown();
}

System::Clock::Internal::MockClock mMockClock;
Expand Down Expand Up @@ -653,6 +659,7 @@ class TestICDManager
NL_TEST_ASSERT(aSuite, stayActivePromisedMs == 20000);
}

#if CHIP_CONFIG_ENABLE_ICD_CIP
#if CHIP_CONFIG_PERSIST_SUBSCRIPTIONS
#if CHIP_CONFIG_SUBSCRIPTION_TIMEOUT_RESUMPTION
static void TestShouldCheckInMsgsBeSentAtActiveModeFunction(nlTestSuite * aSuite, void * aContext)
Expand Down Expand Up @@ -723,6 +730,7 @@ class TestICDManager
NL_TEST_ASSERT(aSuite, ctx->mICDManager.ShouldCheckInMsgsBeSentAtActiveModeFunction(kTestFabricIndex1, kClientNodeId11));
}
#endif // CHIP_CONFIG_PERSIST_SUBSCRIPTIONS
#endif // CHIP_CONFIG_ENABLE_ICD_CIP

static void TestHandleTestEventTriggerActiveModeReq(nlTestSuite * aSuite, void * aContext)
{
Expand Down Expand Up @@ -1117,27 +1125,29 @@ namespace {
static const nlTest sTests[] = {
NL_TEST_DEF("TestICDModeDurations", TestICDManager::TestICDModeDurations),
NL_TEST_DEF("TestOnSubscriptionReport", TestICDManager::TestOnSubscriptionReport),
NL_TEST_DEF("TestICDModeDurationsWith0ActiveModeDurationWithoutActiveSub",
TestICDManager::TestICDModeDurationsWith0ActiveModeDurationWithoutActiveSub),
NL_TEST_DEF("TestICDModeDurationsWith0ActiveModeDurationWithActiveSub",
TestICDManager::TestICDModeDurationsWith0ActiveModeDurationWithActiveSub),
NL_TEST_DEF("TestKeepActivemodeRequests", TestICDManager::TestKeepActivemodeRequests),
NL_TEST_DEF("TestICDMRegisterUnregisterEvents", TestICDManager::TestICDMRegisterUnregisterEvents),
NL_TEST_DEF("TestICDCounter", TestICDManager::TestICDCounter),
NL_TEST_DEF("TestICDStayActive", TestICDManager::TestICDMStayActive),
#if CHIP_CONFIG_ENABLE_ICD_CIP
NL_TEST_DEF("TestICDCounter", TestICDManager::TestICDCounter),
NL_TEST_DEF("TestICDMRegisterUnregisterEvents", TestICDManager::TestICDMRegisterUnregisterEvents),
NL_TEST_DEF("TestICDModeDurationsWith0ActiveModeDurationWithActiveSub",
TestICDManager::TestICDModeDurationsWith0ActiveModeDurationWithActiveSub),
NL_TEST_DEF("TestICDModeDurationsWith0ActiveModeDurationWithoutActiveSub",
TestICDManager::TestICDModeDurationsWith0ActiveModeDurationWithoutActiveSub),
NL_TEST_DEF("TestShouldCheckInMsgsBeSentAtActiveModeFunction", TestICDManager::TestShouldCheckInMsgsBeSentAtActiveModeFunction),
NL_TEST_DEF("TestHandleTestEventTriggerActiveModeReq", TestICDManager::TestHandleTestEventTriggerActiveModeReq),
NL_TEST_DEF("TestHandleTestEventTriggerInvalidateHalfCounterValues",
TestICDManager::TestHandleTestEventTriggerInvalidateHalfCounterValues),
NL_TEST_DEF("TestHandleTestEventTriggerInvalidateAllCounterValues",
TestICDManager::TestHandleTestEventTriggerInvalidateAllCounterValues),
NL_TEST_DEF("TestICDStateObserverOnICDModeChange", TestICDManager::TestICDStateObserverOnICDModeChange),
NL_TEST_DEF("TestICDStateObserverOnICDModeChangeOnInit", TestICDManager::TestICDStateObserverOnICDModeChangeOnInit),
#endif // CHIP_CONFIG_ENABLE_ICD_CIP
NL_TEST_DEF("TestHandleTestEventTriggerActiveModeReq", TestICDManager::TestHandleTestEventTriggerActiveModeReq),
NL_TEST_DEF("TestICDStateObserverOnEnterIdleModeActiveModeDuration",
TestICDManager::TestICDStateObserverOnEnterIdleModeActiveModeDuration),
NL_TEST_DEF("TestICDStateObserverOnEnterIdleModeActiveModeThreshold",
TestICDManager::TestICDStateObserverOnEnterIdleModeActiveModeThreshold),
NL_TEST_DEF("TestICDStateObserverOnEnterActiveMode", TestICDManager::TestICDStateObserverOnEnterActiveMode),
NL_TEST_DEF("TestICDStateObserverOnICDModeChange", TestICDManager::TestICDStateObserverOnICDModeChange),
NL_TEST_DEF("TestICDStateObserverOnICDModeChangeOnInit", TestICDManager::TestICDStateObserverOnICDModeChangeOnInit),
NL_TEST_DEF("TestICDStateObserverOnTransitionToIdleModeGreaterActiveModeDuration",
TestICDManager::TestICDStateObserverOnTransitionToIdleModeGreaterActiveModeDuration),
NL_TEST_DEF("TestICDStateObserverOnTransitionToIdleModeEqualActiveModeDuration",
Expand Down
Loading

0 comments on commit 3062e91

Please sign in to comment.