Add test and tools.

This commit is contained in:
Fancy code 2024-06-15 08:39:16 +08:00
parent cadab3c801
commit aaa00524a2
119 changed files with 7910 additions and 0 deletions

63
test/CMakeLists.txt Normal file
View File

@ -0,0 +1,63 @@
# cmake_minimum_required(VERSION 2.8.0)
# Compile gtest for test code.
if (EXISTS "${EXTERNAL_LIBS_OUTPUT_PATH}/libgtest.a")
message("googletest compile ok.")
else()
execute_process(COMMAND sh build_gtest.sh ${TARGET_PLATFORM} ${PLATFORM_PATH} ${PLATFORM_PATH} WORKING_DIRECTORY ${EXTERNAL_SOURCE_PATH}/gtest/)
execute_process(COMMAND mkdir ${PLATFORM_PATH}/output_files
WORKING_DIRECTORY ${EXTERNAL_SOURCE_PATH}/gtest/)
execute_process(COMMAND mkdir ${PLATFORM_PATH}/output_files/libs
WORKING_DIRECTORY ${EXTERNAL_SOURCE_PATH}/gtest/)
execute_process(COMMAND mkdir ${PLATFORM_PATH}/output_files/libs/external
WORKING_DIRECTORY ${EXTERNAL_SOURCE_PATH}/gtest/)
execute_process(COMMAND mv ${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/lib/libgtest.a ${EXTERNAL_LIBS_OUTPUT_PATH}/libgtest.a
WORKING_DIRECTORY ${EXTERNAL_SOURCE_PATH}/gtest/)
execute_process(COMMAND mv ${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/lib/libgmock.a ${EXTERNAL_LIBS_OUTPUT_PATH}/libgmock.a
WORKING_DIRECTORY ${EXTERNAL_SOURCE_PATH}/gtest/)
endif()
if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX})
message("linux mock api will be set.")
set(TEST_LINUX_MOCK "-Wl,--wrap=fx_open" CACHE STRING INTERNAL FORCE)
set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=fx_fstat" CACHE STRING INTERNAL FORCE)
set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=fx_tcgetattr" CACHE STRING INTERNAL FORCE)
set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=fx_tcsetattr" CACHE STRING INTERNAL FORCE)
set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=fx_select" CACHE STRING INTERNAL FORCE)
set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=fx_write" CACHE STRING INTERNAL FORCE)
set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=fx_read" CACHE STRING INTERNAL FORCE)
set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=fx_access" CACHE STRING INTERNAL FORCE)
set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=fx_fopen" CACHE STRING INTERNAL FORCE)
endif()
# Mock Linux api.
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${TEST_LINUX_MOCK}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TEST_LINUX_MOCK}")
# build gtest and gmock
# add_custom_command(
# OUTPUT ${EXTERNAL_LIBS_OUTPUT_PATH}/libgtest.a
# OUTPUT ${EXTERNAL_LIBS_OUTPUT_PATH}/libgmock.a
# COMMAND echo "Build google test for test code."
# COMMAND sh build_gtest.sh ${TARGET_PLATFORM} ${PLATFORM_PATH} ${PLATFORM_PATH}
# COMMAND mv ${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/lib/libgtest.a ${EXTERNAL_LIBS_OUTPUT_PATH}/libgtest.a
# COMMAND mv ${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/lib/libgmock.a ${EXTERNAL_LIBS_OUTPUT_PATH}/libgmock.a
# WORKING_DIRECTORY ${EXTERNAL_SOURCE_PATH}/gtest/
# )
# # add_custom_target(
# # google_test
# # DEPENDS libgtest.a libgmock.a
# # )
# add_custom_target(
# libgtest.a
# DEPENDS ${EXTERNAL_LIBS_OUTPUT_PATH}/libgtest.a
# )
# add_custom_target(
# libgmock.a
# DEPENDS ${EXTERNAL_LIBS_OUTPUT_PATH}/libgmock.a
# )
add_subdirectory(all)
add_subdirectory(application)
add_subdirectory(middleware)
add_subdirectory(utils)
add_subdirectory(hal)

51
test/GtestUsing.h Normal file
View File

@ -0,0 +1,51 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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.
*/
#ifndef GTEST_USING_H
#define GTEST_USING_H
#include <gmock/gmock.h>
#include <gtest/gtest.h>
using ::testing::_;
using ::testing::Action;
using ::testing::ActionInterface;
using ::testing::AnyNumber;
using ::testing::Assign;
using ::testing::AtLeast;
using ::testing::ByMove;
using ::testing::ByRef;
using ::testing::DefaultValue;
using ::testing::DoAll;
using ::testing::DoDefault;
using ::testing::IgnoreResult;
using ::testing::Invoke;
using ::testing::InvokeWithoutArgs;
using ::testing::MakePolymorphicAction;
using ::testing::PolymorphicAction;
using ::testing::Return;
using ::testing::ReturnNew;
using ::testing::ReturnNull;
using ::testing::ReturnPointee;
using ::testing::ReturnRef;
using ::testing::ReturnRefOfCopy;
using ::testing::ReturnRoundRobin;
using ::testing::SaveArg;
using ::testing::SetArgPointee;
using ::testing::SetArgReferee;
using ::testing::SetArgumentPointee;
using ::testing::Unused;
using ::testing::WithArgs;
using ::testing::internal::BuiltInDefaultValue;
// using ::testing::Mock::VerifyAndClearExpectations;
using ::testing::Mock;
#endif

28
test/all/CMakeLists.txt Normal file
View File

@ -0,0 +1,28 @@
# include(${CMAKE_SOURCE_DIR}/build/independent_source.cmake)
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
set(EXECUTABLE_OUTPUT_PATH ${TEST_OUTPUT_PATH}/bin)
include_directories(
.
./src
./include
${UTILS_SOURCE_PATH}/Log/include
${UTILS_SOURCE_PATH}/StatusCode/include
${MIDDLEWARE_SOURCE_PATH}/IpcConfig/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include
)
link_directories(
${LIBS_OUTPUT_PATH}
${EXTERNAL_LIBS_OUTPUT_PATH}
)
aux_source_directory(. SRC_FILES)
# aux_source_directory(${TEST_SOURCE_PATH}/middleware/IpcConfig/src SRC_FILES)
set(TARGET_NAME AllTest)
add_executable(${TARGET_NAME} ${SRC_FILES})
target_link_libraries(${TARGET_NAME} IpcConfig gtest gmock pthread)
if(${TEST_COVERAGE} MATCHES "true")
target_link_libraries(${TARGET_NAME} gcov)
endif()

9
test/all/mainTest.cpp Normal file
View File

@ -0,0 +1,9 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
#include <unistd.h>
int main(int argc, char *argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@ -0,0 +1,4 @@
add_subdirectory(HuntingCamera)
add_subdirectory(MissionManager)
# add_subdirectory(VersionReleaseTool)

View File

@ -0,0 +1,96 @@
# include(${CMAKE_SOURCE_DIR}/build/independent_source.cmake)
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
include(${APPLICATION_SOURCE_PATH}/HuntingCamera/build/hunting_camera.cmake)
set(EXECUTABLE_OUTPUT_PATH ${TEST_OUTPUT_PATH}/bin)
include_directories(
./src
./tool/include
${HUNTTING_MAIN_INCLUDE_PATH}
${MIDDLEWARE_SOURCE_PATH}/McuManager/include
${MIDDLEWARE_SOURCE_PATH}/McuManager/src
${MIDDLEWARE_SOURCE_PATH}/DeviceManager/include
${MIDDLEWARE_SOURCE_PATH}/DeviceManager/src
${UTILS_SOURCE_PATH}/McuProtocol/include
${UTILS_SOURCE_PATH}/UartDevice/include
${UTILS_SOURCE_PATH}/LedControl/include
${TEST_SOURCE_PATH}
${TEST_SOURCE_PATH}/application/MissionManager/tool/include
${TEST_SOURCE_PATH}/middleware/McuManager/tool/include
${TEST_SOURCE_PATH}/middleware/AppManager/tool/include
${TEST_SOURCE_PATH}/middleware/DeviceManager/tool/include
${TEST_SOURCE_PATH}/utils/LinuxApiMock/include
${TEST_SOURCE_PATH}/utils/TestManager/include
${TEST_SOURCE_PATH}/utils/McuProtocol/tool/include
${TEST_SOURCE_PATH}/utils/UartDevice/tool/include
${TEST_SOURCE_PATH}/hal/tool/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include
)
link_directories(
${LIBS_OUTPUT_PATH}
${EXTERNAL_LIBS_OUTPUT_PATH}
${TEST_TOOLS_OUTPUT_PATH}
)
aux_source_directory(. SRC_FILES_MAIN)
aux_source_directory(./src SRC_FILES)
if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX})
aux_source_directory(./src_mock SRC_FILES)
endif()
set(TARGET_NAME HuntingCameraTest)
add_executable(${TARGET_NAME} ${SRC_FILES_MAIN} ${SRC_FILES})
target_link_libraries(${TARGET_NAME} McuProtocolTestTool UartDeviceTestTool)
target_link_libraries(${TARGET_NAME} HuntingMainLib MissionManagerTestTool McuManagerTestTool McuAskBaseTestTool
AppManagerTestTool HalTestTool DeviceManagerTestTool TestManager UartDevice Hal LinuxApiMock gtest gmock pthread)
target_link_libraries(${TARGET_NAME} UpgradeTool Servers)
if(${TEST_COVERAGE} MATCHES "true")
target_link_libraries(${TARGET_NAME} gcov)
endif()
if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true")
add_custom_target(
HuntingCameraTest_code_check
COMMAND ${CLANG_TIDY_EXE}
-checks='${CLANG_TIDY_CHECKS}'
--header-filter=.*
--system-headers=false
${SRC_FILES}
${SRC_FILES_MAIN}
${CLANG_TIDY_CONFIG}
# --line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/gtest.h\"}]'
--line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/*.h\"}]'
-p ${PLATFORM_PATH}/cmake-shell
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/application/HuntingCamera
)
add_custom_command(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make HuntingCameraTest_code_check
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
file(GLOB_RECURSE HEADER_FILES *.h)
add_custom_target(
HuntingCameraTest_code_format
COMMAND ${CLANG_FORMAT_EXE}
-style=file
-i ${SRC_FILES} ${SRC_FILES_MAIN} ${HEADER_FILES}
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/application/HuntingCamera
)
add_custom_command(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make HuntingCameraTest_code_check
COMMAND make HuntingCameraTest_code_format
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
endif()
define_file_name(${TARGET_NAME})
# if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tool/CMakeLists.txt")
# add_subdirectory(tool)
# endif()

View File

@ -0,0 +1,23 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
#include <unistd.h>
int main(int argc, char *argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@ -0,0 +1,23 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 "GtestUsing.h"
#include "ILog.h"
#include "MainThread.h"
#include "McuManagerTestTool.h"
#include "TestManager.h"
#include <thread>
namespace HuntingCameraTest
{
} // namespace HuntingCameraTest

View File

@ -0,0 +1,44 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 "AppManagerTestTool.h"
#include "GtestUsing.h"
#include "HalTestTool.h"
#include "HuntingCameraTest.h"
#include "ILog.h"
#include "MainThread.h"
#include "McuManagerTestTool.h"
#include "MissionManagerTestTool.h"
#include "TestManager.h"
#include <thread>
namespace AppManager_Mock_Test
{
/**
* @brief Construct a new test f object
* ../output_files/test/bin/HuntingCameraTest
* --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_EXAMPLE_SetSdCardStatus
*/
TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_EXAMPLE_SetSdCardStatus)
{
MainThread::GetInstance()->Init();
TestManager::ResetTimeOut(1000 * 6);
std::this_thread::sleep_for(std::chrono::milliseconds(500));
AppManagerTestTool::MockAppClientConnect();
HalTestTool::MockSdCardRemove(mLinuxTest);
std::this_thread::sleep_for(std::chrono::milliseconds(500));
HalTestTool::MockSdCardInsert(mLinuxTest);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MainThread::GetInstance()->Runing();
}
} // namespace AppManager_Mock_Test

View File

@ -0,0 +1,206 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 "AppManagerTestTool.h"
#include "GtestUsing.h"
#include "HalTestTool.h"
#include "HuntingCameraTest.h"
#include "ILog.h"
#include "MainThread.h"
#include "McuManagerTestTool.h"
#include "MissionManagerTestTool.h"
#include "TestManager.h"
#include <thread>
namespace AppMonitor_Mock_Test
{
// ../output_files/test/bin/HuntingCameraTest
// --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_AUTO_GetProductInfo
TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_AUTO_GetProductInfo)
{
MainThread::GetInstance()->Init();
TestManager::ResetTimeOut(1000 * 3);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockGetProductInfo();
MainThread::GetInstance()->Runing();
}
// ../output_files/test/bin/HuntingCameraTest
// --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_AUTO_GetDeviceAttr
TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_AUTO_GetDeviceAttr)
{
MainThread::GetInstance()->Init();
TestManager::ResetTimeOut(1000 * 3);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockGetDeviceAttr();
MainThread::GetInstance()->Runing();
}
// ../output_files/test/bin/HuntingCameraTest
// --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_AUTO_GetMediaInfo
TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_AUTO_GetMediaInfo)
{
MainThread::GetInstance()->Init();
TestManager::ResetTimeOut(1000 * 3);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockGetMediaInfo();
MainThread::GetInstance()->Runing();
}
// ../output_files/test/bin/HuntingCameraTest
// --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_AUTO_GetSdCardInfo
TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_AUTO_GetSdCardInfo)
{
MainThread::GetInstance()->Init();
TestManager::ResetTimeOut(1000 * 3);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockGetSdCardInfo();
MainThread::GetInstance()->Runing();
}
// ../output_files/test/bin/HuntingCameraTest
// --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_AUTO_GetBatteryInfo
TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_AUTO_GetBatteryInfo)
{
MainThread::GetInstance()->Init();
TestManager::ResetTimeOut(1000 * 3);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockGetBatteryInfo();
MainThread::GetInstance()->Runing();
}
// ../output_files/test/bin/HuntingCameraTest
// --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_AUTO_SetDateTime
TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_AUTO_SetDateTime)
{
MainThread::GetInstance()->Init();
TestManager::ResetTimeOut(1000 * 3);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockSetDateTime();
MainThread::GetInstance()->Runing();
}
// ../output_files/test/bin/HuntingCameraTest
// --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_AUTO_SetTimeZone
TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_AUTO_SetTimeZone)
{
MainThread::GetInstance()->Init();
TestManager::ResetTimeOut(1000 * 3);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockSetTimeZone();
MainThread::GetInstance()->Runing();
}
// ../output_files/test/bin/HuntingCameraTest
// --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_AUTO_MockUploadFiles
TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_AUTO_MockUploadFiles)
{
MainThread::GetInstance()->Init();
TestManager::ResetTimeOut(1000 * 3);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockUploadFiles();
MainThread::GetInstance()->Runing();
}
// ../output_files/test/bin/HuntingCameraTest
// --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_AUTO_McuOpenFailed
TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_AUTO_McuOpenFailed)
{
McuManagerTestTool::MockMcuDeviceOpenFailed(mLinuxTest);
MainThread::GetInstance()->Init();
TestManager::ResetTimeOut(1000 * 3);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// MockUploadFiles();
MainThread::GetInstance()->Runing();
}
// ../output_files/test/bin/HuntingCameraTest
// --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_AUTO_GetParamValue
TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_AUTO_GetParamValue)
{
McuManagerTestTool::MockMcuDeviceOpenFailed(mLinuxTest);
MainThread::GetInstance()->Init();
TestManager::ResetTimeOut(1000 * 3);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockGetParamValue("all");
MainThread::GetInstance()->Runing();
}
// ../output_files/test/bin/HuntingCameraTest
// --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_AUTO_GetCapability
TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_AUTO_GetCapability)
{
McuManagerTestTool::MockMcuDeviceOpenFailed(mLinuxTest);
MainThread::GetInstance()->Init();
TestManager::ResetTimeOut(1000 * 3);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockGetCapability();
MainThread::GetInstance()->Runing();
}
// ../output_files/test/bin/HuntingCameraTest
// --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_AUTO_GetLockVideoStatus
TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_AUTO_GetLockVideoStatus)
{
McuManagerTestTool::MockMcuDeviceOpenFailed(mLinuxTest);
MainThread::GetInstance()->Init();
TestManager::ResetTimeOut(1000 * 3);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockGetLockVideoStatus();
MainThread::GetInstance()->Runing();
}
// ../output_files/test/bin/HuntingCameraTest
// --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_AUTO_GetStorageInfo
TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_AUTO_GetStorageInfo)
{
McuManagerTestTool::MockMcuDeviceOpenFailed(mLinuxTest);
MainThread::GetInstance()->Init();
TestManager::ResetTimeOut(1000 * 3);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockGetStorageInfo();
MainThread::GetInstance()->Runing();
}
// ../output_files/test/bin/HuntingCameraTest
// --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_AUTO_GetStorageFileList
TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_AUTO_GetStorageFileList)
{
McuManagerTestTool::MockMcuDeviceOpenFailed(mLinuxTest);
MainThread::GetInstance()->Init();
TestManager::ResetTimeOut(1000 * 3);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockGetStorageFileList();
MainThread::GetInstance()->Runing();
}
// ../output_files/test/bin/HuntingCameraTest
// --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_AUTO_SetParamValue
TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_AUTO_SetParamValue)
{
McuManagerTestTool::MockMcuDeviceOpenFailed(mLinuxTest);
MainThread::GetInstance()->Init();
TestManager::ResetTimeOut(1000 * 3);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockSetParamValue();
MainThread::GetInstance()->Runing();
}
// ../output_files/test/bin/HuntingCameraTest
// --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_AUTO_EnterRecorder
TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_AUTO_EnterRecorder)
{
McuManagerTestTool::MockMcuDeviceOpenFailed(mLinuxTest);
MainThread::GetInstance()->Init();
TestManager::ResetTimeOut(1000 * 3);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockEnterRecorder();
MainThread::GetInstance()->Runing();
}
// ../output_files/test/bin/HuntingCameraTest
// --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_AUTO_AppPlayback
TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_AUTO_AppPlayback)
{
McuManagerTestTool::MockMcuDeviceOpenFailed(mLinuxTest);
MainThread::GetInstance()->Init();
TestManager::ResetTimeOut(1000 * 3);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockAppPlayback();
MainThread::GetInstance()->Runing();
}
} // namespace AppMonitor_Mock_Test

View File

@ -0,0 +1,82 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 "AppManagerTestTool.h"
#include "GtestUsing.h"
#include "HalTestTool.h"
#include "HuntingCameraTest.h"
#include "ILog.h"
#include "MainThread.h"
#include "McuManagerTestTool.h"
#include "MissionManagerTestTool.h"
#include "TestManager.h"
#include <thread>
namespace DeviceManager_Mock_Test
{
/**
* @brief Construct a new test f object
* ../output_files/test/bin/HuntingCameraTest
* --gtest_filter=HuntingCameraTest.HS_INTEGRATION_HunttingCamera_EXAMPLE_KeyControlHold
*/
TEST_F(HuntingCameraTest, HS_INTEGRATION_HunttingCamera_EXAMPLE_KeyControlHold)
{
MainThread::GetInstance()->Init();
TestManager::ResetTimeOut(1000 * 3);
HalTestTool::MockKeyClick("reset", 1000); // Simulate pressing a button.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MainThread::GetInstance()->Runing();
}
/**
* @brief Construct a new test f object
* ../output_files/test/bin/HuntingCameraTest
* --gtest_filter=HuntingCameraTest.HS_INTEGRATION_HunttingCamera_EXAMPLE_KeyControl2
*/
TEST_F(HuntingCameraTest, HS_INTEGRATION_HunttingCamera_EXAMPLE_KeyControl2)
{
MainThread::GetInstance()->Init();
TestManager::ResetTimeOut(1000);
HalTestTool::MockKeyClick("reset", 10); // Simulate pressing a button.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MainThread::GetInstance()->Runing();
}
/**
* @brief Construct a new test f object
* ../output_files/test/bin/HuntingCameraTest
* --gtest_filter=HuntingCameraTest.HS_INTEGRATION_HunttingCamera_EXAMPLE_KeyControlClick
*/
TEST_F(HuntingCameraTest, HS_INTEGRATION_HunttingCamera_EXAMPLE_KeyControlClick)
{
SetAllCamerasResult(mAllCamerasMock);
MockOtherSideIpcMissionReply(IpcMission::TEST);
MainThread::GetInstance()->Init();
TestManager::ResetTimeOut(1000 * 3);
HalTestTool::MockKeyClick("reset", 200); // Simulate pressing a button.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MainThread::GetInstance()->Runing();
}
/**
* @brief Construct a new test f object
* ../output_files/test/bin/HuntingCameraTest
* --gtest_filter=HuntingCameraTest.HS_INTEGRATION_HunttingCamera_EXAMPLE_LedControl
*/
TEST_F(HuntingCameraTest, HS_INTEGRATION_HunttingCamera_EXAMPLE_LedControl)
{
SetAllLedsResult(mAllLedsMock);
MockOtherSideIpcMissionReply(IpcMission::TEST);
MainThread::GetInstance()->Init();
TestManager::ResetTimeOut(1000 * 3);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MainThread::GetInstance()->Runing();
}
} // namespace DeviceManager_Mock_Test

View File

@ -0,0 +1,107 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 "HuntingCameraTest.h"
#include "GtestUsing.h"
#include "ILog.h"
#include "MainThread.h"
#include <thread>
const char *KEY_RESET = "reset";
const char *LED_DEVICE_STATUS = "device_status";
void MainThreadTest::CustomizationInit(void)
{
// Do nothing here to make sure test tool work.
}
HuntingCameraTest::HuntingCameraTest()
{
}
HuntingCameraTest::~HuntingCameraTest()
{
}
void HuntingCameraTest::SetUpTestCase()
{
CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END);
}
void HuntingCameraTest::TearDownTestCase()
{
ILogUnInit();
}
void HuntingCameraTest::SetUp()
{
CreateAllCamerasMcok();
std::shared_ptr<MainThread> mainThread = std::make_shared<MainThreadTest>();
MainThread::GetInstance(&mainThread);
HalTestTool::Init();
CreateAllKeysMcok();
CreateAllLedsMcok();
AppManagerTestTool::Init();
MissionManagerTestTool::Init();
mLinuxTest = LinuxTest::CreateLinuxTest();
std::shared_ptr<LinuxApiMock> test = mLinuxTest;
LinuxApiMock::GetInstance(&test);
LinuxApiMock::GetInstance()->Init();
McuManagerTestTool::Init(mLinuxTest);
HalTestTool::InitSdCardHal(mLinuxTest);
DeviceManagerTestTool::Init();
TestManager::Init();
}
void HuntingCameraTest::TearDown()
{
TestManager::UnInit();
MissionManagerTestTool::UnInit();
AppManagerTestTool::UnInit();
HalTestTool::UnInit();
LinuxApiMock::GetInstance()->UnInit();
mLinuxTest = std::make_shared<LinuxTest>();
std::shared_ptr<LinuxApiMock> test = std::make_shared<LinuxApiMock>();
LinuxApiMock::GetInstance(&test);
McuManagerTestTool::UnInit();
MainThread::GetInstance()->UnInit();
std::shared_ptr<MainThread> mainThread = std::make_shared<MainThread>();
MainThread::GetInstance(&mainThread);
DeviceManagerTestTool::UnInit();
DestroyAllCamerasMock();
DestroyAllKeysMock();
DestroyAllLedsMock();
}
void HuntingCameraTest::CreateAllCamerasMcok(void)
{
std::shared_ptr<VCameraHal> camera = HalTestTool::MakeCameraHalTest(CameraType::MAIN_CAMERA);
mAllCamerasMock[CameraType::MAIN_CAMERA] = camera;
}
void HuntingCameraTest::DestroyAllCamerasMock(void)
{
mAllCamerasMock.clear();
}
void HuntingCameraTest::CreateAllKeysMcok(void)
{
std::shared_ptr<VKeyHal> key = HalTestTool::MakeKeyHalTest(KEY_RESET);
mAllKeysMock[KEY_RESET] = key;
HalTestTool::SetAllKeysResult(mAllKeysMock);
}
void HuntingCameraTest::DestroyAllKeysMock(void)
{
mAllKeysMock.clear();
}
void HuntingCameraTest::CreateAllLedsMcok(void)
{
std::shared_ptr<VLedHal> led = HalTestTool::MakeLedHalTest(LED_DEVICE_STATUS);
mAllLedsMock[LED_DEVICE_STATUS] = led;
}
void HuntingCameraTest::DestroyAllLedsMock(void)
{
mAllLedsMock.clear();
}

