1 /*
2  * Copyright (c) 2021 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 "jsi_interface_tdd_test.h"
17 #include "acelite_config.h"
18 #include "jerryscript-core.h"
19 #include "js_debugger_config.h"
20 #include "jsi_types.h"
21 #include <cstdio>
22 #include <cstring>
23 
24 namespace OHOS {
25 namespace ACELite {
26 #define TO_JERRY_VALUE(a) ((jerry_value_t)(uintptr_t)(a))
27 #define TO_JSI_VALUE(a) ((JSIValue)(uintptr_t)(a))
28 
29 static int8_t g_descValue = 0;
30 
JsiInterfaceTddTest()31 JsiInterfaceTddTest::JsiInterfaceTddTest() {}
32 
SetUp()33 void JsiInterfaceTddTest::SetUp()
34 {
35     Debugger::GetInstance().SetupJSContext();
36     jerry_init(JERRY_INIT_EMPTY);
37 }
38 
TearDown()39 void JsiInterfaceTddTest::TearDown()
40 {
41     jerry_cleanup();
42     Debugger::GetInstance().ReleaseJSContext();
43 }
44 
Function(const JSIValue thisVal,const JSIValue args[],uint8_t argsNum)45 JSIValue JsiInterfaceTddTest::Function(const JSIValue thisVal, const JSIValue args[], uint8_t argsNum)
46 {
47     (void)thisVal;
48     (void)args;
49     (void)argsNum;
50     printf("Function called\n");
51     return JSI::CreateUndefined();
52 }
53 
Setter(const JSIValue thisVal,const JSIValue args[],uint8_t argsNum)54 JSIValue JsiInterfaceTddTest::Setter(const JSIValue thisVal, const JSIValue args[], uint8_t argsNum)
55 {
56     (void)thisVal;
57     (void)argsNum;
58     double newValue = JSI::ValueToNumber(args[0]);
59     g_descValue = (int8_t)newValue;
60 
61     printf("Setter called, setting: %d\n", g_descValue);
62     return JSI::CreateUndefined();
63 }
64 
Getter(const JSIValue thisVal,const JSIValue args[],uint8_t argsNum)65 JSIValue JsiInterfaceTddTest::Getter(const JSIValue thisVal, const JSIValue args[], uint8_t argsNum)
66 {
67     (void)thisVal;
68     (void)args;
69     (void)argsNum;
70     g_descValue++;
71     printf("Getter called, returning: %d\n", g_descValue);
72     return JSI::CreateNumber(g_descValue);
73 }
74 
OnDestroy(int8_t statusCode)75 void JsiInterfaceTddTest::OnDestroy(int8_t statusCode)
76 {
77     printf("OnDestroy called, statusCode=%d\n", statusCode);
78 }
79 
OnTerminate(int8_t statusCode)80 void JsiInterfaceTddTest::OnTerminate(int8_t statusCode)
81 {
82     printf("OnTerminate called, statusCode=%d\n", statusCode);
83 }
84 
JSIInterfaceTest001()85 void JsiInterfaceTddTest::JSIInterfaceTest001()
86 {
87     TDD_CASE_BEGIN();
88     /**
89      * @tc.steps: step1. call JSI CreateObject
90      */
91     JSIValue object = JSI::CreateObject();
92     /**
93      * @tc.expected: step1. ValueIsObject return true
94      */
95     bool res = JSI::ValueIsObject(object);
96     if (res) {
97         printf("JSIInterfaceTest001 pass\n");
98     } else {
99         printf("JSIInterfaceTest001 fail\n");
100     }
101     EXPECT_TRUE(res);
102     JSI::ReleaseValue(object);
103     TDD_CASE_END();
104 }
105 
JSIInterfaceTest002()106 void JsiInterfaceTddTest::JSIInterfaceTest002()
107 {
108     TDD_CASE_BEGIN();
109     /**
110      * @tc.steps: step1. call JSI GetGlobalObject
111      */
112     JSIValue global = JSI::GetGlobalObject();
113     /**
114      * @tc.expected: step1. global is not undefined
115      */
116     if (JSI::ValueIsUndefined(global)) {
117         printf("JSIInterfaceTest002 fail\n");
118     } else {
119         printf("JSIInterfaceTest002 pass\n");
120     }
121     EXPECT_FALSE(JSI::ValueIsUndefined(global));
122     JSI::ReleaseValue(global);
123     TDD_CASE_END();
124 }
125 
JSIInterfaceTest003()126 void JsiInterfaceTddTest::JSIInterfaceTest003()
127 {
128     TDD_CASE_BEGIN();
129     /**
130      * @tc.steps: step1. call JSI GetJSHeapStatus
131      */
132     JSHeapStatus status;
133     bool res = JSI::GetJSHeapStatus(status);
134     /**
135      * @tc.expected: step1. res is true
136      */
137     if (res) {
138         printf("JSIInterfaceTest003 JSHeapStatus:totalBytes=%d allocBytes=%d peakAllocBytes=%d\n", status.totalBytes,
139                status.allocBytes, status.peakAllocBytes);
140         printf("JSIInterfaceTest003 pass\n");
141     } else {
142         printf("JSIInterfaceTest003 fail\n");
143     }
144     EXPECT_TRUE(res);
145     TDD_CASE_END();
146 }
147 
JSIInterfaceTest004()148 void JsiInterfaceTddTest::JSIInterfaceTest004()
149 {
150     TDD_CASE_BEGIN();
151     /**
152      * @tc.steps: step1. call JSI CreateArray
153      */
154     uint32_t initLength = 10;
155     JSIValue array = JSI::CreateArray(initLength);
156 
157     /**
158      * @tc.steps: step2. call JSI ValueIsArray with Array object
159      */
160     bool res1 = JSI::ValueIsArray(array);
161     JSIValue undefined = JSI::CreateUndefined();
162 
163     /**
164      * @tc.steps: step3. call JSI ValueIsArray with Undefined object
165      */
166     bool res2 = JSI::ValueIsArray(undefined);
167 
168     /**
169      * @tc.steps: step4. call JSI GetArrayLength
170      */
171     uint32_t length = JSI::GetArrayLength(array);
172     /**
173      * @tc.expected: step2-step4. res1 = true, res2 = false and length = 10
174      */
175     if (res1 && !res2 && (length == initLength)) {
176         printf("JSIInterfaceTest004 pass\n");
177     } else {
178         printf("JSIInterfaceTest004 fail\n");
179     }
180     EXPECT_TRUE(res1 && !res2 && (length == initLength));
181     JSI::ReleaseValueList(array, undefined);
182     TDD_CASE_END();
183 }
184 
JSIInterfaceTest005()185 void JsiInterfaceTddTest::JSIInterfaceTest005()
186 {
187     TDD_CASE_BEGIN();
188     /**
189      * @tc.steps: step1. call JSI CreateFunction
190      */
191     JSIValue func = JSI::CreateFunction(Function);
192 
193     /**
194      * @tc.steps: step2. call JSI ValueIsFunction with Function object
195      */
196     bool res1 = JSI::ValueIsFunction(func);
197     JSIValue undefined = JSI::CreateUndefined();
198 
199     /**
200      * @tc.steps: step3. call JSI ValueIsFunction with Undefined object
201      */
202     bool res2 = JSI::ValueIsFunction(undefined);
203 
204     /**
205      * @tc.steps: step4. call JSI CallFunction
206      */
207     JSI::CallFunction(func, undefined, nullptr, 0);
208     /**
209      * @tc.expected: step2-step4. res1 = true, res2 = false and Function was called
210      */
211     if (res1 && !res2) {
212         printf("JSIInterfaceTest005 pass\n");
213     } else {
214         printf("JSIInterfaceTest005 fail\n");
215     }
216     EXPECT_TRUE(res1 && !res2);
217     JSI::ReleaseValueList(func, undefined);
218     TDD_CASE_END();
219 }
220 
JSIInterfaceTest006()221 void JsiInterfaceTddTest::JSIInterfaceTest006()
222 {
223     TDD_CASE_BEGIN();
224     /**
225      * @tc.steps: step1. call JSI CreateFunction
226      */
227     double initNum = 123;
228     JSIValue num = JSI::CreateNumber(initNum);
229 
230     /**
231      * @tc.steps: step2. call JSI ValueIsNumber with Number object
232      */
233     bool res1 = JSI::ValueIsNumber(num);
234     JSIValue undefined = JSI::CreateUndefined();
235 
236     /**
237      * @tc.steps: step3. call JSI ValueIsNumber with Undefined object
238      */
239     bool res2 = JSI::ValueIsNumber(undefined);
240 
241     /**
242      * @tc.steps: step4. call JSI ValueToNumber
243      */
244     int32_t numValue = (int32_t)JSI::ValueToNumber(num);
245     /**
246      * @tc.expected: step2-step4. res1 = true, res2 = false and numValue = 123
247      */
248     if (res1 && !res2 && numValue == (int32_t)initNum) {
249         printf("JSIInterfaceTest006 pass\n");
250     } else {
251         printf("JSIInterfaceTest006 fail\n");
252     }
253     EXPECT_TRUE(res1 && !res2 && numValue == (int32_t)initNum);
254     JSI::ReleaseValueList(num, undefined);
255     TDD_CASE_END();
256 }
257 
JSIInterfaceTest007()258 void JsiInterfaceTddTest::JSIInterfaceTest007()
259 {
260     TDD_CASE_BEGIN();
261     /**
262      * @tc.steps: step1. call JSI CreateString
263      */
264     JSIValue str = JSI::CreateString("test");
265 
266     /**
267      * @tc.steps: step2. call JSI ValueIsString with String object
268      */
269     bool res1 = JSI::ValueIsString(str);
270     JSIValue undefined = JSI::CreateUndefined();
271 
272     /**
273      * @tc.steps: step3. call JSI ValueIsString with Undefined object
274      */
275     bool res2 = JSI::ValueIsString(undefined);
276 
277     /**
278      * @tc.steps: step4. call JSI ValueToString
279      */
280     char *strValue = JSI::ValueToString(str);
281     /**
282      * @tc.expected: step2-step4. res1 = true, res2 = false and strValue = "test"
283      */
284     if ((strValue != nullptr) && res1 && !res2 && !strcmp(strValue, "test")) {
285         printf("JSIInterfaceTest007 pass\n");
286     } else {
287         printf("JSIInterfaceTest007 fail\n");
288     }
289     EXPECT_TRUE((strValue != nullptr) && res1 && !res2 && !strcmp(strValue, "test"));
290     JSI::ReleaseValueList(str, undefined);
291     JSI::ReleaseString(strValue);
292     TDD_CASE_END();
293 }
294 
JSIInterfaceTest008()295 void JsiInterfaceTddTest::JSIInterfaceTest008()
296 {
297     TDD_CASE_BEGIN();
298     /**
299      * @tc.steps: step1. call JSI CreateBoolean
300      */
301     JSIValue jsBool = JSI::CreateBoolean(true);
302 
303     /**
304      * @tc.steps: step2. call JSI ValueIsBoolean with Boolean object
305      */
306     bool res1 = JSI::ValueIsBoolean(jsBool);
307     JSIValue undefined = JSI::CreateUndefined();
308 
309     /**
310      * @tc.steps: step3. call JSI ValueIsBoolean with Undefined object
311      */
312     bool res2 = JSI::ValueIsBoolean(undefined);
313 
314     /**
315      * @tc.steps: step4. call JSI ValueToBoolean
316      */
317     bool jsBoolValue = JSI::ValueToBoolean(jsBool);
318     /**
319      * @tc.expected: step2-step4. res1 = true, res2 = false and jsBoolValue = true
320      */
321     if (res1 && !res2 && jsBoolValue) {
322         printf("JSIInterfaceTest008 pass\n");
323     } else {
324         printf("JSIInterfaceTest008 fail\n");
325     }
326     EXPECT_TRUE(res1 && !res2 && jsBoolValue);
327     JSI::ReleaseValueList(jsBool, undefined);
328     TDD_CASE_END();
329 }
330 
JSIInterfaceTest009()331 void JsiInterfaceTddTest::JSIInterfaceTest009()
332 {
333     TDD_CASE_BEGIN();
334     /**
335      * @tc.steps: step1. call JSI CreateUndefined
336      */
337     JSIValue undef = JSI::CreateUndefined();
338 
339     /**
340      * @tc.steps: step2. call JSI ValueIsUndefined with Undefined object
341      */
342     bool res1 = JSI::ValueIsUndefined(undef);
343     JSIValue obj = JSI::CreateObject();
344 
345     /**
346      * @tc.steps: step3. call JSI ValueIsUndefined with obj
347      */
348     bool res2 = JSI::ValueIsUndefined(obj);
349     /**
350      * @tc.expected: step2-step3. res1 = true, res2 = false
351      */
352     if (res1 && !res2) {
353         printf("JSIInterfaceTest009 pass\n");
354     } else {
355         printf("JSIInterfaceTest009 fail\n");
356     }
357     EXPECT_TRUE(res1 && !res2);
358     JSI::ReleaseValueList(undef, obj);
359     TDD_CASE_END();
360 }
361 
JSIInterfaceTest010()362 void JsiInterfaceTddTest::JSIInterfaceTest010()
363 {
364     TDD_CASE_BEGIN();
365     /**
366      * @tc.steps: step1. call JSI CreateNull
367      */
368     JSIValue nullObj = JSI::CreateNull();
369 
370     /**
371      * @tc.steps: step2. call JSI ValueIsNull with Null object
372      */
373     bool res1 = JSI::ValueIsNull(nullObj);
374     JSIValue obj = JSI::CreateObject();
375 
376     /**
377      * @tc.steps: step3. call JSI ValueIsNull with obj
378      */
379     bool res2 = JSI::ValueIsNull(obj);
380     /**
381      * @tc.expected: step2-step3. res1 = true, res2 = false
382      */
383     if (res1 && !res2) {
384         printf("JSIInterfaceTest010 pass\n");
385     } else {
386         printf("JSIInterfaceTest010 fail\n");
387     }
388     EXPECT_TRUE(res1 && !res2);
389     JSI::ReleaseValueList(nullObj, obj);
390     TDD_CASE_END();
391 }
392 
393 #if IS_ENABLED(JS_FWK_SYMBOL)
JSIInterfaceTest011()394 void JsiInterfaceTddTest::JSIInterfaceTest011()
395 {
396     TDD_CASE_BEGIN();
397     /**
398      * @tc.steps: step1. call JSI CreateSymbol
399      */
400     JSIValue strValue = JSI::CreateString("Symbol description string");
401     JSIValue symbol = JSI::CreateSymbol(strValue);
402 
403     /**
404      * @tc.steps: step2. call JSI ValueIsSymbol with Symbol object
405      */
406     bool res1 = JSI::ValueIsSymbol(symbol);
407     JSIValue undef = JSI::CreateUndefined();
408 
409     /**
410      * @tc.steps: step3. call JSI ValueIsSymbol with undef
411      */
412     bool res2 = JSI::ValueIsSymbol(undef);
413     /**
414      * @tc.expected: step2-step3. res1 = true, res2 = false
415      */
416     if (res1 && !res2) {
417         printf("JSIInterfaceTest011 pass\n");
418     } else {
419         printf("JSIInterfaceTest011 fail\n");
420     }
421     EXPECT_TRUE(res1 && !res2);
422     JSI::ReleaseValueList(strValue, symbol);
423     TDD_CASE_END();
424 }
425 #endif // ENABLED(JS_FWK_SYMBOL)
426 
JSIInterfaceTest012()427 void JsiInterfaceTddTest::JSIInterfaceTest012()
428 {
429     TDD_CASE_BEGIN();
430     /**
431      * @tc.steps: step1. call JSI CreateObject
432      */
433     JSIValue obj = JSI::CreateObject();
434 
435     /**
436      * @tc.steps: step2. call JSI ValueIsObject with obj
437      */
438     bool res1 = JSI::ValueIsObject(obj);
439     JSIValue undef = JSI::CreateUndefined();
440 
441     /**
442      * @tc.steps: step3. call JSI ValueIsObject with undef
443      */
444     bool res2 = JSI::ValueIsObject(undef);
445     /**
446      * @tc.expected: step2-step3. res1 = true, res2 = false
447      */
448     if (!res1 || res2) {
449         printf("JSIInterfaceTest012 fail\n");
450     }
451     EXPECT_FALSE(!res1 || res2);
452 
453     /**
454      * @tc.steps: step4. call JSI AcquireValue
455      */
456     JSIValue acquire = JSI::AcquireValue(obj);
457     if (acquire != undef) {
458         printf("JSIInterfaceTest012 pass\n");
459     }
460     EXPECT_TRUE(acquire != undef);
461     /**
462      * @tc.steps: step5. call JSI ReleaseValue on acquire
463      */
464     JSI::ReleaseValue(acquire);
465     /**
466      * @tc.expected: step5. no exception log
467      */
468     JSI::ReleaseValueList(obj, undef);
469     TDD_CASE_END();
470 }
471 
JSIInterfaceTest013()472 void JsiInterfaceTddTest::JSIInterfaceTest013()
473 {
474     TDD_CASE_BEGIN();
475     /**
476      * @tc.steps: step1. call JSI JsonParse
477      */
478     JSIValue jsonValue = JSI::JsonParse("{\"key1\":\"value1\",\"key2\":\"value2\"}");
479 
480     /**
481      * @tc.steps: step2. call JSI ValueIsObject with obj
482      */
483     char *jsonStr = JSI::JsonStringify(jsonValue);
484     /**
485      * @tc.expected: step2. jsonStr = "{\"key1\":\"value1\",\"key2\":\"value2\"}"
486      */
487     if ((jsonStr != nullptr) && !strcmp(jsonStr, "{\"key1\":\"value1\",\"key2\":\"value2\"}")) {
488         printf("JSIInterfaceTest013 pass\n");
489     } else {
490         printf("JSIInterfaceTest013 fail\n");
491     }
492     EXPECT_TRUE((jsonStr != nullptr) && !strcmp(jsonStr, "{\"key1\":\"value1\",\"key2\":\"value2\"}"));
493     JSI::ReleaseValue(jsonValue);
494     JSI::ReleaseString(jsonStr);
495     TDD_CASE_END();
496 }
497 
JSIInterfaceTest014()498 void JsiInterfaceTddTest::JSIInterfaceTest014()
499 {
500     TDD_CASE_BEGIN();
501     /**
502      * @tc.steps: step1. call JSI SetNumberProperty
503      */
504     JSIValue obj = JSI::CreateObject();
505     double initNum = 123;
506     JSI::SetNumberProperty(obj, "num", initNum);
507 
508     /**
509      * @tc.steps: step2. call JSI GetNumberProperty
510      */
511     int32_t num = (int32_t)JSI::GetNumberProperty(obj, "num");
512     /**
513      * @tc.expected: step2. num = 123
514      */
515     if (num == (int32_t)initNum) {
516         printf("JSIInterfaceTest014 pass\n");
517     } else {
518         printf("JSIInterfaceTest014 fail\n");
519     }
520     EXPECT_TRUE(num == (int32_t)initNum);
521     JSI::ReleaseValue(obj);
522     TDD_CASE_END();
523 }
524 
JSIInterfaceTest015()525 void JsiInterfaceTddTest::JSIInterfaceTest015()
526 {
527     TDD_CASE_BEGIN();
528     /**
529      * @tc.steps: step1. call JSI SetBooleanProperty
530      */
531     JSIValue obj = JSI::CreateObject();
532     JSI::SetBooleanProperty(obj, "bool", true);
533 
534     /**
535      * @tc.steps: step2. call JSI GetBooleanProperty
536      */
537     bool boolValue = JSI::GetBooleanProperty(obj, "bool");
538     /**
539      * @tc.expected: step2. boolValue = true
540      */
541     if (boolValue) {
542         printf("JSIInterfaceTest015 pass\n");
543     } else {
544         printf("JSIInterfaceTest015 fail\n");
545     }
546     EXPECT_TRUE(boolValue);
547     JSI::ReleaseValue(obj);
548     TDD_CASE_END();
549 }
550 
JSIInterfaceTest016()551 void JsiInterfaceTddTest::JSIInterfaceTest016()
552 {
553     TDD_CASE_BEGIN();
554     /**
555      * @tc.steps: step1. call JSI SetStringProperty
556      */
557     JSIValue obj = JSI::CreateObject();
558     JSI::SetStringProperty(obj, "string", "test");
559 
560     /**
561      * @tc.steps: step2. call JSI GetStringProperty
562      */
563     char *str = JSI::GetStringProperty(obj, "string");
564     /**
565      * @tc.expected: step2. str = "test"
566      */
567     if (str != nullptr && !strcmp(str, "test")) {
568         printf("JSIInterfaceTest016 pass\n");
569     } else {
570         printf("JSIInterfaceTest016 fail\n");
571     }
572     EXPECT_TRUE(str != nullptr && !strcmp(str, "test"));
573     JSI::ReleaseValue(obj);
574     JSI::ReleaseString(str);
575     TDD_CASE_END();
576 }
577 
JSIInterfaceTest017()578 void JsiInterfaceTddTest::JSIInterfaceTest017()
579 {
580     TDD_CASE_BEGIN();
581     /**
582      * @tc.steps: step1. call JSI SetPropertyByIndex
583      */
584     uint32_t initLength = 2;
585     double initNum = 123;
586     JSIValue array = JSI::CreateArray(initLength);
587     JSIValue numValue = JSI::CreateNumber(initNum);
588     JSIValue strValue = JSI::CreateString("test");
589     JSI::SetPropertyByIndex(array, 0, numValue);
590     JSI::SetPropertyByIndex(array, 1, strValue);
591 
592     /**
593      * @tc.steps: step2. call JSI GetPropertyByIndex
594      */
595     JSIValue numValueGet = JSI::GetPropertyByIndex(array, 0);
596     JSIValue strValueGet = JSI::GetPropertyByIndex(array, 1);
597     int32_t num = (int32_t)JSI::ValueToNumber(numValueGet);
598     char *str = JSI::ValueToString(strValueGet);
599     /**
600      * @tc.expected: step2. str = "test", num = 123
601      */
602     if ((str != nullptr) && !strcmp(str, "test") && (num == (int32_t)initNum)) {
603         printf("JSIInterfaceTest017 pass\n");
604     } else {
605         printf("JSIInterfaceTest017 fail\n");
606     }
607     EXPECT_TRUE((str != nullptr) && !strcmp(str, "test") && (num == (int32_t)initNum));
608     JSI::ReleaseValueList(array, numValue, strValue, numValueGet, strValueGet);
609     JSI::ReleaseString(str);
610     TDD_CASE_END();
611 }
612 
613 #if IS_ENABLED(JS_FWK_TYPEDARRAY)
JSIInterfaceTest018()614 void JsiInterfaceTddTest::JSIInterfaceTest018()
615 {
616     TDD_CASE_BEGIN();
617     /**
618      * @tc.steps: step1. call JSI CreateArrayBuffer
619      */
620     uint8_t *ptr = nullptr;
621     size_t initLength = 2;
622     uint8_t value1 = 128;
623     uint8_t value2 = 255;
624     JSIValue buffer = JSI::CreateArrayBuffer(initLength, ptr);
625     if (!ptr) {
626         JSI::ReleaseValue(buffer);
627         printf("JSIInterfaceTest018 fail\n");
628         EXPECT_TRUE(false);
629         return;
630     }
631     ptr[0] = value1;
632     ptr[1] = value2;
633     JSIValue undef = JSI::CreateUndefined();
634 
635     /**
636      * @tc.steps: step2. call JSI ValueIsArrayBuffer
637      */
638     bool res1 = JSI::ValueIsArrayBuffer(buffer);
639 
640     /**
641      * @tc.steps: step3. call JSI ValueIsArrayBuffer with undef
642      */
643     bool res2 = JSI::ValueIsArrayBuffer(undef);
644 
645     /**
646      * @tc.steps: step4. call JSI GetArrayBufferInfo
647      */
648     size_t length;
649     uint8_t *ptr2 = JSI::GetArrayBufferInfo(buffer, length);
650     /**
651      * @tc.expected: step2-step4. res1 = true, res2 = false, length = 2
652      */
653     if ((ptr2 != nullptr) && res1 && !res2 && (length == initLength) && (ptr2[0] == value1) && (ptr2[1] == value2)) {
654         printf("JSIInterfaceTest018 pass\n");
655     } else {
656         printf("JSIInterfaceTest018 fail\n");
657     }
658     EXPECT_TRUE((ptr2 != nullptr) && res1 && !res2 && (length == initLength) && (ptr2[0] == value1) &&
659                 (ptr2[1] == value2));
660     JSI::ReleaseValueList(buffer, undef);
661     TDD_CASE_END();
662 }
663 
JSIInterfaceTest019()664 void JsiInterfaceTddTest::JSIInterfaceTest019()
665 {
666     TDD_CASE_BEGIN();
667     /**
668      * @tc.steps: step1. create an ArrayBuffer
669      */
670     uint8_t *ptr = nullptr;
671     size_t initLength = 4;
672     uint8_t value1 = 128;
673     uint8_t value2 = 255;
674     size_t initOffset = 2;
675     JSIValue buffer = JSI::CreateArrayBuffer(initLength, ptr);
676     if (!ptr) {
677         JSI::ReleaseValue(buffer);
678         printf("JSIInterfaceTest019 fail\n");
679         EXPECT_TRUE(false);
680         return;
681     }
682     ptr[0] = 0;
683     ptr[1] = 0;
684     ptr[initOffset] = value1;
685     ptr[initOffset + 1] = value2;
686 
687     /**
688      * @tc.steps: step2. create an TypedArray
689      */
690     size_t byteLength = 2;
691     JSIValue typedArray = JSI::CreateTypedArray(TypedArrayType::JSI_UINT8_ARRAY, byteLength, buffer, initOffset);
692     JSI::ReleaseValue(buffer);
693     JSIValue undef = JSI::CreateUndefined();
694 
695     /**
696      * @tc.steps: step3. call JSI ValueIsTypedArray
697      */
698     bool res1 = JSI::ValueIsTypedArray(typedArray);
699 
700     /**
701      * @tc.steps: step4. call JSI ValueIsTypedArray with undef
702      */
703     bool res2 = JSI::ValueIsTypedArray(undef);
704 
705     /**
706      * @tc.steps: step5. call JSI GetTypedArrayInfo
707      */
708     TypedArrayType type;
709     size_t length, offset;
710     JSIValue arrayBuffer;
711     uint8_t *ptr2 = JSI::GetTypedArrayInfo(typedArray, type, length, arrayBuffer, offset);
712     /**
713      * @tc.expected: step3-step5. res1 = true, res2 = false, typedArray params are correct
714      */
715     if (res1 && !res2 && (type == TypedArrayType::JSI_UINT8_ARRAY) && (length == byteLength) &&
716         (offset == initOffset) && ptr2 != nullptr && (ptr2[offset] == value1) && (ptr2[offset + 1] == value2)) {
717         printf("JSIInterfaceTest019 pass\n");
718     } else {
719         printf("JSIInterfaceTest019 fail\n");
720     }
721     EXPECT_TRUE(res1 && !res2 && (type == TypedArrayType::JSI_UINT8_ARRAY) && (length == byteLength) &&
722                 (offset == initOffset) && ptr2 != nullptr && (ptr2[offset] == value1) && (ptr2[offset + 1] == value2));
723 
724     JSI::ReleaseValueList(typedArray, undef, arrayBuffer);
725     TDD_CASE_END();
726 }
727 #endif // ENABLED(JS_FWK_TYPEDARRAY)
728 
JSIInterfaceTest020()729 void JsiInterfaceTddTest::JSIInterfaceTest020()
730 {
731     TDD_CASE_BEGIN();
732     /**
733      * @tc.steps: step1. initial JSPropertyDescriptor object
734      */
735     JSPropertyDescriptor desc;
736     const char *propName = "propTest";
737     desc.getter = Getter;
738     desc.setter = Setter;
739 
740     /**
741      * @tc.steps: step2. call JSI DefineNamedProperty
742      */
743     JSIValue globalObj = JSI::GetGlobalObject();
744     bool res = JSI::DefineNamedProperty(globalObj, propName, desc);
745     JSI::ReleaseValue(globalObj);
746 
747     /**
748      * @tc.steps: step3. run an example js code to use the getter/setter
749      */
750     const char *jerrySrc = "this.propTest; this.propTest; this.propTest = 4; this.propTest";
751     jerry_value_t evalResult =
752         jerry_eval(reinterpret_cast<const jerry_char_t *>(jerrySrc), strlen(jerrySrc), JERRY_PARSE_NO_OPTS);
753     int8_t num = jerry_get_number_value(evalResult);
754     uint8_t expectedNum = 5;
755     /**
756      * @tc.expected: step3. res = true, num = 5
757      */
758     if (res && (num == expectedNum)) {
759         printf("JSIInterfaceTest020 pass\n");
760     } else {
761         printf("JSIInterfaceTest020 fail\n");
762     }
763     EXPECT_TRUE(res && (num == expectedNum));
764     jerry_release_value(evalResult);
765     TDD_CASE_END();
766 }
767 
JSIInterfaceTest021()768 void JsiInterfaceTddTest::JSIInterfaceTest021()
769 {
770     TDD_CASE_BEGIN();
771     /**
772      * @tc.steps: step1. create an object and set properties
773      */
774     JSIValue object = JSI::CreateObject();
775     const char *key1 = "key1";
776     const char *key2 = "key2";
777     JSI::SetNumberProperty(object, key1, 0);
778     JSI::SetNumberProperty(object, key2, 1);
779 
780     /**
781      * @tc.steps: step2. call JSI GetObjectKeys
782      */
783     JSIValue keys = JSI::GetObjectKeys(object);
784     JSIValue keyValue1 = JSI::GetPropertyByIndex(keys, 0);
785     JSIValue keyValue2 = JSI::GetPropertyByIndex(keys, 1);
786     char *keyAcquired1 = JSI::ValueToString(keyValue1);
787     char *keyAcquired2 = JSI::ValueToString(keyValue2);
788     int32_t num1 = JSI::GetNumberProperty(object, keyAcquired1);
789     int32_t num2 = JSI::GetNumberProperty(object, keyAcquired2);
790     /**
791      * @tc.expected: step2. keyAcquired1 = key1, num1 = 0, keyAcquired2 = key2, num1 = 1
792      */
793     if (keyAcquired1 != nullptr && !strcmp(keyAcquired1, key1) && num1 == 0 && keyAcquired2 != nullptr &&
794         !strcmp(keyAcquired2, key2) && num2 == 1) {
795         printf("JSIInterfaceTest021 pass\n");
796     } else {
797         printf("JSIInterfaceTest021 fail\n");
798     }
799     EXPECT_TRUE(keyAcquired1 != nullptr && !strcmp(keyAcquired1, key1) && num1 == 0 && keyAcquired2 != nullptr &&
800                 !strcmp(keyAcquired2, key2) && num2 == 1);
801 
802     JSI::ReleaseValueList(object, keys, keyValue1, keyValue2);
803     JSI::ReleaseString(keyAcquired1);
804     JSI::ReleaseString(keyAcquired2);
805     TDD_CASE_END();
806 }
807 
JSIInterfaceTest022()808 void JsiInterfaceTddTest::JSIInterfaceTest022()
809 {
810     TDD_CASE_BEGIN();
811     /**
812      * @tc.steps: step1. create an object and call JSI SetOnDestroy
813      */
814     JSIValue object = JSI::CreateObject();
815     JSI::SetOnDestroy(object, OnDestroy);
816 
817     /**
818      * @tc.steps: step2. get destroy callback from object
819      */
820     const char *name = "onDestroyHandler";
821     JSIValue funObj = JSI::GetNamedProperty(object, name);
822     void *nativePtr = nullptr;
823     jerry_value_t jObj = (jerry_value_t)(uintptr_t)(funObj);
824     bool exist = jerry_get_object_native_pointer(jObj, &nativePtr, nullptr);
825     /**
826      * @tc.expected: step2. invoke callback and the OnDestroy function will be called
827      */
828     if (!exist || (nativePtr == nullptr)) {
829         printf("JSIInterfaceTest022 fail\n");
830     } else {
831         JsiCallback callback = reinterpret_cast<JsiCallback>(nativePtr);
832         (*callback)(JSI_ERR_OK);
833         printf("JSIInterfaceTest022 pass\n");
834     }
835     EXPECT_TRUE(nativePtr != nullptr);
836     jerry_delete_object_native_pointer(jObj, nullptr);
837     JSI::ReleaseValueList(object, funObj);
838     TDD_CASE_END();
839 }
840 
JSIInterfaceTest023()841 void JsiInterfaceTddTest::JSIInterfaceTest023()
842 {
843     TDD_CASE_BEGIN();
844     /**
845      * @tc.steps: step1. create an object and call JSI SetOnTerminate
846      */
847     JSIValue object = JSI::CreateObject();
848     JSI::SetOnTerminate(object, OnTerminate);
849 
850     /**
851      * @tc.steps: step2. get terminate callback from object
852      */
853     const char *name = "onTerminateHandler";
854     JSIValue funObj = JSI::GetNamedProperty(object, name);
855     void *nativePtr = nullptr;
856     jerry_value_t jObj = (jerry_value_t)(uintptr_t)(funObj);
857     bool exist = jerry_get_object_native_pointer(jObj, &nativePtr, nullptr);
858     /**
859      * @tc.expected: step2. invoke callback and the OnTerminate function will be called
860      */
861     if (!exist || (nativePtr == nullptr)) {
862         printf("JSIInterfaceTest023 fail\n");
863     } else {
864         JsiCallback callback = reinterpret_cast<JsiCallback>(nativePtr);
865         (*callback)(JSI_ERR_OK);
866         printf("JSIInterfaceTest023 pass\n");
867     }
868     EXPECT_TRUE(nativePtr != nullptr);
869     jerry_delete_object_native_pointer(jObj, nullptr);
870     JSI::ReleaseValueList(object, funObj);
871     TDD_CASE_END();
872 }
873 
JSIInterfaceTest024()874 void JsiInterfaceTddTest::JSIInterfaceTest024()
875 {
876     TDD_CASE_BEGIN();
877     /**
878      * @tc.steps: step1. call JSI CreateString
879      */
880     JSIValue str = JSI::CreateString("test");
881 
882     /**
883      * @tc.steps: step2. call JSI ValueIsString with String object
884      */
885     bool res1 = JSI::ValueIsString(str);
886     JSIValue undefined = JSI::CreateUndefined();
887 
888     /**
889      * @tc.steps: step3. call JSI ValueIsString with Undefined object
890      */
891     bool res2 = JSI::ValueIsString(undefined);
892 
893     /**
894      * @tc.steps: step4. call JSI JSIValueToString
895      */
896     char *strValue = JSI::JSIValueToString(str);
897     /**
898      * @tc.expected: step2-step4. res1 = true, res2 = false and strValue = "test"
899      */
900     if ((strValue != nullptr) && res1 && !res2 && !strcmp(strValue, "test")) {
901         printf("JSIInterfaceTest024 pass\n");
902     } else {
903         printf("JSIInterfaceTest024 fail\n");
904     }
905     EXPECT_TRUE((strValue != nullptr) && res1 && !res2 && !strcmp(strValue, "test"));
906     JSI::ReleaseValueList(str, undefined);
907     JSI::ReleaseString(strValue);
908     TDD_CASE_END();
909 }
910 
JSIInterfaceTest025()911 void JsiInterfaceTddTest::JSIInterfaceTest025()
912 {
913     TDD_CASE_BEGIN();
914     /**
915      * @tc.steps: step1. call JSI CreateString
916      */
917     double initNum = 123;
918     JSIValue num = JSI::CreateNumber(initNum);
919 
920     /**
921      * @tc.steps: step2. call JSI ValueIsNumber with Number object
922      */
923     bool res1 = JSI::ValueIsNumber(num);
924     JSIValue undefined = JSI::CreateUndefined();
925 
926     /**
927      * @tc.steps: step3. call JSI ValueIsNumber with Undefined object
928      */
929     bool res2 = JSI::ValueIsNumber(undefined);
930 
931     /**
932      * @tc.steps: step4. call JSI JSIValueToString
933      */
934     char *strValue = JSI::JSIValueToString(num);
935     /**
936      * @tc.expected: step2-step4. res1 = true, res2 = false and strValue = "test"
937      */
938     if ((strValue != nullptr) && res1 && !res2 && !strcmp(strValue, "123")) {
939         printf("JSIInterfaceTest025 pass\n");
940     } else {
941         printf("JSIInterfaceTest025 fail\n");
942     }
943     EXPECT_TRUE((strValue != nullptr) && res1 && !res2 && !strcmp(strValue, "123"));
944     JSI::ReleaseValueList(num, undefined);
945     JSI::ReleaseString(strValue);
946     TDD_CASE_END();
947 }
948 
JSIInterfaceTest026()949 void JsiInterfaceTddTest::JSIInterfaceTest026()
950 {
951     TDD_CASE_BEGIN();
952     /**
953      * @tc.steps: step1. call JSI CreateBoolean
954      */
955     JSIValue jsBool = JSI::CreateBoolean(true);
956 
957     /**
958      * @tc.steps: step2. call JSI ValueIsBoolean with Boolean object
959      */
960     bool res1 = JSI::ValueIsBoolean(jsBool);
961     JSIValue undefined = JSI::CreateUndefined();
962 
963     /**
964      * @tc.steps: step3. call JSI ValueIsBoolean with Undefined object
965      */
966     bool res2 = JSI::ValueIsBoolean(undefined);
967 
968     /**
969      * @tc.steps: step4. call JSI JSIValueToString
970      */
971     char *strValue = JSI::JSIValueToString(jsBool);
972     /**
973      * @tc.expected: step2-step4. res1 = true, res2 = false and strValue = "test"
974      */
975     if ((strValue != nullptr) && res1 && !res2 && !strcmp(strValue, "true")) {
976         printf("JSIInterfaceTest026 pass\n");
977     } else {
978         printf("JSIInterfaceTest026 fail\n");
979     }
980     EXPECT_TRUE((strValue != nullptr) && res1 && !res2 && !strcmp(strValue, "true"));
981     JSI::ReleaseValueList(jsBool, undefined);
982     JSI::ReleaseString(strValue);
983     TDD_CASE_END();
984 }
985 
JSIInterfaceTest027()986 void JsiInterfaceTddTest::JSIInterfaceTest027()
987 {
988     TDD_CASE_BEGIN();
989     /**
990      * @tc.steps: step1. call JSI CreateObject
991      */
992     JSIValue object = JSI::CreateObject();
993     /**
994      * @tc.expected: step2. ValueIsObject return true
995      */
996     bool res1 = JSI::ValueIsObject(object);
997     JSIValue undefined = JSI::CreateUndefined();
998 
999     /**
1000      * @tc.steps: step3. call JSI ValueIsObject with Undefined object
1001      */
1002     bool res2 = JSI::ValueIsObject(undefined);
1003 
1004     /**
1005      * @tc.steps: step4. call JSI JSIValueToString
1006      */
1007     char *strValue = JSI::JSIValueToString(object);
1008     /**
1009      * @tc.expected: step2-step4. res1 = true, res2 = false and strValue = "test"
1010      */
1011     if ((strValue != nullptr) && res1 && !res2 && !strcmp(strValue, "[object Object]")) {
1012         printf("JSIInterfaceTest027 pass\n");
1013     } else {
1014         printf("JSIInterfaceTest027 fail\n");
1015     }
1016     EXPECT_TRUE((strValue != nullptr) && res1 && !res2 && !strcmp(strValue, "[object Object]"));
1017     JSI::ReleaseValueList(object, undefined);
1018     JSI::ReleaseString(strValue);
1019     TDD_CASE_END();
1020 }
1021 
JSIInterfaceTest028()1022 void JsiInterfaceTddTest::JSIInterfaceTest028()
1023 {
1024     TDD_CASE_BEGIN();
1025     /**
1026      * @tc.steps: step1. call JSI CreateArray
1027      */
1028     uint32_t initLength = 10;
1029     JSIValue array = JSI::CreateArray(initLength);
1030 
1031     /**
1032      * @tc.steps: step2. call JSI ValueIsArray with Array object
1033      */
1034     bool res1 = JSI::ValueIsArray(array);
1035     JSIValue undefined = JSI::CreateUndefined();
1036 
1037     /**
1038      * @tc.steps: step3. call JSI ValueIsArray with Undefined object
1039      */
1040     bool res2 = JSI::ValueIsArray(undefined);
1041 
1042     /**
1043      * @tc.steps: step4. call JSI GetArrayLength
1044      */
1045     uint32_t length = JSI::GetArrayLength(array);
1046 
1047     /**
1048      * @tc.steps: step5. call JSI JSIValueToString
1049      */
1050     char *strValue = JSI::JSIValueToString(array);
1051     /**
1052      * @tc.expected: step2-step5. res1 = true, res2 = false and strValue = "test"
1053      */
1054     if ((strValue != nullptr) && res1 && !res2 && (length == initLength) && !strcmp(strValue, ",,,,,,,,,")) {
1055         printf("JSIInterfaceTest028 pass\n");
1056     } else {
1057         printf("JSIInterfaceTest028 fail\n");
1058     }
1059     EXPECT_TRUE((strValue != nullptr) && res1 && !res2 && (length == initLength) && !strcmp(strValue, ",,,,,,,,,"));
1060     JSI::ReleaseValueList(array, undefined);
1061     JSI::ReleaseString(strValue);
1062     TDD_CASE_END();
1063 }
1064 
JSIInterfaceTest029()1065 void JsiInterfaceTddTest::JSIInterfaceTest029()
1066 {
1067     TDD_CASE_BEGIN();
1068     /**
1069      * @tc.steps: step1. call JSI CreateFunction
1070      */
1071     JSIValue func = JSI::CreateFunction(Function);
1072 
1073     /**
1074      * @tc.steps: step2. call JSI ValueIsFunction with Function object
1075      */
1076     bool res1 = JSI::ValueIsFunction(func);
1077     JSIValue undefined = JSI::CreateUndefined();
1078 
1079     /**
1080      * @tc.steps: step3. call JSI ValueIsFunction with Undefined object
1081      */
1082     bool res2 = JSI::ValueIsFunction(undefined);
1083 
1084     /**
1085      * @tc.steps: step4. call JSI JSIValueToString
1086      */
1087     char *strValue = JSI::JSIValueToString(func);
1088     /**
1089      * @tc.expected: step2-step4. res1 = true, res2 = false and strValue = "test"
1090      */
1091     if ((strValue != nullptr) && res1 && !res2 && !strcmp(strValue, "function(){/* ecmascript */}")) {
1092         printf("JSIInterfaceTest029 pass\n");
1093     } else {
1094         printf("JSIInterfaceTest029 fail\n");
1095     }
1096     EXPECT_TRUE((strValue != nullptr) && res1 && !res2 && !strcmp(strValue, "function(){/* ecmascript */}"));
1097     JSI::ReleaseValueList(func, undefined);
1098     JSI::ReleaseString(strValue);
1099     TDD_CASE_END();
1100 }
1101 
JSIInterfaceTest030()1102 void JsiInterfaceTddTest::JSIInterfaceTest030()
1103 {
1104     TDD_CASE_BEGIN();
1105     /**
1106      * @tc.steps: step1. try to create one error with null msg
1107      */
1108     JSIValue errRef = JSI::CreateErrorWithCode(JSI_ERR_CODE_PERMISSION_DENIED, nullptr);
1109     /**
1110      * @tc.expected: step1. errRef = undefined
1111      */
1112     JSIValue undefinedValue = JSI::CreateUndefined();
1113     if (errRef == undefinedValue) {
1114         printf("JSIInterfaceTest030 pass\n");
1115     } else {
1116         printf("JSIInterfaceTest030 fail\n");
1117     }
1118     EXPECT_TRUE(errRef == undefinedValue);
1119     JSI::ReleaseValue(errRef);
1120     TDD_CASE_END();
1121 }
1122 
JSIInterfaceTest031()1123 void JsiInterfaceTddTest::JSIInterfaceTest031()
1124 {
1125     TDD_CASE_BEGIN();
1126     /**
1127      * @tc.steps: step1. try to create one error with valid msg
1128      */
1129     JSIValue errRef = JSI::CreateErrorWithCode(0, "this is error message");
1130     /**
1131      * @tc.expected: step1. errRef is valid
1132      */
1133     if (JSI::ValueIsError(errRef)) {
1134         printf("JSIInterfaceTest031 pass\n");
1135     } else {
1136         printf("JSIInterfaceTest031 fail\n");
1137     }
1138     EXPECT_TRUE(JSI::ValueIsError(errRef));
1139     JSI::ReleaseValue(errRef);
1140     TDD_CASE_END();
1141 }
1142 
JSIInterfaceTest032()1143 void JsiInterfaceTddTest::JSIInterfaceTest032()
1144 {
1145     TDD_CASE_BEGIN();
1146     /**
1147      * @tc.steps: step1. try to create one error with very long msg which is over 256
1148      */
1149     const char *veryLongMessage =
1150         "this is one very long test message which is used to check if the length can be verified by the function, this "
1151         "is one very long test message which is used to check if the length can be verified by the function, this is "
1152         "one very long test message which is used to check if the length can be verified by the function.";
1153     JSIValue errRef = JSI::CreateErrorWithCode(JSI_ERR_CODE_NOT_SUPPORTED, veryLongMessage);
1154     /**
1155      * @tc.expected: step1. errRef = undefined
1156      */
1157     JSIValue undefValue = JSI::CreateUndefined();
1158     if (errRef == undefValue) {
1159         printf("JSIInterfaceTest032 pass\n");
1160     } else {
1161         printf("JSIInterfaceTest032 fail\n");
1162     }
1163     EXPECT_TRUE(errRef == undefValue);
1164     JSI::ReleaseValue(errRef);
1165     TDD_CASE_END();
1166 }
1167 
JSIInterfaceTest033()1168 void JsiInterfaceTddTest::JSIInterfaceTest033()
1169 {
1170     TDD_CASE_BEGIN();
1171     /**
1172      * @tc.steps: step1. try to create one error with empty string
1173      */
1174     JSIValue errResult = JSI::CreateErrorWithCode(JSI_ERR_CODE_PARAM_CHECK_FAILED, "");
1175     /**
1176      * @tc.expected: step1. errRef is valid
1177      */
1178     if (JSI::ValueIsError(errResult)) {
1179         printf("JSIInterfaceTest033 pass\n");
1180     } else {
1181         printf("JSIInterfaceTest033 fail\n");
1182     }
1183     EXPECT_TRUE(JSI::ValueIsError(errResult));
1184     JSI::ReleaseValue(errResult);
1185     TDD_CASE_END();
1186 }
1187 
JSIInterfaceTest034()1188 void JsiInterfaceTddTest::JSIInterfaceTest034()
1189 {
1190     TDD_CASE_BEGIN();
1191     /**
1192      * @tc.steps: step1. try to create one error with extra data
1193      */
1194     JSIValue extraData = JSI::CreateObject();
1195     JSIValue errRef = JSI::CreateErrorWithCode(JSI_ERR_CODE_NOT_SUPPORTED, "error info", extraData);
1196     /**
1197      * @tc.expected: step1. errRef is valid
1198      */
1199     bool result = JSI::ValueIsError(errRef);
1200     if (result) {
1201         printf("JSIInterfaceTest034 pass\n");
1202     } else {
1203         printf("JSIInterfaceTest034 fail\n");
1204     }
1205     EXPECT_TRUE(result);
1206     JSI::ReleaseValue(extraData);
1207     JSI::ReleaseValue(errRef);
1208     TDD_CASE_END();
1209 }
1210 
JSIInterfaceTest035()1211 void JsiInterfaceTddTest::JSIInterfaceTest035()
1212 {
1213     TDD_CASE_BEGIN();
1214     /**
1215      * @tc.steps: step1. try to create one error with extra data
1216      */
1217     JSIValue extraData = JSI::CreateObject();
1218     JSI::SetStringProperty(extraData, "info", "message");
1219     const char *errMsg = "method is not support";
1220     JSIValue errRef = JSI::CreateErrorWithCode(JSI_ERR_CODE_NOT_SUPPORTED, errMsg, extraData);
1221     /**
1222      * @tc.expected: step1. errRef is valid
1223      */
1224     JSIValue resultExtracted = TO_JSI_VALUE(jerry_get_value_from_error(TO_JERRY_VALUE(errRef), false));
1225     bool result = JSI::ValueIsObject(resultExtracted);
1226     if (result) {
1227         result = (JSI::GetNumberProperty(resultExtracted, "code") == JSI_ERR_CODE_NOT_SUPPORTED);
1228     }
1229     if (result) {
1230         printf("JSIInterfaceTest035 pass\n");
1231     } else {
1232         printf("JSIInterfaceTest035 fail\n");
1233     }
1234     EXPECT_TRUE(result);
1235     JSI::ReleaseValue(resultExtracted);
1236     JSI::ReleaseValue(extraData);
1237     JSI::ReleaseValue(errRef);
1238     TDD_CASE_END();
1239 }
1240 
JSIInterfaceTest036()1241 void JsiInterfaceTddTest::JSIInterfaceTest036()
1242 {
1243     TDD_CASE_BEGIN();
1244     /**
1245      * @tc.steps: step1. try to create one error with extra data
1246      */
1247     JSIValue extraData = JSI::CreateObject();
1248     JSI::SetStringProperty(extraData, "test", "info");
1249     const char *errMsgContent = "check failed";
1250     JSIValue errRef = JSI::CreateErrorWithCode(JSI_ERR_CODE_PARAM_CHECK_FAILED, errMsgContent, extraData);
1251 
1252     /**
1253      * @tc.expected: step1. errRef is valid
1254      */
1255     JSIValue extractedValue = TO_JSI_VALUE(jerry_get_value_from_error(TO_JERRY_VALUE(errRef), false));
1256     bool msgCheckResult = JSI::ValueIsObject(extractedValue);
1257     if (msgCheckResult) {
1258         char *messageStr = JSI::GetStringProperty(extractedValue, "message");
1259         msgCheckResult = (messageStr != nullptr && strcmp(messageStr, errMsgContent) == 0);
1260     }
1261     if (msgCheckResult) {
1262         printf("JSIInterfaceTest036 pass\n");
1263     } else {
1264         printf("JSIInterfaceTest036 fail\n");
1265     }
1266     EXPECT_TRUE(msgCheckResult);
1267     // clean up
1268     JSI::ReleaseValue(extractedValue);
1269     JSI::ReleaseValue(extraData);
1270     JSI::ReleaseValue(errRef);
1271     TDD_CASE_END();
1272 }
1273 
JSIInterfaceTest037()1274 void JsiInterfaceTddTest::JSIInterfaceTest037()
1275 {
1276     TDD_CASE_BEGIN();
1277     /**
1278      * @tc.steps: step1. try to create one error with extra data
1279      */
1280     JSIValue extraDataObject = JSI::CreateObject();
1281     const char *propertyKey = "dataKey";
1282     JSI::SetStringProperty(extraDataObject, propertyKey, "result");
1283     const char *notSupportedMsg = "not supported";
1284     JSIValue errRefToBeCheck = JSI::CreateErrorWithCode(JSI_ERR_CODE_NOT_SUPPORTED, notSupportedMsg, extraDataObject);
1285 
1286     /**
1287      * @tc.expected: step1. errRef is valid, check data property
1288      */
1289     JSIValue extractV = TO_JSI_VALUE(jerry_get_value_from_error(TO_JERRY_VALUE(errRefToBeCheck), false));
1290     bool dataCheckResult = JSI::ValueIsObject(extractV);
1291     if (dataCheckResult) {
1292         JSIValue dataPropertyValue = JSI::GetNamedProperty(extractV, "data");
1293         dataCheckResult = JSI::ValueIsObject(dataPropertyValue);
1294         char *dataValue = JSI::GetStringProperty(dataPropertyValue, propertyKey);
1295         if (dataValue != nullptr && strcmp(dataValue, propertyKey) == 0) {
1296             dataCheckResult = true;
1297         }
1298         JSI::ReleaseValue(dataPropertyValue);
1299     }
1300     if (dataCheckResult) {
1301         printf("JSIInterfaceTest037 pass\n");
1302     } else {
1303         printf("JSIInterfaceTest037 fail\n");
1304     }
1305     EXPECT_TRUE(dataCheckResult);
1306     // clean up
1307     JSI::ReleaseValue(extractV);
1308     JSI::ReleaseValue(extraDataObject);
1309     JSI::ReleaseValue(errRefToBeCheck);
1310     TDD_CASE_END();
1311 }
1312 
JSIInterfaceTest038()1313 void JsiInterfaceTddTest::JSIInterfaceTest038()
1314 {
1315     TDD_CASE_BEGIN();
1316     /**
1317      * @tc.steps: step1. try to create one error with invalid extra data
1318      */
1319     JSIValue functionValue = JSI::CreateFunction(Function); // not obj type
1320     const char *errorInfo = "not supported";
1321     JSIValue errResult = JSI::CreateErrorWithCode(JSI_ERR_CODE_NOT_SUPPORTED, errorInfo, functionValue);
1322 
1323     /**
1324      * @tc.expected: step1. errRef is valid, check data property
1325      */
1326     JSIValue extractedVal = TO_JSI_VALUE(jerry_get_value_from_error(TO_JERRY_VALUE(errResult), false));
1327     bool dataCheckRes = JSI::ValueIsObject(extractedVal);
1328     if (dataCheckRes) {
1329         JSIValue dataPropertyValue = JSI::GetNamedProperty(extractedVal, "data");
1330         dataCheckRes = JSI::ValueIsUndefined(dataPropertyValue);
1331         JSI::ReleaseValue(dataPropertyValue);
1332     }
1333     if (!dataCheckRes) {
1334         printf("JSIInterfaceTest038 pass\n");
1335     } else {
1336         printf("JSIInterfaceTest038 fail\n");
1337     }
1338     EXPECT_FALSE(dataCheckRes);
1339     // clean up
1340     JSI::ReleaseValue(extractedVal);
1341     JSI::ReleaseValue(functionValue);
1342     JSI::ReleaseValue(errResult);
1343     TDD_CASE_END();
1344 }
1345 
RunTests()1346 void JsiInterfaceTddTest::RunTests()
1347 {
1348     JSIInterfaceTest001();
1349     JSIInterfaceTest002();
1350     JSIInterfaceTest003();
1351     JSIInterfaceTest004();
1352     JSIInterfaceTest005();
1353     JSIInterfaceTest006();
1354     JSIInterfaceTest007();
1355     JSIInterfaceTest008();
1356     JSIInterfaceTest009();
1357     JSIInterfaceTest010();
1358 #if IS_ENABLED(JS_FWK_SYMBOL)
1359     JSIInterfaceTest011();
1360 #endif // ENABLED(JS_FWK_SYMBOL)
1361     JSIInterfaceTest012();
1362     JSIInterfaceTest013();
1363     JSIInterfaceTest014();
1364     JSIInterfaceTest015();
1365     JSIInterfaceTest016();
1366     JSIInterfaceTest017();
1367 #if IS_ENABLED(JS_FWK_TYPEDARRAY)
1368     JSIInterfaceTest018();
1369     JSIInterfaceTest019();
1370 #endif // ENABLED(JS_FWK_TYPEDARRAY)
1371     JSIInterfaceTest020();
1372     JSIInterfaceTest021();
1373     JSIInterfaceTest022();
1374     JSIInterfaceTest023();
1375     JSIInterfaceTest024();
1376     JSIInterfaceTest025();
1377     JSIInterfaceTest026();
1378     JSIInterfaceTest027();
1379     JSIInterfaceTest028();
1380     JSIInterfaceTest029();
1381     JSIInterfaceTest030();
1382     JSIInterfaceTest031();
1383     JSIInterfaceTest032();
1384     JSIInterfaceTest033();
1385     JSIInterfaceTest034();
1386     JSIInterfaceTest035();
1387     JSIInterfaceTest036();
1388     JSIInterfaceTest037();
1389     JSIInterfaceTest038();
1390 }
1391 
1392 #ifdef TDD_ASSERTIONS
1393 /**
1394  * @tc.name: JSIInterfaceTest001
1395  * @tc.desc: Verify JSI interface CreateObject.
1396  */
1397 HWTEST_F(JsiInterfaceTddTest, test001, TestSize.Level1)
1398 {
1399     JsiInterfaceTddTest::JSIInterfaceTest001();
1400 }
1401 
1402 /**
1403  * @tc.name: JSIInterfaceTest002
1404  * @tc.desc: Verify JSI interface GetGlobalObject.
1405  */
1406 HWTEST_F(JsiInterfaceTddTest, test002, TestSize.Level1)
1407 {
1408     JsiInterfaceTddTest::JSIInterfaceTest002();
1409 }
1410 
1411 /**
1412  * @tc.name: JSIInterfaceTest003
1413  * @tc.desc: Verify JSI interface GetJSHeapStatus.
1414  */
1415 HWTEST_F(JsiInterfaceTddTest, test003, TestSize.Level1)
1416 {
1417     JsiInterfaceTddTest::JSIInterfaceTest003();
1418 }
1419 
1420 /**
1421  * @tc.name: JSIInterfaceTest004
1422  * @tc.desc: Verify JSI interface related to Array object.
1423  */
1424 HWTEST_F(JsiInterfaceTddTest, test004, TestSize.Level1)
1425 {
1426     JsiInterfaceTddTest::JSIInterfaceTest004();
1427 }
1428 
1429 /**
1430  * @tc.name: JSIInterfaceTest005
1431  * @tc.desc: Verify JSI interface related to Function object.
1432  */
1433 HWTEST_F(JsiInterfaceTddTest, test005, TestSize.Level1)
1434 {
1435     JsiInterfaceTddTest::JSIInterfaceTest005();
1436 }
1437 
1438 /**
1439  * @tc.name: JSIInterfaceTest006
1440  * @tc.desc: Verify JSI interface related to Number object.
1441  */
1442 HWTEST_F(JsiInterfaceTddTest, test006, TestSize.Level1)
1443 {
1444     JsiInterfaceTddTest::JSIInterfaceTest006();
1445 }
1446 
1447 /**
1448  * @tc.name: JSIInterfaceTest007
1449  * @tc.desc: Verify JSI interface related to String object.
1450  */
1451 HWTEST_F(JsiInterfaceTddTest, test007, TestSize.Level1)
1452 {
1453     JsiInterfaceTddTest::JSIInterfaceTest007();
1454 }
1455 
1456 /**
1457  * @tc.name: JSIInterfaceTest008
1458  * @tc.desc: Verify JSI interface related to Boolean object.
1459  */
1460 HWTEST_F(JsiInterfaceTddTest, test008, TestSize.Level1)
1461 {
1462     JsiInterfaceTddTest::JSIInterfaceTest008();
1463 }
1464 
1465 /**
1466  * @tc.name: JSIInterfaceTest009
1467  * @tc.desc: Verify JSI interface related to Undefined object.
1468  */
1469 HWTEST_F(JsiInterfaceTddTest, test009, TestSize.Level1)
1470 {
1471     JsiInterfaceTddTest::JSIInterfaceTest009();
1472 }
1473 
1474 /**
1475  * @tc.name: JSIInterfaceTest010
1476  * @tc.desc: Verify JSI interface related to Null object.
1477  */
1478 HWTEST_F(JsiInterfaceTddTest, test010, TestSize.Level1)
1479 {
1480     JsiInterfaceTddTest::JSIInterfaceTest010();
1481 }
1482 
1483 #if IS_ENABLED(JS_FWK_SYMBOL)
1484 /**
1485  * @tc.name: JSIInterfaceTest011
1486  * @tc.desc: Verify JSI interface related to Symbol object.
1487  */
1488 HWTEST_F(JsiInterfaceTddTest, test011, TestSize.Level1)
1489 {
1490     JsiInterfaceTddTest::JSIInterfaceTest011();
1491 }
1492 #endif // ENABLED(JS_FWK_SYMBOL)
1493 
1494 /**
1495  * @tc.name: JSIInterfaceTest012
1496  * @tc.desc: Verify JSI interface related to JS Object.
1497  */
1498 HWTEST_F(JsiInterfaceTddTest, test012, TestSize.Level1)
1499 {
1500     JsiInterfaceTddTest::JSIInterfaceTest012();
1501 }
1502 
1503 /**
1504  * @tc.name: JSIInterfaceTest013
1505  * @tc.desc: Verify JSI interface related to JS JSON.
1506  */
1507 HWTEST_F(JsiInterfaceTddTest, test013, TestSize.Level1)
1508 {
1509     JsiInterfaceTddTest::JSIInterfaceTest013();
1510 }
1511 
1512 /**
1513  * @tc.name: JSIInterfaceTest014
1514  * @tc.desc: Verify JSI interface related to JS Number property.
1515  */
1516 HWTEST_F(JsiInterfaceTddTest, test014, TestSize.Level1)
1517 {
1518     JsiInterfaceTddTest::JSIInterfaceTest014();
1519 }
1520 
1521 /**
1522  * @tc.name: JSIInterfaceTest015
1523  * @tc.desc: Verify JSI interface related to JS Boolean property.
1524  */
1525 HWTEST_F(JsiInterfaceTddTest, test015, TestSize.Level1)
1526 {
1527     JsiInterfaceTddTest::JSIInterfaceTest015();
1528 }
1529 
1530 /**
1531  * @tc.name: JSIInterfaceTest016
1532  * @tc.desc: Verify JSI interface related to JS String property.
1533  */
1534 HWTEST_F(JsiInterfaceTddTest, test016, TestSize.Level1)
1535 {
1536     JsiInterfaceTddTest::JSIInterfaceTest016();
1537 }
1538 
1539 /**
1540  * @tc.name: JSIInterfaceTest017
1541  * @tc.desc: Verify JSI interface related to JS property with index.
1542  */
1543 HWTEST_F(JsiInterfaceTddTest, test017, TestSize.Level1)
1544 {
1545     JsiInterfaceTddTest::JSIInterfaceTest017();
1546 }
1547 
1548 #if IS_ENABLED(JS_FWK_TYPEDARRAY)
1549 /**
1550  * @tc.name: JSIInterfaceTest018
1551  * @tc.desc: Verify JSI interface related to JS ArrayBuffer.
1552  */
1553 HWTEST_F(JsiInterfaceTddTest, test018, TestSize.Level1)
1554 {
1555     JsiInterfaceTddTest::JSIInterfaceTest018();
1556 }
1557 
1558 /**
1559  * @tc.name: JSIInterfaceTest019
1560  * @tc.desc: Verify JSI interface related to JS TypedArray.
1561  */
1562 HWTEST_F(JsiInterfaceTddTest, test019, TestSize.Level1)
1563 {
1564     JsiInterfaceTddTest::JSIInterfaceTest019();
1565 }
1566 #endif
1567 
1568 /**
1569  * @tc.name: JSIInterfaceTest020
1570  * @tc.desc: Verify JSI interface related to JS descriptor.
1571  */
1572 HWTEST_F(JsiInterfaceTddTest, test020, TestSize.Level1)
1573 {
1574     JsiInterfaceTddTest::JSIInterfaceTest020();
1575 }
1576 
1577 /**
1578  * @tc.name: JSIInterfaceTest021
1579  * @tc.desc: Verify JSI interface GetObjectKeys.
1580  */
1581 HWTEST_F(JsiInterfaceTddTest, test021, TestSize.Level0)
1582 {
1583     JsiInterfaceTddTest::JSIInterfaceTest021();
1584 }
1585 
1586 /**
1587  * @tc.name: JSIInterfaceTest022
1588  * @tc.desc: Verify JSI interfaces related to OnDestroy callback.
1589  */
1590 HWTEST_F(JsiInterfaceTddTest, test022, TestSize.Level0)
1591 {
1592     JsiInterfaceTddTest::JSIInterfaceTest022();
1593 }
1594 
1595 /**
1596  * @tc.name: JSIInterfaceTest023
1597  * @tc.desc: Verify JSI interfaces related to OnTerminate callback.
1598  */
1599 HWTEST_F(JsiInterfaceTddTest, test023, TestSize.Level0)
1600 {
1601     JsiInterfaceTddTest::JSIInterfaceTest023();
1602 }
1603 
1604 /**
1605  * @tc.name: JSIInterfaceTest024
1606  * @tc.desc: Verify JSI interfaces related to JS String to String.
1607  * @tc.require: I5NCYY
1608  */
1609 HWTEST_F(JsiInterfaceTddTest, test024, TestSize.Level0)
1610 {
1611     JsiInterfaceTddTest::JSIInterfaceTest024();
1612 }
1613 
1614 /**
1615  * @tc.name: JSIInterfaceTest025
1616  * @tc.desc: Verify JSI interfaces related to JS Number to String.
1617  * @tc.require: I5NCYY
1618  */
1619 HWTEST_F(JsiInterfaceTddTest, test025, TestSize.Level0)
1620 {
1621     JsiInterfaceTddTest::JSIInterfaceTest025();
1622 }
1623 
1624 /**
1625  * @tc.name: JSIInterfaceTest026
1626  * @tc.desc: Verify JSI interfaces related to JS bool to String.
1627  * @tc.require: I5NCYY
1628  */
1629 HWTEST_F(JsiInterfaceTddTest, test026, TestSize.Level0)
1630 {
1631     JsiInterfaceTddTest::JSIInterfaceTest026();
1632 }
1633 
1634 /**
1635  * @tc.name: JSIInterfaceTest027
1636  * @tc.desc: Verify JSI interfaces related to JS Object to String.
1637  * @tc.require: I5NCYY
1638  */
1639 HWTEST_F(JsiInterfaceTddTest, test027, TestSize.Level0)
1640 {
1641     JsiInterfaceTddTest::JSIInterfaceTest027();
1642 }
1643 
1644 /**
1645  * @tc.name: JSIInterfaceTest028
1646  * @tc.desc: Verify JSI interfaces related to JS array to String.
1647  * @tc.require: I5NCYY
1648  */
1649 HWTEST_F(JsiInterfaceTddTest, test028, TestSize.Level0)
1650 {
1651     JsiInterfaceTddTest::JSIInterfaceTest028();
1652 }
1653 
1654 /**
1655  * @tc.name: JSIInterfaceTest029
1656  * @tc.desc: Verify JSI interfaces related to JS func to String.
1657  * @tc.require: I5NCYY
1658  */
1659 HWTEST_F(JsiInterfaceTddTest, test029, TestSize.Level0)
1660 {
1661     JsiInterfaceTddTest::JSIInterfaceTest029();
1662 }
1663 
1664 /**
1665  * @tc.name: JSIInterfaceTest030
1666  * @tc.desc: Verify JSI create error function.
1667  * @tc.require: I5TIPU
1668  */
1669 HWTEST_F(JsiInterfaceTddTest, test030, TestSize.Level0)
1670 {
1671     JsiInterfaceTddTest::JSIInterfaceTest030();
1672 }
1673 
1674 /**
1675  * @tc.name: JSIInterfaceTest031
1676  * @tc.desc: Verify JSI create error function.
1677  * @tc.require: I5TIPU
1678  */
1679 HWTEST_F(JsiInterfaceTddTest, test031, TestSize.Level0)
1680 {
1681     JsiInterfaceTddTest::JSIInterfaceTest031();
1682 }
1683 
1684 /**
1685  * @tc.name: JSIInterfaceTest032
1686  * @tc.desc: Verify JSI create error function.
1687  * @tc.require: I5TIPU
1688  */
1689 HWTEST_F(JsiInterfaceTddTest, test032, TestSize.Level0)
1690 {
1691     JsiInterfaceTddTest::JSIInterfaceTest032();
1692 }
1693 
1694 /**
1695  * @tc.name: JSIInterfaceTest033
1696  * @tc.desc: Verify JSI create error function.
1697  * @tc.require: I5TIPU
1698  */
1699 HWTEST_F(JsiInterfaceTddTest, test033, TestSize.Level0)
1700 {
1701     JsiInterfaceTddTest::JSIInterfaceTest033();
1702 }
1703 
1704 /**
1705  * @tc.name: JSIInterfaceTest034
1706  * @tc.desc: Verify JSI create error function.
1707  * @tc.require: I5TIPU
1708  */
1709 HWTEST_F(JsiInterfaceTddTest, test034, TestSize.Level0)
1710 {
1711     JsiInterfaceTddTest::JSIInterfaceTest034();
1712 }
1713 
1714 /**
1715  * @tc.name: JSIInterfaceTest035
1716  * @tc.desc: Verify JSI create error function.
1717  * @tc.require: I5TIPU
1718  */
1719 HWTEST_F(JsiInterfaceTddTest, test035, TestSize.Level0)
1720 {
1721     JsiInterfaceTddTest::JSIInterfaceTest035();
1722 }
1723 
1724 /**
1725  * @tc.name: JSIInterfaceTest036
1726  * @tc.desc: Verify JSI create error function.
1727  * @tc.require: I5TIPU
1728  */
1729 HWTEST_F(JsiInterfaceTddTest, test036, TestSize.Level0)
1730 {
1731     JsiInterfaceTddTest::JSIInterfaceTest036();
1732 }
1733 
1734 /**
1735  * @tc.name: JSIInterfaceTest037
1736  * @tc.desc: Verify JSI create error function.
1737  * @tc.require: I5TIPU
1738  */
1739 HWTEST_F(JsiInterfaceTddTest, test037, TestSize.Level0)
1740 {
1741     JsiInterfaceTddTest::JSIInterfaceTest037();
1742 }
1743 
1744 /**
1745  * @tc.name: JSIInterfaceTest038
1746  * @tc.desc: Verify JSI create error function.
1747  * @tc.require: I5TIPU
1748  */
1749 HWTEST_F(JsiInterfaceTddTest, test038, TestSize.Level0)
1750 {
1751     JsiInterfaceTddTest::JSIInterfaceTest038();
1752 }
1753 #endif
1754 } // namespace ACELite
1755 } // namespace OHOS
1756