• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_content_pattern.h"
28 #include "core/components_ng/pattern/navigation/navigation_pattern.h"
29 #include "core/components_ng/pattern/navrouter/navdestination_layout_property.h"
30 #include "core/components_ng/pattern/navrouter/navdestination_model_ng.h"
31 #include "core/components_ng/pattern/navrouter/navdestination_pattern.h"
32 #include "core/components_v2/inspector/inspector_constants.h"
33 #include "test/mock/core/common/mock_container.h"
34 
35 using namespace testing;
36 using namespace testing::ext;
37 
38 namespace OHOS::Ace::NG {
39 namespace {
40 const float DEFAULT_DENSITY =  3.5f;
41 } // namespace
42 class ObserverTestNg : public testing::Test {
43 public:
44     static void SetUpTestCase();
45     static void TearDownTestCase();
46 };
47 
SetUpTestCase()48 void ObserverTestNg::SetUpTestCase()
49 {
50     MockContainer::SetUp();
51 }
52 
TearDownTestCase()53 void ObserverTestNg::TearDownTestCase()
54 {
55     MockContainer::TearDown();
56 }
57 
58 
59 /**
60  * @tc.name: ObserverTestNg001
61  * @tc.desc: Test the operation of Observer
62  * @tc.type: FUNC
63  */
64 HWTEST_F(ObserverTestNg, ObserverTestNg001, TestSize.Level1)
65 {
66     auto navigation = NavigationGroupNode::GetOrCreateGroupNode(
__anona5da01530202() 67         "navigation", 11, []() { return AceType::MakeRefPtr<NavigationPattern>(); });
68     auto contentNode = NavDestinationGroupNode::GetOrCreateGroupNode(
__anona5da01530302() 69         V2::NAVDESTINATION_VIEW_ETS_TAG, 22, []() { return AceType::MakeRefPtr<NavDestinationPattern>(); });
70     auto pattern = contentNode->GetPattern<NavDestinationPattern>();
71     UIObserverHandler::GetInstance().NotifyNavigationStateChange(AceType::WeakClaim(Referenced::RawPtr(pattern)),
72                                                                  NavDestinationState::ON_SHOWN);
73     ASSERT_EQ(UIObserverHandler::GetInstance().navigationHandleFunc_, nullptr);
74     auto pattern1 = navigation->GetPattern<NavigationPattern>();
75     pattern1->navigationStack_ = AceType::MakeRefPtr<NavigationStack>();
76     UIObserverHandler::GetInstance().NotifyNavigationStateChange(AceType::WeakClaim(Referenced::RawPtr(pattern1)),
77                                                                  NavDestinationState::ON_SHOWN);
78     ASSERT_EQ(UIObserverHandler::GetInstance().navigationHandleFunc_, nullptr);
79 }
80 
81 /**
82  * @tc.name: ObserverTestNg002
83  * @tc.desc: Test the operation of Observer
84  * @tc.type: FUNC
85  */
86 HWTEST_F(ObserverTestNg, ObserverTestNg002, TestSize.Level1)
87 {
88     auto navigation = NavigationGroupNode::GetOrCreateGroupNode(
__anona5da01530402() 89         "navigation", 11, []() { return AceType::MakeRefPtr<NavigationPattern>(); });
90     navigation->GetPattern<NavigationPattern>()->navigationStack_ = AceType::MakeRefPtr<NavigationStack>();
91     auto contentNode = NavDestinationGroupNode::GetOrCreateGroupNode(
__anona5da01530502() 92         V2::NAVDESTINATION_VIEW_ETS_TAG, 22, []() { return AceType::MakeRefPtr<NavDestinationPattern>(); });
93     auto pathInfo = AceType::MakeRefPtr<NavPathInfo>();
94     ASSERT_NE(pathInfo, nullptr);
95     auto context = AceType::MakeRefPtr<NavDestinationContext>();
96     ASSERT_NE(context, nullptr);
97     context->SetNavPathInfo(pathInfo);
98 
99     auto pattern = contentNode->GetPattern<NavDestinationPattern>();
100     pattern->SetNavDestinationContext(context);
101     pattern->name_ = "test_name";
102     pattern->isOnShow_ = true;
103     pattern->navigationNode_ = AceType::WeakClaim(Referenced::RawPtr(navigation));
104 
105     auto info = UIObserverHandler::GetInstance().GetNavigationState(nullptr);
106     ASSERT_EQ(info, nullptr);
107 
108     info = UIObserverHandler::GetInstance().GetNavigationState(navigation);
109     ASSERT_EQ(info, nullptr);
110 
111     ASSERT_EQ(pattern->GetNavigationNode(), navigation);
112 
113     info = UIObserverHandler::GetInstance().GetNavigationState(contentNode);
114     ASSERT_NE(info, nullptr);
115     ASSERT_EQ(info->name, "test_name");
116     ASSERT_EQ(info->navigationId, "");
117     ASSERT_EQ(info->state, NavDestinationState::ON_SHOWN);
118 }
119 
120 /**
121  * @tc.name: ObserverTestNg003
122  * @tc.desc: Test the operation of Observer
123  * @tc.type: FUNC
124  */
125 HWTEST_F(ObserverTestNg, ObserverTestNg003, TestSize.Level1)
126 {
127     auto frameNode = FrameNode::GetOrCreateFrameNode(
__anona5da01530602() 128         V2::SCROLL_ETS_TAG, 12, []() { return AceType::MakeRefPtr<ScrollPattern>(); });
129     auto pattern = frameNode->GetPattern<ScrollablePattern>();
130     UIObserverHandler::GetInstance().NotifyScrollEventStateChange(AceType::WeakClaim(Referenced::RawPtr(pattern)),
131                                                                  ScrollEventType::SCROLL_START);
132     ASSERT_EQ(UIObserverHandler::GetInstance().scrollEventHandleFunc_, nullptr);
133 }
134 
135 /**
136  * @tc.name: ObserverTestNg004
137  * @tc.desc: Test the operation of Observer
138  * @tc.type: FUNC
139  */
140 HWTEST_F(ObserverTestNg, ObserverTestNg004, TestSize.Level1)
141 {
142     auto frameNode = FrameNode::GetOrCreateFrameNode(
__anona5da01530702() 143         V2::SCROLL_ETS_TAG, 12, []() { return AceType::MakeRefPtr<ScrollPattern>(); });
144     auto pattern = frameNode->GetPattern<ScrollablePattern>();
145     double offset = 0.0f;
146     pattern->UpdateCurrentOffset(offset, SCROLL_FROM_AXIS);
147 
148     auto info = UIObserverHandler::GetInstance().GetScrollEventState(frameNode);
149     ASSERT_EQ(info->id, frameNode->GetInspectorId().value_or(""));
150     ASSERT_EQ(info->uniqueId, frameNode->GetId());
151     ASSERT_EQ(info->scrollEvent, ScrollEventType::SCROLL_START);
152     ASSERT_EQ(info->offset, offset);
153 }
154 
155 /**
156  * @tc.name: ObserverTestNg005
157  * @tc.desc: Test the operation of Observer
158  * @tc.type: FUNC
159  */
160 HWTEST_F(ObserverTestNg, ObserverTestNg005, TestSize.Level1)
161 {
162     auto targetDensity = DEFAULT_DENSITY;
__anona5da01530802(AbilityContextInfo& context, double density) 163     UIObserverHandler::GetInstance().densityHandleFunc_ = [](AbilityContextInfo& context, double density) -> void {
164         EXPECT_EQ(density, DEFAULT_DENSITY);
165     };
166     UIObserverHandler::GetInstance().NotifyDensityChange(targetDensity);
167 }
168 
169 /**
170  * @tc.name: ObserverTestNg006
171  * @tc.desc: Test the operation of Observer
172  * @tc.type: FUNC
173  */
174 HWTEST_F(ObserverTestNg, ObserverTestNg006, TestSize.Level1)
175 {
176     std::optional<NavDestinationInfo> from;
177     std::optional<NavDestinationInfo> to;
178     NavigationOperation operation = NavigationOperation::PUSH;
179     UIObserverHandler::NavDestinationSwitchHandleFunc handleFunc = [](const AbilityContextInfo&,
__anona5da01530902(const AbilityContextInfo&, NavDestinationSwitchInfo& info) 180                                                                        NavDestinationSwitchInfo& info) -> void {
181         EXPECT_EQ(info.operation, NavigationOperation::PUSH);
182     };
183     UIObserverHandler::GetInstance().navDestinationSwitchHandleFunc_ = handleFunc;
184     UIObserverHandler::GetInstance().NotifyNavDestinationSwitch(std::move(from), std::move(to), operation);
185 }
186 
187 /**
188  * @tc.name: ObserverTestNg007
189  * @tc.desc: Test the operation of Observer
190  * @tc.type: FUNC
191  */
192 HWTEST_F(ObserverTestNg, ObserverTestNg007, TestSize.Level1)
193 {
194     GestureEvent gestureEventInfo;
195     ClickInfo clickInfo = ClickInfo(0);
196     RefPtr<FrameNode> frameNode = nullptr;
197 
198     UIObserverHandler::GetInstance().NotifyWillClick(gestureEventInfo, clickInfo, frameNode);
199     ASSERT_EQ(UIObserverHandler::GetInstance().willClickHandleFunc_, nullptr);
200 }
201 
202 /**
203  * @tc.name: ObserverTestNg008
204  * @tc.desc: Test the operation of Observer
205  * @tc.type: FUNC
206  */
207 HWTEST_F(ObserverTestNg, ObserverTestNg008, TestSize.Level1)
208 {
209     GestureEvent gestureEventInfo;
210     ClickInfo clickInfo = ClickInfo(0);
211     RefPtr<FrameNode> frameNode = nullptr;
212 
213     UIObserverHandler::GetInstance().NotifyDidClick(gestureEventInfo, clickInfo, frameNode);
214     ASSERT_EQ(UIObserverHandler::GetInstance().didClickHandleFunc_, nullptr);
215 }
216 
217 /**
218  * @tc.name: ObserverTestNg009
219  * @tc.desc: Test the func of GetHandleNavDestinationSwitchFunc
220  * @tc.type: FUNC
221  */
222 HWTEST_F(ObserverTestNg, ObserverTestNg009, TestSize.Level1)
223 {
224     std::optional<NavDestinationInfo> from;
225     std::optional<NavDestinationInfo> to;
226     UIObserverHandler::NavDestinationSwitchHandleFunc handleFunc = [](const AbilityContextInfo&,
__anona5da01530a02(const AbilityContextInfo&, NavDestinationSwitchInfo&) 227                                                                        NavDestinationSwitchInfo&) -> void {};
228     UIObserverHandler::GetInstance().navDestinationSwitchHandleFunc_ = handleFunc;
229     UIObserverHandler::NavDestinationSwitchHandleFunc func =
230         UIObserverHandler::GetInstance().GetHandleNavDestinationSwitchFunc();
231     ASSERT_NE(handleFunc, nullptr);
232     ASSERT_NE(func, nullptr);
233 }
234 
235 /**
236  * @tc.name: ObserverTestNg010
237  * @tc.desc: Test the operation of Observer
238  * @tc.type: FUNC
239  */
240 HWTEST_F(ObserverTestNg, ObserverTestNg010, TestSize.Level1)
241 {
242     auto navigation = NavigationGroupNode::GetOrCreateGroupNode(
__anona5da01530b02() 243         "navigation", 11, []() { return AceType::MakeRefPtr<NavigationPattern>(); });
244     navigation->GetPattern<NavigationPattern>()->navigationStack_ = AceType::MakeRefPtr<NavigationStack>();
245     auto contentNode = NavDestinationGroupNode::GetOrCreateGroupNode(
__anona5da01530c02() 246         V2::NAVDESTINATION_VIEW_ETS_TAG, 22, []() { return AceType::MakeRefPtr<NavDestinationPattern>(); });
247     auto pathInfo = AceType::MakeRefPtr<NavPathInfo>();
248     ASSERT_NE(pathInfo, nullptr);
249     auto context = AceType::MakeRefPtr<NavDestinationContext>();
250     ASSERT_NE(context, nullptr);
251     context->SetNavPathInfo(pathInfo);
252 
253     auto pattern = contentNode->GetPattern<NavDestinationPattern>();
254     pattern->SetNavDestinationContext(context);
255     pattern->name_ = "test_name";
256     pattern->isOnShow_ = true;
257     pattern->navigationNode_ = AceType::WeakClaim(Referenced::RawPtr(navigation));
258 
259     auto info = UIObserverHandler::GetInstance().GetNavigationOuterState(nullptr);
260     ASSERT_EQ(info, nullptr);
261 
262     info = UIObserverHandler::GetInstance().GetNavigationOuterState(navigation);
263     ASSERT_EQ(info, nullptr);
264 
265     ASSERT_EQ(pattern->GetNavigationNode(), navigation);
266 
267     info = UIObserverHandler::GetInstance().GetNavigationOuterState(contentNode);
268     ASSERT_EQ(info, nullptr);
269 }
270 
271 /**
272  * @tc.name: ObserverTestNg011
273  * @tc.desc: Test the operation of Observer
274  * @tc.type: FUNC
275  */
276 HWTEST_F(ObserverTestNg, ObserverTestNg011, TestSize.Level1)
277 {
278     auto navigation = NavigationGroupNode::GetOrCreateGroupNode(
__anona5da01530d02() 279         "navigation", 11, []() { return AceType::MakeRefPtr<NavigationPattern>(); });
280     navigation->GetPattern<NavigationPattern>()->navigationStack_ = AceType::MakeRefPtr<NavigationStack>();
281     auto contentNode = NavDestinationGroupNode::GetOrCreateGroupNode(
__anona5da01530e02() 282         V2::NAVDESTINATION_VIEW_ETS_TAG, 22, []() { return AceType::MakeRefPtr<NavDestinationPattern>(); });
283     auto pathInfo = AceType::MakeRefPtr<NavPathInfo>();
284     ASSERT_NE(pathInfo, nullptr);
285     auto context = AceType::MakeRefPtr<NavDestinationContext>();
286     ASSERT_NE(context, nullptr);
287     context->SetNavPathInfo(pathInfo);
288 
289     auto pattern = contentNode->GetPattern<NavDestinationPattern>();
290     pattern->SetNavDestinationContext(context);
291     pattern->name_ = "test_name";
292     pattern->isOnShow_ = true;
293     pattern->navigationNode_ = AceType::WeakClaim(Referenced::RawPtr(navigation));
294 
295     auto info = UIObserverHandler::GetInstance().GetNavigationInnerState(nullptr);
296     ASSERT_EQ(info, nullptr);
297 
298     info = UIObserverHandler::GetInstance().GetNavigationInnerState(navigation);
299     ASSERT_EQ(info, nullptr);
300 }
301 
302 /**
303  * @tc.name: ObserverTestNg012
304  * @tc.desc: Test the operation of Observer
305  * @tc.type: FUNC
306  */
307 HWTEST_F(ObserverTestNg, ObserverTestNg012, TestSize.Level1)
308 {
309     auto navigation = NavigationGroupNode::GetOrCreateGroupNode(
__anona5da01530f02() 310         "navigation", 11, []() { return AceType::MakeRefPtr<NavigationPattern>(); });
311     navigation->GetPattern<NavigationPattern>()->navigationStack_ = AceType::MakeRefPtr<NavigationStack>();
312     auto navigationContentNode = FrameNode::GetOrCreateFrameNode(V2::NAVIGATION_CONTENT_ETS_TAG, 12,
__anona5da01531002() 313         []() { return AceType::MakeRefPtr<NavigationContentPattern>(); });
314     auto navDestination = NavDestinationGroupNode::GetOrCreateGroupNode(
__anona5da01531102() 315         V2::NAVDESTINATION_VIEW_ETS_TAG, 22, []() { return AceType::MakeRefPtr<NavDestinationPattern>(); });
316     auto pathInfo = AceType::MakeRefPtr<NavPathInfo>();
317     ASSERT_NE(pathInfo, nullptr);
318     auto context = AceType::MakeRefPtr<NavDestinationContext>();
319     ASSERT_NE(context, nullptr);
320     context->SetNavPathInfo(pathInfo);
321     navDestination->SetParent(navigationContentNode);
322     auto pattern = navDestination->GetPattern<NavDestinationPattern>();
323     pattern->SetNavDestinationContext(context);
324     pattern->name_ = "test_name";
325     pattern->isOnShow_ = true;
326     pattern->navigationNode_ = AceType::WeakClaim(Referenced::RawPtr(navigation));
327 
328     auto info = UIObserverHandler::GetInstance().GetNavigationOuterState(nullptr);
329     ASSERT_EQ(info, nullptr);
330 
331     info = UIObserverHandler::GetInstance().GetNavigationOuterState(navigation);
332     ASSERT_EQ(info, nullptr);
333 
334     ASSERT_EQ(pattern->GetNavigationNode(), navigation);
335 
336     info = UIObserverHandler::GetInstance().GetNavigationOuterState(navDestination);
337     ASSERT_NE(info, nullptr);
338     ASSERT_EQ(info->name, "test_name");
339     ASSERT_EQ(info->navigationId, "");
340     ASSERT_EQ(info->state, NavDestinationState::ON_SHOWN);
341 }
342 }
343