From 6396ec3fcb34428a33e330ab00d1f1249bb7adc3 Mon Sep 17 00:00:00 2001 From: jinweiliu Date: Tue, 20 Jun 2023 15:27:22 +0000 Subject: [PATCH] add ut Signed-off-by: jinweiliu Change-Id: Icdd767746fddf4bacea0ccb3568da1d62aa452b3 --- .../core/components_ng/base/view_abstract.cpp | 10 ++ .../core/components_ng/event/click_event.cpp | 5 +- .../core/components_ng/event/click_event.h | 1 + .../event/click_event/click_event_test_ng.cpp | 99 ++++++++++- .../event/event_hub/event_hub_test_ng.cpp | 163 +++++++++++++++++- .../input_event_hub_test_ng.cpp | 110 +++++++++++- .../event/touch_event/touch_event_test_ng.cpp | 48 ++++++ 7 files changed, 431 insertions(+), 5 deletions(-) diff --git a/frameworks/core/components_ng/base/view_abstract.cpp b/frameworks/core/components_ng/base/view_abstract.cpp index 4e3e620bf64..21ef3ff1526 100644 --- a/frameworks/core/components_ng/base/view_abstract.cpp +++ b/frameworks/core/components_ng/base/view_abstract.cpp @@ -506,6 +506,7 @@ void ViewAbstract::SetBorderStyle(const BorderStyleProperty& value) void ViewAbstract::DisableOnClick() { + LOGD("Disable OnClick event"); auto gestureHub = ViewStackProcessor::GetInstance()->GetMainFrameNodeGestureEventHub(); CHECK_NULL_VOID(gestureHub); gestureHub->ClearUserOnClick(); @@ -513,6 +514,7 @@ void ViewAbstract::DisableOnClick() void ViewAbstract::DisableOnTouch() { + LOGD("Disable OnTouch event"); auto gestureHub = ViewStackProcessor::GetInstance()->GetMainFrameNodeGestureEventHub(); CHECK_NULL_VOID(gestureHub); gestureHub->ClearUserOnTouch(); @@ -520,6 +522,7 @@ void ViewAbstract::DisableOnTouch() void ViewAbstract::DisableOnKeyEvent() { + LOGD("Disable OnKey event"); auto focusHub = ViewStackProcessor::GetInstance()->GetOrCreateMainFrameNodeFocusHub(); CHECK_NULL_VOID(focusHub); focusHub->ClearUserOnKey(); @@ -527,6 +530,7 @@ void ViewAbstract::DisableOnKeyEvent() void ViewAbstract::DisableOnHover() { + LOGD("Disable OnHover event"); auto eventHub = ViewStackProcessor::GetInstance()->GetMainFrameNodeInputEventHub(); CHECK_NULL_VOID(eventHub); eventHub->ClearUserOnHover(); @@ -534,6 +538,7 @@ void ViewAbstract::DisableOnHover() void ViewAbstract::DisableOnMouse() { + LOGD("Disable OnMouse event"); auto eventHub = ViewStackProcessor::GetInstance()->GetMainFrameNodeInputEventHub(); CHECK_NULL_VOID(eventHub); eventHub->ClearUserOnMouse(); @@ -541,6 +546,7 @@ void ViewAbstract::DisableOnMouse() void ViewAbstract::DisableOnAppear() { + LOGD("Disable OnAppear event"); auto eventHub = ViewStackProcessor::GetInstance()->GetMainFrameNodeEventHub(); CHECK_NULL_VOID(eventHub); eventHub->ClearUserOnAppear(); @@ -548,6 +554,7 @@ void ViewAbstract::DisableOnAppear() void ViewAbstract::DisableOnDisAppear() { + LOGD("Disable OnDisAppear event"); auto eventHub = ViewStackProcessor::GetInstance()->GetMainFrameNodeEventHub(); CHECK_NULL_VOID(eventHub); eventHub->ClearUserOnDisAppear(); @@ -555,6 +562,7 @@ void ViewAbstract::DisableOnDisAppear() void ViewAbstract::DisableOnAreaChange() { + LOGD("Disable OnAreaChange event"); auto pipeline = PipelineContext::GetCurrentContext(); CHECK_NULL_VOID(pipeline); auto frameNode = ViewStackProcessor::GetInstance()->GetMainFrameNode(); @@ -564,6 +572,7 @@ void ViewAbstract::DisableOnAreaChange() void ViewAbstract::DisableOnFocus() { + LOGD("Disable OnFocus event"); auto focusHub = ViewStackProcessor::GetInstance()->GetOrCreateMainFrameNodeFocusHub(); CHECK_NULL_VOID(focusHub); focusHub->ClearUserOnFocus(); @@ -571,6 +580,7 @@ void ViewAbstract::DisableOnFocus() void ViewAbstract::DisableOnBlur() { + LOGD("Disable OnBlur event"); auto focusHub = ViewStackProcessor::GetInstance()->GetOrCreateMainFrameNodeFocusHub(); CHECK_NULL_VOID(focusHub); focusHub->ClearUserOnBlur(); diff --git a/frameworks/core/components_ng/event/click_event.cpp b/frameworks/core/components_ng/event/click_event.cpp index 0b2e0b987bd..b8fe5fbe7ec 100644 --- a/frameworks/core/components_ng/event/click_event.cpp +++ b/frameworks/core/components_ng/event/click_event.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -40,6 +40,7 @@ void ClickEventActuator::OnCollectTouchTarget(const OffsetF& coordinateOffset, c CHECK_NULL_VOID(frameNode); if (!clickRecognizer_) { + LOGD("Create clickRecognizer"); clickRecognizer_ = MakeRefPtr(); } clickRecognizer_->SetOnAction(GetClickEvent()); @@ -68,4 +69,4 @@ GestureEventFunc ClickEventActuator::GetClickEvent() return callback; } -} // namespace OHOS::Ace::NG \ No newline at end of file +} // namespace OHOS::Ace::NG diff --git a/frameworks/core/components_ng/event/click_event.h b/frameworks/core/components_ng/event/click_event.h index 80161efe24e..1f905f4936b 100644 --- a/frameworks/core/components_ng/event/click_event.h +++ b/frameworks/core/components_ng/event/click_event.h @@ -61,6 +61,7 @@ public: void SetUserCallback(GestureEventFunc&& callback) { + LOGD("Set usercallback"); if (userCallback_) { userCallback_.Reset(); } diff --git a/frameworks/core/components_ng/test/event/click_event/click_event_test_ng.cpp b/frameworks/core/components_ng/test/event/click_event/click_event_test_ng.cpp index 1d63179afa6..1a2a1ab1682 100755 --- a/frameworks/core/components_ng/test/event/click_event/click_event_test_ng.cpp +++ b/frameworks/core/components_ng/test/event/click_event/click_event_test_ng.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -36,11 +36,13 @@ namespace { constexpr double GESTURE_EVENT_PROPERTY_VALUE = 10.0; constexpr int32_t CLICK_TEST_RESULT_SIZE = 1; constexpr int32_t CLICK_TEST_RESULT_SIZE_2 = 2; +constexpr int32_t CLICK_TEST_RESULT_SIZE_0 = 0; constexpr uint32_t CLICK_EVENTS_SIZE = 1; constexpr uint32_t CLICK_EVENTS_SIZE_2 = 2; const TouchRestrict CLICK_TOUCH_RESTRICT = { TouchRestrict::CLICK }; constexpr float WIDTH = 400.0f; constexpr float HEIGHT = 400.0f; +const std::string RESULT_SUCCESS = "success"; const OffsetF COORDINATE_OFFSET(WIDTH, HEIGHT); } // namespace @@ -210,4 +212,99 @@ HWTEST_F(ClickEventTestNg, ClickEventActuatorTest003, TestSize.Level1) */ (*clickEventActuator.clickRecognizer_->onAction_)(info); } + +/** + * @tc.name: ClickEventActuatorTest004 + * @tc.desc: test clear user callback. + * @tc.type: FUNC + */ +HWTEST_F(ClickEventTestNg, ClickEventActuatorTest004, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create EventActuator. + */ + auto eventHub = AceType::MakeRefPtr(); + auto frameNode = AceType::MakeRefPtr(V2::TEXT_ETS_TAG, -1, AceType::MakeRefPtr()); + eventHub->AttachHost(frameNode); + auto gestureEventHub = AceType::MakeRefPtr(AceType::WeakClaim(AceType::RawPtr(eventHub))); + ClickEventActuator clickEventActuator = ClickEventActuator(AceType::WeakClaim(AceType::RawPtr(gestureEventHub))); + + /** + * @tc.steps: step2. Invoke OnCollectTouchTarget when clickEvents_ is empty and userCallback_ is not nullptr. + * @tc.expected: OnCollectTouchTarget will return directly and finalResult is 1. + */ + auto getEventTargetImpl = eventHub->CreateGetEventTargetImpl(); + EXPECT_NE(getEventTargetImpl, nullptr); + + TouchTestResult finalResult; + std::string result; + GestureEventFunc callback = [&result](GestureEvent& info) { result = RESULT_SUCCESS; }; + + clickEventActuator.SetUserCallback(std::move(callback)); + clickEventActuator.OnCollectTouchTarget(COORDINATE_OFFSET, CLICK_TOUCH_RESTRICT, getEventTargetImpl, finalResult); + EXPECT_NE(clickEventActuator.userCallback_->callback_, nullptr); + + GestureEvent info = GestureEvent(); + clickEventActuator.userCallback_->callback_(info); + EXPECT_EQ(result, RESULT_SUCCESS); + EXPECT_EQ(finalResult.size(), CLICK_TEST_RESULT_SIZE); + + clickEventActuator.ClearUserCallback(); + clickEventActuator.OnCollectTouchTarget(COORDINATE_OFFSET, CLICK_TOUCH_RESTRICT, getEventTargetImpl, finalResult); + EXPECT_EQ(clickEventActuator.userCallback_, nullptr); + EXPECT_EQ(finalResult.size(), CLICK_TEST_RESULT_SIZE); +} + +/** + * @tc.name: ClickEventActuatorTest005 + * @tc.desc: test user callback and clickevent are different. + * @tc.type: FUNC + */ +HWTEST_F(ClickEventTestNg, ClickEventActuatorTest005, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create EventActuator. + */ + auto eventHub = AceType::MakeRefPtr(); + auto frameNode = AceType::MakeRefPtr(V2::TEXT_ETS_TAG, -1, AceType::MakeRefPtr()); + eventHub->AttachHost(frameNode); + auto gestureEventHub = AceType::MakeRefPtr(AceType::WeakClaim(AceType::RawPtr(eventHub))); + ClickEventActuator clickEventActuator = ClickEventActuator(AceType::WeakClaim(AceType::RawPtr(gestureEventHub))); + + /** + * @tc.steps: step2. test clear callback and add event. + * @tc.expected: Add clickRecognizer_ to finalResult, and it's size is equal 1. + */ + auto getEventTargetImpl = eventHub->CreateGetEventTargetImpl(); + EXPECT_NE(getEventTargetImpl, nullptr); + clickEventActuator.ClearUserCallback(); + + GestureEventFunc callback = [](GestureEvent& info) {}; + auto clickEvent = AceType::MakeRefPtr(std::move(callback)); + clickEventActuator.AddClickEvent(clickEvent); + + TouchTestResult finalResult; + clickEventActuator.OnCollectTouchTarget(COORDINATE_OFFSET, CLICK_TOUCH_RESTRICT, getEventTargetImpl, finalResult); + EXPECT_EQ(finalResult.size(), CLICK_TEST_RESULT_SIZE); + + /** + * @tc.steps: step3. test clear callback again. + * @tc.expected: Add clickRecognizer_ to finalResult, and it's size is equal 1. + */ + TouchTestResult finalResultAfterClear; + clickEventActuator.ClearUserCallback(); + clickEventActuator.OnCollectTouchTarget( + COORDINATE_OFFSET, CLICK_TOUCH_RESTRICT, getEventTargetImpl, finalResultAfterClear); + EXPECT_EQ(finalResultAfterClear.size(), CLICK_TEST_RESULT_SIZE); + + /** + * @tc.steps: step4. test clear event again. + * @tc.expected: callback and event are null, and it's size is equal 0. + */ + clickEventActuator.RemoveClickEvent(clickEvent); + TouchTestResult finalResultAfterClearEvent; + clickEventActuator.OnCollectTouchTarget( + COORDINATE_OFFSET, CLICK_TOUCH_RESTRICT, getEventTargetImpl, finalResultAfterClearEvent); + EXPECT_EQ(finalResultAfterClearEvent.size(), CLICK_TEST_RESULT_SIZE_0); +} } // namespace OHOS::Ace::NG diff --git a/frameworks/core/components_ng/test/event/event_hub/event_hub_test_ng.cpp b/frameworks/core/components_ng/test/event/event_hub/event_hub_test_ng.cpp index f95b5143bcd..1d8fa4ccfbc 100644 --- a/frameworks/core/components_ng/test/event/event_hub/event_hub_test_ng.cpp +++ b/frameworks/core/components_ng/test/event/event_hub/event_hub_test_ng.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -15,6 +15,7 @@ #include "gtest/gtest.h" +#define private public #include "base/geometry/ng/offset_t.h" #include "base/memory/ace_type.h" #include "base/memory/referenced.h" @@ -50,12 +51,19 @@ const float NEW_HEIGHT = 500.0f; const RectF NEW_RECT = RectF(NEW_X_VALUE, NEW_Y_VALUE, NEW_WIDTH, NEW_HEIGHT); const OffsetF NEW_ORIGIN = OffsetF(NEW_WIDTH, NEW_HEIGHT); +const RectF RECT_DELTA = RectF(1.0f, 1.0f, 1.0f, 1.0f); +const OffsetF OFFSET_DELTA = OffsetF(1.0f, 1.0f); +const OffsetF ORIGIN_DELTA = OffsetF(1.0f, 1.0f); + const std::string STRINGCTER_A = "A"; const std::string STRINGCTER_Q = "Q"; const std::string STRINGCTER_E = "E"; constexpr int32_t NUM_CTRL_VALUE = 1; constexpr int32_t NUM_SHIFT_VALUE = 2; constexpr int32_t NUM_ALT_VALUE = 4; + +const std::string RESULT_SUCCESS_ONE = "sucess1"; +const std::string RESULT_SUCCESS_TWO = "sucess2"; } // namespace class EventHubTestNg : public testing::Test { @@ -307,4 +315,157 @@ HWTEST_F(EventHubTestNg, EventHubDragEventsTest005, TestSize.Level1) } keyboardShortcut.clear(); } + +/** + * @tc.name: EventHubDisableAreaChange001 + * @tc.desc: Create EventHub and test disable areaChange function. + * @tc.type: FUNC + */ +HWTEST_F(EventHubTestNg, EventHubDisableAreaChange001, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create EventHub. + * @tc.expected: eventHub is not null. + */ + auto eventHub = AceType::MakeRefPtr(); + EXPECT_NE(eventHub, nullptr); + + /** + * @tc.steps: step2. Set EventHub OnAreaChanged function and fire it. + * @tc.expected: onAreaChanged is invoked and the temp values are assigned with correct values. + */ + RectF tempOldRect; + OffsetF tempOldOrigin; + RectF tempNewRect; + OffsetF tempNewOrigin; + auto onAreaChanged = [&tempOldRect, &tempOldOrigin, &tempNewRect, &tempNewOrigin]( + const RectF& oldRect, const OffsetF& oldOrigin, const RectF& rect, const OffsetF& origin) { + tempOldRect = oldRect; + tempOldOrigin = oldOrigin; + tempNewRect = rect; + tempNewOrigin = origin; + }; + + eventHub->SetOnAreaChanged(onAreaChanged); + eventHub->FireOnAreaChanged(OLD_RECT, OLD_ORIGIN, NEW_RECT, NEW_ORIGIN); + EXPECT_TRUE(eventHub->HasOnAreaChanged()); + EXPECT_EQ(tempOldRect, OLD_RECT); + EXPECT_EQ(tempOldOrigin, OLD_ORIGIN); + EXPECT_EQ(tempNewRect, NEW_RECT); + EXPECT_EQ(tempNewOrigin, NEW_ORIGIN); + + /** + * @tc.steps: step3. Clear the callback. + * @tc.expected: onAreaChanged is empty. + */ + eventHub->ClearUserOnAreaChanged(); + eventHub->FireOnAreaChanged( + OLD_RECT + OFFSET_DELTA, OLD_ORIGIN + ORIGIN_DELTA, NEW_RECT + OFFSET_DELTA, NEW_ORIGIN + ORIGIN_DELTA); + EXPECT_FALSE(eventHub->HasOnAreaChanged()); + EXPECT_EQ(tempOldRect, OLD_RECT); + EXPECT_EQ(tempOldOrigin, OLD_ORIGIN); + EXPECT_EQ(tempNewRect, NEW_RECT); + EXPECT_EQ(tempNewOrigin, NEW_ORIGIN); + + /** + * @tc.steps: step3. Set/fire areaChange function. + * @tc.expected: areaChange is assigned with correct value. + */ + eventHub->SetOnAreaChanged(onAreaChanged); + eventHub->FireOnAreaChanged( + OLD_RECT + OFFSET_DELTA, OLD_ORIGIN + ORIGIN_DELTA, NEW_RECT + OFFSET_DELTA, NEW_ORIGIN + ORIGIN_DELTA); + EXPECT_TRUE(eventHub->HasOnAreaChanged()); + EXPECT_EQ(tempOldRect, OLD_RECT + OFFSET_DELTA); + EXPECT_EQ(tempOldOrigin, OLD_ORIGIN + ORIGIN_DELTA); + EXPECT_EQ(tempNewRect, NEW_RECT + OFFSET_DELTA); + EXPECT_EQ(tempNewOrigin, NEW_ORIGIN + ORIGIN_DELTA); +} + +/** + * @tc.name: EventHubDisableAppear001 + * @tc.desc: Create EventHub and test disable onAppear function. + * @tc.type: FUNC + */ +HWTEST_F(EventHubTestNg, EventHubDisableAppear001, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create EventHub. + * @tc.expected: eventHub is not null. + */ + auto eventHub = AceType::MakeRefPtr(); + EXPECT_NE(eventHub, nullptr); + + /** + * @tc.steps: step2. Set EventHub onAppear function and fire it. + * @tc.expected: onAppear is invoked and the temp values are assigned with correct values. + */ + std::string result; + auto onAppear = [&result]() { result = RESULT_SUCCESS_ONE; }; + + eventHub->SetOnAppear(onAppear); + EXPECT_NE(eventHub->onAppear_, nullptr); + eventHub->onAppear_(); + EXPECT_EQ(result, RESULT_SUCCESS_ONE); + /** + * @tc.steps: step3. Clear the callback. + * @tc.expected: onAppear is empty. + */ + eventHub->ClearUserOnAppear(); + EXPECT_EQ(eventHub->onAppear_, nullptr); + + /** + * @tc.steps: step3. Set/fire EventHub onAppear function. + * @tc.expected: onAppear is assigned with correct value. + */ + auto onAppear2 = [&result]() { result = RESULT_SUCCESS_TWO; }; + + eventHub->SetOnAppear(onAppear2); + EXPECT_NE(eventHub->onAppear_, nullptr); + eventHub->onAppear_(); + EXPECT_EQ(result, RESULT_SUCCESS_TWO); +} + +/** + * @tc.name: EventHubDisableDisAppear001 + * @tc.desc: Create EventHub and test disable onDisAppear function. + * @tc.type: FUNC + */ +HWTEST_F(EventHubTestNg, EventHubDisableDisAppear001, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create EventHub. + * @tc.expected: eventHub is not null. + */ + auto eventHub = AceType::MakeRefPtr(); + EXPECT_NE(eventHub, nullptr); + + /** + * @tc.steps: step2. Set EventHub onDisAppear function and fire it. + * @tc.expected: onDisAppear is invoked and the temp values are assigned with correct values. + */ + std::string result; + auto onDisAppear = [&result]() { result = RESULT_SUCCESS_ONE; }; + + eventHub->SetOnDisappear(onDisAppear); + EXPECT_NE(eventHub->onDisappear_, nullptr); + eventHub->onDisappear_(); + EXPECT_EQ(result, RESULT_SUCCESS_ONE); + /** + * @tc.steps: step3. Clear the callback. + * @tc.expected: onDisAppear is empty. + */ + eventHub->ClearUserOnDisAppear(); + EXPECT_EQ(eventHub->onDisappear_, nullptr); + + /** + * @tc.steps: step3. Set/fire EventHub onDisappear function. + * @tc.expected: disAppear is assigned with correct value. + */ + auto onDisAppear2 = [&result]() { result = RESULT_SUCCESS_TWO; }; + + eventHub->SetOnDisappear(onDisAppear2); + EXPECT_NE(eventHub->onDisappear_, nullptr); + eventHub->onDisappear_(); + EXPECT_EQ(result, RESULT_SUCCESS_TWO); +} } // namespace OHOS::Ace::NG diff --git a/frameworks/core/components_ng/test/event/input_event_hub/input_event_hub_test_ng.cpp b/frameworks/core/components_ng/test/event/input_event_hub/input_event_hub_test_ng.cpp index 2ce08c6b8ca..d14a470dbb7 100644 --- a/frameworks/core/components_ng/test/event/input_event_hub/input_event_hub_test_ng.cpp +++ b/frameworks/core/components_ng/test/event/input_event_hub/input_event_hub_test_ng.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -40,6 +40,8 @@ constexpr float WIDTH = 400.0f; constexpr float HEIGHT = 400.0f; const OffsetF COORDINATE_OFFSET(WIDTH, HEIGHT); constexpr bool HOVER_VALUE = true; +const std::string RESULT_SUCCESS_ONE = "sucess1"; +const std::string RESULT_SUCCESS_TWO = "sucess2"; } // namespace class InputEventHubTestNg : public testing::Test { @@ -410,4 +412,110 @@ HWTEST_F(InputEventHubTestNg, InputEventHubBindContextMenuTest007, TestSize.Leve inputEventHub->BindContextMenu(std::move(onMouse2)); EXPECT_NE(inputEventHub->mouseEventActuator_, nullptr); } + +/** + * @tc.name: DisableMouseEvent001 + * @tc.desc: Test disable mouse event. + * @tc.type: FUNC + */ +HWTEST_F(InputEventHubTestNg, DisableMouseEvent001, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create InputEventHub. + */ + auto eventHub = AceType::MakeRefPtr(); + auto frameNode = AceType::MakeRefPtr(V2::TEXT_ETS_TAG, -1, AceType::MakeRefPtr()); + eventHub->AttachHost(frameNode); + auto inputEventHub = AceType::MakeRefPtr(AceType::WeakClaim(AceType::RawPtr(eventHub))); + EXPECT_NE(inputEventHub, nullptr); + + /** + * @tc.steps: step2. Initialize mouseEventActuator_, and set callback + * @tc.expected: callback is right. + */ + inputEventHub->mouseEventActuator_ = + AceType::MakeRefPtr(AceType::WeakClaim(AceType::RawPtr(inputEventHub))); + std::string result; + OnMouseEventFunc onMouse = [&result](MouseInfo& info) { result = RESULT_SUCCESS_ONE; }; + inputEventHub->SetMouseEvent(std::move(onMouse)); + EXPECT_NE(inputEventHub->mouseEventActuator_->userCallback_, nullptr); + + MouseInfo press; + press.SetButton(MouseButton::LEFT_BUTTON); + press.SetAction(MouseAction::PRESS); + inputEventHub->mouseEventActuator_->userCallback_->onMouseCallback_(press); + EXPECT_EQ(result, RESULT_SUCCESS_ONE); + + /** + * @tc.steps: step3. Clear the callback. + * @tc.expected: callback is null. + */ + inputEventHub->ClearUserOnMouse(); + EXPECT_EQ(inputEventHub->mouseEventActuator_->userCallback_, nullptr); + + /** + * @tc.steps: step4. Set the callback again. + * @tc.expected: callback is right. + */ + OnMouseEventFunc onMouse2 = [&result](MouseInfo& info) { result = RESULT_SUCCESS_TWO; }; + inputEventHub->SetMouseEvent(std::move(onMouse2)); + EXPECT_NE(inputEventHub->mouseEventActuator_->userCallback_, nullptr); + + MouseInfo release; + release.SetButton(MouseButton::LEFT_BUTTON); + release.SetAction(MouseAction::RELEASE); + inputEventHub->mouseEventActuator_->userCallback_->onMouseCallback_(release); + EXPECT_EQ(result, RESULT_SUCCESS_TWO); +} + +/** + * @tc.name: DisableHoverEvent001 + * @tc.desc: Test disable hover event. + * @tc.type: FUNC + */ +HWTEST_F(InputEventHubTestNg, DisableHoverEvent001, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create InputEventHub. + */ + auto eventHub = AceType::MakeRefPtr(); + auto frameNode = AceType::MakeRefPtr(V2::TEXT_ETS_TAG, -1, AceType::MakeRefPtr()); + eventHub->AttachHost(frameNode); + auto inputEventHub = AceType::MakeRefPtr(AceType::WeakClaim(AceType::RawPtr(eventHub))); + EXPECT_NE(inputEventHub, nullptr); + + /** + * @tc.steps: step2. Initialize hoverEventActuator_, and set callback + * @tc.expected: callback is right. + */ + inputEventHub->hoverEventActuator_ = + AceType::MakeRefPtr(AceType::WeakClaim(AceType::RawPtr(inputEventHub))); + std::string result; + OnHoverFunc onHover = [&result](bool, HoverInfo) { result = RESULT_SUCCESS_ONE; }; + inputEventHub->SetHoverEvent(std::move(onHover)); + EXPECT_NE(inputEventHub->hoverEventActuator_->userCallback_, nullptr); + + HoverInfo hover; + inputEventHub->hoverEventActuator_->userCallback_->onHoverEventCallback_(true, hover); + EXPECT_EQ(result, RESULT_SUCCESS_ONE); + + /** + * @tc.steps: step3. Clear the callback. + * @tc.expected: callback is null. + */ + inputEventHub->ClearUserOnHover(); + EXPECT_EQ(inputEventHub->hoverEventActuator_->userCallback_, nullptr); + + /** + * @tc.steps: step4. Set the callback again. + * @tc.expected: callback is right. + */ + OnHoverFunc onHover2 = [&result](bool, HoverInfo) { result = RESULT_SUCCESS_TWO; }; + inputEventHub->SetHoverEvent(std::move(onHover2)); + EXPECT_NE(inputEventHub->hoverEventActuator_->userCallback_, nullptr); + + HoverInfo hover2; + inputEventHub->hoverEventActuator_->userCallback_->onHoverEventCallback_(true, hover2); + EXPECT_EQ(result, RESULT_SUCCESS_TWO); +} } // namespace OHOS::Ace::NG diff --git a/frameworks/core/components_ng/test/event/touch_event/touch_event_test_ng.cpp b/frameworks/core/components_ng/test/event/touch_event/touch_event_test_ng.cpp index 1e003d400b5..b481f914214 100644 --- a/frameworks/core/components_ng/test/event/touch_event/touch_event_test_ng.cpp +++ b/frameworks/core/components_ng/test/event/touch_event/touch_event_test_ng.cpp @@ -244,4 +244,52 @@ HWTEST_F(TouchEventTestNg, TouchEventActuatorHandleAndDispatchTest004, TestSize. const TouchEvent touchEvent3 { .pointers = POINTERS_2 }; EXPECT_TRUE(touchEventActuator->HandleEvent(touchEvent3)); } + +/** + * @tc.name: TouchEventDisable001 + * @tc.desc: Create TouchEventActuator and test disable. + * @tc.type: FUNC + */ +HWTEST_F(TouchEventTestNg, TouchEventDisable001, TestSize.Level1) +{ + /** + * @tc.steps: step1. Create TouchEventActuator. + * @tc.expected: touchEventActuator is not nullptr. + */ + auto touchEventActuator = AceType::MakeRefPtr(); + EXPECT_NE(touchEventActuator, nullptr); + + /** + * @tc.steps: step2. Invoke DispatchEvent. + * @tc.expected: TouchTestResult size has been increased one. + */ + const TouchEvent touchEvent { .tiltX = TILT_X_VALUE, .tiltY = TILT_Y_VALUE, .pointers = POINTERS }; + EXPECT_TRUE(touchEventActuator->DispatchEvent(touchEvent)); + + /** + * @tc.steps: step3. Invoke HandleEvent when touchEvents_ and userCallback_ is empty. + * @tc.expected: HandleEvent will return true directly. + */ + EXPECT_TRUE(touchEventActuator->HandleEvent(touchEvent)); + + /** + * @tc.steps: step4. Invoke ReplaceTouchEvent to initialize userCallback_. + */ + double unknownTiltX = 0.0; + const TouchEventFunc callback = [&unknownTiltX]( + TouchEventInfo& info) { unknownTiltX = info.GetTiltX().value_or(0.0); }; + TouchEventFunc callback1 = callback; + touchEventActuator->ReplaceTouchEvent(std::move(callback1)); + EXPECT_NE(touchEventActuator->userCallback_, nullptr); + + EXPECT_TRUE(touchEventActuator->HandleEvent(touchEvent)); + EXPECT_EQ(unknownTiltX, TILT_X_VALUE); + + /** + * @tc.steps: step5. Invoke Clear func to clear userCallback_. + */ + touchEventActuator->ClearUserCallback(); + EXPECT_EQ(touchEventActuator->userCallback_, nullptr); + EXPECT_TRUE(touchEventActuator->HandleEvent(touchEvent)); +} } // namespace OHOS::Ace::NG -- Gitee