• 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 
16 #include "common/common.h"
17 #include "customcomponent_event_test.h"
18 #include "../manager/plugin_manager.h"
19 
20 namespace ArkUICapiTest {
21 namespace {
22 uint32_t defalutColor = 0xffff0000;
23 uint32_t actionedColor = 0xff00ff00;
24 bool g_flag = false;
25 constexpr int32_t DRAW_EVENT_ID = 3;
26 constexpr int32_t LAYOUT_EVENT_ID = 4;
27 constexpr int32_t MEASURE_EVENT_ID = 5;
28 constexpr int32_t FOREGROUND_EVENT_ID = 5;
29 constexpr int32_t BACKGROUND_EVENT_ID = 6;
30 constexpr int32_t MIN_WIDTH = 100;
31 constexpr int32_t MIN_HEIGHT = 100;
32 constexpr int32_t MAX_WIDTH = 200;
33 constexpr int32_t MAX_HEIGHT = 200;
34 constexpr int32_t PERCENT_REFERENCE_WIDTH = 50;
35 constexpr int32_t PERCENT_REFERENCE_HEIGHT = 50;
36 }
37 
38 ArkUI_NodeHandle customNode;
39 
OnActioned()40 void OnActioned()
41 {
42     if (!g_flag && customNode) {
43         g_flag = true;
44         ArkUI_NumberValue value[] = {{.u32 = actionedColor}};
45         ArkUI_AttributeItem value_item = {value, sizeof(value) / sizeof(ArkUI_NumberValue)};
46         auto nodeAPI = reinterpret_cast<ArkUI_NativeNodeAPI_1 *>(
47             OH_ArkUI_QueryModuleInterfaceByName(ARKUI_NATIVE_NODE, "ArkUI_NativeNodeAPI_1"));
48         auto ret = nodeAPI->setAttribute(customNode, NODE_BACKGROUND_COLOR, &value_item);
49         OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Manager",
50             "SWIPEGESTURE  setBackgroundColor result %{public}d", ret);
51     }
52 }
53 
CreateNativeNode(napi_env env,napi_callback_info info)54 napi_value CustomComponentEventTest::CreateNativeNode(napi_env env, napi_callback_info info)
55 {
56     OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "CustomComponentEventTest", "CreateNativeNode");
57     size_t argc = PARAM_1;
58     napi_value args[PARAM_1] = {nullptr};
59     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
60     size_t length = PARAM_64;
61     size_t strLength = PARAM_0;
62     char xComponentID[PARAM_64] = {PARAM_0};
63     napi_get_value_string_utf8(env, args[PARAM_0], xComponentID, length, &strLength);
64 
65     if ((env == nullptr) || (info == nullptr)) {
66         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "ListItemHitTestBehaviorTest",
67                      "GetContext env or info is null");
68         return nullptr;
69     }
70     g_flag = false;
71     auto nodeAPI = reinterpret_cast<ArkUI_NativeNodeAPI_1 *>(
72         OH_ArkUI_QueryModuleInterfaceByName(ARKUI_NATIVE_NODE, "ArkUI_NativeNodeAPI_1"));
73     customNode = nodeAPI->createNode(ARKUI_NODE_CUSTOM);
74     ArkUI_NumberValue widthValue[] = { {.f32 = 500} };
75     ArkUI_AttributeItem widthItem = { .value = widthValue, .size = sizeof(widthValue) / sizeof(ArkUI_NumberValue) };
76     nodeAPI->setAttribute(customNode, NODE_WIDTH, &widthItem);
77     ArkUI_NumberValue heightValue[] = { {.f32 = 500} };
78     ArkUI_AttributeItem heightItem = { .value = heightValue, .size = sizeof(widthValue) / sizeof(ArkUI_NumberValue) };
79     nodeAPI->setAttribute(customNode, NODE_HEIGHT, &heightItem);
80     ArkUI_NumberValue value[] = {{.u32 = defalutColor}};
81     ArkUI_AttributeItem value_item = {value, sizeof(value) / sizeof(ArkUI_NumberValue)};
82     nodeAPI->setAttribute(customNode, NODE_BACKGROUND_COLOR, &value_item);
83     ArkUI_AttributeItem id_item = {};
84     id_item.string = "customEventComponentID";
85     nodeAPI->setAttribute(customNode, NODE_ID, &id_item);
86     nodeAPI->registerNodeCustomEvent(customNode, ARKUI_NODE_CUSTOM_EVENT_ON_MEASURE, MEASURE_EVENT_ID, nullptr);
87     nodeAPI->registerNodeCustomEvent(customNode, ARKUI_NODE_CUSTOM_EVENT_ON_LAYOUT, LAYOUT_EVENT_ID, nullptr);
88     nodeAPI->registerNodeCustomEvent(customNode, ARKUI_NODE_CUSTOM_EVENT_ON_DRAW, DRAW_EVENT_ID, nullptr);
89     nodeAPI->registerNodeCustomEvent(customNode, ARKUI_NODE_CUSTOM_EVENT_ON_FOREGROUND_DRAW,
90         FOREGROUND_EVENT_ID, nullptr);
91     nodeAPI->registerNodeCustomEvent(customNode, ARKUI_NODE_CUSTOM_EVENT_ON_OVERLAY_DRAW, BACKGROUND_EVENT_ID, nullptr);
92     nodeAPI->registerNodeCustomEventReceiver([] (ArkUI_NodeCustomEvent* event) {
93         OnActioned();
94         auto targetId = OH_ArkUI_NodeCustomEvent_GetEventTargetId(event);
95         auto userData = OH_ArkUI_NodeCustomEvent_GetUserData(event);
96         auto nodeHandle = OH_ArkUI_NodeCustomEvent_GetNodeHandle(event);
97         auto type = OH_ArkUI_NodeCustomEvent_GetEventType(event);
98         OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Manager", "NodeCustomEvent targetId %{public}d", targetId);
99         OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Manager",
100             "NodeCustomEvent  userData %{public}d", !!userData);
101         OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Manager",
102             "NodeCustomEvent  nodeHandle %{public}d", !!nodeHandle);
103         OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Manager", "NodeCustomEvent type  %{public}d", type);
104         if (type == ARKUI_NODE_CUSTOM_EVENT_ON_LAYOUT) {
105             auto constrait = OH_ArkUI_NodeCustomEvent_GetLayoutConstraintInMeasure(event);
106             OH_ArkUI_LayoutConstraint_SetMinWidth(constrait, MIN_WIDTH);
107             OH_ArkUI_LayoutConstraint_SetMinHeight(constrait, MIN_HEIGHT);
108             OH_ArkUI_LayoutConstraint_SetMaxWidth(constrait, MAX_WIDTH);
109             OH_ArkUI_LayoutConstraint_SetMaxHeight(constrait, MAX_HEIGHT);
110             OH_ArkUI_LayoutConstraint_SetPercentReferenceWidth(constrait,
111                 PERCENT_REFERENCE_WIDTH);
112             OH_ArkUI_LayoutConstraint_SetPercentReferenceHeight(constrait,
113                 PERCENT_REFERENCE_HEIGHT);
114         }
115         auto constrait = OH_ArkUI_NodeCustomEvent_GetLayoutConstraintInMeasure(event);
116         auto minWidth = OH_ArkUI_LayoutConstraint_GetMinWidth(constrait);
117         auto minHeight = OH_ArkUI_LayoutConstraint_GetMinHeight(constrait);
118         auto maxWidth = OH_ArkUI_LayoutConstraint_GetMaxWidth(constrait);
119         auto maxHeight = OH_ArkUI_LayoutConstraint_GetMaxHeight(constrait);
120         auto percentReferenceWidth = OH_ArkUI_LayoutConstraint_GetPercentReferenceWidth(constrait);
121         auto percentReferenceHeight = OH_ArkUI_LayoutConstraint_GetPercentReferenceHeight(constrait);
122         auto* cavaseContext = OH_ArkUI_NodeCustomEvent_GetDrawContextInDraw(event);
123         auto* canvas = OH_ArkUI_DrawContext_GetCanvas(cavaseContext);
124         auto size = OH_ArkUI_DrawContext_GetSize(cavaseContext);
125         OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Manager",
126             "NodeCustomEvent constrait  %{public}d", !!constrait);
127         OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Manager",
128             "LayoutConstraint minWidth  %{public}d", minWidth);
129         OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Manager",
130             "LayoutConstraint minWidth  %{public}d", minWidth);
131         OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Manager",
132             "LayoutConstraint maxWidth  %{public}d", maxWidth);
133         OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Manager",
134             "LayoutConstraint maxHeight  %{public}d", maxHeight);
135         OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Manager",
136             "LayoutConstraint percentReferenceWidth  %{public}d", percentReferenceWidth);
137         OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Manager",
138             "LayoutConstraint percentReferenceHeight  %{public}d", percentReferenceHeight);
139 
140         OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Manager",
141             "LayoutConstraint cavaseContext  %{public}d", !!cavaseContext);
142         OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Manager",
143             "LayoutConstraint canvas  %{public}d", !!canvas);
144         OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "Manager",
145             "LayoutConstraint size.width  %{public}d", size.width);
146     });
147     std::string id(xComponentID);
148     if (OH_NativeXComponent_AttachNativeRootNode(
149         PluginManager::GetInstance()->GetNativeXComponent(id), customNode) ==
150         INVALID_PARAM) {
151         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "ListItemHitTestBehaviorTest",
152                      "OH_NativeXComponent_AttachNativeRootNode failed");
153     }
154 
155     napi_value exports;
156     if (napi_create_object(env, &exports) != napi_ok) {
157         napi_throw_type_error(env, NULL, "napi_create_object failed");
158         return nullptr;
159     }
160     return exports;
161 }
162 } // namespace ArkUICapiTest
163