• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "ui_model.h"
18 
19 using namespace OHOS::uitest;
20 using namespace std;
21 
TEST(RectAlgorithmTest,testCheckEqual)22 TEST(RectAlgorithmTest, testCheckEqual)
23 {
24     Rect rect(100, 200, 300, 400);
25     ASSERT_TRUE(RectAlgorithm::CheckEqual(rect, rect));
26     Rect rect1(0, 0, 0, 0);
27     ASSERT_FALSE(RectAlgorithm::CheckEqual(rect, rect1));
28 }
29 
TEST(RectAlgorithmTest,testRectIntersection)30 TEST(RectAlgorithmTest, testRectIntersection)
31 {
32     Rect rect0(100, 200, 300, 400);
33     Rect rect1(0, 100, 0, 100);
34     Rect rect2(200, 300, 400, 500);
35     Rect rect3(100, 150, 200, 350);
36     Rect rect4(150, 250, 350, 450);
37     Rect rect5(120, 180, 320, 380);
38 
39     Rect intersection {0, 0, 0, 0};
40     ASSERT_FALSE(RectAlgorithm::CheckIntersectant(rect0, rect1));
41     ASSERT_FALSE(RectAlgorithm::CheckIntersectant(rect0, rect2));
42     ASSERT_TRUE(RectAlgorithm::CheckIntersectant(rect0, rect3));
43     ASSERT_FALSE(RectAlgorithm::ComputeIntersection(rect0, rect1, intersection)); // no overlap
44     ASSERT_FALSE(RectAlgorithm::ComputeIntersection(rect0, rect2, intersection)); // no overlap
45     ASSERT_TRUE(RectAlgorithm::ComputeIntersection(rect0, rect3, intersection)); // x,y-overlap
46     ASSERT_EQ(100, intersection.left_);
47     ASSERT_EQ(150, intersection.right_);
48     ASSERT_EQ(300, intersection.top_);
49     ASSERT_EQ(350, intersection.bottom_);
50     intersection = {0, 0, 0, 0};
51     ASSERT_TRUE(RectAlgorithm::CheckIntersectant(rect0, rect4));
52     ASSERT_TRUE(RectAlgorithm::ComputeIntersection(rect0, rect4, intersection)); // x,y-overlap
53     ASSERT_EQ(150, intersection.left_);
54     ASSERT_EQ(200, intersection.right_);
55     ASSERT_EQ(350, intersection.top_);
56     ASSERT_EQ(400, intersection.bottom_);
57     intersection = {0, 0, 0, 0};
58     ASSERT_TRUE(RectAlgorithm::CheckIntersectant(rect0, rect5));
59     ASSERT_TRUE(RectAlgorithm::ComputeIntersection(rect0, rect5, intersection)); // fully contained
60     ASSERT_EQ(120, intersection.left_);
61     ASSERT_EQ(180, intersection.right_);
62     ASSERT_EQ(320, intersection.top_);
63     ASSERT_EQ(380, intersection.bottom_);
64 }
65 
TEST(RectAlgorithmTest,testPointInRect)66 TEST(RectAlgorithmTest, testPointInRect)
67 {
68     auto rect = Rect(10, 20, 10, 20);
69     // x not in section
70     ASSERT_FALSE(RectAlgorithm::IsInnerPoint(rect, Point(5, 15)));
71     ASSERT_FALSE(RectAlgorithm::IsInnerPoint(rect, Point(10, 15)));
72     ASSERT_FALSE(RectAlgorithm::IsInnerPoint(rect, Point(25, 15)));
73     ASSERT_FALSE(RectAlgorithm::IsInnerPoint(rect, Point(20, 15)));
74     // y not in section
75     ASSERT_FALSE(RectAlgorithm::IsInnerPoint(rect, Point(15, 5)));
76     ASSERT_FALSE(RectAlgorithm::IsInnerPoint(rect, Point(15, 10)));
77     ASSERT_FALSE(RectAlgorithm::IsInnerPoint(rect, Point(15, 25)));
78     ASSERT_FALSE(RectAlgorithm::IsInnerPoint(rect, Point(15, 20)));
79     // x and y not in section
80     ASSERT_FALSE(RectAlgorithm::IsInnerPoint(rect, Point(5, 5)));
81     ASSERT_FALSE(RectAlgorithm::IsInnerPoint(rect, Point(25, 25)));
82     // x and y in section
83     ASSERT_TRUE(RectAlgorithm::IsInnerPoint(rect, Point(15, 15)));
84 }
85 
TEST(RectAlgorithmTest,testPointOnRectEdge)86 TEST(RectAlgorithmTest, testPointOnRectEdge)
87 {
88     auto rect = Rect(10, 20, 10, 20);
89     // on corner
90     ASSERT_TRUE(RectAlgorithm::IsPointOnEdge(rect, Point(10, 10)));
91     ASSERT_TRUE(RectAlgorithm::IsPointOnEdge(rect, Point(10, 20)));
92     ASSERT_TRUE(RectAlgorithm::IsPointOnEdge(rect, Point(20, 10)));
93     ASSERT_TRUE(RectAlgorithm::IsPointOnEdge(rect, Point(20, 20)));
94     // on edge
95     ASSERT_TRUE(RectAlgorithm::IsPointOnEdge(rect, Point(10, 15)));
96     ASSERT_TRUE(RectAlgorithm::IsPointOnEdge(rect, Point(15, 10)));
97     ASSERT_TRUE(RectAlgorithm::IsPointOnEdge(rect, Point(20, 15)));
98     ASSERT_TRUE(RectAlgorithm::IsPointOnEdge(rect, Point(15, 20)));
99     // in rect
100     ASSERT_FALSE(RectAlgorithm::IsPointOnEdge(rect, Point(15, 15)));
101     // out of rect
102     ASSERT_FALSE(RectAlgorithm::IsPointOnEdge(rect, Point(5, 10)));
103     ASSERT_FALSE(RectAlgorithm::IsPointOnEdge(rect, Point(25, 10)));
104     ASSERT_FALSE(RectAlgorithm::IsPointOnEdge(rect, Point(10, 5)));
105     ASSERT_FALSE(RectAlgorithm::IsPointOnEdge(rect, Point(10, 25)));
106     ASSERT_FALSE(RectAlgorithm::IsPointOnEdge(rect, Point(5, 15)));
107     ASSERT_FALSE(RectAlgorithm::IsPointOnEdge(rect, Point(25, 15)));
108     ASSERT_FALSE(RectAlgorithm::IsPointOnEdge(rect, Point(15, 5)));
109     ASSERT_FALSE(RectAlgorithm::IsPointOnEdge(rect, Point(15, 25)));
110 }
111 
TEST(RectAlgorithmTest,computeVisibleRegion)112 TEST(RectAlgorithmTest, computeVisibleRegion)
113 {
114     //  ______
115     // |      |
116     // |  ++++|++++++
117     // |  + ..|...  +
118     // |__+_:_| _:__+____
119     //    + :..|.:  +    |
120     //    +    |    +    |
121     //    +++++|+++++    |
122     //         |_________|
123     Rect rect(100, 200, 100, 200);
124     vector<Rect> overlaySet = {Rect(0, 140, 0, 140), Rect(160, 250, 160, 250), Rect(120, 160, 120, 160)};
125     auto region = Rect(0, 0, 0, 0);
126     constexpr size_t rounds = 1E5;
127     constexpr size_t middle = rounds / 2;
128     vector<uint32_t> elapseUs;
129     for (size_t idx = 0; idx < rounds; idx++) {
130         const auto micros0 = GetCurrentMicroseconds();
131         ASSERT_TRUE(RectAlgorithm::ComputeMaxVisibleRegion(rect, overlaySet, region));
132         const auto micros1 = GetCurrentMicroseconds();
133         elapseUs.push_back(micros1 - micros0);
134     }
135     // check result
136     auto expectedVisibleRegion = Rect(100, 160, 160, 200);
137     ASSERT_TRUE(RectAlgorithm::CheckEqual(region, expectedVisibleRegion));
138     // check elapse (use median), should not exceed baseline: 10us
139     constexpr uint32_t epalseBaseline = 10;
140     sort(elapseUs.begin(), elapseUs.end());
141     ASSERT_LE(elapseUs.at(middle), epalseBaseline);
142 }
143 
TEST(RectAlgorithmTest,computeVisibleRegionSpecialCases)144 TEST(RectAlgorithmTest, computeVisibleRegionSpecialCases)
145 {
146     //   same-to-overlay   fully-covered      not-covered       fully-covered 2      not-covered-2
147     //                     _____________       __________       _____________        _______
148     //    +++++++++++     | +++++++++++ |     |__________|     | ++++++++++++|      |_______|
149     //    +         +     | +         + |     ++++++++++++     |_+__________+|_      +++++++++
150     //    +++++++++++     | +++++++++++ |     +          +      |++++++++++++  |     +++++++++__
151     //                    |_____________|     ++++++++++++      |______________|       |________|
152     //
153     Rect rect(100, 200, 100, 200);
154     const vector<vector<Rect>> overlaySet = {
155         {Rect(100, 200, 100, 200)},
156         {Rect(0, 300, 0, 300)},
157         {Rect(100, 200, 0, 100)},
158         {Rect(0, 200, 0, 150), Rect(100, 300, 150, 300)},
159         {Rect(50, 250, 0, 100), Rect(150, 300, 200, 300)}
160     };
161     const vector<Rect> expectedVisibleRegion = {
162         {Rect(0, 0, 0, 0)},
163         {Rect(0, 0, 0, 0)},
164         {Rect(100, 200, 100, 200)},
165         {Rect(0, 0, 0, 0)},
166         {Rect(100, 200, 100, 200)}
167     };
168 
169     for (size_t idx = 0; idx < 5; idx++) {
170         auto region = Rect(0, 0, 0, 0);
171         const auto visible = RectAlgorithm::ComputeMaxVisibleRegion(rect, overlaySet.at(idx), region);
172         const auto& expectedRegion = expectedVisibleRegion.at(idx);
173         ASSERT_TRUE(RectAlgorithm::CheckEqual(region, expectedRegion));
174         ASSERT_EQ(expectedRegion.GetHeight() > 0 && expectedRegion.GetWidth() > 0, visible);
175     }
176 }