Skip to content

Commit

Permalink
Extract duplicated code for app test cases into AppTestContext (#10968)
Browse files Browse the repository at this point in the history
  • Loading branch information
kghost authored and pull[bot] committed Jan 28, 2022
1 parent 53c90ed commit 1ff1f91
Show file tree
Hide file tree
Showing 23 changed files with 371 additions and 520 deletions.
47 changes: 47 additions & 0 deletions src/app/tests/AppTestContext.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2021 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.
*/

#include <app/tests/AppTestContext.h>

#include <app/InteractionModelEngine.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/ErrorStr.h>

namespace chip {
namespace Test {

CHIP_ERROR AppContext::Init()
{
ReturnErrorOnFailure(chip::Platform::MemoryInit());
ReturnErrorOnFailure(mIOContext.Init());
ReturnErrorOnFailure(mTransportManager.Init("LOOPBACK"));
ReturnErrorOnFailure(MessagingContext::Init(&mTransportManager, &mIOContext));
ReturnErrorOnFailure(chip::app::InteractionModelEngine::GetInstance()->Init(&GetExchangeManager(), nullptr));

return CHIP_NO_ERROR;
}

CHIP_ERROR AppContext::Shutdown()
{
ReturnErrorOnFailure(MessagingContext::Shutdown());
ReturnErrorOnFailure(mIOContext.Shutdown());
chip::Platform::MemoryShutdown();

return CHIP_NO_ERROR;
}

} // namespace Test
} // namespace chip
57 changes: 57 additions & 0 deletions src/app/tests/AppTestContext.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2021 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.
*/
#pragma once

#include <messaging/tests/MessagingContext.h>
#include <transport/raw/tests/NetworkTestHelpers.h>

#include <nlunit-test.h>

namespace chip {
namespace Test {

/**
* @brief The context of test cases for messaging layer. It wil initialize network layer and system layer, and create
* two secure sessions, connected with each other. Exchanges can be created for each secure session.
*/
class AppContext : public MessagingContext
{
public:
/// Initialize the underlying layers and test suite pointer
CHIP_ERROR Init();

// Shutdown all layers, finalize operations
CHIP_ERROR Shutdown();

static int Initialize(void * context)
{
auto * ctx = static_cast<AppContext *>(context);
return ctx->Init() == CHIP_NO_ERROR ? SUCCESS : FAILURE;
}

static int Finalize(void * context)
{
auto * ctx = static_cast<AppContext *>(context);
return ctx->Shutdown() == CHIP_NO_ERROR ? SUCCESS : FAILURE;
}

private:
chip::TransportMgr<chip::Test::LoopbackTransport> mTransportManager;
chip::Test::IOContext mIOContext;
};

} // namespace Test
} // namespace chip
22 changes: 19 additions & 3 deletions src/app/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@ import("//build_overrides/nlunit_test.gni")
import("${chip_root}/build/chip/chip_test_suite.gni")
import("${chip_root}/src/platform/device.gni")

static_library("helpers") {
output_name = "libAppTestHelpers"
output_dir = "${root_out_dir}/lib"

sources = [
"AppTestContext.cpp",
"AppTestContext.h",
]

cflags = [ "-Wconversion" ]

deps = [
"${chip_root}/src/lib/support",
"${chip_root}/src/messaging/tests:helpers",
"${chip_root}/src/transport/raw/tests:helpers",
]
}

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

Expand Down Expand Up @@ -49,11 +67,9 @@ chip_test_suite("tests") {
public_deps = [
"${chip_root}/src/app",
"${chip_root}/src/app/common:cluster-objects",
"${chip_root}/src/app/tests:helpers",
"${chip_root}/src/app/util:device_callbacks_manager",
"${chip_root}/src/lib/core",
"${chip_root}/src/messaging/tests:helpers",
"${chip_root}/src/protocols",
"${chip_root}/src/transport/raw/tests:helpers",
"${nlunit_test_root}:nlunit-test",
]

Expand Down
Loading

0 comments on commit 1ff1f91

Please sign in to comment.