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 "frameworks/bridge/common/dom/dom_div.h"
19 #include "frameworks/bridge/common/dom/dom_document.h"
20 #include "frameworks/bridge/common/dom/dom_rating.h"
21 #include "frameworks/bridge/common/dom/dom_swiper.h"
22 #include "frameworks/bridge/common/dom/dom_text.h"
23 #include "frameworks/bridge/test/unittest/jsfrontend/dom_node_factory.h"
24
25 using namespace testing;
26 using namespace testing::ext;
27
28 namespace OHOS::Ace::Framework {
29
30 namespace {
31
32 const std::string EVENT_TEST_001 = ""
33 "{ "
34 " \"tag\": \"swiper\", "
35 " \"commonEvent\": [ "
36 " \"touchstart\", "
37 " \"touchmove\", "
38 " \"touchcancel\", "
39 " \"touchend\" "
40 " ] "
41 "}";
42
43 const std::string EVENT_TEST_002 = ""
44 "{ "
45 " \"tag\": \"text\", "
46 " \"commonEvent\": [ "
47 " \"click\", "
48 " \"longpress\" "
49 " ] "
50 "}";
51
52 const std::string EVENT_TEST_003 = ""
53 "{ "
54 " \"tag\": \"text\", "
55 " \"commonEvent\": [ "
56 " \"touchstart\", "
57 " \"touchmove\", "
58 " \"touchcancel\", "
59 " \"touchend\", "
60 " \"click\", "
61 " \"longpress\" "
62 " ] "
63 "}";
64
65 #ifndef WEARABLE_PRODUCT
66 const std::string EVENT_TEST_004 = ""
67 "{ "
68 " \"tag\": \"rating\", "
69 " \"event\": [ "
70 " \"change\" "
71 " ], "
72 " \"commonEvent\": [ "
73 " \"touchstart\", "
74 " \"click\" "
75 " ] "
76 "}";
77 #endif
78
79 const std::string EVENT_TEST_005 = ""
80 "{ "
81 " \"tag\": \"swiper\" "
82 "}";
83
84 const std::string EVENT_TEST_006 = ""
85 "{ "
86 " \"tag\": \"swiper\", "
87 " \"commonEvent\": [ "
88 " \"focus\", "
89 " \"blur\", "
90 " \"key\" "
91 " ], "
92 " \"commonStyle\": [{ "
93 " \"focusable\" : \"true\" "
94 " } "
95 " ] "
96 "}";
97
98 } // namespace
99
100 class CommonEventTest : public testing::Test {
101 public:
102 static void SetUpTestCase();
103 static void TearDownTestCase();
104 void SetUp();
105 void TearDown();
106 };
107
SetUpTestCase()108 void CommonEventTest::SetUpTestCase() {}
TearDownTestCase()109 void CommonEventTest::TearDownTestCase() {}
SetUp()110 void CommonEventTest::SetUp() {}
TearDown()111 void CommonEventTest::TearDown() {}
112
113 /**
114 * @tc.name: DomCommonEventTest001
115 * @tc.desc: Test add touch events to dom swiper successfully.
116 * @tc.type: FUNC
117 * @tc.require: issueI5JQ5Y
118 */
119 HWTEST_F(CommonEventTest, DomCommonEventTest001, TestSize.Level1)
120 {
121 /**
122 * @tc.steps: step1. Construct string with right fields, then create swiper node with it.
123 * @tc.expected: step1. Swiper node is created successfully.
124 */
125 auto swiper = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(EVENT_TEST_001);
126 RefPtr<TouchListenerComponent> touchEventComponent = swiper->GetTouchListenerComponent();
127 ASSERT_TRUE(touchEventComponent != nullptr);
128
129 /**
130 * @tc.steps: step2. Check touch eventId of created swiper node.
131 * @tc.expected: step2. The value of touch eventId is as expected.
132 */
133 EXPECT_TRUE(touchEventComponent->GetOnTouchDownId() == std::to_string(swiper->GetNodeId()));
134 EXPECT_TRUE(touchEventComponent->GetOnTouchDownId().GetData().eventType == DOM_TOUCH_START);
135 EXPECT_TRUE(touchEventComponent->GetOnTouchMoveId() == std::to_string(swiper->GetNodeId()));
136 EXPECT_TRUE(touchEventComponent->GetOnTouchMoveId().GetData().eventType == DOM_TOUCH_MOVE);
137 EXPECT_TRUE(touchEventComponent->GetOnTouchUpId() == std::to_string(swiper->GetNodeId()));
138 EXPECT_TRUE(touchEventComponent->GetOnTouchUpId().GetData().eventType == DOM_TOUCH_END);
139 EXPECT_TRUE(touchEventComponent->GetOnTouchCancelId() == std::to_string(swiper->GetNodeId()));
140 EXPECT_TRUE(touchEventComponent->GetOnTouchCancelId().GetData().eventType == DOM_TOUCH_CANCEL);
141 }
142
143 /**
144 * @tc.name: DomCommonEventTest002
145 * @tc.desc: Test add click and longpress event to dom text successfully.
146 * @tc.type: FUNC
147 * @tc.require: issueI5JQ39
148 */
149 HWTEST_F(CommonEventTest, DomCommonEventTest002, TestSize.Level1)
150 {
151 /**
152 * @tc.steps: step1. Construct string with right fields, then create text node with it.
153 * @tc.expected: step1. Text node is created successfully.
154 */
155 auto text = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(EVENT_TEST_002);
156 RefPtr<GestureListenerComponent> gestureEventComponent = text->GetGestureListenerComponent();
157 ASSERT_TRUE(gestureEventComponent != nullptr);
158
159 /**
160 * @tc.steps: step2. Check click and longpress eventId of created text node.
161 * @tc.expected: step2. The value of click and longpress eventId is as expected.
162 */
163 EXPECT_TRUE(gestureEventComponent->GetOnClickId().GetData().eventType == DOM_CLICK);
164 EXPECT_TRUE(gestureEventComponent->GetOnClickId() == std::to_string(text->GetNodeId()));
165 EXPECT_TRUE(gestureEventComponent->GetOnLongPressId().GetData().eventType == DOM_LONG_PRESS);
166 EXPECT_TRUE(gestureEventComponent->GetOnLongPressId() == std::to_string(text->GetNodeId()));
167 }
168
169 /**
170 * @tc.name: DomCommonEventTest003
171 * @tc.desc: Test add touch events and click/longpress event to dom text successfully.
172 * @tc.type: FUNC
173 * @tc.require: issueI5JQ54
174 */
175 HWTEST_F(CommonEventTest, DomCommonEventTest003, TestSize.Level1)
176 {
177 /**
178 * @tc.steps: step1. Construct string with right fields, then create text node with it.
179 * @tc.expected: step1. Text node is created successfully.
180 */
181 auto text = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(EVENT_TEST_003);
182 RefPtr<TouchListenerComponent> touchEventComponent = text->GetTouchListenerComponent();
183 RefPtr<GestureListenerComponent> gestureEventComponent = text->GetGestureListenerComponent();
184 ASSERT_TRUE(gestureEventComponent != nullptr);
185 ASSERT_TRUE(touchEventComponent != nullptr);
186
187 /**
188 * @tc.steps: step2. Check all event eventId of created text node.
189 * @tc.expected: step2. The value of eventId about all events is as expected.
190 */
191 EXPECT_TRUE(touchEventComponent->GetOnTouchDownId() == std::to_string(text->GetNodeId()));
192 EXPECT_TRUE(touchEventComponent->GetOnTouchDownId().GetData().eventType == DOM_TOUCH_START);
193 EXPECT_TRUE(touchEventComponent->GetOnTouchMoveId() == std::to_string(text->GetNodeId()));
194 EXPECT_TRUE(touchEventComponent->GetOnTouchMoveId().GetData().eventType == DOM_TOUCH_MOVE);
195 EXPECT_TRUE(touchEventComponent->GetOnTouchUpId() == std::to_string(text->GetNodeId()));
196 EXPECT_TRUE(touchEventComponent->GetOnTouchUpId().GetData().eventType == DOM_TOUCH_END);
197 EXPECT_TRUE(touchEventComponent->GetOnTouchCancelId() == std::to_string(text->GetNodeId()));
198 EXPECT_TRUE(touchEventComponent->GetOnTouchCancelId().GetData().eventType == DOM_TOUCH_CANCEL);
199 EXPECT_TRUE(gestureEventComponent->GetOnClickId().GetData().eventType == DOM_CLICK);
200 EXPECT_TRUE(gestureEventComponent->GetOnClickId() == std::to_string(text->GetNodeId()));
201 EXPECT_TRUE(gestureEventComponent->GetOnLongPressId().GetData().eventType == DOM_LONG_PRESS);
202 EXPECT_TRUE(gestureEventComponent->GetOnLongPressId() == std::to_string(text->GetNodeId()));
203 }
204
205 #ifndef WEARABLE_PRODUCT
206 /**
207 * @tc.name: DomCommonEventTest004
208 * @tc.desc: Test private change event and common gesture event to dom rating successfully.
209 * @tc.type: FUNC
210 * @tc.require: issueI5JQ2T
211 */
212 HWTEST_F(CommonEventTest, DomCommonEventTest004, TestSize.Level1)
213 {
214 /**
215 * @tc.steps: step1. Construct string with right fields, then create rating node with it.
216 * @tc.expected: step1. Swiper node is created successfully.
217 */
218 auto rating = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(EVENT_TEST_004);
219 RefPtr<TouchListenerComponent> touchEventComponent = rating->GetTouchListenerComponent();
220 RefPtr<GestureListenerComponent> gestureEventComponent = rating->GetGestureListenerComponent();
221 auto boxChild = DOMNodeFactory::GetInstance().GetBoxChildComponent(rating);
222 RefPtr<RatingComponent> ratingComponent = AceType::DynamicCast<RatingComponent>(boxChild->GetChild());
223 ASSERT_TRUE(gestureEventComponent != nullptr);
224 ASSERT_TRUE(touchEventComponent != nullptr);
225 ASSERT_TRUE(ratingComponent != nullptr);
226
227 /**
228 * @tc.steps: step2. Check all event eventId of created rating node.
229 * @tc.expected: step2. The value of eventId about all events is as expected.
230 */
231 EXPECT_TRUE(touchEventComponent->GetOnTouchDownId() == std::to_string(rating->GetNodeId()));
232 EXPECT_TRUE(touchEventComponent->GetOnTouchDownId().GetData().eventType == DOM_TOUCH_START);
233 EXPECT_TRUE(gestureEventComponent->GetOnClickId().GetData().eventType == DOM_CLICK);
234 EXPECT_TRUE(gestureEventComponent->GetOnClickId() == std::to_string(rating->GetNodeId()));
235 EXPECT_TRUE(ratingComponent->GetChangeEventId() == std::to_string(rating->GetNodeId()));
236 EXPECT_TRUE(ratingComponent->GetChangeEventId().GetData().eventType == DOM_CHANGE);
237 }
238 #endif
239
240 /**
241 * @tc.name: DomCommonEventTest005
242 * @tc.desc: Test default event value without add event to swiper node.
243 * @tc.type: FUNC
244 */
245 HWTEST_F(CommonEventTest, DomCommonEventTest005, TestSize.Level1)
246 {
247 /**
248 * @tc.steps: step1. Construct string with right fields, then create swiper node with it.
249 * @tc.expected: step1. Swiper node is created successfully and has no event.
250 */
251 auto swiper = DOMNodeFactory::GetInstance().CreateDOMNodeFromDsl(EVENT_TEST_005);
252 RefPtr<TouchListenerComponent> touchEventComponent = swiper->GetTouchListenerComponent();
253 RefPtr<GestureListenerComponent> gestureEventComponent = swiper->GetGestureListenerComponent();
254 ASSERT_TRUE(gestureEventComponent == nullptr);
255 ASSERT_TRUE(touchEventComponent == nullptr);
256 }
257
258 } // namespace OHOS::Ace::Framework
259