1 /*
2  * Copyright (c) 2023 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_TEST_MOCK_NAVIGATION_STACK_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_TEST_MOCK_NAVIGATION_STACK_H
18 
19 #include "gmock/gmock.h"
20 
21 #define protected public
22 #define private public
23 #include "core/components_ng/pattern/navigation/navigation_stack.h"
24 #include "core/components_ng/pattern/navrouter/navdestination_pattern.h"
25 #include "core/components_ng/pattern/navrouter/navdestination_model_ng.h"
26 #include "test/mock/core/common/mock_container.h"
27 struct MockReplace {
28     int32_t isReplace_ = 0;
29 };
30 
31 struct MockNavPathInfo {
32     std::string name = "";
33     std::string navDestinationId = "undefined";
34 
MockNavPathInfoMockNavPathInfo35     explicit MockNavPathInfo(std::string name) : name(std::move(name)) {}
36 };
37 
38 using NavigationInterceptionEvent = std::function<void(const OHOS::Ace::RefPtr<OHOS::Ace::NG::NavDestinationContext>,
39     const OHOS::Ace::RefPtr<OHOS::Ace::NG::NavDestinationContext>, OHOS::Ace::NG::NavigationOperation, bool)>;
40 class MockNavigationStack : public OHOS::Ace::NG::NavigationStack {
41     DECLARE_ACE_TYPE(MockNavigationStack, NavigationStack);
42 public:
UpdateReplaceValue(int32_t isReplace)43     void UpdateReplaceValue(int32_t isReplace) const override
44     {
45         mockReplace_->isReplace_ = isReplace;
46     }
47 
GetReplaceValue()48     int32_t GetReplaceValue() const override
49     {
50         return mockReplace_->isReplace_;
51     }
52 
GetDisableAnimation()53     bool GetDisableAnimation() const override
54     {
55         return false;
56     }
57 
SetOnStateChangedCallback(std::function<void ()> callback)58     void SetOnStateChangedCallback(std::function<void()> callback) override
59     {
60         onStateChangedCallback_ = callback;
61     }
62 
GetOnStateChangedCallback()63     std::function<void()> GetOnStateChangedCallback() const
64     {
65         return onStateChangedCallback_;
66     }
67 
SetInterceptionBeforeCallback(NavigationInterceptionEvent callback)68     void SetInterceptionBeforeCallback(NavigationInterceptionEvent callback)
69     {
70         beforeCallback_ = callback;
71     }
72 
SetInterceptionAfterCallback(NavigationInterceptionEvent afterCallback)73     void SetInterceptionAfterCallback(NavigationInterceptionEvent afterCallback)
74     {
75         afterCallback_ = afterCallback;
76     }
77 
SetInterceptionModeCallback(std::function<void (OHOS::Ace::NG::NavigationMode)> modeCallback)78     void SetInterceptionModeCallback(std::function<void(OHOS::Ace::NG::NavigationMode)> modeCallback)
79     {
80         modeCallback_ = modeCallback;
81     }
82 
FireNavigationModeChange(OHOS::Ace::NG::NavigationMode mode)83     void FireNavigationModeChange(OHOS::Ace::NG::NavigationMode mode) override
84     {
85         if (modeCallback_) {
86             modeCallback_(mode);
87         }
88     }
89 
FireNavigationInterception(bool isBefore,const OHOS::Ace::RefPtr<OHOS::Ace::NG::NavDestinationContext> & from,const OHOS::Ace::RefPtr<OHOS::Ace::NG::NavDestinationContext> & to,OHOS::Ace::NG::NavigationOperation operation,bool isAnimated)90     void FireNavigationInterception(bool isBefore, const OHOS::Ace::RefPtr<OHOS::Ace::NG::NavDestinationContext>& from,
91         const OHOS::Ace::RefPtr<OHOS::Ace::NG::NavDestinationContext>& to, OHOS::Ace::NG::NavigationOperation operation,
92         bool isAnimated) override
93     {
94         if (isBefore) {
95             if (beforeCallback_) {
96                 beforeCallback_(from, to, operation, isAnimated);
97             }
98         } else {
99             if (afterCallback_) {
100                 afterCallback_(from, to, operation, isAnimated);
101             }
102         }
103     }
104 
105     MOCK_METHOD1(OnAttachToParent, void(OHOS::Ace::RefPtr<OHOS::Ace::NG::NavigationStack>));
106     MOCK_METHOD0(OnDetachFromParent, void());
107 
CreateNodeByIndex(int32_t index,const OHOS::Ace::WeakPtr<OHOS::Ace::NG::UINode> & customNode)108     OHOS::Ace::RefPtr<OHOS::Ace::NG::UINode> CreateNodeByIndex(int32_t index,
109         const OHOS::Ace::WeakPtr<OHOS::Ace::NG::UINode>& customNode) override
110     {
111         auto* stack = OHOS::Ace::NG::ViewStackProcessor::GetInstance();
112         // navDestination node
113         int32_t nodeId = stack->ClaimNodeId();
114         auto frameNode = OHOS::Ace::NG::NavDestinationGroupNode::GetOrCreateGroupNode(
115             OHOS::Ace::V2::NAVDESTINATION_VIEW_ETS_TAG, nodeId, []() {
116                 return OHOS::Ace::AceType::MakeRefPtr<OHOS::Ace::NG::NavDestinationPattern>();
117             });
118         EXPECT_NE(frameNode, nullptr);
119         auto name = mockPathArray_[index].name;
120         auto container = OHOS::Ace::MockContainer::Current();
121         auto navigationRoute = container->GetNavigationRoute();
122         if (!navigationRoute) {
123             return nullptr;
124         }
125         if (!navigationRoute->HasLoaded(name)) {
126             int32_t res = navigationRoute->LoadPage(name);
127             if (res != 0) {
128                 return frameNode;
129             }
130         }
131         auto pattern = OHOS::Ace::AceType::DynamicCast<OHOS::Ace::NG::NavDestinationPattern>(frameNode->GetPattern());
132         EXPECT_NE(pattern, nullptr);
133         pattern->SetName(name);
134         return frameNode;
135     }
136 
Push(const std::string & name,int32_t index)137     void Push(const std::string& name, int32_t index) override
138     {
139         mockPathArray_.push_back(MockNavPathInfo(name));
140     }
141 
142     void Push(const std::string& name, const OHOS::Ace::RefPtr<OHOS::Ace::NG::RouteInfo>& routeInfo = nullptr) override
143     {
144         mockPathArray_.push_back(MockNavPathInfo(name));
145     }
146 
GetAllPathName()147     std::vector<std::string> GetAllPathName() override
148     {
149         std::vector<std::string> pathNames;
150         for (int32_t i = 0; i < static_cast<int32_t>(mockPathArray_.size()); i++) {
151             pathNames.emplace_back(mockPathArray_[i].name);
152         }
153         return pathNames;
154     }
155 
GetAllPathIndex()156     std::vector<int32_t> GetAllPathIndex() override
157     {
158         if (mockPathArray_.empty()) {
159             return {};
160         }
161         std::vector<int32_t> pathIndex;
162         for (int32_t i = 0; i < static_cast<int32_t>(mockPathArray_.size()); i++) {
163             pathIndex.emplace_back(i);
164         }
165         return pathIndex;
166     }
167 
Clear()168     void Clear() override
169     {
170         OHOS::Ace::NG::NavigationStack::Clear();
171         mockPathArray_.clear();
172     }
173 
Pop()174     void Pop() override
175     {
176         mockPathArray_.pop_back();
177     }
178 
SetLifecycleIndex(int8_t index)179     void SetLifecycleIndex(int8_t index)
180     {
181         lifecycleIndex_ = index;
182     }
183 
GetLifecycleIndex()184     int8_t GetLifecycleIndex() const
185     {
186         return lifecycleIndex_;
187     }
188 
GetPathList()189     OHOS::Ace::NG::NavPathList& GetPathList()
190     {
191         return navPathList_;
192     }
193 
SetDestinationIdToJsStack(int32_t index,const std::string & navDestinationId)194     void SetDestinationIdToJsStack(int32_t index, const std::string& navDestinationId) override
195     {
196         if (index < 0 || index >= static_cast<int32_t>(mockPathArray_.size())) {
197             return;
198         }
199         mockPathArray_[index].navDestinationId = navDestinationId;
200     }
201 
GetNavPathId(int32_t index)202     std::string GetNavPathId(int32_t index)
203     {
204         return mockPathArray_[index].navDestinationId;
205     }
206 
207 private:
208     int8_t lifecycleIndex_ = 0;
209     std::function<void()> onStateChangedCallback_;
210     NavigationInterceptionEvent beforeCallback_;
211     NavigationInterceptionEvent afterCallback_;
212     std::function<void(OHOS::Ace::NG::NavigationMode)> modeCallback_;
213     MockReplace *mockReplace_ = new MockReplace();
214     std::vector<MockNavPathInfo> mockPathArray_;
215 };
216 #endif