• 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 
18 #define private public
19 #define protected public
20 
21 #include "adapter/ohos/entrance/ace_container.h"
22 #include "base/log/dump_log.h"
23 #include "frameworks/core/accessibility/utils/accessibility_rect_info_utils.h"
24 
25 using namespace OHOS::Accessibility;
26 using namespace testing;
27 using namespace testing::ext;
28 
29 namespace OHOS::Ace {
30 } // namespace OHOS::Ace
31 
32 namespace OHOS::Ace::NG {
33 namespace {
34 } // namespace
35 
36 
37 class AccessibilityRealRectInfoTest  : public testing::Test {
38 public:
39     static void SetUpTestCase();
40     static void TearDownTestCase();
41 
42     PointT<int32_t> defaultPoint_ = PointT<int32_t>(0, 0);
43     VectorF defaultScale_ = VectorF(1.0f, 1.0f);
44 
45     int32_t componentX_ = 100;
46     int32_t componentY_ = 200;
47     float componentWidth_ = 300;
48     float componentHeight_ = 400;
49     float scaleX_ = 0.5f;
50     float scaleY_ = 0.5f;
51     float scaleIgnoreX_ = 1.0f;
52     float scaleIgnoreY_ = 1.0f;
53     int32_t defaultDegree_ = 90;
54 };
55 
SetUpTestCase()56 void AccessibilityRealRectInfoTest ::SetUpTestCase()
57 {
58     std::unique_ptr<std::ostream> ostream = std::make_unique<std::ostringstream>();
59     ASSERT_NE(ostream, nullptr);
60     DumpLog::GetInstance().SetDumpFile(std::move(ostream));
61 }
62 
TearDownTestCase()63 void AccessibilityRealRectInfoTest ::TearDownTestCase()
64 {
65 }
66 
67 /**
68  * @tc.name: AccessibilityRealRectInfo_Constructor_Initialization
69  * @tc.desc: Verify constructor initializes member variables correctly
70  * @tc.type: FUNC
71  */
TEST_F(AccessibilityRealRectInfoTest,ConstructorInitialization)72 TEST_F(AccessibilityRealRectInfoTest, ConstructorInitialization)
73 {
74     /**
75      * @tc.step1: Create instance with initial parameters
76      * @tc.expected: Internal state matches construction values
77      */
78     AccessibilityRealRectInfo rect(defaultPoint_, componentWidth_, componentHeight_, defaultDegree_, defaultScale_);
79 
80     /**
81      * @tc.step2: Verify default rotation calculation
82      * @tc.expected: Returns 0 rotation for identical points
83      */
84     EXPECT_EQ(rect.GetRotationByConner(defaultPoint_, defaultPoint_), 0);
85 }
86 
87 /**
88  * @tc.name: AccessibilityRealRectInfo_Rotation_Quadrant_Detection
89  * @tc.desc: Test rotation calculation for all quadrants and edge cases
90  * @tc.type: FUNC
91  */
TEST_F(AccessibilityRealRectInfoTest,RotationCalculationFourQuadrants)92 TEST_F(AccessibilityRealRectInfoTest, RotationCalculationFourQuadrants)
93 {
94     struct RotationTestParam {
95         PointT<int32_t> leftTop;
96         PointT<int32_t> rightBottom;
97         int32_t expectedRotation;
98     };
99 
100     const std::vector<RotationTestParam> testCases = {
101         { {10, 20}, {30, 40}, 0 },     // Normal bottom-right quadrant
102         { {30, 20}, {10, 40}, 90 },     // Left quadrant rotation
103         { {30, 40}, {10, 20}, 180 },    // Top-left quadrant
104         { {10, 40}, {30, 20}, 270 },    // Bottom quadrant rotation
105         { {10, 10}, {10, 10}, 0 }       // Identical points case
106     };
107 
108     for (const auto& testCase : testCases) {
109         /**
110          * @tc.step1: Initialize instance with test parameters
111          * @tc.expected: Instance created without errors
112          */
113         AccessibilityRealRectInfo rect(defaultPoint_, 0, 0, 0, defaultScale_);
114 
115         /**
116          * @tc.step2: Calculate rotation for test points
117          * @tc.expected: Rotation matches quadrant expectation
118          */
119         EXPECT_EQ(rect.GetRotationByConner(testCase.leftTop, testCase.rightBottom), testCase.expectedRotation);
120     }
121 }
122 
123 /**
124  * @tc.name: AccessibilityRealRectInfo_Scale_Accumulation
125  * @tc.desc: Verify scale factor multiplication logic
126  * @tc.type: FUNC
127  */
TEST_F(AccessibilityRealRectInfoTest,ScaleFactorAccumulation)128 TEST_F(AccessibilityRealRectInfoTest, ScaleFactorAccumulation)
129 {
130     /**
131      * @tc.step1: Initialize with base scale (1.0, 1.0)
132      * @tc.expected: Initial scale values set correctly
133      */
134     AccessibilityRealRectInfo rect(defaultPoint_, componentWidth_, componentHeight_, 0, VectorF(0.5f, 0.6f));
135 
136     /**
137      * @tc.step2: Apply parent scale factors
138      * @tc.expected: Scale values multiply correctly
139      */
140     VectorF parentScale(0.8f, 0.4f);
141     rect.UpdateScale(parentScale);
142     EXPECT_FLOAT_EQ(rect.scale_.x, 0.4f);
143     EXPECT_FLOAT_EQ(rect.scale_.y, 0.24f);
144 }
145 
146 /**
147  * @tc.name: AccessibilityRealRectInfo_Coordinate_Mapping_NoRotation
148  * @tc.desc: Test coordinate transformation without rotation
149  * @tc.type: FUNC
150  */
TEST_F(AccessibilityRealRectInfoTest,CoordinateMappingNoRotation)151 TEST_F(AccessibilityRealRectInfoTest, CoordinateMappingNoRotation)
152 {
153     /**
154      * @tc.step1: Create instance with 0° rotation
155      * @tc.expected: Rotation initialized to 0 degrees
156      */
157     AccessibilityRealRectInfo rect(PointT<int32_t>(componentX_, componentY_),
158         componentWidth_, componentHeight_, 0, VectorF(scaleX_, scaleY_));
159 
160     /**
161      * @tc.step2: Apply scaling to test point
162      * @tc.expected: Point coordinates scaled correctly
163      */
164     int32_t componentTestX = 50;
165     int32_t componentTestY = 60;
166     PointT<int32_t> testPoint(componentTestX, componentTestY);
167     rect.UpdatePointWithScale(testPoint);
168     EXPECT_EQ(testPoint.GetX(), componentTestX * scaleX_);
169     EXPECT_EQ(testPoint.GetY(), componentTestY * scaleY_);
170 
171     /**
172      * @tc.step3: Transform to real coordinates
173      * @tc.expected: Final coordinates match expected values
174      */
175     rect.UpdatePointToReal(testPoint);
176 
177     EXPECT_EQ(testPoint.GetX(), componentX_ + componentTestX * scaleX_);
178     EXPECT_EQ(testPoint.GetY(), componentY_ + componentTestY * scaleY_);
179 }
180 
181 /**
182  * @tc.name: AccessibilityRealRectInfo_90Degree_Rotation_Transform
183  * @tc.desc: Verify coordinate transformation with 90° rotation
184  * @tc.type: FUNC
185  */
TEST_F(AccessibilityRealRectInfoTest,CoordinateMapping90DegreeRotation)186 TEST_F(AccessibilityRealRectInfoTest, CoordinateMapping90DegreeRotation)
187 {
188     /**
189      * @tc.step1: Create instance with 90° rotation
190      * @tc.expected: Rotation initialized to 90 degrees
191      */
192     AccessibilityRealRectInfo rect(PointT<int32_t>(componentX_, componentY_),
193         componentWidth_, componentHeight_, defaultDegree_, VectorF(scaleIgnoreX_, scaleIgnoreY_));
194 
195     /**
196      * @tc.step2: Transform test coordinates
197      * @tc.expected: Coordinates follow rotation matrix calculation
198      */
199     int32_t componentTestX = 50;
200     int32_t componentTestY = 60;
201     PointT<int32_t> testPoint(componentTestX, componentTestY);
202     rect.UpdatePointToReal(testPoint);
203     EXPECT_EQ(testPoint.GetX(), componentX_ - componentTestY);
204     EXPECT_EQ(testPoint.GetY(), componentY_ + componentTestX);
205 }
206 
207 /**
208  * @tc.name: AccessibilityRealRectInfo_Rect_Generation_Workflow
209  * @tc.desc: Test complete rectangle generation process
210  * @tc.type: FUNC
211  */
TEST_F(AccessibilityRealRectInfoTest,RectInfoGenerationWorkflow)212 TEST_F(AccessibilityRealRectInfoTest, RectInfoGenerationWorkflow)
213 {
214     /**
215      * @tc.step1: Initialize parent rectangle parameters and Define relative corner points
216      * @tc.expected: Parent properties set correctly
217      */
218     int32_t parentScaleX = 2.0f;
219     int32_t parentScaleY = 2.0f;
220     int32_t parentDegree = 90;
221     AccessibilityRealRectInfo parentRect(
222         PointT<int32_t>(0, 0), 200.0f, 300.0f, parentDegree, VectorF(parentScaleX, parentScaleY));
223     int32_t componentTestX = 10;
224     int32_t componentTestY = 20;
225     PointT<int32_t> leftTop(componentTestX, componentTestY);
226     int32_t componentTestWidth = 20;
227     int32_t componentTestHeight = 30;
228     PointT<int32_t> rightBottom(componentTestX + componentTestWidth, componentTestY + componentTestHeight);
229     float componentTestScaleX = 1.5f;
230     float componentTestScaleY = 1.5f;
231     VectorF scale(componentTestScaleX, componentTestScaleY);
232 
233     /**
234      * @tc.step2: Generate new rectangle info
235      * @tc.expected: Dimensions and rotation match expectations
236      */
237     auto newRect = parentRect.GenerateRectInfoByRelativeConner(leftTop, rightBottom, scale);
238 
239     EXPECT_FLOAT_EQ(newRect.width_, componentTestHeight * parentScaleY);
240     EXPECT_EQ(newRect.rotation_, parentDegree);
241     EXPECT_FLOAT_EQ(newRect.scale_.x, componentTestScaleX * parentScaleX);
242     EXPECT_FLOAT_EQ(newRect.scale_.y, componentTestScaleY * parentScaleY);
243 }
244 
245 /**
246  * @tc.name: AccessibilityRealRectInfo_Zero_Dimension_Handling
247  * @tc.desc: Verify zero dimension handling logic
248  * @tc.type: FUNC
249  */
TEST_F(AccessibilityRealRectInfoTest,ZeroDimensionHandling)250 TEST_F(AccessibilityRealRectInfoTest, ZeroDimensionHandling)
251 {
252     /**
253      * @tc.step1: Create instance with zero dimensions
254      * @tc.expected: Width/height initialized to zero
255      */
256     AccessibilityRealRectInfo rect(PointT<int32_t>(componentX_, componentY_),
257         0, 0, 0, VectorF(1.0f, 1.0f));
258 
259     /**
260      * @tc.step2: Transform origin point
261      * @tc.expected: Coordinates remain at original position
262      */
263     PointT<int32_t> testPoint(0, 0);
264     rect.UpdatePointToReal(testPoint);
265     EXPECT_EQ(testPoint.GetX(), componentX_);
266     EXPECT_EQ(testPoint.GetY(), componentY_);
267 }
268 } // namespace OHOS::Ace::NG