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 "components/ui_picker.h"
17 #include <climits>
18 #include <gtest/gtest.h>
19 #include "gfx_utils/style.h"
20 
21 using namespace testing::ext;
22 namespace OHOS {
23 class UIPickerTest : public testing::Test {
24 public:
25     static void SetUpTestCase(void);
26     static void TearDownTestCase(void);
27     static UIPicker* picker_;
28 };
29 
30 UIPicker* UIPickerTest::picker_ = nullptr;
31 
SetUpTestCase(void)32 void UIPickerTest::SetUpTestCase(void)
33 {
34     if (picker_ == nullptr) {
35         picker_ = new UIPicker();
36     }
37 }
38 
TearDownTestCase(void)39 void UIPickerTest::TearDownTestCase(void)
40 {
41     if (picker_ != nullptr) {
42         delete picker_;
43         picker_ = nullptr;
44     }
45 }
46 
47 /**
48  * @tc.name: UIPickerGetViewType_001
49  * @tc.desc: Verify GetViewType function, equal.
50  * @tc.type: FUNC
51  * @tc.require: AR000EEMQ6
52  */
53 HWTEST_F(UIPickerTest, UIPickerGetViewType_001, TestSize.Level1)
54 {
55     if (picker_ == nullptr) {
56         EXPECT_EQ(1, 0);
57         return;
58     }
59     EXPECT_EQ(picker_->GetViewType(), UI_PICKER);
60 }
61 
62 /**
63  * @tc.name: UIPickerGetHighlightFontId_001
64  * @tc.desc: Verify GetHighlightFontId function, equal.
65  * @tc.type: FUNC
66  * @tc.require: AR000EEMQ6
67  */
68 HWTEST_F(UIPickerTest, UIPickerGetHighlightFontId_001, TestSize.Level1)
69 {
70     if (picker_ == nullptr) {
71         EXPECT_EQ(1, 0);
72         return;
73     }
74     uint8_t highlightFontId = StyleDefault::GetPickerHighlightStyle().font_;
75     EXPECT_EQ(picker_->GetHighlightFontId(), highlightFontId);
76 }
77 
78 /**
79  * @tc.name: UIPickerSetFontId_001
80  * @tc.desc: Verify SetFontId function, equal.
81  * @tc.type: FUNC
82  * @tc.require: AR000EEMQ6
83  */
84 HWTEST_F(UIPickerTest, UIPickerSetFontId_001, TestSize.Level1)
85 {
86     if (picker_ == nullptr) {
87         EXPECT_EQ(1, 0);
88         return;
89     }
90     const uint8_t backgroundFontId = 16;
91     const uint8_t highlightFontId = 18;
92 
93     picker_->SetFontId(backgroundFontId, highlightFontId);
94     EXPECT_EQ(picker_->GetBackgroundFontId(), backgroundFontId);
95     EXPECT_EQ(picker_->GetHighlightFontId(), highlightFontId);
96 }
97 
98 /**
99  * @tc.name: UIPickerSetTextColor_001
100  * @tc.desc: Verify SetTextColor function, equal.
101  * @tc.type: FUNC
102  * @tc.require: AR000EEMQ6
103  */
104 HWTEST_F(UIPickerTest, UIPickerSetTextColor_001, TestSize.Level1)
105 {
106     if (picker_ == nullptr) {
107         EXPECT_EQ(1, 0);
108         return;
109     }
110     ColorType backgroundColor;
111     ColorType highlightColor;
112     backgroundColor.alpha = OPA_OPAQUE;
113     highlightColor.alpha = OPA_OPAQUE;
114 
115     picker_->SetTextColor(backgroundColor, highlightColor);
116     EXPECT_EQ(picker_->GetBackgroundTextColor().alpha, backgroundColor.alpha);
117     EXPECT_EQ(picker_->GetHighlightTextColor().alpha, highlightColor.alpha);
118 }
119 
120 /**
121  * @tc.name: UIPickerSetWidth_001
122  * @tc.desc: Verify SetWidth function, equal.
123  * @tc.type: FUNC
124  * @tc.require: AR000EEMQ6
125  */
126 HWTEST_F(UIPickerTest, UIPickerSetWidth_001, TestSize.Level1)
127 {
128     if (picker_ == nullptr) {
129         EXPECT_EQ(1, 0);
130         return;
131     }
132     const int16_t width = 10;
133     const int16_t height = 20;
134 
135     picker_->SetWidth(width);
136     picker_->SetHeight(height);
137     EXPECT_EQ(picker_->GetWidth(), width);
138     EXPECT_EQ(picker_->GetHeight(), height);
139 }
140 
141 
142 /**
143  * @tc.name: UIPickerSetSelected_001
144  * @tc.desc: Verify SetSelected function, equal.
145  * @tc.type: FUNC
146  * @tc.require: AR000EEMQ6
147  */
148 HWTEST_F(UIPickerTest, UIPickerSetSelected_001, TestSize.Level0)
149 {
150     if (picker_ == nullptr) {
151         EXPECT_EQ(1, 0);
152         return;
153     }
154     const uint16_t index = 30;
155     const int16_t itemHeight = 10;
156     const int16_t width = 100;
157     const int16_t height = 100;
158     const int16_t startValue= 0;
159     const int16_t endValue = 100;
160     picker_->SetPosition(0, 0, width, height);
161     picker_->SetItemHeight(itemHeight);
162     picker_->SetValues(startValue, endValue);
163     const char* value[1];
164 
165     value[0] = "abc";
166     picker_->SetValues(value, 1);
167     picker_->SetValues(startValue, endValue);
168     picker_->SetSelected(index);
169     EXPECT_EQ(picker_->GetSelected(), index);
170 }
171 }
172