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 "napi/native_api.h"
16 #include "inputmethod/inputmethod_attach_options_capi.h"
17 #include "inputmethod/inputmethod_controller_capi.h"
18 #include "inputmethod/inputmethod_cursor_info_capi.h"
19 #include "inputmethod/inputmethod_inputmethod_proxy_capi.h"
20 #include "inputmethod/inputmethod_private_command_capi.h"
21 #include "inputmethod/inputmethod_text_avoid_info_capi.h"
22 #include "inputmethod/inputmethod_text_config_capi.h"
23 #include "inputmethod/inputmethod_text_editor_proxy_capi.h"
24 #include "inputmethod/inputmethod_types_capi.h"
25 #include "native_common.h"
26 #include "hilog/log.h"
27 #include "codecvt"
28 #include "locale"
29 
30 InputMethod_TextEditorProxy *g_txtEditProxy = nullptr;
31 InputMethod_InputMethodProxy *g_inputMethodProxy = nullptr;
32 InputMethod_PrivateCommand *g_privateCommand = nullptr;
33 InputMethod_CursorInfo *g_cursorInfo = nullptr;
34 InputMethod_TextAvoidInfo *g_textAvoidInfo = nullptr;
35 InputMethod_AttachOptions *g_options = nullptr;
36 
GetTextConfigFunc(InputMethod_TextEditorProxy *textEditorProxy, InputMethod_TextConfig *config)37 void GetTextConfigFunc(InputMethod_TextEditorProxy *textEditorProxy, InputMethod_TextConfig *config)
38 {
39     OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>GetTextConfigFunc successful.");
40 }
41 
InsertTextFunc(InputMethod_TextEditorProxy *textEditorProxy, const char16_t *text, size_t length)42 void InsertTextFunc(InputMethod_TextEditorProxy *textEditorProxy, const char16_t *text, size_t length)
43 {
44     OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>InsertTextFunc successful.");
45 }
46 
DeleteForwardFunc(InputMethod_TextEditorProxy *textEditorProxy, int32_t length)47 void DeleteForwardFunc(InputMethod_TextEditorProxy *textEditorProxy, int32_t length)
48 {
49     OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>DeleteForwardFunc successful.");
50 }
51 
DeleteBackwardFunc(InputMethod_TextEditorProxy *textEditorProxy, int32_t length)52 void DeleteBackwardFunc(InputMethod_TextEditorProxy *textEditorProxy, int32_t length)
53 {
54     OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>DeleteBackwardFunc successful.");
55 }
56 
SendKeyboardStatusFunc(InputMethod_TextEditorProxy *textEditorProxy, InputMethod_KeyboardStatus keyboardStatus)57 void SendKeyboardStatusFunc(InputMethod_TextEditorProxy *textEditorProxy, InputMethod_KeyboardStatus keyboardStatus)
58 {
59     OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>SendKeyboardStatusFunc successful.");
60 }
61 
SendEnterKeyFunc(InputMethod_TextEditorProxy *textEditorProxy, InputMethod_EnterKeyType enterKeyType)62 void SendEnterKeyFunc(InputMethod_TextEditorProxy *textEditorProxy, InputMethod_EnterKeyType enterKeyType)
63 {
64     OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>SendEnterKeyFunc successful.");
65 }
66 
MoveCursorFunc(InputMethod_TextEditorProxy *textEditorProxy, InputMethod_Direction direction)67 void MoveCursorFunc(InputMethod_TextEditorProxy *textEditorProxy, InputMethod_Direction direction)
68 {
69     OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>MoveCursorFunc successful.");
70 }
71 
HandleSetSelectionFunc(InputMethod_TextEditorProxy *textEditorProxy, int32_t start, int32_t end)72 void HandleSetSelectionFunc(InputMethod_TextEditorProxy *textEditorProxy, int32_t start, int32_t end)
73 {
74     OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>HandleSetSelectionFunc successful.");
75 }
76 
HandleExtendActionFunc(InputMethod_TextEditorProxy *textEditorProxy, InputMethod_ExtendAction action)77 void HandleExtendActionFunc(InputMethod_TextEditorProxy *textEditorProxy, InputMethod_ExtendAction action)
78 {
79     OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>HandleExtendActionFunc successful.");
80 }
81 
GetLeftTextOfCursorFunc(InputMethod_TextEditorProxy *textEditorProxy, int32_t number, char16_t text[], size_t *length)82 void GetLeftTextOfCursorFunc(InputMethod_TextEditorProxy *textEditorProxy, int32_t number, char16_t text[],
83                              size_t *length)
84 {
85     OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>GetLeftTextOfCursorFunc successful.");
86 }
87 
GetRightTextOfCursorFunc(InputMethod_TextEditorProxy *textEditorProxy, int32_t number, char16_t text[], size_t *length)88 void GetRightTextOfCursorFunc(InputMethod_TextEditorProxy *textEditorProxy, int32_t number, char16_t text[],
89                               size_t *length)
90 {
91     OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>GetRightTextOfCursorFunc successful.");
92 }
93 
GetTextIndexAtCursorFunc(InputMethod_TextEditorProxy *textEditorProxy)94 int32_t GetTextIndexAtCursorFunc(InputMethod_TextEditorProxy *textEditorProxy)
95 {
96     OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>GetTextIndexAtCursorFunc successful.");
97     return 0;
98 }
99 
ReceivePrivateCommandFunc(InputMethod_TextEditorProxy *textEditorProxy, InputMethod_PrivateCommand *privateCommand[], size_t size)100 int32_t ReceivePrivateCommandFunc(InputMethod_TextEditorProxy *textEditorProxy,
101                                   InputMethod_PrivateCommand *privateCommand[], size_t size)
102                                   {
103     OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>ReceivePrivateCommandFunc successful.");
104     return 0;
105 }
106 
SetPreviewTextFunc(InputMethod_TextEditorProxy *textEditorProxy, const char16_t text[], size_t length, int32_t start, int32_t end)107 int32_t SetPreviewTextFunc(InputMethod_TextEditorProxy *textEditorProxy, const char16_t text[], size_t length,
108                            int32_t start, int32_t end)
109 {
110     OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>SetPreviewTextFunc successful.");
111     return 0;
112 }
113 
FinishTextPreviewFunc(InputMethod_TextEditorProxy *textEditorProxy)114 void FinishTextPreviewFunc(InputMethod_TextEditorProxy *textEditorProxy)
115 {
116     OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>FinishTextPreviewFunc successful.");
117 }
118 
TextEditorProxy_Create()119 void TextEditorProxy_Create()
120 {
121     if (g_txtEditProxy == nullptr) {
122         g_txtEditProxy = OH_TextEditorProxy_Create();
123         OH_TextEditorProxy_SetGetTextConfigFunc(g_txtEditProxy, GetTextConfigFunc);
124         OH_TextEditorProxy_SetInsertTextFunc(g_txtEditProxy, InsertTextFunc);
125         OH_TextEditorProxy_SetDeleteForwardFunc(g_txtEditProxy, DeleteForwardFunc);
126         OH_TextEditorProxy_SetDeleteBackwardFunc(g_txtEditProxy, DeleteBackwardFunc);
127         OH_TextEditorProxy_SetSendKeyboardStatusFunc(g_txtEditProxy, SendKeyboardStatusFunc);
128         OH_TextEditorProxy_SetSendEnterKeyFunc(g_txtEditProxy, SendEnterKeyFunc);
129         OH_TextEditorProxy_SetMoveCursorFunc(g_txtEditProxy, MoveCursorFunc);
130         OH_TextEditorProxy_SetHandleSetSelectionFunc(g_txtEditProxy, HandleSetSelectionFunc);
131         OH_TextEditorProxy_SetHandleExtendActionFunc(g_txtEditProxy, HandleExtendActionFunc);
132         OH_TextEditorProxy_SetGetLeftTextOfCursorFunc(g_txtEditProxy, GetLeftTextOfCursorFunc);
133         OH_TextEditorProxy_SetGetRightTextOfCursorFunc(g_txtEditProxy, GetRightTextOfCursorFunc);
134         OH_TextEditorProxy_SetGetTextIndexAtCursorFunc(g_txtEditProxy, GetTextIndexAtCursorFunc);
135         OH_TextEditorProxy_SetReceivePrivateCommandFunc(g_txtEditProxy, ReceivePrivateCommandFunc);
136         OH_TextEditorProxy_SetSetPreviewTextFunc(g_txtEditProxy, SetPreviewTextFunc);
137         OH_TextEditorProxy_SetFinishTextPreviewFunc(g_txtEditProxy, FinishTextPreviewFunc);
138         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>TextEditorProxy_Create successful.");
139     }
140 }
141 
AttachOptions_Create(bool showKeyboard)142 void AttachOptions_Create(bool showKeyboard)
143 {
144     g_options = OH_AttachOptions_Create(showKeyboard);
145     }
146 
SUB_IMF_NDK_AttachOptions_Create_0100(napi_env env, napi_callback_info info)147 static napi_value SUB_IMF_NDK_AttachOptions_Create_0100(napi_env env, napi_callback_info info)
148 {
149     g_options = OH_AttachOptions_Create(true);
150     bool showKeyboard = false;
151     InputMethod_ErrorCode returnValue = OH_AttachOptions_IsShowKeyboard(g_options, &showKeyboard);
152     OH_AttachOptions_Destroy(g_options);
153 
154     if (returnValue == InputMethod_ErrorCode::IME_ERR_OK) {
155         napi_value result = nullptr;
156         NAPI_CALL(env, napi_create_string_utf8(env, "OK", NAPI_AUTO_LENGTH, &result));
157         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_AttachOptions_IsShowKeyboard successful.");
158         return result;
159     }
160     napi_value result = nullptr;
161     NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
162     OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_AttachOptions_IsShowKeyboard fail.");
163     return result;
164 }
165 
SUB_IMF_NDK_InputMethodController_Attach_0100(napi_env env, napi_callback_info info)166 static napi_value SUB_IMF_NDK_InputMethodController_Attach_0100(napi_env env, napi_callback_info info)
167 {
168     TextEditorProxy_Create();
169     AttachOptions_Create(false);
170     InputMethod_ErrorCode returnValue = OH_InputMethodController_Attach(g_txtEditProxy, g_options, &g_inputMethodProxy);
171 
172     if (returnValue == InputMethod_ErrorCode::IME_ERR_OK) {
173         napi_value result = nullptr;
174         NAPI_CALL(env, napi_create_string_utf8(env, "OK", NAPI_AUTO_LENGTH, &result));
175         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_InputMethodController_Attach successful.");
176         return result;
177     }
178     napi_value result = nullptr;
179     NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
180     OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_InputMethodController_Attach fail.");
181     return result;
182 }
183 
SUB_IMF_NDK_InputMethodController_Detach_0100(napi_env env, napi_callback_info info)184 static napi_value SUB_IMF_NDK_InputMethodController_Detach_0100(napi_env env, napi_callback_info info)
185 {
186     TextEditorProxy_Create();
187     AttachOptions_Create(false);
188     OH_InputMethodController_Attach(g_txtEditProxy, g_options, &g_inputMethodProxy);
189     InputMethod_ErrorCode returnValue = OH_InputMethodController_Detach(g_inputMethodProxy);
190     OH_TextEditorProxy_Destroy(g_txtEditProxy);
191     g_txtEditProxy = nullptr;
192     if (returnValue == InputMethod_ErrorCode::IME_ERR_OK) {
193         napi_value result = nullptr;
194         NAPI_CALL(env, napi_create_string_utf8(env, "OK", NAPI_AUTO_LENGTH, &result));
195         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_InputMethodController_Detach successful.");
196         return result;
197     }
198     napi_value result = nullptr;
199     NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
200     OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_InputMethodController_Detach fail.");
201     return result;
202 }
203 
SUB_IMF_NDK_InputMethodProxy_ShowKeyboard_0100(napi_env env, napi_callback_info info)204 static napi_value SUB_IMF_NDK_InputMethodProxy_ShowKeyboard_0100(napi_env env, napi_callback_info info)
205 {
206     TextEditorProxy_Create();
207     AttachOptions_Create(false);
208     OH_InputMethodController_Attach(g_txtEditProxy, g_options, &g_inputMethodProxy);
209     InputMethod_ErrorCode returnValue = OH_InputMethodProxy_ShowKeyboard(g_inputMethodProxy);
210     if (returnValue == InputMethod_ErrorCode::IME_ERR_OK) {
211         napi_value result = nullptr;
212         NAPI_CALL(env, napi_create_string_utf8(env, "OK", NAPI_AUTO_LENGTH, &result));
213         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_InputMethodProxy_ShowKeyboard successful.");
214         return result;
215     }
216     napi_value result = nullptr;
217     NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
218     OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_InputMethodProxy_ShowKeyboard fail.");
219     return result;
220 }
221 
SUB_IMF_NDK_InputMethodProxy_HideKeyboard_0100(napi_env env, napi_callback_info info)222 static napi_value SUB_IMF_NDK_InputMethodProxy_HideKeyboard_0100(napi_env env, napi_callback_info info)
223 {
224     TextEditorProxy_Create();
225     AttachOptions_Create(true);
226     OH_InputMethodController_Attach(g_txtEditProxy, g_options, &g_inputMethodProxy);
227     InputMethod_ErrorCode returnValue = OH_InputMethodProxy_HideKeyboard(g_inputMethodProxy);
228     if (returnValue == InputMethod_ErrorCode::IME_ERR_OK) {
229         napi_value result = nullptr;
230         NAPI_CALL(env, napi_create_string_utf8(env, "OK", NAPI_AUTO_LENGTH, &result));
231         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_InputMethodProxy_HideKeyboard successful.");
232         return result;
233     }
234     napi_value result = nullptr;
235     NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
236     OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_InputMethodProxy_HideKeyboard fail.");
237     return result;
238 }
239 
SUB_IMF_NDK_InputMethodProxy_NotifySelectionChange_0100(napi_env env, napi_callback_info info)240 static napi_value SUB_IMF_NDK_InputMethodProxy_NotifySelectionChange_0100(napi_env env, napi_callback_info info)
241 {
242     TextEditorProxy_Create();
243     AttachOptions_Create(true);
244     OH_InputMethodController_Attach(g_txtEditProxy, g_options, &g_inputMethodProxy);
245     char16_t text[] = u"TestValue";
246     InputMethod_ErrorCode returnValue = OH_InputMethodProxy_NotifySelectionChange(g_inputMethodProxy, text, 9, 1, 3);
247     if (returnValue == InputMethod_ErrorCode::IME_ERR_OK) {
248         napi_value result = nullptr;
249         NAPI_CALL(env, napi_create_string_utf8(env, "OK", NAPI_AUTO_LENGTH, &result));
250         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_InputMethodProxy_NotifySelectionChange successful.");
251         return result;
252     }
253     napi_value result = nullptr;
254     NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
255     OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_InputMethodProxy_NotifySelectionChange fail.");
256     return result;
257 }
258 
SUB_IMF_NDK_InputMethodProxy_NotifyConfigurationChange_0100(napi_env env, napi_callback_info info)259 static napi_value SUB_IMF_NDK_InputMethodProxy_NotifyConfigurationChange_0100(napi_env env, napi_callback_info info)
260 {
261     TextEditorProxy_Create();
262     AttachOptions_Create(true);
263     OH_InputMethodController_Attach(g_txtEditProxy, g_options, &g_inputMethodProxy);
264     InputMethod_ErrorCode returnValue =
265         OH_InputMethodProxy_NotifyConfigurationChange(g_inputMethodProxy, IME_ENTER_KEY_SEND, IME_TEXT_INPUT_TYPE_TEXT);
266     if (returnValue == InputMethod_ErrorCode::IME_ERR_OK) {
267         napi_value result = nullptr;
268         NAPI_CALL(env, napi_create_string_utf8(env, "OK", NAPI_AUTO_LENGTH, &result));
269         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_InputMethodProxy_NotifyConfigurationChange successful.");
270         return result;
271     }
272     napi_value result = nullptr;
273     NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
274     OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_InputMethodProxy_NotifyConfigurationChange fail.");
275     return result;
276 }
277 
SUB_IMF_NDK_InputMethodProxy_NotifyCursorUpdate_0100(napi_env env, napi_callback_info info)278 static napi_value SUB_IMF_NDK_InputMethodProxy_NotifyCursorUpdate_0100(napi_env env, napi_callback_info info)
279 {
280     TextEditorProxy_Create();
281     AttachOptions_Create(true);
282     OH_InputMethodController_Attach(g_txtEditProxy, g_options, &g_inputMethodProxy);
283     InputMethod_CursorInfo *cursorInfo = OH_CursorInfo_Create(100, 200, 300, 400);
284     InputMethod_ErrorCode returnValue = OH_InputMethodProxy_NotifyCursorUpdate(g_inputMethodProxy, cursorInfo);
285     if (returnValue == InputMethod_ErrorCode::IME_ERR_OK) {
286         napi_value result = nullptr;
287         NAPI_CALL(env, napi_create_string_utf8(env, "OK", NAPI_AUTO_LENGTH, &result));
288         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "",
289                      "NDKTest====>SUB_IMF_NDK_InputMethodProxy_NotifyConfigurationChange_0100 successful.");
290         return result;
291     }
292     napi_value result = nullptr;
293     NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
294     OH_LOG_Print(LOG_APP, LOG_INFO, 0, "",
295                  "NDKTest====>SUB_IMF_NDK_InputMethodProxy_NotifyConfigurationChange_0100 fail.");
296     return result;
297 }
298 
SUB_IMF_NDK_InputMethodProxy_SendPrivateCommand_0100(napi_env env, napi_callback_info info)299 static napi_value SUB_IMF_NDK_InputMethodProxy_SendPrivateCommand_0100(napi_env env, napi_callback_info info)
300 {
301     TextEditorProxy_Create();
302     AttachOptions_Create(true);
303     OH_InputMethodController_Attach(g_txtEditProxy, g_options, &g_inputMethodProxy);
304     std::string keyStr1 = "testKey_1";
305     std::string valStr1 = "testValue_2";
306     InputMethod_PrivateCommand *keyCommand1 = nullptr;
307     keyCommand1 = OH_PrivateCommand_Create(const_cast<char *>(keyStr1.c_str()), keyStr1.length());
308     OH_PrivateCommand_SetStrValue(keyCommand1, const_cast<char *>(valStr1.c_str()), valStr1.length());
309     OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_PrivateCommand_SetStrValue successful.");
310 
311     std::string keyStr2 = "testKey_2";
312     bool valStr2 = true;
313     InputMethod_PrivateCommand *keyCommand2 = nullptr;
314     keyCommand2 = OH_PrivateCommand_Create(const_cast<char *>(keyStr2.c_str()), keyStr2.length());
315     OH_PrivateCommand_SetBoolValue(keyCommand2, valStr2);
316     OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_PrivateCommand_SetBoolValue successful.");
317 
318     std::string keyStr3 = "testKey_3";
319     int32_t valStr3 = 123456789;
320     InputMethod_PrivateCommand *keyCommand3 = nullptr;
321     keyCommand3 = OH_PrivateCommand_Create(const_cast<char *>(keyStr3.c_str()), keyStr3.length());
322     OH_PrivateCommand_SetIntValue(keyCommand3, valStr3);
323     OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_PrivateCommand_SetIntValue successful.");
324 
325     InputMethod_PrivateCommand *privateCommand[] = {keyCommand1, keyCommand2, keyCommand3};
326     InputMethod_ErrorCode returnValue = OH_InputMethodProxy_SendPrivateCommand(g_inputMethodProxy, privateCommand, 3);
327     if (returnValue == InputMethod_ErrorCode::IME_ERR_OK) {
328         napi_value result = nullptr;
329         NAPI_CALL(env, napi_create_string_utf8(env, "OK", NAPI_AUTO_LENGTH, &result));
330         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_InputMethodProxy_SendPrivateCommand successful.");
331         return result;
332     }
333     OH_PrivateCommand_Destroy(*privateCommand);
334     napi_value result = nullptr;
335     NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
336     OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_InputMethodProxy_SendPrivateCommand fail.");
337     return result;
338 }
339 
SUB_IMF_NDK_PrivateCommand_SetKey_0100(napi_env env, napi_callback_info info)340 static napi_value SUB_IMF_NDK_PrivateCommand_SetKey_0100(napi_env env, napi_callback_info info)
341 {
342     std::string key = "key";
343     auto privateCommand = OH_PrivateCommand_Create(const_cast<char *>(key.c_str()), key.length());
344 
345     std::string newKey = "newKey";
346     InputMethod_ErrorCode returnValue =
347         OH_PrivateCommand_SetKey(privateCommand, const_cast<char *>(newKey.c_str()), newKey.length());
348     if (returnValue != InputMethod_ErrorCode::IME_ERR_OK) {
349         napi_value result = nullptr;
350         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
351         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_InputMethodProxy_SendPrivateCommand fail.");
352         return result;
353     }
354 
355     const char *actStrKey = nullptr;
356     size_t actStrKeyLength = 0;
357     returnValue = OH_PrivateCommand_GetKey(privateCommand, &actStrKey, &actStrKeyLength);
358     if (actStrKey != newKey) {
359         napi_value result = nullptr;
360         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
361         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_PrivateCommand_GetKey fail.");
362         return result;
363     }
364 
365     returnValue = OH_PrivateCommand_SetBoolValue(privateCommand, true);
366     if (returnValue == InputMethod_ErrorCode::IME_ERR_OK) {
367         napi_value result = nullptr;
368         NAPI_CALL(env, napi_create_string_utf8(env, "OK", NAPI_AUTO_LENGTH, &result));
369         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_PrivateCommand_SetBoolValue successful.");
370         return result;
371     }
372 
373     bool actBoolValue = false;
374     returnValue = OH_PrivateCommand_GetBoolValue(privateCommand, &actBoolValue);
375     if (actBoolValue != true) {
376         napi_value result = nullptr;
377         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
378         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_PrivateCommand_GetBoolValue fail.");
379         return result;
380     }
381 
382     returnValue = OH_PrivateCommand_SetIntValue(privateCommand, 0);
383     if (returnValue == InputMethod_ErrorCode::IME_ERR_OK) {
384         napi_value result = nullptr;
385         NAPI_CALL(env, napi_create_string_utf8(env, "OK", NAPI_AUTO_LENGTH, &result));
386         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_PrivateCommand_SetIntValue successful.");
387         return result;
388     }
389 
390     int32_t actIntValue = 5;
391     returnValue = OH_PrivateCommand_GetIntValue(privateCommand, &actIntValue);
392     if (actIntValue != 0) {
393         napi_value result = nullptr;
394         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
395         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_PrivateCommand_GetIntValue fail.");
396         return result;
397     }
398 
399     std::string extStrValue = "testString";
400     returnValue =
401         OH_PrivateCommand_SetStrValue(privateCommand, const_cast<char *>(extStrValue.c_str()), extStrValue.length());
402     if (returnValue == InputMethod_ErrorCode::IME_ERR_OK) {
403         napi_value result = nullptr;
404         NAPI_CALL(env, napi_create_string_utf8(env, "OK", NAPI_AUTO_LENGTH, &result));
405         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_PrivateCommand_SetStrValue successful.");
406         return result;
407     }
408 
409     const char *actStrValue = nullptr;
410     size_t actStrValueLength = 0;
411     returnValue = OH_PrivateCommand_GetStrValue(privateCommand, &actStrValue, &actStrValueLength);
412     if (actStrValue != extStrValue) {
413         napi_value result = nullptr;
414         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
415         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>NDKTest====>OH_PrivateCommand_GetStrValue fail.");
416         return result;
417     }
418 
419     InputMethod_CommandValueType valueType = IME_COMMAND_VALUE_TYPE_INT32;
420     OH_PrivateCommand_GetValueType(privateCommand, &valueType);
421     if (valueType != IME_COMMAND_VALUE_TYPE_STRING) {
422         napi_value result = nullptr;
423         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
424         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_PrivateCommand_GetBoolValue fail.");
425         return result;
426     }
427 
428     napi_value result = nullptr;
429     NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
430     OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_PrivateCommand_GetValueType fail.");
431     return result;
432 }
433 
SUB_IMF_NDK_TextAvoidInfo_Create_0100(napi_env env, napi_callback_info info)434 static napi_value SUB_IMF_NDK_TextAvoidInfo_Create_0100(napi_env env, napi_callback_info info)
435 {
436     double expPositionY = 20;
437     double expHeight = 20;
438     g_textAvoidInfo = OH_TextAvoidInfo_Create(expPositionY, expHeight);
439     double actPositionY = 0.0;
440     double actHeight = 0.0;
441     InputMethod_ErrorCode returnValue = OH_TextAvoidInfo_SetPositionY(g_textAvoidInfo, expPositionY);
442     if (returnValue != InputMethod_ErrorCode::IME_ERR_OK) {
443         napi_value result = nullptr;
444         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
445         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_TextAvoidInfo_SetPositionY fail.");
446         return result;
447     }
448 
449     returnValue = OH_TextAvoidInfo_SetHeight(g_textAvoidInfo, expHeight);
450     if (returnValue != InputMethod_ErrorCode::IME_ERR_OK) {
451         napi_value result = nullptr;
452         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
453         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_TextAvoidInfo_SetHeight fail.");
454         return result;
455     }
456 
457     returnValue = OH_TextAvoidInfo_GetPositionY(g_textAvoidInfo, &actPositionY);
458     if (returnValue != InputMethod_ErrorCode::IME_ERR_OK) {
459         napi_value result = nullptr;
460         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
461         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_TextAvoidInfo_GetPositionY fail.");
462         return result;
463     }
464     if (actPositionY != expPositionY) {
465         napi_value result = nullptr;
466         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
467         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>NDKTest====>expect != actual fail.");
468         return result;
469     }
470 
471     returnValue = OH_TextAvoidInfo_GetHeight(g_textAvoidInfo, &actHeight);
472     if (returnValue != InputMethod_ErrorCode::IME_ERR_OK) {
473         napi_value result = nullptr;
474         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
475         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_TextAvoidInfo_GetHeight fail.");
476         return result;
477     }
478     if (actHeight != expHeight) {
479         napi_value result = nullptr;
480         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
481         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>NDKTest====>expect != actual fail.");
482         return result;
483     }
484 
485     OH_TextAvoidInfo_Destroy(g_textAvoidInfo);
486 
487     napi_value result = nullptr;
488     NAPI_CALL(env, napi_create_string_utf8(env, "OK", NAPI_AUTO_LENGTH, &result));
489     OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>SUB_IMF_NDK_TextAvoidInfo_Create_0100 successful.");
490     return result;
491 }
492 
SUB_IMF_NDK_TextConfig_Create_0100(napi_env env, napi_callback_info info)493 static napi_value SUB_IMF_NDK_TextConfig_Create_0100(napi_env env, napi_callback_info info)
494 {
495     InputMethod_TextConfig *config = OH_TextConfig_Create();
496     InputMethod_TextInputType expInputType = IME_TEXT_INPUT_TYPE_NUMBER_DECIMAL;
497     InputMethod_TextInputType actInputType = IME_TEXT_INPUT_TYPE_NONE;
498     InputMethod_EnterKeyType expEnterKeyType = IME_ENTER_KEY_UNSPECIFIED;
499     InputMethod_EnterKeyType actEnterKeyType = IME_ENTER_KEY_NONE;
500 
501     InputMethod_ErrorCode returnValue = OH_TextConfig_SetInputType(config, expInputType);
502     if (returnValue != InputMethod_ErrorCode::IME_ERR_OK) {
503         napi_value result = nullptr;
504         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
505         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_TextConfig_SetInputType fail.");
506         return result;
507     }
508     returnValue = OH_TextConfig_GetInputType(config, &actInputType);
509     if (actInputType != expInputType) {
510         napi_value result = nullptr;
511         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
512         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_TextConfig_GetInputType fail.");
513         return result;
514     }
515 
516     returnValue = OH_TextConfig_SetEnterKeyType(config, expEnterKeyType);
517     if (returnValue != InputMethod_ErrorCode::IME_ERR_OK) {
518         napi_value result = nullptr;
519         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
520         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_TextConfig_SetEnterKeyType fail.");
521         return result;
522     }
523     returnValue = OH_TextConfig_GetEnterKeyType(config, &actEnterKeyType);
524     if (actEnterKeyType != expEnterKeyType) {
525         napi_value result = nullptr;
526         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
527         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>expect != actual fail.");
528         return result;
529     }
530 
531     returnValue = OH_TextConfig_SetPreviewTextSupport(config, true);
532     if (returnValue != InputMethod_ErrorCode::IME_ERR_OK) {
533         napi_value result = nullptr;
534         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
535         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_TextConfig_SetPreviewTextSupport fail.");
536         return result;
537     }
538     bool isPreviewTextSupported = false;
539     returnValue = OH_TextConfig_IsPreviewTextSupported(config, &isPreviewTextSupported);
540     if (returnValue != InputMethod_ErrorCode::IME_ERR_OK) {
541         napi_value result = nullptr;
542         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
543         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_TextConfig_IsPreviewTextSupported fail.");
544         return result;
545     }
546     if (isPreviewTextSupported != true) {
547         napi_value result = nullptr;
548         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
549         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_InputMethodProxy_SendPrivateCommand fail.");
550         return result;
551     }
552 
553     int32_t expStart = 1;
554     int32_t expEnd = 2;
555     returnValue = OH_TextConfig_SetSelection(config, expStart, expEnd);
556     if (returnValue != InputMethod_ErrorCode::IME_ERR_OK) {
557         napi_value result = nullptr;
558         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
559         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_TextConfig_SetSelection fail.");
560         return result;
561     }
562     int32_t actStart = 0;
563     int32_t actEnd = 0;
564     returnValue = OH_TextConfig_GetSelection(config, &actStart, &actEnd);
565     if (returnValue != InputMethod_ErrorCode::IME_ERR_OK) {
566         napi_value result = nullptr;
567         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
568         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_TextConfig_GetSelection fail.");
569         return result;
570     }
571     if (actStart != expStart or actEnd != expEnd) {
572         napi_value result = nullptr;
573         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
574         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>expect != actual fail.");
575         return result;
576     }
577 
578     int32_t expWindowId = 1;
579     returnValue = OH_TextConfig_SetWindowId(config, expWindowId);
580     if (returnValue != InputMethod_ErrorCode::IME_ERR_OK) {
581         napi_value result = nullptr;
582         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
583         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_TextConfig_SetWindowId fail.");
584         return result;
585     }
586     int32_t actWindowId = 2;
587     returnValue = OH_TextConfig_GetWindowId(config, &actWindowId);
588     if (returnValue != InputMethod_ErrorCode::IME_ERR_OK) {
589         napi_value result = nullptr;
590         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
591         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_TextConfig_GetWindowId fail.");
592         return result;
593     }
594     if (actWindowId != expWindowId) {
595         napi_value result = nullptr;
596         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
597         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_InputMethodProxy_SendPrivateCommand fail.");
598         return result;
599     }
600 
601     InputMethod_CursorInfo *cursorInfo = nullptr;
602     returnValue = OH_TextConfig_GetCursorInfo(config, &cursorInfo);
603     if (returnValue != InputMethod_ErrorCode::IME_ERR_OK) {
604         napi_value result = nullptr;
605         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
606         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_TextConfig_GetCursorInfo fail.");
607         return result;
608     }
609 
610     double expPositionY = 20;
611     double expHeight = 20;
612     g_textAvoidInfo = OH_TextAvoidInfo_Create(expPositionY, expHeight);
613     returnValue = OH_TextConfig_GetTextAvoidInfo(config, &g_textAvoidInfo);
614     if (returnValue != InputMethod_ErrorCode::IME_ERR_OK) {
615         napi_value result = nullptr;
616         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
617         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_TextConfig_GetCursorInfo fail.");
618         return result;
619     }
620 
621     OH_TextConfig_Destroy(config);
622 
623     napi_value result = nullptr;
624     NAPI_CALL(env, napi_create_string_utf8(env, "OK", NAPI_AUTO_LENGTH, &result));
625     OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>SUB_IMF_NDK_TextConfig_Create_0100 successful.");
626     return result;
627 }
628 
SUB_IMF_NDK_TextEditorProxy_Get_0100(napi_env env, napi_callback_info info)629 static napi_value SUB_IMF_NDK_TextEditorProxy_Get_0100(napi_env env, napi_callback_info info)
630 {
631     TextEditorProxy_Create();
632     OH_TextEditorProxy_GetTextConfigFunc getTextConfigFunc = nullptr;
633     InputMethod_ErrorCode returnValue = OH_TextEditorProxy_GetGetTextConfigFunc(g_txtEditProxy, &getTextConfigFunc);
634     if (returnValue != InputMethod_ErrorCode::IME_ERR_OK) {
635         napi_value result = nullptr;
636         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
637         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_TextEditorProxy_GetGetTextConfigFunc fail.");
638         return result;
639     }
640 
641     OH_TextEditorProxy_InsertTextFunc insertTextFunc = nullptr;
642     returnValue = OH_TextEditorProxy_GetInsertTextFunc(g_txtEditProxy, &insertTextFunc);
643     if (returnValue != InputMethod_ErrorCode::IME_ERR_OK) {
644         napi_value result = nullptr;
645         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
646         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_TextEditorProxy_GetInsertTextFunc fail.");
647         return result;
648     }
649 
650     OH_TextEditorProxy_DeleteForwardFunc deleteForwardFunc = nullptr;
651     returnValue = OH_TextEditorProxy_GetDeleteForwardFunc(g_txtEditProxy, &deleteForwardFunc);
652     if (returnValue != InputMethod_ErrorCode::IME_ERR_OK) {
653         napi_value result = nullptr;
654         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
655         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_TextEditorProxy_GetDeleteForwardFunc fail.");
656         return result;
657     }
658 
659     OH_TextEditorProxy_DeleteBackwardFunc deleteBackwardFunc = nullptr;
660     returnValue = OH_TextEditorProxy_GetDeleteBackwardFunc(g_txtEditProxy, &deleteBackwardFunc);
661     if (returnValue != InputMethod_ErrorCode::IME_ERR_OK) {
662         napi_value result = nullptr;
663         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
664         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_TextEditorProxy_GetDeleteBackwardFunc fail.");
665         return result;
666     }
667 
668     OH_TextEditorProxy_SendKeyboardStatusFunc sendKeyboardStatusFunc = nullptr;
669     returnValue = OH_TextEditorProxy_GetSendKeyboardStatusFunc(g_txtEditProxy, &sendKeyboardStatusFunc);
670     if (returnValue != InputMethod_ErrorCode::IME_ERR_OK) {
671         napi_value result = nullptr;
672         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
673         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_TextEditorProxy_GetSendKeyboardStatusFunc fail.");
674         return result;
675     }
676 
677     OH_TextEditorProxy_SendEnterKeyFunc sendEnterKeyFunc = nullptr;
678     returnValue = OH_TextEditorProxy_GetSendEnterKeyFunc(g_txtEditProxy, &sendEnterKeyFunc);
679     if (returnValue != InputMethod_ErrorCode::IME_ERR_OK) {
680         napi_value result = nullptr;
681         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
682         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_TextEditorProxy_GetSendEnterKeyFunc fail.");
683         return result;
684     }
685 
686     OH_TextEditorProxy_MoveCursorFunc moveCursorFunc = nullptr;
687     returnValue = OH_TextEditorProxy_GetMoveCursorFunc(g_txtEditProxy, &moveCursorFunc);
688     if (returnValue != InputMethod_ErrorCode::IME_ERR_OK) {
689         napi_value result = nullptr;
690         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
691         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_TextEditorProxy_GetMoveCursorFunc fail.");
692         return result;
693     }
694 
695     OH_TextEditorProxy_HandleSetSelectionFunc handleSetSelectionFunc = nullptr;
696     returnValue = OH_TextEditorProxy_GetHandleSetSelectionFunc(g_txtEditProxy, &handleSetSelectionFunc);
697     if (returnValue != InputMethod_ErrorCode::IME_ERR_OK) {
698         napi_value result = nullptr;
699         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
700         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_TextEditorProxy_GetHandleSetSelectionFunc fail.");
701         return result;
702     }
703 
704     OH_TextEditorProxy_HandleExtendActionFunc handleExtendActionFunc = nullptr;
705     returnValue = OH_TextEditorProxy_GetHandleExtendActionFunc(g_txtEditProxy, &handleExtendActionFunc);
706     if (returnValue != InputMethod_ErrorCode::IME_ERR_OK) {
707         napi_value result = nullptr;
708         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
709         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_TextEditorProxy_GetHandleExtendActionFunc fail.");
710         return result;
711     }
712 
713     OH_TextEditorProxy_GetLeftTextOfCursorFunc getLeftTextOfCursorFunc = nullptr;
714     returnValue = OH_TextEditorProxy_GetGetLeftTextOfCursorFunc(g_txtEditProxy, &getLeftTextOfCursorFunc);
715     if (returnValue != InputMethod_ErrorCode::IME_ERR_OK) {
716         napi_value result = nullptr;
717         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
718         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_TextEditorProxy_GetGetLeftTextOfCursorFunc fail.");
719         return result;
720     }
721 
722     OH_TextEditorProxy_GetRightTextOfCursorFunc getRightTextOfCursorFunc = nullptr;
723     returnValue = OH_TextEditorProxy_GetGetRightTextOfCursorFunc(g_txtEditProxy, &getRightTextOfCursorFunc);
724     if (returnValue != InputMethod_ErrorCode::IME_ERR_OK) {
725         napi_value result = nullptr;
726         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
727         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_TextEditorProxy_GetGetRightTextOfCursorFunc fail.");
728         return result;
729     }
730 
731     OH_TextEditorProxy_GetTextIndexAtCursorFunc getTextIndexAtCursorFunc = nullptr;
732     returnValue = OH_TextEditorProxy_GetGetTextIndexAtCursorFunc(g_txtEditProxy, &getTextIndexAtCursorFunc);
733     if (returnValue != InputMethod_ErrorCode::IME_ERR_OK) {
734         napi_value result = nullptr;
735         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
736         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_TextEditorProxy_GetGetTextIndexAtCursorFunc fail.");
737         return result;
738     }
739 
740     OH_TextEditorProxy_ReceivePrivateCommandFunc receivePrivateCommandFunc = nullptr;
741     returnValue = OH_TextEditorProxy_GetReceivePrivateCommandFunc(g_txtEditProxy, &receivePrivateCommandFunc);
742     if (returnValue != InputMethod_ErrorCode::IME_ERR_OK) {
743         napi_value result = nullptr;
744         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
745         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_TextEditorProxy_GetReceivePrivateCommandFunc fail.");
746         return result;
747     }
748 
749     OH_TextEditorProxy_SetPreviewTextFunc setPreviewTextFunc = nullptr;
750     returnValue = OH_TextEditorProxy_GetSetPreviewTextFunc(g_txtEditProxy, &setPreviewTextFunc);
751     if (returnValue != InputMethod_ErrorCode::IME_ERR_OK) {
752         napi_value result = nullptr;
753         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
754         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_TextEditorProxy_GetSetPreviewTextFunc fail.");
755         return result;
756     }
757 
758     OH_TextEditorProxy_FinishTextPreviewFunc finishTextPreviewFunc = nullptr;
759     returnValue = OH_TextEditorProxy_GetFinishTextPreviewFunc(g_txtEditProxy, &finishTextPreviewFunc);
760     if (returnValue != InputMethod_ErrorCode::IME_ERR_OK) {
761         napi_value result = nullptr;
762         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
763         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_TextEditorProxy_GetFinishTextPreviewFunc fail.");
764         return result;
765     }
766 
767     napi_value result = nullptr;
768     NAPI_CALL(env, napi_create_string_utf8(env, "OK", NAPI_AUTO_LENGTH, &result));
769     OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>SUB_IMF_NDK_TextEditorProxy_Get_0100 successful.");
770     return result;
771 }
772 
SUB_IMF_NDK_CursorInfo_Create_0100(napi_env env, napi_callback_info info)773 static napi_value SUB_IMF_NDK_CursorInfo_Create_0100(napi_env env, napi_callback_info info)
774 {
775     double expLeft = 1.1;
776     double expTop = 2.2;
777     double expWidth = 3.3;
778     double expHeight = 4.4;
779     auto cursorInfo = OH_CursorInfo_Create(expLeft, expTop, expWidth, expHeight);
780     double actLeft = 0;
781     double actTop = 0;
782     double actWidth = 0;
783     double actHeight = 0;
784     InputMethod_ErrorCode returnValue = OH_CursorInfo_SetRect(cursorInfo, actLeft, actTop, actWidth, actHeight);
785     if (returnValue != InputMethod_ErrorCode::IME_ERR_OK) {
786         napi_value result = nullptr;
787         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
788         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_CursorInfo_SetRect fail.");
789         return result;
790     }
791 
792     returnValue = OH_CursorInfo_GetRect(cursorInfo, &actLeft, &actTop, &actWidth, &actHeight);
793     if (returnValue != InputMethod_ErrorCode::IME_ERR_OK) {
794         napi_value result = nullptr;
795         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
796         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "", "NDKTest====>OH_CursorInfo_GetRect fail.");
797         return result;
798     }
799     if (actLeft != 0.0 or actTop != 0.0 or actWidth != 0.0 or actHeight != 0.0) {
800         napi_value result = nullptr;
801         NAPI_CALL(env, napi_create_int32(env, returnValue, &result));
802         OH_LOG_Print(LOG_APP, LOG_INFO, 0, "",
803                      "NDKTest====>SUB_IMF_NDK_InputMethodProxy_NotifyConfigurationChange_0100 fail.");
804         return result;
805     }
806     OH_CursorInfo_Destroy(cursorInfo);
807 
808     napi_value result = nullptr;
809     NAPI_CALL(env, napi_create_string_utf8(env, "OK", NAPI_AUTO_LENGTH, &result));
810     OH_LOG_Print(LOG_APP, LOG_INFO, 0, "",
811                  "NDKTest====>SUB_IMF_NDK_InputMethodProxy_NotifyConfigurationChange_0100 successful.");
812     return result;
813 }
814 
815 
816 EXTERN_C_START
Init(napi_env env, napi_value exports)817 static napi_value Init(napi_env env, napi_value exports)
818 {
819     napi_property_descriptor desc[] = {
820         {"SUB_IMF_NDK_AttachOptions_Create_0100", nullptr, SUB_IMF_NDK_AttachOptions_Create_0100, nullptr, nullptr,
821          nullptr, napi_default, nullptr},
822         {"SUB_IMF_NDK_InputMethodController_Attach_0100", nullptr, SUB_IMF_NDK_InputMethodController_Attach_0100,
823          nullptr, nullptr, nullptr, napi_default, nullptr},
824         {"SUB_IMF_NDK_InputMethodController_Detach_0100", nullptr, SUB_IMF_NDK_InputMethodController_Detach_0100,
825          nullptr, nullptr, nullptr, napi_default, nullptr},
826         {"SUB_IMF_NDK_InputMethodProxy_ShowKeyboard_0100", nullptr, SUB_IMF_NDK_InputMethodProxy_ShowKeyboard_0100,
827          nullptr, nullptr, nullptr, napi_default, nullptr},
828         {"SUB_IMF_NDK_InputMethodProxy_HideKeyboard_0100", nullptr, SUB_IMF_NDK_InputMethodProxy_HideKeyboard_0100,
829          nullptr, nullptr, nullptr, napi_default, nullptr},
830         {"SUB_IMF_NDK_InputMethodProxy_NotifySelectionChange_0100", nullptr,
831          SUB_IMF_NDK_InputMethodProxy_NotifySelectionChange_0100, nullptr, nullptr, nullptr, napi_default, nullptr},
832         {"SUB_IMF_NDK_InputMethodProxy_NotifyConfigurationChange_0100", nullptr,
833          SUB_IMF_NDK_InputMethodProxy_NotifyConfigurationChange_0100, nullptr, nullptr, nullptr, napi_default, nullptr},
834         {"SUB_IMF_NDK_InputMethodProxy_NotifyCursorUpdate_0100", nullptr,
835          SUB_IMF_NDK_InputMethodProxy_NotifyCursorUpdate_0100, nullptr, nullptr, nullptr, napi_default, nullptr},
836         {"SUB_IMF_NDK_InputMethodProxy_SendPrivateCommand_0100", nullptr,
837          SUB_IMF_NDK_InputMethodProxy_SendPrivateCommand_0100, nullptr, nullptr, nullptr, napi_default, nullptr},
838         {"SUB_IMF_NDK_PrivateCommand_SetKey_0100", nullptr, SUB_IMF_NDK_PrivateCommand_SetKey_0100, nullptr, nullptr,
839          nullptr, napi_default, nullptr},
840         {"SUB_IMF_NDK_TextAvoidInfo_Create_0100", nullptr, SUB_IMF_NDK_TextAvoidInfo_Create_0100, nullptr, nullptr,
841          nullptr, napi_default, nullptr},
842         {"SUB_IMF_NDK_TextConfig_Create_0100", nullptr, SUB_IMF_NDK_TextConfig_Create_0100, nullptr, nullptr, nullptr,
843          napi_default, nullptr},
844         {"SUB_IMF_NDK_TextEditorProxy_Get_0100", nullptr, SUB_IMF_NDK_TextEditorProxy_Get_0100, nullptr, nullptr,
845          nullptr, napi_default, nullptr},
846         {"SUB_IMF_NDK_CursorInfo_Create_0100", nullptr, SUB_IMF_NDK_CursorInfo_Create_0100, nullptr, nullptr, nullptr,
847          napi_default, nullptr}};
848     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
849     return exports;
850 }
851 EXTERN_C_END
852 
853 static napi_module demoModule = {
854     .nm_version = 1,
855     .nm_flags = 0,
856     .nm_filename = nullptr,
857     .nm_register_func = Init,
858     .nm_modname = "entry",
859     .nm_priv = ((void *)0),
860     .reserved = {0},
861 };
862 
RegisterEntryModule(void)863 extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }
864