/* * 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 &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(); DeviceManagerMockInit(mDeviceManagerMock); OverrideDeviceMakePtrObject(mDeviceManagerMock); } void DeviceManagerTestTool::UnInit(void) { mDeviceManagerMock.reset(); mKeyMonitorMock.reset(); CancelOverrideDeviceMakePtrObject(); } std::shared_ptr 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 ledControl = std::make_shared(state, aliveTimeMs, blinkTimeMs); IDeviceManager::GetInstance()->ControlLed(ledName, ledControl); return ledControl; } void DeviceManagerTestTool::DestoryLedControl(std::shared_ptr &ledControl) { std::shared_ptr ledControlMock = std::dynamic_pointer_cast(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 &mock) { auto getKeyMonitor = [=](std::shared_ptr &monitor) { LogInfo("mKeyMonitorMock get.\n"); mKeyMonitorMock = std::dynamic_pointer_cast(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 &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(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(KeyEvent::HOLD_DOWN), _)) .Times(AtLeast(1)) .WillRepeatedly(DoAll(Return())); EXPECT_CALL(*mock.get(), KeyEventReport(keyName, static_cast(KeyEvent::HOLD_UP), _)) .Times(HOLD_UP_EVENT_HAPPENED_ONLY_ONCE) .WillOnce(DoAll(Return())); } }