Skip to content

Commit 74fa3f3

Browse files
feasel0restyled-commits
andauthoredJul 9, 2024··
Updated AppContext, LoopbackMessagingContext, and UDPMessagingContext to serve as PW test fixture classes. (#34036)
* Modified MessagingContext and its subclasses as well as AppContext to reflect PW migration. * Modifed all tests that use LoopbackMessagingContext or AppContext. Moved LoopbackMessagingContext data to heap. * Removed unneeded scope from call to parent setup/teardown * Made some helper functions into class methods. * Restyled by prettier-markdown * Reverted TestICDManager back to using LoopbackMessagingContext * fixing merge conflicts * fixing merge conflicts * Fixed duplicate code and missing semicolon * Trying to update TestReadHandler_DataVersionFiltersTruncated * Restyled by whitespace * Restyled by clang-format * fixed problem with the new TestReadHandler_DataVersionFiltersTruncated * fix in tracing * Changes to MessagingContext member variable initialization in response to code review * Used std::unique_ptr for MessagingContext.mpData --------- Co-authored-by: Restyled.io <commits@restyled.io>
1 parent a803ee3 commit 74fa3f3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1953
-2713
lines changed
 

‎docs/testing/unit_testing.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,8 @@ your overriding function make sure to check `HasFailure()` and return if the
218218
parent function failed.
219219

220220
If you don't override any of the setup/teardown functions, you can simply make a
221-
type alias: `using YourTestContext = Test::AppContextPW;` instead of defining
222-
your own text context class.
221+
type alias: `using YourTestContext = Test::AppContext;` instead of defining your
222+
own text context class.
223223

224224
## Best practices
225225

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

+17-29
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* See the License for the specific language governing permissions and
1616
* limitations under the License.
1717
*/
18+
#include <pw_unit_test/framework.h>
19+
1820
#include <app/SubscriptionsInfoProvider.h>
1921
#include <app/TestEventTriggerDelegate.h>
2022
#include <app/icd/server/ICDConfigurationData.h>
@@ -24,7 +26,6 @@
2426
#include <app/icd/server/ICDStateObserver.h>
2527
#include <app/icd/server/tests/ICDConfigurationDataTestAccess.h>
2628
#include <crypto/DefaultSessionKeystore.h>
27-
#include <gtest/gtest.h>
2829
#include <lib/address_resolve/AddressResolve.h>
2930
#include <lib/core/DataModelTypes.h>
3031
#include <lib/core/NodeId.h>
@@ -124,18 +125,16 @@ class TestSubscriptionsInfoProvider : public SubscriptionsInfoProvider
124125
bool mHasPersistedSubscription = false;
125126
};
126127

127-
System::Clock::Internal::MockClock * pMockClock = nullptr;
128-
System::Clock::ClockBase * pRealClock = nullptr;
129-
chip::Test::LoopbackMessagingContext * pMessagingContext = nullptr;
128+
System::Clock::Internal::MockClock * pMockClock = nullptr;
129+
System::Clock::ClockBase * pRealClock = nullptr;
130130

131131
} // namespace
132132

