• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "common/common.h"
16 #include "../manager/plugin_manager.h"
17 #include "NodeUniqueIdTest.h"
18 #include <arkui/native_node.h>
19 #include <string>
20 #include <arkui/native_node_napi.h>
21 
22 static ArkUI_NativeNodeAPI_1 *nodeAPI = nullptr;
23 static ArkUI_NodeHandle Column = nullptr;
24 static ArkUI_NodeHandle image = nullptr;
25 static ArkUI_NodeHandle text1 = nullptr;
26 static ArkUI_NodeHandle text2 = nullptr;
27 static ArkUI_NodeHandle text3 = nullptr;
28 static ArkUI_NodeHandle text4 = nullptr;
29 static ArkUI_NodeHandle text5 = nullptr;
30 static ArkUI_NodeHandle text6 = nullptr;
31 static ArkUI_NodeHandle text7 = nullptr;
32 
33 namespace ArkUICapiTest {
34 
CreateChildNode(ArkUI_NativeNodeAPI_1 * nodeAPI)35 static ArkUI_NodeHandle CreateChildNode(ArkUI_NativeNodeAPI_1 *nodeAPI)
36 {
37     Column = nodeAPI->createNode(ARKUI_NODE_COLUMN);
38     image = nodeAPI->createNode(ARKUI_NODE_IMAGE);
39 
40     text1 = nodeAPI->createNode(ARKUI_NODE_TEXT);
41     ArkUI_AttributeItem text1_item = {.string = "GetNodeUniqueId 正常"};
42     nodeAPI->setAttribute(text1, NODE_TEXT_CONTENT, &text1_item);
43     ArkUI_AttributeItem text1_id = {.string = "text1_id"};
44     nodeAPI->setAttribute(text1, NODE_ID, &text1_id);
45 
46     text2 = nodeAPI->createNode(ARKUI_NODE_TEXT);
47     ArkUI_AttributeItem text2_item = {.string = "GetNodeUniqueId 节点指针置空"};
48     nodeAPI->setAttribute(text2, NODE_TEXT_CONTENT, &text2_item);
49     ArkUI_AttributeItem text2_id = {.string = "text2_id"};
50     nodeAPI->setAttribute(text2, NODE_ID, &text2_id);
51 
52     text3 = nodeAPI->createNode(ARKUI_NODE_TEXT);
53     ArkUI_AttributeItem text3_item = {.string = "GetNodeUniqueId 传参nullptr"};
54     nodeAPI->setAttribute(text3, NODE_TEXT_CONTENT, &text3_item);
55     ArkUI_AttributeItem text3_id = {.string = "text3_id"};
56     nodeAPI->setAttribute(text3, NODE_ID, &text3_id);
57 
58     text4 = nodeAPI->createNode(ARKUI_NODE_TEXT);
59     ArkUI_AttributeItem text4_item = {.string = "GetNodeHandleByUniqueId 正常"};
60     nodeAPI->setAttribute(text4, NODE_TEXT_CONTENT, &text4_item);
61     ArkUI_AttributeItem text4_id = {.string = "text4_id"};
62     nodeAPI->setAttribute(text4, NODE_ID, &text4_id);
63 
64     text5 = nodeAPI->createNode(ARKUI_NODE_TEXT);
65     ArkUI_AttributeItem text5_item = {.string = "GetNodeHandleByUniqueId 传参NULL"};
66     nodeAPI->setAttribute(text5, NODE_TEXT_CONTENT, &text5_item);
67     ArkUI_AttributeItem text5_id = {.string = "text5_id"};
68     nodeAPI->setAttribute(text5, NODE_ID, &text5_id);
69 
70     text6 = nodeAPI->createNode(ARKUI_NODE_TEXT);
71     ArkUI_AttributeItem text6_item = {.string = "GetNodeHandleByUniqueId 不存在对应节点"};
72     nodeAPI->setAttribute(text6, NODE_TEXT_CONTENT, &text6_item);
73     ArkUI_AttributeItem text6_id = {.string = "text6_id"};
74     nodeAPI->setAttribute(text6, NODE_ID, &text6_id);
75 
76     text7 = nodeAPI->createNode(ARKUI_NODE_TEXT);
77     ArkUI_AttributeItem text7_item = {
78         .string = "OH_ArkUI_NodeUtils_GetLayoutPositionInGlobalDisplay "
79                   "不存在对应节点"};
80     nodeAPI->setAttribute(text7, NODE_TEXT_CONTENT, &text7_item);
81     ArkUI_AttributeItem text7_id = {.string = "text7_id"};
82     nodeAPI->setAttribute(text7, NODE_ID, &text7_id);
83 
84     nodeAPI->addChild(Column, text1);
85     nodeAPI->addChild(Column, image);
86     nodeAPI->addChild(Column, text2);
87     nodeAPI->addChild(Column, text3);
88     nodeAPI->addChild(Column, text4);
89     nodeAPI->addChild(Column, text5);
90     nodeAPI->addChild(Column, text6);
91     nodeAPI->addChild(Column, text7);
92     return Column;
93 }
94 
95 
CreatUniqueIdTest(ArkUI_NativeNodeAPI_1 * nodeAPI)96 static void CreatUniqueIdTest(ArkUI_NativeNodeAPI_1 *nodeAPI)
97 {
98     ArkUI_NumberValue background_color_value[] = {{.u32 = COLOR_GREEN}};
99     ArkUI_AttributeItem background_color_item = {background_color_value,
100         sizeof(background_color_value) / sizeof(ArkUI_NumberValue)};
101     //UniqueId主要测试代码
102     auto uniqueId = 0;
103     //ARKUI_NODE_COLUMN
104     auto errorCode1 = OH_ArkUI_NodeUtils_GetNodeUniqueId(Column, &uniqueId);
105     OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN,
106         "Manager","aaa GetColumnNodeUniqueId1 errCode:%{public}d;uniqueId:%{public}d;", errorCode1, uniqueId);
107     if( errorCode1 == ARKUI_ERROR_CODE_NO_ERROR) {
108         nodeAPI->setAttribute(text1, NODE_BACKGROUND_COLOR, &background_color_item);
109     }
110 
111     ArkUI_NodeHandle node;
112     auto errorCode2 = OH_ArkUI_NodeUtils_GetNodeHandleByUniqueId(uniqueId, &node);
113     OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN,
114         "Manager","aaa GetNodeHandle1 errCode:%{public}d;node:%{public}p;", errorCode2, node);
115     if( errorCode2 == ARKUI_ERROR_CODE_NO_ERROR) {
116         nodeAPI->setAttribute(text4, NODE_BACKGROUND_COLOR, &background_color_item);
117     }
118 
119     //节点指针置空
120     image = nullptr;
121     auto errorCode13 = OH_ArkUI_NodeUtils_GetNodeUniqueId(image, &uniqueId);
122     OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN,
123         "Manager","aaa GetNodeUniqueId_setNullptr errCode:%{public}d;uniqueId:%{public}d;", errorCode13, uniqueId);
124     if( errorCode13 == ARKUI_ERROR_CODE_PARAM_INVALID && uniqueId == -1) {
125         nodeAPI->setAttribute(text2, NODE_BACKGROUND_COLOR, &background_color_item);
126     }
127 
128     //传参nullptr
129     auto errorCode14 = OH_ArkUI_NodeUtils_GetNodeUniqueId(nullptr, &uniqueId);
130     OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN,
131         "Manager","aaa GetNodeUniqueId_nullptr errCode:%{public}d;uniqueId:%{public}d;", errorCode14, uniqueId);
132     if( errorCode14 == ARKUI_ERROR_CODE_PARAM_INVALID && uniqueId == -1) {
133         nodeAPI->setAttribute(text3, NODE_BACKGROUND_COLOR, &background_color_item);
134     }
135 
136     //传参NULL
137     auto errorCode23 = OH_ArkUI_NodeUtils_GetNodeHandleByUniqueId(NULL, &node);
138     OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN,
139         "Manager","aaa GetNodeHandle_NULL errCode:%{public}d;node:%{public}p;", errorCode23, node);
140     if( errorCode23 == ARKUI_ERROR_CODE_PARAM_INVALID && node == NULL) {
141         nodeAPI->setAttribute(text5, NODE_BACKGROUND_COLOR, &background_color_item);
142     }
143 
144     //UniqueId不存在对应节点
145     auto errorCode24 = OH_ArkUI_NodeUtils_GetNodeHandleByUniqueId(1001, &node);
146     OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN,
147         "Manager","aaa GetNodeHandle_100 errCode:%{public}d;node:%{public}p;", errorCode24, node);
148     if( errorCode24 == ARKUI_ERROR_CODE_PARAM_INVALID && node == NULL) {
149         nodeAPI->setAttribute(text6, NODE_BACKGROUND_COLOR, &background_color_item);
150     }
151 
152     // UniqueId不存在对应节点
153     ArkUI_IntOffset Offset;
154     auto errorCode25 =
155         OH_ArkUI_NodeUtils_GetLayoutPositionInGlobalDisplay(node, &Offset);
156     OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Manager",
157                  "aaa GetNodeHandle_100 errCode:%{public}d;node:%{public}p;",
158                  errorCode25, node);
159     if (errorCode25 == ARKUI_ERROR_CODE_PARAM_INVALID && node == NULL) {
160         nodeAPI->setAttribute(text7, NODE_BACKGROUND_COLOR,
161                               &background_color_item);
162     }
163 }
164 
testNodeUniqueId001(napi_env env,napi_callback_info info)165 napi_value NodeUniqueIdTest::testNodeUniqueId001(napi_env env, napi_callback_info info)
166 {
167     OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "testAddCustomProperty001", "CreateNativeNode");
168     size_t argc = PARAM_1;
169     napi_value args[PARAM_1] = {nullptr};
170     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
171     size_t length = PARAM_64;
172     size_t strLength = PARAM_0;
173     char xComponentID[PARAM_64] = {PARAM_0};
174     napi_get_value_string_utf8(env, args[PARAM_0], xComponentID, length, &strLength);
175     if ((env == nullptr) || (info == nullptr)) {
176         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "testAddCustomProperty001",
177             "GetContext env or info is null");
178         return nullptr;
179     }
180     OH_ArkUI_GetModuleInterface(ARKUI_NATIVE_NODE, ArkUI_NativeNodeAPI_1, nodeAPI);
181     auto root = CreateChildNode(nodeAPI);
182     CreatUniqueIdTest(nodeAPI);
183 
184     std::string id(xComponentID);
185     if (OH_NativeXComponent_AttachNativeRootNode(PluginManager::GetInstance()->GetNativeXComponent(id), root) ==
186         INVALID_PARAM) {
187         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "testAddCustomProperty001",
188                      "OH_NativeXComponent_AttachNativeRootNode failed");
189     }
190     napi_value exports;
191     if (napi_create_object(env, &exports) != napi_ok) {
192         napi_throw_type_error(env, nullptr, "napi_create_object failed");
193         return nullptr;
194     }
195     return exports;
196 }
197 
198 } // namespace ArkUICapiTest