diff --git a/test/hal/tool/include/HalTestTool.h b/test/hal/tool/include/HalTestTool.h index 4006ab06..9d8008c9 100644 --- a/test/hal/tool/include/HalTestTool.h +++ b/test/hal/tool/include/HalTestTool.h @@ -14,8 +14,8 @@ */ #ifndef HAL_TEST_TOOL_H #define HAL_TEST_TOOL_H -#include "HalCpp.h" -#include "KeyControl.h" +#include "IHalCpp.h" +// #include "KeyControl.h" #include #include using ::testing::_; @@ -48,26 +48,6 @@ using ::testing::SetArgumentPointee; using ::testing::Unused; using ::testing::WithArgs; using ::testing::internal::BuiltInDefaultValue; -class HalCppTest : public HalCpp -{ -public: - HalCppTest() = default; - virtual ~HalCppTest() = default; - StatusCode GetAllLeds(std::map> &allLeds) override; - StatusCode GetAllKeys(std::map> &allKeys) override; - -protected: - virtual StatusCode GetAllLedsTrace(std::map> &allLeds); - virtual StatusCode GetAllKeysTrace(std::map> &allKeys); -}; -class HalCppMock : public HalCppTest -{ -public: - HalCppMock() = default; - virtual ~HalCppMock() = default; - MOCK_METHOD1(GetAllLedsTrace, StatusCode(std::map> &)); - MOCK_METHOD1(GetAllKeysTrace, StatusCode(std::map> &)); -}; class HalTestTool { public: @@ -76,7 +56,7 @@ public: void Init(void); void UnInit(void); void SetAllKeysResult(std::map> &allKeys); - void SetKeyEvent(const std::string keyName, const KeyHalEvent &event); + // void SetKeyEvent(const std::string keyName, const KeyHalEvent &event); // TODO: unused function? void SetKeyClick(const std::string &keyName, const unsigned int &pressingTimeMs = 200); protected: @@ -86,8 +66,8 @@ private: void KeyEventHappendOnce(std::shared_ptr &vMock, const unsigned int &pressingTimeMs); private: - void HalMockInit(std::shared_ptr &mock); - void SetAllKeysResult(std::shared_ptr &mock, std::map> &allKeys); + void HalMockInit(std::shared_ptr &vMock); + void SetAllKeysResult(std::shared_ptr &vMock, std::map> &allKeys); std::shared_ptr SearchKey(const std::string &keyName); void InitAllKeysMock(std::map> &allKeys); void InitKeysMock(std::shared_ptr &vMock); @@ -96,7 +76,7 @@ public: static std::shared_ptr MakeKeyHalTest(const std::string &keyName); private: - std::shared_ptr mHalMock; + std::shared_ptr mHalMock; std::map> mAllKeys; }; #endif \ No newline at end of file diff --git a/test/hal/tool/src/HalCppMock.cpp b/test/hal/tool/src/HalCppMock.cpp new file mode 100644 index 00000000..855a65c4 --- /dev/null +++ b/test/hal/tool/src/HalCppMock.cpp @@ -0,0 +1,44 @@ +/* + * 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 "HalCppMock.h" +#include "ILog.h" +StatusCode HalCppTest::GetAllLeds(std::map> &allLeds) +{ + LogInfo("HalCppTest::GetAllLeds\n"); + StatusCode code = GetAllLedsTrace(allLeds); + if (StatusCodeEqual(code, "STATUS_CODE_VIRTUAL_FUNCTION")) { + return HalCpp::GetAllLeds(allLeds); + } + return code; +} +StatusCode HalCppTest::GetAllKeys(std::map> &allKeys) +{ + LogInfo("HalCppTest::GetAllKeys\n"); + StatusCode code = GetAllKeysTrace(allKeys); + if (StatusCodeEqual(code, "STATUS_CODE_VIRTUAL_FUNCTION")) { + return HalCpp::GetAllKeys(allKeys); + } + return code; +} +StatusCode HalCppTest::GetAllLedsTrace(std::map> &allLeds) +{ + LogInfo("HalCppTest::GetAllLedsTrace\n"); + return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); +} +StatusCode HalCppTest::GetAllKeysTrace(std::map> &allKeys) +{ + LogInfo("HalCppTest::GetAllKeysTrace\n"); + return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); +} \ No newline at end of file diff --git a/test/hal/tool/src/HalCppMock.h b/test/hal/tool/src/HalCppMock.h new file mode 100644 index 00000000..864b2af0 --- /dev/null +++ b/test/hal/tool/src/HalCppMock.h @@ -0,0 +1,39 @@ +/* + * 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 HAL_CPP_MOCK_H +#define HAL_CPP_MOCK_H +#include "HalCpp.h" +#include "HalTestTool.h" +class HalCppTest : public HalCpp +{ +public: + HalCppTest() = default; + virtual ~HalCppTest() = default; + StatusCode GetAllLeds(std::map> &allLeds) override; + StatusCode GetAllKeys(std::map> &allKeys) override; + +protected: + virtual StatusCode GetAllLedsTrace(std::map> &allLeds); + virtual StatusCode GetAllKeysTrace(std::map> &allKeys); +}; +class HalCppMock : public HalCppTest +{ +public: + HalCppMock() = default; + virtual ~HalCppMock() = default; + MOCK_METHOD1(GetAllLedsTrace, StatusCode(std::map> &)); + MOCK_METHOD1(GetAllKeysTrace, StatusCode(std::map> &)); +}; +#endif \ No newline at end of file diff --git a/test/hal/tool/src/HalMakePtrTest.h b/test/hal/tool/src/HalMakePtrTest.h index 4c99d88e..681320a0 100644 --- a/test/hal/tool/src/HalMakePtrTest.h +++ b/test/hal/tool/src/HalMakePtrTest.h @@ -14,6 +14,7 @@ */ #ifndef HAL_MAKE_PTR_TEST_H #define HAL_MAKE_PTR_TEST_H +#include "HalCppMock.h" #include "HalMakePtr.h" #include "HalTestTool.h" #include "KeyControlMock.h" diff --git a/test/hal/tool/src/HalTestTool.cpp b/test/hal/tool/src/HalTestTool.cpp index f0db8f39..e915fcee 100644 --- a/test/hal/tool/src/HalTestTool.cpp +++ b/test/hal/tool/src/HalTestTool.cpp @@ -13,68 +13,18 @@ * limitations under the License. */ #include "HalTestTool.h" +#include "HalCppMock.h" #include "HalMakePtrTest.h" #include "ILog.h" +#include "KeyControl.h" #include "KeyControlMock.h" #include -StatusCode HalCppTest::GetAllLeds(std::map> &allLeds) -{ - LogInfo("HalCppTest::GetAllLeds\n"); - StatusCode code = GetAllLedsTrace(allLeds); - if (StatusCodeEqual(code, "STATUS_CODE_VIRTUAL_FUNCTION")) { - return HalCpp::GetAllLeds(allLeds); - } - return code; -} -StatusCode HalCppTest::GetAllKeys(std::map> &allKeys) -{ - LogInfo("HalCppTest::GetAllKeys\n"); - StatusCode code = GetAllKeysTrace(allKeys); - if (StatusCodeEqual(code, "STATUS_CODE_VIRTUAL_FUNCTION")) { - return HalCpp::GetAllKeys(allKeys); - } - return code; -} -StatusCode HalCppTest::GetAllLedsTrace(std::map> &allLeds) -{ - LogInfo("HalCppTest::GetAllLedsTrace\n"); - return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); -} -StatusCode HalCppTest::GetAllKeysTrace(std::map> &allKeys) -{ - LogInfo("HalCppTest::GetAllKeysTrace\n"); - return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); -} -KeyControlTest::KeyControlTest(const std::string &keyName) : mKeyName(keyName) -{ - // -} -void KeyControlTest::SetKeyMonitor(std::shared_ptr &monitor) -{ - // - mMonitor = monitor; -} -void KeyControlTest::CheckKeyStatus(void) -{ - // - TimerKeyEventTrigger(KeyHalEvent::PRESSING); -} -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); - KeyEventTriggerTrace(keyName, event, timeMs); - auto monitor = mMonitor.lock(); - if (mMonitor.expired()) { - LogError("monitor is nullptr.\n"); - return; - } - monitor->KeyEventHappened(keyName, static_cast(event), timeMs); -} void HalTestTool::Init(void) { mHalMock = std::make_shared(); HalMockInit(mHalMock); - OverrideHalMakePtrObject(mHalMock); + std::shared_ptr mock = std::dynamic_pointer_cast(mHalMock); + OverrideHalMakePtrObject(mock); } void HalTestTool::UnInit(void) { @@ -83,24 +33,25 @@ void HalTestTool::UnInit(void) } void HalTestTool::SetAllKeysResult(std::map> &allKeys) { - SetAllKeysResult(mHalMock, allKeys); + std::shared_ptr halMock = mHalMock; + SetAllKeysResult(halMock, allKeys); mAllKeys = allKeys; } -void HalTestTool::SetKeyEvent(const std::string keyName, const KeyHalEvent &event) -{ - std::shared_ptr key = SearchKey(keyName); - if (!key) { - LogError("Can't set key event, key not found.\n"); - return; - } - std::shared_ptr keyMock = std::dynamic_pointer_cast(key); - if (keyMock) { - keyMock->SetKeyEvent(event); - } - else { - LogWarning("Key mock error.\n"); - } -} +// void HalTestTool::SetKeyEvent(const std::string keyName, const KeyHalEvent &event) +// { +// std::shared_ptr key = SearchKey(keyName); +// if (!key) { +// LogError("Can't set key event, key not found.\n"); +// return; +// } +// std::shared_ptr keyMock = std::dynamic_pointer_cast(key); +// if (keyMock) { +// keyMock->SetKeyEvent(event); +// } +// else { +// LogWarning("Key mock error.\n"); +// } +// } void HalTestTool::SetKeyClick(const std::string &keyName, const unsigned int &pressingTimeMs) { std::shared_ptr key = SearchKey(keyName); @@ -139,13 +90,6 @@ void HalTestTool::KeyEventHappendOnce(std::shared_ptr &vMock, const uns .WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION)))); } } -void HalTestTool::SetAllKeysResult(std::shared_ptr &mock, - std::map> &allKeys) -{ - 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) { std::shared_ptr mock; @@ -184,13 +128,30 @@ void HalTestTool::InitKeysMock(std::shared_ptr &vMock) constexpr int KEY_SHOULD_NOT_TRIGGER_WHEN_NOBODY_SET_KEY_RESULT = 0; EXPECT_CALL(*mock.get(), KeyEventTriggerTrace(_, _, _)).Times(KEY_SHOULD_NOT_TRIGGER_WHEN_NOBODY_SET_KEY_RESULT); } -void HalTestTool::HalMockInit(std::shared_ptr &mock) +void HalTestTool::HalMockInit(std::shared_ptr &vMock) { + std::shared_ptr mock = std::dynamic_pointer_cast(vMock); + if (!mock) { + LogError("vMock error.\n"); + return; + } EXPECT_CALL(*mock.get(), GetAllLedsTrace(_)) .WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION)))); EXPECT_CALL(*mock.get(), GetAllKeysTrace(_)) .WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION)))); } +void HalTestTool::SetAllKeysResult(std::shared_ptr &vMock, + std::map> &allKeys) +{ + std::shared_ptr mock = std::dynamic_pointer_cast(vMock); + if (!mock) { + LogError("vMock error.\n"); + return; + } + EXPECT_CALL(*mock.get(), GetAllKeysTrace(_)) + .WillRepeatedly(DoAll(SetArgReferee<0>(allKeys), Return(CreateStatusCode(STATUS_CODE_OK)))); + InitAllKeysMock(allKeys); +} std::shared_ptr HalTestTool::MakeKeyHalTest(const std::string &keyName) { std::shared_ptr key = std::make_shared(keyName); diff --git a/test/hal/tool/src/KeyControlMock.cpp b/test/hal/tool/src/KeyControlMock.cpp index 71d4e979..61d8965b 100644 --- a/test/hal/tool/src/KeyControlMock.cpp +++ b/test/hal/tool/src/KeyControlMock.cpp @@ -14,6 +14,31 @@ */ #include "KeyControlMock.h" #include "ILog.h" +KeyControlTest::KeyControlTest(const std::string &keyName) : mKeyName(keyName) +{ + // +} +void KeyControlTest::SetKeyMonitor(std::shared_ptr &monitor) +{ + // + mMonitor = monitor; +} +void KeyControlTest::CheckKeyStatus(void) +{ + // + TimerKeyEventTrigger(KeyHalEvent::PRESSING); +} +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); + KeyEventTriggerTrace(keyName, event, timeMs); + auto monitor = mMonitor.lock(); + if (mMonitor.expired()) { + LogError("monitor is nullptr.\n"); + return; + } + monitor->KeyEventHappened(keyName, static_cast(event), timeMs); +} KeyControlMock::KeyControlMock(const std::string &keyName) : KeyControlTest(keyName) { // diff --git a/test/middleware/DeviceManager/CMakeLists.txt b/test/middleware/DeviceManager/CMakeLists.txt index 54a3a05e..5e012e61 100644 --- a/test/middleware/DeviceManager/CMakeLists.txt +++ b/test/middleware/DeviceManager/CMakeLists.txt @@ -12,7 +12,7 @@ include_directories( # ${UTILS_SOURCE_PATH}/McuProtocol/include ${UTILS_SOURCE_PATH}/KeyControl/include ${HAL_SOURCE_PATH}/include - ${HAL_SOURCE_PATH}/src + # ${HAL_SOURCE_PATH}/src ${MIDDLEWARE_SOURCE_PATH}/DeviceManager/include ${MIDDLEWARE_SOURCE_PATH}/DeviceManager/src # ${MIDDLEWARE_SOURCE_PATH}/McuAskBase/include diff --git a/test/middleware/DeviceManager/tool/src/DeviceManagerTestTool.cpp b/test/middleware/DeviceManager/tool/src/DeviceManagerTestTool.cpp index 30d81dd9..8d4dc9c8 100644 --- a/test/middleware/DeviceManager/tool/src/DeviceManagerTestTool.cpp +++ b/test/middleware/DeviceManager/tool/src/DeviceManagerTestTool.cpp @@ -15,6 +15,7 @@ #include "DeviceManagerTestTool.h" #include "DeviceManagerMakePtrTest.h" #include "ILog.h" +#include "KeyControl.h" const StatusCode DeviceManagerTool::SetAllKeysMonitor(std::shared_ptr &monitor) { LogInfo("DeviceManagerTool::SetAllKeysMonitor\n");