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
16 #include "common/common.h"
17 #include "../manager/plugin_manager.h"
18 #include "node_handle_by_id_test.h"
19 #include <arkui/native_node.h>
20 #include <string>
21 ArkUI_NativeNodeAPI_1 *nodeAPI1 = nullptr;
22 static int NUMBER_0 = 0;
23 static int NUMBER_1 = 1;
24 static int NUMBER_2 = 2;
25 static int NUMBER_9 = 9;
26
27 namespace ArkUICapiTest {
CreateChildNode(ArkUI_NativeNodeAPI_1 * nodeAPI)28 static ArkUI_NodeHandle CreateChildNode(ArkUI_NativeNodeAPI_1 *nodeAPI)
29 {
30 // column
31 ArkUI_NodeHandle column = nodeAPI->createNode(ARKUI_NODE_COLUMN);
32
33 // 创建文本
34 ArkUI_NodeHandle text = nodeAPI->createNode(ARKUI_NODE_TEXT);
35 ArkUI_AttributeItem text_item = {.string = "C API"};
36 nodeAPI->setAttribute(text, NODE_TEXT_CONTENT, &text_item);
37 ArkUI_AttributeItem text_id = {.string = "Text_CAPI"};
38 nodeAPI->setAttribute(text, NODE_ID, &text_id);
39
40 // 创建button
41 ArkUI_NodeHandle button = nodeAPI->createNode(ARKUI_NODE_BUTTON);
42 ArkUI_AttributeItem button_item = {.string = "Click"};
43 nodeAPI->setAttribute(button, NODE_BUTTON_LABEL, &button_item);
44 ArkUI_AttributeItem button_id = {.string = "Same_CAPI"};
45 nodeAPI->setAttribute(button, NODE_ID, &button_id);
46
47 // 创建文本
48 ArkUI_NodeHandle text1 = nodeAPI->createNode(ARKUI_NODE_TEXT);
49 ArkUI_AttributeItem text_item1 = {.string = "C API1"};
50 nodeAPI->setAttribute(text1, NODE_TEXT_CONTENT, &text_item1);
51 ArkUI_AttributeItem text_id1 = {.string = "Same_CAPI"};
52 nodeAPI->setAttribute(text1, NODE_ID, &text_id);
53
54 // 创建文本2
55 ArkUI_NodeHandle text2 = nodeAPI->createNode(ARKUI_NODE_TEXT);
56 ArkUI_AttributeItem text_item2 = {.string = "C API2"};
57 nodeAPI->setAttribute(text2, NODE_TEXT_CONTENT, &text_item2);
58 ArkUI_AttributeItem text_id2 = {.string = "Same_CAPI2"};
59 nodeAPI->setAttribute(text2, NODE_ID, &text_id2);
60
61 // 创建button2
62 ArkUI_NodeHandle button2 = nodeAPI->createNode(ARKUI_NODE_BUTTON);
63 ArkUI_AttributeItem button_item2 = {.string = "Click2"};
64 nodeAPI->setAttribute(button2, NODE_BUTTON_LABEL, &button_item2);
65 ArkUI_AttributeItem button_id2 = {.string = "Same_CAPI2"};
66 nodeAPI->setAttribute(button2, NODE_ID, &button_id2);
67
68 // 创建bt0
69 ArkUI_NodeHandle bt0 = nodeAPI->createNode(ARKUI_NODE_BUTTON);
70 ArkUI_AttributeItem bt0_item = {.string = "no error"};
71 nodeAPI->setAttribute(bt0, NODE_BUTTON_LABEL, &bt0_item);
72 ArkUI_AttributeItem bt0_id = {.string = "button_id0"};
73 nodeAPI->setAttribute(bt0, NODE_ID, &bt0_id);
74 nodeAPI->registerNodeEvent(bt0, NODE_ON_CLICK, NUMBER_0, nullptr);
75
76 // 创建bt1
77 ArkUI_NodeHandle bt1 = nodeAPI->createNode(ARKUI_NODE_BUTTON);
78 ArkUI_AttributeItem bt1_item = {.string = "invalid"};
79 nodeAPI->setAttribute(bt1, NODE_BUTTON_LABEL, &bt1_item);
80 ArkUI_AttributeItem bt1_id = {.string = "button_id1"};
81 nodeAPI->setAttribute(bt1, NODE_ID, &bt1_id);
82 nodeAPI->registerNodeEvent(bt1, NODE_ON_CLICK, NUMBER_1, nullptr);
83
84 // 创建bt2
85 ArkUI_NodeHandle bt2 = nodeAPI->createNode(ARKUI_NODE_BUTTON);
86 ArkUI_AttributeItem bt2_item = {.string = "same id"};
87 nodeAPI->setAttribute(bt2, NODE_BUTTON_LABEL, &bt2_item);
88 ArkUI_AttributeItem bt2_id = {.string = "button_id2"};
89 nodeAPI->setAttribute(bt2, NODE_ID, &bt2_id);
90 nodeAPI->registerNodeEvent(bt2, NODE_ON_CLICK, NUMBER_2, nullptr);
91
92 nodeAPI->addChild(column, text);
93 nodeAPI->addChild(column, button);
94 nodeAPI->addChild(column, text1);
95 nodeAPI->addChild(column, text2);
96 nodeAPI->addChild(column, button2);
97 nodeAPI->addChild(column, bt0);
98 nodeAPI->addChild(column, bt1);
99 nodeAPI->addChild(column, bt2);
100 return column;
101 }
102
OnEventReceive(ArkUI_NodeEvent * event)103 static void OnEventReceive(ArkUI_NodeEvent *event)
104 {
105 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "testNodeHandleById001", "OnEventReceive");
106 if (event == nullptr) {
107 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "testNodeHandleById001",
108 "OnEventReceive: event is null");
109 return;
110 }
111
112 auto targetId = OH_ArkUI_NodeEvent_GetTargetId(event);
113 auto node = OH_ArkUI_NodeEvent_GetNodeHandle(event);
114 ArkUI_NumberValue background_color_value[] = {{.u32 = COLOR_GREEN}};
115 ArkUI_AttributeItem background_color_item = {background_color_value,
116 sizeof(background_color_value) / sizeof(ArkUI_NumberValue)};
117 ArkUI_NodeHandle node_ptr = nullptr;
118 if (targetId == NUMBER_0) {
119 auto ret = OH_ArkUI_NodeUtils_GetAttachedNodeHandleById("Text_CAPI", &node_ptr);
120 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Manager",
121 "kkk getNodeType %{public}d --ec = %{public}d ", OH_ArkUI_NodeUtils_GetNodeType(node_ptr), ret);
122 if (ret == ARKUI_ERROR_CODE_NO_ERROR) {
123 nodeAPI1->setAttribute(node, NODE_BACKGROUND_COLOR, &background_color_item);
124 }
125 }
126 if (targetId == NUMBER_1) {
127 auto ret = OH_ArkUI_NodeUtils_GetAttachedNodeHandleById("NoId", &node_ptr);
128 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Manager",
129 "kkk getNodeType %{public}d --ec = %{public}d ", OH_ArkUI_NodeUtils_GetNodeType(node_ptr), ret);
130 if (ret == ARKUI_ERROR_CODE_PARAM_INVALID) {
131 nodeAPI1->setAttribute(node, NODE_BACKGROUND_COLOR, &background_color_item);
132 }
133 }
134 if (targetId == NUMBER_2) {
135 ArkUI_NodeHandle node_ptr1 = nullptr;
136 auto ret = OH_ArkUI_NodeUtils_GetAttachedNodeHandleById("Same_CAPI", &node_ptr);
137 auto ret1 = OH_ArkUI_NodeUtils_GetAttachedNodeHandleById("Same_CAPI2", &node_ptr1);
138 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Manager",
139 "kkk getNodeType %{public}d --ec = %{public}d ", OH_ArkUI_NodeUtils_GetNodeType(node_ptr), ret);
140 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Manager",
141 "kkk getNodeType %{public}d --ec = %{public}d ", OH_ArkUI_NodeUtils_GetNodeType(node_ptr1), ret1);
142 if (OH_ArkUI_NodeUtils_GetNodeType(node_ptr) == NUMBER_9 && OH_ArkUI_NodeUtils_GetNodeType(node_ptr1) == NUMBER_1) {
143 nodeAPI1->setAttribute(node, NODE_BACKGROUND_COLOR, &background_color_item);
144 }
145 }
146 }
147
testNodeHandleById001(napi_env env,napi_callback_info info)148 napi_value NodeHandleByIdTest::testNodeHandleById001(napi_env env, napi_callback_info info)
149 {
150 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "testNodeHandleByIdTest001", "CreateNativeNode");
151 size_t argc = PARAM_1;
152 napi_value args[PARAM_1] = {nullptr};
153 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
154 size_t length = PARAM_64;
155 size_t strLength = PARAM_0;
156 char xComponentID[PARAM_64] = {PARAM_0};
157 napi_get_value_string_utf8(env, args[PARAM_0], xComponentID, length, &strLength);
158 if ((env == nullptr) || (info == nullptr)) {
159 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "testNodeHandleByIdTest001",
160 "GetContext env or info is null");
161 return nullptr;
162 }
163 OH_ArkUI_GetModuleInterface(ARKUI_NATIVE_NODE, ArkUI_NativeNodeAPI_1, nodeAPI1);
164 static ArkUI_NodeHandle root = CreateChildNode(nodeAPI1);
165 nodeAPI1->registerNodeEventReceiver(&OnEventReceive);
166
167 std::string id(xComponentID);
168 if (OH_NativeXComponent_AttachNativeRootNode(PluginManager::GetInstance()->GetNativeXComponent(id), root) ==
169 INVALID_PARAM) {
170 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "testAddCustomProperty001",
171 "OH_NativeXComponent_AttachNativeRootNode failed");
172 }
173 napi_value exports;
174 if (napi_create_object(env, &exports) != napi_ok) {
175 napi_throw_type_error(env, nullptr, "napi_create_object failed");
176 return nullptr;
177 }
178 return exports;
179 }
180
181 } // namespace ArkUICapiTest