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/list_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 ListLayoutTest : public testing::Test {
36 public:
37 static void SetUpTestCase(void);
38 static void TearDownTestCase(void);
39 static ListLayout* listLayout_;
40 };
41
42 ListLayout* ListLayoutTest::listLayout_ = nullptr;
43
SetUpTestCase(void)44 void ListLayoutTest::SetUpTestCase(void)
45 {
46 if (listLayout_ == nullptr) {
47 listLayout_ = new ListLayout();
48 }
49 }
50
TearDownTestCase(void)51 void ListLayoutTest::TearDownTestCase(void)
52 {
53 if (listLayout_ != nullptr) {
54 delete listLayout_;
55 listLayout_ = nullptr;
56 }
57 }
58
59 /**
60 * @tc.name: ListLayoutSetDirection_001
61 * @tc.desc: Verify SetDirection function, equal.
62 * @tc.type: FUNC
63 * @tc.require: AR000DSMR7
64 */
65 HWTEST_F(ListLayoutTest, ListLayoutSetDirection_001, TestSize.Level0)
66 {
67 if (listLayout_ == nullptr) {
68 EXPECT_EQ(1, 0);
69 return;
70 }
71 EXPECT_EQ(listLayout_->GetDirection(), 1);
72
73 listLayout_->SetDirection(0);
74 EXPECT_EQ(listLayout_->GetDirection(), 0);
75 }
76
77 /**
78 * @tc.name: ListLayout_001
79 * @tc.desc: Normal Process.
80 * @tc.type: FUNC
81 * @tc.require: AR000DSMR7
82 */
83 HWTEST_F(ListLayoutTest, ListLayout_001, TestSize.Level1)
84 {
85 if (listLayout_ == nullptr) {
86 EXPECT_EQ(1, 0);
87 return;
88 }
89 listLayout_->Resize(600, 300); // 600: layout width; 300: layout height
90 EXPECT_EQ(listLayout_->GetHeight(), 300); // 300: set layout height
91 listLayout_->SetDirection(ListLayout::VERTICAL);
92 auto view1 = CreatView();
93 listLayout_->Add(view1);
94 auto view2 = CreatView();
95 listLayout_->Add(view2);
96 EXPECT_EQ(view1->GetX(), 250); // 250: view x after layout
97 EXPECT_EQ(view1->GetY(), 0);
98 EXPECT_EQ(view2->GetX(), 250); // 250: view x after layout
99 EXPECT_EQ(view2->GetY(), 100); // 100: view y after layout
100 EXPECT_EQ(listLayout_->GetHeight(), 200); // 200: Sum view height
101 listLayout_->RemoveAll();
102 delete view1;
103 delete view2;
104 }
105
106 /**
107 * @tc.name: ListLayout_002
108 * @tc.desc: Normal Process.
109 * @tc.type: FUNC
110 * @tc.require: AR000DSMR7
111 */
112 HWTEST_F(ListLayoutTest, ListLayout_002, TestSize.Level1)
113 {
114 if (listLayout_ == nullptr) {
115 EXPECT_EQ(1, 0);
116 return;
117 }
118 listLayout_->Resize(600, 300); // 600: layout width; 300: layout height
119 EXPECT_EQ(listLayout_->GetWidth(), 600); // 600: set layout width
120 listLayout_->SetDirection(ListLayout::HORIZONTAL);
121 auto view1 = CreatView();
122 listLayout_->Add(view1);
123 auto view2 = CreatView();
124 listLayout_->Add(view2);
125 EXPECT_EQ(view1->GetX(), 0);
126 EXPECT_EQ(view1->GetY(), 100); // 100: view y after layout
127 EXPECT_EQ(view2->GetX(), 100); // 100: view x after layout
128 EXPECT_EQ(view2->GetY(), 100); // 100: view y after layout
129 EXPECT_EQ(listLayout_->GetWidth(), 200); // 200: Sum view height
130 listLayout_->RemoveAll();
131 delete view1;
132 delete view2;
133 }
134 } // namespace OHOS