hunting/test/hal/tool/src/KeyControlMock.cpp
Fancy code 6610558788 Backup
2024-04-28 14:55:38 +08:00

102 lines
4.3 KiB
C++

/*
* 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"
unsigned int KeyControlTest::GetStatusCheckPeriodMs(void)
{
return PERIPHERAL_CHECK_PERIOD_MS;
}
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);
}
const std::string KeyControlTest::GetKeyName(void)
{
return mKeyName;
}
StatusCode KeyControlTest::KeyEventTriggerTrace(const std::string &keyName, const KeyEvent &event,
const unsigned int &timeMs)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
KeyControlMock::KeyControlMock(const std::string &keyName) : KeyControlTest(keyName)
{
}
void KeyControlMock::SetKeyEvent(const KeyHalEvent &event)
{
KeyHalEventTrigger(event);
}
bool KeyControlMock::MockKeyClick(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("MockKeyClick 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);
}