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_util_move_test.h"
19 #include <arkui/native_node.h>
20 #include <string>
21 #include <arkui/native_node_napi.h>
22
23 static ArkUI_NativeNodeAPI_1 *nodeAPI = nullptr;
24 static int NUMBER_0 = 0;
25 static int NUMBER_1 = 1;
26 static int NUMBER_2 = 2;
27 static int NUMBER_9 = 9;
28 static ArkUI_NodeHandle column;
29 static ArkUI_NodeHandle column1;
30 static ArkUI_NodeHandle column2;
31 static ArkUI_NodeHandle text;
32 static ArkUI_NodeHandle text1;
33 static ArkUI_NodeHandle text2;
34 static ArkUI_NodeHandle text3;
35 static ArkUI_NodeHandle text4;
36 static ArkUI_NodeHandle stack;
37 static ArkUI_NodeHandle bt0;
38 static ArkUI_NodeHandle bt1;
39 struct A {
40 ArkUI_NodeHandle child;
41 ArkUI_NodeHandle parent;
42 ArkUI_NodeHandle parent2;
43 int32_t index;
44 int32_t index2;
45 };
46
47 namespace ArkUICapiTest {
48
CreateChildNode(ArkUI_NativeNodeAPI_1 * nodeAPI)49 static ArkUI_NodeHandle CreateChildNode(ArkUI_NativeNodeAPI_1 *nodeAPI)
50 {
51 column = nodeAPI->createNode(ARKUI_NODE_COLUMN);
52 column1 = nodeAPI->createNode(ARKUI_NODE_COLUMN);
53 column2 = nodeAPI->createNode(ARKUI_NODE_COLUMN);
54 ArkUI_AttributeItem column1_id = {.string = "column1_id"};
55 nodeAPI->setAttribute(column1, NODE_ID, &column1_id);
56 ArkUI_AttributeItem column2_id = {.string = "column2_id"};
57 nodeAPI->setAttribute(column2, NODE_ID, &column2_id);
58 ArkUI_NumberValue value[] = {200};
59 ArkUI_AttributeItem item = {value, sizeof(value) / sizeof(ArkUI_NumberValue)};
60 nodeAPI->setAttribute(column, NODE_WIDTH, &item);
61 nodeAPI->setAttribute(column1, NODE_WIDTH, &item);
62 nodeAPI->setAttribute(column2, NODE_WIDTH, &item);
63 ArkUI_NumberValue value1[] = {200};
64 ArkUI_AttributeItem item1 = {value1, sizeof(value1) / sizeof(ArkUI_NumberValue)};
65 nodeAPI->setAttribute(column, NODE_HEIGHT, &item1);
66 ArkUI_NumberValue value2[] = {100};
67 ArkUI_AttributeItem item2 = {value2, sizeof(value2) / sizeof(ArkUI_NumberValue)};
68 nodeAPI->setAttribute(column1, NODE_HEIGHT, &item2);
69 nodeAPI->setAttribute(column2, NODE_HEIGHT, &item2);
70
71 ArkUI_NodeHandle row0 = nodeAPI->createNode(ARKUI_NODE_ROW);
72 bt0 = nodeAPI->createNode(ARKUI_NODE_BUTTON);
73 ArkUI_AttributeItem bt0_id = {.string = "bt0_id"};
74 nodeAPI->setAttribute(bt0, NODE_ID, &bt0_id);
75 ArkUI_AttributeItem bt0_item = {.string = "appendCol2"};
76 nodeAPI->setAttribute(bt0, NODE_BUTTON_LABEL, &bt0_item);
77 nodeAPI->registerNodeEvent(bt0, NODE_ON_CLICK, NUMBER_0, nullptr);
78
79 bt1 = nodeAPI->createNode(ARKUI_NODE_BUTTON);
80 ArkUI_AttributeItem bt1_id = {.string = "bt1_id"};
81 nodeAPI->setAttribute(bt1, NODE_ID, &bt1_id);
82 ArkUI_AttributeItem bt1_item = {.string = "error code"};
83 nodeAPI->setAttribute(bt1, NODE_BUTTON_LABEL, &bt1_item);
84 nodeAPI->registerNodeEvent(bt1, NODE_ON_CLICK, NUMBER_9, nullptr);
85
86 nodeAPI->addChild(column, column1);
87 nodeAPI->addChild(row0, bt0);
88 nodeAPI->addChild(row0, bt1);
89 nodeAPI->addChild(column, row0);
90
91 text = nodeAPI->createNode(ARKUI_NODE_XCOMPONENT);
92 text1 = nodeAPI->createNode(ARKUI_NODE_XCOMPONENT);
93 text2 = nodeAPI->createNode(ARKUI_NODE_XCOMPONENT);
94 text3 = nodeAPI->createNode(ARKUI_NODE_XCOMPONENT);
95 text4 = nodeAPI->createNode(ARKUI_NODE_XCOMPONENT);
96
97 ArkUI_NumberValue background_color_value1[] = {{.u32 = COLOR_RED}};
98 ArkUI_AttributeItem background_color_item1 = {background_color_value1,
99 sizeof(background_color_value1) / sizeof(ArkUI_NumberValue)};
100 nodeAPI->setAttribute(text, NODE_BACKGROUND_COLOR, &background_color_item1);
101 ArkUI_AttributeItem itemStringStyle0 = {.string = "xc1"};
102 nodeAPI->setAttribute(text, NODE_ID, &itemStringStyle0);
103 ArkUI_NumberValue background_color_value2[] = {{.u32 = COLOR_GREEN}};
104 ArkUI_AttributeItem background_color_item2 = {background_color_value2,
105 sizeof(background_color_value2) / sizeof(ArkUI_NumberValue)};
106 ArkUI_NumberValue background_color_value3[] = {{.u32 = COLOR_BLUE}};
107 ArkUI_AttributeItem background_color_item3 = {background_color_value3,
108 sizeof(background_color_value3) / sizeof(ArkUI_NumberValue)};
109 nodeAPI->setAttribute(text1, NODE_BACKGROUND_COLOR, &background_color_item2);
110 nodeAPI->setAttribute(text2, NODE_BACKGROUND_COLOR, &background_color_item3);
111 nodeAPI->setAttribute(text3, NODE_BACKGROUND_COLOR, &background_color_item2);
112 nodeAPI->setAttribute(text4, NODE_BACKGROUND_COLOR, &background_color_item3);
113
114 ArkUI_NumberValue value5[] = {100};
115 ArkUI_AttributeItem item5 = {value5, sizeof(value5) / sizeof(ArkUI_NumberValue)};
116 nodeAPI->setAttribute(text, NODE_WIDTH, &item5);
117 nodeAPI->setAttribute(text1, NODE_WIDTH, &item5);
118 nodeAPI->setAttribute(text2, NODE_WIDTH, &item5);
119 nodeAPI->setAttribute(text3, NODE_WIDTH, &item5);
120 nodeAPI->setAttribute(text4, NODE_WIDTH, &item5);
121
122 ArkUI_NumberValue value6[] = {10};
123 ArkUI_AttributeItem item6 = {value6, sizeof(value6) / sizeof(ArkUI_NumberValue)};
124 nodeAPI->setAttribute(text, NODE_HEIGHT, &item6);
125 nodeAPI->setAttribute(text1, NODE_HEIGHT, &item6);
126 nodeAPI->setAttribute(text2, NODE_HEIGHT, &item6);
127 nodeAPI->setAttribute(text3, NODE_HEIGHT, &item6);
128 nodeAPI->setAttribute(text4, NODE_HEIGHT, &item6);
129
130 nodeAPI->addChild(column1, text);
131 nodeAPI->addChild(column1, text1);
132 nodeAPI->addChild(column1, text2);
133 nodeAPI->addChild(column2, text3);
134 nodeAPI->addChild(column2, text4);
135 return column;
136 }
137
testNodeUtilMove001(napi_env env,napi_callback_info info)138 napi_value NodeUtilMoveTest::testNodeUtilMove001(napi_env env, napi_callback_info info)
139 {
140 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "testAddCustomProperty001", "CreateNativeNode");
141 size_t argc = PARAM_1;
142 napi_value args[PARAM_1] = {nullptr};
143 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
144 size_t length = PARAM_64;
145 size_t strLength = PARAM_0;
146 char xComponentID[PARAM_64] = {PARAM_0};
147 napi_get_value_string_utf8(env, args[PARAM_0], xComponentID, length, &strLength);
148 if ((env == nullptr) || (info == nullptr)) {
149 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "testAddCustomProperty001",
150 "GetContext env or info is null");
151 return nullptr;
152 }
153 OH_ArkUI_GetModuleInterface(ARKUI_NATIVE_NODE, ArkUI_NativeNodeAPI_1, nodeAPI);
154 static ArkUI_NodeHandle startNode = CreateChildNode(nodeAPI);
155
156 A *a = new A;
157 a->child = text;
158 a->parent = column2;
159 a->parent2 = column1;
160 a->index = NUMBER_0;
161 a->index2 = NUMBER_2;
162
163 nodeAPI->registerNodeEvent(column1, NODE_ON_CLICK, NUMBER_1, a);
164 nodeAPI->registerNodeEvent(column2, NODE_ON_CLICK, NUMBER_2, a);
165
166 nodeAPI->registerNodeEventReceiver([](ArkUI_NodeEvent *event) {
167 auto targetId = OH_ArkUI_NodeEvent_GetTargetId(event);
168 auto *nodeAPI = reinterpret_cast<ArkUI_NativeNodeAPI_1 *>(
169 OH_ArkUI_QueryModuleInterfaceByName(ARKUI_NATIVE_NODE, "ArkUI_NativeNodeAPI_1"));
170 if (targetId == NUMBER_0) {
171 nodeAPI->addChild(column, column2);
172 }
173 if (targetId == NUMBER_1) {
174 auto err = OH_ArkUI_NodeUtils_MoveTo(text, column2, 0);
175 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Manager", "kkk click1 = %{public}d", err);
176 auto parent = OH_ArkUI_NodeUtils_GetParentInPageTree(text);
177 auto getParentId = nodeAPI->getAttribute(parent, NODE_ID)->string;
178 if (strcmp(getParentId, "column2_id") == 0) {
179 ArkUI_NumberValue background_color_value[] = {{.u32 = COLOR_GREEN}};
180 ArkUI_AttributeItem background_color_item = {background_color_value,
181 sizeof(background_color_value) / sizeof(ArkUI_NumberValue)};
182 nodeAPI->setAttribute(bt0, NODE_BACKGROUND_COLOR, &background_color_item);
183 }
184 }
185 if (targetId == NUMBER_9) {
186 auto err1 = OH_ArkUI_NodeUtils_MoveTo(nullptr, column1, 0);
187 auto err2 = OH_ArkUI_NodeUtils_MoveTo(text, column2, 2);
188 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Manager", "kkk click2 = %{public}d", err1);
189 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Manager", "kkk click2 = %{public}d", err2);
190 if (err1 == ARKUI_ERROR_CODE_PARAM_INVALID && err2 == ARKUI_ERROR_CODE_NO_ERROR) {
191 ArkUI_NumberValue background_color_value[] = {{.u32 = COLOR_GREEN}};
192 ArkUI_AttributeItem background_color_item = {background_color_value,
193 sizeof(background_color_value) / sizeof(ArkUI_NumberValue)};
194 nodeAPI->setAttribute(bt1, NODE_BACKGROUND_COLOR, &background_color_item);
195 }
196 }
197
198 });
199
200 std::string id(xComponentID);
201 if (OH_NativeXComponent_AttachNativeRootNode(PluginManager::GetInstance()->GetNativeXComponent(id), startNode) ==
202 INVALID_PARAM) {
203 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "testAddCustomProperty001",
204 "OH_NativeXComponent_AttachNativeRootNode failed");
205 }
206 napi_value exports;
207 if (napi_create_object(env, &exports) != napi_ok) {
208 napi_throw_type_error(env, nullptr, "napi_create_object failed");
209 return nullptr;
210 }
211 return exports;
212 }
213
214
215 } // namespace ArkUICapiTest