• 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 #include <gtest/gtest.h>
17 #include "ani_gtest.h"
18 #include "libani_helpers/ani_signature_builder.h"
19 
20 namespace ark::ets::ani_helpers::testing {
21 
22 // NOLINTNEXTLINE
23 using namespace arkts::ani_signature;
24 class SignatureBuilderTest : public ani::testing::AniTest {
25 public:
26     const Module module_ = Builder::BuildModule("ani_signature_builder_runtime_test");
27     const Namespace namespace_ = Builder::BuildNamespace({module_.Name(), "TestNamespace"});
28     const Type class_ = Builder::BuildClass({module_.Name(), "TestClass"});
29 };
30 
TEST_F(SignatureBuilderTest,BuilderTest)31 TEST_F(SignatureBuilderTest, BuilderTest)
32 {
33     ani_module mod {nullptr};
34     ASSERT_EQ(env_->FindModule(module_.Descriptor().c_str(), &mod), ANI_OK);
35     ani_namespace ns {nullptr};
36     ASSERT_EQ(env_->FindNamespace(namespace_.Descriptor().c_str(), &ns), ANI_OK);
37     ani_class cls {nullptr};
38     ASSERT_EQ(env_->FindClass(class_.Descriptor().c_str(), &cls), ANI_OK);
39 }
40 
TEST_F(SignatureBuilderTest,PrimitiveTypeSignatureTest)41 TEST_F(SignatureBuilderTest, PrimitiveTypeSignatureTest)
42 {
43     ani_class cls {nullptr};
44     ASSERT_EQ(env_->FindClass(class_.Descriptor().c_str(), &cls), ANI_OK);
45     ani_static_method sMethod {nullptr};
46     std::string signature;
47 
48     SignatureBuilder sbBoolean {};
49     sbBoolean.AddBoolean().AddBoolean().SetReturnBoolean();
50     signature = sbBoolean.BuildSignatureDescriptor();
51     ASSERT_EQ(env_->Class_FindStaticMethod(cls, "func", signature.c_str(), &sMethod), ANI_OK);
52 
53     SignatureBuilder sbChar {};
54     sbChar.AddChar().AddChar().SetReturnChar();
55     signature = sbBoolean.BuildSignatureDescriptor();
56     ASSERT_EQ(env_->Class_FindStaticMethod(cls, "func", signature.c_str(), &sMethod), ANI_OK);
57 
58     SignatureBuilder sbByte {};
59     sbByte.AddByte().AddByte().SetReturnByte();
60     signature = sbByte.BuildSignatureDescriptor();
61     ASSERT_EQ(env_->Class_FindStaticMethod(cls, "func", signature.c_str(), &sMethod), ANI_OK);
62 
63     SignatureBuilder sbShort {};
64     sbShort.AddShort().AddShort().SetReturnShort();
65     signature = sbShort.BuildSignatureDescriptor();
66     ASSERT_EQ(env_->Class_FindStaticMethod(cls, "func", signature.c_str(), &sMethod), ANI_OK);
67 
68     SignatureBuilder sbInt {};
69     sbInt.AddInt().AddInt().SetReturnInt();
70     signature = sbInt.BuildSignatureDescriptor();
71     ASSERT_EQ(env_->Class_FindStaticMethod(cls, "func", signature.c_str(), &sMethod), ANI_OK);
72 
73     SignatureBuilder sbLong {};
74     sbLong.AddLong().AddLong().SetReturnLong();
75     signature = sbLong.BuildSignatureDescriptor();
76     ASSERT_EQ(env_->Class_FindStaticMethod(cls, "func", signature.c_str(), &sMethod), ANI_OK);
77 
78     SignatureBuilder sbFloat {};
79     sbFloat.AddFloat().AddFloat().SetReturnFloat();
80     signature = sbFloat.BuildSignatureDescriptor();
81     ASSERT_EQ(env_->Class_FindStaticMethod(cls, "func", signature.c_str(), &sMethod), ANI_OK);
82 
83     SignatureBuilder sbDouble {};
84     sbDouble.AddDouble().AddDouble().SetReturnDouble();
85     signature = sbDouble.BuildSignatureDescriptor();
86     ASSERT_EQ(env_->Class_FindStaticMethod(cls, "func", signature.c_str(), &sMethod), ANI_OK);
87 }
88 
TEST_F(SignatureBuilderTest,ReferenceTypeSignatureTest)89 TEST_F(SignatureBuilderTest, ReferenceTypeSignatureTest)
90 {
91     const Module stdCore = Builder::BuildModule("std.core");
92 
93     ani_namespace ns {nullptr};
94     ASSERT_EQ(env_->FindNamespace(namespace_.Descriptor().c_str(), &ns), ANI_OK);
95     ani_function nsFunction {nullptr};
96     std::string signature;
97 
98     SignatureBuilder sbString {};
99     sbString.AddClass({stdCore.Name(), "String"}).SetReturnInt();
100     signature = sbString.BuildSignatureDescriptor();
101     ASSERT_EQ(env_->Namespace_FindFunction(ns, "func", signature.c_str(), &nsFunction), ANI_OK);
102 
103     SignatureBuilder sbEnum {};
104     sbEnum.AddEnum({module_.Name(), "COLOR"}).SetReturnInt();
105     signature = sbEnum.BuildSignatureDescriptor();
106     ASSERT_EQ(env_->Namespace_FindFunction(ns, "func", signature.c_str(), &nsFunction), ANI_OK);
107 
108     SignatureBuilder sbFnType {};
109     sbFnType.AddFunctionalObject(2U, false).SetReturnInt();
110     signature = sbFnType.BuildSignatureDescriptor();
111     ASSERT_EQ(env_->Namespace_FindFunction(ns, "func", signature.c_str(), &nsFunction), ANI_OK);
112 
113     SignatureBuilder sbFnTypeRestArgs {};
114     sbFnTypeRestArgs.AddFunctionalObject(2U, true).SetReturnInt();
115     signature = sbFnTypeRestArgs.BuildSignatureDescriptor();
116     ASSERT_EQ(env_->Namespace_FindFunction(ns, "func", signature.c_str(), &nsFunction), ANI_OK);
117 
118     SignatureBuilder sbUndefined {};
119     sbUndefined.AddUndefined().SetReturnInt();
120     signature = sbUndefined.BuildSignatureDescriptor();
121     ASSERT_EQ(env_->Namespace_FindFunction(ns, "funcUndefined", signature.c_str(), &nsFunction), ANI_OK);
122 
123     SignatureBuilder sbNull {};
124     sbNull.AddNull().SetReturnInt();
125     signature = sbNull.BuildSignatureDescriptor();
126     ASSERT_EQ(env_->Namespace_FindFunction(ns, "funcNull", signature.c_str(), &nsFunction), ANI_OK);
127 }
128 }  // namespace ark::ets::ani_helpers::testing