From 2d9e3711b1f84635575185b4c7923b52fdb02445 Mon Sep 17 00:00:00 2001 From: Fancy code <258828110.@qq.com> Date: Wed, 29 May 2024 16:28:53 +0800 Subject: [PATCH] Fixed:Key control mock bug. --- application/MissionManager/CMakeLists.txt | 3 +- application/MissionManager/src/TopState.cpp | 6 +++- .../src_mock/DeviceManager_Mock_Test.cpp | 30 +++++++++++++++-- test/hal/tool/src/HalTestTool.cpp | 1 + test/hal/tool/src/KeyControlMock.cpp | 32 +++++++++++++++---- test/hal/tool/src/KeyControlMock.h | 6 +++- 6 files changed, 67 insertions(+), 11 deletions(-) diff --git a/application/MissionManager/CMakeLists.txt b/application/MissionManager/CMakeLists.txt index 28b2a04..b566318 100644 --- a/application/MissionManager/CMakeLists.txt +++ b/application/MissionManager/CMakeLists.txt @@ -9,6 +9,7 @@ include_directories( ./include ${UTILS_SOURCE_PATH}/StatusCode/include ${UTILS_SOURCE_PATH}/Log/include + ${UTILS_SOURCE_PATH}/KeyControl/include ${MIDDLEWARE_SOURCE_PATH}/StateMachine/include ${MIDDLEWARE_SOURCE_PATH}/AppManager/include ${MIDDLEWARE_SOURCE_PATH}/MediaManager/include @@ -28,7 +29,7 @@ aux_source_directory(./src SRC_FILES) set(TARGET_NAME MissionManager) add_library(${TARGET_NAME} STATIC ${SRC_FILES}) -target_link_libraries(${TARGET_NAME} McuAskBase StateMachine MediaManager StorageManager DeviceManager HuntingUpgrade StatusCode Log) +target_link_libraries(${TARGET_NAME} McuAskBase StateMachine MediaManager StorageManager DeviceManager HuntingUpgrade KeyControl StatusCode Log) if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true") add_custom_target( diff --git a/application/MissionManager/src/TopState.cpp b/application/MissionManager/src/TopState.cpp index f0fb11e..ab1e7bc 100644 --- a/application/MissionManager/src/TopState.cpp +++ b/application/MissionManager/src/TopState.cpp @@ -15,6 +15,7 @@ #include "TopState.h" #include "ILog.h" #include "IMediaManager.h" +#include "KeyControl.h" #include "MissionStateMachine.h" TopState::TopState() : State("TopState") { @@ -50,7 +51,10 @@ StatusCode TopState::ReportEvent(const MediaReportEvent &event) } void TopState::KeyEventReport(const std::string &keyName, const VirtualKeyEvent &event, const unsigned int &timeMs) { - LogInfo(" KeyEventReport:key name = %s, time = %d\n", keyName.c_str(), timeMs); + LogInfo(" KeyEventReport:key name = %s, key event = %s, time = %d\n", + keyName.c_str(), + PrintKeyEvent(static_cast(event)), + timeMs); } bool TopState::StorageStartInitHandle(VStateMachineData *msg) { diff --git a/test/application/HuntingCamera/src_mock/DeviceManager_Mock_Test.cpp b/test/application/HuntingCamera/src_mock/DeviceManager_Mock_Test.cpp index c9cd013..b7efebc 100644 --- a/test/application/HuntingCamera/src_mock/DeviceManager_Mock_Test.cpp +++ b/test/application/HuntingCamera/src_mock/DeviceManager_Mock_Test.cpp @@ -27,9 +27,9 @@ namespace DeviceManager_Mock_Test /** * @brief Construct a new test f object * ../output_files/test/bin/HuntingCameraTest - * --gtest_filter=HuntingCameraTest.HS_INTEGRATION_HunttingCamera_EXAMPLE_KeyControl + * --gtest_filter=HuntingCameraTest.HS_INTEGRATION_HunttingCamera_EXAMPLE_KeyControlHold */ -TEST_F(HuntingCameraTest, HS_INTEGRATION_HunttingCamera_EXAMPLE_KeyControl) +TEST_F(HuntingCameraTest, HS_INTEGRATION_HunttingCamera_EXAMPLE_KeyControlHold) { MainThread::GetInstance()->Init(); TestManager::ResetTimeOut(1000 * 3); @@ -37,4 +37,30 @@ TEST_F(HuntingCameraTest, HS_INTEGRATION_HunttingCamera_EXAMPLE_KeyControl) 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) +{ + 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(); +} } // namespace DeviceManager_Mock_Test \ No newline at end of file diff --git a/test/hal/tool/src/HalTestTool.cpp b/test/hal/tool/src/HalTestTool.cpp index 6634a1d..dd6b320 100644 --- a/test/hal/tool/src/HalTestTool.cpp +++ b/test/hal/tool/src/HalTestTool.cpp @@ -350,6 +350,7 @@ void HalTestTool::MockSdCardInsert(std::shared_ptr &mock) std::shared_ptr HalTestTool::MakeKeyHalTest(const std::string &keyName) { std::shared_ptr key = std::make_shared(keyName); + std::dynamic_pointer_cast(key)->InitKeyControlMock(); return key; } std::shared_ptr HalTestTool::MakeLedHalTest(const std::string &ledName) diff --git a/test/hal/tool/src/KeyControlMock.cpp b/test/hal/tool/src/KeyControlMock.cpp index fa7c18b..6bb42aa 100644 --- a/test/hal/tool/src/KeyControlMock.cpp +++ b/test/hal/tool/src/KeyControlMock.cpp @@ -25,13 +25,24 @@ void KeyControlTest::SetKeyMonitor(std::shared_ptr &monitor) { mMonitor = monitor; } -void KeyControlTest::CheckKeyStatus(void) +void KeyControlMock::CheckKeyStatus(void) { - TimerKeyEventTrigger(KeyHalEvent::PRESSING); + KeyHalEvent pinValue = mKeyStatus; + // LogInfo("KeyContrl::CheckKeyValue pin = %d, pinValue = %d\n", mGpioPin, pinValue); + if (pinValue == KeyHalEvent::NOT_PRESSING && KeyHalEvent::NOT_PRESSING != mLastKeyStatus) { + KeyControl::KeyHalEventTrigger(KeyHalEvent::NOT_PRESSING); + } + else if (pinValue != KeyHalEvent::NOT_PRESSING && KeyHalEvent::NOT_PRESSING == mLastKeyStatus) { + KeyControl::KeyHalEventTrigger(KeyHalEvent::PRESSING); + } + else { + KeyControl::TimerKeyEventTrigger(KeyHalEvent::PRESSING); + } + mLastKeyStatus = pinValue; } void KeyControlTest::KeyEventTrigger(const std::string &keyName, const KeyEvent &event, const unsigned int &timeMs) { - LogInfo("KeyEventTrigger keyName = %s, event = %s, time = %u\n", keyName.c_str(), PrintKeyEvent(event), timeMs); + // LogInfo("KeyEventTrigger keyName = %s, event = %s, time = %u\n", keyName.c_str(), PrintKeyEvent(event), timeMs); KeyEventTriggerTrace(keyName, event, timeMs); auto monitor = mMonitor.lock(); if (mMonitor.expired()) { @@ -49,9 +60,16 @@ StatusCode KeyControlTest::KeyEventTriggerTrace(const std::string &keyName, cons { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); } -KeyControlMock::KeyControlMock(const std::string &keyName) : KeyControlTest(keyName) +KeyControlMock::KeyControlMock(const std::string &keyName) + : KeyControlTest(keyName), mKeyStatus(KeyHalEvent::NOT_PRESSING), mLastKeyStatus(KeyHalEvent::NOT_PRESSING) { } +void KeyControlMock::InitKeyControlMock(void) +{ + std::shared_ptr mock = std::dynamic_pointer_cast(KeyControl::shared_from_this()); + EXPECT_CALL(*mock.get(), KeyEventTriggerTrace(mock->GetKeyName(), _, _)) + .WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION)))); +} void KeyControlMock::SetKeyEvent(const KeyHalEvent &event) { KeyHalEventTrigger(event); @@ -61,9 +79,11 @@ bool KeyControlMock::MockKeyClick(const unsigned int &pressingTimeMs) if (mMutex.try_lock()) { auto keyClickThread = [=](std::shared_ptr key) { KeyEventHappendOnce(key, pressingTimeMs); - key->KeyHalEventTrigger(KeyHalEvent::PRESSING); + // key->KeyHalEventTrigger(KeyHalEvent::PRESSING); + mKeyStatus = KeyHalEvent::PRESSING; std::this_thread::sleep_for(std::chrono::milliseconds(pressingTimeMs)); - key->KeyHalEventTrigger(KeyHalEvent::NOT_PRESSING); + // key->KeyHalEventTrigger(KeyHalEvent::NOT_PRESSING); + mKeyStatus = KeyHalEvent::NOT_PRESSING; mMutex.unlock(); }; std::shared_ptr tmp = KeyControl::shared_from_this(); diff --git a/test/hal/tool/src/KeyControlMock.h b/test/hal/tool/src/KeyControlMock.h index 3180011..6d53c24 100644 --- a/test/hal/tool/src/KeyControlMock.h +++ b/test/hal/tool/src/KeyControlMock.h @@ -25,7 +25,7 @@ public: virtual ~KeyControlTest() = default; unsigned int GetStatusCheckPeriodMs(void) override; void SetKeyMonitor(std::shared_ptr &monitor) override; - void CheckKeyStatus(void) override; + // void CheckKeyStatus(void) override; void KeyEventTrigger(const std::string &keyName, const KeyEvent &event, const unsigned int &timeMs) override; const std::string GetKeyName(void) override; @@ -42,6 +42,8 @@ class KeyControlMock : public KeyControlTest public: KeyControlMock(const std::string &keyName); virtual ~KeyControlMock() = default; + void CheckKeyStatus(void) override; + void InitKeyControlMock(void); void SetKeyEvent(const KeyHalEvent &event); bool MockKeyClick(const unsigned int &pressingTimeMs = 200); MOCK_METHOD3(KeyEventTriggerTrace, StatusCode(const std::string &, const KeyEvent &, const unsigned int &)); @@ -51,5 +53,7 @@ private: private: std::mutex mMutex; + KeyHalEvent mKeyStatus; + KeyHalEvent mLastKeyStatus; }; #endif \ No newline at end of file