From 50be3bf21bc23aced8b1fada228755aa5e668251 Mon Sep 17 00:00:00 2001 From: Fancy code <258828110.@qq.com> Date: Mon, 4 Mar 2024 17:39:19 -0800 Subject: [PATCH] Backup:test code. --- application/HunttingCamera/src/MainThread.cpp | 8 +- .../MissionManager/include/IMissionManager.h | 5 +- .../MissionManager/src/IMissionManager.cpp | 4 +- test/CMakeLists.txt | 4 +- test/application/CMakeLists.txt | 2 + .../application/HunttingCamera/CMakeLists.txt | 75 +++++++++++++++++++ test/application/HunttingCamera/mainTest.cpp | 23 ++++++ .../src/HunttingCamera_Test.cpp | 59 +++++++++++++++ test/utils/CMakeLists.txt | 1 + test/utils/TestManager/CMakeLists.txt | 53 +++++++++++++ test/utils/TestManager/include/TestManager.h | 38 ++++++++++ test/utils/TestManager/src/TestManager.cpp | 68 +++++++++++++++++ 12 files changed, 332 insertions(+), 8 deletions(-) create mode 100644 test/application/CMakeLists.txt create mode 100644 test/application/HunttingCamera/CMakeLists.txt create mode 100644 test/application/HunttingCamera/mainTest.cpp create mode 100644 test/application/HunttingCamera/src/HunttingCamera_Test.cpp create mode 100644 test/utils/TestManager/CMakeLists.txt create mode 100644 test/utils/TestManager/include/TestManager.h create mode 100644 test/utils/TestManager/src/TestManager.cpp diff --git a/application/HunttingCamera/src/MainThread.cpp b/application/HunttingCamera/src/MainThread.cpp index 8e838d6..a0aa26d 100644 --- a/application/HunttingCamera/src/MainThread.cpp +++ b/application/HunttingCamera/src/MainThread.cpp @@ -52,13 +52,15 @@ StatusCode MainThread::UnInit(void) } StatusCode MainThread::CreateAllModules(void) { - // CreateLogModule(); - // CreateHalModule(); CreateHalCppModule(); CreateMissionManagerModule(); return CreateStatusCode(STATUS_CODE_OK); } -void MainThread::DestoryAllModules(void) {} +void MainThread::DestoryAllModules(void) +{ + DestroyMissionManagerModule(); + DestroyHalCppModule(); +} void MainThread::ResetAllPtrMaker(void) {} void MainThread::Runing(void) { diff --git a/application/MissionManager/include/IMissionManager.h b/application/MissionManager/include/IMissionManager.h index 1e28b62..a4e082a 100644 --- a/application/MissionManager/include/IMissionManager.h +++ b/application/MissionManager/include/IMissionManager.h @@ -18,6 +18,7 @@ #include #include bool CreateMissionManagerModule(void); +bool DestroyMissionManagerModule(void); enum class MissionEvent { TEST = 0, @@ -54,7 +55,7 @@ public: IMissionManager() = default; virtual ~IMissionManager() = default; static std::shared_ptr &GetInstance(std::shared_ptr *impl = nullptr); - virtual const StatusCode Init(void) { return CreateStatusCode(STATUS_CODE_OK); } - virtual const StatusCode UnInit(void) { return CreateStatusCode(STATUS_CODE_OK); } + virtual const StatusCode Init(void); + virtual const StatusCode UnInit(void); }; #endif \ No newline at end of file diff --git a/application/MissionManager/src/IMissionManager.cpp b/application/MissionManager/src/IMissionManager.cpp index f253fb1..5bc83ca 100644 --- a/application/MissionManager/src/IMissionManager.cpp +++ b/application/MissionManager/src/IMissionManager.cpp @@ -27,4 +27,6 @@ std::shared_ptr &IMissionManager::GetInstance(std::shared_ptr +#include +#include +#include +int main(int argc, char *argv[]) +{ + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file diff --git a/test/application/HunttingCamera/src/HunttingCamera_Test.cpp b/test/application/HunttingCamera/src/HunttingCamera_Test.cpp new file mode 100644 index 0000000..39e5875 --- /dev/null +++ b/test/application/HunttingCamera/src/HunttingCamera_Test.cpp @@ -0,0 +1,59 @@ +/* + * 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 "TestManager.h" +#include +namespace HunttingCameraTest +{ +class HunttingCameraTest : public testing::Test, public TestManager +{ +public: + HunttingCameraTest() {} + virtual ~HunttingCameraTest() {} + static void SetUpTestCase() + { + CreateLogModule(); + ILogInit(LOG_INSTANCE_TYPE_END); + } + static void TearDownTestCase() { ILogUnInit(); } + virtual void SetUp() + { + // CreateAllKeysMcok(); + // HalTestTool::Init(); + // HunttingCameraTestTool::Init(); + // CreateHalCppModule(); + // CreateDeviceManagerModule(); + TestManager::Init(); + } + virtual void TearDown() + { + // HalTestTool::UnInit(); + // HunttingCameraTestTool::UnInit(); + // DestroyDeviceManagerModule(); + // DestroyAllKeysMock(); + TestManager::UnInit(); + } +}; +// ../output_files/test/bin/HunttingCameraTest --gtest_filter=HunttingCameraTest.INTEGRATION_DeviceManager_EXAMPLE_Demo +TEST_F(HunttingCameraTest, INTEGRATION_DeviceManager_EXAMPLE_Demo) +{ + MainThread::GetInstance()->Init(); + ResetTimeOut(1000 * 3); + MainThread::GetInstance()->Runing(); + MainThread::GetInstance()->UnInit(); +} +} // namespace HunttingCameraTest \ No newline at end of file diff --git a/test/utils/CMakeLists.txt b/test/utils/CMakeLists.txt index 2d9c2bb..d79f2dd 100644 --- a/test/utils/CMakeLists.txt +++ b/test/utils/CMakeLists.txt @@ -7,5 +7,6 @@ add_subdirectory(UartDevice) add_subdirectory(LinuxApiMock) add_subdirectory(McuProtocol) add_subdirectory(FxHttpServer) +add_subdirectory(TestManager) diff --git a/test/utils/TestManager/CMakeLists.txt b/test/utils/TestManager/CMakeLists.txt new file mode 100644 index 0000000..6c7331c --- /dev/null +++ b/test/utils/TestManager/CMakeLists.txt @@ -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}/HunttingCamera/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 ("${CLANG_TIDY_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}) \ No newline at end of file diff --git a/test/utils/TestManager/include/TestManager.h b/test/utils/TestManager/include/TestManager.h new file mode 100644 index 0000000..c1706c2 --- /dev/null +++ b/test/utils/TestManager/include/TestManager.h @@ -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 +#include +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 \ No newline at end of file diff --git a/test/utils/TestManager/src/TestManager.cpp b/test/utils/TestManager/src/TestManager.cpp new file mode 100644 index 0000000..819d153 --- /dev/null +++ b/test/utils/TestManager/src/TestManager.cpp @@ -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 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(); + } + } +} \ No newline at end of file