Add:DeviceManager test tool.

This commit is contained in:
Fancy code 2024-02-17 10:17:53 -08:00
parent 73236bba8f
commit 10921b1b57
13 changed files with 335 additions and 12 deletions

View File

@ -31,6 +31,13 @@ typedef struct key_status
const VirtualKeyEvent mKeyEvent; const VirtualKeyEvent mKeyEvent;
const long int mHoldTimeMs; const long int mHoldTimeMs;
} KeyStatus; } KeyStatus;
class VKeyMonitor
{
public:
VKeyMonitor() = default;
virtual ~VKeyMonitor() = default;
void KeyEventReport(const std::string &keyName, const VirtualKeyEvent &event, const unsigned int &timeMs) {}
};
class IDeviceManager class IDeviceManager
{ {
public: public:
@ -41,5 +48,6 @@ public:
virtual const StatusCode UnInit(void); virtual const StatusCode UnInit(void);
virtual const StatusCode SetLedState(const std::string &ledName, const VirtualLedState &currentState, virtual const StatusCode SetLedState(const std::string &ledName, const VirtualLedState &currentState,
const unsigned int &KeepAliveTime, const unsigned int &blinkPeriod); const unsigned int &KeepAliveTime, const unsigned int &blinkPeriod);
virtual const StatusCode SetAllKeysMonitor(std::shared_ptr<VKeyMonitor> &monitor);
}; };
#endif #endif

View File

@ -30,3 +30,7 @@ const StatusCode DeviceManager::UnInit(void)
KeyManager::GetInstance()->UnInit(); KeyManager::GetInstance()->UnInit();
return CreateStatusCode(STATUS_CODE_OK); return CreateStatusCode(STATUS_CODE_OK);
} }
const StatusCode DeviceManager::SetAllKeysMonitor(std::shared_ptr<VKeyMonitor> &monitor)
{
return KeyManager::GetInstance()->SetKeyMonitor(monitor);
}

View File

@ -28,6 +28,7 @@ public:
// const StatusCode SetLedState(const std::string &ledName, const VirtualLedState &currentState, // const StatusCode SetLedState(const std::string &ledName, const VirtualLedState &currentState,
// const unsigned int &keepAliveTime = DEFAULT_KEEP_ALIVE_TIME, // const unsigned int &keepAliveTime = DEFAULT_KEEP_ALIVE_TIME,
// const unsigned int &blinkPeriod = LED_NOT_BLINK) override; // const unsigned int &blinkPeriod = LED_NOT_BLINK) override;
const StatusCode SetAllKeysMonitor(std::shared_ptr<VKeyMonitor> &monitor) override;
private: private:
// std::vector<std::shared_ptr<LedManager>> mLedManagers; // std::vector<std::shared_ptr<LedManager>> mLedManagers;

View File

@ -32,6 +32,10 @@ const StatusCode IDeviceManager::Init(void) { return CreateStatusCode(STATUS_COD
const StatusCode IDeviceManager::UnInit(void) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); } const StatusCode IDeviceManager::UnInit(void) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); }
const StatusCode IDeviceManager::SetLedState(const std::string &ledName, const VirtualLedState &currentState, const StatusCode IDeviceManager::SetLedState(const std::string &ledName, const VirtualLedState &currentState,
const unsigned int &KeepAliveTime, const unsigned int &blinkPeriod) const unsigned int &KeepAliveTime, const unsigned int &blinkPeriod)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
const StatusCode IDeviceManager::SetAllKeysMonitor(std::shared_ptr<VKeyMonitor> &monitor)
{ {
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
} }

View File

