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 "node_handle_by_id_test.h"
17 #include "common/common.h"
18 #include "../manager/plugin_manager.h"
19 #include "notify_drag_test.h"
20 #include <arkui/native_node.h>
21 #include <string>
22 #include <arkui/native_animate.h>
23 #include <arkui/native_gesture.h>
24 #include <arkui/native_node_napi.h>
25 #include <arkui/native_interface.h>
26 #include <arkui/native_type.h>
27 #include <arkui/native_node.h>
28 #include <ace/xcomponent/native_interface_xcomponent.h>
29 #include <multimedia/image_framework/image/pixelmap_native.h>
30 #include <arkui/drag_and_drop.h>
31 #include <thread>
32 #include <unistd.h>
33
34 static ArkUI_NodeHandle text1 = nullptr;
35 static ArkUI_NodeHandle text2 = nullptr;
36 static ArkUI_NativeNodeAPI_1 *nodeAPI = nullptr;
37 namespace ArkUICapiTest {
38
createTextChildNode(ArkUI_NativeNodeAPI_1 * nodeAPI)39 static auto createTextChildNode(ArkUI_NativeNodeAPI_1 *nodeAPI)
40 {
41 auto text = nodeAPI->createNode(ARKUI_NODE_TEXT);
42 ArkUI_AttributeItem LABEL_Item = {.string = "drag_getDataTypeCount Test"};
43 ArkUI_NumberValue fontSize[] = {20};
44 ArkUI_AttributeItem Font_Item = {fontSize, 1};
45 ArkUI_NumberValue marginValue[] = {20};
46 ArkUI_AttributeItem marginItem = {marginValue, 1};
47 nodeAPI->setAttribute(text, NODE_TEXT_CONTENT, &LABEL_Item);
48 nodeAPI->setAttribute(text, NODE_TEXT_FONT, &Font_Item);
49 nodeAPI->setAttribute(text, NODE_MARGIN, &marginItem);
50 nodeAPI->registerNodeEvent(text, NODE_ON_DRAG_START, 1, nullptr);
51 nodeAPI->registerNodeEvent(text, NODE_ON_PRE_DRAG, 1, nullptr);
52 OH_ArkUI_SetNodeDraggable(text, true);
53
54 return text;
55 }
56
createDragAreaChildNode(ArkUI_NativeNodeAPI_1 * nodeAPI)57 static auto createDragAreaChildNode(ArkUI_NativeNodeAPI_1 *nodeAPI)
58 {
59 auto dragArea = nodeAPI->createNode(ARKUI_NODE_COLUMN);
60 ArkUI_NumberValue heightValue1[] = {150};
61 ArkUI_AttributeItem heightItem = {heightValue1, 1};
62 ArkUI_NumberValue widthValue[] = {200};
63 ArkUI_AttributeItem widthItem = {widthValue, 1};
64 ArkUI_NumberValue borderValue[] = {1};
65 ArkUI_AttributeItem borderItem = {borderValue, 1};
66 nodeAPI->setAttribute(dragArea, NODE_WIDTH, &widthItem);
67 nodeAPI->setAttribute(dragArea, NODE_HEIGHT, &heightItem);
68 nodeAPI->setAttribute(dragArea, NODE_BORDER_WIDTH, &borderItem);
69 nodeAPI->registerNodeEvent(dragArea, NODE_ON_DROP, 1, nullptr);
70 nodeAPI->registerNodeEvent(dragArea, NODE_ON_DRAG_ENTER, 1, nullptr);
71 nodeAPI->registerNodeEvent(dragArea, NODE_ON_DRAG_MOVE, 1, nullptr);
72 nodeAPI->registerNodeEvent(dragArea, NODE_ON_DRAG_LEAVE, 1, nullptr);
73 nodeAPI->registerNodeEvent(dragArea, NODE_ON_DRAG_END, 1, nullptr);
74
75 return dragArea;
76 }
77
OnEventReceive(ArkUI_NodeEvent * event)78 static void OnEventReceive(ArkUI_NodeEvent *event)
79 {
80 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "getDataTypesCount", "OnEventReceive");
81 if (event == nullptr) {
82 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN,
83 "getDataTypesCount", "OnEventReceive: event is null");
84 return;
85 }
86 if (OH_ArkUI_NodeEvent_GetEventType(event) == NODE_ON_DROP) {
87 ArkUI_DragEvent* dragEvent = OH_ArkUI_NodeEvent_GetDragEvent(event);
88 OH_ArkUI_DragEvent_SetDragResult(dragEvent, ARKUI_DRAG_RESULT_FAILED);
89 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "zxy dragTest", "zxy NODE_ON_DROP");
90 int32_t requestId = -1;
91 auto ret = OH_ArkUI_DragEvent_RequestDragEndPending(dragEvent, &requestId);
92 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "zxy dragTest",
93 "zxy get id = %{public}d, ret = %{public}d", requestId, ret);
94 if (ret == 0) {
95 std::thread tt([requestId](){
96 sleep(1.1);
97 auto res1 = OH_ArkUI_NotifyDragResult(requestId, ARKUI_DRAG_RESULT_SUCCESSFUL);
98 auto res2 =OH_ArkUI_NotifyDragEndPendingDone(requestId);
99 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "zxy dragTest",
100 "zxy get res1 = %{public}d, res2 = %{public}d",res1, res2);
101 if (res1 == ARKUI_ERROR_CODE_NO_ERROR && res2 == ARKUI_ERROR_CODE_NO_ERROR) {
102 ArkUI_NumberValue background_color_value[] = {{.u32 = COLOR_GREEN}};
103 ArkUI_AttributeItem background_color_item = {background_color_value,
104 sizeof(background_color_value) / sizeof(ArkUI_NumberValue)};
105 nodeAPI->setAttribute(text1, NODE_BACKGROUND_COLOR, &background_color_item);
106 }
107
108 });
109 tt.detach();
110 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "zxy dragTest", "zxy NODE_ON_DROP_DONE");
111 }
112 }
113
114 if (OH_ArkUI_NodeEvent_GetEventType(event) == NODE_ON_DROP) {
115 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "zxy dragTest", "zxy NODE_ON_DRAG_ENTER");
116 ArkUI_DragEvent* dragEvent = OH_ArkUI_NodeEvent_GetDragEvent(event);
117 OH_ArkUI_DragEvent_SetDragResult(dragEvent, ARKUI_DRAG_RESULT_FAILED);
118 auto ret = OH_ArkUI_DragEvent_RequestDragEndPending(nullptr, nullptr);
119 auto res1= OH_ArkUI_NotifyDragResult(NULL, ARKUI_DRAG_RESULT_FAILED);
120 auto res2 = OH_ArkUI_NotifyDragEndPendingDone(NULL);
121 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "zxy dragTest",
122 "zxy get res1 = %{public}d, res2 = %{public}d",res1, res2);
123
124 if (res1 == ARKUI_ERROR_CODE_DRAG_DROP_OPERATION_NOT_ALLOWED &&
125 res2 == ARKUI_ERROR_CODE_DRAG_DROP_OPERATION_NOT_ALLOWED) {
126 ArkUI_NumberValue background_color_value[] = {{.u32 = COLOR_GREEN}};
127 ArkUI_AttributeItem background_color_item = {background_color_value,
128 sizeof(background_color_value) / sizeof(ArkUI_NumberValue)};
129 nodeAPI->setAttribute(text2, NODE_BACKGROUND_COLOR, &background_color_item);
130 }
131 }
132 }
133
testNotifyDragTest001(napi_env env,napi_callback_info info)134 napi_value NotifyDragTest::testNotifyDragTest001(napi_env env, napi_callback_info info)
135 {
136 OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "testNodeHandleByIdTest001", "CreateNativeNode");
137 size_t argc = PARAM_1;
138 napi_value args[PARAM_1] = {nullptr};
139 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
140 size_t length = PARAM_64;
141 size_t strLength = PARAM_0;
142 char xComponentID[PARAM_64] = {PARAM_0};
143 napi_get_value_string_utf8(env, args[PARAM_0], xComponentID, length, &strLength);
144 if ((env == nullptr) || (info == nullptr)) {
145 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "testNodeHandleByIdTest001",
146 "GetContext env or info is null");
147 return nullptr;
148 }
149
150 OH_ArkUI_GetModuleInterface(ARKUI_NATIVE_NODE, ArkUI_NativeNodeAPI_1, nodeAPI);
151 auto column = nodeAPI->createNode(ARKUI_NODE_COLUMN);
152
153 auto text = createTextChildNode(nodeAPI);
154 auto dragArea = createDragAreaChildNode(nodeAPI);
155
156 ArkUI_AttributeItem id_item = {};
157 id_item.string = "getDataTypesCountTest";
158 nodeAPI->setAttribute(text, NODE_ID, &id_item);
159 id_item.string = "dragArea";
160 nodeAPI->setAttribute(dragArea, NODE_ID, &id_item);
161 text1 = nodeAPI->createNode(ARKUI_NODE_TEXT);
162 ArkUI_AttributeItem id_item1 = {};
163 id_item1.string = "7";
164 text2 = nodeAPI->createNode(ARKUI_NODE_TEXT);
165 id_item1.string = "7";
166 nodeAPI->setAttribute(text1, NODE_TEXT_CONTENT, &id_item1);
167 nodeAPI->setAttribute(text2, NODE_TEXT_CONTENT, &id_item1);
168 ArkUI_AttributeItem id_item2 = {};
169 id_item2.string = "text1_id";
170 ArkUI_AttributeItem id_item3 = {};
171 id_item3.string = "text2_id";
172 nodeAPI->setAttribute(text1, NODE_ID, &id_item2);
173 nodeAPI->setAttribute(text2, NODE_ID, &id_item3);
174 nodeAPI->addChild(column, text);
175 nodeAPI->addChild(column, dragArea);
176 nodeAPI->addChild(column, text1);
177 nodeAPI->addChild(column, text2);
178 nodeAPI->registerNodeEventReceiver(&OnEventReceive);
179
180 std::string id(xComponentID);
181 if (OH_NativeXComponent_AttachNativeRootNode(PluginManager::GetInstance()->GetNativeXComponent(id), column) ==
182 INVALID_PARAM) {
183 OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "testAddCustomProperty001",
184 "OH_NativeXComponent_AttachNativeRootNode failed");
185 }
186 napi_value exports;
187 if (napi_create_object(env, &exports) != napi_ok) {
188 napi_throw_type_error(env, nullptr, "napi_create_object failed");
189 return nullptr;
190 }
191 return exports;
192 }
193
194 } // namespace ArkUICapiTest