View File

@ -0,0 +1,65 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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.
*/
#ifndef HUNTING_CAMERA_TEST_H
#define HUNTING_CAMERA_TEST_H
#include "AppManagerTestTool.h"
#include "DeviceManagerTestTool.h"
#include "GtestUsing.h"
#include "HalTestTool.h"
#include "LinuxApiMock.h"
#include "MainThread.h"
#include "McuManagerTestTool.h"
#include "MissionManagerTestTool.h"
#include "TestManager.h"
#include <memory>
class MainThreadTest : public MainThread
{
public:
MainThreadTest() = default;
virtual ~MainThreadTest() = default;
void CustomizationInit(void) override;
};
class HuntingCameraTest : public testing::Test,
public TestManager,
public MissionManagerTestTool,
public McuManagerTestTool,
public AppManagerTestTool,
public DeviceManagerTestTool,
virtual public HalTestTool
{
public:
HuntingCameraTest();
virtual ~HuntingCameraTest();
static void SetUpTestCase();
static void TearDownTestCase();
void SetUp() override;
void TearDown() override;
private:
void CreateAllCamerasMcok(void);
void DestroyAllCamerasMock(void);
void CreateAllKeysMcok(void);
void DestroyAllKeysMock(void);
void CreateAllLedsMcok(void);
void DestroyAllLedsMock(void);
protected:
std::shared_ptr<LinuxTest> mLinuxTest;
std::map<CameraType, std::shared_ptr<VCameraHal>> mAllCamerasMock;
std::map<std::string, std::shared_ptr<VKeyHal>> mAllKeysMock;
std::map<std::string, std::shared_ptr<VLedHal>> mAllLedsMock;
};
#endif

View File

@ -0,0 +1,67 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 "AppManagerTestTool.h"
#include "GtestUsing.h"
#include "HalTestTool.h"
#include "HuntingCameraTest.h"
#include "ILog.h"
#include "MainThread.h"
#include "McuManagerTestTool.h"
#include "MissionManagerTestTool.h"
#include "TestManager.h"
#include <thread>
namespace McuManager_Mock_Test
{
/**
* @brief Construct a new test f object
* ../output_files/test/bin/HuntingCameraTest
* --gtest_filter=HuntingCameraTest.HS_INTEGRATION_HunttingCamera_EXAMPLE_UartOpenSuccessButReadNothing
*/
TEST_F(HuntingCameraTest, HS_INTEGRATION_HunttingCamera_EXAMPLE_UartOpenSuccessButReadNothing)
{
McuManagerTestTool::MockMcuDeviceOpenSuccessButReadNothing(mLinuxTest);
MainThread::GetInstance()->Init();
TestManager::ResetTimeOut(1000 * 3);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MainThread::GetInstance()->Runing();
RemoveUpgradeFile();
}
/**
* @brief Construct a new test f object
* ../output_files/test/bin/HuntingCameraTest
* --gtest_filter=HuntingCameraTest.HS_INTEGRATION_HunttingCamera_EXAMPLE_OnMissionStateStart
*/
TEST_F(HuntingCameraTest, HS_INTEGRATION_HunttingCamera_EXAMPLE_OnMissionStateStart)
{
McuManagerTestTool::MockOtherSideIpcMissionReply(IpcMission::ON);
MainThread::GetInstance()->Init();
TestManager::ResetTimeOut(1000 * 3);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MainThread::GetInstance()->Runing();
}
/**
* @brief Construct a new test f object
* ../output_files/test/bin/HuntingCameraTest
* --gtest_filter=HuntingCameraTest.HS_INTEGRATION_HunttingCamera_EXAMPLE_PirTriggeredMissionStateStart
*/
TEST_F(HuntingCameraTest, HS_INTEGRATION_HunttingCamera_EXAMPLE_PirTriggeredMissionStateStart)
{
McuManagerTestTool::MockOtherSideIpcMissionReply(IpcMission::PIR_TRIGGERED);
MainThread::GetInstance()->Init();
TestManager::ResetTimeOut(1000 * 3);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MainThread::GetInstance()->Runing();
}
} // namespace McuManager_Mock_Test

View File

@ -0,0 +1,43 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 "AppManagerTestTool.h"
#include "GtestUsing.h"
#include "HalTestTool.h"
#include "HuntingCameraTest.h"
#include "ILog.h"
#include "MainThread.h"
#include "McuManagerTestTool.h"
#include "MissionManagerTestTool.h"
#include "TestManager.h"
#include <thread>
namespace MediaManager_Mock_Test
{
// ../output_files/test/bin/HuntingCameraTest
// --gtest_filter=HuntingCameraTest.INTEGRATION_HunttingCamera_EXAMPLE_MediaReprot
TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_EXAMPLE_MediaReprot)
{
SetAllCamerasResult(mAllCamerasMock);
MockReportCameraEvent("/tmp/test.MP4", CameraType::MAIN_CAMERA);
CreateUpgradeFile();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// McuManagerTestTool::MockMcuDeviceOpenFailed(mLinuxTest);
MainThread::GetInstance()->Init();
TestManager::ResetTimeOut(1000 * 4);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// MockAppPlayback();
MainThread::GetInstance()->Runing();
RemoveUpgradeFile();
}
} // namespace MediaManager_Mock_Test

View File

@ -0,0 +1,40 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 "AppManagerTestTool.h"
#include "GtestUsing.h"
#include "HalTestTool.h"
#include "HuntingCameraTest.h"
#include "ILog.h"
#include "MainThread.h"
#include "McuManagerTestTool.h"
#include "MissionManagerTestTool.h"
#include "TestManager.h"
#include <thread>
namespace TestMissionState_Mock_Test
{
/**
* @brief Construct a new test f object
* ../output_files/test/bin/HuntingCameraTest
* --gtest_filter=HuntingCameraTest.HS_INTEGRATION_HunttingCamera_EXAMPLE_TestMissionResetKeyCapture
*/
TEST_F(HuntingCameraTest, HS_INTEGRATION_HunttingCamera_EXAMPLE_TestMissionResetKeyCapture)
{
McuManagerTestTool::MockOtherSideIpcMissionReply(IpcMission::TEST);
MainThread::GetInstance()->Init();
TestManager::ResetTimeOut(1000 * 3);
HalTestTool::MockKeyClick("reset", 200); // Simulate pressing a button.
MainThread::GetInstance()->Runing();
}
}

View File

@ -0,0 +1,4 @@
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tool/CMakeLists.txt")
add_subdirectory(tool)
endif()

View File

@ -0,0 +1,35 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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.
*/
#ifndef MISSION_MANAGER_TEST_TOOL_H
#define MISSION_MANAGER_TEST_TOOL_H
#include "GtestUsing.h"
#include "IMissionManager.h"
#include <memory>
class MissionManagerTestTool
{
public:
MissionManagerTestTool() = default;
virtual ~MissionManagerTestTool() = default;
void Init(void);
void UnInit(void);
protected:
void CreateUpgradeFile(void);
void RemoveUpgradeFile(void);
private:
std::shared_ptr<IMissionManager> mMissionManagerMock;
};
#endif

View File

@ -0,0 +1,86 @@
# include(${CMAKE_SOURCE_DIR}/build/independent_source.cmake)
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
# include(${APPLICATION_SOURCE_PATH}/VersionRelease/build/hunting_camera.cmake)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR_IPCSDK}/tools/version_release)
include_directories(
./src
# ${HUNTTING_MAIN_INCLUDE_PATH}
# ${MIDDLEWARE_SOURCE_PATH}/McuManager/include
# ${MIDDLEWARE_SOURCE_PATH}/McuManager/src
${UTILS_SOURCE_PATH}/UpgradeTool/include
${UTILS_SOURCE_PATH}/StatusCode/include
${UTILS_SOURCE_PATH}/Log/include
${TEST_SOURCE_PATH}
# ${TEST_SOURCE_PATH}/application/MissionManager/tool/include
# ${TEST_SOURCE_PATH}/middleware/McuManager/tool/include
# ${TEST_SOURCE_PATH}/middleware/AppManager/tool/include
# ${TEST_SOURCE_PATH}/utils/LinuxApiMock/include
# ${TEST_SOURCE_PATH}/utils/TestManager/include
# ${TEST_SOURCE_PATH}/utils/McuProtocol/tool/include
# ${TEST_SOURCE_PATH}/utils/UartDevice/tool/include
# ${TEST_SOURCE_PATH}/hal/tool/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include
)
link_directories(
${LIBS_OUTPUT_PATH}
${EXTERNAL_LIBS_OUTPUT_PATH}
)
aux_source_directory(. SRC_FILES_MAIN)
aux_source_directory(./src SRC_FILES)
if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX})
aux_source_directory(./src_mock SRC_FILES)
endif()
add_definitions(-DHUNTING_UPGRADE_BUILD_FILE="../ipc-sdk/middleware/HuntingUpgrade/build/hunting_upgrade.cmake")
set(TARGET_NAME VersionReleaseTool)
add_executable(${TARGET_NAME} ${SRC_FILES_MAIN} ${SRC_FILES})
target_link_libraries(${TARGET_NAME} UpgradeTool StatusCode Log gtest gmock pthread)
if(${TEST_COVERAGE} MATCHES "true")
target_link_libraries(${TARGET_NAME} gcov)
endif()
if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true")
add_custom_target(
VersionReleaseTool_code_check
COMMAND ${CLANG_TIDY_EXE}
-checks='${CLANG_TIDY_CHECKS}'
--header-filter=.*
--system-headers=false
${SRC_FILES}
${SRC_FILES_MAIN}
${CLANG_TIDY_CONFIG}
# --line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/gtest.h\"}]'
--line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/*.h\"}]'
-p ${PLATFORM_PATH}/cmake-shell
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/application/VersionRelease
)
add_custom_command(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make VersionReleaseTool_code_check
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
file(GLOB_RECURSE HEADER_FILES *.h)
add_custom_target(
VersionReleaseTool_code_format
COMMAND ${CLANG_FORMAT_EXE}
-style=file
-i ${SRC_FILES} ${SRC_FILES_MAIN} ${HEADER_FILES}
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/application/VersionRelease
)
add_custom_command(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make VersionReleaseTool_code_check
COMMAND make VersionReleaseTool_code_format
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
endif()
define_file_name(${TARGET_NAME})

View File

@ -0,0 +1,31 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 "ArgvAnalysis.h"
#include "ILog.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
#include <unistd.h>
int main(int argc, char *argv[])
{
CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END);
ArgvAnalysis::GetInstance()->Analyze(argc, argv);
testing::InitGoogleTest(&argc, argv);
int result = RUN_ALL_TESTS();
ILogUnInit();
DestroyLogModule();
return result;
}

View File

@ -0,0 +1,78 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 "ArgvAnalysis.h"
#include "ILog.h"
const char *V_SOURCE_FILE = "v_source_file";
const char *V_OUTPUT_FILE = "v_output_file";
std::shared_ptr<ArgvAnalysis> &ArgvAnalysis::GetInstance(std::shared_ptr<ArgvAnalysis> *impl)
{
static auto instance = std::make_shared<ArgvAnalysis>();
if (impl) {
if (instance.use_count() == 1) {
LogInfo("Instance changed succeed.\n");
instance = *impl;
}
else {
LogError("Can't changing the instance becase of using by some one.\n");
}
}
return instance;
}
void ArgvAnalysis::Analyze(int argc, char *argv[])
{
LogInfo("argc: %d\n", argc);
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
size_t pos = arg.find('=');
if (pos != std::string::npos) {
std::string key = arg.substr(0, pos);
std::string value = arg.substr(pos + 1);
// remove'--'
if (key.substr(0, 2) == "--") {
key = key.substr(2);
}
mOptions[key] = value;
}
else {
LogError("Invalid argument format: %s\n", arg.c_str());
return;
}
}
for (const auto &pair : mOptions) {
LogInfo("Key: %s, Value: %s\n", pair.first.c_str(), pair.second.c_str());
}
}
std::string ArgvAnalysis::GetSourceFile(void)
{
auto it = mOptions.find(V_SOURCE_FILE);
if (it != mOptions.end()) {
return it->second;
}
LogWarning("Can't find the source file.\n");
return "";
}
std::string ArgvAnalysis::GetOutputFile(void)
{
auto it = mOptions.find(V_OUTPUT_FILE);
if (it != mOptions.end()) {
return it->second;
}
LogWarning("Can't find the source file.\n");
return "";
}

View File

@ -0,0 +1,33 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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.
*/
#ifndef ARGV_ANALYSIS_H
#define ARGV_ANALYSIS_H
#include "StatusCode.h"
#include <map>
#include <thread>
class ArgvAnalysis
{
public:
ArgvAnalysis() = default;
virtual ~ArgvAnalysis() = default;
static std::shared_ptr<ArgvAnalysis> &GetInstance(std::shared_ptr<ArgvAnalysis> *impl = nullptr);
void Analyze(int argc, char *argv[]);
std::string GetSourceFile(void);
std::string GetOutputFile(void);
private:
std::map<std::string, std::string> mOptions;
};
#endif

View File

@ -0,0 +1,51 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 "VersionBase.h"
#include <fstream>
#include <iostream>
#include <regex>
#include <sstream>
#include <string>
void VersionBase::GetVersion(const std::string &fileName, const std::regex &pattern, std::string &version)
{
std::ifstream file(fileName);
if (!file.is_open()) {
std::cerr << "无法打开文件: " << fileName << std::endl;
return;
}
std::string line;
std::string versionGet;
// std::regex pattern(R"(add_definitions\(-DHUNTING_CAMERA_VERSION=\"([^"]+)\"\))");
while (std::getline(file, line)) {
std::smatch match;
if (std::regex_search(line, match, pattern)) {
// 如果找到匹配项match[1]将包含捕获的版本号
versionGet = match[1].str();
break; // 找到后退出循环
}
}
file.close();
if (versionGet.empty()) {
std::cerr << "在文件中未找到指定的行或版本号!" << std::endl;
return;
}
std::cout << "提取的版本号: " << versionGet << std::endl;
return;
}

View File

@ -0,0 +1,26 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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.
*/
#ifndef VERSION_BASE_H
#define VERSION_BASE_H
#include <string>
#include <regex>
class VersionBase
{
public:
VersionBase() = default;
virtual ~VersionBase() = default;
void GetVersion(const std::string &fileName, const std::regex &pattern, std::string &version);
};
#endif

View File

@ -0,0 +1,75 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 "VersionReleaseTool.h"
#include "ArgvAnalysis.h"
#include "GtestUsing.h"
#include "ILog.h"
#include "UpgradeTool.h"
#include <thread>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <regex>
namespace VersionReleaseTool
{
/**
* @brief Construct a new TEST object
* ../output_files/test/bin/VersionReleaseTool --v_source_file=./test.bin --v_output_file=./test_output.bin
*/
static void GetVersion(std::string &versiond)
{
const std::string filename = HUNTING_UPGRADE_BUILD_FILE;
std::ifstream file(filename);
if (!file.is_open()) {
std::cerr << "无法打开文件: " << filename << std::endl;
return ;
}
std::string line;
std::string version;
std::regex pattern(R"(add_definitions\(-DHUNTING_CAMERA_VERSION=\"([^"]+)\"\))");
while (std::getline(file, line)) {
std::smatch match;
if (std::regex_search(line, match, pattern)) {
// 如果找到匹配项match[1]将包含捕获的版本号
version = match[1].str();
break; // 找到后退出循环
}
}
file.close();
if (version.empty()) {
std::cerr << "在文件中未找到指定的行或版本号!" << std::endl;
return ;
}
std::cout << "提取的版本号: " << version << std::endl;
return ;
}
TEST(VersionReleaseTool, Version)
{
std::string sourceFile = ArgvAnalysis::GetInstance()->GetSourceFile();
std::string outputFile = ArgvAnalysis::GetInstance()->GetOutputFile();
std::string version;
GetVersion(version);
UpgradeTool::GetInstance()->PackFile(sourceFile, outputFile, "1.0.0", "product", "project", "upgradeType");
}
} // namespace VersionReleaseTool

View File

@ -0,0 +1,18 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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.
*/
#ifndef VERSION_RELEASE_TOOL_H
#define VERSION_RELEASE_TOOL_H
#endif

71
test/hal/CMakeLists.txt Normal file
View File

