From 0687f0dfe987bea2e02923d67c793343773ec452 Mon Sep 17 00:00:00 2001 From: wangxiuxiu96 Date: Thu, 12 Jun 2025 14:01:47 +0800 Subject: [PATCH] fix:add getWithRange diff Signed-off-by: wangxiuxiu96 --- .../frontend_delegate_declarative.cpp | 2 +- .../frontend_delegate_declarative.h | 2 +- .../ng/frontend_delegate_declarative_ng.cpp | 2 +- .../ng/frontend_delegate_declarative_ng.h | 2 +- .../bridge/js_frontend/frontend_delegate.h | 2 +- .../render/adapter/component_snapshot.cpp | 33 +++++++++++++--- .../js_component_snapshot.cpp | 32 ++++++++++----- .../core/rosen/rosen_render_context_test.cpp | 39 +++++++++++++++++++ 8 files changed, 94 insertions(+), 20 deletions(-) diff --git a/frameworks/bridge/declarative_frontend/frontend_delegate_declarative.cpp b/frameworks/bridge/declarative_frontend/frontend_delegate_declarative.cpp index 054e6d14ed7..626a7e2ce6a 100644 --- a/frameworks/bridge/declarative_frontend/frontend_delegate_declarative.cpp +++ b/frameworks/bridge/declarative_frontend/frontend_delegate_declarative.cpp @@ -3636,7 +3636,7 @@ std::pair> FrontendDelegateDeclarative return {ERROR_CODE_INTERNAL_ERROR, nullptr}; } -void FrontendDelegateDeclarative::GetSnapshotWithRange(const NG::NodeIdentity startID, const NG::NodeIdentity endID, +void FrontendDelegateDeclarative::GetSnapshotWithRange(const NG::NodeIdentity& startID, const NG::NodeIdentity& endID, const bool isStartRect, std::function, int32_t, std::function)>&& callback, const NG::SnapshotOptions& options) diff --git a/frameworks/bridge/declarative_frontend/frontend_delegate_declarative.h b/frameworks/bridge/declarative_frontend/frontend_delegate_declarative.h index 439cdb55c4c..2e6ae942999 100644 --- a/frameworks/bridge/declarative_frontend/frontend_delegate_declarative.h +++ b/frameworks/bridge/declarative_frontend/frontend_delegate_declarative.h @@ -309,7 +309,7 @@ public: std::pair> GetSyncSnapshotByUniqueId(int32_t uniqueId, const NG::SnapshotOptions& options) override; - void GetSnapshotWithRange(const NG::NodeIdentity startID, const NG::NodeIdentity endID, const bool isStartRect, + void GetSnapshotWithRange(const NG::NodeIdentity& startID, const NG::NodeIdentity& endID, const bool isStartRect, std::function, int32_t, std::function)>&& callback, const NG::SnapshotOptions& options) override; diff --git a/frameworks/bridge/declarative_frontend/ng/frontend_delegate_declarative_ng.cpp b/frameworks/bridge/declarative_frontend/ng/frontend_delegate_declarative_ng.cpp index 519ba29ed7a..9e7d61d238b 100644 --- a/frameworks/bridge/declarative_frontend/ng/frontend_delegate_declarative_ng.cpp +++ b/frameworks/bridge/declarative_frontend/ng/frontend_delegate_declarative_ng.cpp @@ -1397,7 +1397,7 @@ std::pair> FrontendDelegateDeclarative return {ERROR_CODE_INTERNAL_ERROR, nullptr}; } -void FrontendDelegateDeclarativeNG::GetSnapshotWithRange(const NG::NodeIdentity startID, const NG::NodeIdentity endID, +void FrontendDelegateDeclarativeNG::GetSnapshotWithRange(const NG::NodeIdentity& startID, const NG::NodeIdentity& endID, const bool isStartRect, std::function, int32_t, std::function)>&& callback, const NG::SnapshotOptions& options) diff --git a/frameworks/bridge/declarative_frontend/ng/frontend_delegate_declarative_ng.h b/frameworks/bridge/declarative_frontend/ng/frontend_delegate_declarative_ng.h index 03d2aea8e03..ae372f1bfeb 100644 --- a/frameworks/bridge/declarative_frontend/ng/frontend_delegate_declarative_ng.h +++ b/frameworks/bridge/declarative_frontend/ng/frontend_delegate_declarative_ng.h @@ -249,7 +249,7 @@ public: std::pair> GetSyncSnapshotByUniqueId(int32_t uniqueId, const NG::SnapshotOptions& options) override; - void GetSnapshotWithRange(const NG::NodeIdentity startID, const NG::NodeIdentity endID, const bool isStartRect, + void GetSnapshotWithRange(const NG::NodeIdentity& startID, const NG::NodeIdentity& endID, const bool isStartRect, std::function, int32_t, std::function)>&& callback, const NG::SnapshotOptions& options) override; diff --git a/frameworks/bridge/js_frontend/frontend_delegate.h b/frameworks/bridge/js_frontend/frontend_delegate.h index 5198f71f609..75aaebcf6a2 100644 --- a/frameworks/bridge/js_frontend/frontend_delegate.h +++ b/frameworks/bridge/js_frontend/frontend_delegate.h @@ -282,7 +282,7 @@ public: return {}; } - virtual void GetSnapshotWithRange(const NG::NodeIdentity startID, const NG::NodeIdentity endID, + virtual void GetSnapshotWithRange(const NG::NodeIdentity& startID, const NG::NodeIdentity& endID, const bool isStartRect, std::function, int32_t, std::function)>&& callback, const NG::SnapshotOptions& options) diff --git a/frameworks/core/components_ng/render/adapter/component_snapshot.cpp b/frameworks/core/components_ng/render/adapter/component_snapshot.cpp index 3eed9da762e..1d1fd9c4ec3 100644 --- a/frameworks/core/components_ng/render/adapter/component_snapshot.cpp +++ b/frameworks/core/components_ng/render/adapter/component_snapshot.cpp @@ -700,7 +700,22 @@ RefPtr ComponentSnapshot::GetRangeIDNode(const NodeIdentity& ID) if (!ID.first.empty()) { return Inspector::GetFrameNodeByKey(ID.first); } - return AceType::DynamicCast(OHOS::Ace::ElementRegister::GetInstance()->GetNodeById(ID.second)); + + auto node = OHOS::Ace::ElementRegister::GetInstance()->GetNodeById(ID.second); + if (!node) { + TAG_LOGW(AceLogTag::ACE_COMPONENT_SNAPSHOT, "Node with id %{public}d not found", ID.second); + return nullptr; + } + + auto frameNode = AceType::DynamicCast(node); + if (!frameNode) { + TAG_LOGW(AceLogTag::ACE_COMPONENT_SNAPSHOT, + "Node with id %{public}d is not a FrameNode (actual type: %{public}s)", + ID.second, AceType::TypeName(node)); + return nullptr; + } + + return frameNode; } std::string ComponentSnapshot::GetRangeIDStr(const NodeIdentity& ID) @@ -714,11 +729,17 @@ void ComponentSnapshot::GetWithRange(const NodeIdentity& startID, const NodeIden CHECK_RUN_ON(UI); auto startNode = GetRangeIDNode(startID); auto endNode = GetRangeIDNode(endID); - if (!startNode || !endNode) { + if (!startNode) { TAG_LOGW(AceLogTag::ACE_COMPONENT_SNAPSHOT, - "Can't find a component that startId or endId are " SEC_PLD(%{public}s) " and " SEC_PLD(%{public}s) - ", please check your parameters are correct", - SEC_PARAM(GetRangeIDStr(startID).c_str()), SEC_PARAM(GetRangeIDStr(endID).c_str())); + "Node not found that startld is " SEC_PLD(%{public}s), + SEC_PARAM(GetRangeIDStr(startID).c_str())); + callback(nullptr, ERROR_CODE_INTERNAL_ERROR, nullptr); + return; + } + if (!endNode) { + TAG_LOGW(AceLogTag::ACE_COMPONENT_SNAPSHOT, + "Node not found that endld is " SEC_PLD(%{public}s), + SEC_PARAM(GetRangeIDStr(endID).c_str())); callback(nullptr, ERROR_CODE_INTERNAL_ERROR, nullptr); return; } @@ -739,7 +760,7 @@ void ComponentSnapshot::GetWithRange(const NodeIdentity& startID, const NodeIden options.scale, options.scale, options.waitUntilRenderFinished); if (!isSystem) { TAG_LOGW(AceLogTag::ACE_COMPONENT_SNAPSHOT, - "No system permissions to take screenshot, please grant the permission to take screenshot"); + "No system permissions to take screenshot"); callback(nullptr, ERROR_CODE_PERMISSION_DENIED, nullptr); } } diff --git a/interfaces/napi/kits/component_snapshot/js_component_snapshot.cpp b/interfaces/napi/kits/component_snapshot/js_component_snapshot.cpp index 72cb19e4f71..17b77f6f8d6 100644 --- a/interfaces/napi/kits/component_snapshot/js_component_snapshot.cpp +++ b/interfaces/napi/kits/component_snapshot/js_component_snapshot.cpp @@ -672,13 +672,19 @@ bool JudgeRangeType(napi_env env, napi_callback_info info, int32_t argNum) JsComponentSnapshot helper(env, info); napi_valuetype type = napi_undefined; - napi_typeof(env, helper.GetArgv(argNum), &type); + napi_value argv = helper.GetArgv(argNum); + if (argv == nullptr) { + napi_close_escapable_handle_scope(env, scope); + return false; + } + napi_typeof(env, argv, &type); if (type != napi_valuetype::napi_number && type != napi_valuetype::napi_string) { TAG_LOGW(AceLogTag::ACE_COMPONENT_SNAPSHOT, "Parsing argument failed, not of number or string type."); NapiThrow(env, "parameter uniqueId is not of type number or string", ERROR_CODE_PARAM_INVALID); napi_close_escapable_handle_scope(env, scope); return false; } + napi_close_escapable_handle_scope(env, scope); return true; } @@ -689,16 +695,18 @@ bool JudgeRectValue(napi_env env, napi_callback_info info) JsComponentSnapshot helper(env, info); - if (GETWITHRANGE_ISSTARTRECT_NUMBER >= JsComponentSnapshot::ARGC_MAX) { + napi_valuetype type = napi_undefined; + napi_value argv = helper.GetArgv(GETWITHRANGE_ISSTARTRECT_NUMBER); + if (argv == nullptr) { + napi_close_escapable_handle_scope(env, scope); return true; } - - napi_valuetype type = napi_undefined; - napi_typeof(env, helper.GetArgv(GETWITHRANGE_ISSTARTRECT_NUMBER), &type); + napi_typeof(env, argv, &type); bool isRect = true; if (type == napi_valuetype::napi_boolean) { - napi_get_value_bool(env, helper.GetArgv(GETWITHRANGE_ISSTARTRECT_NUMBER), &isRect); + napi_get_value_bool(env, argv, &isRect); } + napi_close_escapable_handle_scope(env, scope); return isRect; } @@ -711,13 +719,19 @@ NG::NodeIdentity GetNodeIdentity(napi_env env, napi_callback_info info, int32_t NG::NodeIdentity nodeIdentity; napi_valuetype type = napi_undefined; - napi_typeof(env, helper.GetArgv(index), &type); + napi_value argv = helper.GetArgv(index); + if (argv == nullptr) { + napi_close_escapable_handle_scope(env, scope); + return nodeIdentity; + } + napi_typeof(env, argv, &type); if (type == napi_valuetype::napi_number) { - napi_get_value_int32(env, helper.GetArgv(index), &nodeIdentity.second); + napi_get_value_int32(env, argv, &nodeIdentity.second); } else { napi_valuetype valueType = napi_null; - GetNapiString(env, helper.GetArgv(index), nodeIdentity.first, valueType); + GetNapiString(env, argv, nodeIdentity.first, valueType); } + napi_close_escapable_handle_scope(env, scope); return nodeIdentity; } diff --git a/test/unittest/core/rosen/rosen_render_context_test.cpp b/test/unittest/core/rosen/rosen_render_context_test.cpp index ea32ba3b265..7635d05f5bd 100644 --- a/test/unittest/core/rosen/rosen_render_context_test.cpp +++ b/test/unittest/core/rosen/rosen_render_context_test.cpp @@ -1697,6 +1697,45 @@ HWTEST_F(RosenRenderContextTest, GetWithRange005, TestSize.Level1) EXPECT_EQ(errorCode, ERROR_CODE_INTERNAL_ERROR); } +/** + * @tc.name: GetWithRange006 + * @tc.desc: Test GetWithRange Func. + * @tc.type: FUNC + */ +HWTEST_F(RosenRenderContextTest, GetWithRange006, TestSize.Level1) +{ + /** + * @tc.steps: step1.Mock data. + */ + auto startNode = FrameNode::CreateFrameNode("startNode", -1, AceType::MakeRefPtr()); + auto endNode = FrameNode::CreateFrameNode("endNode", -1, AceType::MakeRefPtr()); + NodeIdentity startID; + NodeIdentity endID; + bool isStartRect = true; + int32_t errorCode = -1; + ComponentSnapshot::JsCallback callback = [&errorCode](std::shared_ptr pixmap, int32_t errCode, + std::function finishCallback) { + errorCode = errCode; + }; + SnapshotOptions options{}; + + /** + * @tc.steps: step2. Call ComponentSnapshot::GetWithRange. + * @tc.expected: Check error code. + */ + startID.first = "0"; + endID.first = "endNode"; + ComponentSnapshot snapshot; + snapshot.GetWithRange(startID, endID, isStartRect, std::move(callback), options); + EXPECT_EQ(errorCode, ERROR_CODE_INTERNAL_ERROR); + + errorCode = -1; + startID.first = "startNode"; + endID.first = "0"; + snapshot.GetWithRange(startID, endID, isStartRect, std::move(callback), options); + EXPECT_EQ(errorCode, ERROR_CODE_INTERNAL_ERROR); +} + /** * @tc.name: GetRangeIDNode001 * @tc.desc: Test GetRangeIDNode Func. -- Gitee