1 /*
2 * Copyright (c) 2021-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
18 #include "core/components/common/layout/constants.h"
19 #include "core/components/theme/theme_manager_impl.h"
20 #include "frameworks/bridge/common/dom/dom_document.h"
21 #include "frameworks/bridge/common/dom/dom_swiper.h"
22 #include "frameworks/bridge/test/unittest/jsfrontend/dom_node_factory.h"
23
24 using namespace testing;
25 using namespace testing::ext;
26
27 namespace OHOS::Ace::Framework {
28 namespace {
29
30 constexpr uint32_t TEST_INDEX_VALUE = 0;
31 constexpr double TEST_INTERVAL_VALUE = 2000.0;
32 constexpr double TEST_DURATION_VALUE = 500.0;
33 constexpr double TEST_INDICATOR_SIZE = 10.0;
34 constexpr double TEST_INDICATOR_TOP = 20.0;
35 constexpr double TEST_INDICATOR_LEFT = 20.0;
36 constexpr double TEST_INDICATOR_RIGHT = 20.0;
37 constexpr double TEST_INDICATOR_BOTTOM = 20.0;
38 const std::string TEST_INDICATOR_COLOR = "#12345678";
39 const std::string TEST_INDICATOR_SELECT_COLOR = "#87654321";
40 constexpr int32_t TEST_AXIS_VALUE = 0;
41 constexpr int32_t TEST_CHILDREN_SIZE = 2;
42 constexpr uint32_t TEST_INDEX_VALUE_004 = 3;
43 constexpr uint32_t DEFAULT_INDEX_VALUE = 0;
44 constexpr double DEFAULT_INTERVAL_VALUE = 3000.0;
45 constexpr int32_t DEFAULT_AXIS_VALUE = 1;
46
47 const std::string SWIPER_TEST_001 = ""
48 "{ "
49 " \"tag\": \"swiper\", "
50 " \"attr\": [{ "
51 " \"index\" : \"0\" "
52 " }, "
53 " {"
54 " \"duration\" : \"500\" "
55 " }, "
56 " {"
57 " \"loop\" : \"false\" "
58 " }, "
59 " {"
60 " \"autoplay\" : \"true\" "
61 " }, "
62 " {"
63 " \"interval\" : \"2000\" "
64 " }, "
65 " {"
66 " \"vertical\" : \"true\" "
67 " }, "
68 " {"
69 " \"indicator\" : \"true\" "
70 " }], "
71 " \"style\": [{ "
72 " \"indicatorColor\" : \"#12345678\" "
73 " }, "
74 " { "
75 " \"indicatorSelectedColor\" : \"#87654321\" "
76 " }, "
77 " {"
78 " \"indicatorSize\" : \"10\" "
79 " }, "
80 " {"
81 " \"indicatorTop\" : \"20\" "
82 " }, "
83 " {"
84 " \"indicatorLeft\" : \"20\" "
85 " }, "
86 " {"
87 " \"indicatorRight\" : \"20\" "
88 " }, "
89 " {"
90 " \"indicatorBottom\" : \"20\" "
91 " }]"
92 "}";
93
94 const std::string SWIPER_TEST_002 = ""
95 "{ "
96 " \"tag\": \"swiper\", "
97 " \"attr\" : [{"
98 " \"indicator\" : \"false\" "
99 " }] "
100 "}";
101
102 const std::string SWIPER_TEST_003 = ""
103 "{ "
104 " \"tag\": \"swiper\", "
105 " \"child\": [{ "
106 " \"tag\":\"text\" "
107 " },"
108 " { "
109 " \"tag\":\"text\" "
110 " }] "
111 "}";
112
113 const std::string SWIPER_TEST_004 = ""
114 "{ "
115 " \"tag\": \"swiper\", "
116 " \"attr\" : [{ "
117 " \"index\" : \"3\" "
118 " }], "
119 " \"child\": [{ "
120 " \"tag\":\"text\" "
121 " },"
122 " { "
123 " \"tag\":\"text\" "
124 " },"
125 " { "
126 " \"tag\":\"text\" "
127 " }] "
128 "}";
129
130 const std::string SWIPER_TEST_005 = ""
131 "{ "
132 " \"tag\": \"swiper\" "
133 "}";
134
135 const std::string SWIPER_TEST_006 = ""
136 "{ "
137 " \"tag\": \"swiper\", "
138 " \"event\": [ \"change\" ] "
139 "}";
140
141 } // namespace
142
143 class DomSwiperTest : public testing::Test {
144 public:
145 static void SetUpTestCase();
146 static void TearDownTestCase();
147 void SetUp();
148 void TearDown();
149 };
150
SetUpTestCase()151 void DomSwiperTest::SetUpTestCase() {}
TearDownTestCase()152 void DomSwiperTest::TearDownTestCase() {}
SetUp()153 void DomSwiperTest::SetUp() {}
TearDown()154 void DomSwiperTest::TearDown() {}
155
156 /**
157 * @tc.name: DomSwiperCreatorTest001
158 * @tc.desc: Test swiper node and child swiper component are created as expected.
159 * @tc.type: FUNC
160 */
161 HWTEST_F(DomSwiperTest, DomSwiperCreatorTest001, TestSize.Level1)
162 {
163 /**
164 * @tc.steps: step1. Construct string with right fields, then create swiper node with it.
165 * @tc.expected: step1. Swiper node and child swiper component are created successfully.
166 */
167 auto swiper = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(SWIPER_TEST_001);
168 auto boxChild = DOMNodeFactory::GetInstance().GetBoxChildComponent(swiper);
169 RefPtr<SwiperComponent> swiperComponent = AceType::DynamicCast<SwiperComponent>(boxChild->GetChild());
170 ASSERT_TRUE(swiperComponent != nullptr);
171
172 /**
173 * @tc.steps: step2. Check styles and attributes of created swiper node.
174 * @tc.expected: step2. The styles and attributes are as expected.
175 */
176 EXPECT_EQ(swiperComponent->GetIndex(), TEST_INDEX_VALUE);
177 EXPECT_TRUE(swiperComponent->IsAutoPlay());
178 EXPECT_TRUE(NearEqual(swiperComponent->GetAutoPlayInterval(), TEST_INTERVAL_VALUE));
179 EXPECT_FALSE(swiperComponent->IsLoop());
180 EXPECT_TRUE(NearEqual(swiperComponent->GetDuration(), TEST_DURATION_VALUE));
181 auto indicator = swiperComponent->GetIndicator();
182 EXPECT_TRUE(indicator);
183 if (indicator) {
184 EXPECT_TRUE(NearEqual(indicator->GetSize().Value(), TEST_INDICATOR_SIZE));
185 EXPECT_TRUE(NearEqual(indicator->GetTop().Value(), TEST_INDICATOR_TOP));
186 EXPECT_TRUE(NearEqual(indicator->GetLeft().Value(), TEST_INDICATOR_LEFT));
187 EXPECT_TRUE(NearEqual(indicator->GetBottom().Value(), TEST_INDICATOR_BOTTOM));
188 EXPECT_TRUE(NearEqual(indicator->GetRight().Value(), TEST_INDICATOR_RIGHT));
189 EXPECT_EQ(indicator->GetColor(), Color::FromString(TEST_INDICATOR_COLOR));
190 EXPECT_EQ(indicator->GetSelectedColor(), Color::FromString(TEST_INDICATOR_SELECT_COLOR));
191 }
192 EXPECT_EQ(static_cast<int32_t>(swiperComponent->GetAxis()), TEST_AXIS_VALUE);
193 }
194
195 /**
196 * @tc.name: DomSwiperCreatorTest002
197 * @tc.desc: Test set indicator attribute to swiper component successfully.
198 * @tc.type: FUNC
199 */
200 HWTEST_F(DomSwiperTest, DomSwiperCreatorTest002, TestSize.Level1)
201 {
202 /**
203 * @tc.steps: step1. Construct string with right fields, then create swiper node with it.
204 * @tc.expected: step1. Swiper node and child swiper component are created successfully.
205 */
206 auto swiper = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(SWIPER_TEST_002);
207 auto boxChild = DOMNodeFactory::GetInstance().GetBoxChildComponent(swiper);
208 RefPtr<SwiperComponent> swiperComponent = AceType::DynamicCast<SwiperComponent>(boxChild->GetChild());
209 ASSERT_TRUE(swiperComponent != nullptr);
210
211 /**
212 * @tc.steps: step2. Check indicator value of swiper component.
213 * @tc.expected: step2. The indicator value of swiper component is as expected.
214 */
215 EXPECT_TRUE(swiperComponent->GetIndicator() == nullptr);
216 }
217
218 /**
219 * @tc.name: DomSwiperCreatorTest003
220 * @tc.desc: Test append child to swiper component successfully.
221 * @tc.type: FUNC
222 */
223 HWTEST_F(DomSwiperTest, DomSwiperCreatorTest003, TestSize.Level1)
224 {
225 /**
226 * @tc.steps: step1. Construct string with right fields, then create swiper node with it.
227 * @tc.expected: step1. Swiper node and child swiper component are created successfully.
228 */
229 auto swiper = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(SWIPER_TEST_003);
230 auto boxChild = DOMNodeFactory::GetInstance().GetBoxChildComponent(swiper);
231 RefPtr<SwiperComponent> swiperComponent = AceType::DynamicCast<SwiperComponent>(boxChild->GetChild());
232 ASSERT_TRUE(swiperComponent != nullptr);
233
234 /**
235 * @tc.steps: step2. Check child number of created swiper component.
236 * @tc.expected: step2. The child number of swiper component is as expected.
237 */
238 EXPECT_TRUE(swiperComponent->GetChildren().size() == TEST_CHILDREN_SIZE);
239 }
240
241 /**
242 * @tc.name: DomSwiperCreatorTest004
243 * @tc.desc: Test set index attribute to swiper component successfully.
244 * @tc.type: FUNC
245 */
246 HWTEST_F(DomSwiperTest, DomSwiperCreatorTest004, TestSize.Level1)
247 {
248 /**
249 * @tc.steps: step1. Construct string include index attribute, then create swiper node with it.
250 * @tc.expected: step1. Swiper node and child swiper component are created successfully.
251 */
252 auto swiper = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(SWIPER_TEST_004);
253 auto boxChild = DOMNodeFactory::GetInstance().GetBoxChildComponent(swiper);
254 RefPtr<SwiperComponent> swiperComponent = AceType::DynamicCast<SwiperComponent>(boxChild->GetChild());
255 ASSERT_TRUE(swiperComponent != nullptr);
256
257 /**
258 * @tc.steps: step2. Check index value of created swiper node.
259 * @tc.expected: step2. The index value of swiper node is as expected.
260 */
261 EXPECT_TRUE(swiperComponent->GetIndex() == TEST_INDEX_VALUE_004);
262 }
263
264 /**
265 * @tc.name: DomSwiperCreatorTest005
266 * @tc.desc: Test swiper node and swiper component created successfully with empty input of style and attribute.
267 * @tc.type: FUNC
268 */
269 HWTEST_F(DomSwiperTest, DomSwiperCreatorTest005, TestSize.Level1)
270 {
271 /**
272 * @tc.steps: step1. Construct string without style and attribute, then create swiper node with it .
273 * @tc.expected: step1. Swiper node and child swiper component are created successfully.
274 */
275 auto swiper = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(SWIPER_TEST_005);
276 auto boxChild = DOMNodeFactory::GetInstance().GetBoxChildComponent(swiper);
277 RefPtr<SwiperComponent> swiperComponent = AceType::DynamicCast<SwiperComponent>(boxChild->GetChild());
278 ASSERT_TRUE(swiperComponent != nullptr);
279
280 /**
281 * @tc.steps: step2. Check styles and attributes of created swiper node.
282 * @tc.expected: step2. Swiper node's attrs and styles are initialized with default value.
283 */
284 EXPECT_EQ(swiperComponent->GetIndex(), DEFAULT_INDEX_VALUE);
285 EXPECT_FALSE(swiperComponent->IsAutoPlay());
286 EXPECT_TRUE(NearEqual(swiperComponent->GetAutoPlayInterval(), DEFAULT_INTERVAL_VALUE));
287 EXPECT_TRUE(swiperComponent->IsLoop());
288 auto indicator = swiperComponent->GetIndicator();
289 EXPECT_TRUE(indicator);
290 if (indicator) {
291 auto themeManager = AceType::MakeRefPtr<ThemeManagerImpl>();
292 RefPtr<SwiperIndicatorTheme> theme = themeManager->GetTheme<SwiperIndicatorTheme>();
293 EXPECT_TRUE(indicator->GetSize() == theme->GetSize());
294 EXPECT_EQ(indicator->GetColor(), theme->GetColor());
295 EXPECT_EQ(indicator->GetSelectedColor(), theme->GetSelectedColor());
296 }
297 EXPECT_EQ(static_cast<int32_t>(swiperComponent->GetAxis()), DEFAULT_AXIS_VALUE);
298 }
299
300 /**
301 * @tc.name: DomSwiperCreatorTest006
302 * @tc.desc: Test add event to swiper component successfully.
303 * @tc.type: FUNC
304 */
305 HWTEST_F(DomSwiperTest, DomSwiperCreatorTest006, TestSize.Level1)
306 {
307 /**
308 * @tc.steps: step1. Construct string with right fields, then create swiper node with it.
309 * @tc.expected: step1. Swiper node and child swiper component are created successfully.
310 */
311 auto swiper = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(SWIPER_TEST_006);
312 auto boxChild = DOMNodeFactory::GetInstance().GetBoxChildComponent(swiper);
313 RefPtr<SwiperComponent> swiperComponent = AceType::DynamicCast<SwiperComponent>(boxChild->GetChild());
314 ASSERT_TRUE(swiperComponent != nullptr);
315
316 /**
317 * @tc.steps: step2. Check eventId of created swiper component.
318 * @tc.expected: step2. The eventId value of swiper component is as expected.
319 */
320 EXPECT_TRUE(swiperComponent->GetChangeEventId() == std::to_string(swiper->GetNodeId()));
321 }
322
323 } // namespace OHOS::Ace::Framework
324