Improve: Hal test tool.

This commit is contained in:
Fancy code 2024-02-19 10:14:51 -08:00
parent 4efd610707
commit a02fcf958b
21 changed files with 449 additions and 135 deletions

View File

@ -31,7 +31,7 @@ std::shared_ptr<IHalCpp> &IHalCpp::GetInstance(std::shared_ptr<IHalCpp> *impl)
}
StatusCode IHalCpp::Init(void) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); }
StatusCode IHalCpp::UnInit(void) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); }
StatusCode IHalCpp::GetLedHals(std::vector<std::shared_ptr<VLedHal>> &ledHals)
StatusCode IHalCpp::GetAllLeds(std::map<std::string, std::shared_ptr<VLedHal>> &allLeds)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}

View File

@ -25,6 +25,7 @@ constexpr int INVALID_PERIOD = -1;
constexpr int PERIPHERAL_CHECK_PERIOD_MS = 100;
constexpr int IMEI_LEN = 15;
void CreateHalCppModule(void);
void DestroyHalCppModule(void);
class VKeyHalMonitor
{
public:
@ -47,12 +48,7 @@ class VLedHal
{
public:
VLedHal() = default;
~VLedHal() = default;
virtual StatusCode SetHalLedState(const VirtualLedState &state)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
virtual std::string GetLedName(void) { return "undefine"; }
virtual ~VLedHal() = default;
};
class IHalCpp
{
@ -62,7 +58,7 @@ public:
static std::shared_ptr<IHalCpp> &GetInstance(std::shared_ptr<IHalCpp> *impl = nullptr);
virtual StatusCode Init(void);
virtual StatusCode UnInit(void);
virtual StatusCode GetLedHals(std::vector<std::shared_ptr<VLedHal>> &ledHals);
virtual StatusCode GetAllLeds(std::map<std::string, std::shared_ptr<VLedHal>> &allLeds);
virtual StatusCode GetAllKeys(std::map<std::string, std::shared_ptr<VKeyHal>> &allKeys);
};
#endif

View File

@ -50,6 +50,11 @@ void CreateHalCppModule(void)
IHalCpp::GetInstance(&instance);
}
}
void DestroyHalCppModule(void)
{
auto instance = std::make_shared<IHalCpp>();
IHalCpp::GetInstance(&instance);
}
std::shared_ptr<HalMakePtr> &HalMakePtr::GetInstance(std::shared_ptr<HalMakePtr> *impl)
{
static auto instance = std::make_shared<HalMakePtr>();

View File

@ -8,6 +8,7 @@ include_directories(
./include
${UTILS_SOURCE_PATH}/StatusCode/include
${UTILS_SOURCE_PATH}/Log/include
${UTILS_SOURCE_PATH}/LedControl/include
${HAL_SOURCE_PATH}/include
)
#do not rely on any other library
@ -21,7 +22,7 @@ aux_source_directory(./src SRC_FILES)
set(TARGET_NAME DeviceManager)
add_library(${TARGET_NAME} STATIC ${SRC_FILES})
target_link_libraries(${TARGET_NAME} Hal StatusCode Log)
target_link_libraries(${TARGET_NAME} LedControl Hal StatusCode Log)
if ("${CLANG_TIDY_SUPPORT}" MATCHES "true")
add_custom_target(

View File

@ -38,6 +38,12 @@ public:
virtual ~VKeyMonitor() = default;
virtual void KeyEventReport(const std::string &keyName, const VirtualKeyEvent &event, const unsigned int &timeMs) {}
};
class VirtualLedControl
{
public:
VirtualLedControl() = default;
virtual ~VirtualLedControl() = default;
};
class IDeviceManager
{
public:
@ -46,8 +52,7 @@ public:
static std::shared_ptr<IDeviceManager> &GetInstance(std::shared_ptr<IDeviceManager> *impl = nullptr);
virtual const StatusCode Init(void);
virtual const StatusCode UnInit(void);
virtual const StatusCode SetLedState(const std::string &ledName, const VirtualLedState &currentState,
const unsigned int &KeepAliveTime, const unsigned int &blinkPeriod);
virtual const StatusCode ControlLed(const std::string &ledName, std::shared_ptr<VirtualLedControl> &control);
virtual const StatusCode SetAllKeysMonitor(std::shared_ptr<VKeyMonitor> &monitor);
};
#endif

View File

@ -16,18 +16,22 @@
#include "DeviceManager.h"
#include "ILog.h"
#include "KeyManager.h"
#include "LedManager.h"
#include <vector>
const StatusCode DeviceManager::Init(void)
{
KeyManager::GetInstance()->Init();
KeyManager::GetInstance()->StartTimer();
// LedManager::GetInstance()->Init();
// LedManager::GetInstance()->StartTimer();
return CreateStatusCode(STATUS_CODE_OK);
}
const StatusCode DeviceManager::UnInit(void)
{
KeyManager::GetInstance()->UnInit();
// LedManager::GetInstance()->UnInit();
return CreateStatusCode(STATUS_CODE_OK);
}
const StatusCode DeviceManager::SetAllKeysMonitor(std::shared_ptr<VKeyMonitor> &monitor)
@ -35,3 +39,8 @@ const StatusCode DeviceManager::SetAllKeysMonitor(std::shared_ptr<VKeyMonitor> &
LogInfo("DeviceManager::SetAllKeysMonitor\n");
return KeyManager::GetInstance()->SetKeyMonitor(monitor);
}
const StatusCode DeviceManager::ControlLed(const std::string &ledName, std::shared_ptr<VirtualLedControl> &control)
{
LedManager::GetInstance()->ControlLed(ledName, control);
return CreateStatusCode(STATUS_CODE_OK);
}

View File

@ -25,10 +25,8 @@ public:
const StatusCode Init(void) override;
const StatusCode UnInit(void) override;
// const StatusCode SetLedState(const std::string &ledName, const VirtualLedState &currentState,
// const unsigned int &keepAliveTime = DEFAULT_KEEP_ALIVE_TIME,
// const unsigned int &blinkPeriod = LED_NOT_BLINK) override;
const StatusCode SetAllKeysMonitor(std::shared_ptr<VKeyMonitor> &monitor) override;
const StatusCode ControlLed(const std::string &ledName, std::shared_ptr<VirtualLedControl> &control) override;
private:
// std::vector<std::shared_ptr<LedManager>> mLedManagers;

View File

@ -30,8 +30,7 @@ std::shared_ptr<IDeviceManager> &IDeviceManager::GetInstance(std::shared_ptr<IDe
}
const StatusCode IDeviceManager::Init(void) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); }
const StatusCode IDeviceManager::UnInit(void) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); }
const StatusCode IDeviceManager::SetLedState(const std::string &ledName, const VirtualLedState &currentState,
const unsigned int &KeepAliveTime, const unsigned int &blinkPeriod)
const StatusCode IDeviceManager::ControlLed(const std::string &ledName, std::shared_ptr<VirtualLedControl> &control)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}

