1 /*
2 * Copyright (c) 2023 iSoftStone Information Technology (Group) 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 protected public
19 #define private public
20
21 #include "base/memory/ace_type.h"
22 #include "base/memory/referenced.h"
23 #include "core/components_ng/base/frame_node.h"
24 #include "core/components_ng/base/observer_handler.h"
25 #include "core/components_ng/base/ui_node.h"
26 #include "core/components_ng/pattern/scroll/scroll_pattern.h"
27 #include "core/components_ng/pattern/navigation/navigation_pattern.h"
28 #include "core/components_ng/pattern/navrouter/navdestination_layout_property.h"
29 #include "core/components_ng/pattern/navrouter/navdestination_model_ng.h"
30 #include "core/components_ng/pattern/navrouter/navdestination_pattern.h"
31 #include "core/components_v2/inspector/inspector_constants.h"
32 #include "test/mock/core/common/mock_container.h"
33
34 using namespace testing;
35 using namespace testing::ext;
36
37 namespace OHOS::Ace::NG {
38 namespace {
39 const float DEFAULT_DENSITY = 3.5f;
40 } // namespace
41 class ObserverTestNg : public testing::Test {
42 public:
43 static void SetUpTestCase();
44 static void TearDownTestCase();
45 };
46
SetUpTestCase()47 void ObserverTestNg::SetUpTestCase()
48 {
49 MockContainer::SetUp();
50 }
51
TearDownTestCase()52 void ObserverTestNg::TearDownTestCase()
53 {
54 MockContainer::TearDown();
55 }
56
57
58 /**
59 * @tc.name: ObserverTestNg001
60 * @tc.desc: Test the operation of Observer
61 * @tc.type: FUNC
62 */
63 HWTEST_F(ObserverTestNg, ObserverTestNg001, TestSize.Level1)
64 {
65 auto navigation = NavigationGroupNode::GetOrCreateGroupNode(
__anon27b6ed4b0202() 66 "navigation", 11, []() { return AceType::MakeRefPtr<NavigationPattern>(); });
67 auto contentNode = NavDestinationGroupNode::GetOrCreateGroupNode(
__anon27b6ed4b0302() 68 V2::NAVDESTINATION_VIEW_ETS_TAG, 22, []() { return AceType::MakeRefPtr<NavDestinationPattern>(); });
69 auto pattern = contentNode->GetPattern<NavDestinationPattern>();
70 UIObserverHandler::GetInstance().NotifyNavigationStateChange(AceType::WeakClaim(Referenced::RawPtr(pattern)),
71 NavDestinationState::ON_SHOWN);
72 ASSERT_EQ(UIObserverHandler::GetInstance().navigationHandleFunc_, nullptr);
73 auto pattern1 = navigation->GetPattern<NavigationPattern>();
74 pattern1->navigationStack_ = AceType::MakeRefPtr<NavigationStack>();
75 UIObserverHandler::GetInstance().NotifyNavigationStateChange(AceType::WeakClaim(Referenced::RawPtr(pattern1)),
76 NavDestinationState::ON_SHOWN);
77 ASSERT_EQ(UIObserverHandler::GetInstance().navigationHandleFunc_, nullptr);
78 }
79
80 /**
81 * @tc.name: ObserverTestNg002
82 * @tc.desc: Test the operation of Observer
83 * @tc.type: FUNC
84 */
85 HWTEST_F(ObserverTestNg, ObserverTestNg002, TestSize.Level1)
86 {
87 auto navigation = NavigationGroupNode::GetOrCreateGroupNode(
__anon27b6ed4b0402() 88 "navigation", 11, []() { return AceType::MakeRefPtr<NavigationPattern>(); });
89 navigation->GetPattern<NavigationPattern>()->navigationStack_ = AceType::MakeRefPtr<NavigationStack>();
90 auto contentNode = NavDestinationGroupNode::GetOrCreateGroupNode(
__anon27b6ed4b0502() 91 V2::NAVDESTINATION_VIEW_ETS_TAG, 22, []() { return AceType::MakeRefPtr<NavDestinationPattern>(); });
92 auto pathInfo = AceType::MakeRefPtr<NavPathInfo>();
93 ASSERT_NE(pathInfo, nullptr);
94 auto context = AceType::MakeRefPtr<NavDestinationContext>();
95 ASSERT_NE(context, nullptr);
96 context->SetNavPathInfo(pathInfo);
97
98 auto pattern = contentNode->GetPattern<NavDestinationPattern>();
99 pattern->SetNavDestinationContext(context);
100 pattern->name_ = "test_name";
101 pattern->isOnShow_ = true;
102 pattern->navigationNode_ = AceType::WeakClaim(Referenced::RawPtr(navigation));
103
104 auto info = UIObserverHandler::GetInstance().GetNavigationState(nullptr);
105 ASSERT_EQ(info, nullptr);
106
107 info = UIObserverHandler::GetInstance().GetNavigationState(navigation);
108 ASSERT_EQ(info, nullptr);
109
110 ASSERT_EQ(pattern->GetNavigationNode(), navigation);
111
112 info = UIObserverHandler::GetInstance().GetNavigationState(contentNode);
113 ASSERT_NE(info, nullptr);
114 ASSERT_EQ(info->name, "test_name");
115 ASSERT_EQ(info->navigationId, "");
116 ASSERT_EQ(info->state, NavDestinationState::ON_SHOWN);
117 }
118
119 /**
120 * @tc.name: ObserverTestNg003
121 * @tc.desc: Test the operation of Observer
122 * @tc.type: FUNC
123 */
124 HWTEST_F(ObserverTestNg, ObserverTestNg003, TestSize.Level1)
125 {
126 auto frameNode = FrameNode::GetOrCreateFrameNode(
__anon27b6ed4b0602() 127 V2::SCROLL_ETS_TAG, 12, []() { return AceType::MakeRefPtr<ScrollPattern>(); });
128 auto pattern = frameNode->GetPattern<ScrollablePattern>();
129 UIObserverHandler::GetInstance().NotifyScrollEventStateChange(AceType::WeakClaim(Referenced::RawPtr(pattern)),
130 ScrollEventType::SCROLL_START);
131 ASSERT_EQ(UIObserverHandler::GetInstance().scrollEventHandleFunc_, nullptr);
132 }
133
134 /**
135 * @tc.name: ObserverTestNg004
136 * @tc.desc: Test the operation of Observer
137 * @tc.type: FUNC
138 */
139 HWTEST_F(ObserverTestNg, ObserverTestNg004, TestSize.Level1)
140 {
141 auto frameNode = FrameNode::GetOrCreateFrameNode(
__anon27b6ed4b0702() 142 V2::SCROLL_ETS_TAG, 12, []() { return AceType::MakeRefPtr<ScrollPattern>(); });
143 auto pattern = frameNode->GetPattern<ScrollablePattern>();
144 double offset = 0.0f;
145 pattern->UpdateCurrentOffset(offset, SCROLL_FROM_AXIS);
146
147 auto info = UIObserverHandler::GetInstance().GetScrollEventState(frameNode);
148 ASSERT_EQ(info->id, frameNode->GetInspectorId().value_or(""));
149 ASSERT_EQ(info->uniqueId, frameNode->GetId());
150 ASSERT_EQ(info->scrollEvent, ScrollEventType::SCROLL_START);
151 ASSERT_EQ(info->offset, offset);
152 }
153
154 /**
155 * @tc.name: ObserverTestNg005
156 * @tc.desc: Test the operation of Observer
157 * @tc.type: FUNC
158 */
159 HWTEST_F(ObserverTestNg, ObserverTestNg005, TestSize.Level1)
160 {
161 auto targetDensity = DEFAULT_DENSITY;
__anon27b6ed4b0802(AbilityContextInfo& context, double density) 162 UIObserverHandler::GetInstance().densityHandleFunc_ = [](AbilityContextInfo& context, double density) -> void {
163 EXPECT_EQ(density, DEFAULT_DENSITY);
164 };
165 UIObserverHandler::GetInstance().NotifyDensityChange(targetDensity);
166 }
167
168 /**
169 * @tc.name: ObserverTestNg006
170 * @tc.desc: Test the operation of Observer
171 * @tc.type: FUNC
172 */
173 HWTEST_F(ObserverTestNg, ObserverTestNg006, TestSize.Level1)
174 {
175 std::optional<NavDestinationInfo> from;
176 std::optional<NavDestinationInfo> to;
177 NavigationOperation operation = NavigationOperation::PUSH;
178 UIObserverHandler::NavDestinationSwitchHandleFunc handleFunc = [](const AbilityContextInfo&,
__anon27b6ed4b0902(const AbilityContextInfo&, NavDestinationSwitchInfo& info) 179 NavDestinationSwitchInfo& info) -> void {
180 EXPECT_EQ(info.operation, NavigationOperation::PUSH);
181 };
182 UIObserverHandler::GetInstance().navDestinationSwitchHandleFunc_ = handleFunc;
183 UIObserverHandler::GetInstance().NotifyNavDestinationSwitch(std::move(from), std::move(to), operation);
184 }
185
186 /**
187 * @tc.name: ObserverTestNg007
188 * @tc.desc: Test the operation of Observer
189 * @tc.type: FUNC
190 */
191 HWTEST_F(ObserverTestNg, ObserverTestNg007, TestSize.Level1)
192 {
193 GestureEvent gestureEventInfo;
194 ClickInfo clickInfo = ClickInfo(0);
195 RefPtr<FrameNode> frameNode = nullptr;
196
197 UIObserverHandler::GetInstance().NotifyWillClick(gestureEventInfo, clickInfo, frameNode);
198 ASSERT_EQ(UIObserverHandler::GetInstance().willClickHandleFunc_, nullptr);
199 }
200
201 /**
202 * @tc.name: ObserverTestNg008
203 * @tc.desc: Test the operation of Observer
204 * @tc.type: FUNC
205 */
206 HWTEST_F(ObserverTestNg, ObserverTestNg008, TestSize.Level1)
207 {
208 GestureEvent gestureEventInfo;
209 ClickInfo clickInfo = ClickInfo(0);
210 RefPtr<FrameNode> frameNode = nullptr;
211
212 UIObserverHandler::GetInstance().NotifyDidClick(gestureEventInfo, clickInfo, frameNode);
213 ASSERT_EQ(UIObserverHandler::GetInstance().didClickHandleFunc_, nullptr);
214 }
215
216 /**
217 * @tc.name: ObserverTestNg009
218 * @tc.desc: Test the func of GetHandleNavDestinationSwitchFunc
219 * @tc.type: FUNC
220 */
221 HWTEST_F(ObserverTestNg, ObserverTestNg009, TestSize.Level1)
222 {
223 std::optional<NavDestinationInfo> from;
224 std::optional<NavDestinationInfo> to;
225 UIObserverHandler::NavDestinationSwitchHandleFunc handleFunc = [](const AbilityContextInfo&,
__anon27b6ed4b0a02(const AbilityContextInfo&, NavDestinationSwitchInfo&) 226 NavDestinationSwitchInfo&) -> void {};
227 UIObserverHandler::GetInstance().navDestinationSwitchHandleFunc_ = handleFunc;
228 UIObserverHandler::NavDestinationSwitchHandleFunc func =
229 UIObserverHandler::GetInstance().GetHandleNavDestinationSwitchFunc();
230 ASSERT_NE(handleFunc, nullptr);
231 ASSERT_NE(func, nullptr);
232 }
233 }
234