• 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 "ani_gtest.h"
17 
18 namespace ark::ets::ani::testing {
19 
20 class ObjectSetFieldByteTest : public AniTest {
21 public:
GetTestDataForByte(ani_object * packResult,ani_field * fieldByteResult,ani_field * fieldStringResult)22     void GetTestDataForByte(ani_object *packResult, ani_field *fieldByteResult, ani_field *fieldStringResult)
23     {
24         auto packRef = CallEtsFunction<ani_ref>("object_set_field_byte_test", "newPackObject");
25 
26         ani_class cls {};
27         ASSERT_EQ(env_->FindClass("Lobject_set_field_byte_test/Pack;", &cls), ANI_OK);
28 
29         ani_field fieldByte {};
30         ASSERT_EQ(env_->Class_FindField(cls, "byte_value", &fieldByte), ANI_OK);
31 
32         ani_field fieldString {};
33         ASSERT_EQ(env_->Class_FindField(cls, "string_value", &fieldString), ANI_OK);
34 
35         *packResult = static_cast<ani_object>(packRef);
36         *fieldByteResult = fieldByte;
37         *fieldStringResult = fieldString;
38     }
39 };
40 
TEST_F(ObjectSetFieldByteTest,set_field_byte)41 TEST_F(ObjectSetFieldByteTest, set_field_byte)
42 {
43     ani_object pack {};
44     ani_field fieldByte {};
45     ani_field fieldString {};
46     GetTestDataForByte(&pack, &fieldByte, &fieldString);
47     const int zoerValue = 0;
48     const int maxByteValue = 127;
49     const int valuE128 = -128;
50     ASSERT_EQ(CallEtsFunction<ani_boolean>("object_set_field_byte_test", "checkByteValue", pack, zoerValue), ANI_TRUE);
51 
52     const int32_t loopCount = 3;
53     for (int32_t i = 1; i <= loopCount; i++) {
54         ASSERT_EQ(env_->Object_SetField_Byte(pack, fieldByte, maxByteValue), ANI_OK);
55         ASSERT_EQ(CallEtsFunction<ani_boolean>("object_set_field_byte_test", "checkByteValue", pack, maxByteValue),
56                   ANI_TRUE);
57 
58         ani_byte byt {};
59         ASSERT_EQ(env_->Object_GetField_Byte(pack, fieldByte, &byt), ANI_OK);
60         ASSERT_EQ(byt, maxByteValue);
61 
62         ASSERT_EQ(env_->Object_SetField_Byte(pack, fieldByte, valuE128), ANI_OK);
63         ASSERT_EQ(CallEtsFunction<ani_boolean>("object_set_field_byte_test", "checkByteValue", pack, valuE128),
64                   ANI_TRUE);
65 
66         ASSERT_EQ(env_->Object_GetField_Byte(pack, fieldByte, &byt), ANI_OK);
67         ASSERT_EQ(byt, valuE128);
68 
69         ASSERT_EQ(env_->Object_SetField_Byte(pack, fieldByte, zoerValue), ANI_OK);
70         ASSERT_EQ(CallEtsFunction<ani_boolean>("object_set_field_byte_test", "checkByteValue", pack, zoerValue),
71                   ANI_TRUE);
72 
73         ASSERT_EQ(env_->Object_GetField_Byte(pack, fieldByte, &byt), ANI_OK);
74         ASSERT_EQ(byt, zoerValue);
75     }
76 }
77 
TEST_F(ObjectSetFieldByteTest,set_field_byte_invalid_args_env)78 TEST_F(ObjectSetFieldByteTest, set_field_byte_invalid_args_env)
79 {
80     ani_object pack {};
81     ani_field fieldByte {};
82     ani_field fieldString {};
83     GetTestDataForByte(&pack, &fieldByte, &fieldString);
84 
85     const int maxByteValue = 127;
86     ASSERT_EQ(env_->c_api->Object_SetField_Byte(nullptr, pack, fieldByte, maxByteValue), ANI_INVALID_ARGS);
87 }
88 
TEST_F(ObjectSetFieldByteTest,set_field_byte_invalid_field_type)89 TEST_F(ObjectSetFieldByteTest, set_field_byte_invalid_field_type)
90 {
91     ani_object pack {};
92     ani_field fieldByte {};
93     ani_field fieldString {};
94     GetTestDataForByte(&pack, &fieldByte, &fieldString);
95 
96     const int maxByteValue = 127;
97     ASSERT_EQ(env_->Object_SetField_Byte(pack, fieldString, maxByteValue), ANI_INVALID_TYPE);
98 }
99 
TEST_F(ObjectSetFieldByteTest,set_field_byte_invalid_args_object)100 TEST_F(ObjectSetFieldByteTest, set_field_byte_invalid_args_object)
101 {
102     ani_object pack {};
103     ani_field fieldByte {};
104     ani_field fieldString {};
105     GetTestDataForByte(&pack, &fieldByte, &fieldString);
106 
107     const int maxByteValue = 127;
108     ASSERT_EQ(env_->Object_SetField_Byte(nullptr, fieldByte, maxByteValue), ANI_INVALID_ARGS);
109 }
110 
TEST_F(ObjectSetFieldByteTest,set_field_byte_invalid_args_field)111 TEST_F(ObjectSetFieldByteTest, set_field_byte_invalid_args_field)
112 {
113     ani_object pack {};
114     ani_field fieldByte {};
115     ani_field fieldString {};
116     GetTestDataForByte(&pack, &fieldByte, &fieldString);
117 
118     const int maxByteValue = 127;
119     ASSERT_EQ(env_->Object_SetField_Byte(pack, nullptr, maxByteValue), ANI_INVALID_ARGS);
120 }
121 
122 }  // namespace ark::ets::ani::testing