mirror of
https://gitee.com/jiuyilian/embedded-framework.git
synced 2025-01-06 10:16:51 -05:00
Add:KeyControl test code.
This commit is contained in:
parent
1ada288d3f
commit
48a5c6f4ba
|
@ -24,7 +24,6 @@ using VirtualKeyEvent = unsigned char;
|
|||
constexpr int INVALID_PERIOD = -1;
|
||||
constexpr int PERIPHERAL_CHECK_PERIOD_MS = 100;
|
||||
constexpr int IMEI_LEN = 15;
|
||||
// constexpr long int KEY_DO_NOT_HOLD_PRESSING = -1;
|
||||
void CreateHalCppModule(void);
|
||||
class VKeyHalMonitor
|
||||
{
|
||||
|
@ -41,7 +40,7 @@ public:
|
|||
VKeyHal() = default;
|
||||
virtual ~VKeyHal() = default;
|
||||
virtual void CheckKeyStatus(void) {}
|
||||
virtual const std::string GetKeyName(void) { return "undefine"; }
|
||||
// virtual const std::string GetKeyName(void) { return "undefine"; }
|
||||
virtual void GetHoldPressingTimeMs(long int &holdTimeMs, VirtualKeyEvent &event) {}
|
||||
virtual void SetKeyMonitor(std::shared_ptr<VKeyHalMonitor> &monitor) {}
|
||||
};
|
||||
|
|
|
@ -84,10 +84,10 @@ void KeyManager::GetAllKeysState(std::map<std::string, KeyStatus> &status)
|
|||
}
|
||||
void KeyManager::SetAllKeysMonitor(void)
|
||||
{
|
||||
std::shared_ptr<VKeyHalMonitor> monitor = shared_from_this();
|
||||
std::map<std::string, std::shared_ptr<VKeyHal>>::iterator iter;
|
||||
for (iter = mAllKeyHal.begin(); iter != mAllKeyHal.end(); ++iter) {
|
||||
std::shared_ptr<VKeyHal> keyHal = iter->second;
|
||||
std::shared_ptr<VKeyHalMonitor> monitor = shared_from_this();
|
||||
keyHal->SetKeyMonitor(monitor);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -71,20 +71,22 @@ public:
|
|||
class KeyControlTest : public KeyControl, public VKeyHal
|
||||
{
|
||||
public:
|
||||
KeyControlTest() = default;
|
||||
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:
|
||||
std::shared_ptr<VKeyHalMonitor> mMonitor;
|
||||
const std::string mKeyName;
|
||||
std::weak_ptr<VKeyHalMonitor> mMonitor;
|
||||
};
|
||||
class KeyControlMock : public KeyControlTest
|
||||
{
|
||||
public:
|
||||
KeyControlMock() = default;
|
||||
KeyControlMock(const std::string &keyName);
|
||||
virtual ~KeyControlMock() = default;
|
||||
void SetKeyEvent(const KeyHalEvent &event);
|
||||
};
|
||||
|
@ -97,6 +99,7 @@ public:
|
|||
void UnInit(void);
|
||||
void SetAllKeysResult(std::map<std::string, std::shared_ptr<VKeyHal>> &allKeys);
|
||||
void SetKeyEvent(const std::string keyName, const KeyHalEvent &event);
|
||||
void SetKeyClick(const std::string &keyName, const unsigned int &pressingTimeMs = 200);
|
||||
|
||||
private:
|
||||
void HalMockInit(std::shared_ptr<HalCppMock> &mock);
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
#include "HalTestTool.h"
|
||||
#include "HalMakePtrTest.h"
|
||||
#include "ILog.h"
|
||||
#include <thread>
|
||||
StatusCode HalCppTest::GetLedHals(std::vector<std::shared_ptr<VLedHal>> &ledHals)
|
||||
{
|
||||
LogInfo("HalCppTest::GetLedHals\n");
|
||||
|
@ -43,6 +44,10 @@ StatusCode HalCppTest::GetAllKeysTrace(std::map<std::string, std::shared_ptr<VKe
|
|||
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)
|
||||
{
|
||||
//
|
||||
|
@ -55,8 +60,17 @@ void KeyControlTest::CheckKeyStatus(void)
|
|||
}
|
||||
void KeyControlTest::KeyEventTrigger(const std::string &keyName, const KeyEvent &event, const unsigned int &timeMs)
|
||||
{
|
||||
LogInfo("KeyEventTrigger time = %u\n", timeMs);
|
||||
mMonitor->KeyEventHappened(keyName, static_cast<VirtualKeyEvent>(event), timeMs);
|
||||
LogInfo("KeyEventTrigger keyName = %s, event = %s, time = %u\n", keyName.c_str(), PrintKeyEvent(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)
|
||||
{
|
||||
//
|
||||
}
|
||||
void KeyControlMock::SetKeyEvent(const KeyHalEvent &event)
|
||||
{
|
||||
|
@ -71,8 +85,6 @@ void HalTestTool::Init(void)
|
|||
}
|
||||
void HalTestTool::UnInit(void)
|
||||
{
|
||||
// std::shared_ptr<IHalCpp> impl = std::make_shared<IHalCpp>();
|
||||
// IHalCpp::GetInstance(&impl);
|
||||
mHalMock.reset();
|
||||
CancelOverrideHalMakePtrObject();
|
||||
}
|
||||
|
@ -90,6 +102,21 @@ void HalTestTool::SetKeyEvent(const std::string keyName, const KeyHalEvent &even
|
|||
}
|
||||
key->SetKeyEvent(event);
|
||||
}
|
||||
void HalTestTool::SetKeyClick(const std::string &keyName, const unsigned int &pressingTimeMs)
|
||||
{
|
||||
std::shared_ptr<KeyControlMock> key = SearchKey(keyName);
|
||||
if (!key) {
|
||||
LogError("Can't set key event, key not found.\n");
|
||||
return;
|
||||
}
|
||||
auto keyClickThread = [=](std::shared_ptr<KeyControlMock> key) {
|
||||
key->KeyHalEventTrigger(KeyHalEvent::PRESSING);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(pressingTimeMs));
|
||||
key->KeyHalEventTrigger(KeyHalEvent::NOT_PRESSING);
|
||||
};
|
||||
std::thread clickThread = std::thread(keyClickThread, key);
|
||||
clickThread.detach();
|
||||
}
|
||||
void HalTestTool::SetAllKeysResult(std::shared_ptr<HalCppMock> &mock,
|
||||
std::map<std::string, std::shared_ptr<VKeyHal>> &allKeys)
|
||||
{
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
#include <thread>
|
||||
namespace DeviceManagerTest
|
||||
{
|
||||
const char *KEY_TEST = "TEST";
|
||||
class DeviceManagerTest : public testing::Test, public HalTestTool
|
||||
{
|
||||
public:
|
||||
|
@ -33,10 +34,7 @@ public:
|
|||
static void TearDownTestCase() { ILogUnInit(); }
|
||||
virtual void SetUp()
|
||||
{
|
||||
// mLinuxTest = LinuxTest::CreateLinuxTest();
|
||||
// std::shared_ptr<LinuxApiMock> test = mLinuxTest;
|
||||
// LinuxApiMock::GetInstance(&test);
|
||||
// LinuxApiMock::GetInstance()->Init();
|
||||
CreateAllKeysMcok();
|
||||
HalTestTool::Init();
|
||||
CreateHalCppModule();
|
||||
CreateDeviceManagerModule();
|
||||
|
@ -44,28 +42,36 @@ public:
|
|||
}
|
||||
virtual void TearDown()
|
||||
{
|
||||
// LinuxApiMock::GetInstance()->UnInit();
|
||||
// mLinuxTest = std::make_shared<LinuxTest>();
|
||||
// std::shared_ptr<LinuxApiMock> test = std::make_shared<LinuxApiMock>();
|
||||
// LinuxApiMock::GetInstance(&test);
|
||||
HalTestTool::UnInit();
|
||||
// DestroyMcuManager();
|
||||
DestroyDeviceManagerModule();
|
||||
DestroyAllKeysMock();
|
||||
}
|
||||
|
||||
private:
|
||||
void CreateAllKeysMcok(void)
|
||||
{
|
||||
std::shared_ptr<KeyControlMock> key = std::make_shared<KeyControlMock>(KEY_TEST);
|
||||
mAllKeysMock[KEY_TEST] = key;
|
||||
}
|
||||
void DestroyAllKeysMock(void)
|
||||
{
|
||||
//
|
||||
mAllKeysMock.clear();
|
||||
}
|
||||
|
||||
public:
|
||||
// std::shared_ptr<LinuxTest> mLinuxTest;
|
||||
protected:
|
||||
std::map<std::string, std::shared_ptr<VKeyHal>> mAllKeysMock;
|
||||
};
|
||||
// ../output_files/test/bin/DeviceManagerTest --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_Demo
|
||||
TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_Demo)
|
||||
{
|
||||
std::map<std::string, std::shared_ptr<VKeyHal>> allKeys;
|
||||
std::shared_ptr<KeyControlMock> key = std::make_shared<KeyControlMock>();
|
||||
allKeys["test"] = key;
|
||||
SetAllKeysResult(allKeys);
|
||||
SetAllKeysResult(mAllKeysMock);
|
||||
IDeviceManager::GetInstance()->Init();
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
||||
SetKeyEvent("test", KeyHalEvent::PRESSING);
|
||||
// SetKeyEvent(KEY_TEST, KeyHalEvent::PRESSING);
|
||||
SetKeyClick(KEY_TEST);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
||||
IDeviceManager::GetInstance()->UnInit();
|
||||
}
|
||||
|
|
|
@ -17,8 +17,6 @@
|
|||
#include <functional>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
constexpr long int KEY_DO_NOT_HOLD_PRESSING = -1;
|
||||
constexpr int KEY_ACTION_LONG_CLICK = 1000 * 5;
|
||||
constexpr int KEY_ACTION_SHORT_CLICK = 200;
|
||||
constexpr int KEY_ACTION_HOLD_DWON = 500;
|
||||
constexpr long int KEY_NOT_PRESSING = -1;
|
||||
|
@ -42,25 +40,19 @@ public:
|
|||
virtual ~VKeyControl() = default;
|
||||
virtual const std::string GetKeyName(void) { return "undefine"; }
|
||||
virtual unsigned int GetStatusCheckPeriodMs(void) { return 0; }
|
||||
virtual void KeyHalEventTrigger(const KeyHalEvent &event) {}
|
||||
virtual void KeyEventTrigger(const std::string &keyName, const KeyEvent &event, const unsigned int &timeMs) {}
|
||||
virtual void TimerKeyEventTrigger(const KeyHalEvent &event) {}
|
||||
virtual long int GetHoldPressingTimeMs(void) { return KEY_DO_NOT_HOLD_PRESSING; }
|
||||
};
|
||||
// using KeyActionReport = std::function<void(const std::string &, const KeyEvent &, const unsigned int &)>;
|
||||
class KeyControl : public VKeyControl, public std::enable_shared_from_this<KeyControl>
|
||||
{
|
||||
public:
|
||||
KeyControl();
|
||||
// KeyControl(std::shared_ptr<VKeyControl> &keyHal, const KeyActionReport &keyAction,
|
||||
// const long int &longClickTime = KEY_ACTION_LONG_CLICK);
|
||||
~KeyControl();
|
||||
void KeyHalEventTrigger(const KeyHalEvent &event) override;
|
||||
void TimerKeyEventTrigger(const KeyHalEvent &event) override;
|
||||
long int GetHoldPressingTimeMs(void) override;
|
||||
virtual ~KeyControl();
|
||||
void Init(void);
|
||||
void UnInit(void);
|
||||
void ActionReport(const std::string &key, const KeyHalEvent &keyEvent);
|
||||
void KeyHalEventHandle(const std::string &key, const KeyHalEvent &keyEvent);
|
||||
void TimerKeyEventTrigger(const KeyHalEvent &event);
|
||||
void KeyHalEventTrigger(const KeyHalEvent &event);
|
||||
long int GetHoldPressingTimeMs(void);
|
||||
|
||||
private:
|
||||
void KeyPressingTrigger(const std::string &key);
|
||||
|
@ -72,4 +64,5 @@ private:
|
|||
long int mPressingTime;
|
||||
long int mLongClickTime;
|
||||
};
|
||||
const char *PrintKeyEvent(const KeyEvent &event);
|
||||
#endif
|
|
@ -18,35 +18,13 @@ constexpr long int KEY_PRESSING = 0;
|
|||
constexpr unsigned int NOT_A_HOLD_KEY_ACTION = 0;
|
||||
KeyControl::KeyControl()
|
||||
{
|
||||
// mKeyHal = nullptr;
|
||||
// mKeyActionReport = nullptr;
|
||||
mPressingTime = KEY_NOT_PRESSING;
|
||||
mLongClickTime = 0;
|
||||
}
|
||||
// KeyControl::KeyControl(std::shared_ptr<VKeyControl> &keyHal, const KeyActionReport &keyAction,
|
||||
// const long int &longClickTime)
|
||||
// : mKeyActionReport(keyAction), mLongClickTime(longClickTime)
|
||||
// {
|
||||
// mPressingTime = KEY_NOT_PRESSING;
|
||||
// }
|
||||
KeyControl::~KeyControl() {}
|
||||
void KeyControl::KeyHalEventTrigger(const KeyHalEvent &event)
|
||||
{
|
||||
std::lock_guard<std::mutex> locker(mMutex);
|
||||
ActionReport(GetKeyName(), event);
|
||||
}
|
||||
void KeyControl::TimerKeyEventTrigger(const KeyHalEvent &event)
|
||||
{
|
||||
std::lock_guard<std::mutex> locker(mMutex);
|
||||
if (IsKeyPressing()) {
|
||||
ActionReport(GetKeyName(), event);
|
||||
}
|
||||
}
|
||||
long int KeyControl::GetHoldPressingTimeMs(void) { return mPressingTime; }
|
||||
bool KeyControl::IsKeyPressing(void) { return mPressingTime >= KEY_PRESSING ? true : false; }
|
||||
// void KeyControl::Init(void) { SetKeyHalOwner(shared_from_this()); }
|
||||
void KeyControl::UnInit(void) {}
|
||||
void KeyControl::ActionReport(const std::string &key, const KeyHalEvent &keyEvent)
|
||||
void KeyControl::KeyHalEventHandle(const std::string &key, const KeyHalEvent &keyEvent)
|
||||
{
|
||||
if (KEY_PRESSING <= mPressingTime) {
|
||||
mPressingTime += GetStatusCheckPeriodMs();
|
||||
|
@ -63,6 +41,23 @@ void KeyControl::ActionReport(const std::string &key, const KeyHalEvent &keyEven
|
|||
break;
|
||||
}
|
||||
}
|
||||
void KeyControl::TimerKeyEventTrigger(const KeyHalEvent &event)
|
||||
{
|
||||
std::lock_guard<std::mutex> locker(mMutex);
|
||||
if (IsKeyPressing()) {
|
||||
KeyHalEventHandle(GetKeyName(), event);
|
||||
}
|
||||
}
|
||||
void KeyControl::KeyHalEventTrigger(const KeyHalEvent &event)
|
||||
{
|
||||
std::lock_guard<std::mutex> locker(mMutex);
|
||||
KeyHalEventHandle(GetKeyName(), event);
|
||||
}
|
||||
long int KeyControl::GetHoldPressingTimeMs(void)
|
||||
{
|
||||
//
|
||||
return mPressingTime;
|
||||
}
|
||||
void KeyControl::KeyPressingTrigger(const std::string &key)
|
||||
{
|
||||
if (mLongClickTime <= mPressingTime) // Do not support long click, it should be count in application code.
|
||||
|
@ -84,4 +79,25 @@ void KeyControl::KeyNotPressingTrigger(const std::string &key)
|
|||
KeyEventTrigger(key, KeyEvent::HOLD_UP, mPressingTime);
|
||||
}
|
||||
mPressingTime = KEY_NOT_PRESSING;
|
||||
}
|
||||
const char *PrintKeyEvent(const KeyEvent &event)
|
||||
{
|
||||
switch (event) {
|
||||
case KeyEvent::SHORT_CLICK: {
|
||||
return "SHORT_CLICK";
|
||||
break;
|
||||
}
|
||||
case KeyEvent::HOLD_DOWN: {
|
||||
return "HOLD_DOWN";
|
||||
break;
|
||||
}
|
||||
case KeyEvent::HOLD_UP: {
|
||||
return "HOLD_UP";
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
return "unknown event";
|
||||
break;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user