133133
namespace chip {
134134
namespace app {
135135

136-
class TestICDManager : public ::testing::Test
136+
class TestICDManager : public Test::LoopbackMessagingContext
137137
{
138-
139138
public:
140139
/*
141140
* Advance the test Mock clock time by the amout passed in argument
@@ -147,7 +146,7 @@ class TestICDManager : public ::testing::Test
147146
static void AdvanceClockAndRunEventLoop(Milliseconds64 time)
148147
{
149148
pMockClock->AdvanceMonotonic(time);
150-
pMessagingContext->GetIOContext().DriveIO();
149+
GetIOContext().DriveIO();
151150
}
152151

153152
// Performs shared setup for all tests in the test suite
@@ -159,16 +158,12 @@ class TestICDManager : public ::testing::Test
159158
ASSERT_NE(pMockClock, nullptr);
160159
}
161160

162-
if (pMessagingContext == nullptr)
163-
{
164-
pMessagingContext = new LoopbackMessagingContext();
165-
ASSERT_NE(pMessagingContext, nullptr);
166-
}
161+
LoopbackMessagingContext::SetUpTestSuite();
162+
VerifyOrReturn(!HasFailure());
167163

168-
pMessagingContext->SetUpTestSuite();
169164
ASSERT_EQ(chip::DeviceLayer::PlatformMgr().InitChipStack(), CHIP_NO_ERROR);
170165

171-
DeviceLayer::SetSystemLayerForTesting(&(pMessagingContext->GetSystemLayer()));
166+
DeviceLayer::SetSystemLayerForTesting(&GetSystemLayer());
172167
pRealClock = &SystemClock();
173168
Clock::Internal::SetSystemClockForTesting(pMockClock);
174169
}
@@ -180,39 +175,34 @@ class TestICDManager : public ::testing::Test
180175
DeviceLayer::SetSystemLayerForTesting(nullptr);
181176

182177
DeviceLayer::PlatformMgr().Shutdown();
183-
pMessagingContext->TearDownTestSuite();
178+
179+
LoopbackMessagingContext::TearDownTestSuite();
184180

185181
if (pMockClock != nullptr)
186182
{
187183
delete pMockClock;
188184
pMockClock = nullptr;
189185
}
190186

191-
if (pMessagingContext != nullptr)
192-
{
193-
delete pMessagingContext;
194-
pMessagingContext = nullptr;
195-
}
196-
197187
pRealClock = nullptr;
198188
}
199189

200190
// Performs setup for each individual test in the test suite
201191
void SetUp() override
202192
{
203-
pMessagingContext->SetUp();
193+
LoopbackMessagingContext::SetUp();
194+
VerifyOrReturn(!HasFailure());
204195

205196
mICDStateObserver.ResetAll();
206197
mICDManager.RegisterObserver(&mICDStateObserver);
207-
mICDManager.Init(&testStorage, &(pMessagingContext->GetFabricTable()), &mKeystore,
208-
&(pMessagingContext->GetExchangeManager()), &mSubInfoProvider);
198+
mICDManager.Init(&testStorage, &GetFabricTable(), &mKeystore, &GetExchangeManager(), &mSubInfoProvider);
209199
}
210200

211201
// Performs teardown for each individual test in the test suite
212202
void TearDown() override
213203
{
214204
mICDManager.Shutdown();
215-
pMessagingContext->TearDown();
205+
LoopbackMessagingContext::TearDown();
216206
}
217207

218208
TestSessionKeystoreImpl mKeystore;
@@ -576,8 +566,7 @@ TEST_F(TestICDManager, TestICDCounter)
576566

577567
// Shut down and reinit ICDManager to increment counter
578568
mICDManager.Shutdown();
579-
mICDManager.Init(&(testStorage), &(pMessagingContext->GetFabricTable()), &(mKeystore),
580-
&(pMessagingContext->GetExchangeManager()), &(mSubInfoProvider));
569+
mICDManager.Init(&(testStorage), &GetFabricTable(), &(mKeystore), &GetExchangeManager(), &(mSubInfoProvider));
581570
mICDManager.RegisterObserver(&(mICDStateObserver));
582571

583572
EXPECT_EQ(counter + ICDConfigurationData::kICDCounterPersistenceIncrement,
@@ -982,8 +971,7 @@ TEST_F(TestICDManager, TestICDStateObserverOnICDModeChangeOnInit)
982971
// Shut down and reinit ICDManager - We should go to LIT mode since we have a registration
983972
mICDManager.Shutdown();
984973
mICDManager.RegisterObserver(&(mICDStateObserver));
985-
mICDManager.Init(&testStorage, &(pMessagingContext->GetFabricTable()), &mKeystore, &(pMessagingContext->GetExchangeManager()),
986-
&mSubInfoProvider);
974+
mICDManager.Init(&testStorage, &GetFabricTable(), &mKeystore, &GetExchangeManager(), &mSubInfoProvider);
987975

988976
// We have a registration, transition to LIT mode
989977
EXPECT_TRUE(mICDStateObserver.mOnICDModeChangeCalled);

0 commit comments

Comments
 (0)
Please sign in to comment.