Backup:AppManager test code.

This commit is contained in:
Fancy code 2024-03-03 19:04:45 -08:00
parent a73b84039a
commit 6d23741b77
10 changed files with 37 additions and 14 deletions

View File

@ -15,10 +15,10 @@
#include "AppManagerMakePtr.h" #include "AppManagerMakePtr.h"
#include "AppManager.h" #include "AppManager.h"
#include "ILog.h" #include "ILog.h"
extern bool CreateProtocolHandleImpl(); // extern bool CreateProtocolHandleImpl();
bool CreateAppManagerModule(void) bool CreateAppManagerModule(void)
{ {
CreateProtocolHandleImpl(); // TODO: not good for gtest. // CreateProtocolHandleImpl(); // TODO: not good for gtest.
auto instance = std::make_shared<IAppManager>(); auto instance = std::make_shared<IAppManager>();
StatusCode code = AppManagerMakePtr::GetInstance()->CreateAppManager(instance); StatusCode code = AppManagerMakePtr::GetInstance()->CreateAppManager(instance);
if (IsCodeOK(code)) { if (IsCodeOK(code)) {

View File

@ -45,7 +45,7 @@ endif()
set(TARGET_NAME AppManagerTest) set(TARGET_NAME AppManagerTest)
add_executable(${TARGET_NAME} ${SRC_FILES_MAIN} ${SRC_FILES}) add_executable(${TARGET_NAME} ${SRC_FILES_MAIN} ${SRC_FILES})
target_link_libraries(${TARGET_NAME} AppManager AppManagerTestTool gtest gmock pthread) target_link_libraries(${TARGET_NAME} AppManagerTestTool 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()

View File

@ -33,18 +33,13 @@ public:
static void TearDownTestCase() { ILogUnInit(); } static void TearDownTestCase() { ILogUnInit(); }
virtual void SetUp() virtual void SetUp()
{ {
// CreateAllKeysMcok(); AppManagerTestTool::Init();
// HalTestTool::Init();
// AppManagerTestTool::Init();
// CreateHalCppModule();
CreateAppManagerModule(); CreateAppManagerModule();
} }
virtual void TearDown() virtual void TearDown()
{ {
// HalTestTool::UnInit(); AppManagerTestTool::UnInit();
// AppManagerTestTool::UnInit();
DestroyAppManagerModule(); DestroyAppManagerModule();
// DestroyAllKeysMock();
} }
protected: protected:
@ -70,7 +65,9 @@ TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_Demo)
// ../output_files/test/bin/AppManagerTest --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_Upload // ../output_files/test/bin/AppManagerTest --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_AUTO_Upload
TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_Upload) TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_AUTO_Upload)
{ {
std::shared_ptr<VAppMonitor> monitor = AppManagerTestTool::MakeMonitorMock();
IAppManager::GetInstance()->Init(mAppParam); IAppManager::GetInstance()->Init(mAppParam);
IAppManager::GetInstance()->SetAppMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::this_thread::sleep_for(std::chrono::milliseconds(100));
MockUploadFiles(); MockUploadFiles();
std::this_thread::sleep_for(std::chrono::milliseconds(1000)); std::this_thread::sleep_for(std::chrono::milliseconds(1000));

View File

@ -9,6 +9,7 @@ include_directories(
${UTILS_SOURCE_PATH}/Log/include ${UTILS_SOURCE_PATH}/Log/include
${UTILS_SOURCE_PATH}/Servers/include ${UTILS_SOURCE_PATH}/Servers/include
${MIDDLEWARE_SOURCE_PATH}/AppManager/src ${MIDDLEWARE_SOURCE_PATH}/AppManager/src
${MIDDLEWARE_SOURCE_PATH}/AppManager/src/Protocol/SixFrame
${TEST_SOURCE_PATH} ${TEST_SOURCE_PATH}
${TEST_SOURCE_PATH}/utils/LinuxApiMock/include ${TEST_SOURCE_PATH}/utils/LinuxApiMock/include
${TEST_SOURCE_PATH}/utils/McuProtocol/tool/include ${TEST_SOURCE_PATH}/utils/McuProtocol/tool/include
@ -32,7 +33,7 @@ add_definitions(-DAPP_MANAGER_HTTP_SERVER_PORT=${APP_MANAGER_HTTP_SERVER_PORT})
aux_source_directory(./src TEST_TOOL_SRC_FILES) aux_source_directory(./src TEST_TOOL_SRC_FILES)
set(TEST_TOOL_TARGET AppManagerTestTool) set(TEST_TOOL_TARGET AppManagerTestTool)
add_library(${TEST_TOOL_TARGET} STATIC ${TEST_TOOL_SRC_FILES}) add_library(${TEST_TOOL_TARGET} STATIC ${TEST_TOOL_SRC_FILES})
target_link_libraries(${TEST_TOOL_TARGET} Servers Log) target_link_libraries(${TEST_TOOL_TARGET} AppManager Servers Log)
if ("${CLANG_TIDY_SUPPORT}" MATCHES "true") if ("${CLANG_TIDY_SUPPORT}" MATCHES "true")
add_custom_target( add_custom_target(

View File

@ -35,5 +35,9 @@ private:
private: private:
std::shared_ptr<IAppManager> mAppManagerMock; std::shared_ptr<IAppManager> mAppManagerMock;
std::shared_ptr<VAppMonitor> mAppMonitorMock; std::shared_ptr<VAppMonitor> mAppMonitorMock;
public:
static std::shared_ptr<VAppMonitor> MakeMonitorMock(void);
void AppMonitorInit(std::shared_ptr<VAppMonitor> &vMock);
}; };
#endif #endif

View File

@ -30,6 +30,8 @@ void CancelOverrideAppManagerMakePtrObject(void)
if (test) { if (test) {
test->mAppManagerMock.reset(); test->mAppManagerMock.reset();
} }
tmp.reset();
test.reset();
std::shared_ptr<AppManagerMakePtr> impl = std::make_shared<AppManagerMakePtrTest>(); std::shared_ptr<AppManagerMakePtr> impl = std::make_shared<AppManagerMakePtrTest>();
AppManagerMakePtr::GetInstance(&impl); AppManagerMakePtr::GetInstance(&impl);
} }

View File

@ -14,12 +14,12 @@
*/ */
#ifndef APP_MANAGER_MAKE_PTR_TEST_H #ifndef APP_MANAGER_MAKE_PTR_TEST_H
#define APP_MANAGER_MAKE_PTR_TEST_H #define APP_MANAGER_MAKE_PTR_TEST_H
#include "AppManagerMakePtr.h"
#include "AppManagerMock.h" #include "AppManagerMock.h"
#include "AppManagerTestTool.h" #include "AppManagerTestTool.h"
#include "SixFrameMakePtr.h"
void OverrideAppManagerMakePtrObject(std::shared_ptr<AppManagerMock> &appManagerMock); void OverrideAppManagerMakePtrObject(std::shared_ptr<AppManagerMock> &appManagerMock);
void CancelOverrideAppManagerMakePtrObject(void); void CancelOverrideAppManagerMakePtrObject(void);
class AppManagerMakePtrTest : public AppManagerMakePtr class AppManagerMakePtrTest : public SixFrameMakePtr
{ {
public: public:
AppManagerMakePtrTest(); AppManagerMakePtrTest();

View File

@ -16,7 +16,7 @@
#include "ILog.h" #include "ILog.h"
const StatusCode AppManagerTest::SetAppMonitor(std::shared_ptr<VAppMonitor> &monitor) const StatusCode AppManagerTest::SetAppMonitor(std::shared_ptr<VAppMonitor> &monitor)
{ {
LogInfo("AppManagerTest::GetAllLeds\n"); LogInfo("AppManagerTest::SetAppMonitor\n");
StatusCode code = SetAppMonitorTrace(monitor); StatusCode code = SetAppMonitorTrace(monitor);
if (StatusCodeEqual(code, "STATUS_CODE_VIRTUAL_FUNCTION")) { if (StatusCodeEqual(code, "STATUS_CODE_VIRTUAL_FUNCTION")) {
return AppManager::SetAppMonitor(monitor); return AppManager::SetAppMonitor(monitor);

View File

@ -51,9 +51,27 @@ void AppManagerTestTool::AppManagerMockInit(std::shared_ptr<IAppManager> &vMock)
auto getAppMonitor = [=](std::shared_ptr<VAppMonitor> &monitor) { auto getAppMonitor = [=](std::shared_ptr<VAppMonitor> &monitor) {
LogInfo("mAppMonitorMock get.\n"); LogInfo("mAppMonitorMock get.\n");
mAppMonitorMock = std::dynamic_pointer_cast<AppMonitorMock>(monitor); mAppMonitorMock = std::dynamic_pointer_cast<AppMonitorMock>(monitor);
AppManagerTestTool::AppMonitorInit(mAppMonitorMock);
}; };
constexpr int ONLY_BE_CALLED_ONCE = 1; constexpr int ONLY_BE_CALLED_ONCE = 1;
EXPECT_CALL(*mock.get(), SetAppMonitorTrace(_)) EXPECT_CALL(*mock.get(), SetAppMonitorTrace(_))
.Times(ONLY_BE_CALLED_ONCE) .Times(ONLY_BE_CALLED_ONCE)
.WillOnce(DoAll(WithArgs<0>(Invoke(getAppMonitor)), Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION)))); .WillOnce(DoAll(WithArgs<0>(Invoke(getAppMonitor)), Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION))));
}
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(), UploadFileTrace(_))
.WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION))));
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(), UploadFileTrace(_))
.WillRepeatedly(DoAll(Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION))));
}
} }

View File

@ -73,6 +73,7 @@ void ServersMock::MockUploadFiles(void)
ServerHttp *http = NewServersHttp(mockRequest.c_str()); ServerHttp *http = NewServersHttp(mockRequest.c_str());
if (http) { if (http) {
http->filePath = (char *)PLATFORM_PATH "/output_files/test/bin/AppManagerTest"; http->filePath = (char *)PLATFORM_PATH "/output_files/test/bin/AppManagerTest";
LogInfo("http post file:%s\n", http->filePath);
HttpPostFile(http); HttpPostFile(http);
if (http->reply) { if (http->reply) {
char *replyStr = (char *)malloc(http->replyLength + 1); char *replyStr = (char *)malloc(http->replyLength + 1);