From 4efd61070745f639883cc007790288a6c3abb6ab Mon Sep 17 00:00:00 2001 From: Fancy code <258828110.@qq.com> Date: Sun, 18 Feb 2024 02:36:00 -0800 Subject: [PATCH] Improve:DeviceManager test tool. --- .../DeviceManager/include/IDeviceManager.h | 2 +- middleware/DeviceManager/src/KeyManager.cpp | 1 + test/hal/tool/include/HalTestTool.h | 3 ++ test/hal/tool/src/HalTestTool.cpp | 1 + .../DeviceManager/src/DeviceManager_Test.cpp | 2 +- .../DeviceManager/tool/CMakeLists.txt | 2 +- .../tool/include/DeviceManagerTestTool.h | 8 ++++- .../tool/src/DeviceManagerTestTool.cpp | 31 +++++++++++++++++++ 8 files changed, 46 insertions(+), 4 deletions(-) diff --git a/middleware/DeviceManager/include/IDeviceManager.h b/middleware/DeviceManager/include/IDeviceManager.h index 0690fcb1..7b8bffd0 100644 --- a/middleware/DeviceManager/include/IDeviceManager.h +++ b/middleware/DeviceManager/include/IDeviceManager.h @@ -36,7 +36,7 @@ class VKeyMonitor public: VKeyMonitor() = default; virtual ~VKeyMonitor() = default; - void KeyEventReport(const std::string &keyName, const VirtualKeyEvent &event, const unsigned int &timeMs) {} + virtual void KeyEventReport(const std::string &keyName, const VirtualKeyEvent &event, const unsigned int &timeMs) {} }; class IDeviceManager { diff --git a/middleware/DeviceManager/src/KeyManager.cpp b/middleware/DeviceManager/src/KeyManager.cpp index d3f6b2de..b56513ac 100644 --- a/middleware/DeviceManager/src/KeyManager.cpp +++ b/middleware/DeviceManager/src/KeyManager.cpp @@ -108,5 +108,6 @@ void KeyManager::KeyEventHappened(const std::string &keyName, const VirtualKeyEv LogError("monitor is nullptr.\n"); return; } + LogInfo("KeyManager::KeyEventHappened: key = %s, event = %d, time = %u\n", keyName.c_str(), event, timeMs); monitor->KeyEventReport(keyName, static_cast(event), timeMs); } \ No newline at end of file diff --git a/test/hal/tool/include/HalTestTool.h b/test/hal/tool/include/HalTestTool.h index c3466cda..1e482ffe 100644 --- a/test/hal/tool/include/HalTestTool.h +++ b/test/hal/tool/include/HalTestTool.h @@ -117,6 +117,9 @@ public: void SetKeyEvent(const std::string keyName, const KeyHalEvent &event); void SetKeyClick(const std::string &keyName, const unsigned int &pressingTimeMs = 200); +protected: + virtual void DeviceManagerNotice(const std::string &keyName, const unsigned int &pressingTimeMs) {} + private: void KeyEventHappendOnce(std::shared_ptr &mock, const unsigned int &pressingTimeMs); diff --git a/test/hal/tool/src/HalTestTool.cpp b/test/hal/tool/src/HalTestTool.cpp index e24ea873..9f9e933c 100644 --- a/test/hal/tool/src/HalTestTool.cpp +++ b/test/hal/tool/src/HalTestTool.cpp @@ -151,6 +151,7 @@ void HalTestTool::SetKeyClick(const std::string &keyName, const unsigned int &pr } if (key->SetKeyClick(pressingTimeMs)) { // KeyEventHappendOnce(key, pressingTimeMs); + DeviceManagerNotice(keyName, pressingTimeMs); } } void HalTestTool::KeyEventHappendOnce(std::shared_ptr &mock, const unsigned int &pressingTimeMs) diff --git a/test/middleware/DeviceManager/src/DeviceManager_Test.cpp b/test/middleware/DeviceManager/src/DeviceManager_Test.cpp index 12dd21c5..724d8313 100644 --- a/test/middleware/DeviceManager/src/DeviceManager_Test.cpp +++ b/test/middleware/DeviceManager/src/DeviceManager_Test.cpp @@ -22,7 +22,7 @@ namespace DeviceManagerTest { const char *KEY_TEST = "TEST"; -class DeviceManagerTest : public testing::Test, public HalTestTool, public DeviceManagerTestTool +class DeviceManagerTest : public testing::Test, virtual public HalTestTool, public DeviceManagerTestTool { public: DeviceManagerTest() {} diff --git a/test/middleware/DeviceManager/tool/CMakeLists.txt b/test/middleware/DeviceManager/tool/CMakeLists.txt index cab43043..53cbb1b7 100644 --- a/test/middleware/DeviceManager/tool/CMakeLists.txt +++ b/test/middleware/DeviceManager/tool/CMakeLists.txt @@ -7,7 +7,7 @@ include_directories( ./include ${UTILS_SOURCE_PATH}/StatusCode/include ${UTILS_SOURCE_PATH}/Log/include - ${UTILS_SOURCE_PATH}/McuProtocol/include + # ${UTILS_SOURCE_PATH}/McuProtocol/include ${MIDDLEWARE_SOURCE_PATH}/DeviceManager/src ${TEST_SOURCE_PATH}/utils/LinuxApiMock/include ${TEST_SOURCE_PATH}/utils/McuProtocol/tool/include diff --git a/test/middleware/DeviceManager/tool/include/DeviceManagerTestTool.h b/test/middleware/DeviceManager/tool/include/DeviceManagerTestTool.h index 912d8f91..71916d30 100644 --- a/test/middleware/DeviceManager/tool/include/DeviceManagerTestTool.h +++ b/test/middleware/DeviceManager/tool/include/DeviceManagerTestTool.h @@ -15,6 +15,7 @@ #ifndef DEVICE_MANAGER_TEST_TOOL_H #define DEVICE_MANAGER_TEST_TOOL_H #include "DeviceManager.h" +#include "HalTestTool.h" #include #include using ::testing::_; @@ -73,7 +74,7 @@ public: virtual ~KeyMonitorMock() = default; MOCK_METHOD3(KeyEventReport, void(const std::string &, const VirtualKeyEvent &, const unsigned int &)); }; -class DeviceManagerTestTool +class DeviceManagerTestTool : virtual public HalTestTool { public: DeviceManagerTestTool() = default; @@ -81,8 +82,13 @@ public: void Init(void); void UnInit(void); +protected: + void DeviceManagerNotice(const std::string &keyName, const unsigned int &pressingTimeMs) override; + private: void DeviceManagerMockInit(std::shared_ptr &mock); + void KeyMonitorInit(std::shared_ptr &mock, const std::string &keyName, + const unsigned int &pressingTimeMs); private: std::shared_ptr mDeviceManagerMock; diff --git a/test/middleware/DeviceManager/tool/src/DeviceManagerTestTool.cpp b/test/middleware/DeviceManager/tool/src/DeviceManagerTestTool.cpp index e13f05d5..30d81dd9 100644 --- a/test/middleware/DeviceManager/tool/src/DeviceManagerTestTool.cpp +++ b/test/middleware/DeviceManager/tool/src/DeviceManagerTestTool.cpp @@ -36,6 +36,16 @@ void DeviceManagerTestTool::UnInit(void) mKeyMonitorMock.reset(); CancelOverrideDeviceMakePtrObject(); } +void DeviceManagerTestTool::DeviceManagerNotice(const std::string &keyName, const unsigned int &pressingTimeMs) +{ + LogInfo("DeviceManagerTestTool::DeviceManagerNotice\n"); + if (mKeyMonitorMock) { + KeyMonitorInit(mKeyMonitorMock, keyName, pressingTimeMs); + } + else { + LogWarning("mKeyMonitorMock is nullptr.\n"); + } +} void DeviceManagerTestTool::DeviceManagerMockInit(std::shared_ptr &mock) { auto getKeyMonitor = [=](std::shared_ptr &monitor) { @@ -47,4 +57,25 @@ void DeviceManagerTestTool::DeviceManagerMockInit(std::shared_ptr(Invoke(getKeyMonitor)), Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION)))); // .WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION)))); +} +void DeviceManagerTestTool::KeyMonitorInit(std::shared_ptr &mock, const std::string &keyName, + const unsigned int &pressingTimeMs) +{ + // EXPECT_CALL(*mock.get(), KeyEventReport(_, _, _)).Times(100).WillRepeatedly(DoAll(Return())); + EXPECT_CALL(*mock.get(), KeyEventReport(_, _, _)).WillRepeatedly(DoAll(Return())); + constexpr int CLICK_EVENT_HAPPENED_ONLY_ONCE = 1; + constexpr int HOLD_UP_EVENT_HAPPENED_ONLY_ONCE = 1; + if (KEY_ACTION_SHORT_CLICK <= pressingTimeMs && pressingTimeMs < KEY_ACTION_HOLD_DWON) { + EXPECT_CALL(*mock.get(), KeyEventReport(keyName, static_cast(KeyEvent::SHORT_CLICK), _)) + .Times(CLICK_EVENT_HAPPENED_ONLY_ONCE) + .WillRepeatedly(DoAll(Return())); + } + if (KEY_ACTION_HOLD_DWON <= pressingTimeMs) { + EXPECT_CALL(*mock.get(), KeyEventReport(keyName, static_cast(KeyEvent::HOLD_DOWN), _)) + .Times(AtLeast(1)) + .WillRepeatedly(DoAll(Return())); + EXPECT_CALL(*mock.get(), KeyEventReport(keyName, static_cast(KeyEvent::HOLD_UP), _)) + .Times(HOLD_UP_EVENT_HAPPENED_ONLY_ONCE) + .WillOnce(DoAll(Return())); + } } \ No newline at end of file