• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 */
15import {BusinessError} from "@ohos.base";
16import * as lib from "callbackTest";
17
18loadLibrary("ani_callback");
19
20function test_void_input_void_output() {
21    lib.testCbV(() => {
22        console.log("void input void output callback!");
23    });
24}
25
26function test_int_input_void_output() {
27    lib.testCbI((a) => {
28        assertEQ(a, 1);
29    });
30}
31
32function test_string_boolean_input() {
33    lib.testCbS((a, b) => {
34        if (b) {
35            assertEQ(a, "hello");
36        }
37    });
38}
39
40function test_string_return_value() {
41    let result = lib.testCbRs((a) => {
42        return "my return value";
43    });
44    assertEQ(result, "my return value");
45}
46
47function test_struct_input() {
48    lib.testCbStruct((a) => {
49        let data:
50            lib.Data = {a: (a.a + "main"), b: (a.b + "main"), c: (a.c + 100)};
51        assertEQ(data.a, "amain");
52        assertEQ(data.b, "bmain");
53        assertEQ(data.c, 101);
54        return data;
55    });
56}
57
58function test_int_string_return_value(){
59    let instance: lib.MyInterface = lib.getInterface();
60    let result =instance.testCbIntString((a,b)=>{
61        assertEQ(a, 10);
62        assertEQ(b, 100);
63    });
64    assertEQ(result, "testCbIntString");
65}
66
67function test_int_bool_return_value(){
68    let instance: lib.MyInterface = lib.getInterface();
69    let result =instance.testCbIntBool((a,b)=>{
70        assertEQ(a, 100);
71        assertEQ(b, 1000000000 as double);
72    });
73    assertEQ(result, true);
74}
75
76function test_int_enum_return_value(){
77    let instance: lib.MyInterface = lib.getInterface();
78    let result = instance.testCbEnum((a)=>{
79        assertEQ(a,10);
80    })
81    assertEQ(result.toString(), "123");
82}
83
84function main() {
85    console.log("##############start#############");
86    const suite = new ArkTestsuite("Callback Tests");
87
88    suite.addTest("test void input void output", test_void_input_void_output);
89    suite.addTest("test int input void output", test_int_input_void_output);
90    suite.addTest("test string boolean input", test_string_boolean_input);
91    suite.addTest("test string return value", test_string_return_value);
92    suite.addTest("test struct input", test_struct_input);
93
94    suite.addTest("test int string return value", test_int_string_return_value);
95    suite.addTest("test int bool return value", test_int_bool_return_value);
96    suite.addTest("test int enum return value", test_int_enum_return_value);
97    exit(suite.run());
98}