1 /*
2 * Copyright (c) 2025 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 <gtest/gtest.h>
17
18 #include "global.h"
19 #include "native_inputmethod_types.h"
20
HWTEST_F(InputMethod_PrivateCommandTest,Create_ValidKey_ShouldCreateObject)21 HWTEST_F(InputMethod_PrivateCommandTest, Create_ValidKey_ShouldCreateObject)
22 {
23 char key[] = "testKey";
24 InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1);
25 EXPECT_NE(command, nullptr);
26 OH_PrivateCommand_Destroy(command);
27 }
28
HWTEST_F(InputMethod_PrivateCommandTest,Create_EmptyKey_ShouldCreateObject)29 HWTEST_F(InputMethod_PrivateCommandTest, Create_EmptyKey_ShouldCreateObject)
30 {
31 char key[] = "";
32 InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, 0);
33 EXPECT_NE(command, nullptr);
34 OH_PrivateCommand_Destroy(command);
35 }
36
HWTEST_F(InputMethod_PrivateCommandTest,Destroy_ValidCommand_ShouldNotCrash)37 HWTEST_F(InputMethod_PrivateCommandTest, Destroy_ValidCommand_ShouldNotCrash)
38 {
39 char key[] = "testKey";
40 InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1);
41 EXPECT_NE(command, nullptr);
42 OH_PrivateCommand_Destroy(command);
43 }
44
HWTEST_F(InputMethod_PrivateCommandTest,Destroy_NullCommand_ShouldLogError)45 HWTEST_F(InputMethod_PrivateCommandTest, Destroy_NullCommand_ShouldLogError)
46 {
47 OH_PrivateCommand_Destroy(nullptr);
48 }
49
HWTEST_F(InputMethod_PrivateCommandTest,SetKey_ValidCommandAndKey_ShouldSetKey)50 HWTEST_F(InputMethod_PrivateCommandTest, SetKey_ValidCommandAndKey_ShouldSetKey)
51 {
52 char key[] = "testKey";
53 InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1);
54 EXPECT_NE(command, nullptr);
55
56 char newKey[] = "newKey";
57 InputMethod_ErrorCode result = OH_PrivateCommand_SetKey(command, newKey, sizeof(newKey) - 1);
58 EXPECT_EQ(result, IME_ERR_OK);
59
60 const char *actualKey;
61 size_t keyLength;
62 result = OH_PrivateCommand_GetKey(command, &actualKey, &keyLength);
63 EXPECT_EQ(result, IME_ERR_OK);
64 EXPECT_STREQ(actualKey, newKey);
65 EXPECT_EQ(keyLength, sizeof(newKey) - 1);
66
67 OH_PrivateCommand_Destroy(command);
68 }
69
HWTEST_F(InputMethod_PrivateCommandTest,SetKey_NullCommand_ShouldReturnError)70 HWTEST_F(InputMethod_PrivateCommandTest, SetKey_NullCommand_ShouldReturnError)
71 {
72 char key[] = "testKey";
73 InputMethod_ErrorCode result = OH_PrivateCommand_SetKey(nullptr, key, sizeof(key) - 1);
74 EXPECT_EQ(result, IME_ERR_NULL_POINTER);
75 }
76
HWTEST_F(InputMethod_PrivateCommandTest,SetKey_NullKey_ShouldReturnError)77 HWTEST_F(InputMethod_PrivateCommandTest, SetKey_NullKey_ShouldReturnError)
78 {
79 char key[] = "testKey";
80 InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1);
81 EXPECT_NE(command, nullptr);
82
83 InputMethod_ErrorCode result = OH_PrivateCommand_SetKey(command, nullptr, sizeof(key) - 1);
84 EXPECT_EQ(result, IME_ERR_NULL_POINTER);
85
86 OH_PrivateCommand_Destroy(command);
87 }
88
HWTEST_F(InputMethod_PrivateCommandTest,SetBoolValue_ValidCommand_ShouldSetValue)89 HWTEST_F(InputMethod_PrivateCommandTest, SetBoolValue_ValidCommand_ShouldSetValue)
90 {
91 char key[] = "testKey";
92 InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1);
93 EXPECT_NE(command, nullptr);
94
95 InputMethod_ErrorCode result = OH_PrivateCommand_SetBoolValue(command, true);
96 EXPECT_EQ(result, IME_ERR_OK);
97
98 bool value;
99 result = OH_PrivateCommand_GetBoolValue(command, &value);
100 EXPECT_EQ(result, IME_ERR_OK);
101 EXPECT_TRUE(value);
102
103 OH_PrivateCommand_Destroy(command);
104 }
105
HWTEST_F(InputMethod_PrivateCommandTest,SetBoolValue_NullCommand_ShouldReturnError)106 HWTEST_F(InputMethod_PrivateCommandTest, SetBoolValue_NullCommand_ShouldReturnError)
107 {
108 InputMethod_ErrorCode result = OH_PrivateCommand_SetBoolValue(nullptr, true);
109 EXPECT_EQ(result, IME_ERR_NULL_POINTER);
110 }
111
HWTEST_F(InputMethod_PrivateCommandTest,SetIntValue_ValidCommand_ShouldSetValue)112 HWTEST_F(InputMethod_PrivateCommandTest, SetIntValue_ValidCommand_ShouldSetValue)
113 {
114 char key[] = "testKey";
115 InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1);
116 EXPECT_NE(command, nullptr);
117
118 InputMethod_ErrorCode result = OH_PrivateCommand_SetIntValue(command, 42);
119 EXPECT_EQ(result, IME_ERR_OK);
120
121 int32_t value;
122 result = OH_PrivateCommand_GetIntValue(command, &value);
123 EXPECT_EQ(result, IME_ERR_OK);
124 EXPECT_EQ(value, 42);
125
126 OH_PrivateCommand_Destroy(command);
127 }
128
HWTEST_F(InputMethod_PrivateCommandTest,SetIntValue_NullCommand_ShouldReturnError)129 HWTEST_F(InputMethod_PrivateCommandTest, SetIntValue_NullCommand_ShouldReturnError)
130 {
131 InputMethod_ErrorCode result = OH_PrivateCommand_SetIntValue(nullptr, 42);
132 EXPECT_EQ(result, IME_ERR_NULL_POINTER);
133 }
134
HWTEST_F(InputMethod_PrivateCommandTest,SetStrValue_ValidCommandAndValue_ShouldSetValue)135 HWTEST_F(InputMethod_PrivateCommandTest, SetStrValue_ValidCommandAndValue_ShouldSetValue)
136 {
137 char key[] = "testKey";
138 InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1);
139 EXPECT_NE(command, nullptr);
140
141 char value[] = "testValue";
142 InputMethod_ErrorCode result = OH_PrivateCommand_SetStrValue(command, value, sizeof(value) - 1);
143 EXPECT_EQ(result, IME_ERR_OK);
144
145 const char *actualValue;
146 size_t valueLength;
147 result = OH_PrivateCommand_GetStrValue(command, &actualValue, &valueLength);
148 EXPECT_EQ(result, IME_ERR_OK);
149 EXPECT_STREQ(actualValue, value);
150 EXPECT_EQ(valueLength, sizeof(value) - 1);
151
152 OH_PrivateCommand_Destroy(command);
153 }
154
HWTEST_F(InputMethod_PrivateCommandTest,SetStrValue_NullCommand_ShouldReturnError)155 HWTEST_F(InputMethod_PrivateCommandTest, SetStrValue_NullCommand_ShouldReturnError)
156 {
157 char value[] = "testValue";
158 InputMethod_ErrorCode result = OH_PrivateCommand_SetStrValue(nullptr, value, sizeof(value) - 1);
159 EXPECT_EQ(result, IME_ERR_NULL_POINTER);
160 }
161
HWTEST_F(InputMethod_PrivateCommandTest,SetStrValue_NullValue_ShouldReturnError)162 HWTEST_F(InputMethod_PrivateCommandTest, SetStrValue_NullValue_ShouldReturnError)
163 {
164 char key[] = "testKey";
165 InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1);
166 EXPECT_NE(command, nullptr);
167
168 InputMethod_ErrorCode result = OH_PrivateCommand_SetStrValue(command, nullptr, 0);
169 EXPECT_EQ(result, IME_ERR_NULL_POINTER);
170
171 OH_PrivateCommand_Destroy(command);
172 }
173
HWTEST_F(InputMethod_PrivateCommandTest,GetKey_ValidCommand_ShouldReturnKey)174 HWTEST_F(InputMethod_PrivateCommandTest, GetKey_ValidCommand_ShouldReturnKey)
175 {
176 char key[] = "testKey";
177 InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1);
178 EXPECT_NE(command, nullptr);
179
180 const char *actualKey;
181 size_t keyLength;
182 InputMethod_ErrorCode result = OH_PrivateCommand_GetKey(command, &actualKey, &keyLength);
183 EXPECT_EQ(result, IME_ERR_OK);
184 EXPECT_STREQ(actualKey, key);
185 EXPECT_EQ(keyLength, sizeof(key) - 1);
186
187 OH_PrivateCommand_Destroy(command);
188 }
189
HWTEST_F(InputMethod_PrivateCommandTest,GetKey_NullCommand_ShouldReturnError)190 HWTEST_F(InputMethod_PrivateCommandTest, GetKey_NullCommand_ShouldReturnError)
191 {
192 const char *key;
193 size_t keyLength;
194 InputMethod_ErrorCode result = OH_PrivateCommand_GetKey(nullptr, &key, &keyLength);
195 EXPECT_EQ(result, IME_ERR_NULL_POINTER);
196 }
197
HWTEST_F(InputMethod_PrivateCommandTest,GetKey_NullKey_ShouldReturnError)198 HWTEST_F(InputMethod_PrivateCommandTest, GetKey_NullKey_ShouldReturnError)
199 {
200 char key[] = "testKey";
201 InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1);
202 EXPECT_NE(command, nullptr);
203
204 size_t keyLength;
205 InputMethod_ErrorCode result = OH_PrivateCommand_GetKey(command, nullptr, &keyLength);
206 EXPECT_EQ(result, IME_ERR_NULL_POINTER);
207
208 OH_PrivateCommand_Destroy(command);
209 }
210
HWTEST_F(InputMethod_PrivateCommandTest,GetKey_NullKeyLength_ShouldReturnError)211 HWTEST_F(InputMethod_PrivateCommandTest, GetKey_NullKeyLength_ShouldReturnError)
212 {
213 char key[] = "testKey";
214 InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1);
215 EXPECT_NE(command, nullptr);
216
217 const char *actualKey;
218 InputMethod_ErrorCode result = OH_PrivateCommand_GetKey(command, &actualKey, nullptr);
219 EXPECT_EQ(result, IME_ERR_NULL_POINTER);
220
221 OH_PrivateCommand_Destroy(command);
222 }
223
HWTEST_F(InputMethod_PrivateCommandTest,GetValueType_ValidCommand_ShouldReturnCorrectType)224 HWTEST_F(InputMethod_PrivateCommandTest, GetValueType_ValidCommand_ShouldReturnCorrectType)
225 {
226 char key[] = "testKey";
227 InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1);
228 EXPECT_NE(command, nullptr);
229
230 InputMethod_ErrorCode result = OH_PrivateCommand_SetBoolValue(command, true);
231 EXPECT_EQ(result, IME_ERR_OK);
232
233 InputMethod_CommandValueType type;
234 result = OH_PrivateCommand_GetValueType(command, &type);
235 EXPECT_EQ(result, IME_ERR_OK);
236 EXPECT_EQ(type, IME_COMMAND_VALUE_TYPE_BOOL);
237
238 OH_PrivateCommand_Destroy(command);
239 }
240
HWTEST_F(InputMethod_PrivateCommandTest,GetValueType_NullCommand_ShouldReturnError)241 HWTEST_F(InputMethod_PrivateCommandTest, GetValueType_NullCommand_ShouldReturnError)
242 {
243 InputMethod_CommandValueType type;
244 InputMethod_ErrorCode result = OH_PrivateCommand_GetValueType(nullptr, &type);
245 EXPECT_EQ(result, IME_ERR_NULL_POINTER);
246 }
247
HWTEST_F(InputMethod_PrivateCommandTest,GetValueType_NullType_ShouldReturnError)248 HWTEST_F(InputMethod_PrivateCommandTest, GetValueType_NullType_ShouldReturnError)
249 {
250 char key[] = "testKey";
251 InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1);
252 EXPECT_NE(command, nullptr);
253
254 InputMethod_ErrorCode result = OH_PrivateCommand_GetValueType(command, nullptr);
255 EXPECT_EQ(result, IME_ERR_NULL_POINTER);
256
257 OH_PrivateCommand_Destroy(command);
258 }
259
HWTEST_F(InputMethod_PrivateCommandTest,GetBoolValue_ValidCommand_ShouldReturnBool)260 HWTEST_F(InputMethod_PrivateCommandTest, GetBoolValue_ValidCommand_ShouldReturnBool)
261 {
262 char key[] = "testKey";
263 InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1);
264 EXPECT_NE(command, nullptr);
265
266 InputMethod_ErrorCode result = OH_PrivateCommand_SetBoolValue(command, true);
267 EXPECT_EQ(result, IME_ERR_OK);
268
269 bool value;
270 result = OH_PrivateCommand_GetBoolValue(command, &value);
271 EXPECT_EQ(result, IME_ERR_OK);
272 EXPECT_TRUE(value);
273
274 OH_PrivateCommand_Destroy(command);
275 }
276
HWTEST_F(InputMethod_PrivateCommandTest,GetBoolValue_NullCommand_ShouldReturnError)277 HWTEST_F(InputMethod_PrivateCommandTest, GetBoolValue_NullCommand_ShouldReturnError)
278 {
279 bool value;
280 InputMethod_ErrorCode result = OH_PrivateCommand_GetBoolValue(nullptr, &value);
281 EXPECT_EQ(result, IME_ERR_NULL_POINTER);
282 }
283
HWTEST_F(InputMethod_PrivateCommandTest,GetBoolValue_NullValue_ShouldReturnError)284 HWTEST_F(InputMethod_PrivateCommandTest, GetBoolValue_NullValue_ShouldReturnError)
285 {
286 char key[] = "testKey";
287 InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1);
288 EXPECT_NE(command, nullptr);
289
290 InputMethod_ErrorCode result = OH_PrivateCommand_GetBoolValue(command, nullptr);
291 EXPECT_EQ(result, IME_ERR_NULL_POINTER);
292
293 OH_PrivateCommand_Destroy(command);
294 }
295
HWTEST_F(InputMethod_PrivateCommandTest,GetBoolValue_NonBoolValue_ShouldReturnError)296 HWTEST_F(InputMethod_PrivateCommandTest, GetBoolValue_NonBoolValue_ShouldReturnError)
297 {
298 char key[] = "testKey";
299 InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1);
300 EXPECT_NE(command, nullptr);
301
302 InputMethod_ErrorCode result = OH_PrivateCommand_SetIntValue(command, 42);
303 EXPECT_EQ(result, IME_ERR_OK);
304
305 bool value;
306 result = OH_PrivateCommand_GetBoolValue(command, &value);
307 EXPECT_EQ(result, IME_ERR_QUERY_FAILED);
308
309 OH_PrivateCommand_Destroy(command);
310 }
311
HWTEST_F(InputMethod_PrivateCommandTest,GetIntValue_ValidCommand_ShouldReturnInt)312 HWTEST_F(InputMethod_PrivateCommandTest, GetIntValue_ValidCommand_ShouldReturnInt)
313 {
314 char key[] = "testKey";
315 InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1);
316 EXPECT_NE(command, nullptr);
317
318 InputMethod_ErrorCode result = OH_PrivateCommand_SetIntValue(command, 42);
319 EXPECT_EQ(result, IME_ERR_OK);
320
321 int32_t value;
322 result = OH_PrivateCommand_GetIntValue(command, &value);
323 EXPECT_EQ(result, IME_ERR_OK);
324 EXPECT_EQ(value, 42);
325
326 OH_PrivateCommand_Destroy(command);
327 }
328
HWTEST_F(InputMethod_PrivateCommandTest,GetIntValue_NullCommand_ShouldReturnError)329 HWTEST_F(InputMethod_PrivateCommandTest, GetIntValue_NullCommand_ShouldReturnError)
330 {
331 int32_t value;
332 InputMethod_ErrorCode result = OH_PrivateCommand_GetIntValue(nullptr, &value);
333 EXPECT_EQ(result, IME_ERR_NULL_POINTER);
334 }
335
HWTEST_F(InputMethod_PrivateCommandTest,GetIntValue_NullValue_ShouldReturnError)336 HWTEST_F(InputMethod_PrivateCommandTest, GetIntValue_NullValue_ShouldReturnError)
337 {
338 char key[] = "testKey";
339 InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1);
340 EXPECT_NE(command, nullptr);
341
342 InputMethod_ErrorCode result = OH_PrivateCommand_GetIntValue(command, nullptr);
343 EXPECT_EQ(result, IME_ERR_NULL_POINTER);
344
345 OH_PrivateCommand_Destroy(command);
346 }
347
HWTEST_F(InputMethod_PrivateCommandTest,GetIntValue_NonIntValue_ShouldReturnError)348 HWTEST_F(InputMethod_PrivateCommandTest, GetIntValue_NonIntValue_ShouldReturnError)
349 {
350 char key[] = "testKey";
351 InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1);
352 EXPECT_NE(command, nullptr);
353
354 InputMethod_ErrorCode result = OH_PrivateCommand_SetBoolValue(command, true);
355 EXPECT_EQ(result, IME_ERR_OK);
356
357 int32_t value;
358 result = OH_PrivateCommand_GetIntValue(command, &value);
359 EXPECT_EQ(result, IME_ERR_QUERY_FAILED);
360
361 OH_PrivateCommand_Destroy(command);
362 }
363
HWTEST_F(InputMethod_PrivateCommandTest,GetStrValue_ValidCommand_ShouldReturnString)364 HWTEST_F(InputMethod_PrivateCommandTest, GetStrValue_ValidCommand_ShouldReturnString)
365 {
366 char key[] = "testKey";
367 InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1);
368 EXPECT_NE(command, nullptr);
369
370 char value[] = "testValue";
371 InputMethod_ErrorCode result = OH_PrivateCommand_SetStrValue(command, value, sizeof(value) - 1);
372 EXPECT_EQ(result, IME_ERR_OK);
373
374 const char *actualValue;
375 size_t valueLength;
376 result = OH_PrivateCommand_GetStrValue(command, &actualValue, &valueLength);
377 EXPECT_EQ(result, IME_ERR_OK);
378 EXPECT_STREQ(actualValue, value);
379 EXPECT_EQ(valueLength, sizeof(value) - 1);
380
381 OH_PrivateCommand_Destroy(command);
382 }
383
HWTEST_F(InputMethod_PrivateCommandTest,GetStrValue_NullCommand_ShouldReturnError)384 HWTEST_F(InputMethod_PrivateCommandTest, GetStrValue_NullCommand_ShouldReturnError)
385 {
386 const char *value;
387 size_t valueLength;
388 InputMethod_ErrorCode result = OH_PrivateCommand_GetStrValue(nullptr, &value, &valueLength);
389 EXPECT_EQ(result, IME_ERR_NULL_POINTER);
390 }
391
HWTEST_F(InputMethod_PrivateCommandTest,GetStrValue_NullValue_ShouldReturnError)392 HWTEST_F(InputMethod_PrivateCommandTest, GetStrValue_NullValue_ShouldReturnError)
393 {
394 char key[] = "testKey";
395 InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1);
396 EXPECT_NE(command, nullptr);
397
398 size_t valueLength;
399 InputMethod_ErrorCode result = OH_PrivateCommand_GetStrValue(command, nullptr, &valueLength);
400 EXPECT_EQ(result, IME_ERR_NULL_POINTER);
401
402 OH_PrivateCommand_Destroy(command);
403 }
404
HWTEST_F(InputMethod_PrivateCommandTest,GetStrValue_NullValueLength_ShouldReturnError)405 HWTEST_F(InputMethod_PrivateCommandTest, GetStrValue_NullValueLength_ShouldReturnError)
406 {
407 char key[] = "testKey";
408 InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1);
409 EXPECT_NE(command, nullptr);
410
411 const char *value;
412 InputMethod_ErrorCode result = OH_PrivateCommand_GetStrValue(command, &value, nullptr);
413 EXPECT_EQ(result, IME_ERR_NULL_POINTER);
414
415 OH_PrivateCommand_Destroy(command);
416 }
417
HWTEST_F(InputMethod_PrivateCommandTest,GetStrValue_NonStringValue_ShouldReturnError)418 HWTEST_F(InputMethod_PrivateCommandTest, GetStrValue_NonStringValue_ShouldReturnError)
419 {
420 char key[] = "testKey";
421 InputMethod_PrivateCommand *command = OH_PrivateCommand_Create(key, sizeof(key) - 1);
422 EXPECT_NE(command, nullptr);
423
424 InputMethod_ErrorCode result = OH_PrivateCommand_SetIntValue(command, 42);
425 EXPECT_EQ(result, IME_ERR_OK);
426
427 const char *value;
428 size_t valueLength;
429 result = OH_PrivateCommand_GetStrValue(command, &value, &valueLength);
430 EXPECT_EQ(result, IME_ERR_QUERY_FAILED);
431
432 OH_PrivateCommand_Destroy(command);
433 }