Improve:Hal test tool.

This commit is contained in:
Fancy code 2024-02-19 10:48:40 -08:00
parent a02fcf958b
commit d05a8757cf
8 changed files with 156 additions and 105 deletions

View File

@ -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 <gmock/gmock.h>
#include <gtest/gtest.h>
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<std::string, std::shared_ptr<VLedHal>> &allLeds) override;
StatusCode GetAllKeys(std::map<std::string, std::shared_ptr<VKeyHal>> &allKeys) override;
protected:
virtual StatusCode GetAllLedsTrace(std::map<std::string, std::shared_ptr<VLedHal>> &allLeds);
virtual StatusCode GetAllKeysTrace(std::map<std::string, std::shared_ptr<VKeyHal>> &allKeys);
};
class HalCppMock : public HalCppTest
{
public:
HalCppMock() = default;
virtual ~HalCppMock() = default;
MOCK_METHOD1(GetAllLedsTrace, StatusCode(std::map<std::string, std::shared_ptr<VLedHal>> &));
MOCK_METHOD1(GetAllKeysTrace, StatusCode(std::map<std::string, std::shared_ptr<VKeyHal>> &));
};
class HalTestTool
{
public:
@ -76,7 +56,7 @@ public:
void Init(void);
void UnInit(void);
void SetAllKeysResult(std::map<std::string, std::shared_ptr<VKeyHal>> &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<VKeyHal> &vMock, const unsigned int &pressingTimeMs);
private:
void HalMockInit(std::shared_ptr<HalCppMock> &mock);
void SetAllKeysResult(std::shared_ptr<HalCppMock> &mock, std::map<std::string, std::shared_ptr<VKeyHal>> &allKeys);
void HalMockInit(std::shared_ptr<IHalCpp> &vMock);
void SetAllKeysResult(std::shared_ptr<IHalCpp> &vMock, std::map<std::string, std::shared_ptr<VKeyHal>> &allKeys);
std::shared_ptr<VKeyHal> SearchKey(const std::string &keyName);
void InitAllKeysMock(std::map<std::string, std::shared_ptr<VKeyHal>> &allKeys);
void InitKeysMock(std::shared_ptr<VKeyHal> &vMock);
@ -96,7 +76,7 @@ public:
static std::shared_ptr<VKeyHal> MakeKeyHalTest(const std::string &keyName);
private:
std::shared_ptr<HalCppMock> mHalMock;
std::shared_ptr<IHalCpp> mHalMock;
std::map<std::string, std::shared_ptr<VKeyHal>> mAllKeys;
};
#endif

View File

@ -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<std::string, std::shared_ptr<VLedHal>> &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<std::string, std::shared_ptr<VKeyHal>> &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<std::string, std::shared_ptr<VLedHal>> &allLeds)
{
LogInfo("HalCppTest::GetAllLedsTrace\n");
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
StatusCode HalCppTest::GetAllKeysTrace(std::map<std::string, std::shared_ptr<VKeyHal>> &allKeys)
{
LogInfo("HalCppTest::GetAllKeysTrace\n");
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}

View File

@ -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<std::string, std::shared_ptr<VLedHal>> &allLeds) override;
StatusCode GetAllKeys(std::map<std::string, std::shared_ptr<VKeyHal>> &allKeys) override;
protected:
virtual StatusCode GetAllLedsTrace(std::map<std::string, std::shared_ptr<VLedHal>> &allLeds);
virtual StatusCode GetAllKeysTrace(std::map<std::string, std::shared_ptr<VKeyHal>> &allKeys);
};
class HalCppMock : public HalCppTest
{
public:
HalCppMock() = default;
virtual ~HalCppMock() = default;
MOCK_METHOD1(GetAllLedsTrace, StatusCode(std::map<std::string, std::shared_ptr<VLedHal>> &));
MOCK_METHOD1(GetAllKeysTrace, StatusCode(std::map<std::string, std::shared_ptr<VKeyHal>> &));
};
#endif

View File

@ -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"

View File