View File

@ -30,11 +30,11 @@ public:
void UnInit(void);
void StartTimer(void);
void StopTimer(void);
void Timer(void);
void GetAllKeysState(std::map<std::string, KeyStatus> &status);
const StatusCode SetKeyMonitor(std::shared_ptr<VKeyMonitor> &monitor);
private:
void Timer(void);
void SetKeyHalMonitor(void);
private:

View File

@ -0,0 +1,94 @@
/*
* 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 "LedManager.h"
#include "ILog.h"
std::shared_ptr<LedManager> &LedManager::GetInstance(std::shared_ptr<LedManager> *impl)
{
static auto instance = std::make_shared<LedManager>();
if (impl) {
if (instance.use_count() == 1) {
LogInfo("Instance changed succeed.\n");
instance = *impl;
}
else {
LogError("Can't changing the instance becase of using by some one.\n");
}
}
return instance;
}
void LedManager::Init(void)
{
std::map<std::string, std::shared_ptr<VLedHal>> allLeds;
IHalCpp::GetInstance()->GetAllLeds(allLeds);
LedConversion(allLeds);
StartTimer();
}
void LedManager::UnInit(void)
{
StopTimer();
mAllLedHal.clear();
}
void LedManager::Timer(void)
{
mTimerRuning = true;
std::map<std::string, std::shared_ptr<VLedControl>>::iterator iter;
while (mTimerRuning) {
mMutex.lock();
for (iter = mAllLedHal.begin(); iter != mAllLedHal.end(); ++iter) {
std::shared_ptr<VLedControl> ledHal = iter->second;
ledHal->CheckState(LED_STATE_CHECK_PERIOD_MS);
}
mMutex.unlock();
std::this_thread::sleep_for(std::chrono::milliseconds(LED_STATE_CHECK_PERIOD_MS));
}
}
void LedManager::ControlLed(const std::string &ledName, std::shared_ptr<VirtualLedControl> &control)
{
std::lock_guard<std::mutex> locker(mMutex);
std::shared_ptr<VSingleControl> singleControl = std::dynamic_pointer_cast<VSingleControl>(control);
if (!singleControl) {
LogError("led can't be controled.\n");
return;
}
mAllLedHal[ledName]->AddLedState(singleControl);
}
void LedManager::StartTimer(void)
{
auto timerThread = [](std::shared_ptr<LedManager> timer) {
LogInfo("Led timer started.\n");
timer->Timer();
};
mTimer = std::thread(timerThread, shared_from_this());
}
void LedManager::StopTimer(void)
{
mTimerRuning = false;
if (mTimer.joinable()) {
mTimer.join();
}
}
void LedManager::LedConversion(std::map<std::string, std::shared_ptr<VLedHal>> &ledHal)
{
std::map<std::string, std::shared_ptr<VLedHal>>::iterator iter;
for (iter = ledHal.begin(); iter != ledHal.end(); ++iter) {
std::shared_ptr<VLedControl> led = std::dynamic_pointer_cast<VLedControl>(iter->second);
if (led) {
mAllLedHal[iter->first] = led;
}
else {
LogWarning("Missing something.\n");
}
}
}

View File

@ -0,0 +1,46 @@
/*
* 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 LED_MANAGER_H
#define LED_MANAGER_H
#include "IDeviceManager.h"
#include "IHalCpp.h"
#include "LedControl.h"
#include <memory>
#include <mutex>
#include <thread>
constexpr int LED_STATE_CHECK_PERIOD_MS = 100;
class LedManager : public std::enable_shared_from_this<LedManager>
{
public:
LedManager() = default;
virtual ~LedManager() = default;
static std::shared_ptr<LedManager> &GetInstance(std::shared_ptr<LedManager> *impl = nullptr);
void Init(void);
void UnInit(void);
void StartTimer(void);
void StopTimer(void);
void ControlLed(const std::string &ledName, std::shared_ptr<VirtualLedControl> &control);
private:
void Timer(void);
void LedConversion(std::map<std::string, std::shared_ptr<VLedHal>> &ledHal);
private:
std::mutex mMutex;
std::map<std::string, std::shared_ptr<VLedControl>> mAllLedHal;
bool mTimerRuning;
std::thread mTimer;
};
#endif

View File

@ -8,6 +8,7 @@ include_directories(
${UTILS_SOURCE_PATH}/StatusCode/include
${UTILS_SOURCE_PATH}/Log/include
${UTILS_SOURCE_PATH}/KeyControl/include
${UTILS_SOURCE_PATH}/LedControl/include
# ${UTILS_SOURCE_PATH}/McuProtocol/include
${HAL_SOURCE_PATH}/src
# /home/xiaojiazhu/project/rkipc/battery/ipc-rk1106/ipc-sdk/hal/src/HalCpp.h
@ -21,7 +22,7 @@ include_directories(
aux_source_directory(./src TEST_TOOL_SRC_FILES)
set(TEST_TOOL_TARGET HalTestTool)
add_library(${TEST_TOOL_TARGET} STATIC ${TEST_TOOL_SRC_FILES})
target_link_libraries(${TEST_TOOL_TARGET} KeyControl Log)
target_link_libraries(${TEST_TOOL_TARGET} KeyControl LedControl Log)
if ("${CLANG_TIDY_SUPPORT}" MATCHES "true")
add_custom_target(

View File

@ -53,11 +53,11 @@ class HalCppTest : public HalCpp
public:
HalCppTest() = default;
virtual ~HalCppTest() = default;
StatusCode GetLedHals(std::vector<std::shared_ptr<VLedHal>> &ledHals) override;
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 GetLedHalsTrace(std::vector<std::shared_ptr<VLedHal>> &ledHals);
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
@ -65,47 +65,9 @@ class HalCppMock : public HalCppTest
public:
HalCppMock() = default;
virtual ~HalCppMock() = default;
MOCK_METHOD1(GetLedHalsTrace, StatusCode(std::vector<std::shared_ptr<VLedHal>> &));
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 KeyControlTest : public KeyControl, public VKeyHal
{
public:
KeyControlTest(const std::string &keyName);
virtual ~KeyControlTest() = default;
unsigned int GetStatusCheckPeriodMs(void) override { return PERIPHERAL_CHECK_PERIOD_MS; }
void SetKeyMonitor(std::shared_ptr<VKeyHalMonitor> &monitor) override;
void CheckKeyStatus(void) override;
void KeyEventTrigger(const std::string &keyName, const KeyEvent &event, const unsigned int &timeMs) override;
const std::string GetKeyName(void) override { return mKeyName; }
private:
virtual StatusCode KeyEventTriggerTrace(const std::string &keyName, const KeyEvent &event,
const unsigned int &timeMs)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
private:
const std::string mKeyName;
std::weak_ptr<VKeyHalMonitor> mMonitor;
};
class KeyControlMock : public KeyControlTest
{
public:
KeyControlMock(const std::string &keyName);
virtual ~KeyControlMock() = default;
// void KeyEventTrigger(const std::string &keyName, const KeyEvent &event, const unsigned int &timeMs) override;
void SetKeyEvent(const KeyHalEvent &event);
bool SetKeyClick(const unsigned int &pressingTimeMs = 200);
MOCK_METHOD3(KeyEventTriggerTrace, StatusCode(const std::string &, const KeyEvent &, const unsigned int &));
private:
void KeyEventHappendOnce(std::shared_ptr<KeyControlMock> &mock, const unsigned int &pressingTimeMs);
private:
std::mutex mMutex;
};
class HalTestTool
{
public:
@ -121,14 +83,17 @@ protected:
virtual void DeviceManagerNotice(const std::string &keyName, const unsigned int &pressingTimeMs) {}
private:
void KeyEventHappendOnce(std::shared_ptr<KeyControlMock> &mock, const unsigned int &pressingTimeMs);
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);
std::shared_ptr<KeyControlMock> SearchKey(const std::string &keyName);
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<KeyControlMock> &mock);
void InitKeysMock(std::shared_ptr<VKeyHal> &vMock);
public:
static std::shared_ptr<VKeyHal> MakeKeyHalTest(const std::string &keyName);
private:
std::shared_ptr<HalCppMock> mHalMock;

View File

@ -16,6 +16,7 @@
#define HAL_MAKE_PTR_TEST_H
#include "HalMakePtr.h"
#include "HalTestTool.h"
#include "KeyControlMock.h"
void OverrideHalMakePtrObject(std::shared_ptr<HalCppMock> &halMock);
void CancelOverrideHalMakePtrObject(void);
class HalMakePtrTest : public HalMakePtr

View File

@ -15,13 +15,14 @@
#include "HalTestTool.h"
#include "HalMakePtrTest.h"
#include "ILog.h"
#include "KeyControlMock.h"
#include <thread>
StatusCode HalCppTest::GetLedHals(std::vector<std::shared_ptr<VLedHal>> &ledHals)
StatusCode HalCppTest::GetAllLeds(std::map<std::string, std::shared_ptr<VLedHal>> &allLeds)
{
LogInfo("HalCppTest::GetLedHals\n");
StatusCode code = GetLedHalsTrace(ledHals);
LogInfo("HalCppTest::GetAllLeds\n");
StatusCode code = GetAllLedsTrace(allLeds);
if (StatusCodeEqual(code, "STATUS_CODE_VIRTUAL_FUNCTION")) {
return HalCpp::GetLedHals(ledHals);
return HalCpp::GetAllLeds(allLeds);
}
return code;
}
@ -34,9 +35,9 @@ StatusCode HalCppTest::GetAllKeys(std::map<std::string, std::shared_ptr<VKeyHal>
}
return code;
}
StatusCode HalCppTest::GetLedHalsTrace(std::vector<std::shared_ptr<VLedHal>> &ledHals)
StatusCode HalCppTest::GetAllLedsTrace(std::map<std::string, std::shared_ptr<VLedHal>> &allLeds)
{
LogInfo("HalCppTest::GetLedHalsTrace\n");
LogInfo("HalCppTest::GetAllLedsTrace\n");
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
StatusCode HalCppTest::GetAllKeysTrace(std::map<std::string, std::shared_ptr<VKeyHal>> &allKeys)
@ -69,54 +70,6 @@ void KeyControlTest::KeyEventTrigger(const std::string &keyName, const KeyEvent
}
monitor->KeyEventHappened(keyName, static_cast<VirtualKeyEvent>(event), timeMs);
}
KeyControlMock::KeyControlMock(const std::string &keyName) : KeyControlTest(keyName)
{
//
}
void KeyControlMock::SetKeyEvent(const KeyHalEvent &event)
{
//
KeyHalEventTrigger(event);
}
bool KeyControlMock::SetKeyClick(const unsigned int &pressingTimeMs)
{
if (mMutex.try_lock()) {
auto keyClickThread = [=](std::shared_ptr<KeyControlMock> key) {
KeyEventHappendOnce(key, pressingTimeMs);
key->KeyHalEventTrigger(KeyHalEvent::PRESSING);
std::this_thread::sleep_for(std::chrono::milliseconds(pressingTimeMs));
key->KeyHalEventTrigger(KeyHalEvent::NOT_PRESSING);
mMutex.unlock();
};
std::shared_ptr<KeyControl> tmp = KeyControl::shared_from_this();
std::shared_ptr<KeyControlMock> key = std::dynamic_pointer_cast<KeyControlMock>(tmp);
std::thread clickThread = std::thread(keyClickThread, key);
clickThread.detach();
return true;
}
LogWarning("SetKeyClick failed, becase key was lock.\n");
return false;
}
void KeyControlMock::KeyEventHappendOnce(std::shared_ptr<KeyControlMock> &mock, const unsigned int &pressingTimeMs)
{
EXPECT_CALL(*mock.get(), KeyEventTriggerTrace(_, _, _))
.WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION))));
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(), KeyEventTriggerTrace(mock->GetKeyName(), KeyEvent::SHORT_CLICK, _))
.Times(CLICK_EVENT_HAPPENED_ONLY_ONCE)
.WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION))));
}
if (KEY_ACTION_HOLD_DWON <= pressingTimeMs) {
EXPECT_CALL(*mock.get(), KeyEventTriggerTrace(mock->GetKeyName(), KeyEvent::HOLD_DOWN, _))
.Times(AtLeast(1))
.WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION))));
EXPECT_CALL(*mock.get(), KeyEventTriggerTrace(mock->GetKeyName(), KeyEvent::HOLD_UP, _))
.Times(HOLD_UP_EVENT_HAPPENED_ONLY_ONCE)
.WillOnce(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION))));
}
}
void HalTestTool::Init(void)
{
mHalMock = std::make_shared<HalCppMock>();
@ -135,27 +88,43 @@ void HalTestTool::SetAllKeysResult(std::map<std::string, std::shared_ptr<VKeyHal
}
void HalTestTool::SetKeyEvent(const std::string keyName, const KeyHalEvent &event)
{
std::shared_ptr<KeyControlMock> key = SearchKey(keyName);
std::shared_ptr<VKeyHal> key = SearchKey(keyName);
if (!key) {
LogError("Can't set key event, key not found.\n");
return;
}
key->SetKeyEvent(event);
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<KeyControlMock> key = SearchKey(keyName);
std::shared_ptr<VKeyHal> key = SearchKey(keyName);
if (!key) {
LogError("Can't set key event, key not found.\n");
return;
}
if (key->SetKeyClick(pressingTimeMs)) {
// KeyEventHappendOnce(key, pressingTimeMs);
std::shared_ptr<KeyControlMock> keyMock = std::dynamic_pointer_cast<KeyControlMock>(key);
if (keyMock) {
if (keyMock->SetKeyClick(pressingTimeMs)) {
DeviceManagerNotice(keyName, pressingTimeMs);
}
}
void HalTestTool::KeyEventHappendOnce(std::shared_ptr<KeyControlMock> &mock, const unsigned int &pressingTimeMs)
else {
LogWarning("Key mock error.\n");
}
}
void HalTestTool::KeyEventHappendOnce(std::shared_ptr<VKeyHal> &vMock, const unsigned int &pressingTimeMs)
{
std::shared_ptr<KeyControlMock> mock = std::dynamic_pointer_cast<KeyControlMock>(vMock);
if (!mock) {
LogError("vMock error.\n");
return;
}
EXPECT_CALL(*mock.get(), KeyEventTriggerTrace(_, _, _))
.WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION))));
constexpr int CLICK_EVENT_HAPPENED_ONLY_ONCE = 1;
@ -177,7 +146,7 @@ void HalTestTool::SetAllKeysResult(std::shared_ptr<HalCppMock> &mock,
.WillRepeatedly(DoAll(SetArgReferee<0>(allKeys), Return(CreateStatusCode(STATUS_CODE_OK))));
InitAllKeysMock(allKeys);
}
std::shared_ptr<KeyControlMock> HalTestTool::SearchKey(const std::string &keyName)
std::shared_ptr<VKeyHal> HalTestTool::SearchKey(const std::string &keyName)
{
std::shared_ptr<KeyControlMock> mock;
std::map<std::string, std::shared_ptr<VKeyHal>>::iterator iter;
@ -195,24 +164,35 @@ void HalTestTool::InitAllKeysMock(std::map<std::string, std::shared_ptr<VKeyHal>
std::map<std::string, std::shared_ptr<VKeyHal>>::iterator iter;
for (iter = allKeys.begin(); iter != allKeys.end(); ++iter) {
std::shared_ptr<VKeyHal> keyHal = iter->second;
std::shared_ptr<KeyControlMock> mock = std::dynamic_pointer_cast<KeyControlMock>(keyHal);
if (mock) {
InitKeysMock(mock);
}
else {
LogWarning("Invalid key mock.\n");
InitKeysMock(keyHal);
// std::shared_ptr<KeyControlMock> mock = std::dynamic_pointer_cast<KeyControlMock>(keyHal);
// if (mock) {
// InitKeysMock(mock);
// }
// else {
// LogWarning("Invalid key mock.\n");
// }
}
}
}
void HalTestTool::InitKeysMock(std::shared_ptr<KeyControlMock> &mock)
void HalTestTool::InitKeysMock(std::shared_ptr<VKeyHal> &vMock)
{
std::shared_ptr<KeyControlMock> mock = std::dynamic_pointer_cast<KeyControlMock>(vMock);
if (!mock) {
LogError("vMock error.\n");
return;
}
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)
{
EXPECT_CALL(*mock.get(), GetLedHalsTrace(_))
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))));
}
std::shared_ptr<VKeyHal> HalTestTool::MakeKeyHalTest(const std::string &keyName)
{
std::shared_ptr<VKeyHal> key = std::make_shared<KeyControlMock>(keyName);
return key;
}

View File

@ -0,0 +1,69 @@
/*
* 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 "KeyControlMock.h"
#include "ILog.h"
KeyControlMock::KeyControlMock(const std::string &keyName) : KeyControlTest(keyName)
{
//
}
void KeyControlMock::SetKeyEvent(const KeyHalEvent &event)
{
//
KeyHalEventTrigger(event);
}
bool KeyControlMock::SetKeyClick(const unsigned int &pressingTimeMs)
{
if (mMutex.try_lock()) {
auto keyClickThread = [=](std::shared_ptr<KeyControlMock> key) {
KeyEventHappendOnce(key, pressingTimeMs);
key->KeyHalEventTrigger(KeyHalEvent::PRESSING);
std::this_thread::sleep_for(std::chrono::milliseconds(pressingTimeMs));
key->KeyHalEventTrigger(KeyHalEvent::NOT_PRESSING);
mMutex.unlock();
};
std::shared_ptr<KeyControl> tmp = KeyControl::shared_from_this();
std::shared_ptr<KeyControlMock> key = std::dynamic_pointer_cast<KeyControlMock>(tmp);
std::thread clickThread = std::thread(keyClickThread, key);
clickThread.detach();
return true;
}
LogWarning("SetKeyClick failed, becase key was lock.\n");
return false;
}
void KeyControlMock::KeyEventHappendOnce(std::shared_ptr<KeyControlMock> &mock, const unsigned int &pressingTimeMs)
{
EXPECT_CALL(*mock.get(), KeyEventTriggerTrace(_, _, _))
.WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION))));
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(), KeyEventTriggerTrace(mock->GetKeyName(), KeyEvent::SHORT_CLICK, _))
.Times(CLICK_EVENT_HAPPENED_ONLY_ONCE)
.WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION))));
return;
}
if (KEY_ACTION_HOLD_DWON <= pressingTimeMs) {
EXPECT_CALL(*mock.get(), KeyEventTriggerTrace(mock->GetKeyName(), KeyEvent::HOLD_DOWN, _))
.Times(AtLeast(1))
.WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION))));
EXPECT_CALL(*mock.get(), KeyEventTriggerTrace(mock->GetKeyName(), KeyEvent::HOLD_UP, _))
.Times(HOLD_UP_EVENT_HAPPENED_ONLY_ONCE)
.WillOnce(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION))));
return;
}
constexpr int KEY_SHOULD_NOT_TRIGGER_WHEN_NOBODY_SET_KEY_RESULT = 0;
EXPECT_CALL(*mock.get(), KeyEventTriggerTrace(mock->GetKeyName(), _, _))
.Times(KEY_SHOULD_NOT_TRIGGER_WHEN_NOBODY_SET_KEY_RESULT);
}

View File

@ -0,0 +1,59 @@
/*
* 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 KEY_CONTROL_MOCK_H
#define KEY_CONTROL_MOCK_H
#include "HalTestTool.h"
#include "IHalCpp.h"
#include "KeyControl.h"
#include <thread>
class KeyControlTest : public KeyControl, public VKeyHal
{
public:
KeyControlTest(const std::string &keyName);
virtual ~KeyControlTest() = default;
unsigned int GetStatusCheckPeriodMs(void) override { return PERIPHERAL_CHECK_PERIOD_MS; }
void SetKeyMonitor(std::shared_ptr<VKeyHalMonitor> &monitor) override;
void CheckKeyStatus(void) override;
void KeyEventTrigger(const std::string &keyName, const KeyEvent &event, const unsigned int &timeMs) override;
const std::string GetKeyName(void) override { return mKeyName; }
private:
virtual StatusCode KeyEventTriggerTrace(const std::string &keyName, const KeyEvent &event,
const unsigned int &timeMs)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
private:
const std::string mKeyName;
std::weak_ptr<VKeyHalMonitor> mMonitor;
};
class KeyControlMock : public KeyControlTest
{
public:
KeyControlMock(const std::string &keyName);
virtual ~KeyControlMock() = default;
// void KeyEventTrigger(const std::string &keyName, const KeyEvent &event, const unsigned int &timeMs) override;
void SetKeyEvent(const KeyHalEvent &event);
bool SetKeyClick(const unsigned int &pressingTimeMs = 200);
MOCK_METHOD3(KeyEventTriggerTrace, StatusCode(const std::string &, const KeyEvent &, const unsigned int &));
private:
void KeyEventHappendOnce(std::shared_ptr<KeyControlMock> &mock, const unsigned int &pressingTimeMs);
private:
std::mutex mMutex;
};
#endif

View File

@ -0,0 +1,23 @@
/*
* 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 "LedControlMock.h"
LedControlTest::LedControlTest(const std::string &ledName) : mLedName(ledName)
{
//
}
LedControlMock::LedControlMock(const std::string &ledName) : LedControlTest(ledName)
{
//
}

View File

@ -0,0 +1,34 @@
/*
* 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 LED_CONTROL_MOCK_H
#define LED_CONTROL_MOCK_H
#include "IHalCpp.h"
#include "LedControl.h"
class LedControlTest : public LedControl, public VLedHal
{
public:
LedControlTest(const std::string &ledName);
virtual ~LedControlTest() = default;
private:
const std::string mLedName;
};
class LedControlMock : public LedControlTest
{
public:
LedControlMock(const std::string &ledName);
virtual ~LedControlMock() = default;
};
#endif

View File

@ -53,7 +53,7 @@ public:
private:
void CreateAllKeysMcok(void)
{
std::shared_ptr<KeyControlMock> key = std::make_shared<KeyControlMock>(KEY_TEST);
std::shared_ptr<VKeyHal> key = HalTestTool::MakeKeyHalTest(KEY_TEST);
mAllKeysMock[KEY_TEST] = key;
}
void DestroyAllKeysMock(void)
@ -85,6 +85,23 @@ TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_KeyShortPress)
IDeviceManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/DeviceManagerTest
// --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_KeyInvalidShort
/**
* @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_KeyInvalidShort)
{
SetAllKeysResult(mAllKeysMock);
IDeviceManager::GetInstance()->Init();
std::shared_ptr<VKeyMonitor> monitor = std::make_shared<KeyMonitorMock>();
IDeviceManager::GetInstance()->SetAllKeysMonitor(monitor);
SetKeyClick(KEY_TEST, 50); // 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
@ -115,4 +132,12 @@ TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_SetKeyMonitor)
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IDeviceManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/DeviceManagerTest
// --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControLed
TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControLed)
{
// IDeviceManager::GetInstance()->Init();
// std::this_thread::sleep_for(std::chrono::milliseconds(2000));
// IDeviceManager::GetInstance()->UnInit();
}
} // namespace DeviceManagerTest

View File

@ -54,14 +54,18 @@ public:
VLedControl() = default;
virtual ~VLedControl() = default;
virtual bool SetLedState(const LedState &state) { return false; }
virtual void AddLedState(std::shared_ptr<VSingleControl> &control) {}
virtual void CheckState(const unsigned int &period) {}
// virtual void SetHalLedState(const VirtualLedState &state) {}
virtual std::string GetLedName(void) { return "undefine"; }
};
class LedControl : virtual public VLedControl
{
public:
LedControl() = default;
virtual ~LedControl() = default;
void CheckState(const unsigned int &period) override;
void AddLedState(std::shared_ptr<VSingleControl> &state);
void CheckState(const unsigned int &period);
private:
void NewLedStateStart(void);