• 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 "textarea_inputfilter_test.h"
17 
18 #include "../manager/plugin_manager.h"
19 #include "common/common.h"
20 
21 #define INPUT_FILTER_1 "[a-z]"
22 
23 namespace ArkUICapiTest {
24 
CreateNode(ArkUI_NativeNodeAPI_1 * nodeAPI,const char * str)25 static auto CreateNode(ArkUI_NativeNodeAPI_1* nodeAPI, const char* str)
26 {
27     auto nodeHandle = nodeAPI->createNode(ARKUI_NODE_TEXT_AREA);
28     // set width
29     ArkUI_NumberValue width_value[] = { { .f32 = SIZE_350 } };
30     ArkUI_AttributeItem width_item = { width_value, sizeof(width_value) / sizeof(ArkUI_NumberValue) };
31     nodeAPI->setAttribute(nodeHandle, NODE_WIDTH, &width_item);
32 
33     // set height
34     ArkUI_NumberValue height_value[] = { { .f32 = SIZE_200 } };
35     ArkUI_AttributeItem height_item = { height_value, sizeof(height_value) / sizeof(ArkUI_NumberValue) };
36     nodeAPI->setAttribute(nodeHandle, NODE_HEIGHT, &height_item);
37 
38     ArkUI_NumberValue margin_value[] = { { .f32 = SIZE_5 } };
39     ArkUI_AttributeItem margin_item = { margin_value, sizeof(margin_value) / sizeof(ArkUI_NumberValue) };
40     nodeAPI->setAttribute(nodeHandle, NODE_MARGIN, &margin_item);
41 
42     ArkUI_NumberValue value[] = { { .f32 = SIZE_30 } };
43     ArkUI_AttributeItem value_item = { value, sizeof(value) / sizeof(ArkUI_NumberValue) };
44     nodeAPI->setAttribute(nodeHandle, NODE_FONT_SIZE, &value_item);
45 
46     // set  str
47     ArkUI_AttributeItem textAreaItem = {};
48     textAreaItem.string = str;
49     nodeAPI->setAttribute(nodeHandle, NODE_TEXT_INPUT_INPUT_FILTER, &textAreaItem);
50 
51     nodeAPI->registerNodeEvent(nodeHandle, NODE_TEXT_AREA_ON_INPUT_FILTER_ERROR, ON_CHANGE_EVENT_ID, nullptr);
52 
53     return nodeHandle;
54 }
EventReceive(ArkUI_NodeEvent * event)55 static void EventReceive(ArkUI_NodeEvent* event)
56 {
57     OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "TextAreaIntentFilterTest", "OnEventReceive");
58     if (event == nullptr) {
59         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "TextAreaIntentFilterTest", "OnEventReceive: event is null");
60         return;
61     }
62 
63     int32_t eventId = OH_ArkUI_NodeEvent_GetTargetId(event);
64     OH_LOG_Print(
65         LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "TextAreaIntentFilterTest", "OnEventReceive eventId: %{public}d", eventId);
66 
67     ArkUI_NativeNodeAPI_1* nodeAPI = nullptr;
68     OH_ArkUI_GetModuleInterface(ARKUI_NATIVE_NODE, ArkUI_NativeNodeAPI_1, nodeAPI);
69     auto nodeHandler = OH_ArkUI_NodeEvent_GetNodeHandle(event);
70 
71     if (eventId == ON_CHANGE_EVENT_ID) {
72         ArkUI_NumberValue background_color_value[] = { { .u32 = COLOR_GREEN } };
73         ArkUI_AttributeItem background_color_item = { background_color_value,
74             sizeof(background_color_value) / sizeof(ArkUI_NumberValue) };
75         nodeAPI->setAttribute(nodeHandler, NODE_BACKGROUND_COLOR, &background_color_item);
76 
77         return;
78     }
79 }
CreateNativeNode(napi_env env,napi_callback_info info)80 napi_value TextAreaInputFilterTest::CreateNativeNode(napi_env env, napi_callback_info info)
81 {
82     OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "TextAreaIntentFilterTest", "CreateNativeNode");
83     size_t argc = 1;
84     napi_value args[1] = { nullptr };
85     napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
86     size_t length = 64;
87     size_t strLength = 0;
88     char xComponentID[64] = { 0 };
89     napi_get_value_string_utf8(env, args[0], xComponentID, length, &strLength);
90 
91     if ((env == nullptr) || (info == nullptr)) {
92         OH_LOG_Print(
93             LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "TextInputInputFilterTest", "GetContext env or info is null");
94         return nullptr;
95     }
96     ArkUI_NativeNodeAPI_1* nodeAPI = nullptr;
97     OH_ArkUI_GetModuleInterface(ARKUI_NATIVE_NODE, ArkUI_NativeNodeAPI_1, nodeAPI);
98 
99     auto column = nodeAPI->createNode(ARKUI_NODE_COLUMN);
100     const char* srcLocal1 = INPUT_FILTER_1;
101     auto textArea = CreateNode(nodeAPI, srcLocal1);
102     ArkUI_AttributeItem item = {};
103     item.string = "1234abc";
104     nodeAPI->setAttribute(textArea, NODE_TEXT_INPUT_TEXT, &item);
105     item.string = "textAreaIntentFilter";
106     nodeAPI->setAttribute(textArea, NODE_ID, &item);
107     nodeAPI->addChild(column, textArea);
108     nodeAPI->registerNodeEventReceiver(&EventReceive);
109 
110     std::string id(xComponentID);
111     if (OH_NativeXComponent_AttachNativeRootNode(PluginManager::GetInstance()->GetNativeXComponent(id), column) ==
112         INVALID_PARAM) {
113         OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "TextInputInputFilterTest",
114             "OH_NativeXComponent_AttachNativeRootNode failed");
115     }
116     napi_value exports;
117     if (napi_create_object(env, &exports) != napi_ok) {
118         napi_throw_type_error(env, NULL, "napi_create_object failed");
119         return nullptr;
120     }
121     return exports;
122 }
123 } // namespace ArkUICapiTest