From 818aea1417988338690bbea3ad00226aba4045d3 Mon Sep 17 00:00:00 2001 From: Fancy code <258828110.@qq.com> Date: Wed, 21 Feb 2024 03:30:38 -0800 Subject: [PATCH] Add:Led control test code. --- test/hal/tool/include/HalTestTool.h | 5 ++- test/hal/tool/src/HalTestTool.cpp | 41 ++++++++++--------- test/hal/tool/src/LedControlMock.cpp | 31 +++++++++++--- test/hal/tool/src/LedControlMock.h | 3 +- .../DeviceManager/src/DeviceManager_Test.cpp | 40 ++++++++++++++++++ .../tool/src/DeviceManagerTestTool.cpp | 2 +- 6 files changed, 94 insertions(+), 28 deletions(-) diff --git a/test/hal/tool/include/HalTestTool.h b/test/hal/tool/include/HalTestTool.h index 73597d3..0aa46cd 100644 --- a/test/hal/tool/include/HalTestTool.h +++ b/test/hal/tool/include/HalTestTool.h @@ -59,7 +59,8 @@ public: void SetAllLedsResult(std::map> &allLeds); // void SetKeyEvent(const std::string keyName, const KeyHalEvent &event); // TODO: unused function? void SetKeyClick(const std::string &keyName, const unsigned int &pressingTimeMs = 200); - void SetLedStateExpectations(const std::string &ledName, const LedState &state); + void SetLedStateExpectations(const std::string &ledName, const LedState &state, const unsigned int &aliveTimeMs, + const unsigned int &blinkTimeMs); protected: virtual void DeviceManagerNotice(const std::string &keyName, const unsigned int &pressingTimeMs) {} @@ -76,7 +77,7 @@ private: private: void SetAllLedsResult(std::shared_ptr &vMock, std::map> &allLeds); - std::shared_ptr SearchLed(const std::string &ledName); + std::shared_ptr &SearchLed(const std::string &ledName); void InitAllLedsMock(std::map> &allLeds); void InitLedsMock(std::shared_ptr &vMock); diff --git a/test/hal/tool/src/HalTestTool.cpp b/test/hal/tool/src/HalTestTool.cpp index 88fe20e..1a9701e 100644 --- a/test/hal/tool/src/HalTestTool.cpp +++ b/test/hal/tool/src/HalTestTool.cpp @@ -78,20 +78,22 @@ void HalTestTool::SetKeyClick(const std::string &keyName, const unsigned int &pr LogWarning("Key mock error.\n"); } } -void HalTestTool::SetLedStateExpectations(const std::string &ledName, const LedState &state) +void HalTestTool::SetLedStateExpectations(const std::string &ledName, const LedState &state, + const unsigned int &aliveTimeMs, const unsigned int &blinkTimeMs) { - std::shared_ptr led = SearchLed(ledName); - if (!led) { - LogError("Can't set led state, led not found.\n"); - return; - } - std::shared_ptr ledMock = std::dynamic_pointer_cast(led); - if (ledMock) { - LedControlMock::SetLedStateMock(ledMock, state); - } - else { - LogWarning("led mock error.\n"); - } + std::shared_ptr &led = SearchLed(ledName); + // if (!led) { + // LogError("Can't set led state, led not found.\n"); + // return; + // } + // std::shared_ptr ledMock = std::dynamic_pointer_cast(led); + // if (ledMock) { + // LedControlMock::SetLedStateMock(ledMock, state, aliveTimeMs, blinkTimeMs); + // } + // else { + // LogWarning("led mock error.\n"); + // } + LedControlMock::SetLedStateMock(led, state, aliveTimeMs, blinkTimeMs); } void HalTestTool::KeyEventHappendOnce(std::shared_ptr &vMock, const unsigned int &pressingTimeMs) { @@ -192,18 +194,19 @@ void HalTestTool::SetAllLedsResult(std::shared_ptr &vMock, .WillRepeatedly(DoAll(WithArgs<0>(Invoke(getAllLeds)), Return(CreateStatusCode(STATUS_CODE_OK)))); InitAllLedsMock(allLeds); } -std::shared_ptr HalTestTool::SearchLed(const std::string &ledName) +std::shared_ptr &HalTestTool::SearchLed(const std::string &ledName) { + static std::shared_ptr noLed = std::make_shared(); std::shared_ptr mock; std::map>::iterator iter; iter = mAllLeds.find(ledName); if (iter != mAllLeds.end()) { - mock = std::dynamic_pointer_cast(mAllLeds[ledName]); + // mock = std::dynamic_pointer_cast(mAllLeds[ledName]); + std::shared_ptr &existLed = mAllLeds[ledName]; + return existLed; } - else { - LogWarning("Can't found the led control.\n"); - } - return mock; + LogWarning("Can't found the led control.\n"); + return noLed; } void HalTestTool::InitAllLedsMock(std::map> &allLeds) { diff --git a/test/hal/tool/src/LedControlMock.cpp b/test/hal/tool/src/LedControlMock.cpp index 8530c93..13417e0 100644 --- a/test/hal/tool/src/LedControlMock.cpp +++ b/test/hal/tool/src/LedControlMock.cpp @@ -35,14 +35,35 @@ StatusCode LedControlTest::SetLedStateTrace(const LedState &state) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); } -void LedControlMock::SetLedStateMock(std::shared_ptr &mock, const LedState &state) +void LedControlMock::SetLedStateMock(std::shared_ptr &vMock, const LedState &state, + const unsigned int &aliveTimeMs, const unsigned int &blinkTimeMs) { LogInfo("LedControlMock::SetLedState\n"); + std::shared_ptr mock = std::dynamic_pointer_cast(vMock); + if (!mock) { + LogError("Can't set led state, led not found.\n"); + return; + } constexpr int SOMEBODY_CONTROL_LED = 1; EXPECT_CALL(*mock.get(), SetLedStateTrace(_)) - // .Times(SOMEBODY_CONTROL_LED) - .WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION)))); - EXPECT_CALL(*mock.get(), SetLedStateTrace(state)) - .Times(SOMEBODY_CONTROL_LED) .WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION)))); + auto defaultExpectations = [=, &vMock]() { + // std::shared_ptr mock = std::dynamic_pointer_cast(vMock); + // if (!mock) { + // LogError("Can't set led state, led not found.\n"); + // return; + // } + // EXPECT_CALL(*mock.get(), SetLedStateTrace(state)) + // .WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION)))); + }; + if (blinkTimeMs == LED_NOT_BLINK) { + EXPECT_CALL(*mock.get(), SetLedStateTrace(state)) + .Times(SOMEBODY_CONTROL_LED) + .WillRepeatedly(DoAll(Invoke(defaultExpectations), Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION)))); + } + else { + EXPECT_CALL(*mock.get(), SetLedStateTrace(state)) + .Times(AtLeast(SOMEBODY_CONTROL_LED)) + .WillRepeatedly(DoAll(Invoke(defaultExpectations), Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION)))); + } } \ No newline at end of file diff --git a/test/hal/tool/src/LedControlMock.h b/test/hal/tool/src/LedControlMock.h index 6aa4793..2f566ac 100644 --- a/test/hal/tool/src/LedControlMock.h +++ b/test/hal/tool/src/LedControlMock.h @@ -38,6 +38,7 @@ public: MOCK_METHOD1(SetLedStateTrace, StatusCode(const LedState &)); public: - static void SetLedStateMock(std::shared_ptr &mock, const LedState &state); + static void SetLedStateMock(std::shared_ptr &vMock, const LedState &state, const unsigned int &aliveTimeMs, + const unsigned int &blinkTimeMs); }; #endif \ No newline at end of file diff --git a/test/middleware/DeviceManager/src/DeviceManager_Test.cpp b/test/middleware/DeviceManager/src/DeviceManager_Test.cpp index 7d691fa..5abed79 100644 --- a/test/middleware/DeviceManager/src/DeviceManager_Test.cpp +++ b/test/middleware/DeviceManager/src/DeviceManager_Test.cpp @@ -149,4 +149,44 @@ TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed) 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 monitor = std::make_shared(); + 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 monitor = std::make_shared(); + 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 monitor = std::make_shared(); + 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(); +} } // namespace DeviceManagerTest \ No newline at end of file diff --git a/test/middleware/DeviceManager/tool/src/DeviceManagerTestTool.cpp b/test/middleware/DeviceManager/tool/src/DeviceManagerTestTool.cpp index c8b176e..2895e1f 100644 --- a/test/middleware/DeviceManager/tool/src/DeviceManagerTestTool.cpp +++ b/test/middleware/DeviceManager/tool/src/DeviceManagerTestTool.cpp @@ -42,7 +42,7 @@ std::shared_ptr DeviceManagerTestTool::ControlLed(const std:: const unsigned int &aliveTimeMs, const unsigned int &blinkTimeMs) { - HalTestTool::SetLedStateExpectations(ledName, state); + HalTestTool::SetLedStateExpectations(ledName, state, aliveTimeMs, blinkTimeMs); std::shared_ptr ledControl = std::make_shared(state, aliveTimeMs, blinkTimeMs); IDeviceManager::GetInstance()->ControlLed(ledName, ledControl);