Improve:AppManager test code.

This commit is contained in:
Fancy code 2024-03-05 19:01:11 -08:00
parent d42b38c5dc
commit 82b6cf4663
9 changed files with 130 additions and 53 deletions

View File

@ -0,0 +1,29 @@
/*
* 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 "MissionState.h"
#include "IAppManager.h"
#include "ILog.h"
MissionState::MissionState(const std::string &name) : State(name) {}
void MissionState::GoInState()
{
//
LogInfo(" ========== MissionState::GoInState.\n");
}
void MissionState::GoOutState()
{
//
LogInfo(" ========== MissionState::GoOutState.\n");
}
bool MissionState::ExecuteStateMsg(VStateMachineData *msg) { return DataProcessing::EventHandle(msg); }

View File

@ -0,0 +1,28 @@
/*
* 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 MISSION_STATE_H
#define MISSION_STATE_H
#include "DataProcessing.h"
#include "IStateMachine.h"
class MissionState : public State, public DataProcessing
{
public:
MissionState(const std::string &name);
virtual ~MissionState() = default;
void GoInState() override;
void GoOutState() override;
bool ExecuteStateMsg(VStateMachineData *msg) override;
};
#endif

View File

@ -15,17 +15,17 @@
#include "TestMissionState.h"
#include "IAppManager.h"
#include "ILog.h"
TestMissionState::TestMissionState() : State("TestMissionState") {}
TestMissionState::TestMissionState() : MissionState("TestMissionState") {}
void TestMissionState::GoInState()
{
//
MissionState::GoInState();
LogInfo(" ========== TestMissionState::GoInState.\n");
AppParam mAppParam("192.168.1.29", APP_MANAGER_HTTP_SERVER_PORT); // TODO:
IAppManager::GetInstance()->Init(mAppParam);
}
void TestMissionState::GoOutState()
{
//
MissionState::GoOutState();
LogInfo(" ========== TestMissionState::GoOutState.\n");
}
bool TestMissionState::ExecuteStateMsg(VStateMachineData *msg) { return DataProcessing::EventHandle(msg); }
bool TestMissionState::ExecuteStateMsg(VStateMachineData *msg) { return MissionState::EventHandle(msg); }

View File

@ -15,9 +15,9 @@
#ifndef TEST_MISSION_STATE_H
#define TEST_MISSION_STATE_H
#include "AppMonitor.h"
#include "DataProcessing.h"
#include "IStateMachine.h"
class TestMissionState : public State, public DataProcessing, public AppMonitor
#include "MissionState.h"
class TestMissionState : public MissionState, public AppMonitor
{
public:
TestMissionState();

View File

@ -13,6 +13,7 @@ include_directories(
${UTILS_SOURCE_PATH}/UartDevice/include
${TEST_SOURCE_PATH}
${TEST_SOURCE_PATH}/middleware/McuManager/tool/include
${TEST_SOURCE_PATH}/middleware/AppManager/tool/include
${TEST_SOURCE_PATH}/utils/LinuxApiMock/include
${TEST_SOURCE_PATH}/utils/TestManager/include
${TEST_SOURCE_PATH}/utils/McuProtocol/tool/include
@ -34,7 +35,7 @@ endif()
set(TARGET_NAME HunttingCameraTest)
add_executable(${TARGET_NAME} ${SRC_FILES_MAIN} ${SRC_FILES})
target_link_libraries(${TARGET_NAME} HunttingMainLib McuManagerTestTool McuAskBaseTestTool TestManager gtest gmock pthread)
target_link_libraries(${TARGET_NAME} HunttingMainLib McuManagerTestTool McuAskBaseTestTool AppManagerTestTool TestManager gtest gmock pthread)
if(${TEST_COVERAGE} MATCHES "true")
target_link_libraries(${TARGET_NAME} gcov)
endif()

View File

@ -12,6 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "AppManagerTestTool.h"
#include "GtestUsing.h"
#include "ILog.h"
#include "MainThread.h"
@ -20,7 +21,10 @@
#include <thread>
namespace HunttingCameraTest
{
class HunttingCameraTest : public testing::Test, public TestManager, public McuManagerTestTool
class HunttingCameraTest : public testing::Test,
public TestManager,
public McuManagerTestTool,
public AppManagerTestTool
{
public:
HunttingCameraTest() {}
@ -68,6 +72,8 @@ TEST_F(HunttingCameraTest, INTEGRATION_DeviceManager_EXAMPLE_Demo)
{
MainThread::GetInstance()->Init();
TestManager::ResetTimeOut(1000 * 3);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockGetMediaInfo();
MainThread::GetInstance()->Runing();
}
} // namespace HunttingCameraTest

View File

@ -106,7 +106,7 @@ void AppManagerTestTool::MockSetTimeZone(void)
LogError("vMock error.\n");
return;
}
EXPECT_CALL(*mock.get(), SetDateTimeTrace(_))
EXPECT_CALL(*mock.get(), SetTimeZoneTrace(_))
.Times(ONLY_BE_CALLED_ONCE)
.WillOnce(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION))));
ServersMock::GetInstance()->MockSetTimeZone();
@ -125,7 +125,8 @@ void AppManagerTestTool::AppManagerMockInit(std::shared_ptr<IAppManager> &vMock)
}
auto getAppMonitor = [=](std::shared_ptr<VAppMonitor> &monitor) {
LogInfo("mAppMonitorMock get.\n");
mAppMonitorMock = std::dynamic_pointer_cast<AppMonitorMock>(monitor);
// mAppMonitorMock = std::dynamic_pointer_cast<AppMonitorMock>(monitor);
mAppMonitorMock = monitor;
AppManagerTestTool::AppMonitorInit(mAppMonitorMock);
};
EXPECT_CALL(*mock.get(), SetAppMonitorTrace(_))
@ -134,27 +135,32 @@ void AppManagerTestTool::AppManagerMockInit(std::shared_ptr<IAppManager> &vMock)
}
std::shared_ptr<VAppMonitor> AppManagerTestTool::MakeMonitorMock(void)
{
auto monitor = std::make_shared<AppMonitorMock>();
EXPECT_CALL(*monitor.get(), GetProductInfoTrace(_))
.WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION))));
EXPECT_CALL(*monitor.get(), GetDeviceAttrTrace(_))
.WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION))));
EXPECT_CALL(*monitor.get(), GetMediaInfoTrace(_))
.WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION))));
EXPECT_CALL(*monitor.get(), GetSdCardInfoTrace(_))
.WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION))));
EXPECT_CALL(*monitor.get(), GetBatteryInfoTrace(_))
.WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION))));
EXPECT_CALL(*monitor.get(), SetDateTimeTrace(_))
.WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION))));
EXPECT_CALL(*monitor.get(), UploadFileTrace(_))
.WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION))));
class Monitor : public AppMonitorMock, public AppMonitorTest
{
public:
Monitor() = default;
virtual ~Monitor() = default;
};
std::shared_ptr<VAppMonitor> monitor = std::make_shared<Monitor>();
// auto result = std::dynamic_pointer_cast<VAppMonitor>(monitor);
return monitor;
}
void AppManagerTestTool::AppMonitorInit(std::shared_ptr<VAppMonitor> &vMock)
{
std::shared_ptr<AppMonitorMock> mock = std::dynamic_pointer_cast<AppMonitorMock>(vMock);
if (mock) {
EXPECT_CALL(*mock.get(), GetProductInfoTrace(_))
.WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION))));
EXPECT_CALL(*mock.get(), GetDeviceAttrTrace(_))
.WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION))));
EXPECT_CALL(*mock.get(), GetMediaInfoTrace(_))
.WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION))));
EXPECT_CALL(*mock.get(), GetSdCardInfoTrace(_))
.WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION))));
EXPECT_CALL(*mock.get(), GetBatteryInfoTrace(_))
.WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION))));
EXPECT_CALL(*mock.get(), SetDateTimeTrace(_))
.WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION))));
EXPECT_CALL(*mock.get(), UploadFileTrace(_))
.WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION))));
}

View File

@ -23,9 +23,9 @@ StatusCode AppMonitorTest::GetProductInfo(AppGetProductInfo &param)
}
return code;
}
StatusCode AppMonitorTest::GetProductInfoTrace(AppGetProductInfo &param)
StatusCode AppMonitorTrace::GetProductInfoTrace(AppGetProductInfo &param)
{
LogInfo("AppMonitorTest::GetProductInfoTrace\n");
LogInfo("AppMonitorTrace::GetProductInfoTrace\n");
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
StatusCode AppMonitorTest::GetDeviceAttr(AppGetDeviceAttr &param)
@ -37,9 +37,9 @@ StatusCode AppMonitorTest::GetDeviceAttr(AppGetDeviceAttr &param)
}
return code;
}
StatusCode AppMonitorTest::GetDeviceAttrTrace(AppGetDeviceAttr &param)
StatusCode AppMonitorTrace::GetDeviceAttrTrace(AppGetDeviceAttr &param)
{
LogInfo("AppMonitorTest::GetDeviceAttrTrace\n");
LogInfo("AppMonitorTrace::GetDeviceAttrTrace\n");
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
StatusCode AppMonitorTest::GetMediaInfo(AppGetMeidaInfo &param)
@ -51,9 +51,9 @@ StatusCode AppMonitorTest::GetMediaInfo(AppGetMeidaInfo &param)
}
return code;
}
StatusCode AppMonitorTest::GetMediaInfoTrace(AppGetMeidaInfo &param)
StatusCode AppMonitorTrace::GetMediaInfoTrace(AppGetMeidaInfo &param)
{
LogInfo("AppMonitorTest::GetMediaInfoTrace\n");
LogInfo("AppMonitorTrace::GetMediaInfoTrace\n");
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
StatusCode AppMonitorTest::GetSdCardInfo(AppGetSdCardInfo &param)
@ -65,9 +65,9 @@ StatusCode AppMonitorTest::GetSdCardInfo(AppGetSdCardInfo &param)
}
return code;
}
StatusCode AppMonitorTest::GetSdCardInfoTrace(AppGetSdCardInfo &param)
StatusCode AppMonitorTrace::GetSdCardInfoTrace(AppGetSdCardInfo &param)
{
LogInfo("AppMonitorTest::GetSdCardInfoTrace\n");
LogInfo("AppMonitorTrace::GetSdCardInfoTrace\n");
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
StatusCode AppMonitorTest::GetBatteryInfo(AppGetBatteryInfo &param)
@ -79,9 +79,9 @@ StatusCode AppMonitorTest::GetBatteryInfo(AppGetBatteryInfo &param)
}
return code;
}
StatusCode AppMonitorTest::GetBatteryInfoTrace(AppGetBatteryInfo &param)
StatusCode AppMonitorTrace::GetBatteryInfoTrace(AppGetBatteryInfo &param)
{
LogInfo("AppMonitorTest::GetBatteryInfoTrace\n");
LogInfo("AppMonitorTrace::GetBatteryInfoTrace\n");
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
StatusCode AppMonitorTest::SetDateTime(AppSetDateTime &param)
@ -99,9 +99,9 @@ StatusCode AppMonitorTest::SetDateTime(AppSetDateTime &param)
}
return code;
}
StatusCode AppMonitorTest::SetDateTimeTrace(AppSetDateTime &param)
StatusCode AppMonitorTrace::SetDateTimeTrace(AppSetDateTime &param)
{
LogInfo("AppMonitorTest::SetDateTimeTrace\n");
LogInfo("AppMonitorTrace::SetDateTimeTrace\n");
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
StatusCode AppMonitorTest::SetTimeZone(const unsigned int &zone)
@ -113,9 +113,9 @@ StatusCode AppMonitorTest::SetTimeZone(const unsigned int &zone)
}
return code;
}
StatusCode AppMonitorTest::SetTimeZoneTrace(const unsigned int &zone)
StatusCode AppMonitorTrace::SetTimeZoneTrace(const unsigned int &zone)
{
LogInfo("AppMonitorTest::SetTimeZoneTrace\n");
LogInfo("AppMonitorTrace::SetTimeZoneTrace\n");
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
StatusCode AppMonitorTest::UploadFile(AppUploadFile &param)
@ -127,8 +127,8 @@ StatusCode AppMonitorTest::UploadFile(AppUploadFile &param)
}
return code;
}
StatusCode AppMonitorTest::UploadFileTrace(AppUploadFile &param)
StatusCode AppMonitorTrace::UploadFileTrace(AppUploadFile &param)
{
LogInfo("AppMonitorTest::UploadFileTrace\n");
LogInfo("AppMonitorTrace::UploadFileTrace\n");
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}

View File

@ -16,7 +16,23 @@
#define APP_MONITOR_MOCK_H
#include "GtestUsing.h"
#include "IAppManager.h"
class AppMonitorTest : public VAppMonitor
class AppMonitorTrace
{
public:
AppMonitorTrace() = default;
virtual ~AppMonitorTrace() = default;
protected:
virtual StatusCode GetProductInfoTrace(AppGetProductInfo &param);
virtual StatusCode GetDeviceAttrTrace(AppGetDeviceAttr &param);
virtual StatusCode GetMediaInfoTrace(AppGetMeidaInfo &param);
virtual StatusCode GetSdCardInfoTrace(AppGetSdCardInfo &param);
virtual StatusCode GetBatteryInfoTrace(AppGetBatteryInfo &param);
virtual StatusCode SetDateTimeTrace(AppSetDateTime &param);
virtual StatusCode SetTimeZoneTrace(const unsigned int &zone);
virtual StatusCode UploadFileTrace(AppUploadFile &param);
};
class AppMonitorTest : public VAppMonitor, virtual public AppMonitorTrace
{
public:
AppMonitorTest() = default;
@ -29,18 +45,8 @@ public:
StatusCode SetDateTime(AppSetDateTime &param) override;
StatusCode SetTimeZone(const unsigned int &zone) override;
StatusCode UploadFile(AppUploadFile &param) override;
protected:
virtual StatusCode GetProductInfoTrace(AppGetProductInfo &param);
virtual StatusCode GetDeviceAttrTrace(AppGetDeviceAttr &param);
virtual StatusCode GetMediaInfoTrace(AppGetMeidaInfo &param);
virtual StatusCode GetSdCardInfoTrace(AppGetSdCardInfo &param);
virtual StatusCode GetBatteryInfoTrace(AppGetBatteryInfo &param);
virtual StatusCode SetDateTimeTrace(AppSetDateTime &param);
virtual StatusCode SetTimeZoneTrace(const unsigned int &zone);
virtual StatusCode UploadFileTrace(AppUploadFile &param);
};
class AppMonitorMock : public AppMonitorTest
class AppMonitorMock : virtual public AppMonitorTrace
{
public:
AppMonitorMock() = default;
@ -51,6 +57,7 @@ public:
MOCK_METHOD1(GetSdCardInfoTrace, StatusCode(AppGetSdCardInfo &));
MOCK_METHOD1(GetBatteryInfoTrace, StatusCode(AppGetBatteryInfo &));
MOCK_METHOD1(SetDateTimeTrace, StatusCode(AppSetDateTime &));
MOCK_METHOD1(SetTimeZoneTrace, StatusCode(const unsigned int &));
MOCK_METHOD1(UploadFileTrace, StatusCode(AppUploadFile &));
};
#endif