235 lines
8.8 KiB
C++
235 lines
8.8 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 "HalTestTool.h"
|
|
#include "HalCppMock.h"
|
|
#include "HalMakePtrTest.h"
|
|
#include "ILog.h"
|
|
#include "KeyControl.h"
|
|
#include "KeyControlMock.h"
|
|
#include "LedControlMock.h"
|
|
#include <thread>
|
|
void HalTestTool::Init(void)
|
|
{
|
|
mHalMock = std::make_shared<HalCppMock>();
|
|
HalMockInit(mHalMock);
|
|
std::shared_ptr<HalCppMock> mock = std::dynamic_pointer_cast<HalCppMock>(mHalMock);
|
|
OverrideHalMakePtrObject(mock);
|
|
}
|
|
void HalTestTool::UnInit(void)
|
|
{
|
|
mHalMock.reset();
|
|
CancelOverrideHalMakePtrObject();
|
|
mAllKeys.clear();
|
|
mAllLeds.clear();
|
|
}
|
|
void HalTestTool::SetAllKeysResult(std::map<std::string, std::shared_ptr<VKeyHal>> &allKeys)
|
|
{
|
|
std::shared_ptr<IHalCpp> halMock = mHalMock;
|
|
SetAllKeysResult(halMock, allKeys);
|
|
mAllKeys = allKeys;
|
|
}
|
|
void HalTestTool::SetAllLedsResult(std::map<std::string, std::shared_ptr<VLedHal>> &allLeds)
|
|
{
|
|
std::shared_ptr<IHalCpp> halMock = mHalMock;
|
|
SetAllLedsResult(halMock, allLeds);
|
|
mAllLeds = allLeds;
|
|
}
|
|
// 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);
|
|
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) {
|
|
if (keyMock->SetKeyClick(pressingTimeMs)) {
|
|
DeviceManagerNotice(keyName, pressingTimeMs);
|
|
}
|
|
}
|
|
else {
|
|
LogWarning("Key mock error.\n");
|
|
}
|
|
}
|
|
void HalTestTool::SetLedStateExpectations(const std::string &ledName, const LedState &state)
|
|
{
|
|
std::shared_ptr<VLedHal> led = SearchLed(ledName);
|
|
if (!led) {
|
|
LogError("Can't set led state, led not found.\n");
|
|
return;
|
|
}
|
|
std::shared_ptr<LedControlMock> ledMock = std::dynamic_pointer_cast<LedControlMock>(led);
|
|
if (ledMock) {
|
|
LedControlMock::SetLedStateMock(ledMock, state);
|
|
}
|
|
else {
|
|
LogWarning("led 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;
|
|
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))));
|
|
}
|
|
}
|
|
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;
|
|
iter = mAllKeys.find(keyName);
|
|
if (iter != mAllKeys.end()) {
|
|
mock = std::dynamic_pointer_cast<KeyControlMock>(mAllKeys[keyName]);
|
|
}
|
|
else {
|
|
LogWarning("Can't found the key control.\n");
|
|
}
|
|
return mock;
|
|
}
|
|
void HalTestTool::InitAllKeysMock(std::map<std::string, std::shared_ptr<VKeyHal>> &allKeys)
|
|
{
|
|
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;
|
|
InitKeysMock(keyHal);
|
|
}
|
|
}
|
|
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<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);
|
|
}
|
|
void HalTestTool::SetAllLedsResult(std::shared_ptr<IHalCpp> &vMock,
|
|
std::map<std::string, std::shared_ptr<VLedHal>> &allLeds)
|
|
{
|
|
std::shared_ptr<HalCppMock> mock = std::dynamic_pointer_cast<HalCppMock>(vMock);
|
|
if (!mock) {
|
|
LogError("vMock error.\n");
|
|
return;
|
|
}
|
|
/**
|
|
* @brief
|
|
* Note: The SetArgReference function cannot be used here, as it will prevent the smart pointer from being released
|
|
* within the TEST range, resulting in an error only being reported after all test cases have finished running.
|
|
* Can't use:
|
|
* EXPECT_CALL(*mock.get(), GetAllLedsTrace(_))
|
|
* .WillRepeatedly(DoAll(SetArgReferee<0>(allLeds), Return(CreateStatusCode(STATUS_CODE_OK))));
|
|
*/
|
|
auto getAllLeds = [=, &allLeds](std::map<std::string, std::shared_ptr<VLedHal>> &setAllLeds) {
|
|
setAllLeds = allLeds;
|
|
};
|
|
EXPECT_CALL(*mock.get(), GetAllLedsTrace(_))
|
|
.WillRepeatedly(DoAll(WithArgs<0>(Invoke(getAllLeds)), Return(CreateStatusCode(STATUS_CODE_OK))));
|
|
InitAllLedsMock(allLeds);
|
|
}
|
|
std::shared_ptr<VLedHal> HalTestTool::SearchLed(const std::string &ledName)
|
|
{
|
|
std::shared_ptr<LedControlMock> mock;
|
|
std::map<std::string, std::shared_ptr<VLedHal>>::iterator iter;
|
|
iter = mAllLeds.find(ledName);
|
|
if (iter != mAllLeds.end()) {
|
|
mock = std::dynamic_pointer_cast<LedControlMock>(mAllLeds[ledName]);
|
|
}
|
|
else {
|
|
LogWarning("Can't found the led control.\n");
|
|
}
|
|
return mock;
|
|
}
|
|
void HalTestTool::InitAllLedsMock(std::map<std::string, std::shared_ptr<VLedHal>> &allLeds)
|
|
{
|
|
std::map<std::string, std::shared_ptr<VLedHal>>::iterator iter;
|
|
for (iter = allLeds.begin(); iter != allLeds.end(); ++iter) {
|
|
std::shared_ptr<VLedHal> ledHal = iter->second;
|
|
InitLedsMock(ledHal);
|
|
}
|
|
}
|
|
void HalTestTool::InitLedsMock(std::shared_ptr<VLedHal> &vMock)
|
|
{
|
|
std::shared_ptr<LedControlMock> mock = std::dynamic_pointer_cast<LedControlMock>(vMock);
|
|
if (!mock) {
|
|
LogError("vMock error.\n");
|
|
return;
|
|
}
|
|
constexpr int LED_SHOULD_NOT_BE_CONTROLED_WHEN_NOBODY_CONTROL_IT = 0;
|
|
EXPECT_CALL(*mock.get(), SetLedStateTrace(_)).Times(LED_SHOULD_NOT_BE_CONTROLED_WHEN_NOBODY_CONTROL_IT);
|
|
}
|
|
std::shared_ptr<VKeyHal> HalTestTool::MakeKeyHalTest(const std::string &keyName)
|
|
{
|
|
std::shared_ptr<VKeyHal> key = std::make_shared<KeyControlMock>(keyName);
|
|
return key;
|
|
}
|
|
std::shared_ptr<VLedHal> HalTestTool::MakeLedHalTest(const std::string &ledName)
|
|
{
|
|
std::shared_ptr<VLedHal> led = std::make_shared<LedControlMock>(ledName);
|
|
return led;
|
|
} |