@ -0,0 +1,71 @@
# include(${CMAKE_SOURCE_DIR}/build/independent_source.cmake)
set(EXECUTABLE_OUTPUT_PATH ${TEST_OUTPUT_PATH}/bin)
include_directories(
${UTILS_SOURCE_PATH}/StatusCode/include
${UTILS_SOURCE_PATH}/Log/include
${HAL_SOURCE_PATH}/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include
)
link_directories(
${LIBS_OUTPUT_PATH}
${EXTERNAL_LIBS_OUTPUT_PATH}
${TEST_TOOLS_OUTPUT_PATH}
)
aux_source_directory(. SRC_FILES)
aux_source_directory(./src SRC_FILES)
set(TARGET_NAME HalTest)
add_executable(${TARGET_NAME} ${SRC_FILES})
target_link_libraries(${TARGET_NAME} HalTestTool gtest gmock pthread)
target_link_libraries(${TARGET_NAME} Hal Log LinuxApiMock)
if(${TEST_COVERAGE} MATCHES "true")
target_link_libraries(${TARGET_NAME} gcov)
endif()
if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true")
add_custom_target(
HalTest_code_check
COMMAND ${CLANG_TIDY_EXE}
-checks='${CLANG_TIDY_CHECKS}'
--header-filter=.*
--system-headers=false
${SRC_FILES}
${CLANG_TIDY_CONFIG}
# --line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/gtest.h\"}]'
--line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/*.h\"}]'
-p ${PLATFORM_PATH}/cmake-shell
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/hal
)
add_custom_command(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make HalTest_code_check
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
file(GLOB_RECURSE HEADER_FILES *.h)
add_custom_target(
HalTest_code_format
COMMAND ${CLANG_FORMAT_EXE}
-style=file
-i ${SRC_FILES} ${SRC_FILES_MAIN} ${HEADER_FILES}
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/hal
)
add_custom_command(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make HalTest_code_check
COMMAND make HalTest_code_format
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
endif()
define_file_name(${TARGET_NAME})
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tool/CMakeLists.txt")
add_subdirectory(tool)
endif()

23
test/hal/mainTest.cpp Normal file
View File

@ -0,0 +1,23 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
#include <unistd.h>
int main(int argc, char *argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

34
test/hal/src/IHalTest.cpp Normal file
View File

@ -0,0 +1,34 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 "IHal.h"
#include "ILog.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
namespace IHalTest
{
// ../output_files/test/bin/IHalTest --gtest_filter=IHalTest.Demo
TEST(IHalTest, Demo)
{
ILogInit(LOG_EASYLOGGING);
CreateHalModule();
StatusCode code = IHalInit();
if (IsCodeOK(code)) {
PrintStringCode(code);
}
IHalUnInit();
DestroyHalModule();
ILogUnInit();
}
} // namespace IHalTest

View File

@ -0,0 +1,87 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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.
*/
#ifndef HAL_TEST_TOOL_H
#define HAL_TEST_TOOL_H
#include "GtestUsing.h"
#include "IHalCpp.h"
#include "LedControl.h"
#include "LinuxApiMock.h"
class HalTestTool
{
public:
HalTestTool() = default;
virtual ~HalTestTool() = default;
void Init(void);
void InitSdCardHal(std::shared_ptr<LinuxTest> &mock);
void UnInit(void);
/**
* @brief Set the All Keys Result object
* Due to the abstract design of the HAL for the number and type of buttons, it is necessary to externally
* determine the instances of the buttons.
* @param allKeys The instances of the buttons.
*/
void SetAllKeysResult(std::map<std::string, std::shared_ptr<VKeyHal>> &allKeys);
void SetAllLedsResult(std::map<std::string, std::shared_ptr<VLedHal>> &allLeds);
void SetAllCamerasResult(std::map<CameraType, std::shared_ptr<VCameraHal>> &allCameras);
void MockKeyClick(const std::string &keyName, const unsigned int &pressingTimeMs = 200);
void SetLedStateExpectations(const std::string &ledName, const LedState &state, const unsigned int &aliveTimeMs,
const unsigned int &blinkTimeMs);
void MockReportCameraEvent(const std::string &fileName, const CameraType &cameraType);
protected:
virtual void DeviceManagerNotice(const std::string &keyName, const unsigned int &pressingTimeMs);
private:
void KeyEventHappendOnce(std::shared_ptr<VKeyHal> &vMock, const unsigned int &pressingTimeMs);
private:
void HalMockInit(std::shared_ptr<IHalCpp> &vMock);
private: // About key hal
void SetAllKeysResult(std::shared_ptr<IHalCpp> &vMock, std::map<std::string, std::shared_ptr<VKeyHal>> &allKeys);
std::shared_ptr<VKeyHal> SearchKey(const std::string &keyName);
void InitAllKeysMock(std::map<std::string, std::shared_ptr<VKeyHal>> &allKeys);
void InitKeysMock(std::shared_ptr<VKeyHal> &vMock);
private: // About led hal
void SetAllLedsResult(std::shared_ptr<IHalCpp> &vMock, std::map<std::string, std::shared_ptr<VLedHal>> &allLeds);
std::shared_ptr<VLedHal> &SearchLed(const std::string &ledName);
void InitAllLedsMock(std::map<std::string, std::shared_ptr<VLedHal>> &allLeds);
void InitLedsMock(std::shared_ptr<VLedHal> &vMock);
private: // About camera hal
void SetAllCamerasResult(std::shared_ptr<IHalCpp> &vMock,
std::map<CameraType, std::shared_ptr<VCameraHal>> &allCameras);
std::shared_ptr<VCameraHal> SearchCamera(const CameraType &cameraType);
void InitAllCamerasMock(std::map<CameraType, std::shared_ptr<VCameraHal>> &allCameras);
void InitCamerasMock(std::shared_ptr<VCameraHal> &vMock);
protected: // About sd card hal
void MockSdCardRemove(std::shared_ptr<LinuxTest> &mock);
void MockSdCardInsert(std::shared_ptr<LinuxTest> &mock);
public:
static std::shared_ptr<VKeyHal> MakeKeyHalTest(const std::string &keyName);
static std::shared_ptr<VLedHal> MakeLedHalTest(const std::string &ledName);
static std::shared_ptr<VCameraHal> MakeCameraHalTest(const CameraType &type);
private:
std::shared_ptr<IHalCpp> mHalMock;
std::map<std::string, std::shared_ptr<VKeyHal>> mAllKeys;
std::map<std::string, std::shared_ptr<VLedHal>> mAllLeds;
std::map<CameraType, std::shared_ptr<VCameraHal>> mAllCameras;
std::shared_ptr<VSdCardHal> mSdCardHal;
};
#endif

View File

@ -0,0 +1,87 @@
# include(${CMAKE_SOURCE_DIR}/build/independent_source.cmake)
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
include(${MIDDLEWARE_SOURCE_PATH}/AppManager/build/app_manager.cmake)
set(EXECUTABLE_OUTPUT_PATH ${TEST_OUTPUT_PATH}/bin)
include_directories(
./src
./include
./tool/include
${UTILS_SOURCE_PATH}/Log/include
${UTILS_SOURCE_PATH}/StatusCode/include
${UTILS_SOURCE_PATH}/KeyControl/include
${UTILS_SOURCE_PATH}/LedControl/include
${UTILS_SOURCE_PATH}/WebServer/include
${HAL_SOURCE_PATH}/include
${MIDDLEWARE_SOURCE_PATH}/AppManager/include
${MIDDLEWARE_SOURCE_PATH}/AppManager/src
${TEST_SOURCE_PATH}
${TEST_SOURCE_PATH}/hal/tool/include
${TEST_SOURCE_PATH}/utils/LinuxApiMock/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include
)
link_directories(
${LIBS_OUTPUT_PATH}
${EXTERNAL_LIBS_OUTPUT_PATH}
${TEST_TOOLS_OUTPUT_PATH}
)
aux_source_directory(. SRC_FILES_MAIN)
aux_source_directory(./src SRC_FILES)
if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX})
aux_source_directory(./src_mock SRC_FILES)
endif()
set(TARGET_NAME AppManagerTest)
add_executable(${TARGET_NAME} ${SRC_FILES_MAIN} ${SRC_FILES})
target_link_libraries(${TARGET_NAME} AppManagerTestTool HalTestTool gtest gmock pthread)
target_link_libraries(${TARGET_NAME} AppManager Servers KeyControl LedControl Hal LinuxApiMock)
if(${TEST_COVERAGE} MATCHES "true")
target_link_libraries(${TARGET_NAME} gcov)
endif()
if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true")
add_custom_target(
AppManagerTest_code_check
COMMAND ${CLANG_TIDY_EXE}
-checks='${CLANG_TIDY_CHECKS}'
--header-filter=.*
--system-headers=false
${SRC_FILES}
${CLANG_TIDY_CONFIG}
# --line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/gtest.h\"}]'
--line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/*.h\"}]'
-p ${PLATFORM_PATH}/cmake-shell
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/AppManager
)
add_custom_command(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make AppManagerTest_code_check
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
file(GLOB_RECURSE HEADER_FILES *.h)
add_custom_target(
AppManagerTest_code_format
COMMAND ${CLANG_FORMAT_EXE}
-style=file
-i ${SRC_FILES} ${SRC_FILES_MAIN} ${HEADER_FILES}
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/AppManager
)
add_custom_command(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make AppManagerTest_code_check
COMMAND make AppManagerTest_code_format
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
endif()
define_file_name(${TARGET_NAME})
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tool/CMakeLists.txt")
add_subdirectory(tool)
endif()

View File

@ -0,0 +1,23 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
#include <unistd.h>
int main(int argc, char *argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@ -0,0 +1,384 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 "AppManagerTestTool.h"
#include "HalTestTool.h"
#include "IAppManager.h"
#include "ILog.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
namespace AppManagerTest
{
class AppManagerTest : public testing::Test, public AppManagerTestTool, public HalTestTool
{
public:
AppManagerTest() : mAppParam(APP_MANAGER_DEVICE_IP, APP_MANAGER_HTTP_SERVER_PORT, APP_MANAGER_TCP_SERVER_PORT)
{
}
virtual ~AppManagerTest()
{
}
static void SetUpTestCase()
{
CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END);
}
static void TearDownTestCase()
{
ILogUnInit();
DestroyLogModule();
}
virtual void SetUp()
{
HalTestTool::Init();
AppManagerTestTool::Init();
CreateHalCppModule();
CreateAppManagerModule();
}
virtual void TearDown()
{
AppManagerTestTool::UnInit();
HalTestTool::UnInit();
DestroyAppManagerModule();
DestroyHalCppModule();
}
protected:
const AppParam mAppParam;
};
// ../output_files/test/bin/AppManagerTest --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_Demo0
TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_Demo0)
{
std::shared_ptr<VAppMonitor> monitor = AppManagerTestTool::MakeMonitorMock();
IAppManager::GetInstance()->Init(mAppParam);
IAppManager::GetInstance()->SetAppMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockAppClientConnect();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockSetRecordingStatus(RecordingStatus::RECORDING_START);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IAppManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/AppManagerTest --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_Demo
TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_Demo)
{
std::shared_ptr<VAppMonitor> monitor = AppManagerTestTool::MakeMonitorMock();
IAppManager::GetInstance()->Init(mAppParam);
IAppManager::GetInstance()->SetAppMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
AppManagerTestTool::MockGetProductInfo();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
IAppManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/AppManagerTest --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_Upload
TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_Upload)
{
std::shared_ptr<VAppMonitor> monitor = AppManagerTestTool::MakeMonitorMock();
IAppManager::GetInstance()->Init(mAppParam);
IAppManager::GetInstance()->SetAppMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockUploadFiles();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
IAppManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/AppManagerTest
// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_GetDeviceAttr
TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_GetDeviceAttr)
{
std::shared_ptr<VAppMonitor> monitor = AppManagerTestTool::MakeMonitorMock();
IAppManager::GetInstance()->Init(mAppParam);
IAppManager::GetInstance()->SetAppMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
AppManagerTestTool::MockGetDeviceAttr();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
IAppManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/AppManagerTest
// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_GetMediaInfo
TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_GetMediaInfo)
{
std::shared_ptr<VAppMonitor> monitor = AppManagerTestTool::MakeMonitorMock();
IAppManager::GetInstance()->Init(mAppParam);
IAppManager::GetInstance()->SetAppMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
AppManagerTestTool::MockGetMediaInfo();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
IAppManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/AppManagerTest
// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_GetSdCardInfo
TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_GetSdCardInfo)
{
std::shared_ptr<VAppMonitor> monitor = AppManagerTestTool::MakeMonitorMock();
IAppManager::GetInstance()->Init(mAppParam);
IAppManager::GetInstance()->SetAppMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
AppManagerTestTool::MockGetSdCardInfo();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
IAppManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/AppManagerTest
// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_GetBatteryInfo
TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_GetBatteryInfo)
{
std::shared_ptr<VAppMonitor> monitor = AppManagerTestTool::MakeMonitorMock();
IAppManager::GetInstance()->Init(mAppParam);
IAppManager::GetInstance()->SetAppMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
AppManagerTestTool::MockGetBatteryInfo();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
IAppManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/AppManagerTest
// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_SetDateTime
TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_SetDateTime)
{
std::shared_ptr<VAppMonitor> monitor = AppManagerTestTool::MakeMonitorMock();
IAppManager::GetInstance()->Init(mAppParam);
IAppManager::GetInstance()->SetAppMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockSetDateTime();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
IAppManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/AppManagerTest
// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_SetTimeZone
TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_SetTimeZone)
{
std::shared_ptr<VAppMonitor> monitor = AppManagerTestTool::MakeMonitorMock();
IAppManager::GetInstance()->Init(mAppParam);
IAppManager::GetInstance()->SetAppMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockSetTimeZone();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
IAppManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/AppManagerTest
// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_GetParamValue
TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_GetParamValue)
{
std::shared_ptr<VAppMonitor> monitor = AppManagerTestTool::MakeMonitorMock();
IAppManager::GetInstance()->Init(mAppParam);
IAppManager::GetInstance()->SetAppMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
AppManagerTestTool::MockGetParamValue("all");
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
IAppManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/AppManagerTest
// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_GetParamValue2
TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_GetParamValue2)
{
std::shared_ptr<VAppMonitor> monitor = AppManagerTestTool::MakeMonitorMock();
IAppManager::GetInstance()->Init(mAppParam);
IAppManager::GetInstance()->SetAppMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
AppManagerTestTool::MockGetParamValue("rec");
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
IAppManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/AppManagerTest
// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_GetParamItems
TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_GetParamItems)
{
std::shared_ptr<VAppMonitor> monitor = AppManagerTestTool::MakeMonitorMock();
IAppManager::GetInstance()->Init(mAppParam);
IAppManager::GetInstance()->SetAppMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
AppManagerTestTool::MockGetParamItems("all");
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
IAppManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/AppManagerTest
// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_GetParamItems2
TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_GetParamItems2)
{
std::shared_ptr<VAppMonitor> monitor = AppManagerTestTool::MakeMonitorMock();
IAppManager::GetInstance()->Init(mAppParam);
IAppManager::GetInstance()->SetAppMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
AppManagerTestTool::MockGetParamItems("mic");
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
IAppManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/AppManagerTest
// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_GetCapability
TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_GetCapability)
{
std::shared_ptr<VAppMonitor> monitor = AppManagerTestTool::MakeMonitorMock();
IAppManager::GetInstance()->Init(mAppParam);
IAppManager::GetInstance()->SetAppMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
AppManagerTestTool::MockGetCapability();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
IAppManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/AppManagerTest
// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_GetLockVideoStatus
TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_GetLockVideoStatus)
{
std::shared_ptr<VAppMonitor> monitor = AppManagerTestTool::MakeMonitorMock();
IAppManager::GetInstance()->Init(mAppParam);
IAppManager::GetInstance()->SetAppMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
AppManagerTestTool::MockGetLockVideoStatus();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
IAppManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/AppManagerTest
// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_GetStorageInfo
TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_GetStorageInfo)
{
std::shared_ptr<VAppMonitor> monitor = AppManagerTestTool::MakeMonitorMock();
IAppManager::GetInstance()->Init(mAppParam);
IAppManager::GetInstance()->SetAppMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
AppManagerTestTool::MockGetStorageInfo();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
IAppManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/AppManagerTest
// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_GetStorageFileList
TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_GetStorageFileList)
{
std::shared_ptr<VAppMonitor> monitor = AppManagerTestTool::MakeMonitorMock();
IAppManager::GetInstance()->Init(mAppParam);
IAppManager::GetInstance()->SetAppMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
AppManagerTestTool::MockGetStorageFileList();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
IAppManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/AppManagerTest
// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_SetParamValue
TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_SetParamValue)
{
std::shared_ptr<VAppMonitor> monitor = AppManagerTestTool::MakeMonitorMock();
IAppManager::GetInstance()->Init(mAppParam);
IAppManager::GetInstance()->SetAppMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockSetParamValue();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
IAppManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/AppManagerTest
// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_EnterRecorder
TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_EnterRecorder)
{
std::shared_ptr<VAppMonitor> monitor = AppManagerTestTool::MakeMonitorMock();
IAppManager::GetInstance()->Init(mAppParam);
IAppManager::GetInstance()->SetAppMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockEnterRecorder();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
IAppManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/AppManagerTest
// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_AppPlayback
TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_AppPlayback)
{
std::shared_ptr<VAppMonitor> monitor = AppManagerTestTool::MakeMonitorMock();
IAppManager::GetInstance()->Init(mAppParam);
IAppManager::GetInstance()->SetAppMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockAppPlayback();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
IAppManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/AppManagerTest
// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_SetRecordingStatus
TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_SetRecordingStatus)
{
std::shared_ptr<VAppMonitor> monitor = AppManagerTestTool::MakeMonitorMock();
IAppManager::GetInstance()->Init(mAppParam);
IAppManager::GetInstance()->SetAppMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockAppClientConnect();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockSetRecordingStatus(RecordingStatus::RECORDING_START);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IAppManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/AppManagerTest
// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_SetMicrophoneStatus
TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_SetMicrophoneStatus)
{
std::shared_ptr<VAppMonitor> monitor = AppManagerTestTool::MakeMonitorMock();
IAppManager::GetInstance()->Init(mAppParam);
IAppManager::GetInstance()->SetAppMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockAppClientConnect();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockSetMicrophoneStatus(MicrophoneStatus::ON);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IAppManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/AppManagerTest
// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_SetBatteryStatus
TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_SetBatteryStatus)
{
std::shared_ptr<VAppMonitor> monitor = AppManagerTestTool::MakeMonitorMock();
IAppManager::GetInstance()->Init(mAppParam);
IAppManager::GetInstance()->SetAppMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockAppClientConnect();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockSetBatteryStatus(BatteryStatus::CHARGING, 20);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IAppManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/AppManagerTest
// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_SetSdCardStatus
TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_SetSdCardStatus)
{
std::shared_ptr<VAppMonitor> monitor = AppManagerTestTool::MakeMonitorMock();
IAppManager::GetInstance()->Init(mAppParam);
IAppManager::GetInstance()->SetAppMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockAppClientConnect();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockSetSdCardStatus(SdCardStatus::NOT_INSERTED);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IAppManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/AppManagerTest
// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_DeletedFileMessage
TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_DeletedFileMessage)
{
std::shared_ptr<VAppMonitor> monitor = AppManagerTestTool::MakeMonitorMock();
IAppManager::GetInstance()->Init(mAppParam);
IAppManager::GetInstance()->SetAppMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockAppClientConnect();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockDeletedFileMessage("file_name", StorageFileType::VIDEO);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IAppManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/AppManagerTest
// --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_CreatedFileMessage
TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_CreatedFileMessage)
{
std::shared_ptr<VAppMonitor> monitor = AppManagerTestTool::MakeMonitorMock();
IAppManager::GetInstance()->Init(mAppParam);
IAppManager::GetInstance()->SetAppMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockAppClientConnect();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockCreatedFileMessage("file_name", StorageFileType::VIDEO);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IAppManager::GetInstance()->UnInit();
}
} // namespace AppManagerTest

View File

@ -0,0 +1,70 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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.
*/
#ifndef APP_MANAGER_TEST_TOOL_H
#define APP_MANAGER_TEST_TOOL_H
#include "GtestUsing.h"
#include "IAppManager.h"
#include <memory>
class AppManagerTestTool
{
public:
AppManagerTestTool();
virtual ~AppManagerTestTool() = default;
void Init(void);
void UnInit(void);
protected: // About http
void MockGetProductInfo(void);
void MockGetDeviceAttr(void);
void MockGetMediaInfo(void);
void MockGetSdCardInfo(void);
void MockGetBatteryInfo(void);
void MockSetDateTime(void);
void MockSetTimeZone(void);
void MockUploadFiles(void);
void MockGetParamValue(const std::string &paramName);
void MockGetParamItems(const std::string &paramName);
void MockGetCapability(void);
void MockGetLockVideoStatus(void);
void MockGetStorageInfo(void);
void MockGetStorageFileList(void);
void MockSetParamValue(void);
void MockEnterRecorder(void);
void MockAppPlayback(void);
void MockMonitorSetFileList(std::vector<AppGetFileList> &files);
protected: // About TCP
void MockAppClientConnect(void);
void MockSetRecordingStatus(const RecordingStatus &status);
void MockSetMicrophoneStatus(const MicrophoneStatus &status);
void MockSetBatteryStatus(const BatteryStatus &status, const int &batteryCapacity);
void MockSetSdCardStatus(const SdCardStatus &status);
void MockDeletedFileMessage(const std::string &file, const StorageFileType &type);
void MockCreatedFileMessage(const std::string &file, const StorageFileType &type);
private:
void AppManagerMockInit(std::shared_ptr<IAppManager> &vMock);
void AppMonitorInit(std::shared_ptr<VAppMonitor> &vMock);
private:
std::shared_ptr<IAppManager> mAppManagerMock;
std::shared_ptr<VAppMonitor> mAppMonitorMock;
void *mAppClientTool;
std::shared_ptr<VAppClient> mAppClient;
public:
static std::shared_ptr<VAppMonitor> MakeMonitorMock(void);
};
#endif

View File

@ -0,0 +1,9 @@
# cmake_minimum_required(VERSION 2.8.0)
add_subdirectory(IpcConfig)
add_subdirectory(McuManager)
add_subdirectory(McuAskBase)
add_subdirectory(DeviceManager)
add_subdirectory(AppManager)
add_subdirectory(MediaManager)
add_subdirectory(HuntingUpgrade)

View File

@ -0,0 +1,85 @@
# include(${CMAKE_SOURCE_DIR}/build/independent_source.cmake)
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
set(EXECUTABLE_OUTPUT_PATH ${TEST_OUTPUT_PATH}/bin)
include_directories(
./src
./include
./tool/include
${UTILS_SOURCE_PATH}/Log/include
${UTILS_SOURCE_PATH}/StatusCode/include
${UTILS_SOURCE_PATH}/KeyControl/include
${UTILS_SOURCE_PATH}/LedControl/include
${HAL_SOURCE_PATH}/include
${MIDDLEWARE_SOURCE_PATH}/DeviceManager/include
${MIDDLEWARE_SOURCE_PATH}/DeviceManager/src
${TEST_SOURCE_PATH}
${TEST_SOURCE_PATH}/hal/tool/include
${TEST_SOURCE_PATH}/utils/LinuxApiMock/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include
)
link_directories(
${LIBS_OUTPUT_PATH}
${EXTERNAL_LIBS_OUTPUT_PATH}
${TEST_TOOLS_OUTPUT_PATH}
)
aux_source_directory(. SRC_FILES_MAIN)
aux_source_directory(./src SRC_FILES)
if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX})
aux_source_directory(./src_mock SRC_FILES)
endif()
set(TARGET_NAME DeviceManagerTest)
add_executable(${TARGET_NAME} ${SRC_FILES_MAIN} ${SRC_FILES})
target_link_libraries(${TARGET_NAME} DeviceManager DeviceManagerTestTool HalTestTool gtest gmock pthread)
target_link_libraries(${TARGET_NAME} Hal LinuxApiMock)
if(${TEST_COVERAGE} MATCHES "true")
target_link_libraries(${TARGET_NAME} gcov)
endif()
if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true")
add_custom_target(
DeviceManagerTest_code_check
COMMAND ${CLANG_TIDY_EXE}
-checks='${CLANG_TIDY_CHECKS}'
--header-filter=.*
--system-headers=false
${SRC_FILES}
${CLANG_TIDY_CONFIG}
# --line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/gtest.h\"}]'
--line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/*.h\"}]'
-p ${PLATFORM_PATH}/cmake-shell
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/DeviceManager
)
add_custom_command(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make DeviceManagerTest_code_check
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
file(GLOB_RECURSE HEADER_FILES *.h)
add_custom_target(
DeviceManagerTest_code_format
COMMAND ${CLANG_FORMAT_EXE}
-style=file
-i ${SRC_FILES} ${SRC_FILES_MAIN} ${HEADER_FILES}
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/DeviceManager
)
add_custom_command(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make DeviceManagerTest_code_check
COMMAND make DeviceManagerTest_code_format
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
endif()
define_file_name(${TARGET_NAME})
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tool/CMakeLists.txt")
add_subdirectory(tool)
endif()

View File

@ -0,0 +1,23 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
#include <unistd.h>
int main(int argc, char *argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@ -0,0 +1,215 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 "DeviceManagerTestTool.h"
#include "HalTestTool.h"
#include "IDeviceManager.h"
#include "ILog.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
namespace DeviceManagerTest
{
const char *KEY_TEST = "KEY_TEST";
const char *LED_TEST = "LED_TEST";
class DeviceManagerTest : public testing::Test, virtual public HalTestTool, public DeviceManagerTestTool
{
public:
DeviceManagerTest()
{
}
virtual ~DeviceManagerTest()
{
}
static void SetUpTestCase()
{
CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END);
}
static void TearDownTestCase()
{
ILogUnInit();
}
virtual void SetUp()
{
CreateAllKeysMcok();
HalTestTool::Init();
DeviceManagerTestTool::Init();
CreateHalCppModule();
CreateDeviceManagerModule();
// CreateMcuManager();
}
virtual void TearDown()
{
HalTestTool::UnInit();
DeviceManagerTestTool::UnInit();
DestroyDeviceManagerModule();
DestroyAllKeysMock();
}
private:
void CreateAllKeysMcok(void)
{
std::shared_ptr<VKeyHal> key = HalTestTool::MakeKeyHalTest(KEY_TEST);
mAllKeysMock[KEY_TEST] = key;
std::shared_ptr<VLedHal> led = HalTestTool::MakeLedHalTest(LED_TEST);
mAllLedsMock[LED_TEST] = led;
}
void DestroyAllKeysMock(void)
{
mAllKeysMock.clear();
mAllLedsMock.clear();
}
public:
// std::shared_ptr<LinuxTest> mLinuxTest;
protected:
std::map<std::string, std::shared_ptr<VKeyHal>> mAllKeysMock;
std::map<std::string, std::shared_ptr<VLedHal>> mAllLedsMock;
};
// ../output_files/test/bin/DeviceManagerTest
// --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_KeyShortPress
/**
* @brief Construct a new test f object
* This test case demonstrates whether DeviceManager can reprocess key events and report them to the application when
* they are triggered.
*/
TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_KeyShortPress)
{
SetAllKeysResult(mAllKeysMock);
IDeviceManager::GetInstance()->Init();
std::shared_ptr<VKeyMonitor> monitor = std::make_shared<KeyMonitorMock>();
IDeviceManager::GetInstance()->SetAllKeysMonitor(monitor);
MockKeyClick(KEY_TEST); // Simulate pressing a button.
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
IDeviceManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/DeviceManagerTest
// --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_KeyInvalidShort
/**
* @brief Construct a new test f object
* This test case demonstrates whether DeviceManager can reprocess key events and report them to the application when
* they are triggered.
*/
TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_KeyInvalidShort)
{
SetAllKeysResult(mAllKeysMock);
IDeviceManager::GetInstance()->Init();
std::shared_ptr<VKeyMonitor> monitor = std::make_shared<KeyMonitorMock>();
IDeviceManager::GetInstance()->SetAllKeysMonitor(monitor);
MockKeyClick(KEY_TEST, 50); // Simulate pressing a button.
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
IDeviceManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/DeviceManagerTest
// --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_KeyLongPress
/**
* @brief Construct a new test f object
* This test case demonstrates whether DeviceManager can reprocess key events and report them to the application when
* they are triggered.
*/
TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_KeyLongPress)
{
SetAllKeysResult(mAllKeysMock);
IDeviceManager::GetInstance()->Init();
std::shared_ptr<VKeyMonitor> monitor = std::make_shared<KeyMonitorMock>();
IDeviceManager::GetInstance()->SetAllKeysMonitor(monitor);
MockKeyClick(KEY_TEST, 1000); // Simulate pressing a button.
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IDeviceManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/DeviceManagerTest
// --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_SetKeyMonitor
TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_SetKeyMonitor)
{
SetAllKeysResult(mAllKeysMock);
IDeviceManager::GetInstance()->Init();
std::shared_ptr<VKeyMonitor> monitor = std::make_shared<KeyMonitorMock>();
IDeviceManager::GetInstance()->SetAllKeysMonitor(monitor);
MockKeyClick(KEY_TEST, 1000); // Simulate pressing a button.
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
MockKeyClick(KEY_TEST, 1000); // Simulate pressing a button.
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IDeviceManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/DeviceManagerTest
// --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed
TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed)
{
SetAllLedsResult(mAllLedsMock);
IDeviceManager::GetInstance()->Init();
std::shared_ptr<VKeyMonitor> monitor = std::make_shared<KeyMonitorMock>();
IDeviceManager::GetInstance()->SetAllKeysMonitor(monitor);
ControlLed(LED_TEST, LedState::ON, KEEP_ALIVE_FOREVER, LED_NOT_BLINK);
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
IDeviceManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/DeviceManagerTest
// --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed2
TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed2)
{
SetAllLedsResult(mAllLedsMock);
IDeviceManager::GetInstance()->Init();
std::shared_ptr<VKeyMonitor> monitor = std::make_shared<KeyMonitorMock>();
IDeviceManager::GetInstance()->SetAllKeysMonitor(monitor);
constexpr int BLINK_TIME = 500;
ControlLed(LED_TEST, LedState::ON, KEEP_ALIVE_FOREVER, BLINK_TIME);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IDeviceManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/DeviceManagerTest
// --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed3
TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed3)
{
SetAllLedsResult(mAllLedsMock);
IDeviceManager::GetInstance()->Init();
std::shared_ptr<VKeyMonitor> monitor = std::make_shared<KeyMonitorMock>();
IDeviceManager::GetInstance()->SetAllKeysMonitor(monitor);
constexpr int BLINK_TIME = 500;
constexpr int KEEP_ALIVE = 1000;
ControlLed(LED_TEST, LedState::ON, KEEP_ALIVE, BLINK_TIME);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IDeviceManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/DeviceManagerTest
// --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed4
TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed4)
{
SetAllLedsResult(mAllLedsMock);
IDeviceManager::GetInstance()->Init();
std::shared_ptr<VKeyMonitor> monitor = std::make_shared<KeyMonitorMock>();
IDeviceManager::GetInstance()->SetAllKeysMonitor(monitor);
constexpr int KEEP_ALIVE = 1000;
ControlLed(LED_TEST, LedState::ON, KEEP_ALIVE, LED_NOT_BLINK);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IDeviceManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/DeviceManagerTest
// --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed5
TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed5)
{
SetAllLedsResult(mAllLedsMock);
IDeviceManager::GetInstance()->Init();
std::shared_ptr<VKeyMonitor> monitor = std::make_shared<KeyMonitorMock>();
IDeviceManager::GetInstance()->SetAllKeysMonitor(monitor);
std::shared_ptr<LedControlContext> ledControl =
DeviceManagerTestTool::ControlLed(LED_TEST, LedState::ON, KEEP_ALIVE_FOREVER, LED_NOT_BLINK);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
DeviceManagerTestTool::DestoryLedControl(ledControl);
DeviceManagerTestTool::ControlLed(LED_TEST, LedState::ON, KEEP_ALIVE_FOREVER, LED_NOT_BLINK);
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
IDeviceManager::GetInstance()->UnInit();
}
} // namespace DeviceManagerTest

