1 /*
2 * Copyright (c) 2022-2023 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 #define private public
19 #include "base/geometry/ng/offset_t.h"
20 #include "base/memory/ace_type.h"
21 #include "base/memory/referenced.h"
22 #include "core/components_ng/base/frame_node.h"
23 #include "core/components_ng/event/event_hub.h"
24 #include "core/components_ng/pattern/pattern.h"
25 #include "core/components_v2/inspector/inspector_constants.h"
26 #include "test/mock/core/pipeline/mock_pipeline_context.h"
27
28 using namespace testing;
29 using namespace testing::ext;
30
31 namespace OHOS::Ace::NG {
32 namespace {
33 constexpr bool EVENT_HUB_ENABLE = false;
34 const std::string DRAG_STARR_EVENT_TYPE = "drag start";
35 const std::string DRAG_ENTER_EVENT_TYPE = "drag enter";
36 const std::string DRAG_LEAVE_EVENT_TYPE = "drag leave";
37 const std::string DRAG_MOVE_EVENT_TYPE = "drag move";
38 const std::string DRAG_DROP_EVENT_TYPE = "drag drop";
39 const std::string DRAG_END_EVENT_TYPE = "drag end";
40
41 const float OLD_X_VALUE = 10.9f;
42 const float OLD_Y_VALUE = 11.0f;
43 const float OLD_WIDTH = 400.0f;
44 const float OLD_HEIGHT = 400.0f;
45 const RectF OLD_RECT = RectF(OLD_X_VALUE, OLD_Y_VALUE, OLD_WIDTH, OLD_HEIGHT);
46 const OffsetF OLD_ORIGIN = OffsetF(OLD_WIDTH, OLD_HEIGHT);
47
48 const float NEW_X_VALUE = 15.9f;
49 const float NEW_Y_VALUE = 15.0f;
50 const float NEW_WIDTH = 500.0f;
51 const float NEW_HEIGHT = 500.0f;
52 const RectF NEW_RECT = RectF(NEW_X_VALUE, NEW_Y_VALUE, NEW_WIDTH, NEW_HEIGHT);
53 const OffsetF NEW_ORIGIN = OffsetF(NEW_WIDTH, NEW_HEIGHT);
54
55 const RectF RECT_DELTA = RectF(1.0f, 1.0f, 1.0f, 1.0f);
56 const OffsetF OFFSET_DELTA = OffsetF(1.0f, 1.0f);
57 const OffsetF ORIGIN_DELTA = OffsetF(1.0f, 1.0f);
58
59 const std::string STRINGCTER_A = "A";
60 const std::string STRINGCTER_Q = "Q";
61 const std::string STRINGCTER_E = "E";
62 constexpr int32_t NUM_CTRL_VALUE = 1;
63 constexpr int32_t NUM_SHIFT_VALUE = 2;
64 constexpr int32_t NUM_ALT_VALUE = 4;
65
66 const std::string RESULT_SUCCESS_ONE = "sucess1";
67 const std::string RESULT_SUCCESS_TWO = "sucess2";
68 } // namespace
69
70 class EventHubTestNg : public testing::Test {
71 public:
72 static void SetUpTestSuite();
73 static void TearDownTestSuite();
74 void SetUp() override;
75 void TearDown() override;
76 };
77
SetUpTestSuite()78 void EventHubTestNg::SetUpTestSuite()
79 {
80 GTEST_LOG_(INFO) << "EventHubTestNg SetUpTestCase";
81 }
82
TearDownTestSuite()83 void EventHubTestNg::TearDownTestSuite()
84 {
85 GTEST_LOG_(INFO) << "EventHubTestNg TearDownTestCase";
86 }
87
SetUp()88 void EventHubTestNg::SetUp()
89 {
90 MockPipelineContext::SetUp();
91 }
92
TearDown()93 void EventHubTestNg::TearDown()
94 {
95 MockPipelineContext::TearDown();
96 }
97
98 /**
99 * @tc.name: EventHubCreateTest001
100 * @tc.desc: Create EventHub.
101 * @tc.type: FUNC
102 */
103 HWTEST_F(EventHubTestNg, EventHubCreateTest001, TestSize.Level1)
104 {
105 /**
106 * @tc.steps: step1. Create EventHub.
107 * @tc.expected: eventHub is not null.
108 */
109 auto eventHub = AceType::MakeRefPtr<EventHub>();
110 eventHub->MarkModifyDone();
111 EXPECT_NE(eventHub, nullptr);
112
113 /**
114 * @tc.steps: step2. Get EventHub's properties.
115 * @tc.expected: These properties are null when GetOrCreateEventHub functions have not been invoked.
116 */
117 EXPECT_EQ(eventHub->GetGestureEventHub(), nullptr);
118 EXPECT_EQ(eventHub->GetInputEventHub(), nullptr);
119 EXPECT_EQ(eventHub->GetFocusHub(), nullptr);
120 EXPECT_EQ(eventHub->GetFrameNode(), nullptr);
121 EXPECT_EQ(eventHub->GetOnDragStart(), nullptr);
122
123 /**
124 * @tc.steps: step3. Test EventHub's default properties.
125 */
126 EXPECT_TRUE(!eventHub->HasOnAreaChanged());
127 EXPECT_TRUE(!eventHub->HasOnDragStart());
128 EXPECT_TRUE(!eventHub->HasOnDrop());
129 EXPECT_TRUE(eventHub->IsEnabled());
130 }
131
132 /**
133 * @tc.name: EventHubPropertyTest002
134 * @tc.desc: Create EventHub and invoke GetOrCreateEventHub functions.
135 * @tc.type: FUNC
136 */
137 HWTEST_F(EventHubTestNg, EventHubPropertyTest002, TestSize.Level1)
138 {
139 /**
140 * @tc.steps: step1. Create EventHub.
141 * @tc.expected: eventHub is not null.
142 */
143 auto eventHub = AceType::MakeRefPtr<EventHub>();
144 EXPECT_NE(eventHub, nullptr);
145
146 /**
147 * @tc.steps: step2. Invoke GetOrCreateEventHub functions.
148 * @tc.expected: These eventHub properties are not null.
149 */
150 eventHub->GetOrCreateGestureEventHub();
151 eventHub->GetOrCreateInputEventHub();
152 eventHub->GetOrCreateFocusHub();
153 EXPECT_NE(eventHub->GetGestureEventHub(), nullptr);
154 EXPECT_NE(eventHub->GetInputEventHub(), nullptr);
155 EXPECT_NE(eventHub->GetFocusHub(), nullptr);
156
157 auto frameNode = AceType::MakeRefPtr<FrameNode>(V2::TEXT_ETS_TAG, -1, AceType::MakeRefPtr<Pattern>());
158 eventHub->AttachHost(frameNode);
159 EXPECT_TRUE(eventHub->GetFrameNode() != nullptr && eventHub->GetFrameNode()->GetTag() == V2::TEXT_ETS_TAG);
160 eventHub->OnContextAttached();
161 eventHub->SetEnabled(EVENT_HUB_ENABLE);
162 EXPECT_TRUE(!eventHub->IsEnabled());
163 }
164
165 /**
166 * @tc.name: EventHubPropertyTest003
167 * @tc.desc: Create EventHub and set/fire onAreaChanged, onAppear and onDisappear function.
168 * @tc.type: FUNC
169 */
170 HWTEST_F(EventHubTestNg, EventHubPropertyTest003, TestSize.Level1)
171 {
172 /**
173 * @tc.steps: step1. Create EventHub.
174 * @tc.expected: eventHub is not null.
175 */
176 auto eventHub = AceType::MakeRefPtr<EventHub>();
177 EXPECT_NE(eventHub, nullptr);
178
179 /**
180 * @tc.steps: step2. Set EventHub OnAreaChanged function and fire it.
181 * @tc.expected: onAreaChanged is invoked and the temp values are assigned with correct values.
182 */
183 RectF tempOldRect;
184 OffsetF tempOldOrigin;
185 RectF tempNewRect;
186 OffsetF tempNewOrigin;
187 auto onAreaChanged = [&tempOldRect, &tempOldOrigin, &tempNewRect, &tempNewOrigin](
__anone3c9cdf30202( const RectF& oldRect, const OffsetF& oldOrigin, const RectF& rect, const OffsetF& origin) 188 const RectF& oldRect, const OffsetF& oldOrigin, const RectF& rect, const OffsetF& origin) {
189 tempOldRect = oldRect;
190 tempOldOrigin = oldOrigin;
191 tempNewRect = rect;
192 tempNewOrigin = origin;
193 };
194
195 eventHub->SetOnAreaChanged(onAreaChanged);
196 eventHub->FireOnAreaChanged(OLD_RECT, OLD_ORIGIN, NEW_RECT, NEW_ORIGIN);
197 EXPECT_TRUE(eventHub->HasOnAreaChanged());
198 EXPECT_EQ(tempOldRect, OLD_RECT);
199 EXPECT_EQ(tempOldOrigin, OLD_ORIGIN);
200 EXPECT_EQ(tempNewRect, NEW_RECT);
201 EXPECT_EQ(tempNewOrigin, NEW_ORIGIN);
202
203 /**
204 * @tc.steps: step3. Set/fire EventHub onAppear and onDisappear function.
205 * @tc.expected: isAppear is assigned with correct value.
206 */
__anone3c9cdf30302() 207 eventHub->SetOnAppear([]() {});
208 eventHub->FireOnAppear();
209
__anone3c9cdf30402() 210 eventHub->SetOnDisappear([]() {});
211 eventHub->FireOnDisappear();
212 }
213
214 /**
215 * @tc.name: EventHubDragEventsTest004
216 * @tc.desc: Create EventHub and set/fire drag related functions.
217 * @tc.type: FUNC
218 */
219 HWTEST_F(EventHubTestNg, EventHubDragEventsTest004, TestSize.Level1)
220 {
221 /**
222 * @tc.steps: step1. Create EventHub.
223 * @tc.expected: eventHub is not null.
224 */
225 auto eventHub = AceType::MakeRefPtr<EventHub>();
226 EXPECT_NE(eventHub, nullptr);
227
228 /**
229 * @tc.steps: step2. Set EventHub OnDragStart event and fire it.
230 * @tc.expected: OnDragStart is invoked and the temp values are assigned with correct values.
231 */
232 auto dragEvent = AceType::MakeRefPtr<OHOS::Ace::DragEvent>();
233 std::string dragEventType;
234 auto OnDragStartFunc = [&dragEventType](const RefPtr<OHOS::Ace::DragEvent>& /* dragEvent */,
__anone3c9cdf30502(const RefPtr<OHOS::Ace::DragEvent>& , const std::string& eventType) 235 const std::string& eventType) -> DragDropInfo {
236 dragEventType = eventType;
237 return {};
238 };
239 eventHub->SetOnDragStart(OnDragStartFunc);
240 EXPECT_TRUE(eventHub->HasOnDragStart());
241 EXPECT_NE(eventHub->GetOnDragStart(), nullptr);
242 eventHub->GetOnDragStart()(dragEvent, DRAG_STARR_EVENT_TYPE);
243 EXPECT_EQ(dragEventType, DRAG_STARR_EVENT_TYPE);
244
245 /**
246 * @tc.steps: step3. Set EventHub OnDragEnter event and fire it.
247 * @tc.expected: OnDragEnter is invoked and the temp values are assigned with correct values.
248 */
249 auto OnDragFunc = [&dragEventType](const RefPtr<OHOS::Ace::DragEvent>& /* dragEvent */,
__anone3c9cdf30602(const RefPtr<OHOS::Ace::DragEvent>& , const std::string& eventType) 250 const std::string& eventType) { dragEventType = eventType; };
251 auto onDragEnter = OnDragFunc;
252 eventHub->SetOnDragEnter(onDragEnter);
253 eventHub->FireOnDragEnter(dragEvent, DRAG_ENTER_EVENT_TYPE);
254 EXPECT_EQ(dragEventType, DRAG_ENTER_EVENT_TYPE);
255
256 /**
257 * @tc.steps: step4. Set EventHub OnDragLeave event and fire it.
258 * @tc.expected: OnDragLeave is invoked and the temp values are assigned with correct values.
259 */
260 auto onDragLeave = OnDragFunc;
261 eventHub->SetOnDragLeave(onDragLeave);
262 eventHub->FireOnDragLeave(dragEvent, DRAG_LEAVE_EVENT_TYPE);
263 EXPECT_EQ(dragEventType, DRAG_LEAVE_EVENT_TYPE);
264
265 /**
266 * @tc.steps: step5. Set EventHub OnDragMove event and fire it.
267 * @tc.expected: OnDragMove is invoked and the temp values are assigned with correct values.
268 */
269 auto onDragMove = OnDragFunc;
270 eventHub->SetOnDragMove(onDragMove);
271 eventHub->FireOnDragMove(dragEvent, DRAG_MOVE_EVENT_TYPE);
272 EXPECT_EQ(dragEventType, DRAG_MOVE_EVENT_TYPE);
273
274 /**
275 * @tc.steps: step6. Set EventHub OnDrop event and fire it.
276 * @tc.expected: OnDrop is invoked and the temp values are assigned with correct values.
277 */
278 auto onDragDrop = OnDragFunc;
279 eventHub->SetOnDrop(onDragDrop);
280 eventHub->FireOnDragMove(dragEvent, DRAG_DROP_EVENT_TYPE);
281 EXPECT_TRUE(eventHub->HasOnDrop());
282 EXPECT_EQ(dragEventType, DRAG_DROP_EVENT_TYPE);
283 }
284
285 /**
286 * @tc.name: EventHubCreateTest005
287 * @tc.desc: Create EventHub.
288 * @tc.type: FUNC
289 */
290 HWTEST_F(EventHubTestNg, EventHubDragEventsTest005, TestSize.Level1)
291 {
292 auto eventHub = AceType::MakeRefPtr<EventHub>();
293 ASSERT_NE(eventHub, nullptr);
294 std::vector<KeyboardShortcut> keyboardShortcut;
__anone3c9cdf30702() 295 eventHub->SetKeyboardShortcut(STRINGCTER_A, NUM_CTRL_VALUE, []() {});
296 keyboardShortcut = eventHub->GetKeyboardShortcut();
297 for (auto iter = keyboardShortcut.begin(); iter != keyboardShortcut.end(); iter++) {
298 EXPECT_EQ(STRINGCTER_A, (*iter).value);
299 EXPECT_EQ(NUM_CTRL_VALUE, (*iter).keys);
300 }
301 keyboardShortcut.clear();
302
__anone3c9cdf30802() 303 eventHub->SetKeyboardShortcut(STRINGCTER_Q, NUM_SHIFT_VALUE, []() {});
304 eventHub->GetKeyboardShortcut();
305 for (auto iter = keyboardShortcut.begin(); iter != keyboardShortcut.end(); iter++) {
306 EXPECT_EQ(STRINGCTER_Q, (*iter).value);
307 EXPECT_EQ(NUM_SHIFT_VALUE, (*iter).keys);
308 }
309 keyboardShortcut.clear();
310
__anone3c9cdf30902() 311 eventHub->SetKeyboardShortcut(STRINGCTER_E, NUM_ALT_VALUE, []() {});
312 eventHub->GetKeyboardShortcut();
313 for (auto iter = keyboardShortcut.begin(); iter != keyboardShortcut.end(); iter++) {
314 EXPECT_EQ(STRINGCTER_E, (*iter).value);
315 EXPECT_EQ(NUM_CTRL_VALUE, (*iter).keys);
316 }
317 keyboardShortcut.clear();
318 }
319
320 /**
321 * @tc.name: EventHubDragEventsTest006
322 * @tc.desc: Create EventHub and set/fire drag related customer functions.
323 * @tc.type: FUNC
324 */
325 HWTEST_F(EventHubTestNg, EventHubDragEventsTest006, TestSize.Level1)
326 {
327 /**
328 * @tc.steps: step1. Create EventHub.
329 * @tc.expected: eventHub is not null.
330 */
331 auto eventHub = AceType::MakeRefPtr<EventHub>();
332 EXPECT_NE(eventHub, nullptr);
333
334 /**
335 * @tc.steps: step2. Set EventHub Customer OnDragEnter event and fire it.
336 * @tc.expected: OnDragEnter is invoked and the temp values are assigned with correct values.
337 */
338 auto dragEvent = AceType::MakeRefPtr<OHOS::Ace::DragEvent>();
339 std::string dragEventType;
340 auto OnDragFunc = [&dragEventType](const RefPtr<OHOS::Ace::DragEvent>& /* dragEvent */,
__anone3c9cdf30a02(const RefPtr<OHOS::Ace::DragEvent>& , const std::string& eventType) 341 const std::string& eventType) { dragEventType = eventType; };
342 auto onDragEnter = OnDragFunc;
343 eventHub->SetCustomerOnDragFunc(DragFuncType::DRAG_ENTER, onDragEnter);
344 eventHub->FireCustomerOnDragFunc(DragFuncType::DRAG_ENTER, dragEvent, DRAG_ENTER_EVENT_TYPE);
345 EXPECT_EQ(dragEventType, DRAG_ENTER_EVENT_TYPE);
346
347 /**
348 * @tc.steps: step3. Set EventHub Customer OnDragLeave event and fire it.
349 * @tc.expected: OnDragLeave is invoked and the temp values are assigned with correct values.
350 */
351 auto onDragLeave = OnDragFunc;
352 eventHub->SetCustomerOnDragFunc(DragFuncType::DRAG_LEAVE, onDragLeave);
353 eventHub->FireCustomerOnDragFunc(DragFuncType::DRAG_LEAVE, dragEvent, DRAG_LEAVE_EVENT_TYPE);
354 EXPECT_EQ(dragEventType, DRAG_LEAVE_EVENT_TYPE);
355
356 /**
357 * @tc.steps: step4. Set EventHub Customer OnDragMove event and fire it.
358 * @tc.expected: OnDragMove is invoked and the temp values are assigned with correct values.
359 */
360 auto onDragMove = OnDragFunc;
361 eventHub->SetCustomerOnDragFunc(DragFuncType::DRAG_MOVE, onDragMove);
362 eventHub->FireCustomerOnDragFunc(DragFuncType::DRAG_MOVE, dragEvent, DRAG_MOVE_EVENT_TYPE);
363 EXPECT_EQ(dragEventType, DRAG_MOVE_EVENT_TYPE);
364
365 /**
366 * @tc.steps: step6. Set EventHub Customer OnDrop event and fire it.
367 * @tc.expected: OnDrop is invoked and the temp values are assigned with correct values.
368 */
369 auto onDragDrop = OnDragFunc;
370 eventHub->SetCustomerOnDragFunc(DragFuncType::DRAG_DROP, onDragDrop);
371 eventHub->FireCustomerOnDragFunc(DragFuncType::DRAG_DROP, dragEvent, DRAG_DROP_EVENT_TYPE);
372 EXPECT_TRUE(eventHub->HasCustomerOnDrop());
373 EXPECT_EQ(dragEventType, DRAG_DROP_EVENT_TYPE);
374
375 /**
376 * @tc.steps: step7. Set EventHub Customer OnDragEnd event and fire it.
377 * @tc.expected: OnDragEnd is invoked and the temp values are assigned with correct values.
378 */
__anone3c9cdf30b02(const RefPtr<OHOS::Ace::DragEvent>& ) 379 auto OnDragEnd = [&dragEventType](const RefPtr<OHOS::Ace::DragEvent>& /* dragEvent */) {
380 dragEventType = DRAG_END_EVENT_TYPE;
381 };
382 eventHub->SetCustomerOnDragFunc(DragFuncType::DRAG_END, OnDragEnd);
383 eventHub->FireCustomerOnDragFunc(DragFuncType::DRAG_END, dragEvent);
384 EXPECT_EQ(dragEventType, DRAG_END_EVENT_TYPE);
385 }
386
387 /**
388 * @tc.name: EventHubDisableAreaChange001
389 * @tc.desc: Create EventHub and test disable areaChange function.
390 * @tc.type: FUNC
391 */
392 HWTEST_F(EventHubTestNg, EventHubDisableAreaChange001, TestSize.Level1)
393 {
394 /**
395 * @tc.steps: step1. Create EventHub.
396 * @tc.expected: eventHub is not null.
397 */
398 auto eventHub = AceType::MakeRefPtr<EventHub>();
399 EXPECT_NE(eventHub, nullptr);
400
401 /**
402 * @tc.steps: step2. Set EventHub OnAreaChanged function and fire it.
403 * @tc.expected: onAreaChanged is invoked and the temp values are assigned with correct values.
404 */
405 RectF tempOldRect;
406 OffsetF tempOldOrigin;
407 RectF tempNewRect;
408 OffsetF tempNewOrigin;
409 auto onAreaChanged = [&tempOldRect, &tempOldOrigin, &tempNewRect, &tempNewOrigin](
__anone3c9cdf30c02( const RectF& oldRect, const OffsetF& oldOrigin, const RectF& rect, const OffsetF& origin) 410 const RectF& oldRect, const OffsetF& oldOrigin, const RectF& rect, const OffsetF& origin) {
411 tempOldRect = oldRect;
412 tempOldOrigin = oldOrigin;
413 tempNewRect = rect;
414 tempNewOrigin = origin;
415 };
416
417 eventHub->SetOnAreaChanged(onAreaChanged);
418 eventHub->FireOnAreaChanged(OLD_RECT, OLD_ORIGIN, NEW_RECT, NEW_ORIGIN);
419 EXPECT_TRUE(eventHub->HasOnAreaChanged());
420 EXPECT_EQ(tempOldRect, OLD_RECT);
421 EXPECT_EQ(tempOldOrigin, OLD_ORIGIN);
422 EXPECT_EQ(tempNewRect, NEW_RECT);
423 EXPECT_EQ(tempNewOrigin, NEW_ORIGIN);
424
425 /**
426 * @tc.steps: step3. Clear the callback.
427 * @tc.expected: onAreaChanged is empty.
428 */
429 eventHub->ClearUserOnAreaChanged();
430 eventHub->FireOnAreaChanged(
431 OLD_RECT + OFFSET_DELTA, OLD_ORIGIN + ORIGIN_DELTA, NEW_RECT + OFFSET_DELTA, NEW_ORIGIN + ORIGIN_DELTA);
432 EXPECT_FALSE(eventHub->HasOnAreaChanged());
433 EXPECT_EQ(tempOldRect, OLD_RECT);
434 EXPECT_EQ(tempOldOrigin, OLD_ORIGIN);
435 EXPECT_EQ(tempNewRect, NEW_RECT);
436 EXPECT_EQ(tempNewOrigin, NEW_ORIGIN);
437
438 /**
439 * @tc.steps: step3. Set/fire areaChange function.
440 * @tc.expected: areaChange is assigned with correct value.
441 */
442 eventHub->SetOnAreaChanged(onAreaChanged);
443 eventHub->FireOnAreaChanged(
444 OLD_RECT + OFFSET_DELTA, OLD_ORIGIN + ORIGIN_DELTA, NEW_RECT + OFFSET_DELTA, NEW_ORIGIN + ORIGIN_DELTA);
445 EXPECT_TRUE(eventHub->HasOnAreaChanged());
446 EXPECT_EQ(tempOldRect, OLD_RECT + OFFSET_DELTA);
447 EXPECT_EQ(tempOldOrigin, OLD_ORIGIN + ORIGIN_DELTA);
448 EXPECT_EQ(tempNewRect, NEW_RECT + OFFSET_DELTA);
449 EXPECT_EQ(tempNewOrigin, NEW_ORIGIN + ORIGIN_DELTA);
450 }
451
452 /**
453 * @tc.name: EventHubDisableAppear001
454 * @tc.desc: Create EventHub and test disable onAppear function.
455 * @tc.type: FUNC
456 */
457 HWTEST_F(EventHubTestNg, EventHubDisableAppear001, TestSize.Level1)
458 {
459 /**
460 * @tc.steps: step1. Create EventHub.
461 * @tc.expected: eventHub is not null.
462 */
463 auto eventHub = AceType::MakeRefPtr<EventHub>();
464 EXPECT_NE(eventHub, nullptr);
465
466 /**
467 * @tc.steps: step2. Set EventHub onAppear function and fire it.
468 * @tc.expected: onAppear is invoked and the temp values are assigned with correct values.
469 */
470 std::string result;
__anone3c9cdf30d02() 471 auto onAppear = [&result]() { result = RESULT_SUCCESS_ONE; };
472
473 eventHub->SetOnAppear(onAppear);
474 EXPECT_NE(eventHub->onAppear_, nullptr);
475 eventHub->onAppear_();
476 EXPECT_EQ(result, RESULT_SUCCESS_ONE);
477 /**
478 * @tc.steps: step3. Clear the callback.
479 * @tc.expected: onAppear is empty.
480 */
481 eventHub->ClearUserOnAppear();
482 EXPECT_EQ(eventHub->onAppear_, nullptr);
483
484 /**
485 * @tc.steps: step3. Set/fire EventHub onAppear function.
486 * @tc.expected: onAppear is assigned with correct value.
487 */
__anone3c9cdf30e02() 488 auto onAppear2 = [&result]() { result = RESULT_SUCCESS_TWO; };
489
490 eventHub->SetOnAppear(onAppear2);
491 EXPECT_NE(eventHub->onAppear_, nullptr);
492 eventHub->onAppear_();
493 EXPECT_EQ(result, RESULT_SUCCESS_TWO);
494 }
495
496 /**
497 * @tc.name: EventHubDisableDisAppear001
498 * @tc.desc: Create EventHub and test disable onDisAppear function.
499 * @tc.type: FUNC
500 */
501 HWTEST_F(EventHubTestNg, EventHubDisableDisAppear001, TestSize.Level1)
502 {
503 /**
504 * @tc.steps: step1. Create EventHub.
505 * @tc.expected: eventHub is not null.
506 */
507 auto eventHub = AceType::MakeRefPtr<EventHub>();
508 EXPECT_NE(eventHub, nullptr);
509
510 /**
511 * @tc.steps: step2. Set EventHub onDisAppear function and fire it.
512 * @tc.expected: onDisAppear is invoked and the temp values are assigned with correct values.
513 */
514 std::string result;
__anone3c9cdf30f02() 515 auto onDisAppear = [&result]() { result = RESULT_SUCCESS_ONE; };
516
517 eventHub->SetOnDisappear(onDisAppear);
518 EXPECT_NE(eventHub->onDisappear_, nullptr);
519 eventHub->onDisappear_();
520 EXPECT_EQ(result, RESULT_SUCCESS_ONE);
521 /**
522 * @tc.steps: step3. Clear the callback.
523 * @tc.expected: onDisAppear is empty.
524 */
525 eventHub->ClearUserOnDisAppear();
526 EXPECT_EQ(eventHub->onDisappear_, nullptr);
527
528 /**
529 * @tc.steps: step3. Set/fire EventHub onDisappear function.
530 * @tc.expected: disAppear is assigned with correct value.
531 */
__anone3c9cdf31002() 532 auto onDisAppear2 = [&result]() { result = RESULT_SUCCESS_TWO; };
533
534 eventHub->SetOnDisappear(onDisAppear2);
535 EXPECT_NE(eventHub->onDisappear_, nullptr);
536 eventHub->onDisappear_();
537 EXPECT_EQ(result, RESULT_SUCCESS_TWO);
538 }
539
540 /**
541 * @tc.name: SetCurrentUIState001
542 * @tc.desc: Create EventHub and test disable onDisAppear function.
543 * @tc.type: FUNC
544 */
545 HWTEST_F(EventHubTestNg, SetCurrentUIState002, TestSize.Level1)
546 {
547 /**
548 * @tc.steps: step1. Create EventHub.
549 * @tc.expected: eventHub is not null.
550 */
551 auto eventHub = AceType::MakeRefPtr<EventHub>();
552 EXPECT_NE(eventHub, nullptr);
553 bool temp = true;
554
555 /**
556 * @tc.steps: Use SetCurrentUIState to set the UIState state
557 * @tc.expected: Use 'GetCurrentUIState' to obtain the already set UIState
558 */
559 eventHub->AddSupportedState(UI_STATE_NORMAL);
560 eventHub->SetSupportedStates(UI_STATE_NORMAL);
561 eventHub->MarkModifyDone();
562
563 eventHub->SetCurrentUIState(UI_STATE_NORMAL, temp);
564 EXPECT_EQ(eventHub->GetCurrentUIState(), UI_STATE_NORMAL);
565
566 eventHub->IsCurrentStateOn(UI_STATE_NORMAL);
567 eventHub->CreateGetEventTargetImpl();
568
569 eventHub->SetCurrentUIState(UI_STATE_PRESSED, temp);
570 EXPECT_EQ(eventHub->GetCurrentUIState(), UI_STATE_PRESSED);
571
572 eventHub->SetCurrentUIState(UI_STATE_FOCUSED, temp);
573 EXPECT_EQ(eventHub->GetCurrentUIState(), UI_STATE_NORMAL | UI_STATE_PRESSED | UI_STATE_FOCUSED);
574 eventHub->MarkModifyDone();
575
576 eventHub->SetCurrentUIState(UI_STATE_DISABLED, temp);
577 EXPECT_EQ(eventHub->GetCurrentUIState(), UI_STATE_NORMAL | UI_STATE_PRESSED | UI_STATE_FOCUSED | UI_STATE_DISABLED);
578
579 eventHub->SetCurrentUIState(UI_STATE_SELECTED, temp);
580 EXPECT_EQ(eventHub->GetCurrentUIState(),
581 UI_STATE_SELECTED | UI_STATE_NORMAL | UI_STATE_PRESSED | UI_STATE_FOCUSED | UI_STATE_DISABLED);
582 }
583
584 /**
585 * @tc.name: EventHubTest001
586 * @tc.desc: Default branch in test FireCustomerOnDragFunc
587 * @tc.type: FUNC
588 */
589 HWTEST_F(EventHubTestNg, EventHubTest001, TestSize.Level1)
590 {
591 /**
592 * @tc.steps: step1. Create EventHub.
593 * @tc.expected: eventHub is not null.
594 */
595 auto eventHub = AceType::MakeRefPtr<EventHub>();
596 EXPECT_NE(eventHub, nullptr);
597
598 /**
599 * @tc.steps: step2. Create DragEvent.
600 * @tc.expected: DragEvent is not null.
601 */
602 auto dragEvent = AceType::MakeRefPtr<OHOS::Ace::DragEvent>();
603 EXPECT_NE(dragEvent, nullptr);
604
605 /**
606 * @tc.steps: step3. Calling DRAG in FirecustomerOnDragFunc_ CustomerOnDragEnd in END_ Empty branch.
607 * @tc.expected: retFlag is false.
608 */
609 eventHub->FireCustomerOnDragFunc(DragFuncType::DRAG_END, dragEvent, DRAG_DROP_EVENT_TYPE);
610 auto retFlag = eventHub->HasCustomerOnDrop();
611 EXPECT_FALSE(retFlag);
612
613 /**
614 * @tc.steps: step4. Calling the default branch in FirecustomerOnDragFunc.
615 * @tc.expected: retFlag is false.
616 */
617 eventHub->FireCustomerOnDragFunc(DragFuncType(10), dragEvent, DRAG_DROP_EVENT_TYPE);
618 retFlag = eventHub->HasCustomerOnDrop();
619 EXPECT_FALSE(retFlag);
620 }
621
622 /**
623 * @tc.name: EventHubTest002
624 * @tc.desc: Default Branch in Test SetCustomerOnDragFunc(OnDragFunc)
625 * @tc.type: FUNC
626 */
627 HWTEST_F(EventHubTestNg, EventHubTest002, TestSize.Level1)
628 {
629 /**
630 * @tc.steps: step1. Create EventHub.
631 * @tc.expected: eventHub is not null.
632 */
633 auto eventHub = AceType::MakeRefPtr<EventHub>();
634 EXPECT_NE(eventHub, nullptr);
635
636 /**
637 * @tc.steps: step2. Create DragEvent.
638 * @tc.expected: DragEvent is not null.
639 */
640 auto dragEvent = AceType::MakeRefPtr<OHOS::Ace::DragEvent>();
641 EXPECT_NE(dragEvent, nullptr);
642
643 /**
644 * @tc.steps: step3. construct OnDragFunc.
645 */
646 std::string dragEventType;
647 auto onDragFunc = [&dragEventType](const RefPtr<OHOS::Ace::DragEvent>& /* dragEvent */,
__anone3c9cdf31102(const RefPtr<OHOS::Ace::DragEvent>& , const std::string& eventType) 648 const std::string& eventType) { dragEventType = eventType; };
649
650 /**
651 * @tc.steps: step4. Calling the default branch in SetCustomerOnDragFunc.
652 * @tc.expected: eventHub->customerOnDragEnd_ is false.
653 */
654 eventHub->SetCustomerOnDragFunc(DragFuncType(10), onDragFunc);
655 EXPECT_FALSE(eventHub->customerOnDragEnd_);
656 }
657
658 /**
659 * @tc.name: EventHubTest003
660 * @tc.desc: Default Branch in Test SetCustomerOnDragFunc(OnNewDragFunc)
661 * @tc.type: FUNC
662 */
663 HWTEST_F(EventHubTestNg, EventHubTest003, TestSize.Level1)
664 {
665 /**
666 * @tc.steps: step1. Create EventHub.
667 * @tc.expected: eventHub is not null.
668 */
669 auto eventHub = AceType::MakeRefPtr<EventHub>();
670 EXPECT_NE(eventHub, nullptr);
671
672 /**
673 * @tc.steps: step2. Create DragEvent.
674 * @tc.expected: DragEvent is not null.
675 */
676 auto dragEvent = AceType::MakeRefPtr<OHOS::Ace::DragEvent>();
677 EXPECT_NE(dragEvent, nullptr);
678
679 /**
680 * @tc.steps: step3. construct OnDragFunc.
681 */
682 std::string dragEventType;
683 auto onDragFunc = [&dragEventType](
__anone3c9cdf31202( const RefPtr<OHOS::Ace::DragEvent>& ) 684 const RefPtr<OHOS::Ace::DragEvent>& /* dragEvent */) { dragEventType = DRAG_END_EVENT_TYPE; };
685
686 /**
687 * @tc.steps: step4. Call SetCustomerOnDragFunc with OnDragFunc.
688 * @tc.expected: eventHub->customerOnDragEnter_ is false.
689 */
690 eventHub->SetCustomerOnDragFunc(DragFuncType(10), onDragFunc);
691 EXPECT_FALSE(eventHub->customerOnDragEnter_);
692 }
693
694 /**
695 * @tc.name: EventHubTest004
696 * @tc.desc: Test MarkModifyDone
697 * @tc.type: FUNC
698 */
699 HWTEST_F(EventHubTestNg, EventHubTest004, TestSize.Level1)
700 {
701 /**
702 * @tc.steps: step1. Create EventHub.
703 * @tc.expected: eventHub is not null.
704 */
705 auto eventHub = AceType::MakeRefPtr<EventHub>();
706 EXPECT_NE(eventHub, nullptr);
707
708 /**
709 * @tc.steps: step2. Update SupportedState in eventHub using UI_STATE_PRESSED.
710 */
711 eventHub->AddSupportedState(UI_STATE_PRESSED);
712 eventHub->SetSupportedStates(UI_STATE_PRESSED);
713
714 /**
715 * @tc.steps: step3. Call MarkModifyDone, stateStyleMgr_->HasStateStyle(UI_STATE_PRESSED) is a true branch.
716 * @tc.expected: retFlag is true.
717 */
718 eventHub->MarkModifyDone();
719 bool retFlag = eventHub->stateStyleMgr_->HasStateStyle(UI_STATE_PRESSED);
720 EXPECT_TRUE(retFlag);
721
722 /**
723 * @tc.steps: step4. Update SupportedState in eventHub using UI_STATE_DISABLED.
724 */
725 eventHub->AddSupportedState(UI_STATE_DISABLED);
726 eventHub->SetSupportedStates(UI_STATE_DISABLED);
727
728 /**
729 * @tc.steps: step5. Call MarkModifyDone, stateStyleMgr_->HasStateStyle(UI_STATE_DISABLED) is a true branch.
730 * @tc.expected: retFlag is true.
731 */
732 eventHub->MarkModifyDone();
733 retFlag = eventHub->stateStyleMgr_->HasStateStyle(UI_STATE_DISABLED);
734 EXPECT_TRUE(retFlag);
735
736 /**
737 * @tc.steps: step6. Update SupportedState in eventHub using UI_STATE_DISABLED.
738 */
739 eventHub->AddSupportedState(UI_STATE_DISABLED);
740 eventHub->SetSupportedStates(UI_STATE_DISABLED);
741 eventHub->SetEnabled(false);
742
743 /**
744 * @tc.steps: step7. Call MarkModifyDone, enabled_ is a false branch.
745 * @tc.expected: eventHub->IsEnabled() return value is false.
746 */
747 eventHub->MarkModifyDone();
748 EXPECT_FALSE(eventHub->IsEnabled());
749 }
750
751 /**
752 * @tc.name: EventHubTest005
753 * @tc.desc: stateStyleMgr_ in Test SetSupportedStates is a fake branch
754 * @tc.type: FUNC
755 */
756 HWTEST_F(EventHubTestNg, EventHubTest005, TestSize.Level1)
757 {
758 /**
759 * @tc.steps: step1. Create EventHub.
760 * @tc.expected: eventHub is not null.
761 */
762 auto eventHub = AceType::MakeRefPtr<EventHub>();
763 EXPECT_NE(eventHub, nullptr);
764
765 /**
766 * @tc.steps: step2. Call SetSupportedStates with UI_STATE_PRESSED.
767 * @tc.expected: eventHub->stateStyleMgr_ is true.
768 */
769 eventHub->stateStyleMgr_ = nullptr;
770 eventHub->SetSupportedStates(UI_STATE_PRESSED);
771 EXPECT_TRUE(eventHub->stateStyleMgr_);
772 }
773 } // namespace OHOS::Ace::NG
774