@ -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 <thread>
StatusCode HalCppTest::GetAllLeds(std::map<std::string, std::shared_ptr<VLedHal>> &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<std::string, std::shared_ptr<VKeyHal>> &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<std::string, std::shared_ptr<VLedHal>> &allLeds)
{
LogInfo("HalCppTest::GetAllLedsTrace\n");
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
StatusCode HalCppTest::GetAllKeysTrace(std::map<std::string, std::shared_ptr<VKeyHal>> &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<VKeyHalMonitor> &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<VirtualKeyEvent>(event), timeMs);
}
void HalTestTool::Init(void)
{
mHalMock = std::make_shared<HalCppMock>();
HalMockInit(mHalMock);
OverrideHalMakePtrObject(mHalMock);
std::shared_ptr<HalCppMock> mock = std::dynamic_pointer_cast<HalCppMock>(mHalMock);
OverrideHalMakePtrObject(mock);
}
void HalTestTool::UnInit(void)
{
@ -83,24 +33,25 @@ void HalTestTool::UnInit(void)
}
void HalTestTool::SetAllKeysResult(std::map<std::string, std::shared_ptr<VKeyHal>> &allKeys)
{
SetAllKeysResult(mHalMock, allKeys);
std::shared_ptr<IHalCpp> halMock = mHalMock;
SetAllKeysResult(halMock, allKeys);
mAllKeys = allKeys;
}
void HalTestTool::SetKeyEvent(const std::string keyName, const KeyHalEvent &event)
{
std::shared_ptr<VKeyHal> key = SearchKey(keyName);
if (!key) {
LogError("Can't set key event, key not found.\n");
return;
}
std::shared_ptr<KeyControlMock> keyMock = std::dynamic_pointer_cast<KeyControlMock>(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<VKeyHal> key = SearchKey(keyName);
// if (!key) {
// LogError("Can't set key event, key not found.\n");
// return;
// }
// std::shared_ptr<KeyControlMock> keyMock = std::dynamic_pointer_cast<KeyControlMock>(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<VKeyHal> key = SearchKey(keyName);
@ -139,13 +90,6 @@ void HalTestTool::KeyEventHappendOnce(std::shared_ptr<VKeyHal> &vMock, const uns
.WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION))));
}
}
void HalTestTool::SetAllKeysResult(std::shared_ptr<HalCppMock> &mock,
std::map<std::string, std::shared_ptr<VKeyHal>> &allKeys)
{
EXPECT_CALL(*mock.get(), GetAllKeysTrace(_))
.WillRepeatedly(DoAll(SetArgReferee<0>(allKeys), Return(CreateStatusCode(STATUS_CODE_OK))));
InitAllKeysMock(allKeys);
}
std::shared_ptr<VKeyHal> HalTestTool::SearchKey(const std::string &keyName)
{
std::shared_ptr<KeyControlMock> mock;
@ -184,13 +128,30 @@ void HalTestTool::InitKeysMock(std::shared_ptr<VKeyHal> &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<HalCppMock> &mock)
void HalTestTool::HalMockInit(std::shared_ptr<IHalCpp> &vMock)
{
std::shared_ptr<HalCppMock> mock = std::dynamic_pointer_cast<HalCppMock>(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<IHalCpp> &vMock,
std::map<std::string, std::shared_ptr<VKeyHal>> &allKeys)
{
std::shared_ptr<HalCppMock> mock = std::dynamic_pointer_cast<HalCppMock>(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<VKeyHal> HalTestTool::MakeKeyHalTest(const std::string &keyName)
{
std::shared_ptr<VKeyHal> key = std::make_shared<KeyControlMock>(keyName);

View File

@ -14,6 +14,31 @@
*/
#include "KeyControlMock.h"
#include "ILog.h"
KeyControlTest::KeyControlTest(const std::string &keyName) : mKeyName(keyName)
{
//
}
void KeyControlTest::SetKeyMonitor(std::shared_ptr<VKeyHalMonitor> &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<VirtualKeyEvent>(event), timeMs);
}
KeyControlMock::KeyControlMock(const std::string &keyName) : KeyControlTest(keyName)
{
//

View File

@ -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

View File

@ -15,6 +15,7 @@
#include "DeviceManagerTestTool.h"
#include "DeviceManagerMakePtrTest.h"
#include "ILog.h"
#include "KeyControl.h"
const StatusCode DeviceManagerTool::SetAllKeysMonitor(std::shared_ptr<VKeyMonitor> &monitor)
{
LogInfo("DeviceManagerTool::SetAllKeysMonitor\n");