1 /*
2  * Copyright (c) 2023 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 #include "frameworks/core/accessibility/accessibility_manager.h"
21 #include "frameworks/core/accessibility/accessibility_node.h"
22 #include "frameworks/core/accessibility/accessibility_utils.h"
23 
24 using namespace testing;
25 using namespace testing::ext;
26 
27 namespace OHOS::Ace::NG {
28 namespace {} // namespace
29 
30 class AccessibilityUtilsTestNg : public testing::Test {
31 public:
SetUpTestCase()32     static void SetUpTestCase() {};
TearDownTestCase()33     static void TearDownTestCase() {};
34 };
35 
36 /**
37  * @tc.name: accessibilityTest001
38  * @tc.desc: CheckBetterRect-IsCandidateRect
39  * @tc.type: FUNC
40  */
41 HWTEST_F(AccessibilityUtilsTestNg, AccessibilityUtilsTest001, TestSize.Level1)
42 {
43     /**
44      * @tc.step1: create some Rect.
45      * @tc.expected: CheckBetterRect return false.
46      */
47     Rect nodeRect(5.0, 0.0, 15.0, 15.0);
48     Rect itemRect(0.0, 0.0, 10.0, 10.0);
49     Rect tempBest(0.0, 0.0, 10.0, 10.0);
50 
51     /**
52      * @tc.step2: rect not change and test direction left right up down.
53      * @tc.expected: CheckBetterRect always return false.
54      */
55     EXPECT_FALSE(CheckBetterRect(nodeRect, FOCUS_DIRECTION_LEFT, itemRect, tempBest));
56     EXPECT_FALSE(CheckBetterRect(nodeRect, FOCUS_DIRECTION_RIGHT, itemRect, tempBest));
57     EXPECT_FALSE(CheckBetterRect(nodeRect, FOCUS_DIRECTION_UP, itemRect, tempBest));
58     EXPECT_FALSE(CheckBetterRect(nodeRect, FOCUS_DIRECTION_DOWN, itemRect, tempBest));
59 
60     /**
61      * @tc.step3: test CheckBetterRect with unknown direction .
62      * @tc.expected: CheckBetterRect return false.
63      */
64     EXPECT_FALSE(CheckBetterRect(nodeRect, 0, tempBest, itemRect));
65 }
66 
67 /**
68  * @tc.name: accessibilityTest002
69  * @tc.desc: CheckBetterRect-OutrightBetter
70  * @tc.type: FUNC
71  */
72 HWTEST_F(AccessibilityUtilsTestNg, AccessibilityUtilsTest002, TestSize.Level1)
73 {
74     /**
75      * @tc.step1: create some Rect.
76      * @tc.expected: CheckBetterRect return false.
77      */
78     Rect nodeRect(5.0, 5.0, 15.0, 15.0);
79     Rect itemRect(0.0, 0.0, 10.0, 10.0);
80     Rect tempBest(0.0, 0.0, 10.0, 10.0);
81     EXPECT_FALSE(CheckBetterRect(nodeRect, FOCUS_DIRECTION_LEFT, itemRect, tempBest));
82     EXPECT_FALSE(CheckBetterRect(nodeRect, FOCUS_DIRECTION_UP, itemRect, tempBest));
83 
84     /**
85      * @tc.step2: change params, nodeRect now is [0.0, 0.0, 15.0, 15.0], itemRect now is [15.0, 20.0, 10.0, 10.0],
86      * @tc.step2: tempBest now is [15.0, 0.0 ,10.0 ,10.0].
87      * @tc.expected: when direction is FOCUS_DIRECTION_RIGHT return false and FOCUS_DIRECTION_DOWN return true.
88      */
89     nodeRect.SetLeft(0.0);
90     nodeRect.SetTop(0.0);
91     itemRect.SetLeft(15.0);
92     itemRect.SetTop(20.0);
93     tempBest.SetLeft(15.0);
94     EXPECT_FALSE(CheckBetterRect(nodeRect, FOCUS_DIRECTION_RIGHT, itemRect, tempBest));
95     EXPECT_TRUE(CheckBetterRect(nodeRect, FOCUS_DIRECTION_DOWN, itemRect, tempBest));
96 }
97 
98 /**
99  * @tc.name: accessibilityTest003
100  * @tc.desc: CheckBetterRect-IsCandidateRect
101  * @tc.type: FUNC
102  */
103 HWTEST_F(AccessibilityUtilsTestNg, AccessibilityUtilsTest003, TestSize.Level1)
104 {
105     /**
106      * @tc.step1: create some Rect.
107      * @tc.expected: CheckBetterRect return false.
108      */
109     Rect nodeRect(5.0, 0.0, 15.0, 15.0);
110     Rect itemRect(0.0, 0.0, 10.0, 10.0);
111     Rect tempBest(0.0, 0.0, 10.0, 10.0);
112     EXPECT_FALSE(CheckBetterRect(nodeRect, FOCUS_DIRECTION_LEFT, itemRect, tempBest));
113 
114     /**
115      * @tc.step2: change tempBest params, tempBest now is [10.0, 0.0, 10.0, 10.0].
116      * @tc.expected: CheckBetterRect return true.
117      */
118     tempBest.SetLeft(10.0);
119     EXPECT_TRUE(CheckBetterRect(nodeRect, FOCUS_DIRECTION_LEFT, itemRect, tempBest));
120 
121     /**
122      * @tc.step2: change nodeRect params, nodeRect now is [0.0, 0.0, 15.0, 15.0].
123      * @tc.expected: CheckBetterRect return false.
124      */
125     nodeRect.SetLeft(0.0);
126     EXPECT_FALSE(CheckBetterRect(nodeRect, FOCUS_DIRECTION_LEFT, itemRect, tempBest));
127 
128     /**
129      * @tc.step2: change params, itemRect now is [15.0, 20.0, 10.0, 10.0], tempBest now is [15.0, 0.0, 10.0, 10.0].
130      * @tc.expected: CheckBetterRect with direction FOCUS_DIRECTION_RIGHT meeting expectations.
131      */
132     itemRect.SetLeft(15.0);
133     itemRect.SetTop(20.0);
134     tempBest.SetLeft(15.0);
135     EXPECT_FALSE(CheckBetterRect(nodeRect, FOCUS_DIRECTION_RIGHT, itemRect, tempBest));
136     EXPECT_TRUE(CheckBetterRect(nodeRect, FOCUS_DIRECTION_RIGHT, tempBest, itemRect));
137 }
138 
139 /**
140  * @tc.name: AccessibilityUtilsTest004
141  * @tc.desc: Test the direction is FOCUS_DIRECTION_UP and FOCUS_DIRECTION_DOWN left remaining situation
142  * @tc.type: FUNC
143  */
144 HWTEST_F(AccessibilityUtilsTestNg, AccessibilityUtilsTest004, TestSize.Level1)
145 {
146     /**
147      * @tc.step1: create some Rect, itemRect and tempBest out nodeRect.
148      * @tc.expected: CheckBetterRect return false.
149      */
150     Rect nodeRect(10.0, 10.0, 10.0, 10.0);
151     Rect itemRect(5.0, 5.0, 5.0, 5.0);
152     Rect tempBest(5.0, 5.0, 5.0, 5.0);
153     EXPECT_FALSE(CheckBetterRect(nodeRect, FOCUS_DIRECTION_UP, itemRect, tempBest));
154 
155     /**
156      * @tc.step2: change itemRect params, itemRect now is [12.5, 0.0, 5.0, 5.0].
157      * @tc.expected: CheckBetterRect return false.
158      */
159     itemRect.SetLeft(12.5);
160     itemRect.SetTop(0.0);
161     EXPECT_FALSE(CheckBetterRect(nodeRect, FOCUS_DIRECTION_UP, itemRect, tempBest));
162 
163     /**
164      * @tc.step3: change params, itemRect now is [15.0, 5.0, 5.0, 5.0], tempBest now is [5.0, 5.0, 4.0, 6.0].
165      * @tc.expected: CheckBetterRect return true.
166      */
167     itemRect.SetLeft(15.0);
168     itemRect.SetTop(5.0);
169     tempBest.SetWidth(4.0);
170     tempBest.SetHeight(6.0);
171     EXPECT_TRUE(CheckBetterRect(nodeRect, FOCUS_DIRECTION_UP, itemRect, tempBest));
172 
173     /**
174      * @tc.step4: change tempBest params, tempBest now is [5.0, 5.0, 5.0, 5.0].
175      * @tc.expected: CheckBetterRect return true.
176      */
177     tempBest.SetHeight(5.0);
178     tempBest.SetWidth(5.0);
179     EXPECT_TRUE(CheckBetterRect(nodeRect, FOCUS_DIRECTION_UP, itemRect, tempBest));
180 
181     /**
182      * @tc.step5: change tempBest params, tempBest now is [5.0, 9.5, 5.0, 0.5].
183      * @tc.expected: CheckBetterRect return true.
184      */
185     tempBest.SetHeight(0.5);
186     tempBest.SetTop(9.5);
187     EXPECT_TRUE(CheckBetterRect(nodeRect, FOCUS_DIRECTION_UP, itemRect, tempBest));
188 
189     /**
190      * @tc.step5: change params, itemRect now is [15.0, 20.0, 5.0, 5.0],  tempBest now is [20.0, 20.0, 5.0, 0.5].
191      * @tc.expected: CheckBetterRect return true.
192      */
193     itemRect.SetTop(20.0);
194     tempBest.SetLeft(20.0);
195     tempBest.SetTop(20.0);
196     EXPECT_TRUE(CheckBetterRect(nodeRect, FOCUS_DIRECTION_DOWN, itemRect, tempBest));
197 }
198 
199 /**
200  * @tc.name: AccessibilityUtilsTest005
201  * @tc.desc: Verify direction is FOCUS_DIRECTION_LEFT and FOCUS_DIRECTION_RIGHT left remaining situation.
202  * @tc.type: FUNC
203  */
204 HWTEST_F(AccessibilityUtilsTestNg, AccessibilityUtilsTest005, TestSize.Level1)
205 {
206     /**
207      * @tc.step1: create some Rect, intersect itemRect and nodeRect, tempBest out nodeRect.
208      * @tc.expected: CheckBetterRect return true.
209      */
210     Rect nodeRect(10.0, 10.0, 10.0, 10.0);
211     Rect itemRect(5.0, 10.0, 5.0, 5.0);
212     Rect tempBest(5.0, 5.0, 5.0, 5.0);
213     EXPECT_TRUE(CheckBetterRect(nodeRect, FOCUS_DIRECTION_LEFT, itemRect, tempBest));
214 
215     /**
216      * @tc.step2: change params, itemRect now is [20.0, 20.0, 5.0, 5.0],  tempBest now is [20.0, 5.0, 5.0, 5.0].
217      * @tc.expected: CheckBetterRect return false.
218      */
219     itemRect.SetLeft(20.0);
220     itemRect.SetTop(20.0);
221     tempBest.SetLeft(20.0);
222     EXPECT_FALSE(CheckBetterRect(nodeRect, FOCUS_DIRECTION_RIGHT, itemRect, tempBest));
223 }
224 } // namespace OHOS::Ace::NG
225