hunting/test/utils/UartDevice/src_mock/UartDevice_Mock_Test.cpp

92 lines
2.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 "ILog.h"
#include "LinuxApiMock.h"
#include "UartDevice.h"
#include "UartDeviceTestTool.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
namespace UartDeviceMockTest
{
const char *gDeviceName = "dev/s1";
static uart_info gUartDevice = {
gDeviceName,
1152000,
'N',
8,
1,
'N',
};
class UartDeviceMockTest : public testing::Test, public UartDeviceTestTool
{
public:
UartDeviceMockTest() {}
virtual ~UartDeviceMockTest() {}
static void SetUpTestCase()
{
CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END);
}
static void TearDownTestCase() { ILogUnInit(); }
virtual void SetUp()
{
mLinuxTest = LinuxTest::CreateLinuxTest();
std::shared_ptr<LinuxApiMock> test = mLinuxTest;
LinuxApiMock::GetInstance(&test);
LinuxApiMock::GetInstance()->Init();
// UartDeviceDefaultInit(mLinuxTest, gUartDevice);
}
virtual void TearDown()
{
LinuxApiMock::GetInstance()->UnInit();
mLinuxTest = std::make_shared<LinuxTest>();
std::shared_ptr<LinuxApiMock> test = std::make_shared<LinuxApiMock>();
LinuxApiMock::GetInstance(&test);
}
public:
std::shared_ptr<LinuxTest> mLinuxTest;
};
// ../output_files/test/bin/UartDeviceTest --gtest_filter=UartDeviceMockTest.Demo
TEST_F(UartDeviceMockTest, Demo)
{
void *object = CreateUartDevice(gUartDevice);
IUartOpen(object);
// IUartSend(object, nullptr, 0);
// IUartRecv(object, nullptr, 0, 0);
// IUartTcflush(object);
IUartDeviceFree(object);
}
// ../output_files/test/bin/UartDeviceTest --gtest_filter=UartDeviceMockTest.Test
TEST_F(UartDeviceMockTest, Test)
{
auto openThread = [](void) {
LogInfo("===========openThread \n");
void *object = CreateUartDevice(gUartDevice);
IUartOpen(object);
IUartDeviceFree(object);
};
std::thread test1 = std::thread(openThread);
test1.detach();
std::thread test2 = std::thread(openThread);
test2.detach();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
// std::thread(openThread2);
// IUartSend(object, nullptr, 0);
// IUartRecv(object, nullptr, 0, 0);
// IUartTcflush(object);
}
} // namespace UartDeviceMockTest