@ -28,6 +28,10 @@ std::shared_ptr<KeyManager> &KeyManager::GetInstance(std::shared_ptr<KeyManager>
} }
return instance; return instance;
} }
KeyManager::KeyManager()
{
//
}
void KeyManager::Init(void) void KeyManager::Init(void)
{ {
// //
@ -45,7 +49,7 @@ void KeyManager::StartTimer(void)
LogError("StartTimer failed, no key to manager.\n"); LogError("StartTimer failed, no key to manager.\n");
return; return;
} }
SetAllKeysMonitor(); SetKeyHalMonitor();
auto timerThread = [](std::shared_ptr<KeyManager> timer) { auto timerThread = [](std::shared_ptr<KeyManager> timer) {
LogInfo("Key timer started.\n"); LogInfo("Key timer started.\n");
timer->Timer(); timer->Timer();
@ -83,7 +87,12 @@ void KeyManager::GetAllKeysState(std::map<std::string, KeyStatus> &status)
status.insert(std::make_pair(iter->first, result)); status.insert(std::make_pair(iter->first, result));
} }
} }
void KeyManager::SetAllKeysMonitor(void) const StatusCode KeyManager::SetKeyMonitor(std::shared_ptr<VKeyMonitor> &monitor)
{
mKeyMonitor = monitor;
return CreateStatusCode(STATUS_CODE_OK);
}
void KeyManager::SetKeyHalMonitor(void)
{ {
std::shared_ptr<VKeyHalMonitor> monitor = shared_from_this(); std::shared_ptr<VKeyHalMonitor> monitor = shared_from_this();
std::map<std::string, std::shared_ptr<VKeyHal>>::iterator iter; std::map<std::string, std::shared_ptr<VKeyHal>>::iterator iter;
@ -94,5 +103,10 @@ void KeyManager::SetAllKeysMonitor(void)
} }
void KeyManager::KeyEventHappened(const std::string &keyName, const VirtualKeyEvent &event, const unsigned int &timeMs) void KeyManager::KeyEventHappened(const std::string &keyName, const VirtualKeyEvent &event, const unsigned int &timeMs)
{ {
// auto monitor = mKeyMonitor.lock();
if (mKeyMonitor.expired()) {
LogError("monitor is nullptr.\n");
return;
}
monitor->KeyEventReport(keyName, static_cast<VirtualKeyEvent>(event), timeMs);
} }

View File

