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 async_test from "async_test"; 17 18loadLibrary("ani_async"); 19 20function testSyncAdd() { 21 { 22 let res: int; 23 let err: BusinessError = new BusinessError(); 24 try { 25 res = async_test.addSync(1, 2); 26 } catch (e) { 27 err = e as BusinessError; 28 } 29 assertEQ(res, 3, "addSync should correctly add 1 + 2"); 30 } 31 { 32 let res: int; 33 let err: BusinessError = new BusinessError(); 34 try { 35 res = async_test.addSync(0, 2); 36 } catch (e) { 37 err = e as BusinessError; 38 } 39 assertEQ(err.code, 1, "addSync should throw an error"); 40 assertEQ( 41 err.message, "some error happen in add impl", 42 "addSync should throw an error when first parameter is 0"); 43 } 44 console.log("testSyncAdd end"); 45} 46 47function testAsyncAdd() { 48 { 49 let promise = new Promise<int>((resolve, reject) => { 50 async_test.add(10, 20, (err: BusinessError, data: int) => { 51 if (err.code !== 0) { 52 reject(err); 53 } else { 54 resolve(data); 55 } 56 }); 57 }); 58 let res: int; 59 let err: BusinessError = new BusinessError(); 60 try { 61 res = await promise; 62 } catch (e) { 63 err = e as BusinessError; 64 } 65 assertEQ(res, 30, "add callback should receive 10 + 20 = 30"); 66 } 67 { 68 let promise = new Promise<int>((resolve, reject) => { 69 async_test.add(0, 2, (err: BusinessError, data: int) => { 70 if (err.code !== 0) { 71 reject(err); 72 } else { 73 resolve(data); 74 } 75 }); 76 }); 77 let res: int; 78 let err: BusinessError = new BusinessError(); 79 try { 80 res = await promise; 81 } catch (e) { 82 err = e as BusinessError; 83 } 84 assertEQ(err.code, 1, "add callback should receive an err"); 85 assertEQ( 86 err.message, "some error happen in add impl", 87 "add callback should receive an err when first parameter is 0"); 88 } 89 console.log("testAsyncAdd end"); 90} 91 92function testPromiseAdd() { 93 { 94 let res: int; 95 let err: BusinessError = new BusinessError(); 96 try { 97 res = await async_test.add(1, 2); 98 } catch (e) { 99 err = e as BusinessError; 100 } 101 assertEQ(res, 3, "add promise should correctly add 1 + 2"); 102 } 103 { 104 let res: int; 105 let err: BusinessError = new BusinessError(); 106 try { 107 res = await async_test.add(0, 2); 108 } catch (e) { 109 err = e as BusinessError; 110 } 111 assertEQ(err.code, 1, "add promise should throw an error"); 112 assertEQ( 113 err.message, "some error happen in add impl", 114 "add promise should throw an error when first parameter is 0"); 115 } 116 console.log("testPromiseAdd end"); 117} 118 119function testIBase() { 120 const ibase = async_test.getIBase(); 121 assertEQ(ibase.get(), "My IBase", "IBase.get() should return 'My IBase'"); 122 123 await ibase.setPromise("interface async set"); 124 assertEQ( 125 await ibase.getPromise(), "interface async set", 126 "getPromise should resolve with the value set in setAsync"); 127} 128 129function testStaticAsyncMethods() { 130 await async_test.IBase.printPromise(); 131} 132 133function testStructMethods() { 134 await async_test.fromStruct( 135 {a: "abc", b: "efg", c: 3}); // should not throw an error 136 137 { 138 let err: BusinessError = new BusinessError(); 139 try { 140 await async_test.fromStruct({a: "abc", b: "efg", c: 0}); 141 } catch (e) { 142 err = e as BusinessError; 143 } 144 assertEQ(err.code, 1, "fromStruct should throw an error"); 145 assertEQ( 146 err.message, "some error happen in fromStructSync_impl", 147 "fromStruct should throw an error when c is 0"); 148 } 149 150 let data: async_test.Data = await async_test.toStruct("aaa", "bbb", 1); 151 assertEQ(data.a, "aaa", "Data.a should be 'aaa'"); 152 assertEQ(data.b, "bbb", "Data.b should be 'bbb'"); 153 assertEQ(data.c, 1, "Data.c should be 1"); 154 155 { 156 let err: BusinessError = new BusinessError(); 157 try { 158 data = await async_test.toStruct("aaa", "bbb", 0); 159 } catch (e) { 160 err = e as BusinessError; 161 } 162 assertEQ(err.code, 1, "toStruct should throw an error"); 163 assertEQ( 164 err.message, "some error happen in toStructSync_impl", 165 "toStruct should throw an error when c is 0"); 166 } 167} 168 169function main() { 170 const suite = new ArkTestsuite("Async Test Suite"); 171 172 suite.addTest("Sync Add Tests", testSyncAdd); 173 suite.addTest("Async Add Tests", testAsyncAdd); 174 suite.addTest("Promise Add Tests", testPromiseAdd); 175 suite.addTest("IBase Tests", testIBase); 176 suite.addTest("Static Async Methods Tests", testStaticAsyncMethods); 177 suite.addTest("Struct Methods Tests", testStructMethods); 178 179 exit(suite.run()); 180} 181