Skip to content

Commit bd6ff65

Browse files
[ICD] Convert TestICDManager to gtest (#33269)
* Convert TestICDManager to gtest * Refactor friend tests with a single access point * Fix openIoT * remove namespace from friend since they are in the same * fix typo * Replace VerifyOrDie with Assert statement * Remove extra namespaces
1 parent 98c10ee commit bd6ff65

File tree

8 files changed

+877
-879
lines changed

8 files changed

+877
-879
lines changed

src/BUILD.gn

-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ if (chip_build_tests) {
6767
"${chip_root}/src/protocols/user_directed_commissioning/tests",
6868
"${chip_root}/src/transport/retransmit/tests",
6969
"${chip_root}/src/app/icd/server/tests",
70-
"${chip_root}/src/app/icd/server/tests:tests_nltest",
7170
]
7271

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

src/app/icd/server/ICDConfigurationData.h

+7-5
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ namespace chip {
2828
namespace app {
2929
// Forward declaration of ICDManager to allow it to be friend with ICDConfigurationData.
3030
class ICDManager;
31+
} // namespace app
3132

32-
// Forward declaration of TestICDManager to allow it to be friend with the ICDConfigurationData.
33+
namespace Test {
34+
// Forward declaration of ICDConfigurationDataTestAccess tests to allow it to be friend with the ICDConfigurationData.
3335
// Used in unit tests
34-
class TestICDManager;
35-
36-
} // namespace app
36+
class ICDConfigurationDataTestAccess;
37+
} // namespace Test
3738

3839
/**
3940
* @brief ICDConfigurationData manages and stores ICD related configurations for the ICDManager.
@@ -99,7 +100,8 @@ class ICDConfigurationData
99100
// the ICDManager, the ICDManager is a friend that can access the private setters. If a consummer needs to be notified when a
100101
// value is changed, they can leverage the Observer events the ICDManager generates. See src/app/icd/server/ICDStateObserver.h
101102
friend class chip::app::ICDManager;
102-
friend class chip::app::TestICDManager;
103+
104+
friend class chip::Test::ICDConfigurationDataTestAccess;
103105

104106
void SetICDMode(ICDMode mode) { mICDMode = mode; };
105107
void SetSlowPollingInterval(System::Clock::Milliseconds32 slowPollInterval) { mSlowPollingInterval = slowPollInterval; };

src/app/icd/server/ICDManager.h

+7-3
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ using SymmetricKeystore = SessionKeystore;
4747
namespace chip {
4848
namespace app {
4949

50-
// Forward declaration of TestICDManager to allow it to be friend with ICDManager
50+
// Forward declaration of TestICDManager tests to allow it to be friend with ICDManager
5151
// Used in unit tests
52-
class TestICDManager;
52+
class TestICDManager_TestShouldCheckInMsgsBeSentAtActiveModeFunction_Test;
5353

5454
/**
5555
* @brief ICD Manager is responsible of processing the events and triggering the correct action for an ICD
@@ -132,6 +132,8 @@ class ICDManager : public ICDListener, public TestEventTriggerHandler
132132

133133
ICDConfigurationData::ICDMode GetICDMode() { return ICDConfigurationData::GetInstance().GetICDMode(); };
134134

135+
OperationalState GetOperaionalState() { return mOperationalState; };
136+
135137
/**
136138
* @brief Adds the referenced observer in parameters to the mStateObserverPool
137139
* A maximum of CHIP_CONFIG_ICD_OBSERVERS_POOL_SIZE observers can be concurrently registered
@@ -199,7 +201,9 @@ class ICDManager : public ICDListener, public TestEventTriggerHandler
199201
void OnSubscriptionReport() override;
200202

201203
private:
202-
friend class TestICDManager;
204+
// TODO : Once <gtest/gtest_prod.h> can be included, use FRIEND_TEST for the friend class.
205+
friend class TestICDManager_TestShouldCheckInMsgsBeSentAtActiveModeFunction_Test;
206+
203207
/**
204208
* @brief UpdateICDMode evaluates in which mode the ICD can be in; SIT or LIT mode.
205209
* If the current operating mode does not match the evaluated operating mode, function updates the ICDMode and triggers

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

+8-17
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,22 @@ import("//build_overrides/pigweed.gni")
1919
import("${chip_root}/build/chip/chip_test_suite.gni")
2020
import("${chip_root}/src/app/icd/icd.gni")
2121

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-
3822
chip_test_suite("tests") {
3923
output_name = "libICDServerTests"
4024

41-
test_sources = [ "TestICDMonitoringTable.cpp" ]
25+
test_sources = [
26+
"TestICDManager.cpp",
27+
"TestICDMonitoringTable.cpp",
28+
]
29+
30+
sources = [ "ICDConfigurationDataTestAccess.h" ]
4231

4332
public_deps = [
33+
"${chip_root}/src/app/icd/server:manager",
4434
"${chip_root}/src/app/icd/server:monitoring-table",
4535
"${chip_root}/src/lib/support:test_utils",
4636
"${chip_root}/src/lib/support:testing",
37+
"${chip_root}/src/messaging/tests:helpers",
4738
]
4839

4940
cflags = [ "-Wconversion" ]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
*
3+
* Copyright (c) 2024 Project CHIP Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#pragma once
19+
20+
#include <app/icd/server/ICDConfigurationData.h>
21+
22+
namespace chip {
23+
namespace Test {
24+
/**
25+
* @brief Class acts as an accessor to private methods of the ICDConfigurationData class without needing to give friend access to
26+
* each individual test.
27+
* This design is necessary because certain tests validate specific edge cases around specific configurations.
28+
* See ICDConfigurationData.h for more details why SetModeDurations cannot be a public API.
29+
*/
30+
class ICDConfigurationDataTestAccess
31+
{
32+
public:
33+
ICDConfigurationDataTestAccess() = delete;
34+
ICDConfigurationDataTestAccess(ICDConfigurationData * data) : mData(data) {}
35+
36+
CHIP_ERROR SetModeDurations(Optional<System::Clock::Milliseconds32> activeModeDuration,
37+
Optional<System::Clock::Milliseconds32> idleModeDuration)
38+
{
39+
return mData->SetModeDurations(activeModeDuration, idleModeDuration);
40+
}
41+
42+
private:
43+
ICDConfigurationData * mData = nullptr;
44+
};
45+
46+
} // namespace Test
47+
} // namespace chip

0 commit comments

Comments
 (0)