View File

@ -0,0 +1,74 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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.
*/
#ifndef DEVICE_MANAGER_TEST_TOOL_H
#define DEVICE_MANAGER_TEST_TOOL_H
#include "DeviceManager.h"
#include "GtestUsing.h"
#include "HalTestTool.h"
#include "LedControl.h"
class DeviceManagerTool : public DeviceManager
{
public:
DeviceManagerTool() = default;
virtual ~DeviceManagerTool() = default;
const StatusCode SetAllKeysMonitor(std::shared_ptr<VKeyMonitor> &monitor) override;
private:
virtual const StatusCode SetAllKeysMonitorTrace(std::shared_ptr<VKeyMonitor> &monitor)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
};
class DeviceManagerMock : public DeviceManagerTool
{
public:
DeviceManagerMock() = default;
virtual ~DeviceManagerMock() = default;
MOCK_METHOD1(SetAllKeysMonitorTrace, const StatusCode(std::shared_ptr<VKeyMonitor> &));
};
class KeyMonitorMock : public VKeyMonitor
{
public:
KeyMonitorMock() = default;
virtual ~KeyMonitorMock() = default;
MOCK_METHOD3(KeyEventReport, void(const std::string &, const VirtualKeyEvent &, const unsigned int &));
};
class DeviceManagerTestTool : virtual public HalTestTool
{
public:
DeviceManagerTestTool() = default;
virtual ~DeviceManagerTestTool() = default;
void Init(void);
void UnInit(void);
public:
std::shared_ptr<LedControlContext> ControlLed(const std::string &ledName, const LedState &state,
const unsigned int &aliveTimeMs, const unsigned int &blinkTimeMs);
void DestoryLedControl(std::shared_ptr<LedControlContext> &ledControl);
protected:
void DeviceManagerNotice(const std::string &keyName, const unsigned int &pressingTimeMs) override;
private:
void DeviceManagerMockInit(std::shared_ptr<DeviceManagerMock> &mock);
void KeyMonitorInit(std::shared_ptr<KeyMonitorMock> &mock, const std::string &keyName,
const unsigned int &pressingTimeMs);
public:
private:
std::shared_ptr<DeviceManagerMock> mDeviceManagerMock;
std::shared_ptr<KeyMonitorMock> mKeyMonitorMock;
};
#endif

View File

@ -0,0 +1,84 @@
# include(${CMAKE_SOURCE_DIR}/build/independent_source.cmake)
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
include(${MIDDLEWARE_SOURCE_PATH}/HuntingUpgrade/build/hunting_upgrade.cmake)
set(EXECUTABLE_OUTPUT_PATH ${TEST_OUTPUT_PATH}/bin)
include_directories(
./src
./include
./tool/include
${UTILS_SOURCE_PATH}/Log/include
${UTILS_SOURCE_PATH}/StatusCode/include
# ${UTILS_SOURCE_PATH}/LinuxApi/include
# ${HAL_SOURCE_PATH}/include
${MIDDLEWARE_SOURCE_PATH}/HuntingUpgrade/include
${TEST_SOURCE_PATH}
# ${TEST_SOURCE_PATH}/hal/tool/include
# ${TEST_SOURCE_PATH}/utils/LinuxApiMock/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include
)
link_directories(
${LIBS_OUTPUT_PATH}
${EXTERNAL_LIBS_OUTPUT_PATH}
${TEST_TOOLS_OUTPUT_PATH}
)
aux_source_directory(. SRC_FILES_MAIN)
aux_source_directory(./src SRC_FILES)
if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX})
aux_source_directory(./src_mock SRC_FILES)
endif()
set(TARGET_NAME HuntingUpgradeTest)
add_executable(${TARGET_NAME} ${SRC_FILES_MAIN} ${SRC_FILES})
target_link_libraries(${TARGET_NAME} HuntingUpgradeTestTool gtest gmock pthread)
target_link_libraries(${TARGET_NAME} HuntingUpgrade UpgradeTool Log LinuxApiMock)
if(${TEST_COVERAGE} MATCHES "true")
target_link_libraries(${TARGET_NAME} gcov)
endif()
if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true")
add_custom_target(
HuntingUpgradeTest_code_check
COMMAND ${CLANG_TIDY_EXE}
-checks='${CLANG_TIDY_CHECKS}'
--header-filter=.*
--system-headers=false
${SRC_FILES}
${CLANG_TIDY_CONFIG}
# --line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/gtest.h\"}]'
--line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/*.h\"}]'
-p ${PLATFORM_PATH}/cmake-shell
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/HuntingUpgrade
)
add_custom_command(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make HuntingUpgradeTest_code_check
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
file(GLOB_RECURSE HEADER_FILES *.h)
add_custom_target(
HuntingUpgradeTest_code_format
COMMAND ${CLANG_FORMAT_EXE}
-style=file
-i ${SRC_FILES} ${SRC_FILES_MAIN} ${HEADER_FILES}
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/HuntingUpgrade
)
add_custom_command(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make HuntingUpgradeTest_code_check
COMMAND make HuntingUpgradeTest_code_format
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
endif()
define_file_name(${TARGET_NAME})
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tool/CMakeLists.txt")
add_subdirectory(tool)
endif()

View File

@ -0,0 +1,23 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
#include <unistd.h>
int main(int argc, char *argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@ -0,0 +1,68 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 "HuntingUpgradeTestTool.h"
#include "IHuntingUpgrade.h"
#include "ILog.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
namespace HuntingUpgradeTest
{
class HuntingUpgradeTest : public testing::Test, public HuntingUpgradeTestTool
{
public:
HuntingUpgradeTest()
{
}
virtual ~HuntingUpgradeTest()
{
}
static void SetUpTestCase()
{
CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END);
}
static void TearDownTestCase()
{
ILogUnInit();
DestroyLogModule();
}
virtual void SetUp()
{
// HalTestTool::Init();
HuntingUpgradeTestTool::Init();
// CreateHalCppModule();
// CreateHuntingUpgradeModule();
CreateHuntingUpgradeModule();
}
virtual void TearDown()
{
HuntingUpgradeTestTool::UnInit();
// HalTestTool::UnInit();
// DestroyHuntingUpgradeModule();
// DestroyHalCppModule();
DestroyHuntingUpgradeModule();
}
};
/**
* @brief
* ../output_files/test/bin/HuntingUpgradeTest --gtest_filter=HuntingUpgradeTest.HS_RH_UNIT_HuntingUpgrade_EXAMPLE_Demo0
*/
TEST_F(HuntingUpgradeTest, HS_RH_UNIT_HuntingUpgrade_EXAMPLE_Demo0)
{
HuntingUpgradeTestTool::CreateUpgradeFile();
IHuntingUpgrade::GetInstance()->CheckUpgradeFile();
}
} // namespace HuntingUpgradeTest

View File

@ -0,0 +1,32 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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.
*/
#ifndef HUNTING_UPGRADE_TEST_TOOL_H
#define HUNTING_UPGRADE_TEST_TOOL_H
#include "GtestUsing.h"
class HuntingUpgradeTestTool
{
public:
HuntingUpgradeTestTool() = default;
virtual ~HuntingUpgradeTestTool() = default;
protected:
void Init(void);
void UnInit(void);
void CreateUpgradeFile(void);
private:
bool CheckDirectory(const char *filepath);
};
#endif

View File

@ -0,0 +1,84 @@
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
include(${MIDDLEWARE_SOURCE_PATH}/IpcConfig/build/ipc_config.cmake)
set(EXECUTABLE_OUTPUT_PATH ${TEST_OUTPUT_PATH}/bin)
include_directories(
.
./src
./include
${UTILS_SOURCE_PATH}/Log/include
${UTILS_SOURCE_PATH}/StatusCode/include
${UTILS_SOURCE_PATH}/LinuxApi/include
${UTILS_SOURCE_PATH}/ConfigBase/include
${MIDDLEWARE_SOURCE_PATH}/IpcConfig/include
${MIDDLEWARE_SOURCE_PATH}/IpcConfig/src
${TEST_SOURCE_PATH}
${TEST_SOURCE_PATH}/middleware/IpcConfig/tool/include
${TEST_SOURCE_PATH}/utils/LinuxApiMock/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include
)
link_directories(
${LIBS_OUTPUT_PATH}
${EXTERNAL_LIBS_OUTPUT_PATH}
${TEST_TOOLS_OUTPUT_PATH}
)
aux_source_directory(. SRC_FILES_MAIN)
aux_source_directory(./src SRC_FILES)
if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX})
aux_source_directory(./src_mock SRC_FILES)
endif()
set(TARGET_NAME IpcConfigTest)
add_executable(${TARGET_NAME} ${SRC_FILES_MAIN} ${SRC_FILES})
target_link_libraries(${TARGET_NAME} IpcConfigTestTool LinuxApi gtest gmock pthread)
target_link_libraries(${TARGET_NAME} IpcConfig LinuxApiMock)
if(${TEST_COVERAGE} MATCHES "true")
target_link_libraries(${TARGET_NAME} gcov)
endif()
if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true")
add_custom_target(
IpcConfigTest_code_check
COMMAND ${CLANG_TIDY_EXE}
-checks='${CLANG_TIDY_CHECKS}'
--header-filter=.*
--system-headers=false
${SRC_FILES}
${CLANG_TIDY_CONFIG}
# --line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/gtest.h\"}]'
--line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/*.h\"}]'
-p ${PLATFORM_PATH}/cmake-shell
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/IpcConfig
)
add_custom_command(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make IpcConfigTest_code_check
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
file(GLOB_RECURSE HEADER_FILES *.h)
add_custom_target(
IpcConfigTest_code_format
COMMAND ${CLANG_FORMAT_EXE}
-style=file
-i ${SRC_FILES} ${SRC_FILES_MAIN} ${HEADER_FILES}
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/IpcConfig
)
add_custom_command(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make IpcConfigTest_code_check
COMMAND make IpcConfigTest_code_format
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
endif()
define_file_name(${TARGET_NAME})
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tool/CMakeLists.txt")
add_subdirectory(tool)
endif()

View File

@ -0,0 +1,23 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
#include <unistd.h>
int main(int argc, char *argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@ -0,0 +1,128 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 "IIpcConfig.h"
#include "ILog.h"
#include "IpcConfigImpl.h"
#include "LinuxApi.h"
// #include <gmock/gmock.h>
#include <gtest/gtest.h>
namespace IpcConfigTest
{
// ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigDemo.Demo
TEST(IpcConfigDemo, Demo)
{
CreateLogModule();
CreateIpcConfigModule();
ILogInit(LOG_INSTANCE_TYPE_END);
IIpcConfig::GetInstance()->Init();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
const int workMode = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::WORK_MODE);
LogInfo("Get workMode = %d\n", workMode);
const int wworkMode = static_cast<int>(WorkMode::MODE_PIC_VIDEO);
IIpcConfig::GetInstance()->SetInt(IpcConfigKey::WORK_MODE, wworkMode);
const int continuousShot = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::CONTINUOUS_SHOT);
LogInfo("Get continuousShot = %d\n", continuousShot);
const int ccontinuousShot = CONTINUOUS_SHOT_TWO_PIC;
IIpcConfig::GetInstance()->SetInt(IpcConfigKey::CONTINUOUS_SHOT, ccontinuousShot);
const int burstPhotoInterval = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::BURST_PHOTO_INTERVAL);
LogInfo("Get burstPhotoInterval = %d\n", burstPhotoInterval);
const int bburstPhotoInterval = BURST_PHOTO_INTERVAL_DEFAULT;
IIpcConfig::GetInstance()->SetInt(IpcConfigKey::BURST_PHOTO_INTERVAL, bburstPhotoInterval);
const std::string imageSize = IIpcConfig::GetInstance()->GetString(IpcConfigKey::IMGAE_SIZE);
LogInfo("Get image_size = %s\n", imageSize.c_str());
const std::string iimageSize = "410*410";
IIpcConfig::GetInstance()->SetString(IpcConfigKey::IMGAE_SIZE, iimageSize);
const int videoSize = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::VIDEO_LENGTH);
LogInfo("Get video_size = %d\n", videoSize);
const int vvideoSize = VIDEO_SIZE_10;
IIpcConfig::GetInstance()->SetInt(IpcConfigKey::VIDEO_LENGTH, vvideoSize);
const int infraredIampPower = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::INFRARED_POWER);
LogInfo("Get infrared_lamp_power = %d\n", infraredIampPower);
const int iinfraredIampPower = INFRARED_IAMP_POWER_LOW;
IIpcConfig::GetInstance()->SetInt(IpcConfigKey::INFRARED_POWER, iinfraredIampPower);
const int delayed = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::PIR_DELAYED);
LogInfo("Get delayed = %d\n", delayed);
const int ddelayed = DELAYED_MAX;
IIpcConfig::GetInstance()->SetInt(IpcConfigKey::PIR_DELAYED, ddelayed);
const int pirSensitivity = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::PIR_SENSITIVITY);
LogInfo("Get pir_sensitivity = %d\n", pirSensitivity);
const int ppirSensitivity = PIR_SENSITIVITY_MAX;
IIpcConfig::GetInstance()->SetInt(IpcConfigKey::PIR_SENSITIVITY, ppirSensitivity);
const bool storageLoopSwitch = IIpcConfig::GetInstance()->GetBool(IpcConfigKey::STORAGE_LOOP_SWITCH);
LogInfo("Get storage_loop_switch = %d\n", storageLoopSwitch);
const bool sstorageLoopSwitch = false;
IIpcConfig::GetInstance()->SetBool(IpcConfigKey::STORAGE_LOOP_SWITCH, sstorageLoopSwitch);
const bool factoryReset = IIpcConfig::GetInstance()->GetBool(IpcConfigKey::FACTORY_RESET_FLAG);
LogInfo("Get factory_reset = %d\n", factoryReset);
const bool ffactoryReset = false;
IIpcConfig::GetInstance()->SetBool(IpcConfigKey::FACTORY_RESET_FLAG, ffactoryReset);
const bool formattingSDCard = IIpcConfig::GetInstance()->GetBool(IpcConfigKey::FORMATTING_SD_CARD);
LogInfo("Get formatting_SD_card = %d\n", formattingSDCard);
const bool fformattingSDCard = false;
IIpcConfig::GetInstance()->SetBool(IpcConfigKey::FORMATTING_SD_CARD, fformattingSDCard);
const std::string workInterval = IIpcConfig::GetInstance()->GetString(IpcConfigKey::WORK_INTERVAL);
LogInfo("Get work_Interval = %s\n", workInterval.c_str());
const std::string wworkInterval = "06:00:01-06:00:00";
IIpcConfig::GetInstance()->SetString(IpcConfigKey::WORK_INTERVAL, wworkInterval);
const std::string drakMode = IIpcConfig::GetInstance()->GetString(IpcConfigKey::DARK_MODE);
LogInfo("Get dark_mode = %s\n", drakMode.c_str());
const std::string ddrakMode = "18:00:00-06:00:00";
IIpcConfig::GetInstance()->SetString(IpcConfigKey::DARK_MODE, ddrakMode);
short testShort = IIpcConfig::GetInstance()->GetShort(IpcConfigKey::TEST_SHORT);
LogInfo("Get test_short = %d\n", testShort);
const short numShort = 888;
IIpcConfig::GetInstance()->SetShort(IpcConfigKey::TEST_SHORT, numShort);
long testLong = IIpcConfig::GetInstance()->GetLong(IpcConfigKey::TEST_LONG);
LogInfo("Get test_long = %d\n", testLong);
const long numLong = 777;
IIpcConfig::GetInstance()->SetLong(IpcConfigKey::TEST_LONG, numLong);
long long testLLong = IIpcConfig::GetInstance()->GetLLong(IpcConfigKey::TEST_LLONG);
LogInfo("Get test_llong = %d\n", testLLong);
const long long numLLong = 666;
IIpcConfig::GetInstance()->SetLLong(IpcConfigKey::TEST_LLONG, numLLong);
char testChar = IIpcConfig::GetInstance()->GetChar(IpcConfigKey::TEST_CHAR);
LogInfo("Get test_char = %c\n", testChar);
const char numChar = 'A';
IIpcConfig::GetInstance()->SetChar(IpcConfigKey::TEST_CHAR, numChar);
float testFloat = IIpcConfig::GetInstance()->GetFloat(IpcConfigKey::TEST_FLOAT);
LogInfo("Get test_float = %lf\n", testFloat);
const float numFloat = 1.123456;
IIpcConfig::GetInstance()->SetFloat(IpcConfigKey::TEST_FLOAT, numFloat);
IIpcConfig::GetInstance()->ConfigFileSave();
IIpcConfig::GetInstance()->UnInit();
ILogUnInit();
DestroyIpcConfigModule();
DestroyLogModule();
}
} // namespace IpcConfigTest

View File

@ -0,0 +1,65 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 "IpcConfigTest.h"
#include "IIpcConfig.h"
#include "ILog.h"
#include "LinuxApi.h"
#include <thread>
IpcConfigTest::IpcConfigTest()
{
}
IpcConfigTest::~IpcConfigTest()
{
}
void IpcConfigTest::SetUpTestCase()
{
CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END);
}
void IpcConfigTest::TearDownTestCase()
{
ILogUnInit();
DestroyLogModule();
}
void IpcConfigTest::SetUp()
{
mLinuxTest = LinuxTest::CreateLinuxTest();
std::shared_ptr<LinuxApiMock> test = mLinuxTest;
LinuxApiMock::GetInstance(&test);
IpcConfigTestTool::Init(mLinuxTest);
CreateIpcConfigModule();
IIpcConfig::GetInstance()->Init();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
void IpcConfigTest::TearDown()
{
IIpcConfig::GetInstance()->UnInit();
DestroyIpcConfigModule();
RemoveConfigFile();
IpcConfigTestTool::UnInit();
mLinuxTest = std::make_shared<LinuxTest>();
std::shared_ptr<LinuxApiMock> test = std::make_shared<LinuxApiMock>();
LinuxApiMock::GetInstance(&test);
}
void IpcConfigTest::RemoveConfigFile(void)
{
constexpr int FIEL_EXIST = 0;
if (FIEL_EXIST == access(IPC_CONFIG_FILE_PATH, F_OK)) {
constexpr int BUFF_SIZE = 128;
char cmd[BUFF_SIZE] = {0};
snprintf(cmd, BUFF_SIZE, "rm -f %s", IPC_CONFIG_FILE_PATH);
fx_system(cmd);
}
}

View File

@ -0,0 +1,35 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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.
*/
#ifndef IPC_CONFIG_TEST_H
#define IPC_CONFIG_TEST_H
#include "GtestUsing.h"
#include "IpcConfigTestTool.h"
class IpcConfigTest : public testing::Test, public IpcConfigTestTool
{
public:
IpcConfigTest();
virtual ~IpcConfigTest();
static void SetUpTestCase();
static void TearDownTestCase();
virtual void SetUp();
virtual void TearDown();
private:
void RemoveConfigFile(void);
public:
std::shared_ptr<LinuxTest> mLinuxTest;
};
#endif

View File

@ -0,0 +1,158 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 "IpcConfigTest.h"
#include "IIpcConfig.h"
#include "ILog.h"
#include "IpcConfigImpl.h"
extern const char *CONFIG_WIFI_SSID_DEFAULT;
extern const char *CONFIG_WIFI_PASSWORD_DEFAULT;
// ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigTest.HS_RH_UNIT_IpcConfig_AUTO_WifiSsid
TEST_F(IpcConfigTest, HS_RH_UNIT_IpcConfig_AUTO_WifiSsid)
{
const char *MODIFIED_STRING = "modified_string";
const char *MODIFIED_STRING_SHORTER = "modified";
const char *LONG_STRING =
"LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL"
"LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL";
std::string config = IIpcConfig::GetInstance()->GetString(IpcConfigKey::WIFI_SSID);
LogInfo("Get wifi ssid = %s\n", config.c_str());
EXPECT_STREQ(config.c_str(), CONFIG_WIFI_SSID_DEFAULT);
IIpcConfig::GetInstance()->SetString(IpcConfigKey::WIFI_SSID, MODIFIED_STRING);
config = IIpcConfig::GetInstance()->GetString(IpcConfigKey::WIFI_SSID);
LogInfo("Get wifiSsid = %s\n", config.c_str());
EXPECT_STREQ(config.c_str(), MODIFIED_STRING);
IIpcConfig::GetInstance()->SetString(IpcConfigKey::WIFI_SSID, LONG_STRING);
config = IIpcConfig::GetInstance()->GetString(IpcConfigKey::WIFI_SSID);
EXPECT_STREQ(config.c_str(), MODIFIED_STRING);
IIpcConfig::GetInstance()->SetString(IpcConfigKey::WIFI_SSID, "");
config = IIpcConfig::GetInstance()->GetString(IpcConfigKey::WIFI_SSID);
EXPECT_STREQ(config.c_str(), "");
IIpcConfig::GetInstance()->SetString(IpcConfigKey::WIFI_SSID, MODIFIED_STRING_SHORTER);
config = IIpcConfig::GetInstance()->GetString(IpcConfigKey::WIFI_SSID);
EXPECT_STREQ(config.c_str(), MODIFIED_STRING_SHORTER);
IIpcConfig::GetInstance()->SetString(IpcConfigKey::WIFI_SSID, MODIFIED_STRING);
config = IIpcConfig::GetInstance()->GetString(IpcConfigKey::WIFI_SSID);
EXPECT_STREQ(config.c_str(), MODIFIED_STRING);
}
// ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigTest.HS_RH_UNIT_IpcConfig_AUTO_WifiPassword
TEST_F(IpcConfigTest, HS_RH_UNIT_IpcConfig_AUTO_WifiPassword)
{
const char *MODIFIED_STRING = "99999999";
const char *MODIFIED_STRING_SHORTER = "999";
const char *LONG_STRING =
"LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL"
"LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL";
std::string config = IIpcConfig::GetInstance()->GetString(IpcConfigKey::WIFI_PASSWORD);
LogInfo("Get wifi password = %s\n", config.c_str());
EXPECT_STREQ(config.c_str(), CONFIG_WIFI_PASSWORD_DEFAULT);
IIpcConfig::GetInstance()->SetString(IpcConfigKey::WIFI_PASSWORD, MODIFIED_STRING);
config = IIpcConfig::GetInstance()->GetString(IpcConfigKey::WIFI_PASSWORD);
LogInfo("Get wifi password = %s\n", config.c_str());
EXPECT_STREQ(config.c_str(), MODIFIED_STRING);
IIpcConfig::GetInstance()->SetString(IpcConfigKey::WIFI_PASSWORD, LONG_STRING);
config = IIpcConfig::GetInstance()->GetString(IpcConfigKey::WIFI_PASSWORD);
EXPECT_STREQ(config.c_str(), MODIFIED_STRING);
IIpcConfig::GetInstance()->SetString(IpcConfigKey::WIFI_PASSWORD, "");
config = IIpcConfig::GetInstance()->GetString(IpcConfigKey::WIFI_PASSWORD);
EXPECT_STREQ(config.c_str(), "");
IIpcConfig::GetInstance()->SetString(IpcConfigKey::WIFI_PASSWORD, MODIFIED_STRING_SHORTER);
config = IIpcConfig::GetInstance()->GetString(IpcConfigKey::WIFI_PASSWORD);
EXPECT_STREQ(config.c_str(), MODIFIED_STRING_SHORTER);
IIpcConfig::GetInstance()->SetString(IpcConfigKey::WIFI_PASSWORD, MODIFIED_STRING);
config = IIpcConfig::GetInstance()->GetString(IpcConfigKey::WIFI_PASSWORD);
EXPECT_STREQ(config.c_str(), MODIFIED_STRING);
}
// ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigTest.HS_RH_UNIT_IpcConfig_AUTO_WorkMode
TEST_F(IpcConfigTest, HS_RH_UNIT_IpcConfig_AUTO_WorkMode)
{
WorkMode config = IIpcConfig::GetInstance()->GetWorkMode();
EXPECT_EQ(static_cast<int>(config), static_cast<int>(CONFIG_WORK_MODE_DEFAULT));
IIpcConfig::GetInstance()->SetWorkMode(WorkMode::MODE_PIC_VIDEO);
config = IIpcConfig::GetInstance()->GetWorkMode();
EXPECT_EQ(static_cast<int>(config), static_cast<int>(WorkMode::MODE_PIC_VIDEO));
}
// ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigTest.HS_RH_UNIT_IpcConfig_AUTO_ContinueShot
TEST_F(IpcConfigTest, HS_RH_UNIT_IpcConfig_AUTO_ContinueShot)
{
int config = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::CONTINUOUS_SHOT);
EXPECT_EQ(config, CONFIG_CONTINUE_SHOT_DEFAULT);
IIpcConfig::GetInstance()->SetInt(IpcConfigKey::CONTINUOUS_SHOT, 2);
config = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::CONTINUOUS_SHOT);
EXPECT_EQ(config, 2);
}
// ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigTest.HS_RH_UNIT_IpcConfig_AUTO_BurstPhotoInterval
TEST_F(IpcConfigTest, HS_RH_UNIT_IpcConfig_AUTO_BurstPhotoInterval)
{
int config = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::BURST_PHOTO_INTERVAL);
EXPECT_EQ(config, CONFIG_BURST_PHOTO_INTERVAL_DEFAULT);
IIpcConfig::GetInstance()->SetInt(IpcConfigKey::BURST_PHOTO_INTERVAL, 10);
config = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::BURST_PHOTO_INTERVAL);
EXPECT_EQ(config, 10);
}
// ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigTest.HS_RH_UNIT_IpcConfig_AUTO_ImageSize
TEST_F(IpcConfigTest, HS_RH_UNIT_IpcConfig_AUTO_ImageSize)
{
int config = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::IMGAE_SIZE);
EXPECT_EQ(config, CONFIG_IMAGE_SIZE_DEFAULT);
IIpcConfig::GetInstance()->SetInt(IpcConfigKey::IMGAE_SIZE, 16);
config = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::IMGAE_SIZE);
EXPECT_EQ(config, 16);
}
// ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigTest.HS_RH_UNIT_IpcConfig_AUTO_VideoLength
TEST_F(IpcConfigTest, HS_RH_UNIT_IpcConfig_AUTO_VideoLength)
{
int config = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::VIDEO_LENGTH);
EXPECT_EQ(config, CONFIG_VIDEO_SIZE_DEFAULT);
IIpcConfig::GetInstance()->SetInt(IpcConfigKey::VIDEO_LENGTH, 15);
config = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::VIDEO_LENGTH);
EXPECT_EQ(config, 15);
}
// ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigTest.HS_RH_UNIT_IpcConfig_AUTO_PirDelayed
TEST_F(IpcConfigTest, HS_RH_UNIT_IpcConfig_AUTO_PirDelayed)
{
int config = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::PIR_DELAYED);
EXPECT_EQ(config, CONFIG_PIR_DELAYED_DEFAULT);
IIpcConfig::GetInstance()->SetInt(IpcConfigKey::PIR_DELAYED, 15);
config = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::PIR_DELAYED);
EXPECT_EQ(config, 15);
}
// ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigTest.HS_RH_UNIT_IpcConfig_AUTO_StorageLoop
TEST_F(IpcConfigTest, HS_RH_UNIT_IpcConfig_AUTO_StorageLoop)
{
ConfigSwitch config = IIpcConfig::GetInstance()->GetSwitch(IpcConfigKey::STORAGE_LOOP_SWITCH);
EXPECT_EQ(static_cast<int>(config),
static_cast<int>(CONFIG_STORAGE_LOOP_DEFAULT == true ? ConfigSwitch::ON : ConfigSwitch::OFF));
IIpcConfig::GetInstance()->SetSwitch(IpcConfigKey::STORAGE_LOOP_SWITCH, ConfigSwitch::OFF);
config = IIpcConfig::GetInstance()->GetSwitch(IpcConfigKey::STORAGE_LOOP_SWITCH);
EXPECT_EQ(static_cast<int>(config), static_cast<int>(ConfigSwitch::OFF));
}
// ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigTest.HS_RH_UNIT_IpcConfig_AUTO_InfraredPower
TEST_F(IpcConfigTest, HS_RH_UNIT_IpcConfig_AUTO_InfraredPower)
{
ConfigLevel config = IIpcConfig::GetInstance()->GetLevel(IpcConfigKey::INFRARED_POWER);
EXPECT_EQ(static_cast<int>(config), static_cast<int>(CONFIG_INFRARED_POWER_DEFAULT));
IIpcConfig::GetInstance()->SetLevel(IpcConfigKey::INFRARED_POWER, ConfigLevel::HIGHT);
config = IIpcConfig::GetInstance()->GetLevel(IpcConfigKey::INFRARED_POWER);
EXPECT_EQ(static_cast<int>(config), static_cast<int>(ConfigSwitch::OFF));
}
// ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigTest.HS_RH_UNIT_IpcConfig_AUTO_PirSensitivity
TEST_F(IpcConfigTest, HS_RH_UNIT_IpcConfig_AUTO_PirSensitivity)
{
int config = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::PIR_SENSITIVITY);
EXPECT_EQ(config, CONFIG_PIR_SENSITIVITY_DEFAULT);
IIpcConfig::GetInstance()->SetInt(IpcConfigKey::PIR_SENSITIVITY, 0);
config = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::PIR_SENSITIVITY);
EXPECT_EQ(config, 0);
}

