From e4637ce48d7efafcc8c6ded128023aa8066ebaf3 Mon Sep 17 00:00:00 2001 From: tantingting Date: Wed, 21 Aug 2024 10:53:32 +0800 Subject: [PATCH] add inspector Signed-off-by: tantingting --- inspector/connect_inspector.cpp | 34 ++++++++++++++- inspector/connect_inspector.h | 8 +++- inspector/test/connect_server_test.cpp | 58 ++++++++++++++++---------- 3 files changed, 77 insertions(+), 23 deletions(-) diff --git a/inspector/connect_inspector.cpp b/inspector/connect_inspector.cpp index 200d2842..fd427d30 100644 --- a/inspector/connect_inspector.cpp +++ b/inspector/connect_inspector.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. * 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 @@ -26,6 +26,8 @@ static constexpr char REQUEST_MESSAGE[] = "tree"; static constexpr char STOPDEBUGGER_MESSAGE[] = "stopDebugger"; static constexpr char OPEN_ARKUI_STATE_PROFILER[] = "ArkUIStateProfilerOpen"; static constexpr char CLOSE_ARKUI_STATE_PROFILER[] = "ArkUIStateProfilerClose"; +static constexpr char START_RECORD_MESSAGE[] = "rsNodeStartRecord"; +static constexpr char STOP_RECORD_MESSAGE[] = "rsNodeStopRecord"; std::function g_setConnectCallBack; void* HandleDebugManager(void* const server) @@ -67,6 +69,25 @@ void OnOpenMessage(const std::string& message) } } +void OnInspectorRecordMessage(const std::string& message) +{ + if (message.find(START_RECORD_MESSAGE, 0) != std::string::npos) { + if (g_inspector->startRecord_ != nullptr && !g_inspector->isRecording_) { + LOGI("record start"); + g_inspector->startRecord_(); + g_inspector->isRecording_ = true; + } + } + + if (message.find(STOP_RECORD_MESSAGE, 0) != std::string::npos) { + if (g_inspector->stopRecord_ != nullptr && g_inspector->isRecording_) { + LOGI("record stop"); + g_inspector->stopRecord_(); + g_inspector->isRecording_ = false; + } + } +} + void OnMessage(const std::string& message) { std::lock_guard lock(g_connectMutex); @@ -115,6 +136,7 @@ void OnMessage(const std::string& message) g_inspector->setDebugMode_(); } } + OnInspectorRecordMessage(message); } } @@ -269,4 +291,14 @@ void SetProfilerCallback(const std::function &setArkUIStateProfilerS g_inspector->setArkUIStateProfilerStatus_ = setArkUIStateProfilerStatus; } +void SetRecordCallback(const std::function &startRecordFunc, + const std::function &stopRecordFunc) +{ + std::lock_guard lock(g_connectMutex); + if (g_inspector == nullptr) { + g_inspector = std::make_unique(); + } + g_inspector->startRecord_ = startRecordFunc; + g_inspector->stopRecord_ = stopRecordFunc; +} } // OHOS::ArkCompiler::Toolchain diff --git a/inspector/connect_inspector.h b/inspector/connect_inspector.h index 4225304f..2d0420a8 100644 --- a/inspector/connect_inspector.h +++ b/inspector/connect_inspector.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-2024 Huawei Device Co., Ltd. * 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 @@ -59,6 +59,9 @@ void SetSwitchCallBack(const std::function& setSwitchStatus, void SetProfilerCallback(const std::function &setArkUIStateProfilerStatus); +void SetRecordCallback(const std::function &startRecordFunc, + const std::function &stopRecordFunc); + #ifdef __cplusplus #if __cplusplus } @@ -86,6 +89,9 @@ public: std::function createLayoutInfo_; std::function setDebugMode_; int32_t instanceId_ = -1; + std::function startRecord_; + std::function stopRecord_; + bool isRecording_ = false; }; } // namespace OHOS::ArkCompiler::Toolchain diff --git a/inspector/test/connect_server_test.cpp b/inspector/test/connect_server_test.cpp index 1eeeba1a..61934b14 100644 --- a/inspector/test/connect_server_test.cpp +++ b/inspector/test/connect_server_test.cpp @@ -37,6 +37,7 @@ public: void SetUp() override {} void TearDown() override {} + void ReturnValueVerification(); #if defined(OHOS_PLATFORM) static constexpr char CONNECTED_MESSAGE_TEST[] = "connected"; static constexpr char OPEN_MESSAGE_TEST[] = "layoutOpen"; @@ -45,11 +46,14 @@ public: static constexpr char STOPDEBUGGER_MESSAGE_TEST[] = "stopDebugger"; static constexpr char OPEN_ARKUI_STATE_PROFILER_TEST[] = "ArkUIStateProfilerOpen"; static constexpr char CLOSE_ARKUI_STATE_PROFILER_TEST[] = "ArkUIStateProfilerClose"; + static constexpr char START_RECORD_MESSAGE_TEST[] = "rsNodeStartRecord"; + static constexpr char STOP_RECORD_MESSAGE_TEST[] = "rsNodeStopRecord"; static constexpr char HELLO_INSPECTOR_CLIENT[] = "hello inspector client"; static constexpr char INSPECTOR_SERVER_OK[] = "inspector server ok"; static constexpr char INSPECTOR_RUN[] = "inspector run"; static constexpr char INSPECTOR_QUIT[] = "inspector quit"; + static constexpr int32_t WAIT_TIME = 2; #endif }; @@ -83,9 +87,37 @@ void CallbackInit() g_createInfoId = id; }; SetSwitchCallBack(switchStatusCb, createInfoCb, g_instanceId); + + auto startRecordFunc = []() -> void {}; + auto stopRecordFunc = []() -> void {}; + SetRecordCallback(startRecordFunc, stopRecordFunc); GTEST_LOG_(INFO) << "ConnectServerTest::CallbackInit finished"; } +void ConnectServerTest::ReturnValueVerification() +{ + // Waiting for executing the message instruction sent by the client + sleep(WAIT_TIME); + ASSERT_TRUE(g_profilerFlag); + ASSERT_EQ(g_createInfoId, g_instanceId); + ASSERT_TRUE(g_switchStatus); + ASSERT_TRUE(g_connectFlag); + ASSERT_FALSE(WaitForConnection()); + + SendMessage(INSPECTOR_SERVER_OK); + SendLayoutMessage(INSPECTOR_RUN); + + // Waiting for executing the message instruction sent by the client + sleep(WAIT_TIME); + ASSERT_FALSE(g_profilerFlag); + ASSERT_FALSE(g_switchStatus); + ASSERT_FALSE(g_connectFlag); + + SendProfilerMessage(INSPECTOR_QUIT); + + StopServer("InspectorConnectTest"); +} + HWTEST_F(ConnectServerTest, InspectorBasicTest, testing::ext::TestSize.Level0) { ASSERT_TRUE(WaitForConnection()); @@ -109,6 +141,8 @@ HWTEST_F(ConnectServerTest, InspectorConnectTest, testing::ext::TestSize.Level0) EXPECT_TRUE(clientSocket.SendReply(REQUEST_MESSAGE_TEST)); EXPECT_TRUE(clientSocket.SendReply(OPEN_MESSAGE_TEST)); EXPECT_TRUE(clientSocket.SendReply(CONNECTED_MESSAGE_TEST)); + EXPECT_TRUE(clientSocket.SendReply(START_RECORD_MESSAGE_TEST)); + EXPECT_TRUE(clientSocket.SendReply(STOP_RECORD_MESSAGE_TEST)); EXPECT_STREQ(clientSocket.Decode().c_str(), HELLO_INSPECTOR_CLIENT); @@ -125,27 +159,9 @@ HWTEST_F(ConnectServerTest, InspectorConnectTest, testing::ext::TestSize.Level0) clientSocket.Close(); exit(0); } else if (pid > 0) { - // Waiting for executing the message instruction sent by the client - sleep(2); - ASSERT_TRUE(g_profilerFlag); - ASSERT_EQ(g_createInfoId, g_instanceId); - ASSERT_TRUE(g_switchStatus); - ASSERT_TRUE(g_connectFlag); - ASSERT_FALSE(WaitForConnection()); - - SendMessage(INSPECTOR_SERVER_OK); - SendLayoutMessage(INSPECTOR_RUN); - - // Waiting for executing the message instruction sent by the client - sleep(2); - ASSERT_FALSE(g_profilerFlag); - ASSERT_FALSE(g_switchStatus); - ASSERT_FALSE(g_connectFlag); - - SendProfilerMessage(INSPECTOR_QUIT); - - StopServer("InspectorConnectTest"); - } else { + ReturnValueVerification(); + } + else { std::cerr << "InspectorConnectTest::fork failed" << std::endl; } RemoveMessage(g_instanceId); -- Gitee