hunting/test/middleware/DeviceManager/src/DeviceManager_Test.cpp
2024-06-15 08:39:16 +08:00

215 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 "DeviceManagerTestTool.h"
#include "HalTestTool.h"
#include "IDeviceManager.h"
#include "ILog.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
namespace DeviceManagerTest
{
const char *KEY_TEST = "KEY_TEST";
const char *LED_TEST = "LED_TEST";
class DeviceManagerTest : public testing::Test, virtual public HalTestTool, public DeviceManagerTestTool
{
public:
DeviceManagerTest()
{
}
virtual ~DeviceManagerTest()
{
}
static void SetUpTestCase()
{
CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END);
}
static void TearDownTestCase()
{
ILogUnInit();
}
virtual void SetUp()
{
CreateAllKeysMcok();
HalTestTool::Init();
DeviceManagerTestTool::Init();
CreateHalCppModule();
CreateDeviceManagerModule();
// CreateMcuManager();
}
virtual void TearDown()
{
HalTestTool::UnInit();
DeviceManagerTestTool::UnInit();
DestroyDeviceManagerModule();
DestroyAllKeysMock();
}
private:
void CreateAllKeysMcok(void)
{
std::shared_ptr<VKeyHal> key = HalTestTool::MakeKeyHalTest(KEY_TEST);
mAllKeysMock[KEY_TEST] = key;
std::shared_ptr<VLedHal> led = HalTestTool::MakeLedHalTest(LED_TEST);
mAllLedsMock[LED_TEST] = led;
}
void DestroyAllKeysMock(void)
{
mAllKeysMock.clear();
mAllLedsMock.clear();
}
public:
// std::shared_ptr<LinuxTest> mLinuxTest;
protected:
std::map<std::string, std::shared_ptr<VKeyHal>> mAllKeysMock;
std::map<std::string, std::shared_ptr<VLedHal>> mAllLedsMock;
};
// ../output_files/test/bin/DeviceManagerTest
// --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_KeyShortPress
/**
* @brief Construct a new test f object
* This test case demonstrates whether DeviceManager can reprocess key events and report them to the application when
* they are triggered.
*/
TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_KeyShortPress)
{
SetAllKeysResult(mAllKeysMock);
IDeviceManager::GetInstance()->Init();
std::shared_ptr<VKeyMonitor> monitor = std::make_shared<KeyMonitorMock>();
IDeviceManager::GetInstance()->SetAllKeysMonitor(monitor);
MockKeyClick(KEY_TEST); // Simulate pressing a button.
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
IDeviceManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/DeviceManagerTest
// --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_KeyInvalidShort
/**
* @brief Construct a new test f object
* This test case demonstrates whether DeviceManager can reprocess key events and report them to the application when
* they are triggered.
*/
TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_KeyInvalidShort)
{
SetAllKeysResult(mAllKeysMock);
IDeviceManager::GetInstance()->Init();
std::shared_ptr<VKeyMonitor> monitor = std::make_shared<KeyMonitorMock>();
IDeviceManager::GetInstance()->SetAllKeysMonitor(monitor);
MockKeyClick(KEY_TEST, 50); // Simulate pressing a button.
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
IDeviceManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/DeviceManagerTest
// --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_KeyLongPress
/**
* @brief Construct a new test f object
* This test case demonstrates whether DeviceManager can reprocess key events and report them to the application when
* they are triggered.
*/
TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_KeyLongPress)
{
SetAllKeysResult(mAllKeysMock);
IDeviceManager::GetInstance()->Init();
std::shared_ptr<VKeyMonitor> monitor = std::make_shared<KeyMonitorMock>();
IDeviceManager::GetInstance()->SetAllKeysMonitor(monitor);
MockKeyClick(KEY_TEST, 1000); // Simulate pressing a button.
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
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);
MockKeyClick(KEY_TEST, 1000); // Simulate pressing a button.
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
MockKeyClick(KEY_TEST, 1000); // Simulate pressing a button.
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IDeviceManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/DeviceManagerTest
// --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed
TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed)
{
SetAllLedsResult(mAllLedsMock);
IDeviceManager::GetInstance()->Init();
std::shared_ptr<VKeyMonitor> monitor = std::make_shared<KeyMonitorMock>();
IDeviceManager::GetInstance()->SetAllKeysMonitor(monitor);
ControlLed(LED_TEST, LedState::ON, KEEP_ALIVE_FOREVER, LED_NOT_BLINK);
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
IDeviceManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/DeviceManagerTest
// --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed2
TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed2)
{
SetAllLedsResult(mAllLedsMock);
IDeviceManager::GetInstance()->Init();
std::shared_ptr<VKeyMonitor> monitor = std::make_shared<KeyMonitorMock>();
IDeviceManager::GetInstance()->SetAllKeysMonitor(monitor);
constexpr int BLINK_TIME = 500;
ControlLed(LED_TEST, LedState::ON, KEEP_ALIVE_FOREVER, BLINK_TIME);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IDeviceManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/DeviceManagerTest
// --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed3
TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed3)
{
SetAllLedsResult(mAllLedsMock);
IDeviceManager::GetInstance()->Init();
std::shared_ptr<VKeyMonitor> monitor = std::make_shared<KeyMonitorMock>();
IDeviceManager::GetInstance()->SetAllKeysMonitor(monitor);
constexpr int BLINK_TIME = 500;
constexpr int KEEP_ALIVE = 1000;
ControlLed(LED_TEST, LedState::ON, KEEP_ALIVE, BLINK_TIME);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IDeviceManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/DeviceManagerTest
// --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed4
TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed4)
{
SetAllLedsResult(mAllLedsMock);
IDeviceManager::GetInstance()->Init();
std::shared_ptr<VKeyMonitor> monitor = std::make_shared<KeyMonitorMock>();
IDeviceManager::GetInstance()->SetAllKeysMonitor(monitor);
constexpr int KEEP_ALIVE = 1000;
ControlLed(LED_TEST, LedState::ON, KEEP_ALIVE, LED_NOT_BLINK);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IDeviceManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/DeviceManagerTest
// --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed5
TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed5)
{
SetAllLedsResult(mAllLedsMock);
IDeviceManager::GetInstance()->Init();
std::shared_ptr<VKeyMonitor> monitor = std::make_shared<KeyMonitorMock>();
IDeviceManager::GetInstance()->SetAllKeysMonitor(monitor);
std::shared_ptr<LedControlContext> ledControl =
DeviceManagerTestTool::ControlLed(LED_TEST, LedState::ON, KEEP_ALIVE_FOREVER, LED_NOT_BLINK);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
DeviceManagerTestTool::DestoryLedControl(ledControl);
DeviceManagerTestTool::ControlLed(LED_TEST, LedState::ON, KEEP_ALIVE_FOREVER, LED_NOT_BLINK);
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
IDeviceManager::GetInstance()->UnInit();
}
} // namespace DeviceManagerTest