@ -23,7 +23,7 @@
class KeyManager : public VKeyHalMonitor, public std::enable_shared_from_this<KeyManager> class KeyManager : public VKeyHalMonitor, public std::enable_shared_from_this<KeyManager>
{ {
public: public:
KeyManager() = default; KeyManager();
~KeyManager() = default; ~KeyManager() = default;
static std::shared_ptr<KeyManager> &GetInstance(std::shared_ptr<KeyManager> *impl = nullptr); static std::shared_ptr<KeyManager> &GetInstance(std::shared_ptr<KeyManager> *impl = nullptr);
void Init(void); void Init(void);
@ -32,9 +32,10 @@ public:
void StopTimer(void); void StopTimer(void);
void Timer(void); void Timer(void);
void GetAllKeysState(std::map<std::string, KeyStatus> &status); void GetAllKeysState(std::map<std::string, KeyStatus> &status);
const StatusCode SetKeyMonitor(std::shared_ptr<VKeyMonitor> &monitor);
private: private:
void SetAllKeysMonitor(void); void SetKeyHalMonitor(void);
private: private:
void KeyEventHappened(const std::string &keyName, const VirtualKeyEvent &event, void KeyEventHappened(const std::string &keyName, const VirtualKeyEvent &event,
@ -45,5 +46,6 @@ private:
std::map<std::string, std::shared_ptr<VKeyHal>> mAllKeyHal; std::map<std::string, std::shared_ptr<VKeyHal>> mAllKeyHal;
bool mTimerRuning; bool mTimerRuning;
std::thread mTimer; std::thread mTimer;
std::weak_ptr<VKeyMonitor> mKeyMonitor;
}; };
#endif #endif

View File

@ -8,15 +8,15 @@ include_directories(
./tool/include ./tool/include
${UTILS_SOURCE_PATH}/Log/include ${UTILS_SOURCE_PATH}/Log/include
${UTILS_SOURCE_PATH}/StatusCode/include ${UTILS_SOURCE_PATH}/StatusCode/include
${UTILS_SOURCE_PATH}/UartDevice/include # ${UTILS_SOURCE_PATH}/UartDevice/include
${UTILS_SOURCE_PATH}/McuProtocol/include # ${UTILS_SOURCE_PATH}/McuProtocol/include
${UTILS_SOURCE_PATH}/KeyControl/include ${UTILS_SOURCE_PATH}/KeyControl/include
${HAL_SOURCE_PATH}/include ${HAL_SOURCE_PATH}/include
${HAL_SOURCE_PATH}/src ${HAL_SOURCE_PATH}/src
${MIDDLEWARE_SOURCE_PATH}/DeviceManager/include ${MIDDLEWARE_SOURCE_PATH}/DeviceManager/include
${MIDDLEWARE_SOURCE_PATH}/DeviceManager/src ${MIDDLEWARE_SOURCE_PATH}/DeviceManager/src
${MIDDLEWARE_SOURCE_PATH}/McuAskBase/include # ${MIDDLEWARE_SOURCE_PATH}/McuAskBase/include
${TEST_SOURCE_PATH}/utils/LinuxApiMock/include # ${TEST_SOURCE_PATH}/utils/LinuxApiMock/include
${TEST_SOURCE_PATH}/hal/tool/include ${TEST_SOURCE_PATH}/hal/tool/include
# ${TEST_SOURCE_PATH}/utils/UartDevice/tool/include # ${TEST_SOURCE_PATH}/utils/UartDevice/tool/include
# ${TEST_SOURCE_PATH}/utils/McuProtocol/tool/include # ${TEST_SOURCE_PATH}/utils/McuProtocol/tool/include
@ -40,7 +40,7 @@ endif()
set(TARGET_NAME DeviceManagerTest) set(TARGET_NAME DeviceManagerTest)
add_executable(${TARGET_NAME} ${SRC_FILES_MAIN} ${SRC_FILES}) add_executable(${TARGET_NAME} ${SRC_FILES_MAIN} ${SRC_FILES})
target_link_libraries(${TARGET_NAME} DeviceManager HalTestTool gtest gmock pthread) target_link_libraries(${TARGET_NAME} DeviceManager DeviceManagerTestTool HalTestTool gtest gmock pthread)
if(${TEST_COVERAGE} MATCHES "true") if(${TEST_COVERAGE} MATCHES "true")
target_link_libraries(${TARGET_NAME} gcov) target_link_libraries(${TARGET_NAME} gcov)
endif() endif()
@ -85,4 +85,4 @@ endif()
define_file_name(${TARGET_NAME}) define_file_name(${TARGET_NAME})
# add_subdirectory(tool) add_subdirectory(tool)

View File

@ -12,6 +12,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include "DeviceManagerTestTool.h"
#include "HalTestTool.h" #include "HalTestTool.h"
#include "IDeviceManager.h" #include "IDeviceManager.h"
#include "ILog.h" #include "ILog.h"
@ -21,7 +22,7 @@
namespace DeviceManagerTest namespace DeviceManagerTest
{ {
const char *KEY_TEST = "TEST"; const char *KEY_TEST = "TEST";
class DeviceManagerTest : public testing::Test, public HalTestTool class DeviceManagerTest : public testing::Test, public HalTestTool, public DeviceManagerTestTool
{ {
public: public:
DeviceManagerTest() {} DeviceManagerTest() {}
@ -94,4 +95,16 @@ TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_KeyLongPress)
std::this_thread::sleep_for(std::chrono::milliseconds(2000)); std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IDeviceManager::GetInstance()->UnInit(); IDeviceManager::GetInstance()->UnInit();
} }
// ../output_files/test/bin/DeviceManagerTest
// --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_SetKeyMonitor
TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_SetKeyMonitor)
{
SetAllKeysResult(mAllKeysMock);
IDeviceManager::GetInstance()->Init();
std::shared_ptr<VKeyMonitor> monitor = std::make_shared<KeyMonitorMock>();
IDeviceManager::GetInstance()->SetAllKeysMonitor(monitor);
SetKeyClick(KEY_TEST, 1000); // Simulate pressing a button.
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IDeviceManager::GetInstance()->UnInit();
}
} // namespace DeviceManagerTest } // namespace DeviceManagerTest

View File

@ -0,0 +1,52 @@
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
set(EXECUTABLE_OUTPUT_PATH ${EXEC_OUTPUT_PATH})
set(LIBRARY_OUTPUT_PATH ${LIBS_OUTPUT_PATH})
include_directories(
./src
./include
${UTILS_SOURCE_PATH}/StatusCode/include
${UTILS_SOURCE_PATH}/Log/include
${UTILS_SOURCE_PATH}/McuProtocol/include
${MIDDLEWARE_SOURCE_PATH}/DeviceManager/src
${TEST_SOURCE_PATH}/utils/LinuxApiMock/include
${TEST_SOURCE_PATH}/utils/McuProtocol/tool/include
)
# link_directories(
# ${EXTERNAL_SOURCE_PATH}/libconfig/libconfig-1.7.3/lib/.libs
# )
aux_source_directory(./src TEST_TOOL_SRC_FILES)
set(TEST_TOOL_TARGET DeviceManagerTestTool)
add_library(${TEST_TOOL_TARGET} STATIC ${TEST_TOOL_SRC_FILES})
target_link_libraries(${TEST_TOOL_TARGET} Log)
if ("${CLANG_TIDY_SUPPORT}" MATCHES "true")
add_custom_target(
DeviceManagerTestTool_code_check
COMMAND ${CLANG_TIDY_EXE}
-checks='${CLANG_TIDY_CHECKS}'
--header-filter=.*
--system-headers=false
${TEST_TOOL_SRC_FILES}
${CLANG_TIDY_CONFIG}
-p ${PLATFORM_PATH}/cmake-shell
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/DeviceManager/tool
)
file(GLOB_RECURSE HEADER_FILES *.h)
add_custom_target(
DeviceManagerTestTool_code_format
COMMAND ${CLANG_FORMAT_EXE}
-style=file
-i ${TEST_TOOL_SRC_FILES} ${HEADER_FILES}
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/DeviceManager/tool
)
add_custom_command(
TARGET ${TEST_TOOL_TARGET}
PRE_BUILD
COMMAND make DeviceManagerTestTool_code_check
COMMAND make DeviceManagerTestTool_code_format
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
endif()
define_file_name(${TEST_TOOL_TARGET})

View File

@ -0,0 +1,91 @@
/*
* 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.
*/
#ifndef DEVICE_MANAGER_TEST_TOOL_H
#define DEVICE_MANAGER_TEST_TOOL_H
#include "DeviceManager.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
using ::testing::_;
using ::testing::Action;
using ::testing::ActionInterface;
using ::testing::AnyNumber;
using ::testing::Assign;
using ::testing::AtLeast;
using ::testing::ByMove;
using ::testing::ByRef;
using ::testing::DefaultValue;
using ::testing::DoAll;
using ::testing::DoDefault;
using ::testing::IgnoreResult;
using ::testing::Invoke;
using ::testing::InvokeWithoutArgs;
using ::testing::MakePolymorphicAction;
using ::testing::PolymorphicAction;
using ::testing::Return;
using ::testing::ReturnNew;
using ::testing::ReturnNull;
using ::testing::ReturnPointee;
using ::testing::ReturnRef;
using ::testing::ReturnRefOfCopy;
using ::testing::ReturnRoundRobin;
using ::testing::SaveArg;
using ::testing::SetArgPointee;
using ::testing::SetArgumentPointee;
using ::testing::Unused;
using ::testing::WithArgs;
using ::testing::internal::BuiltInDefaultValue;
class DeviceManagerTool : public DeviceManager
{
public:
DeviceManagerTool() = default;
virtual ~DeviceManagerTool() = default;
const StatusCode SetAllKeysMonitor(std::shared_ptr<VKeyMonitor> &monitor) override;
private:
virtual const StatusCode SetAllKeysMonitorTrace(std::shared_ptr<VKeyMonitor> &monitor)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
};
class DeviceManagerMock : public DeviceManagerTool
{
public:
DeviceManagerMock() = default;
virtual ~DeviceManagerMock() = default;
MOCK_METHOD1(SetAllKeysMonitorTrace, const StatusCode(std::shared_ptr<VKeyMonitor> &));
};
class KeyMonitorMock : public VKeyMonitor
{
public:
KeyMonitorMock() = default;
virtual ~KeyMonitorMock() = default;
MOCK_METHOD3(KeyEventReport, void(const std::string &, const VirtualKeyEvent &, const unsigned int &));
};
class DeviceManagerTestTool
{
public:
DeviceManagerTestTool() = default;
virtual ~DeviceManagerTestTool() = default;
void Init(void);
void UnInit(void);
private:
void DeviceManagerMockInit(std::shared_ptr<DeviceManagerMock> &mock);
private:
std::shared_ptr<DeviceManagerMock> mDeviceManagerMock;
std::shared_ptr<KeyMonitorMock> mKeyMonitorMock;
};
#endif

View File

@ -0,0 +1,55 @@
/*
* 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 "DeviceManagerMakePtrTest.h"
#include "ILog.h"
void OverrideHalMakePtrObject(std::shared_ptr<DeviceManagerMock> &halMock)
{
std::shared_ptr<DeviceManagerMakePtr> impl = std::make_shared<DeviceManagerMakePtrTest>();
std::shared_ptr<DeviceManagerMakePtrTest> test = std::dynamic_pointer_cast<DeviceManagerMakePtrTest>(impl);
if (test) {
test->mDeviceManagerMock = halMock;
}
DeviceManagerMakePtr::GetInstance(&impl);
}
void CancelOverrideHalMakePtrObject(void)
{
std::shared_ptr<DeviceManagerMakePtr> tmp = DeviceManagerMakePtr::GetInstance();
std::shared_ptr<DeviceManagerMakePtrTest> test = std::dynamic_pointer_cast<DeviceManagerMakePtrTest>(tmp);
if (test) {
test->mDeviceManagerMock.reset();
}
std::shared_ptr<DeviceManagerMakePtr> impl = std::make_shared<DeviceManagerMakePtrTest>();
DeviceManagerMakePtr::GetInstance(&impl);
}
DeviceManagerMakePtrTest::DeviceManagerMakePtrTest()
{
//
}
DeviceManagerMakePtrTest::~DeviceManagerMakePtrTest()
{
//
mDeviceManagerMock.reset();
}
const StatusCode DeviceManagerMakePtrTest::CreateDeviceManager(std::shared_ptr<IDeviceManager> &impl)
{
if (mDeviceManagerMock) {
LogInfo("CreateDeviceManager mDeviceManagerMock\n");
impl = mDeviceManagerMock;
}
else {
LogWarning("CreateMcuManager failed:mDeviceManagerMock is nullptr.\n");
}
return CreateStatusCode(STATUS_CODE_OK);
}

View File

@ -0,0 +1,31 @@
/*
* 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.
*/
#ifndef DEVICE_MANAGER_MAKE_PTR_TEST_H
#define DEVICE_MANAGER_MAKE_PTR_TEST_H
#include "DeviceManagerMakePtr.h"
#include "DeviceManagerTestTool.h"
void OverrideDeviceMakePtrObject(std::shared_ptr<DeviceManagerMock> &halMock);
void CancelOverrideDeviceMakePtrObject(void);
class DeviceManagerMakePtrTest : public DeviceManagerMakePtr
{
public:
DeviceManagerMakePtrTest();
virtual ~DeviceManagerMakePtrTest();
const StatusCode CreateDeviceManager(std::shared_ptr<IDeviceManager> &impl) override;
public:
std::shared_ptr<DeviceManagerMock> mDeviceManagerMock;
};
#endif

View File

@ -0,0 +1,48 @@
/*
* 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"
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();
CancelOverrideDeviceMakePtrObject();
}
void DeviceManagerTestTool::DeviceManagerMockInit(std::shared_ptr<DeviceManagerMock> &mock)
{
auto getKeyMonitor = [=](std::shared_ptr<VKeyMonitor> &monitor) {
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))));
}