• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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_ng/base/view_stack_processor.h"
19 #define private public
20 #include "core/components_ng/pattern/web/touch_event_listener.h"
21 #undef private
22 #include "core/components_ng/pattern/web/web_pattern.h"
23 #include "core/components_v2/inspector/inspector_constants.h"
24 
25 using namespace testing;
26 using namespace testing::ext;
27 using namespace OHOS::Ace;
28 
29 namespace OHOS::Ace::NG {
30 
31 class TouchEventListenerTest : public testing::Test {
32 public:
33     static void SetUpTestCase();
34     static void TearDownTestCase();
35     void SetUp() override;
36     void TearDown() override;
37 };
38 
SetUpTestCase()39 void TouchEventListenerTest::SetUpTestCase() {}
TearDownTestCase()40 void TouchEventListenerTest::TearDownTestCase() {}
SetUp()41 void TouchEventListenerTest::SetUp() {}
TearDown()42 void TouchEventListenerTest::TearDown() {}
43 
44 /**
45  * @tc.name: TouchEventListenerTest_001
46  * @tc.desc: Test function GetPatternFromListener.
47  * @tc.type: FUNC
48  */
49 HWTEST_F(TouchEventListenerTest, GetPatternFromListener_001, TestSize.Level1)
50 {
51     TouchEventListener touchEventListener;
52     WeakPtr<NG::Pattern> patternSet;
53     touchEventListener.SetPatternToListener(patternSet);
54     auto patternGet = touchEventListener.GetPatternFromListener();
55     EXPECT_EQ(patternSet, patternGet);
56 }
57 
58 /**
59  * @tc.name: TouchEventListenerTest_002
60  * @tc.desc: Test function OnTouchEvent in abnormal condition.
61  * @tc.type: FUNC
62  */
63 HWTEST_F(TouchEventListenerTest, OnTouchEvent_001, TestSize.Level1)
64 {
65     TouchEventListener touchEventListener;
66     WeakPtr<NG::Pattern> patternSet;
67     touchEventListener.SetPatternToListener(patternSet);
68     touchEventListener.OnTouchEvent();
69     EXPECT_EQ(touchEventListener.pattern_.Upgrade(), nullptr);
70 }
71 
72 /**
73  * @tc.name: TouchEventListenerTest_003
74  * @tc.desc: Test function OnTouchEvent in normal condition.
75  * @tc.type: FUNC
76  */
77 HWTEST_F(TouchEventListenerTest, OnTouchEvent_002, TestSize.Level1)
78 {
79     auto* stack = ViewStackProcessor::GetInstance();
80     auto nodeId = stack->ClaimNodeId();
__anonc01854d00102() 81     auto frameNode = FrameNode::GetOrCreateFrameNode(V2::WEB_ETS_TAG, nodeId, []() {
82         return AceType::MakeRefPtr<WebPattern>();
83     });
84     ASSERT_NE(frameNode, nullptr);
85     stack->Push(frameNode);
86 
87     auto webPattern = frameNode->GetPattern<WebPattern>();
88     EXPECT_NE(webPattern, nullptr);
89     TouchEventListener touchEventListener;
90     touchEventListener.SetPatternToListener(webPattern);
91     touchEventListener.OnTouchEvent();
92 }
93 
94 } // namespace OHOS::Ace::NG
95