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 "get_window_info_test.h"
18 #include <arkui/native_node.h>
19 #include <string>
20
21 ArkUI_NativeNodeAPI_1 *nodeAPI2 = nullptr;
22 ArkUI_NodeHandle button_visibility = nullptr;
23 static int NUMBER_0 = 0;
24 static int NUMBER_1 = 1;
25 static int NUMBER_2 = 2;
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 // 创建bt0
33 ArkUI_NodeHandle bt0 = nodeAPI->createNode(ARKUI_NODE_BUTTON);
34 ArkUI_AttributeItem bt0_item = {.string = "no error"};
35 nodeAPI->setAttribute(bt0, NODE_BUTTON_LABEL, &bt0_item);
36 ArkUI_AttributeItem bt0_id = {.string = "button_id0"};
37 nodeAPI->setAttribute(bt0, NODE_ID, &bt0_id);
38 nodeAPI->registerNodeEvent(bt0, NODE_ON_CLICK, NUMBER_0, nullptr);
39 // 创建bt1
40 ArkUI_NodeHandle bt1 = nodeAPI->createNode(ARKUI_NODE_BUTTON);
41 ArkUI_AttributeItem bt1_item = {.string = "invalid"};
42 nodeAPI->setAttribute(bt1, NODE_BUTTON_LABEL, &bt1_item);
43 ArkUI_AttributeItem bt1_id = {.string = "button_id1"};
44 nodeAPI->setAttribute(bt1, NODE_ID, &bt1_id);
45 nodeAPI->registerNodeEvent(bt1, NODE_ON_CLICK, NUMBER_1, nullptr);
46 // 创建bt2
47 ArkUI_NodeHandle bt2 = nodeAPI->createNode(ARKUI_NODE_BUTTON);
48 ArkUI_AttributeItem bt2_item = {.string = "same id"};
49 nodeAPI->setAttribute(bt2, NODE_BUTTON_LABEL, &bt2_item);
50 ArkUI_AttributeItem bt2_id = {.string = "button_id2"};
51 nodeAPI->setAttribute(bt2, NODE_ID, &bt2_id);
52 nodeAPI->registerNodeEvent(bt2, NODE_ON_CLICK, NUMBER_2, nullptr);
53 nodeAPI->addChild(column, bt0);
54 nodeAPI->addChild(column, bt1);
55 nodeAPI->addChild(column, bt2);
56 return column;
57 }
58
CreateChildNode1(ArkUI_NativeNodeAPI_1 * nodeAPI)59 static ArkUI_NodeHandle CreateChildNode1(ArkUI_NativeNodeAPI_1 *nodeAPI)
60 {
61 // column
62 ArkUI_NodeHandle column = nodeAPI->createNode(ARKUI_NODE_COLUMN);
63 // 创建bt0
64 ArkUI_NodeHandle bt0 = nodeAPI->createNode(ARKUI_NODE_BUTTON);
65 ArkUI_AttributeItem bt0_item = {.string = "get window name"};
66 nodeAPI->setAttribute(bt0, NODE_BUTTON_LABEL, &bt0_item);
67 ArkUI_AttributeItem bt0_id = {.string = "button_id0"};
68 nodeAPI->setAttribute(bt0, NODE_ID, &bt0_id);
69 nodeAPI->registerNodeEvent(bt0, NODE_ON_CLICK, NUMBER_0, nullptr);
70 nodeAPI->addChild(column, bt0);
71 return column;
72 }
73
CreateChildNode2(ArkUI_NativeNodeAPI_1 * nodeAPI)74 static ArkUI_NodeHandle CreateChildNode2(ArkUI_NativeNodeAPI_1 *nodeAPI)
75 {
76 // column
77 ArkUI_NodeHandle column = nodeAPI->createNode(ARKUI_NODE_COLUMN);
78 // 创建button
79 button_visibility = nodeAPI->createNode(ARKUI_NODE_BUTTON);
80 ArkUI_AttributeItem button_item = {.string = "visibility"};
81 nodeAPI->setAttribute(button_visibility, NODE_BUTTON_LABEL, &button_item);
82 // 创建bt0
83 ArkUI_NodeHandle bt0 = nodeAPI->createNode(ARKUI_NODE_BUTTON);
84 ArkUI_AttributeItem bt0_item = {.string = "visible"};
85 nodeAPI->setAttribute(bt0, NODE_BUTTON_LABEL, &bt0_item);
86 ArkUI_AttributeItem bt0_id = {.string = "button_id0"};
87 nodeAPI->setAttribute(bt0, NODE_ID, &bt0_id);
88 nodeAPI->registerNodeEvent(bt0, NODE_ON_CLICK, NUMBER_0, nullptr);
89 // 创建bt1
90 ArkUI_NodeHandle bt1 = nodeAPI->createNode(ARKUI_NODE_BUTTON);
91 ArkUI_AttributeItem bt1_item = {.string = "none"};
92 nodeAPI->setAttribute(bt1, NODE_BUTTON_LABEL, &bt1_item);
93 ArkUI_AttributeItem bt1_id = {.string = "button_id1"};
94 nodeAPI->setAttribute(bt1, NODE_ID, &bt1_id);
95 nodeAPI->registerNodeEvent(bt1, NODE_ON_CLICK, NUMBER_1, nullptr);
96 // 创建bt2
97 ArkUI_NodeHandle bt2 = nodeAPI->createNode(ARKUI_NODE_BUTTON);
98 ArkUI_AttributeItem bt2_item = {.string = "hidden"};
99 nodeAPI->setAttribute(bt2, NODE_BUTTON_LABEL, &bt2_item);
100 ArkUI_AttributeItem bt2_id = {.string = "button_id2"};
101 nodeAPI->setAttribute(bt2, NODE_ID, &bt2_id);
102 nodeAPI->registerNodeEvent(bt2, NODE_ON_CLICK, NUMBER_2, nullptr);
103 nodeAPI->addChild(column, button_visibility);
104 nodeAPI->addChild(column, bt0);
105 nodeAPI->addChild(column, bt1);
106 nodeAPI->addChild(column, bt2);
107 return column;
108 }
109
OnEventReceive(ArkUI_NodeEvent * event)110 static void OnEventReceive(ArkUI_NodeEvent *event)
111 {
112 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "testGetWindowInfo001", "OnEventReceive");
113 if (event == nullptr) {
114 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "testGetWindowInfo001",
115 "OnEventReceive: event is null");
116 return;
117 }
118 auto targetId = OH_ArkUI_NodeEvent_GetTargetId(event);
119 auto node = OH_ArkUI_NodeEvent_GetNodeHandle(event);
120 ArkUI_NumberValue background_color_value[] = {{.u32 = COLOR_GREEN}};
121 ArkUI_AttributeItem background_color_item = {background_color_value,
122 sizeof(background_color_value) / sizeof(ArkUI_NumberValue)};
123 if (targetId == NUMBER_0) {
124 ArkUI_HostWindowInfo *info;
125 int32_t errorCode = OH_ArkUI_NodeUtils_GetWindowInfo(node, &info);
126 if (errorCode == ARKUI_ERROR_CODE_NO_ERROR) {
127 nodeAPI2->setAttribute(node, NODE_BACKGROUND_COLOR, &background_color_item);
128 }
129 }
130 if (targetId == NUMBER_1) {
131 ArkUI_HostWindowInfo *info;
132 int32_t errorCode = OH_ArkUI_NodeUtils_GetWindowInfo(nullptr, &info);
133 if (errorCode == ARKUI_ERROR_CODE_PARAM_INVALID) {
134 nodeAPI2->setAttribute(node, NODE_BACKGROUND_COLOR, &background_color_item);
135 }
136 }
137 if (targetId == NUMBER_2) {
138 ArkUI_HostWindowInfo *info;
139 ArkUI_NodeHandle button = nodeAPI2->createNode(ARKUI_NODE_BUTTON);
140 int32_t errorCode = OH_ArkUI_NodeUtils_GetWindowInfo(button, &info);
141 if (errorCode == ARKUI_ERROR_CODE_NODE_NOT_ON_MAIN_TREE) {
142 nodeAPI2->setAttribute(node, NODE_BACKGROUND_COLOR, &background_color_item);
143 }
144 }
145 }
146
testGetWindowInfo001(napi_env env,napi_callback_info info)147 napi_value GetWindowInfoTest::testGetWindowInfo001(napi_env env, napi_callback_info info)
148 {
149 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "testNodeHandleByIdTest001", "CreateNativeNode");
150 size_t argc = PARAM_1;
151 napi_value args[PARAM_1] = {nullptr};
152 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
153 size_t length = PARAM_64;
154 size_t strLength = PARAM_0;
155 char xComponentID[PARAM_64] = {PARAM_0};
156 napi_get_value_string_utf8(env, args[PARAM_0], xComponentID, length, &strLength);
157 if ((env == nullptr) || (info == nullptr)) {
158 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "testNodeHandleByIdTest001",
159 "GetContext env or info is null");
160 return nullptr;
161 }
162
163 OH_ArkUI_GetModuleInterface(ARKUI_NATIVE_NODE, ArkUI_NativeNodeAPI_1, nodeAPI2);
164 static ArkUI_NodeHandle root = CreateChildNode(nodeAPI2);
165 nodeAPI2->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
OnEventReceive1(ArkUI_NodeEvent * event)181 static void OnEventReceive1(ArkUI_NodeEvent *event)
182 {
183 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "testHostWindowInfoGetName002", "OnEventReceive1");
184 if (event == nullptr) {
185 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "testHostWindowInfoGetName002",
186 "OnEventReceive1: event is null");
187 return;
188 }
189 auto targetId = OH_ArkUI_NodeEvent_GetTargetId(event);
190 auto node = OH_ArkUI_NodeEvent_GetNodeHandle(event);
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 if (targetId == NUMBER_0) {
195 ArkUI_HostWindowInfo *info;
196 int32_t errorCode = OH_ArkUI_NodeUtils_GetWindowInfo(node, &info);
197 if (errorCode == ARKUI_ERROR_CODE_NO_ERROR) {
198 const char *windowName = OH_ArkUI_HostWindowInfo_GetName(info);
199 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Manager",
200 "kkk Get-window-name Node belongs to window:%{public}s", windowName);
201 if (strcmp(windowName, "arkui_capi_xts_api150") == 0) {
202 nodeAPI2->setAttribute(node, NODE_BACKGROUND_COLOR, &background_color_item);
203 }
204 OH_ArkUI_HostWindowInfo_Destroy(info);
205 }
206 }
207 }
208
testHostWindowInfoGetName002(napi_env env,napi_callback_info info)209 napi_value GetWindowInfoTest::testHostWindowInfoGetName002(napi_env env, napi_callback_info info)
210 {
211 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "testHostWindowInfoGetNameTest002", "CreateNativeNode");
212 size_t argc = PARAM_1;
213 napi_value args[PARAM_1] = {nullptr};
214 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
215 size_t length = PARAM_64;
216 size_t strLength = PARAM_0;
217 char xComponentID[PARAM_64] = {PARAM_0};
218 napi_get_value_string_utf8(env, args[PARAM_0], xComponentID, length, &strLength);
219 if ((env == nullptr) || (info == nullptr)) {
220 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "testHostWindowInfoGetNameTest002",
221 "GetContext env or info is null");
222 return nullptr;
223 }
224
225 OH_ArkUI_GetModuleInterface(ARKUI_NATIVE_NODE, ArkUI_NativeNodeAPI_1, nodeAPI2);
226 static ArkUI_NodeHandle root = CreateChildNode1(nodeAPI2);
227 nodeAPI2->registerNodeEventReceiver(&OnEventReceive1);
228
229 std::string id(xComponentID);
230 if (OH_NativeXComponent_AttachNativeRootNode(PluginManager::GetInstance()->GetNativeXComponent(id), root) ==
231 INVALID_PARAM) {
232 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "testHostWindowInfoGetNameTest002",
233 "OH_NativeXComponent_AttachNativeRootNode failed");
234 }
235 napi_value exports;
236 if (napi_create_object(env, &exports) != napi_ok) {
237 napi_throw_type_error(env, nullptr, "napi_create_object failed");
238 return nullptr;
239 }
240 return exports;
241 }
242
OnEventReceive2(ArkUI_NodeEvent * event)243 static void OnEventReceive2(ArkUI_NodeEvent *event)
244 {
245 auto node = OH_ArkUI_NodeEvent_GetNodeHandle(event);
246 ArkUI_HostWindowInfo *info;
247 ArkUI_NumberValue background_color_value[] = {{.u32 = COLOR_GREEN}};
248 ArkUI_AttributeItem background_color_item = {background_color_value,
249 sizeof(background_color_value) / sizeof(ArkUI_NumberValue)};
250 if (OH_ArkUI_NodeEvent_GetTargetId(event) == NUMBER_0) {
251 ArkUI_NumberValue visible_value[] = {{.i32 = ARKUI_VISIBILITY_VISIBLE}};
252 ArkUI_AttributeItem visible_value_item = {visible_value, sizeof(visible_value) / sizeof(ArkUI_NumberValue)};
253 nodeAPI2->setAttribute(button_visibility, NODE_VISIBILITY, &visible_value_item);
254 int32_t errorCode = OH_ArkUI_NodeUtils_GetWindowInfo(button_visibility, &info);
255 if (errorCode == ARKUI_ERROR_CODE_NO_ERROR) {
256 const char *windowName = OH_ArkUI_HostWindowInfo_GetName(info);
257 if (strcmp(windowName, "arkui_capi_xts_api150") == 0) {
258 nodeAPI2->setAttribute(node, NODE_BACKGROUND_COLOR, &background_color_item);
259 }
260 OH_ArkUI_HostWindowInfo_Destroy(info);
261 }
262 }
263 if (OH_ArkUI_NodeEvent_GetTargetId(event) == NUMBER_1) {
264 ArkUI_NumberValue visible_value[] = {{.i32 = ARKUI_VISIBILITY_NONE}};
265 ArkUI_AttributeItem visible_value_item = {visible_value, sizeof(visible_value) / sizeof(ArkUI_NumberValue)};
266 nodeAPI2->setAttribute(button_visibility, NODE_VISIBILITY, &visible_value_item);
267 int32_t errorCode = OH_ArkUI_NodeUtils_GetWindowInfo(button_visibility, &info);
268 if (errorCode == ARKUI_ERROR_CODE_NO_ERROR) {
269 const char *windowName = OH_ArkUI_HostWindowInfo_GetName(info);
270 if (strcmp(windowName, "arkui_capi_xts_api150") == 0) {
271 nodeAPI2->setAttribute(node, NODE_BACKGROUND_COLOR, &background_color_item);
272 }
273 OH_ArkUI_HostWindowInfo_Destroy(info);
274 }
275 }
276 if (OH_ArkUI_NodeEvent_GetTargetId(event) == NUMBER_2) {
277 ArkUI_NumberValue visible_value[] = {{.i32 = ARKUI_VISIBILITY_HIDDEN}};
278 ArkUI_AttributeItem visible_value_item = {visible_value, sizeof(visible_value) / sizeof(ArkUI_NumberValue)};
279 nodeAPI2->setAttribute(button_visibility, NODE_VISIBILITY, &visible_value_item);
280 int32_t errorCode = OH_ArkUI_NodeUtils_GetWindowInfo(button_visibility, &info);
281 if (errorCode == ARKUI_ERROR_CODE_NO_ERROR) {
282 const char *windowName = OH_ArkUI_HostWindowInfo_GetName(info);
283 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "Manager",
284 "kkk Get-window-name Node belongs to window:%{public}s", windowName);
285 if (strcmp(windowName, "arkui_capi_xts_api150") == 0) {
286 nodeAPI2->setAttribute(node, NODE_BACKGROUND_COLOR, &background_color_item);
287 }
288 OH_ArkUI_HostWindowInfo_Destroy(info);
289 }
290 }
291 }
292
testHostWindowInfoDestroy003(napi_env env,napi_callback_info info)293 napi_value GetWindowInfoTest::testHostWindowInfoDestroy003(napi_env env, napi_callback_info info)
294 {
295 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "testNodeHandleByIdTest001", "CreateNativeNode");
296 size_t argc = PARAM_1;
297 napi_value args[PARAM_1] = {nullptr};
298 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
299 size_t length = PARAM_64;
300 size_t strLength = PARAM_0;
301 char xComponentID[PARAM_64] = {PARAM_0};
302 napi_get_value_string_utf8(env, args[PARAM_0], xComponentID, length, &strLength);
303 if ((env == nullptr) || (info == nullptr)) {
304 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "testNodeHandleByIdTest001",
305 "GetContext env or info is null");
306 return nullptr;
307 }
308
309 OH_ArkUI_GetModuleInterface(ARKUI_NATIVE_NODE, ArkUI_NativeNodeAPI_1, nodeAPI2);
310 static ArkUI_NodeHandle root = CreateChildNode2(nodeAPI2);
311 nodeAPI2->registerNodeEventReceiver(&OnEventReceive2);
312
313 std::string id(xComponentID);
314 if (OH_NativeXComponent_AttachNativeRootNode(PluginManager::GetInstance()->GetNativeXComponent(id), root) ==
315 INVALID_PARAM) {
316 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "testAddCustomProperty001",
317 "OH_NativeXComponent_AttachNativeRootNode failed");
318 }
319 napi_value exports;
320 if (napi_create_object(env, &exports) != napi_ok) {
321 napi_throw_type_error(env, nullptr, "napi_create_object failed");
322 return nullptr;
323 }
324 return exports;
325 }
326
327 } // namespace ArkUICapiTest