• 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/navigation/navigation_pattern.h"
27 #include "core/components_ng/pattern/navrouter/navdestination_layout_property.h"
28 #include "core/components_ng/pattern/navrouter/navdestination_model_ng.h"
29 #include "core/components_ng/pattern/navrouter/navdestination_pattern.h"
30 #include "core/components_v2/inspector/inspector_constants.h"
31 
32 using namespace testing;
33 using namespace testing::ext;
34 
35 namespace OHOS::Ace::NG {
36 
37 class ObserverTestNg : public testing::Test {
38 public:
SetUpTestCase()39     static void SetUpTestCase() {}
TearDownTestCase()40     static void TearDownTestCase() {}
41 };
42 
43 /**
44  * @tc.name: ObserverTestNg001
45  * @tc.desc: Test the operation of Observer
46  * @tc.type: FUNC
47  */
48 HWTEST_F(ObserverTestNg, ObserverTestNg001, TestSize.Level1)
49 {
50     auto navigation = NavigationGroupNode::GetOrCreateGroupNode(
__anonc6b9f1940102() 51         "navigation", 11, []() { return AceType::MakeRefPtr<NavigationPattern>(); });
52     auto contentNode = NavDestinationGroupNode::GetOrCreateGroupNode(
__anonc6b9f1940202() 53         V2::NAVDESTINATION_VIEW_ETS_TAG, 22, []() { return AceType::MakeRefPtr<NavDestinationPattern>(); });
54     auto pattern = contentNode->GetPattern<NavDestinationPattern>();
55     UIObserverHandler::GetInstance().NotifyNavigationStateChange(AceType::WeakClaim(Referenced::RawPtr(pattern)),
56                                                                  NavDestinationState::ON_SHOWN);
57     ASSERT_EQ(UIObserverHandler::GetInstance().navigationHandleFunc_, nullptr);
58     auto pattern1 = navigation->GetPattern<NavigationPattern>();
59     UIObserverHandler::GetInstance().NotifyNavigationStateChange(AceType::WeakClaim(Referenced::RawPtr(pattern1)),
60                                                                  NavDestinationState::ON_SHOWN);
61     ASSERT_EQ(UIObserverHandler::GetInstance().navigationHandleFunc_, nullptr);
62 }
63 
64 /**
65  * @tc.name: ObserverTestNg002
66  * @tc.desc: Test the operation of Observer
67  * @tc.type: FUNC
68  */
69 HWTEST_F(ObserverTestNg, ObserverTestNg002, TestSize.Level1)
70 {
71     auto navigation = NavigationGroupNode::GetOrCreateGroupNode(
__anonc6b9f1940302() 72         "navigation", 11, []() { return AceType::MakeRefPtr<NavigationPattern>(); });
73     auto contentNode = NavDestinationGroupNode::GetOrCreateGroupNode(
__anonc6b9f1940402() 74         V2::NAVDESTINATION_VIEW_ETS_TAG, 22, []() { return AceType::MakeRefPtr<NavDestinationPattern>(); });
75     auto pattern = contentNode->GetPattern<NavDestinationPattern>();
76     pattern->name_ = "test_name";
77     pattern->isOnShow_ = true;
78     pattern->navigationNode_ = AceType::WeakClaim(Referenced::RawPtr(navigation));
79 
80     auto info = UIObserverHandler::GetInstance().GetNavigationState(nullptr);
81     ASSERT_EQ(info, nullptr);
82 
83     info = UIObserverHandler::GetInstance().GetNavigationState(navigation);
84     ASSERT_EQ(info, nullptr);
85 
86     ASSERT_EQ(pattern->GetNavigationNode(), navigation);
87 
88     info = UIObserverHandler::GetInstance().GetNavigationState(contentNode);
89     ASSERT_EQ(info->name, "test_name");
90     ASSERT_EQ(info->navigationId, "");
91     ASSERT_EQ(info->state, NavDestinationState::ON_SHOWN);
92 }
93 }
94