View File

@ -0,0 +1,35 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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.
*/
#ifndef IPC_CONFIG_TEST_TOOL_H
#define IPC_CONFIG_TEST_TOOL_H
#include "GtestUsing.h"
#include "LinuxApiMock.h"
class IpcConfigTestTool
{
public:
IpcConfigTestTool() = default;
virtual ~IpcConfigTestTool() = default;
void Init(std::shared_ptr<LinuxTest> &mock);
void UnInit(void);
private:
void MockMountedFile(void);
FILE *OpenMockMountedFile(const char *mode);
private:
std::shared_ptr<LinuxTest> mLinuxTest;
FILE *mFile = nullptr;
};
#endif

View File

@ -0,0 +1,4 @@
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tool/CMakeLists.txt")
add_subdirectory(tool)
endif()

View File

@ -0,0 +1,68 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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.
*/
#ifndef MCU_ASK_BASE_TEST_TOOL_H
#define MCU_ASK_BASE_TEST_TOOL_H
#include "GtestUsing.h"
#include "McuAskBase.h"
class McuAskBaseTest : public McuAskBase
{
public:
McuAskBaseTest(const McuAskBlock isBlock, const McuAskReply needReply,
const unsigned int timeoutMs = DEFAULT_ASK_TIMEOUT)
: McuAskBase(isBlock, needReply, timeoutMs)
{
//
}
virtual ~McuAskBaseTest() = default;
ASK_RESULT Blocking(void) override;
bool NeedReply(void) override;
void ReplyFinished(const bool result) override;
bool IfTimeout(const unsigned int &integrationTimeMs) override;
protected:
virtual void BlockingTrace(void)
{
}
virtual void NeedReplyTrace(void)
{
}
virtual void ReplyFinishedTrace(const bool result)
{
}
virtual void IfTimeoutTrace(const unsigned int &integrationTimeMs)
{
}
};
class McuAskBaseTestTool : public McuAskBaseTest
{
public:
McuAskBaseTestTool(const McuAskBlock isBlock, const McuAskReply needReply,
const unsigned int timeoutMs = DEFAULT_ASK_TIMEOUT)
: McuAskBaseTest(isBlock, needReply, timeoutMs)
{
//
}
virtual ~McuAskBaseTestTool() = default;
MOCK_METHOD0(BlockingTrace, void(void));
MOCK_METHOD0(NeedReplyTrace, void(void));
MOCK_METHOD1(ReplyFinishedTrace, void(const bool));
MOCK_METHOD1(IfTimeoutTrace, void(const unsigned int &));
public:
void McuAskDefaultFeatures(std::shared_ptr<McuAskBaseTestTool> &mock);
void McuAskDoNothing(std::shared_ptr<McuAskBaseTestTool> &mock);
void NoNeedToBlocking(std::shared_ptr<McuAskBaseTestTool> &mock);
};
#endif

View File

@ -0,0 +1,92 @@
# include(${CMAKE_SOURCE_DIR}/build/independent_source.cmake)
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
set(EXECUTABLE_OUTPUT_PATH ${TEST_OUTPUT_PATH}/bin)
include_directories(
./src
./include
./tool/include
${UTILS_SOURCE_PATH}/Log/include
${UTILS_SOURCE_PATH}/StatusCode/include
${UTILS_SOURCE_PATH}/UartDevice/include
${UTILS_SOURCE_PATH}/McuProtocol/include
${MIDDLEWARE_SOURCE_PATH}/McuManager/include
${MIDDLEWARE_SOURCE_PATH}/McuManager/src
${MIDDLEWARE_SOURCE_PATH}/McuAskBase/include
${TEST_SOURCE_PATH}
${TEST_SOURCE_PATH}/utils/LinuxApiMock/include
${TEST_SOURCE_PATH}/utils/UartDevice/tool/include
${TEST_SOURCE_PATH}/utils/McuProtocol/tool/include
${TEST_SOURCE_PATH}/middleware/McuAskBase/tool/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include
)
link_directories(
${LIBS_OUTPUT_PATH}
${EXTERNAL_LIBS_OUTPUT_PATH}
${TEST_TOOLS_OUTPUT_PATH}
)
if (DEFINED MCU_UART_DEVICE)
add_definitions(-DMCU_UART_DEVICE=\"${MCU_UART_DEVICE}\")
else()
message(FATAL_ERROR "You set define MCU_UART_DEVICE in toolchan .cmake file to tell what uart device to contrl.")
endif()
aux_source_directory(. SRC_FILES_MAIN)
aux_source_directory(./src SRC_FILES)
if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX})
aux_source_directory(./src_mock SRC_FILES)
endif()
set(TARGET_NAME McuManagerTest)
add_executable(${TARGET_NAME} ${SRC_FILES_MAIN} ${SRC_FILES})
target_link_libraries(${TARGET_NAME} McuAskBase McuManager McuManagerTestTool McuAskBaseTestTool gtest gmock pthread)
target_link_libraries(${TARGET_NAME} McuProtocolTestTool UartDeviceTestTool UartDevice LinuxApiMock)
if(${TEST_COVERAGE} MATCHES "true")
target_link_libraries(${TARGET_NAME} gcov)
endif()
if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true")
add_custom_target(
McuManagerTest_code_check
COMMAND ${CLANG_TIDY_EXE}
-checks='${CLANG_TIDY_CHECKS}'
--header-filter=.*
--system-headers=false
${SRC_FILES}
${CLANG_TIDY_CONFIG}
# --line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/gtest.h\"}]'
--line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/*.h\"}]'
-p ${PLATFORM_PATH}/cmake-shell
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/McuManager
)
add_custom_command(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make McuManagerTest_code_check
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
file(GLOB_RECURSE HEADER_FILES *.h)
add_custom_target(
McuManagerTest_code_format
COMMAND ${CLANG_FORMAT_EXE}
-style=file
-i ${SRC_FILES} ${SRC_FILES_MAIN} ${HEADER_FILES}
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/McuManager
)
add_custom_command(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make McuManagerTest_code_check
COMMAND make McuManagerTest_code_format
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
endif()
define_file_name(${TARGET_NAME})
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tool/CMakeLists.txt")
add_subdirectory(tool)
endif()

View File

@ -0,0 +1,23 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
#include <unistd.h>
int main(int argc, char *argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@ -0,0 +1,254 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 "ILog.h"
#include "IMcuManager.h"
#include "McuAskBase.h"
#include "McuAskBaseTestTool.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
namespace McuManagerTest
{
class McuManagerTest : public testing::Test
{
public:
McuManagerTest()
{
}
virtual ~McuManagerTest()
{
}
static void SetUpTestCase()
{
CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END);
}
static void TearDownTestCase()
{
ILogUnInit();
}
virtual void SetUp()
{
CreateMcuManager();
IMcuManager::GetInstance()->Init();
}
virtual void TearDown()
{
IMcuManager::GetInstance()->UnInit();
DestroyMcuManager();
}
};
// ../output_files/test/bin/McuManagerTest
// --gtest_filter=McuManagerTest.RH_INTEGRATION_McuManager_AUTO_GetIpcMission
TEST_F(McuManagerTest, RH_INTEGRATION_McuManager_AUTO_GetIpcMission)
{
class McuAskTest : public McuAsk<IpcMission>, public McuAskBaseTestTool
{
public:
McuAskTest() : McuAskBaseTestTool(McuAskBlock::BLOCK, McuAskReply::NEED_REPLY)
{
mDataReply = IpcMission::END;
}
virtual ~McuAskTest() = default;
void ReplyFinished(const bool result) override
{
McuAskBaseTestTool::ReplyFinished(result);
if (result) {
LogInfo("Ask data succeed, mDataReply = %d.\n", static_cast<int>(mDataReply));
// Do something here.
EXPECT_GT(static_cast<int>(mDataReply), 0);
EXPECT_LT(static_cast<int>(mDataReply), static_cast<int>(IpcMission::END));
}
else {
LogError("Ask data falied.\n");
}
}
};
std::shared_ptr<VMcuAsk> ask = std::make_shared<McuAskTest>();
std::shared_ptr<McuAskBaseTestTool> testTool = std::dynamic_pointer_cast<McuAskBaseTestTool>(ask);
testTool->McuAskDefaultFeatures(testTool);
IMcuManager::GetInstance()->GetIpcMission(ask);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
// ../output_files/test/bin/McuManagerTest
// --gtest_filter=McuManagerTest.RH_INTEGRATION_McuManager_AUTO_OtherSideSendIpcMission
TEST_F(McuManagerTest, RH_INTEGRATION_McuManager_AUTO_OtherSideSendIpcMission)
{
class MonitorTest : public VMcuMonitor
{
public:
MonitorTest() = default;
virtual ~MonitorTest() = default;
void RecvIpcMissionEvent(std::shared_ptr<VMcuRecv> &recv, const IpcMission &mission) override
{
LogInfo("RecvIpcMissionEvent %s\n", IMcuManager::GetInstance()->PrintIpcMissionString(mission));
std::shared_ptr<McuAsk<ASK_RESULT>> ask = std::dynamic_pointer_cast<McuAsk<ASK_RESULT>>(recv);
ask->mDataReply = ASK_RESULT::SUCCEED;
recv->ReplyFinished(true);
}
};
// IMcuManager::GetInstance()->Init();
std::shared_ptr<VMcuMonitor> monitor = std::make_shared<MonitorTest>();
IMcuManager::GetInstance()->SetMcuMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
// IMcuManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/McuManagerTest
// --gtest_filter=McuManagerTest.RH_INTEGRATION_McuManager_AUTO_CutOffPowerSupply
TEST_F(McuManagerTest, RH_INTEGRATION_McuManager_AUTO_CutOffPowerSupply)
{
class McuAskTest : public McuAskBaseTestTool
{
public:
McuAskTest()
: McuAskBaseTestTool(McuAskBlock::UNRELATED,
McuAskReply::NEED_NOT_REPLY) // using McuAskReply::NEED_NOT_REPLY
{
}
virtual ~McuAskTest() = default;
};
// IMcuManager::GetInstance()->Init();
std::shared_ptr<VMcuAsk> ask = std::make_shared<McuAskTest>();
std::shared_ptr<McuAskBaseTestTool> testTool = std::dynamic_pointer_cast<McuAskBaseTestTool>(ask);
testTool->McuAskDefaultFeatures(testTool);
StatusCode code = IMcuManager::GetInstance()->CutOffPowerSupply(ask);
EXPECT_EQ(code.mStatusCode, STATUS_CODE_OK); // STATUS_CODE_OK means write data to mcu succeed.
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
// IMcuManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/McuManagerTest
// --gtest_filter=McuManagerTest.RH_INTEGRATION_McuManager_AUTO_SetFeedingCycleForWatchDog
TEST_F(McuManagerTest, RH_INTEGRATION_McuManager_AUTO_SetFeedingCycleForWatchDog)
{
class McuAskTest : public McuAsk<ASK_RESULT>, public McuAskBaseTestTool
{
public:
McuAskTest() : McuAskBaseTestTool(McuAskBlock::BLOCK,
McuAskReply::NEED_REPLY) // using McuAskBlock::BLOCK,
{
}
virtual ~McuAskTest() = default;
void ReplyFinished(const bool result) override
{
McuAskBaseTestTool::ReplyFinished(result);
if (result) {
LogInfo("Ask data succeed, mDataReply = %d.\n", static_cast<int>(mDataReply));
EXPECT_LT(static_cast<int>(mDataReply), static_cast<int>(ASK_RESULT::SUCCEED));
}
else {
LogError("Ask data falied.\n");
}
}
};
// IMcuManager::GetInstance()->Init();
std::shared_ptr<VMcuAsk> ask = std::make_shared<McuAskTest>();
std::shared_ptr<McuAskBaseTestTool> testTool = std::dynamic_pointer_cast<McuAskBaseTestTool>(ask);
testTool->McuAskDefaultFeatures(testTool);
StatusCode code = IMcuManager::GetInstance()->SetFeedingCycleForWatchDog(ask, 1, 1, 1);
EXPECT_EQ(code.mStatusCode, STATUS_CODE_OK); // STATUS_CODE_OK means write data to mcu succeed.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// IMcuManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/McuManagerTest
// --gtest_filter=McuManagerTest.RH_INTEGRATION_McuManager_AUTO_FeedWatchDog
TEST_F(McuManagerTest, RH_INTEGRATION_McuManager_AUTO_FeedWatchDog)
{
class McuAskTest : public McuAskBaseTestTool
{
public:
McuAskTest()
: McuAskBaseTestTool(McuAskBlock::UNRELATED,
McuAskReply::NEED_NOT_REPLY) // using McuAskReply::NEED_NOT_REPLY
{
}
virtual ~McuAskTest() = default;
};
// IMcuManager::GetInstance()->Init();
std::shared_ptr<VMcuAsk> ask = std::make_shared<McuAskTest>();
std::shared_ptr<McuAskBaseTestTool> testTool = std::dynamic_pointer_cast<McuAskBaseTestTool>(ask);
testTool->McuAskDefaultFeatures(testTool);
StatusCode code = IMcuManager::GetInstance()->FeedWatchDog(ask);
EXPECT_EQ(code.mStatusCode, STATUS_CODE_OK); // STATUS_CODE_OK means write data to mcu succeed.
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
// IMcuManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/McuManagerTest
// --gtest_filter=McuManagerTest.RH_INTEGRATION_McuManager_AUTO_SetDataTime
TEST_F(McuManagerTest, RH_INTEGRATION_McuManager_AUTO_SetDataTime)
{
class McuAskTest : public McuAsk<ASK_RESULT>, public McuAskBaseTestTool
{
public:
McuAskTest() : McuAskBaseTestTool(McuAskBlock::BLOCK,
McuAskReply::NEED_REPLY) // using McuAskBlock::NOT_BLOCK
{
}
virtual ~McuAskTest() = default;
void ReplyFinished(const bool result) override
{
McuAskBaseTestTool::ReplyFinished(result);
if (result) {
LogInfo("Ask data succeed, mDataReply = %d.\n", static_cast<int>(mDataReply));
EXPECT_LT(static_cast<int>(mDataReply), static_cast<int>(ASK_RESULT::SUCCEED));
}
else {
LogError("Ask data falied.\n");
}
}
};
// IMcuManager::GetInstance()->Init();
std::shared_ptr<VMcuAsk> ask = std::make_shared<McuAskTest>();
std::shared_ptr<McuAskBaseTestTool> testTool = std::dynamic_pointer_cast<McuAskBaseTestTool>(ask);
testTool->McuAskDefaultFeatures(testTool);
McuAskDateTime value(2014, 1, 15, 0, 0, 0);
StatusCode code = IMcuManager::GetInstance()->SetDateTime(ask, value);
EXPECT_EQ(code.mStatusCode, STATUS_CODE_OK); // STATUS_CODE_OK means write data to mcu succeed.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// IMcuManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/McuManagerTest
// --gtest_filter=McuManagerTest.RH_INTEGRATION_McuManager_AUTO_SetPirSensitivity
TEST_F(McuManagerTest, RH_INTEGRATION_McuManager_AUTO_SetPirSensitivity)
{
class McuAskTest : public McuAsk<ASK_RESULT>, public McuAskBaseTestTool
{
public:
McuAskTest() : McuAskBaseTestTool(McuAskBlock::BLOCK,
McuAskReply::NEED_REPLY) // using McuAskBlock::NOT_BLOCK
{
}
virtual ~McuAskTest() = default;
void ReplyFinished(const bool result) override
{
McuAskBaseTestTool::ReplyFinished(result);
if (result) {
LogInfo("Ask data succeed, mDataReply = %d.\n", static_cast<int>(mDataReply));
EXPECT_LT(static_cast<int>(mDataReply), static_cast<int>(ASK_RESULT::SUCCEED));
}
else {
LogError("Ask data falied.\n");
}
}
};
// IMcuManager::GetInstance()->Init();
std::shared_ptr<VMcuAsk> ask = std::make_shared<McuAskTest>();
std::shared_ptr<McuAskBaseTestTool> testTool = std::dynamic_pointer_cast<McuAskBaseTestTool>(ask);
testTool->McuAskDefaultFeatures(testTool);
StatusCode code = IMcuManager::GetInstance()->SetPirSensitivity(ask, 1);
EXPECT_EQ(code.mStatusCode, STATUS_CODE_OK); // STATUS_CODE_OK means write data to mcu succeed.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// IMcuManager::GetInstance()->UnInit();
}
} // namespace McuManagerTest

View File

@ -0,0 +1,48 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 "McuManagerMockTest.h"
McuManagerMockTest::McuManagerMockTest()
{
}
McuManagerMockTest::~McuManagerMockTest()
{
}
void McuManagerMockTest::SetUpTestCase()
{
CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END);
}
void McuManagerMockTest::TearDownTestCase()
{
ILogUnInit();
}
void McuManagerMockTest::SetUp()
{
mLinuxTest = LinuxTest::CreateLinuxTest();
std::shared_ptr<LinuxApiMock> test = mLinuxTest;
LinuxApiMock::GetInstance(&test);
LinuxApiMock::GetInstance()->Init();
McuManagerTestTool::Init(mLinuxTest);
CreateMcuManager();
}
void McuManagerMockTest::TearDown()
{
LinuxApiMock::GetInstance()->UnInit();
mLinuxTest = std::make_shared<LinuxTest>();
std::shared_ptr<LinuxApiMock> test = std::make_shared<LinuxApiMock>();
LinuxApiMock::GetInstance(&test);
McuManagerTestTool::UnInit();
DestroyMcuManager();
}

View File

@ -0,0 +1,36 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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.
*/
#ifndef MCU_MANAGER_MOCK_TEST_H
#define MCU_MANAGER_MOCK_TEST_H
#include "ILog.h"
#include "IMcuManager.h"
#include "LinuxApiMock.h"
#include "McuManagerTestTool.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
class McuManagerMockTest : public testing::Test, public McuManagerTestTool
{
public:
McuManagerMockTest();
virtual ~McuManagerMockTest();
static void SetUpTestCase();
static void TearDownTestCase();
virtual void SetUp();
virtual void TearDown();
public:
std::shared_ptr<LinuxTest> mLinuxTest;
};
#endif

View File

@ -0,0 +1,71 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 "McuAskBase.h"
#include "McuAskBaseTestTool.h"
#include "McuManagerMockTest.h"
#include <thread>
const unsigned char McuRecvData[] = {
0xFA, 0xC1, 0x00, 0x00, 0x00, 0x08, 0x41, 0x01, 0x00, 0x0A, 0x03, 0x08, 0xEC, 0xFA, 0xC1, 0x00, 0x00, 0x00,
0x0A, 0x41, 0x03, 0x08, 0xEC, 0xFA, 0xC1, 0x00, 0x00, 0x00, 0x0A, 0x41, 0x07, 0x00, 0x0C, 0x35, 0x88, 0xFA,
0xC1, 0x00, 0x03, 0x08, 0xEC, 0xFA, 0xC1, 0x00, 0x00, 0x00, 0x0A, 0x41, 0x07, 0x00, 0x0C, 0x35, 0x88, 0xFA,
0xC1, 0x00, 0x00, 0xFA, 0xC1, 0x00, 0x00, 0x00, 0x0A, 0x03, 0x08, 0xEC, 0xFA, 0xC1, 0x00, 0x00, 0x00, 0x0A,
0x41, 0x07, 0x00, 0x0C, 0x35, 0x88, 0xFA, 0xC1, 0x00, 0x00, 0xFA, 0xC1, 0x00, 0x00, 0x00, 0x0A};
const unsigned char McuSendData_GetDateTime[] = {
0xFA, 0xC1, 0x00, 0x00, 0x00, 0x0A, 0x41, 0x07, 0x00, 0x0C, 0x35, 0x88};
const unsigned char McuSendData_OtherSideReplyGetIpcMission[] = {
0xFA, 0xC1, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x00, 0x0D, 0x03, 0x09, 0xBA};
namespace McuManager_AbnormalData_Test
{
/**
* @brief Construct a new test f object
* ../output_files/test/bin/McuManagerTest
* --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_EXAMPLE_ProtocolLengthIsWrong
*/
TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_EXAMPLE_ProtocolLengthIsWrong)
{
IMcuManager::GetInstance()->Init();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
McuManagerTestTool::MockOtherSideSendData(mLinuxTest, McuRecvData, sizeof(McuRecvData));
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
IMcuManager::GetInstance()->UnInit();
}
/**
* @brief Construct a new test f object
* ../output_files/test/bin/McuManagerTest
* --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_EXAMPLE_OtherSideGetDateTime
*/
TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_EXAMPLE_OtherSideGetDateTime)
{
IMcuManager::GetInstance()->Init();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
McuManagerTestTool::MockOtherSideSendData(mLinuxTest, McuSendData_GetDateTime, sizeof(McuSendData_GetDateTime));
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
IMcuManager::GetInstance()->UnInit();
}
/**
* @brief Construct a new test f object
* ../output_files/test/bin/McuManagerTest
* --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_EXAMPLE_OtherSideReplyGetIpcMission
*/
TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_EXAMPLE_OtherSideReplyGetIpcMission)
{
IMcuManager::GetInstance()->Init();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
McuManagerTestTool::MockOtherSideSendData(
mLinuxTest, McuSendData_OtherSideReplyGetIpcMission, sizeof(McuSendData_OtherSideReplyGetIpcMission));
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
IMcuManager::GetInstance()->UnInit();
}
} // namespace McuManager_AbnormalData_Test

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,52 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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.
*/
#ifndef MCU_MANAGER_TEST_TOOL_H
#define MCU_MANAGER_TEST_TOOL_H
#include "GtestUsing.h"
#include "LinuxApiMock.h"
#include "McuManagerImpl.h"
#include "McuProtocolTestTool.h"
class McuManagerImplTest : public McuManagerImpl
{
public:
McuManagerImplTest() = default;
virtual ~McuManagerImplTest() = default;
MOCK_METHOD1(DeleteMcuAsk, void(std::shared_ptr<VMcuAsk> &));
bool CheckAskExist(const std::shared_ptr<VMcuAsk> &ask);
};
class McuManagerTestTool : virtual public McuProtocolTestTool
{
public:
McuManagerTestTool() = default;
virtual ~McuManagerTestTool() = default;
bool CheckAskExist(const std::shared_ptr<VMcuAsk> &ask);
protected:
void Init(std::shared_ptr<LinuxTest> &mock);
void UnInit(void);
void MockOtherSideAskIpcMission(std::shared_ptr<LinuxTest> &mock, const unsigned int &serialNumber);
void MockOtherSideAskHeartBeat(std::shared_ptr<LinuxTest> &mock, const unsigned int &serialNumber);
void MockOtherSideGetIntervalStart(std::shared_ptr<LinuxTest> &mock, const unsigned int &serialNumber);
void MockOtherSideGetDateTime(std::shared_ptr<LinuxTest> &mock, const unsigned int &serialNumber);
void MockOtherSideGetPriSensitivity(std::shared_ptr<LinuxTest> &mock, const unsigned int &serialNumber);
void MockOtherSideSendData(std::shared_ptr<LinuxTest> &mock, const void *data, const size_t &size);
void MockMcuDeviceOpenFailed(std::shared_ptr<LinuxTest> &mock);
void MockMcuDeviceOpenSuccessButReadNothing(std::shared_ptr<LinuxTest> &mock);
void MockOtherSideIpcMissionReply(const IpcMission &replyIpcMission);
private:
std::shared_ptr<McuManagerImplTest> mMcuManagerMock;
};
#endif

