hunting/test/middleware/DeviceManager/tool/src/DeviceManagerTestTool.cpp
2024-06-06 21:35:36 +08:00

103 lines
4.5 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 "DeviceManagerTestTool.h"
#include "DeviceManagerMakePtrTest.h"
#include "ILog.h"
#include "KeyControl.h"
#include "SingleControlMock.h"
const StatusCode DeviceManagerTool::SetAllKeysMonitor(std::shared_ptr<VKeyMonitor> &monitor)
{
LogInfo("DeviceManagerTool::SetAllKeysMonitor\n");
StatusCode code = SetAllKeysMonitorTrace(monitor);
if (StatusCodeEqual(code, "STATUS_CODE_VIRTUAL_FUNCTION")) {
return DeviceManager::SetAllKeysMonitor(monitor);
}
return code;
}
void DeviceManagerTestTool::Init(void)
{
mDeviceManagerMock = std::make_shared<DeviceManagerMock>();
DeviceManagerMockInit(mDeviceManagerMock);
OverrideDeviceMakePtrObject(mDeviceManagerMock);
}
void DeviceManagerTestTool::UnInit(void)
{
mDeviceManagerMock.reset();
mKeyMonitorMock.reset();
CancelOverrideDeviceMakePtrObject();
}
std::shared_ptr<LedControlContext> DeviceManagerTestTool::ControlLed(const std::string &ledName, const LedState &state,
const unsigned int &aliveTimeMs,
const unsigned int &blinkTimeMs)
{
HalTestTool::SetLedStateExpectations(ledName, state, aliveTimeMs, blinkTimeMs);
std::shared_ptr<LedControlContext> ledControl =
std::make_shared<SingleControlMock>(state, aliveTimeMs, blinkTimeMs);
IDeviceManager::GetInstance()->ControlLed(ledName, ledControl);
return ledControl;
}
void DeviceManagerTestTool::DestoryLedControl(std::shared_ptr<LedControlContext> &ledControl)
{
std::shared_ptr<SingleControlMock> ledControlMock = std::dynamic_pointer_cast<SingleControlMock>(ledControl);
if (ledControlMock) {
ledControlMock->DeleteState();
}
else {
LogWarning("DestoryLedCtx failed.\n");
}
}
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) {
LogInfo("mKeyMonitorMock get.\n");
mKeyMonitorMock = std::dynamic_pointer_cast<KeyMonitorMock>(monitor);
};
constexpr int ONLY_BE_CALLED_ONCE = 1;
EXPECT_CALL(*mock.get(), SetAllKeysMonitorTrace(_))
.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()));
}
}