From 73236bba8f8a9736aa058aaebf56f7fc7aa207cc Mon Sep 17 00:00:00 2001 From: Fancy code <258828110.@qq.com> Date: Fri, 16 Feb 2024 23:36:39 -0800 Subject: [PATCH] Add:DeviceManager key test case. --- middleware/DeviceManager/src/KeyManager.cpp | 1 + test/hal/tool/include/HalTestTool.h | 2 ++ test/hal/tool/src/HalTestTool.cpp | 20 +++++++++++++ .../DeviceManager/src/DeviceManager_Test.cpp | 29 +++++++++++++++---- 4 files changed, 47 insertions(+), 5 deletions(-) diff --git a/middleware/DeviceManager/src/KeyManager.cpp b/middleware/DeviceManager/src/KeyManager.cpp index e15de27..6e3201f 100644 --- a/middleware/DeviceManager/src/KeyManager.cpp +++ b/middleware/DeviceManager/src/KeyManager.cpp @@ -36,6 +36,7 @@ void KeyManager::Init(void) void KeyManager::UnInit(void) { // + StopTimer(); mAllKeyHal.clear(); } void KeyManager::StartTimer(void) diff --git a/test/hal/tool/include/HalTestTool.h b/test/hal/tool/include/HalTestTool.h index e1c5130..4f73a3f 100644 --- a/test/hal/tool/include/HalTestTool.h +++ b/test/hal/tool/include/HalTestTool.h @@ -121,6 +121,8 @@ private: void HalMockInit(std::shared_ptr &mock); void SetAllKeysResult(std::shared_ptr &mock, std::map> &allKeys); std::shared_ptr SearchKey(const std::string &keyName); + void InitAllKeysMock(std::map> &allKeys); + void InitKeysMock(std::shared_ptr &mock); private: std::shared_ptr mHalMock; diff --git a/test/hal/tool/src/HalTestTool.cpp b/test/hal/tool/src/HalTestTool.cpp index 0b69e98..ddd9a6f 100644 --- a/test/hal/tool/src/HalTestTool.cpp +++ b/test/hal/tool/src/HalTestTool.cpp @@ -151,6 +151,7 @@ void HalTestTool::SetAllKeysResult(std::shared_ptr &mock, { EXPECT_CALL(*mock.get(), GetAllKeysTrace(_)) .WillRepeatedly(DoAll(SetArgReferee<0>(allKeys), Return(CreateStatusCode(STATUS_CODE_OK)))); + InitAllKeysMock(allKeys); } std::shared_ptr HalTestTool::SearchKey(const std::string &keyName) { @@ -165,6 +166,25 @@ std::shared_ptr HalTestTool::SearchKey(const std::string &keyNam } return mock; } +void HalTestTool::InitAllKeysMock(std::map> &allKeys) +{ + std::map>::iterator iter; + for (iter = allKeys.begin(); iter != allKeys.end(); ++iter) { + std::shared_ptr keyHal = iter->second; + std::shared_ptr mock = std::dynamic_pointer_cast(keyHal); + if (mock) { + InitKeysMock(mock); + } + else { + LogWarning("Invalid key mock.\n"); + } + } +} +void HalTestTool::InitKeysMock(std::shared_ptr &mock) +{ + EXPECT_CALL(*mock.get(), KeyEventTriggerTrace(_, _, _)) + .WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION)))); +} void HalTestTool::HalMockInit(std::shared_ptr &mock) { EXPECT_CALL(*mock.get(), GetLedHalsTrace(_)) diff --git a/test/middleware/DeviceManager/src/DeviceManager_Test.cpp b/test/middleware/DeviceManager/src/DeviceManager_Test.cpp index c6a45b5..04ac955 100644 --- a/test/middleware/DeviceManager/src/DeviceManager_Test.cpp +++ b/test/middleware/DeviceManager/src/DeviceManager_Test.cpp @@ -64,15 +64,34 @@ public: protected: std::map> mAllKeysMock; }; -// ../output_files/test/bin/DeviceManagerTest --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_Demo -TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_Demo) +// ../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::this_thread::sleep_for(std::chrono::milliseconds(1000)); - // SetKeyEvent(KEY_TEST, KeyHalEvent::PRESSING); - SetKeyClick(KEY_TEST); + SetKeyClick(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_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(); + SetKeyClick(KEY_TEST, 1000); // Simulate pressing a button. + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + IDeviceManager::GetInstance()->UnInit(); +} } // namespace DeviceManagerTest \ No newline at end of file