View File

@ -0,0 +1,86 @@
# include(${CMAKE_SOURCE_DIR}/build/independent_source.cmake)
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
include(${MIDDLEWARE_SOURCE_PATH}/AppManager/build/app_manager.cmake)
set(EXECUTABLE_OUTPUT_PATH ${TEST_OUTPUT_PATH}/bin)
include_directories(
./src
./include
./tool/include
${UTILS_SOURCE_PATH}/Log/include
${UTILS_SOURCE_PATH}/StatusCode/include
# ${UTILS_SOURCE_PATH}/KeyControl/include
${UTILS_SOURCE_PATH}/LedControl/include
# ${UTILS_SOURCE_PATH}/WebServer/include
${HAL_SOURCE_PATH}/include
${MIDDLEWARE_SOURCE_PATH}/MediaManager/include
${TEST_SOURCE_PATH}
${TEST_SOURCE_PATH}/hal/tool/include
${TEST_SOURCE_PATH}/utils/LinuxApiMock/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include
)
link_directories(
${LIBS_OUTPUT_PATH}
${EXTERNAL_LIBS_OUTPUT_PATH}
${TEST_TOOLS_OUTPUT_PATH}
)
aux_source_directory(. SRC_FILES_MAIN)
aux_source_directory(./src SRC_FILES)
if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX})
aux_source_directory(./src_mock SRC_FILES)
endif()
set(TARGET_NAME MediaManagerTest)
add_executable(${TARGET_NAME} ${SRC_FILES_MAIN} ${SRC_FILES})
target_link_libraries(${TARGET_NAME} MediaManager MediaManagerTestTool HalTestTool gtest gmock pthread)
target_link_libraries(${TARGET_NAME} Hal LedControl LinuxApiMock)
if(${TEST_COVERAGE} MATCHES "true")
target_link_libraries(${TARGET_NAME} gcov)
endif()
if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true")
add_custom_target(
MediaManagerTest_code_check
COMMAND ${CLANG_TIDY_EXE}
-checks='${CLANG_TIDY_CHECKS}'
--header-filter=.*
--system-headers=false
${SRC_FILES}
${CLANG_TIDY_CONFIG}
# --line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/gtest.h\"}]'
--line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/*.h\"}]'
-p ${PLATFORM_PATH}/cmake-shell
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/AppManager
)
add_custom_command(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make MediaManagerTest_code_check
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
file(GLOB_RECURSE HEADER_FILES *.h)
add_custom_target(
MediaManagerTest_code_format
COMMAND ${CLANG_FORMAT_EXE}
-style=file
-i ${SRC_FILES} ${SRC_FILES_MAIN} ${HEADER_FILES}
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/AppManager
)
add_custom_command(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make MediaManagerTest_code_check
COMMAND make MediaManagerTest_code_format
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
endif()
define_file_name(${TARGET_NAME})
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tool/CMakeLists.txt")
add_subdirectory(tool)
endif()

View File

@ -0,0 +1,23 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
#include <unistd.h>
int main(int argc, char *argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@ -0,0 +1,91 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 "GtestUsing.h"
#include "HalTestTool.h"
#include "ILog.h"
#include "IMediaManager.h"
#include "MediaManagerTestTool.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
namespace MediaManagerTest
{
class MediaManagerTest : public testing::Test, public MediaManagerTestTool, public HalTestTool
{
public:
MediaManagerTest()
{
}
virtual ~MediaManagerTest()
{
}
static void SetUpTestCase()
{
CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END);
}
static void TearDownTestCase()
{
ILogUnInit();
}
virtual void SetUp()
{
CreateAllCamerasMcok();
HalTestTool::Init();
MediaManagerTestTool::Init();
CreateHalCppModule();
CreateMediaManagerModule();
}
virtual void TearDown()
{
MediaManagerTestTool::UnInit();
HalTestTool::UnInit();
DestroyMediaManagerModule();
DestroyHalCppModule();
DestroyAllCamerasMock();
}
private:
void CreateAllCamerasMcok(void)
{
std::shared_ptr<VCameraHal> camera = HalTestTool::MakeCameraHalTest(CameraType::MAIN_CAMERA);
mAllCamerasMock[CameraType::MAIN_CAMERA] = camera;
}
void DestroyAllCamerasMock(void)
{
mAllCamerasMock.clear();
}
protected:
// const AppParam mAppParam;
std::map<CameraType, std::shared_ptr<VCameraHal>> mAllCamerasMock;
};
// ../output_files/test/bin/MediaManagerTest --gtest_filter=MediaManagerTest.INTEGRATION_MediaManager_EXAMPLE_Demo0
TEST_F(MediaManagerTest, INTEGRATION_MediaManager_EXAMPLE_Demo0)
{
SetAllCamerasResult(mAllCamerasMock);
std::shared_ptr<VMediaMonitor> monitor = MediaManagerTestTool::MakeMonitorMock();
IMediaManager::GetInstance()->Init();
IMediaManager::GetInstance()->SetMediaMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockReportCameraEvent("test name", CameraType::MAIN_CAMERA);
// MockAppClientConnect();
// std::this_thread::sleep_for(std::chrono::milliseconds(100));
// MockSetRecordingStatus(RecordingStatus::RECORDING_START);
// std::this_thread::sleep_for(std::chrono::milliseconds(2000));
// IAppManager::GetInstance()->UnInit();
IMediaManager::GetInstance()->UnInit();
}
} // namespace MediaManagerTest

View File

@ -0,0 +1,39 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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.
*/
#ifndef MEDIA_MANAGER_TEST_TOOL_H
#define MEDIA_MANAGER_TEST_TOOL_H
#include "GtestUsing.h"
#include "IMediaManager.h"
#include <memory>
class MediaManagerTestTool
{
public:
MediaManagerTestTool() = default;
virtual ~MediaManagerTestTool() = default;
void Init(void);
void UnInit(void);
private:
void MediaManagerMockInit(std::shared_ptr<IMediaManager> &vMock);
void MediaMonitorInit(std::shared_ptr<VMediaMonitor> &vMock);
private:
std::shared_ptr<IMediaManager> mMediaManagerMock;
std::shared_ptr<VMediaMonitor> mMediaMonitorMock;
public:
static std::shared_ptr<VMediaMonitor> MakeMonitorMock(void);
};
#endif

87
test/test_develop.md Normal file
View File

@ -0,0 +1,87 @@
# 1. 测试用例开发规范
&emsp;&emsp;便于对测试用例进行管理,指定测试用例开发规范。
## 1.1. 命名规则
&emsp;&emsp;制定命名规则是为了规范测试用例行为为自动化测试提供管理遍历因为googletest支持根据测试用例名字通配符检索运行目标测试用例。
### 1.1.1. 基本概念
**硬件仿真**:对接口属性/数据进行模拟/检测的能力常见于芯片板级依赖的接口例如串口GPIO等还有芯片SDK依赖接口例如音视频编解码接口
**单元测试**单元测试指对单一功能接口的测试一般不需要或少量使用gtest的mock功能即可完成测试不需要实际链接外部接口对模块内部代码功能进行有效验证
**用例所属模块**:测试时锁定目标测试用例的作用;
**集成测试**集成测试指对复杂业务进行测试往往需要跨多层级多模块需要大量使用gtest的mock功能才能完成的复杂逻辑测试一般不针对代码本身而是针对产品定义的功能进行有效验证
**测试用例**
1. EXAMPLE:对模块接口进行使用演示的测试用例;
2. AUTO:自动化运行的测试用例;
3. STRESS:压力测试用例,一般不在单次测试中执行;
### 1.1.2. 测试用例命名:
1. 仿真硬件仿真使用hardware simulation的缩写**HS**硬件仿真只能运行在Ubuntu系统进行仿真测试真实硬件接口使用real hardware的缩写**RH**,真实硬件接口测试用例只能运行在开发板进行真机测试;
2. 测试用例类型含单元测试UNIT和集成测试INTEGRATION
3. 用例所属模块:大小驼峰;
4. 测试用例属性EXAMPLE/AUTO/STRESS
5. 测试用例小名;
示例:
该测试用例标识属于SharedData模块的单元测试作为example具有演示接口使用规范的作用测试用例名为Deme7。
```
TEST(SharedDataTest, UNIT_SharedData_EXAMPLE_Demo7)
{
// TODO:
}
```
### 1.1.3. 测试源码文件命名:
1. 每个测试可执行文件都有一个标准mian函数文件名统一为mainTest.cpp;
```
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
#include <unistd.h>
int main(int argc, char *argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS(); // 此处执行所有的测试用例;
}
```
2. 针对源码文件对应的测试代码文件名为源码文件名_Test.cpp;
```
例如cjson.c 对应的测试源码文件为cjson_Test.cpp
```
3. 适配层源码文件的TEST文件命名区分Simulator版本和常规版本Simulator版本在ubuntu模拟仿真环境测试执行代码会调用芯片SDK接口常规版本必须运行在开发板
```
├── CMakeLists.txt
├── mainTest.cpp
├── src
│   └── BoardMain_Test.cpp // 常规测试代码,可运行在开发板系统;
└── src_mock
└── BoardMain_Simulator_Test.cpp // 运行过程会调用芯片依赖的API,需要对这些API进行mock处理实现Linux x86平台的仿真测试
```
4.
## 1.2. TestTool 测试工具
&emsp;&emsp; 每个独立模块SDK内具有独立CMakeLists.txt的代码模块都有对应的TestTool模块主要用于对该模块的接口进行gmock处理且对外暴露接口具备复用属性。一般类命名模块名称+TestTool。
## 1.3. 目录结构
&emsp;&emsp;所有测试代码位于< test >目录下,< test >下的目录结构保持和< SDK >一致表示对应sdk代码模块的测试代码
如果是芯片平台的test目录需要区分板载测试代码和x86 Linux系统的测试代码如下
```
└── hal
├── CMakeLists.txt
├── mainTest.cpp
├── src // 芯片平台的测试代码;
├── src_mock // Linux x86测试代码 需要对板载接口打桩进行测试;
└── tool // TestTool:该模块需要对外复用的测试代码;
```

11
test/utils/CMakeLists.txt Normal file
View File

@ -0,0 +1,11 @@
#Compile gtest for test code.
add_subdirectory(Config)
add_subdirectory(Log)
add_subdirectory(SharedData)
add_subdirectory(WebServer)
add_subdirectory(UartDevice)
add_subdirectory(LinuxApiMock)
add_subdirectory(McuProtocol)
add_subdirectory(FxHttpServer)
add_subdirectory(TestManager)
add_subdirectory(TcpModule)

View File

@ -0,0 +1,49 @@
# include(${CMAKE_SOURCE_DIR}/build/independent_source.cmake)
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
set(EXECUTABLE_OUTPUT_PATH ${TEST_OUTPUT_PATH}/bin)
include_directories(
.
./src
./include
${UTILS_SOURCE_PATH}/Log/include
${UTILS_SOURCE_PATH}/StatusCode/include
${UTILS_SOURCE_PATH}/Config/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include
)
link_directories(
${LIBS_OUTPUT_PATH}
${EXTERNAL_LIBS_OUTPUT_PATH}
)
aux_source_directory(. SRC_FILES)
aux_source_directory(./src SRC_FILES)
set(TARGET_NAME ConfigTest)
add_executable(${TARGET_NAME} ${SRC_FILES})
target_link_libraries(${TARGET_NAME} ConfigBase gtest gmock pthread)
if(${TEST_COVERAGE} MATCHES "true")
target_link_libraries(${TARGET_NAME} gcov)
endif()
if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true")
add_custom_target(
ConfigTest_code_check
COMMAND ${CLANG_TIDY_EXE}
-checks='${CLANG_TIDY_CHECKS}'
--header-filter=.*
--system-headers=false
${SRC_FILES}
${CLANG_TIDY_CONFIG}
# --line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/gtest.h\"}]'
--line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/*.h\"}]'
-p ${PLATFORM_PATH}/cmake-shell
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/utils/Config
)
add_custom_command(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make ConfigTest_code_check
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
endif()

View File

@ -0,0 +1,9 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
#include <unistd.h>
int main(int argc, char *argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@ -0,0 +1,18 @@
#include "ILog.h"
#include "Config.h"
// #include <gmock/gmock.h>
#include <gtest/gtest.h>
namespace ConfigTest
{
// ../output_files/test/bin/ConfigTest --gtest_filter=ConfigTest.Demo
TEST(ConfigTest, Demo)
{
CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END);
VConfig *config = OpenConfigFile("./config");
int value = 0;
ConfigGetInt(config, "number", &value);
ILogUnInit();
DestroyLogModule();
}
} // namespace ConfigTest

View File

@ -0,0 +1,58 @@
# include(${CMAKE_SOURCE_DIR}/build/independent_source.cmake)
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
set(EXECUTABLE_OUTPUT_PATH ${TEST_OUTPUT_PATH}/bin)
include_directories(
${UTILS_SOURCE_PATH}/StatusCode/include
${UTILS_SOURCE_PATH}/Log/include
${UTILS_SOURCE_PATH}/FxHttpServer/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include
)
link_directories(
${LIBS_OUTPUT_PATH}
${EXTERNAL_LIBS_OUTPUT_PATH}
)
aux_source_directory(. SRC_FILES)
aux_source_directory(./src SRC_FILES)
set(TARGET_NAME FxHttpServerTest)
add_executable(${TARGET_NAME} ${SRC_FILES})
target_link_libraries(${TARGET_NAME} FxHttpServer gtest gmock pthread Log)
if(${TEST_COVERAGE} MATCHES "true")
target_link_libraries(${TARGET_NAME} gcov)
endif()
if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true")
add_custom_target(
FxHttpServerTest_code_check
COMMAND ${CLANG_TIDY_EXE}
-checks='${CLANG_TIDY_CHECKS}'
--header-filter=.*
--system-headers=false
${SRC_FILES}
${CLANG_TIDY_CONFIG}
# --line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/gtest.h\"}]'
--line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/*.h\"}]'
-p ${PLATFORM_PATH}/cmake-shell
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/utils/FxHttpServer
)
file(GLOB_RECURSE HEADER_FILES *.h)
add_custom_target(
FxHttpServerTest_code_format
COMMAND ${CLANG_FORMAT_EXE}
-style=file
-i ${SRC_FILES} ${HEADER_FILES}
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/utils/FxHttpServer
)
add_custom_command(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make FxHttpServerTest_code_check
COMMAND make FxHttpServerTest_code_format
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
endif()
define_file_name(${TARGET_NAME})

View File

@ -0,0 +1,23 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
#include <unistd.h>
int main(int argc, char *argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@ -0,0 +1,49 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 "FxHttpServer.h"
#include "ILog.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
namespace FxHttpServerTest
{
static const char *gResponse = " {\
\"result\":0,\
\"info\":{\
\"status\":0,\
\"free\":12000,\
\"total\":\"64000\"\
}\
}";
void HttpHandle(const char *url, const unsigned int urlLength, ResponseHandle responseHandle, void *context)
{
if (url) {
LogInfo("url = %s\n", url);
if (memcmp(url, "/set", strlen("/set")) == 0) {
responseHandle(gResponse, context);
}
}
}
// ../output_files/test/bin/FxHttpServerTest --gtest_filter=FxHttpServerTest.INTEGRATION_AppManager_EXAMPLE_Demo
TEST(FxHttpServerTest, INTEGRATION_AppManager_EXAMPLE_Demo)
{
CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END);
FxHttpServerInit(HttpHandle, 8080);
std::this_thread::sleep_for(std::chrono::milliseconds(1000 * 10));
FxHttpServerUnInit();
ILogUnInit();
}
} // namespace FxHttpServerTest

View File

@ -0,0 +1,52 @@
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
set(EXECUTABLE_OUTPUT_PATH ${EXEC_OUTPUT_PATH})
set(LIBRARY_OUTPUT_PATH ${LIBS_OUTPUT_PATH})
include_directories(
./src
./include
${UTILS_SOURCE_PATH}/Log/include
${TEST_SOURCE_PATH}
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include
)
# link_directories(
# ${EXTERNAL_SOURCE_PATH}/libconfig/libconfig-1.7.3/lib/.libs
# )
aux_source_directory(./src SRC_FILES)
set(TARGET_NAME LinuxApiMock)
add_library(${TARGET_NAME} STATIC ${SRC_FILES})
target_link_libraries(${TARGET_NAME} LinuxApi gtest gmock Log)
if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true")
add_custom_target(
LinuxApiMock_code_check
COMMAND ${CLANG_TIDY_EXE}
-checks='${CLANG_TIDY_CHECKS}'
--header-filter=.*
--system-headers=false
${SRC_FILES}
${CLANG_TIDY_CONFIG}
-p ${PLATFORM_PATH}/cmake-shell
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/utils/LinuxApiMock
)
file(GLOB_RECURSE HEADER_FILES *.h)
add_custom_target(
LinuxApiMock_code_format
COMMAND ${CLANG_FORMAT_EXE}
-style=file
-i ${SRC_FILES} ${HEADER_FILES}
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/utils/LinuxApiMock
)
add_custom_command(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make LinuxApiMock_code_check
COMMAND make LinuxApiMock_code_format
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
endif()
define_file_name(${TARGET_NAME})

View File

@ -0,0 +1,3 @@
# 1. Linux 系统函数模拟接口
&emsp;&emsp; 为了方便在ubuntu系统进行代码的调试运行对Linux标准函数进行多态转换。

View File

@ -0,0 +1,71 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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.
*/
#ifndef LINUX_API_MOCK_H
#define LINUX_API_MOCK_H
#include "GtestUsing.h"
#include <memory>
constexpr int INVALID_HANDLE = -1;
constexpr int MOCK_SELECT_TIME_OUT = 0;
class LinuxApiMock
{
public:
LinuxApiMock() = default;
virtual ~LinuxApiMock() = default;
static std::shared_ptr<LinuxApiMock> &GetInstance(std::shared_ptr<LinuxApiMock> *impl = nullptr);
virtual void Init();
virtual void UnInit();
virtual int fx_open(const char *pathname, int flags);
virtual int fx_tcgetattr(int fd, struct termios *termios_p);
virtual int fx_tcsetattr(int fd, int optional_actions, const struct termios *termios_p);
virtual ssize_t fx_write(int fd, const void *buf, size_t count);
virtual ssize_t fx_read(int fd, void *buf, size_t count);
virtual int fx_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
virtual int fx_fstat(int fd, struct stat *statbuf);
virtual int fx_access(const char *pathname, int mode);
virtual FILE *fx_fopen(const char *pathname, const char *mode);
};
/**
* A simulation interface class used for automated testing in Ubuntu systems, implementing the function of piling on
* some Linux standard interfaces.
*/
class LinuxTest : public LinuxApiMock, public std::enable_shared_from_this<LinuxTest>
{
public:
LinuxTest() = default;
virtual ~LinuxTest() = default;
MOCK_METHOD2(fx_open, int(const char *, int));
MOCK_METHOD2(fx_tcgetattr, int(int, struct termios *));
MOCK_METHOD3(fx_tcsetattr, int(int, int, const struct termios *));
MOCK_METHOD3(fx_write, ssize_t(int, const void *, size_t));
MOCK_METHOD3(fx_read, ssize_t(int, void *, size_t));
MOCK_METHOD5(fx_select, int(int, fd_set *, fd_set *, fd_set *, struct timeval *));
MOCK_METHOD2(fx_fstat, int(int, struct stat *));
MOCK_METHOD2(fx_access, int(const char *, int));
MOCK_METHOD2(fx_fopen, FILE *(const char *, const char *));
public:
/**
* @brief Get the Handle For Mock object
* Obtain a globally unique test handle throughout the entire testing cycle, and the handle generated through this
* interface can avoid handle conflicts between different testing modules. Attention needs to be paid to whether it
* conflicts with non test handles generated by the system.
* @return int
*/
virtual int GetHandleForMock(void);
public:
static std::shared_ptr<LinuxTest> CreateLinuxTest(void);
};
#endif

View File

@ -0,0 +1,25 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 "HandleManager.h"
HandleManager::HandleManager()
{
mFdMax = 1000;
}
int HandleManager::GetHandleForMock(void)
{
int value = mFdMax;
mFdMax++;
return value;
}

View File

@ -0,0 +1,37 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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.
*/
#ifndef HANDLE_MANAGER_H
#define HANDLE_MANAGER_H
#include "LinuxApiMock.h"
/**
* @brief Handle management class, which manages all handles in the test program to prevent unclear objects pointed to
* by handles during simulation.
*
*/
class HandleManager : virtual public LinuxTest
{
public:
HandleManager();
virtual ~HandleManager() = default;
int GetHandleForMock(void) override;
private:
/**
* @brief Ensure that false handles of the test simulation interface are not duplicated.
*
*/
int mFdMax;
};
#endif

View File

@ -0,0 +1,84 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 "LinuxApiMock.h"
#include "ILog.h"
#include "LinuxTestImpl.h"
#include "WrapApi.h"
std::shared_ptr<LinuxApiMock> &LinuxApiMock::GetInstance(std::shared_ptr<LinuxApiMock> *impl)
{
static auto instance = std::make_shared<LinuxApiMock>();
if (impl) {
// The non-thread-safe changing is only for gtest.
instance = *impl;
}
return instance;
}
void LinuxApiMock::Init()
{
}
void LinuxApiMock::UnInit()
{
}
int LinuxApiMock::fx_open(const char *pathname, int flags)
{
// LogInfo("Call the real api.\n");
return __real_fx_open(pathname, flags);
}
int LinuxApiMock::fx_tcgetattr(int fd, struct termios *termios_p)
{
// LogInfo("Call the real api.\n");
return __real_fx_tcgetattr(fd, termios_p);
}
int LinuxApiMock::fx_tcsetattr(int fd, int optional_actions, const struct termios *termios_p)
{
return __real_fx_tcsetattr(fd, optional_actions, termios_p);
}
ssize_t LinuxApiMock::fx_write(int fd, const void *buf, size_t count)
{
// LogInfo("Call the real api.\n");
return __real_fx_write(fd, buf, count);
}
ssize_t LinuxApiMock::fx_read(int fd, void *buf, size_t count)
{
// LogInfo("Call the real api.\n");
return __real_fx_read(fd, buf, count);
}
int LinuxApiMock::fx_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
{
// LogInfo("Call the real api.\n");
return __real_fx_select(nfds, readfds, writefds, exceptfds, timeout);
}
int LinuxApiMock::fx_fstat(int fd, struct stat *statbuf)
{
return __real_fx_fstat(fd, statbuf);
}
int LinuxApiMock::fx_access(const char *pathname, int mode)
{
return __real_fx_access(pathname, mode);
}
FILE *LinuxApiMock::fx_fopen(const char *pathname, const char *mode)
{
return __real_fx_fopen(pathname, mode);
}
std::shared_ptr<LinuxTest> LinuxTest::CreateLinuxTest(void)
{
std::shared_ptr<LinuxTest> test = std::make_shared<LinuxTestImpl>();
LinuxTestImpl::ApiInit(test);
return test;
}
int LinuxTest::GetHandleForMock(void)
{
return INVALID_HANDLE;
}

View File

@ -0,0 +1,164 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 "LinuxTestImpl.h"
#include "ILog.h"
#include <thread>
/**
* @brief The simulated interface has been subjected to lock serial processing to ensure that the return value can be
* safely returned through multiple threads. However, this may affect the timing of the test code, which is not very
* reasonable. If the behavior of the simulated interface is redefined, the impact can be ignored.
* @param mock
*/
void LinuxTestImpl::ApiInit(std::shared_ptr<LinuxTest> &mock)
{
LogInfo("ApiInit\n");
static int openFd = -1;
auto api_open = [=](const char *pathname, int flags) {
LogInfo("Call __real_fx_open, pathname = %s.\n", pathname);
openFd = __real_fx_open(pathname, flags);
LogInfo("openFd = %d\n", openFd);
};
EXPECT_CALL(*mock.get(), fx_open(_, _))
.WillRepeatedly(
DoAll(Invoke((std::dynamic_pointer_cast<LinuxTestImpl>(mock)).get(), &LinuxTestImpl::ApiLock),
WithArgs<0, 1>(Invoke(api_open)),
Invoke((std::dynamic_pointer_cast<LinuxTestImpl>(mock)).get(), &LinuxTestImpl::ApiUnlockThread),
ReturnPointee(&openFd)));
static int resultTcgetattr = -1;
auto api_tcgetattr = [=](int fd, struct termios *termios_p) {
resultTcgetattr = __real_fx_tcgetattr(fd, termios_p);
LogInfo("resultTcgetattr = %d\n", resultTcgetattr);
};
EXPECT_CALL(*mock.get(), fx_tcgetattr(_, _))
.WillRepeatedly(
DoAll(Invoke((std::dynamic_pointer_cast<LinuxTestImpl>(mock)).get(), &LinuxTestImpl::ApiLock),
WithArgs<0, 1>(Invoke(api_tcgetattr)),
Invoke((std::dynamic_pointer_cast<LinuxTestImpl>(mock)).get(), &LinuxTestImpl::ApiUnlockThread),
ReturnPointee(&resultTcgetattr)));
static int resultTcsetattr = -1;
auto api_tcsetattr = [=](int fd, int optional_actions, const struct termios *termios_p) {
resultTcsetattr = __real_fx_tcsetattr(fd, optional_actions, termios_p);
LogInfo("resultTcsetattr = %d\n", resultTcsetattr);
};
EXPECT_CALL(*mock.get(), fx_tcsetattr(_, _, _))
.WillRepeatedly(
DoAll(Invoke((std::dynamic_pointer_cast<LinuxTestImpl>(mock)).get(), &LinuxTestImpl::ApiLock),
WithArgs<0, 1, 2>(Invoke(api_tcsetattr)),
Invoke((std::dynamic_pointer_cast<LinuxTestImpl>(mock)).get(), &LinuxTestImpl::ApiUnlockThread),
ReturnPointee(&resultTcsetattr)));
static int writeLength = -1;
auto api_write = [=](int fd, const void *buf, size_t count) {
writeLength = __real_fx_write(fd, buf, count);
LogInfo("writeLength = %d\n", writeLength);
};
EXPECT_CALL(*mock.get(), fx_write(_, _, _))
.WillRepeatedly(
DoAll(Invoke((std::dynamic_pointer_cast<LinuxTestImpl>(mock)).get(), &LinuxTestImpl::ApiLock),
WithArgs<0, 1, 2>(Invoke(api_write)),
Invoke((std::dynamic_pointer_cast<LinuxTestImpl>(mock)).get(), &LinuxTestImpl::ApiUnlockThread),
ReturnPointee(&writeLength)));
static int readLength = -1;
auto api_read = [=](int fd, void *buf, size_t count) {
readLength = __real_fx_read(fd, buf, count);
LogInfo("readLength = %d\n", readLength);
};
EXPECT_CALL(*mock.get(), fx_read(_, _, _))
.WillRepeatedly(
DoAll(Invoke((std::dynamic_pointer_cast<LinuxTestImpl>(mock)).get(), &LinuxTestImpl::ApiLock),
WithArgs<0, 1, 2>(Invoke(api_read)),
Invoke((std::dynamic_pointer_cast<LinuxTestImpl>(mock)).get(), &LinuxTestImpl::ApiUnlockThread),
ReturnPointee(&readLength)));
static int selectResult = -1;
auto api_select = [=](int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout) {
selectResult = __real_fx_select(nfds, readfds, writefds, exceptfds, timeout);
LogInfo("selectResult = %d\n", selectResult);
};
EXPECT_CALL(*mock.get(), fx_select(_, _, _, _, _))
.WillRepeatedly(
DoAll(Invoke((std::dynamic_pointer_cast<LinuxTestImpl>(mock)).get(), &LinuxTestImpl::ApiLock),
WithArgs<0, 1, 2, 3, 4>(Invoke(api_select)),
Invoke((std::dynamic_pointer_cast<LinuxTestImpl>(mock)).get(), &LinuxTestImpl::ApiUnlockThread),
ReturnPointee(&selectResult)));
static int fstatResult = -1;
auto api_fstat = [=](int fd, struct stat *statbuf) {
LogInfo("Call __real_fx_fstat, fd = %d.\n", fd);
fstatResult = __real_fx_fstat(fd, statbuf);
LogInfo("fstatResult = %d\n", fstatResult);
};
EXPECT_CALL(*mock.get(), fx_fstat(_, _))
.WillRepeatedly(
DoAll(Invoke((std::dynamic_pointer_cast<LinuxTestImpl>(mock)).get(), &LinuxTestImpl::ApiLock),
WithArgs<0, 1>(Invoke(api_fstat)),
Invoke((std::dynamic_pointer_cast<LinuxTestImpl>(mock)).get(), &LinuxTestImpl::ApiUnlockThread),
ReturnPointee(&fstatResult)));
static int accessResult = -1;
auto api_access = [=](const char *pathname, int mode) {
accessResult = __real_fx_access(pathname, mode);
LogInfo("accessResult = %d\n", accessResult);
};
EXPECT_CALL(*mock.get(), fx_access(_, _))
.WillRepeatedly(
DoAll(Invoke((std::dynamic_pointer_cast<LinuxTestImpl>(mock)).get(), &LinuxTestImpl::ApiLock),
WithArgs<0, 1>(Invoke(api_access)),
Invoke((std::dynamic_pointer_cast<LinuxTestImpl>(mock)).get(), &LinuxTestImpl::ApiUnlockThread),
ReturnPointee(&accessResult)));
static FILE *fopenResult = nullptr;
auto api_fopen = [=](const char *pathname, const char *mode) {
fopenResult = __real_fx_fopen(pathname, mode);
};
EXPECT_CALL(*mock.get(), fx_fopen(_, _))
.WillRepeatedly(
DoAll(Invoke((std::dynamic_pointer_cast<LinuxTestImpl>(mock)).get(), &LinuxTestImpl::ApiLock),
WithArgs<0, 1>(Invoke(api_fopen)),
Invoke((std::dynamic_pointer_cast<LinuxTestImpl>(mock)).get(), &LinuxTestImpl::ApiUnlockThread),
ReturnPointee(&fopenResult)));
}
void LinuxTestImpl::Init()
{
}
void LinuxTestImpl::UnInit()
{
if (mApiThread.joinable()) {
mApiThread.join();
}
}
void LinuxTestImpl::ApiLock(void)
{
/**
* @brief This has been optimized and does not require locking; Place the lock position in the WrapApi file, and
* lock the Mock function to ensure that it returns the value of the global variable before it can return, to avoid
* the problem of obtaining incorrect return values due to multithreading timing errors.
*/
// mApiMutex.lock();
// LogInfo("lock api.\n");
}
void LinuxTestImpl::ApiUnlock(void)
{
// mApiMutex.unlock();
// LogInfo("unlock api.\n");
}
void LinuxTestImpl::ApiUnlockThread(void)
{
// LogInfo("ApiUnlockThread\n");
// if (mApiThread.joinable()) {
// mApiThread.join();
// }
// auto api_unlock = [](std::shared_ptr<LinuxTestImpl> test) {
// std::this_thread::sleep_for(std::chrono::milliseconds(API_LOCK_TIME_MS));
// test->ApiUnlock();
// };
// std::shared_ptr<LinuxTestImpl> test = std::dynamic_pointer_cast<LinuxTestImpl>(LinuxTest::shared_from_this());
// mApiThread = std::thread(api_unlock, test);
}

View File

@ -0,0 +1,49 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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.
*/
#ifndef LINUX_TEST_IMPL_H
#define LINUX_TEST_IMPL_H
#include "HandleManager.h"
#include "LinuxApiMock.h"
#include "WrapApi.h"
#include <mutex>
#include <thread>
/**
* @brief The simulated interface will be locked by default to ensure that the return value of the interface can be
* correctly obtained by the application in the default state. This is not very reasonable and should be optimized.
* This is the blocking time after locking, during which it is necessary to ensure that the application can obtain a
* global return value.
*/
constexpr int API_LOCK_TIME_MS = 5;
class LinuxTestImpl : public HandleManager
{
public:
LinuxTestImpl() = default;
virtual ~LinuxTestImpl() = default;
void Init() override;
void UnInit() override;
void ApiLock(void);
void ApiUnlock(void);
private:
void ApiUnlockThread(void);
public:
static void ApiInit(std::shared_ptr<LinuxTest> &mock);
private:
std::mutex mApiMutex;
std::thread mApiThread;
};
#endif

View File

@ -0,0 +1,77 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 "WrapApi.h"
#include "LinuxApiMock.h"
#include <mutex>
#ifdef __cplusplus
extern "C" {
#endif
int __wrap_fx_open(const char *pathname, int flags)
{
static std::mutex gMutex;
std::lock_guard<std::mutex> locker(gMutex);
return LinuxApiMock::GetInstance()->fx_open(pathname, flags);
}
int __wrap_fx_tcgetattr(int fd, struct termios *termios_p)
{
static std::mutex gMutex;
std::lock_guard<std::mutex> locker(gMutex);
return LinuxApiMock::GetInstance()->fx_tcgetattr(fd, termios_p);
}
int __wrap_fx_tcsetattr(int fd, int optional_actions, const struct termios *termios_p)
{
static std::mutex gMutex;
std::lock_guard<std::mutex> locker(gMutex);
return LinuxApiMock::GetInstance()->fx_tcsetattr(fd, optional_actions, termios_p);
}
ssize_t __wrap_fx_write(int fd, const void *buf, size_t count)
{
static std::mutex gMutex;
std::lock_guard<std::mutex> locker(gMutex);
return LinuxApiMock::GetInstance()->fx_write(fd, buf, count);
}
ssize_t __wrap_fx_read(int fd, void *buf, size_t count)
{
static std::mutex gMutex;
std::lock_guard<std::mutex> locker(gMutex);
return LinuxApiMock::GetInstance()->fx_read(fd, buf, count);
}
int __wrap_fx_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
{
static std::mutex gMutex;
std::lock_guard<std::mutex> locker(gMutex); // TODO: The select function can cause severe blocking here.
return LinuxApiMock::GetInstance()->fx_select(nfds, readfds, writefds, exceptfds, timeout);
}
int __wrap_fx_fstat(int fd, struct stat *statbuf)
{
static std::mutex gMutex;
std::lock_guard<std::mutex> locker(gMutex);
return LinuxApiMock::GetInstance()->fx_fstat(fd, statbuf);
}
int __wrap_fx_access(const char *pathname, int mode)
{
static std::mutex gMutex;
std::lock_guard<std::mutex> locker(gMutex);
return LinuxApiMock::GetInstance()->fx_access(pathname, mode);
}
FILE *__wrap_fx_fopen(const char *pathname, const char *mode)
{
static std::mutex gMutex;
std::lock_guard<std::mutex> locker(gMutex);
return LinuxApiMock::GetInstance()->fx_fopen(pathname, mode);
}
#ifdef __cplusplus
}
#endif

View File

@ -0,0 +1,38 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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.
*/
#ifndef WRAP_API_H
#define WRAP_API_H
#include <stdio.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#ifdef __cplusplus
extern "C" {
#endif
// Modify all the real api.
int __real_fx_open(const char *pathname, int flags);
int __real_fx_tcgetattr(int fd, struct termios *termios_p);
int __real_fx_tcsetattr(int fd, int optional_actions, const struct termios *termios_p);
ssize_t __real_fx_write(int fd, const void *buf, size_t count);
ssize_t __real_fx_read(int fd, void *buf, size_t count);
int __real_fx_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
int __real_fx_fstat(int fd, struct stat *statbuf);
int __real_fx_access(const char *pathname, int mode);
FILE *__real_fx_fopen(const char *pathname, const char *mode);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,48 @@
# include(${CMAKE_SOURCE_DIR}/build/independent_source.cmake)
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
set(EXECUTABLE_OUTPUT_PATH ${TEST_OUTPUT_PATH}/bin)
include_directories(
.
./src
./include
${UTILS_SOURCE_PATH}/Log/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include
)
link_directories(
${LIBS_OUTPUT_PATH}
${EXTERNAL_LIBS_OUTPUT_PATH}
)
aux_source_directory(. SRC_FILES)
aux_source_directory(./src SRC_FILES)
set(TARGET_NAME LogTest)
add_executable(${TARGET_NAME} ${SRC_FILES})
target_link_libraries(${TARGET_NAME} Log gtest gmock pthread)
if(${TEST_COVERAGE} MATCHES "true")
target_link_libraries(${TARGET_NAME} gcov)
endif()
if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true")
add_custom_target(
LogTest_code_check
COMMAND ${CLANG_TIDY_EXE}
-checks='${CLANG_TIDY_CHECKS}'
--header-filter=.*
--system-headers=false
${SRC_FILES}
${CLANG_TIDY_CONFIG}
# --line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/gtest.h\"}]'
--line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/*.h\"}]'
-p ${PLATFORM_PATH}/cmake-shell
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/utils/Log
)
add_custom_command(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make LogTest_code_check
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
endif()

View File

@ -0,0 +1,9 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
#include <unistd.h>
int main(int argc, char *argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@ -0,0 +1,17 @@
#include "ILog.h"
// #include <gmock/gmock.h>
#include <gtest/gtest.h>
namespace ILogTest
{
// ../output_files/test/bin/LogTest --gtest_filter=ILogTest.Demo
TEST(ILogTest, Demo)
{
CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END);
LogInfo("hello world.");
LogError("create ... failed.");
LogDebug("a = %d b = %s", 124, "apple");
ILogUnInit();
DestroyLogModule();
}
} // namespace ILogTest

View File

@ -0,0 +1,4 @@
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tool/CMakeLists.txt")
add_subdirectory(tool)
endif()

View File

@ -0,0 +1,124 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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.
*/
#ifndef MCU_PROTOCOL_TEST_TOOL_H
#define MCU_PROTOCOL_TEST_TOOL_H
#include "LinuxApiMock.h"
#include "UartDeviceTestTool.h"
#include <list>
#include <mutex>
#include <thread>
constexpr int READ_PRINT = 0;
constexpr int WRITE_PRINT = 1;
constexpr bool PROTOCOL_HANDLED = true;
constexpr bool PROTOCOL_NOT_HANDLED = false;
using ProtocolHandleFunc = std::function<bool(std::shared_ptr<LinuxTest> &, const int &, const void *, size_t)>;
class McuProtocolTestTool : virtual public UartDeviceTestTool
{
public:
McuProtocolTestTool();
virtual ~McuProtocolTestTool() = default;
void Init(std::shared_ptr<LinuxTest> &mock, const UartInfo &uart);
void UnInit(void);
void MockOtherSideAskIpcMission(std::shared_ptr<LinuxTest> &mock, const unsigned int &serialNumber);
void MockOtherSideAskHeartBeat(std::shared_ptr<LinuxTest> &mock, const unsigned int &serialNumber);
void MockOtherSideAskGetIntervalStart(std::shared_ptr<LinuxTest> &mock, const unsigned int &serialNumber);
void MockOtherSideAskGetDateTime(std::shared_ptr<LinuxTest> &mock, const unsigned int &serialNumber);
void MockOtherSideAskGetPirSensitivity(std::shared_ptr<LinuxTest> &mock, const unsigned int &serialNumber);
void MockOtherSideAskSendAnyData(std::shared_ptr<LinuxTest> &mock, const void *data, const size_t &size);
void ReadNothingAnyTime(std::shared_ptr<LinuxTest> &mock);
void ReadOnceSelectSucceed(std::shared_ptr<LinuxTest> &mock, const int &uartFd);
void MockOtherSideIpcMissionReply(const unsigned char &replyIpcMission);
private:
void InitProtocolBuff(void);
void CheckSerialNumber(const void *buf, const size_t &count);
void ChecCRC16Code(const void *buf, const size_t &count);
void ResetCheckCode(const void *buf, const size_t &count);
/**
* @brief The function of initializing the fx_select function can quickly return when selecting without waiting for
* the timeout to end.
* @param mock
* @param uartFd
*/
void SelectInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd);
void ReplySelectSucceed(std::shared_ptr<LinuxTest> &mock, const int &uartFd);
void ReplySelectTimeOut(std::shared_ptr<LinuxTest> &mock, const int &uartFd);
bool MonitorProtocolPacket(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf, size_t count);
bool IpcMissionProtocolHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf, size_t count);
void IpcMissionProtocolInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf, size_t count);
bool CutOffPowerSupplyProtocolHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf,
size_t count);
bool FeedWatchDogProtocolHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf, size_t count);
bool FeedingCycleProtocolHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf, size_t count);
void FeedingCycleProtocolInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf, size_t count);
bool SetDataTimeProtocolHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf, size_t count);
void SetDataTimeProtocolInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf, size_t count);
bool SetPirSensitivityProtocolHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf,
size_t count);
void SetPirSensitivityProtocolInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf,
size_t count);
bool ContorlInfraredLightHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf, size_t count);
void ContorlInfraredLightInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf, size_t count);
bool GetPhotosensitivityValueHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf,
size_t count);
void GetPhotosensitivityValueInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf,
size_t count);
void LockProtocolHandle(void);
void UnlockProtocolHandle(void);
void UnlockThread(void);
void PipeSelectTimeoutForProtocolHandleImmediately(void);
private:
void OtherSideAskIpcMissionHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd,
const unsigned int &serialNumber);
void OtherSideAskIpcMissionInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd,
const unsigned int &serialNumber);
void OtherSideAskHeartBeatHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd,
const unsigned int &serialNumber);
void OtherSideAskHeartBeatInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd,
const unsigned int &serialNumber);
void OtherSideAskGetIntervalStartHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd,
const unsigned int &serialNumber);
void OtherSideAskGetIntervalStartInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd,
const unsigned int &serialNumber);
void OtherSideAskGetDateTimeHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd,
const unsigned int &serialNumber);
void OtherSideAskGetDateTimeInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd,
const unsigned int &serialNumber);
void OtherSideAskGetPirSensitivityHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd,
const unsigned int &serialNumber);
void OtherSideAskGetPirSensitivityInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd,
const unsigned int &serialNumber);
void OtherSideAskSendSomeDataHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *data,
const size_t &size);
void OtherSideAskSendSomeDataInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *data,
const size_t &size);
private:
static void PrintHexadecimalData(const void *buf, const size_t &bufLength, const char *log);
private:
std::mutex mMutex;
bool mThreadRuning;
std::thread mLockThread;
std::thread mUnLockThread;
std::list<unsigned int> mSerialNumberList;
bool mPipeFdMockSelectInit;
int mPipeFdMockSelect[2];
std::list<ProtocolHandleFunc> mProtocolHandle;
std::shared_ptr<UartInfo> mUart;
int mUartFd;
};
#endif

