1 /*
2 * Copyright (c) 2020-2021 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 "layout/grid_layout.h"
17
18 #include <climits>
19 #include <gtest/gtest.h>
20
21 using namespace testing::ext;
22 namespace OHOS {
23 namespace {
CreatView()24 UIView* CreatView()
25 {
26 uint16_t width = 100; // 100 view width
27 uint16_t height = 100; // 100 view height
28 auto view = new UIView();
29 view->Resize(width, height);
30 EXPECT_EQ(view->GetX(), 0);
31 EXPECT_EQ(view->GetY(), 0);
32 return view;
33 }
34 }
35 class GridLayoutTest : public testing::Test {
36 public:
37 static void SetUpTestCase(void);
38 static void TearDownTestCase(void);
39 static GridLayout* gridLayout_;
40 };
41
42 GridLayout* GridLayoutTest::gridLayout_ = nullptr;
43
SetUpTestCase(void)44 void GridLayoutTest::SetUpTestCase(void)
45 {
46 if (gridLayout_ == nullptr) {
47 gridLayout_ = new GridLayout();
48 gridLayout_->SetPosition(0, 0, 600, 300); // 600: layout width; 300: layout height
49 gridLayout_->SetRows(2); // 2: row
50 gridLayout_->SetCols(3); // 3: col
51 }
52 }
53
TearDownTestCase(void)54 void GridLayoutTest::TearDownTestCase(void)
55 {
56 if (gridLayout_ != nullptr) {
57 delete gridLayout_;
58 gridLayout_ = nullptr;
59 }
60 }
61
62 /**
63 * @tc.name: GridLayout_001
64 * @tc.desc: Normal Process.
65 * @tc.type: FUNC
66 * @tc.require: AR000DSMR7
67 */
68 HWTEST_F(GridLayoutTest, GridLayout_001, TestSize.Level1)
69 {
70 if (gridLayout_ == nullptr) {
71 EXPECT_EQ(1, 0);
72 return;
73 }
74 /*
75 * 1 2 3
76 * 4 5 6
77 */
78 gridLayout_->SetLayoutDirection(LAYOUT_HOR);
79 auto view1 = CreatView();
80 gridLayout_->Add(view1);
81 auto view2 = CreatView();
82 gridLayout_->Add(view2);
83 auto view3 = CreatView();
84 gridLayout_->Add(view3);
85 auto view4 = CreatView();
86 gridLayout_->Add(view4);
87 gridLayout_->LayoutChildren();
88 EXPECT_EQ(view1->GetX(), 50); // 50: view x after layout
89 EXPECT_EQ(view1->GetY(), 25); // 25: view y after layout
90 EXPECT_EQ(view2->GetX(), 250); // 250: view x after layout
91 EXPECT_EQ(view2->GetY(), view1->GetY());
92 EXPECT_EQ(view3->GetX(), 450); // 450: view x after layout
93 EXPECT_EQ(view3->GetY(), view1->GetY());
94 EXPECT_EQ(view4->GetX(), view1->GetX());
95 EXPECT_EQ(view4->GetY(), 175); // 175: view x after layout
96 gridLayout_->RemoveAll();
97 delete view1;
98 delete view2;
99 delete view3;
100 delete view4;
101 }
102
103 /**
104 * @tc.name: GridLayout_002
105 * @tc.desc: Normal Process.
106 * @tc.type: FUNC
107 * @tc.require: AR000DSMR7
108 */
109 HWTEST_F(GridLayoutTest, GridLayout_002, TestSize.Level1)
110 {
111 if (gridLayout_ == nullptr) {
112 EXPECT_EQ(1, 0);
113 return;
114 }
115 /*
116 * 1 3 5
117 * 2 4 6
118 */
119 gridLayout_->SetLayoutDirection(LAYOUT_HOR_R);
120 auto view1 = CreatView();
121 gridLayout_->Add(view1);
122 auto view2 = CreatView();
123 gridLayout_->Add(view2);
124 auto view3 = CreatView();
125 gridLayout_->Add(view3);
126 auto view4 = CreatView();
127 gridLayout_->Add(view4);
128 gridLayout_->LayoutChildren();
129 EXPECT_EQ(view1->GetX(), 50); // 50: view x after layout
130 EXPECT_EQ(view1->GetY(), 25); // 25: view y after layout
131 EXPECT_EQ(view2->GetX(), view1->GetX());
132 EXPECT_EQ(view2->GetY(), 175); // 175: view y afert layout
133 EXPECT_EQ(view3->GetX(), 250); // 250: view x after layout
134 EXPECT_EQ(view3->GetY(), view1->GetY());
135 EXPECT_EQ(view4->GetX(), view3->GetX());
136 EXPECT_EQ(view4->GetY(), view2->GetY());
137 gridLayout_->RemoveAll();
138 delete view1;
139 delete view2;
140 delete view3;
141 delete view4;
142 }
143
144 /**
145 * @tc.name: GridLayout_003
146 * @tc.desc: Normal Process.
147 * @tc.type: FUNC
148 * @tc.require: AR000DSMR7
149 */
150 HWTEST_F(GridLayoutTest, GridLayout_003, TestSize.Level1)
151 {
152 if (gridLayout_ == nullptr) {
153 EXPECT_EQ(1, 0);
154 return;
155 }
156 /*
157 * 1 3 5
158 * 2 4 6
159 */
160 gridLayout_->SetLayoutDirection(LAYOUT_VER);
161 auto view1 = CreatView();
162 gridLayout_->Add(view1);
163 auto view2 = CreatView();
164 gridLayout_->Add(view2);
165 auto view3 = CreatView();
166 gridLayout_->Add(view3);
167 auto view4 = CreatView();
168 gridLayout_->Add(view4);
169 gridLayout_->LayoutChildren();
170 EXPECT_EQ(view1->GetX(), 50); // 50: view x after layout
171 EXPECT_EQ(view1->GetY(), 25); // 25: view y after layout
172 EXPECT_EQ(view2->GetX(), view1->GetX());
173 EXPECT_EQ(view2->GetY(), 175); // 175: view y afert layout
174 EXPECT_EQ(view3->GetX(), 250); // 250: view x after layout
175 EXPECT_EQ(view3->GetY(), view1->GetY());
176 EXPECT_EQ(view4->GetX(), view3->GetX());
177 EXPECT_EQ(view4->GetY(), view2->GetY());
178 gridLayout_->RemoveAll();
179 delete view1;
180 delete view2;
181 delete view3;
182 delete view4;
183 }
184
185 /**
186 * @tc.name: GridLayout_004
187 * @tc.desc: Normal Process.
188 * @tc.type: FUNC
189 * @tc.require: AR000DSMR7
190 */
191 HWTEST_F(GridLayoutTest, GridLayout_004, TestSize.Level1)
192 {
193 if (gridLayout_ == nullptr) {
194 EXPECT_EQ(1, 0);
195 return;
196 }
197 /*
198 * 1 2 3
199 * 4 5 6
200 */
201 gridLayout_->SetLayoutDirection(LAYOUT_VER_R);
202 auto view1 = CreatView();
203 gridLayout_->Add(view1);
204 auto view2 = CreatView();
205 gridLayout_->Add(view2);
206 auto view3 = CreatView();
207 gridLayout_->Add(view3);
208 auto view4 = CreatView();
209 gridLayout_->Add(view4);
210 gridLayout_->LayoutChildren();
211 EXPECT_EQ(view1->GetX(), 50); // 50: view x after layout
212 EXPECT_EQ(view1->GetY(), 25); // 25: view y after layout
213 EXPECT_EQ(view2->GetX(), view1->GetX());
214 EXPECT_EQ(view2->GetY(), 175); // 175: view y afert layout
215 EXPECT_EQ(view3->GetX(), 250); // 250: view x after layout
216 EXPECT_EQ(view3->GetY(), view1->GetY());
217 EXPECT_EQ(view4->GetX(), view3->GetX());
218 EXPECT_EQ(view4->GetY(), view2->GetY());
219 gridLayout_->RemoveAll();
220 delete view1;
221 delete view2;
222 delete view3;
223 delete view4;
224 }
225 } // namespace OHOS
226