Improve:DeviceManager test tool.

This commit is contained in:
Fancy code 2024-02-18 02:36:00 -08:00
parent 9fe836864e
commit 4efd610707
8 changed files with 46 additions and 4 deletions

View File

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

View File

@ -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<VirtualKeyEvent>(event), timeMs);
}

View File

@ -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<KeyControlMock> &mock, const unsigned int &pressingTimeMs);

View File

@ -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<KeyControlMock> &mock, const unsigned int &pressingTimeMs)

View File

@ -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() {}

View File

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

View File

@ -15,6 +15,7 @@
#ifndef DEVICE_MANAGER_TEST_TOOL_H
#define DEVICE_MANAGER_TEST_TOOL_H
#include "DeviceManager.h"
#include "HalTestTool.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
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<DeviceManagerMock> &mock);
void KeyMonitorInit(std::shared_ptr<KeyMonitorMock> &mock, const std::string &keyName,
const unsigned int &pressingTimeMs);
private:
std::shared_ptr<DeviceManagerMock> mDeviceManagerMock;

View File

@ -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<DeviceManagerMock> &mock)
{
auto getKeyMonitor = [=](std::shared_ptr<VKeyMonitor> &monitor) {
@ -47,4 +57,25 @@ void DeviceManagerTestTool::DeviceManagerMockInit(std::shared_ptr<DeviceManagerM
.Times(ONLY_BE_CALLED_ONCE)
.WillOnce(DoAll(WithArgs<0>(Invoke(getKeyMonitor)), Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION))));
// .WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION))));
}
void DeviceManagerTestTool::KeyMonitorInit(std::shared_ptr<KeyMonitorMock> &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<VirtualKeyEvent>(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<VirtualKeyEvent>(KeyEvent::HOLD_DOWN), _))
.Times(AtLeast(1))
.WillRepeatedly(DoAll(Return()));
EXPECT_CALL(*mock.get(), KeyEventReport(keyName, static_cast<VirtualKeyEvent>(KeyEvent::HOLD_UP), _))
.Times(HOLD_UP_EVENT_HAPPENED_ONLY_ONCE)
.WillOnce(DoAll(Return()));
}
}