• 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  */
15 
16 #ifndef ARKTS_ANI_TEST_CLASSOPERATIONS_H
17 #define ARKTS_ANI_TEST_CLASSOPERATIONS_H
18 #include "Common.h"
19 
test_FindClass(ani_env * env,ani_object object)20 ani_boolean test_FindClass([[maybe_unused]] ani_env *env, [[maybe_unused]] ani_object object)
21 {
22     ani_class cls = {};
23     auto status = env->FindClass("Lentry/src/main/src/ets/ClassOperations/Person;", &cls);
24     ASSERT_EQ(status, ANI_OK);
25     ASSERT_NE(cls, nullptr);
26     return ANI_TRUE;
27 }
28 
test_Class_FindStaticMethodBoolean(ani_env * env,ani_object object)29 ani_boolean test_Class_FindStaticMethodBoolean([[maybe_unused]] ani_env *env, [[maybe_unused]] ani_object object)
30 {
31     ani_class cls = {};
32     auto status = env->FindClass("Lentry/src/main/src/ets/ClassOperations/Person;", &cls);
33     ASSERT_EQ(status, ANI_OK);
34     ASSERT_NE(cls, nullptr);
35     ani_static_method method = nullptr;
36     status = env->Class_FindStaticMethod(cls, "or", "ZZ:Z", &method);
37     ASSERT_EQ(status, ANI_OK);
38     ASSERT_NE(method, nullptr);
39     return ANI_TRUE;
40 }
41 
test_Class_FindStaticMethodInt(ani_env * env,ani_object object)42 ani_boolean test_Class_FindStaticMethodInt([[maybe_unused]] ani_env *env, [[maybe_unused]] ani_object object)
43 {
44     ani_class cls = {};
45     auto status = env->FindClass("Lentry/src/main/src/ets/ClassOperations/Person;", &cls);
46     ASSERT_EQ(status, ANI_OK);
47     ASSERT_NE(cls, nullptr);
48     ani_static_method method = nullptr;
49     status = env->Class_FindStaticMethod(cls, "sum", "II:I", &method);
50     ASSERT_EQ(status, ANI_OK);
51     ASSERT_NE(method, nullptr);
52     return ANI_TRUE;
53 }
54 
test_Class_FindMethod(ani_env * env,ani_object object)55 ani_boolean test_Class_FindMethod([[maybe_unused]] ani_env *env, [[maybe_unused]] ani_object object)
56 {
57     ani_class cls = {};
58     auto status = env->FindClass("Lentry/src/main/src/ets/ClassOperations/Person;", &cls);
59     ASSERT_EQ(status, ANI_TRUE);
60     const char *name = "func";
61     const char *signature = "II:I";
62     ani_method result;
63     status = env->Class_FindMethod(cls, name, signature, &result);
64     ASSERT_EQ(status, ANI_OK);
65     ASSERT_NE(result, nullptr);
66     return ANI_TRUE;
67 }
68 
NativeMethodsFooNative(ani_env *,ani_class)69 ani_int NativeMethodsFooNative(ani_env *, ani_class)
70 {
71     const ani_int answer = 42U;
72     return answer;
73 }
74 
test_Class_BindNativeMethods(ani_env * env,ani_object object)75 ani_boolean test_Class_BindNativeMethods([[maybe_unused]] ani_env *env, [[maybe_unused]] ani_object object)
76 {
77     ani_class cls = {};
78     auto status = env->FindClass("Lentry/src/main/src/ets/ClassOperations/Person;", &cls);
79     ASSERT_EQ(status, ANI_OK);
80     std::array methods = {
81         ani_native_function{"foo", ":I", reinterpret_cast<void *>(NativeMethodsFooNative)},
82     };
83     status = env->Class_BindNativeMethods(cls, methods.data(), methods.size());
84     ASSERT_EQ(status, ANI_OK);
85     return ANI_TRUE;
86 }
87 
test_Type_GetSuperClass(ani_env * env,ani_object object)88 ani_boolean test_Type_GetSuperClass([[maybe_unused]] ani_env *env, [[maybe_unused]] ani_object object)
89 {
90     ani_class cls = nullptr;
91     auto status = env->FindClass("Lentry/src/main/src/ets/ClassOperations/Son;", &cls);
92     ASSERT_EQ(status, ANI_OK);
93     ani_class result = nullptr;
94     status = env->Type_GetSuperClass(cls, &result);
95     ASSERT_EQ(status, ANI_OK);
96     ASSERT_NE(result, nullptr);
97     return true;
98 }
99 
test_Type_IsAssignableFrom(ani_env * env,ani_object object)100 ani_boolean test_Type_IsAssignableFrom([[maybe_unused]] ani_env *env, [[maybe_unused]] ani_object object)
101 {
102     ani_class fromCls;
103     ASSERT_EQ(env->FindClass("Lentry/src/main/src/ets/ClassOperations/A;", &fromCls), ANI_OK);
104     ASSERT_NE(fromCls, nullptr);
105     ani_class toCls;
106     ASSERT_EQ(env->FindClass("Lentry/src/main/src/ets/ClassOperations/I;", &toCls), ANI_OK);
107     ASSERT_NE(toCls, nullptr);
108     ani_boolean result;
109     ASSERT_EQ(env->Type_IsAssignableFrom(fromCls, toCls, &result), ANI_OK);
110     return ANI_TRUE;
111 }
112 
test_FindModule(ani_env * env,ani_object object)113 ani_boolean test_FindModule([[maybe_unused]] ani_env *env, [[maybe_unused]] ani_object object)
114 {
115     ani_module module{};
116     ASSERT_EQ(env->FindModule("Lentry/src/main/src/ets/ClassOperations;", &module), ANI_OK);
117     ASSERT_NE(module, nullptr);
118     return ANI_TRUE;
119 }
120 
test_FindNamespace(ani_env * env,ani_object object)121 ani_boolean test_FindNamespace([[maybe_unused]] ani_env *env, [[maybe_unused]] ani_object object)
122 {
123     ani_namespace ns{};
124     ASSERT_EQ(env->FindNamespace("Lentry/src/main/src/ets/ClassOperations/test001;", &ns), ANI_OK);
125     ASSERT_NE(ns, nullptr);
126     return ANI_TRUE;
127 }
128 
test_FindEnum(ani_env * env,ani_object object)129 ani_boolean test_FindEnum([[maybe_unused]] ani_env *env, [[maybe_unused]] ani_object object)
130 {
131     ani_enum aniEnum{};
132     ASSERT_EQ(env->FindEnum("Lentry/src/main/src/ets/ClassOperations/Color;", &aniEnum), ANI_OK);
133     ASSERT_NE(aniEnum, nullptr);
134     return ANI_TRUE;
135 }
136 
137 #endif // ARKTS_ANI_TEST_CLASSOPERATIONS_H
138