• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
28 namespace OHOS::Ace::NG {
29 struct MockReplace {
30     int32_t isReplace_ = 0;
31 };
32 constexpr char UNDEFINED_ID[] = "undefined";
33 
34 class MockNavPathInfo : public NavPathInfo {
35     DECLARE_ACE_TYPE(MockNavPathInfo, NavPathInfo)
36 public:
37     MockNavPathInfo() = default;
MockNavPathInfo(const std::string & name)38     explicit MockNavPathInfo(const std::string& name) : NG::NavPathInfo(name) {}
MockNavPathInfo(const std::string & name,bool isEntry)39     MockNavPathInfo(const std::string& name, bool isEntry) : NG::NavPathInfo(name, isEntry) {}
40     ~MockNavPathInfo() = default;
41 
SetOnPop(std::function<void ()> onPop)42     void SetOnPop(std::function<void()> onPop)
43     {
44         onPop_ = onPop;
45     }
46 
GetOnPop()47     std::function<void()> GetOnPop() const
48     {
49         return onPop_;
50     }
51 
SetNavDestinationId(const std::string & navDestinationId)52     void SetNavDestinationId(const std::string& navDestinationId)
53     {
54         navDestinationId_ = navDestinationId;
55     }
56 
GetNavDestinationId()57     std::string GetNavDestinationId() const
58     {
59         return navDestinationId_;
60     }
61 
62     int32_t index = -1;
63     bool needBuildNewInstance = false;
64     bool fromRecovery = false;
65     int32_t mode = 0; // 0 for standard and 1 for dialog
66 
67 private:
68     std::function<void()> onPop_;
69     std::string navDestinationId_ = UNDEFINED_ID;
70 };
71 
72 enum LaunchMode {
73     STANDARD = 0,
74     MOVE_TO_TOP_SINGLETON,
75     POP_TO_TOP_SINGLETON,
76     NEW_INSTANCE,
77 };
78 
79 using NavigationInterceptionEvent = std::function<void(const RefPtr<NavDestinationContext>,
80     const RefPtr<NavDestinationContext>, NavigationOperation, bool)>;
81 class MockNavigationStack : public NavigationStack {
82     DECLARE_ACE_TYPE(MockNavigationStack, NavigationStack);
83 public:
UpdateReplaceValue(int32_t isReplace)84     void UpdateReplaceValue(int32_t isReplace) const override
85     {
86         mockReplace_->isReplace_ = isReplace;
87     }
88 
GetReplaceValue()89     int32_t GetReplaceValue() const override
90     {
91         return mockReplace_->isReplace_;
92     }
93 
GetDisableAnimation()94     bool GetDisableAnimation() const override
95     {
96         return false;
97     }
98 
SetOnStateChangedCallback(std::function<void ()> callback)99     void SetOnStateChangedCallback(std::function<void()> callback) override
100     {
101         onStateChangedCallback_ = callback;
102     }
103 
GetOnStateChangedCallback()104     std::function<void()> GetOnStateChangedCallback() const
105     {
106         return onStateChangedCallback_;
107     }
108 
SetInterceptionBeforeCallback(NavigationInterceptionEvent callback)109     void SetInterceptionBeforeCallback(NavigationInterceptionEvent callback)
110     {
111         beforeCallback_ = callback;
112     }
113 
SetInterceptionAfterCallback(NavigationInterceptionEvent afterCallback)114     void SetInterceptionAfterCallback(NavigationInterceptionEvent afterCallback)
115     {
116         afterCallback_ = afterCallback;
117     }
118 
SetInterceptionModeCallback(std::function<void (NavigationMode)> modeCallback)119     void SetInterceptionModeCallback(std::function<void(NavigationMode)> modeCallback)
120     {
121         modeCallback_ = modeCallback;
122     }
123 
FireNavigationModeChange(NavigationMode mode)124     void FireNavigationModeChange(NavigationMode mode) override
125     {
126         if (modeCallback_) {
127             modeCallback_(mode);
128         }
129     }
130 
SetLifecycleIndex(int8_t index)131     void SetLifecycleIndex(int8_t index)
132     {
133         lifecycleIndex_ = index;
134     }
135 
GetLifecycleIndex()136     int8_t GetLifecycleIndex() const
137     {
138         return lifecycleIndex_;
139     }
140 
GetPathList()141     NavPathList& GetPathList()
142     {
143         return navPathList_;
144     }
145 
GetNavDestinationId(int32_t index)146     std::string GetNavDestinationId(int32_t index)
147     {
148         return mockPathArray_[index]->GetNavDestinationId();
149     }
150 
151     MOCK_METHOD1(OnAttachToParent, void(RefPtr<NavigationStack>));
152     MOCK_METHOD0(OnDetachFromParent, void());
153 
154     void FireNavigationInterception(bool isBefore, const RefPtr<NavDestinationContext>& from,
155         const RefPtr<NavDestinationContext>& to, NavigationOperation operation, bool isAnimated) override;
156 
157     bool CreateNodeByIndex(int32_t index, const OHOS::Ace::WeakPtr<OHOS::Ace::NG::UINode>& customNode,
158         OHOS::Ace::RefPtr<OHOS::Ace::NG::UINode>& node) override;
159 
160     void SetDestinationIdToJsStack(int32_t index, const std::string& navDestinationId) override;
161 
162     void InitNavPathIndex(const std::vector<std::string>& pathNames) override;
163 
164     bool NeedBuildNewInstance(int32_t index) override;
165 
166     void SetNeedBuildNewInstance(int32_t index, bool need) override;
167 
168     void SetPathArray(const std::vector<NavdestinationRecoveryInfo>& navdestinationsInfo);
169 
170     void SetFromRecovery(int32_t index, bool fromRecovery);
171 
172     bool IsFromRecovery(int32_t index);
173 
174     std::string GetNavDestinationIdByIndex(int32_t index);
175 
176     //  ============================ operation below is for mock NavPathStack in arkTS ============================
177     /**
178      * @brief simply mock push operation of NavPathStack(@arkTS)
179      */
180     void Push(const std::string& name, const RefPtr<RouteInfo>& routeInfo = nullptr) override;
181 
182     void Push(const std::string& name, int32_t index) override;
183 
184     // pushPath(info: NavPathInfo, animated?: boolean): void
185     // pushPath(info: NavPathInof, options?: NavigationOptions): void
186     void MockPushPath(const RefPtr<MockNavPathInfo>& info, bool animated = true,
187         LaunchMode launchmode = LaunchMode::STANDARD);
188 
189     bool MockRemoveByNavDestinationId(const std::string& navDestinationId);
190 
191     int32_t MockRemoveByInexes(std::vector<int32_t> indexes);
192 
193     int32_t MockRemoveByName(const std::string& name);
194 
195     int32_t MockPopToName(const std::string& name);
196 
197     void MockPopToIndex(int32_t index);
198 
199     int32_t MockMoveToTop(const std::string& name);
200 
201     void Clear() override;
202 
203     void Pop() override;
204 
205     void PopToIndex(int32_t index);
206 
207     std::pair<int32_t, std::string> FindInPopArray(const std::string& name);
208 
Size()209     int32_t Size() const
210     {
211         return static_cast<int32_t>(mockPathArray_.size());
212     }
213 
214     std::vector<std::string> GetAllPathName() override;
215 
216     std::vector<int32_t> GetAllPathIndex() override;
217 
218     void SetIsEntryByIndex(int32_t index, bool isEntry) override;
219 
GetSize()220     int32_t GetSize() const
221     {
222         return size_;
223     }
224 
225     // ============================ operation above is for mock NavPathStack in arkTS ============================
226 private:
227     int8_t lifecycleIndex_ = 0;
228     std::function<void()> onStateChangedCallback_;
229     NavigationInterceptionEvent beforeCallback_;
230     NavigationInterceptionEvent afterCallback_;
231     std::function<void(NavigationMode)> modeCallback_;
232     MockReplace *mockReplace_ = new MockReplace();
233     std::vector<RefPtr<MockNavPathInfo>> mockPathArray_;
234     std::vector<RefPtr<MockNavPathInfo>> mockPopArray_;
235     std::map<int32_t, bool> mockIsEntryMap_;
236     int32_t size_ = 0;
237 };
238 } // namespace NG
239 #endif