View File

@ -0,0 +1,62 @@
# include(${CMAKE_SOURCE_DIR}/build/independent_source.cmake)
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
set(EXECUTABLE_OUTPUT_PATH ${TEST_OUTPUT_PATH}/bin)
include_directories(
${UTILS_SOURCE_PATH}/StatusCode/include
${UTILS_SOURCE_PATH}/Log/include
${UTILS_SOURCE_PATH}/SharedData/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include
)
link_directories(
${LIBS_OUTPUT_PATH}
${EXTERNAL_LIBS_OUTPUT_PATH}
)
aux_source_directory(. SRC_FILES)
aux_source_directory(./src SRC_FILES)
set(TARGET_NAME SharedDataTest)
add_executable(${TARGET_NAME} ${SRC_FILES})
target_link_libraries(${TARGET_NAME} SharedData gtest gmock pthread Log)
if(${TEST_COVERAGE} MATCHES "true")
target_link_libraries(${TARGET_NAME} gcov)
endif()
if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true")
add_custom_target(
SharedDataTest_code_check
COMMAND ${CLANG_TIDY_EXE}
-checks='${CLANG_TIDY_CHECKS}'
--header-filter=.*
--system-headers=false
${SRC_FILES}
${CLANG_TIDY_CONFIG}
# --line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/gtest.h\"}]'
--line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/*.h\"}]'
-p ${PLATFORM_PATH}/cmake-shell
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/utils/SharedData
)
file(GLOB_RECURSE HEADER_FILES *.h)
add_custom_target(
SharedDataTest_code_format
COMMAND ${CLANG_FORMAT_EXE}
-style=file
-i ${SRC_FILES} ${HEADER_FILES}
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/utils/SharedData
)
add_custom_command(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make SharedDataTest_code_check
COMMAND make SharedDataTest_code_format
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
endif()
define_file_name(${TARGET_NAME})

View File

@ -0,0 +1,23 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
#include <unistd.h>
int main(int argc, char *argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@ -0,0 +1,347 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 "ILog.h"
#include "SharedData.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
namespace SharedDataTest
{
// ../output_files/test/bin/SharedDataTest --gtest_filter=SharedDataTest.Demo
TEST(SharedDataTest, Demo)
{
CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END);
constexpr int PROJECT_ID = 9;
void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID);
void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID);
if (nullptr != sharedDataMinor) {
int writableData = 99;
IMakeSharedData(sharedDataMinor, sizeof(int), sizeof(int));
ISetWritableData(sharedDataMinor, &writableData, sizeof(int));
}
if (nullptr != sharedDataPrimary) {
int readableData = -1;
IMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int));
IGetReadableData(sharedDataPrimary, &readableData, sizeof(int));
LogInfo("readableData = %d\n", readableData);
IShareDataFree(sharedDataPrimary);
}
if (nullptr != sharedDataMinor) {
IShareDataFree(sharedDataMinor);
}
ILogUnInit();
}
// ../output_files/test/bin/SharedDataTest --gtest_filter=SharedDataTest.Demo2
TEST(SharedDataTest, Demo2)
{
constexpr int WRITABLE_DATA = 189;
CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END);
constexpr int PROJECT_ID = 9;
void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID);
void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID);
if (nullptr != sharedDataMinor) {
IMakeSharedData(sharedDataMinor, sizeof(int), sizeof(int));
ISetWritableData(sharedDataMinor, (void *)&WRITABLE_DATA, sizeof(int));
}
if (nullptr != sharedDataPrimary) {
int readableData = 0;
IMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int));
StatusCode code = IGetReadableData(sharedDataPrimary, &readableData, sizeof(int));
PrintStringCode(code);
LogInfo("readableData = %d\n", readableData);
EXPECT_EQ(readableData, WRITABLE_DATA);
IShareDataFree(sharedDataPrimary);
}
if (nullptr != sharedDataMinor) {
IShareDataFree(sharedDataMinor);
}
ILogUnInit();
}
// ../output_files/test/bin/SharedDataTest --gtest_filter=SharedDataTest.Demo3
TEST(SharedDataTest, Demo3)
{
CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END);
constexpr int PROJECT_ID = 9;
void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID);
void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID);
if (nullptr != sharedDataMinor) {
constexpr int WRITABLE_DATA_LENGTH = 9;
char writableData[WRITABLE_DATA_LENGTH] = {0};
IMakeSharedData(sharedDataMinor, sizeof(int), sizeof(writableData));
ISetWritableData(sharedDataMinor, writableData, sizeof(writableData));
}
if (nullptr != sharedDataPrimary) {
constexpr int DEFAULT_DATA = 0;
int readableData = DEFAULT_DATA;
IMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int));
StatusCode code = IGetReadableData(sharedDataPrimary, &readableData, sizeof(int));
PrintStringCode(code);
LogInfo("readableData = %d\n", readableData);
EXPECT_NE(code.mStatusCode, STATUS_CODE_OK);
EXPECT_EQ(readableData, DEFAULT_DATA);
IShareDataFree(sharedDataPrimary);
}
if (nullptr != sharedDataMinor) {
IShareDataFree(sharedDataMinor);
}
ILogUnInit();
}
// ../output_files/test/bin/SharedDataTest --gtest_filter=SharedDataTest.Demo4
TEST(SharedDataTest, Demo4)
{
constexpr int WRITABLE_DATA = 189;
CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END);
constexpr int PROJECT_ID = 9;
void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID);
void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID);
if (nullptr != sharedDataMinor) {
IMakeSharedData(sharedDataMinor, sizeof(int) * 2, sizeof(int));
ISetWritableData(sharedDataMinor, (void *)&WRITABLE_DATA, sizeof(int));
}
if (nullptr != sharedDataPrimary) {
constexpr int DEFAULT_DATA = 0;
int readableData = DEFAULT_DATA;
IMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int));
StatusCode code = IGetReadableData(sharedDataPrimary, &readableData, sizeof(int));
PrintStringCode(code);
LogInfo("readableData = %d\n", readableData);
EXPECT_EQ(code.mStatusCode, STATUS_CODE_OK);
EXPECT_EQ(readableData, WRITABLE_DATA);
IShareDataFree(sharedDataPrimary);
}
if (nullptr != sharedDataMinor) {
IShareDataFree(sharedDataMinor);
}
ILogUnInit();
}
// ../output_files/test/bin/SharedDataTest --gtest_filter=SharedDataTest.Demo5
TEST(SharedDataTest, Demo5)
{
CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END);
constexpr int PROJECT_ID = 9;
void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID);
if (nullptr != sharedDataPrimary) {
constexpr int DEFAULT_DATA = 0;
int readableData = DEFAULT_DATA;
IMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int));
StatusCode code = IGetReadableData(sharedDataPrimary, &readableData, sizeof(int));
PrintStringCode(code);
LogInfo("readableData = %d\n", readableData);
EXPECT_NE(code.mStatusCode, STATUS_CODE_OK);
EXPECT_EQ(readableData, DEFAULT_DATA);
IShareDataFree(sharedDataPrimary);
}
ILogUnInit();
}
// ../output_files/test/bin/SharedDataTest --gtest_filter=SharedDataTest.Demo6
TEST(SharedDataTest, Demo6)
{
CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END);
constexpr int PROJECT_ID = 9;
void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID);
void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID);
if (nullptr != sharedDataPrimary) {
constexpr int DEFAULT_DATA = 0;
int readableData = DEFAULT_DATA;
IMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int));
StatusCode code = IGetReadableData(sharedDataPrimary, &readableData, sizeof(int));
PrintStringCode(code);
LogInfo("readableData = %d\n", readableData);
EXPECT_NE(code.mStatusCode, STATUS_CODE_OK);
EXPECT_EQ(readableData, DEFAULT_DATA);
IShareDataFree(sharedDataPrimary);
}
if (nullptr != sharedDataMinor) {
IShareDataFree(sharedDataMinor);
}
ILogUnInit();
}
// ../output_files/test/bin/SharedDataTest --gtest_filter=SharedDataTest.Demo7
TEST(SharedDataTest, Demo7)
{
constexpr int WRITABLE_DATA = 189;
CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END);
constexpr int PROJECT_ID = 9;
void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID);
void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID);
if (nullptr != sharedDataPrimary) {
IMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int));
ISetWritableData(sharedDataPrimary, (void *)&WRITABLE_DATA, sizeof(int));
}
if (nullptr != sharedDataMinor) {
constexpr int DEFAULT_DATA = 0;
int readableData = DEFAULT_DATA;
IMakeSharedData(sharedDataMinor, sizeof(int), sizeof(int));
StatusCode code = IGetReadableData(sharedDataMinor, &readableData, sizeof(int));
PrintStringCode(code);
LogInfo("readableData = %d\n", readableData);
EXPECT_EQ(code.mStatusCode, STATUS_CODE_OK);
EXPECT_EQ(readableData, WRITABLE_DATA);
IShareDataFree(sharedDataMinor);
}
if (nullptr != sharedDataPrimary) {
IShareDataFree(sharedDataPrimary);
}
ILogUnInit();
}
// ../output_files/test/bin/SharedDataTest --gtest_filter=SharedDataTest.UNIT_SharedData_DEME_Demo7
TEST(SharedDataTest, UNIT_SharedData_DEME_Demo7)
{
constexpr int WRITABLE_DATA = 189;
CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END);
constexpr int PROJECT_ID = 9;
void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID);
void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID);
if (nullptr != sharedDataPrimary) {
IMakeSharedData(sharedDataPrimary, sizeof(int) * 2, sizeof(int));
ISetWritableData(sharedDataPrimary, (void *)&WRITABLE_DATA, sizeof(int));
}
if (nullptr != sharedDataMinor) {
constexpr int DEFAULT_DATA = 0;
int readableData = DEFAULT_DATA;
IMakeSharedData(sharedDataMinor, sizeof(int), sizeof(int));
StatusCode code = IGetReadableData(sharedDataMinor, &readableData, sizeof(int));
PrintStringCode(code);
LogInfo("readableData = %d\n", readableData);
EXPECT_NE(code.mStatusCode, STATUS_CODE_OK);
EXPECT_EQ(readableData, DEFAULT_DATA);
IShareDataFree(sharedDataMinor);
}
if (nullptr != sharedDataPrimary) {
IShareDataFree(sharedDataPrimary);
}
ILogUnInit();
}
// ../output_files/test/bin/SharedDataTest --gtest_filter=SharedDataTest.UNIT_SharedData_DEME_Demo8
TEST(SharedDataTest, UNIT_SharedData_DEME_Demo8)
{
CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END);
constexpr int PROJECT_ID = 9;
void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID);
void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID);
if (nullptr != sharedDataPrimary) {
int writableData[2] = {0};
IMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int) * 2);
ISetWritableData(sharedDataPrimary, &writableData, sizeof(writableData));
}
if (nullptr != sharedDataMinor) {
constexpr int DEFAULT_DATA = 0;
int readableData = DEFAULT_DATA;
IMakeSharedData(sharedDataMinor, sizeof(int), sizeof(int));
StatusCode code = IGetReadableData(sharedDataMinor, &readableData, sizeof(int));
PrintStringCode(code);
LogInfo("readableData = %d\n", readableData);
EXPECT_NE(code.mStatusCode, STATUS_CODE_OK);
EXPECT_EQ(readableData, DEFAULT_DATA);
IShareDataFree(sharedDataMinor);
}
if (nullptr != sharedDataPrimary) {
IShareDataFree(sharedDataPrimary);
}
ILogUnInit();
}
// ../output_files/test/bin/SharedDataTest --gtest_filter=SharedDataTest.UNIT_SharedData_DEME_Demo9
TEST(SharedDataTest, UNIT_SharedData_DEME_Demo9)
{
constexpr int WRITABLE_DATA = 189;
CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END);
constexpr int PROJECT_ID = 9;
void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID);
void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID);
if (nullptr != sharedDataPrimary) {
IMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int));
ISetWritableData(sharedDataPrimary, (void *)&WRITABLE_DATA, sizeof(int));
}
if (nullptr != sharedDataMinor) {
constexpr int DEFAULT_DATA = 0;
int readableData = DEFAULT_DATA;
IMakeSharedData(sharedDataMinor, sizeof(int), sizeof(int) * 2);
StatusCode code = IGetReadableData(sharedDataMinor, &readableData, sizeof(int));
PrintStringCode(code);
LogInfo("readableData = %d\n", readableData);
EXPECT_NE(code.mStatusCode, STATUS_CODE_OK);
EXPECT_EQ(readableData, DEFAULT_DATA);
IShareDataFree(sharedDataMinor);
}
if (nullptr != sharedDataPrimary) {
IShareDataFree(sharedDataPrimary);
}
ILogUnInit();
}
// ../output_files/test/bin/SharedDataTest --gtest_filter=SharedDataTest.UNIT_SharedData_DEME_Demo10
TEST(SharedDataTest, UNIT_SharedData_DEME_Demo10)
{
constexpr int WRITABLE_DATA = 189;
CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END);
constexpr int PROJECT_ID = 9;
void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID);
void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID);
if (nullptr != sharedDataPrimary) {
IMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int));
ISetWritableData(sharedDataPrimary, (void *)&WRITABLE_DATA, sizeof(int));
}
if (nullptr != sharedDataMinor) {
int readableData[2] = {0};
IMakeSharedData(sharedDataMinor, sizeof(int) * 2, sizeof(int));
StatusCode code = IGetReadableData(sharedDataMinor, readableData, sizeof(readableData));
PrintStringCode(code);
LogInfo("readableData = %d\n", readableData);
EXPECT_NE(code.mStatusCode, STATUS_CODE_OK);
IShareDataFree(sharedDataMinor);
}
if (nullptr != sharedDataPrimary) {
IShareDataFree(sharedDataPrimary);
}
ILogUnInit();
}
// ../output_files/test/bin/SharedDataTest --gtest_filter=SharedDataTest.UNIT_SharedData_DEME_Demo11
TEST(SharedDataTest, UNIT_SharedData_DEME_Demo11)
{
constexpr int WRITABLE_DATA = 189;
CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END);
constexpr int PROJECT_ID = 9;
void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID);
void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID);
IMakeSharedData(sharedDataMinor, sizeof(int), sizeof(int));
if (nullptr != sharedDataPrimary) {
IMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int));
ISetWritableData(sharedDataPrimary, (void *)&WRITABLE_DATA, sizeof(int));
}
if (nullptr != sharedDataMinor) {
int readableData = 0;
StatusCode code = IGetReadableData(sharedDataMinor, &readableData, sizeof(int));
PrintStringCode(code);
LogInfo("readableData = %d\n", readableData);
EXPECT_EQ(code.mStatusCode, STATUS_CODE_OK);
EXPECT_EQ(readableData, 189);
IShareDataFree(sharedDataMinor);
}
if (nullptr != sharedDataPrimary) {
IShareDataFree(sharedDataPrimary);
}
ILogUnInit();
}
} // namespace SharedDataTest

View File

@ -0,0 +1,70 @@
# include(${CMAKE_SOURCE_DIR}/build/independent_source.cmake)
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
set(EXECUTABLE_OUTPUT_PATH ${TEST_OUTPUT_PATH}/bin)
include_directories(
./tool/include
${UTILS_SOURCE_PATH}/StatusCode/include
${UTILS_SOURCE_PATH}/Log/include
${UTILS_SOURCE_PATH}/TcpModule/include
${TEST_SOURCE_PATH}/utils/LinuxApiMock/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include
)
link_directories(
${LIBS_OUTPUT_PATH}
${EXTERNAL_LIBS_OUTPUT_PATH}
)
aux_source_directory(. SRC_FILES)
aux_source_directory(./src SRC_FILES)
if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX})
aux_source_directory(./src_mock SRC_FILES)
endif()
set(TARGET_NAME TcpModuleTest)
add_executable(${TARGET_NAME} ${SRC_FILES})
target_link_libraries(${TARGET_NAME} TcpModule gtest gmock pthread Log)
if(${TEST_COVERAGE} MATCHES "true")
target_link_libraries(${TARGET_NAME} gcov)
endif()
if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX})
target_link_libraries(${TARGET_NAME} LinuxApiMock)
endif()
if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true")
add_custom_target(
TcpModuleTest_code_check
COMMAND ${CLANG_TIDY_EXE}
-checks='${CLANG_TIDY_CHECKS}'
--header-filter=.*
--system-headers=false
${SRC_FILES}
${CLANG_TIDY_CONFIG}
# --line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/gtest.h\"}]'
--line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/*.h\"}]'
-p ${PLATFORM_PATH}/cmake-shell
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/utils/TcpModule
)
file(GLOB_RECURSE HEADER_FILES *.h)
add_custom_target(
TcpModuleTest_code_format
COMMAND ${CLANG_FORMAT_EXE}
-style=file
-i ${SRC_FILES} ${HEADER_FILES}
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/utils/TcpModule
)
add_custom_command(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make TcpModuleTest_code_check
COMMAND make TcpModuleTest_code_format
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
endif()
define_file_name(${TARGET_NAME})
# if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tool/CMakeLists.txt")
# add_subdirectory(tool)
# endif()

View File

@ -0,0 +1,23 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
#include <unistd.h>
int main(int argc, char *argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@ -0,0 +1,74 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 "ILog.h"
#include "TcpModule.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
namespace TcpModuleTest
{
// ../output_files/test/bin/TcpModuleTest --gtest_filter=TcpModuleTest.UNIT_TcpModule_EXAMPLE_AUTO_IllegalObject
/**
* TcpModule module api will not crash when object is illegal.
*/
TEST(TcpModuleTest, UNIT_TcpModule_EXAMPLE_AUTO_IllegalObject)
{
TcpServerParam tcpServerparam = {
.mIp = "127.0.0.1",
.mPort = 9876,
.mAcceptClientFunc = [](void *object, const char *ip) -> bool {
LogInfo("accept client, peer ip: %s\n", ip);
if (nullptr != object) {
AcceptClientWrite(object, "client accept send data.", strlen("client accept send data."));
}
return true;
},
.mClientAcceptParam = {
.mReadFunc = [](const void *data, const ssize_t len, const void *object) -> void {
LogInfo("client accept read data: %s\n", (char *)data);
},
.mClosedFunc = [](const void *object) -> void {
LogInfo("client accept closed.\n");
},
},
};
TcpClientParam param = {
.mIp = "127.0.0.1",
.mPort = 9876,
.mReadFunc = [](const void *data, const ssize_t len, const void *context) -> void {
LogInfo("read data: %s", (char *)data);
return;
},
.mClosedFunc = [](const void *object) -> void {
LogInfo("tcp client closed.\n");
},
};
CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END);
void *tcpServer = CreateTcpServer(tcpServerparam);
std::this_thread::sleep_for(std::chrono::milliseconds(500));
void *tcpClient = CreateTcpClient(param);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
TcpClientWrite(tcpClient, "client send data.", strlen("client send data."));
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
if (nullptr != tcpClient) {
FreeTcpClient(tcpClient);
}
if (nullptr != tcpServer) {
FreeTcpServer(tcpServer);
}
ILogUnInit();
}
} // namespace TcpModuleTest

View File

@ -0,0 +1,53 @@
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
set(EXECUTABLE_OUTPUT_PATH ${EXEC_OUTPUT_PATH})
set(LIBRARY_OUTPUT_PATH ${LIBS_OUTPUT_PATH})
include_directories(
./src
./include
${APPLICATION_SOURCE_PATH}/HuntingCamera/src
${UTILS_SOURCE_PATH}/Log/include
${UTILS_SOURCE_PATH}/StatusCode/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include
)
# link_directories(
# ${EXTERNAL_SOURCE_PATH}/libconfig/libconfig-1.7.3/lib/.libs
# )
aux_source_directory(./src SRC_FILES)
set(TARGET_NAME TestManager)
add_library(${TARGET_NAME} STATIC ${SRC_FILES})
target_link_libraries(${TARGET_NAME} LinuxApi gtest gmock Log)
if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true")
add_custom_target(
TestManager_code_check
COMMAND ${CLANG_TIDY_EXE}
-checks='${CLANG_TIDY_CHECKS}'
--header-filter=.*
--system-headers=false
${SRC_FILES}
${CLANG_TIDY_CONFIG}
-p ${PLATFORM_PATH}/cmake-shell
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/utils/TestManager
)
file(GLOB_RECURSE HEADER_FILES *.h)
add_custom_target(
TestManager_code_format
COMMAND ${CLANG_FORMAT_EXE}
-style=file
-i ${SRC_FILES} ${HEADER_FILES}
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/utils/TestManager
)
add_custom_command(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make TestManager_code_check
COMMAND make TestManager_code_format
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
endif()
define_file_name(${TARGET_NAME})

View File

@ -0,0 +1,38 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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.
*/
#ifndef TEST_MANAGER_H
#define TEST_MANAGER_H
#include <mutex>
#include <thread>
class TestManager
{
public:
TestManager() = default;
virtual ~TestManager() = default;
void Init(void);
void UnInit(void);
void ResetTimeOut(const unsigned int &timeOutMs);
void StartTimer(void);
void StopTimer(void);
void Timer(void);
private:
std::mutex mMutex;
unsigned int mTimeOutMs;
unsigned int mSleepingTime;
bool mRuning;
std::thread mTimerThread;
};
#endif

View File

@ -0,0 +1,68 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 "TestManager.h"
#include "ILog.h"
#include "MainThread.h"
constexpr int TIMER_SLEEP_MS = 100;
void TestManager::Init(void)
{
mSleepingTime = 0;
mTimeOutMs = 0;
mRuning = false;
}
void TestManager::UnInit(void)
{
StopTimer();
if (mTimerThread.joinable()) {
mTimerThread.join();
}
}
void TestManager::ResetTimeOut(const unsigned int &timeOutMs)
{
//
mTimeOutMs = timeOutMs;
StartTimer();
}
void TestManager::StartTimer(void)
{
std::lock_guard<std::mutex> locker(mMutex);
if (mTimerThread.joinable()) {
LogInfo("Timer has started.\n");
return;
}
auto timerThread = [](TestManager *timer) {
//
timer->Timer();
};
mTimerThread = std::thread(timerThread, this);
}
void TestManager::StopTimer(void)
{
//
mRuning = false;
}
void TestManager::Timer(void)
{
mRuning = true;
while (mRuning) {
std::this_thread::sleep_for(std::chrono::milliseconds(TIMER_SLEEP_MS));
mSleepingTime += TIMER_SLEEP_MS;
if (mSleepingTime >= mTimeOutMs) {
LogInfo("Test time out.\n");
mRuning = false;
MainThread::GetInstance()->Exit();
}
}
}

View File

@ -0,0 +1,72 @@
# include(${CMAKE_SOURCE_DIR}/build/independent_source.cmake)
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
set(EXECUTABLE_OUTPUT_PATH ${TEST_OUTPUT_PATH}/bin)
include_directories(
./tool/include
${UTILS_SOURCE_PATH}/StatusCode/include
${UTILS_SOURCE_PATH}/Log/include
${UTILS_SOURCE_PATH}/UartDevice/include
${TEST_SOURCE_PATH}
${TEST_SOURCE_PATH}/utils/LinuxApiMock/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include
)
link_directories(
${LIBS_OUTPUT_PATH}
${EXTERNAL_LIBS_OUTPUT_PATH}
${TEST_TOOLS_OUTPUT_PATH}
)
aux_source_directory(. SRC_FILES)
aux_source_directory(./src SRC_FILES)
if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX})
aux_source_directory(./src_mock SRC_FILES)
endif()
set(TARGET_NAME UartDeviceTest)
add_executable(${TARGET_NAME} ${SRC_FILES})
target_link_libraries(${TARGET_NAME} UartDevice UartDeviceTestTool gtest gmock pthread Log)
if(${TEST_COVERAGE} MATCHES "true")
target_link_libraries(${TARGET_NAME} gcov)
endif()
if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX})
target_link_libraries(${TARGET_NAME} LinuxApiMock)
endif()
if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true")
add_custom_target(
UartDeviceTest_code_check
COMMAND ${CLANG_TIDY_EXE}
-checks='${CLANG_TIDY_CHECKS}'
--header-filter=.*
--system-headers=false
${SRC_FILES}
${CLANG_TIDY_CONFIG}
# --line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/gtest.h\"}]'
--line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/*.h\"}]'
-p ${PLATFORM_PATH}/cmake-shell
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/utils/UartDevice
)
file(GLOB_RECURSE HEADER_FILES *.h)
add_custom_target(
UartDeviceTest_code_format
COMMAND ${CLANG_FORMAT_EXE}
-style=file
-i ${SRC_FILES} ${HEADER_FILES}
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/utils/UartDevice
)
add_custom_command(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make UartDeviceTest_code_check
COMMAND make UartDeviceTest_code_format
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
endif()
define_file_name(${TARGET_NAME})
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tool/CMakeLists.txt")
add_subdirectory(tool)
endif()

View File

@ -0,0 +1,23 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
#include <unistd.h>
int main(int argc, char *argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@ -0,0 +1,61 @@
/*
* Copyright (c) 2023 Fancy Code.
* 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 "ILog.h"
#include "UartDevice.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
namespace UartDeviceTest
{
// ../output_files/test/bin/UartDeviceTest --gtest_filter=UartDeviceTest.UNIT_UartDevice_AUTO_IllegalObject
/**
* UartDevice module api will not crash when object is illegal.
*/
TEST(UartDeviceTest, UNIT_UartDevice_AUTO_IllegalObject)
{
CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END);
IUartOpen(nullptr);
IUartSend(nullptr, nullptr, 0);
IUartRecv(nullptr, nullptr, 0, 0);
IUartTcflush(nullptr);
IUartDeviceFree(nullptr);
char illegalObject[100] = {0};
void *object = &illegalObject[10];
IUartOpen(object);
IUartSend(object, nullptr, 0);
IUartRecv(object, nullptr, 0, 0);
IUartTcflush(object);
IUartDeviceFree(object);
ILogUnInit();
}
// ../output_files/test/bin/UartDeviceTest --gtest_filter=UartDeviceTest.UNIT_UartDevice_EXAMPLE_Demo
TEST(UartDeviceTest, UNIT_UartDevice_EXAMPLE_Demo)
{
CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END);
UartInfo device = {
"dev/s1",
};
void *object = CreateUartDevice(device);
IUartOpen(object);
// IUartSend(object, nullptr, 0);
// IUartRecv(object, nullptr, 0, 0);
// IUartTcflush(object);
IUartDeviceFree(object);
ILogUnInit();
}
} // namespace UartDeviceTest

Some files were not shown because too many files have changed in this diff Show More