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 bool isForceSet = false; 67 bool isReplaced = false; 68 69 private: 70 std::function<void()> onPop_; 71 std::string navDestinationId_ = UNDEFINED_ID; 72 }; 73 74 using NavigationInterceptionEvent = std::function<void(const RefPtr<NavDestinationContext>, 75 const RefPtr<NavDestinationContext>, NavigationOperation, bool)>; 76 class MockNavigationStack : public NavigationStack { 77 DECLARE_ACE_TYPE(MockNavigationStack, NavigationStack); 78 public: UpdateReplaceValue(int32_t isReplace)79 void UpdateReplaceValue(int32_t isReplace) const override 80 { 81 mockReplace_->isReplace_ = isReplace; 82 } 83 GetReplaceValue()84 int32_t GetReplaceValue() const override 85 { 86 return mockReplace_->isReplace_; 87 } 88 GetDisableAnimation()89 bool GetDisableAnimation() const override 90 { 91 return false; 92 } 93 SetOnStateChangedCallback(std::function<void ()> callback)94 void SetOnStateChangedCallback(std::function<void()> callback) override 95 { 96 onStateChangedCallback_ = callback; 97 } 98 GetOnStateChangedCallback()99 std::function<void()> GetOnStateChangedCallback() const 100 { 101 return onStateChangedCallback_; 102 } 103 SetInterceptionBeforeCallback(NavigationInterceptionEvent callback)104 void SetInterceptionBeforeCallback(NavigationInterceptionEvent callback) 105 { 106 beforeCallback_ = callback; 107 } 108 SetInterceptionAfterCallback(NavigationInterceptionEvent afterCallback)109 void SetInterceptionAfterCallback(NavigationInterceptionEvent afterCallback) 110 { 111 afterCallback_ = afterCallback; 112 } 113 SetInterceptionModeCallback(std::function<void (NavigationMode)> modeCallback)114 void SetInterceptionModeCallback(std::function<void(NavigationMode)> modeCallback) 115 { 116 modeCallback_ = modeCallback; 117 } 118 FireNavigationModeChange(NavigationMode mode)119 void FireNavigationModeChange(NavigationMode mode) override 120 { 121 if (modeCallback_) { 122 modeCallback_(mode); 123 } 124 } 125 SetLifecycleIndex(int8_t index)126 void SetLifecycleIndex(int8_t index) 127 { 128 lifecycleIndex_ = index; 129 } 130 GetLifecycleIndex()131 int8_t GetLifecycleIndex() const 132 { 133 return lifecycleIndex_; 134 } 135 GetPathList()136 NavPathList& GetPathList() 137 { 138 return navPathList_; 139 } 140 GetNavDestinationId(int32_t index)141 std::string GetNavDestinationId(int32_t index) 142 { 143 return mockPathArray_[index]->GetNavDestinationId(); 144 } 145 146 MOCK_METHOD1(OnAttachToParent, void(RefPtr<NavigationStack>)); 147 MOCK_METHOD0(OnDetachFromParent, void()); 148 149 void FireNavigationInterception(bool isBefore, const RefPtr<NavDestinationContext>& from, 150 const RefPtr<NavDestinationContext>& to, NavigationOperation operation, bool isAnimated) override; 151 152 bool CreateNodeByIndex(int32_t index, const OHOS::Ace::WeakPtr<OHOS::Ace::NG::UINode>& customNode, 153 OHOS::Ace::RefPtr<OHOS::Ace::NG::UINode>& node) override; 154 155 void SetDestinationIdToJsStack(int32_t index, const std::string& navDestinationId) override; 156 157 void InitNavPathIndex(const std::vector<std::string>& pathNames) override; 158 159 bool NeedBuildNewInstance(int32_t index) override; 160 161 void SetNeedBuildNewInstance(int32_t index, bool need) override; 162 163 void SetPathArray(const std::vector<NavdestinationRecoveryInfo>& navdestinationsInfo); 164 165 void SetFromRecovery(int32_t index, bool fromRecovery); 166 167 bool IsFromRecovery(int32_t index); 168 169 std::string GetNavDestinationIdByIndex(int32_t index); 170 171 // ============================ operation below is for mock NavPathStack in arkTS ============================ 172 /** 173 * @brief simply mock push operation of NavPathStack(@arkTS) 174 */ 175 void Push(const std::string& name, const RefPtr<RouteInfo>& routeInfo = nullptr) override; 176 177 void Push(const std::string& name, int32_t index) override; 178 179 // pushPath(info: NavPathInfo, animated?: boolean): void 180 // pushPath(info: NavPathInof, options?: NavigationOptions): void 181 void MockPushPath(const RefPtr<MockNavPathInfo>& info, bool animated = true, 182 LaunchMode launchmode = LaunchMode::STANDARD); 183 184 bool MockRemoveByNavDestinationId(const std::string& navDestinationId); 185 186 int32_t MockRemoveByInexes(std::vector<int32_t> indexes); 187 188 int32_t MockRemoveByName(const std::string& name); 189 190 int32_t MockPopToName(const std::string& name); 191 192 void MockPopToIndex(int32_t index); 193 194 int32_t MockMoveToTop(const std::string& name); 195 196 void Clear() override; 197 198 void Pop() override; 199 200 void PopToIndex(int32_t index); 201 202 std::pair<int32_t, std::string> FindInPopArray(const std::string& name); 203 Size()204 int32_t Size() const 205 { 206 return static_cast<int32_t>(mockPathArray_.size()); 207 } 208 209 std::vector<std::string> GetAllPathName() override; 210 211 std::vector<int32_t> GetAllPathIndex() override; 212 213 std::vector<RefPtr<MockNavPathInfo>> MockGetPathStack(); 214 215 void MockSetPathStack(std::vector<RefPtr<MockNavPathInfo>>& setPathArray, bool animated = true); 216 217 void SetIsEntryByIndex(int32_t index, bool isEntry) override; 218 GetSize()219 int32_t GetSize() const 220 { 221 return size_; 222 } 223 224 int32_t GetRecoveredDestinationMode(int32_t index); 225 uint64_t GetNavDestinationIdInt(int32_t index); 226 bool GetIsForceSet(int32_t index); 227 void ResetIsForceSetFlag(int32_t index); 228 bool CheckIsReplacedDestination(int32_t index, std::string& replacedName, int32_t& replacedIndex); 229 void SetRecoveryFromReplaceDestination(int32_t index, bool value); 230 231 MOCK_METHOD2(CreateHomeDestination, bool(const WeakPtr<UINode>& customNode, RefPtr<UINode>& node)); 232 233 // ============================ operation above is for mock NavPathStack in arkTS ============================ 234 private: 235 int8_t lifecycleIndex_ = 0; 236 std::function<void()> onStateChangedCallback_; 237 NavigationInterceptionEvent beforeCallback_; 238 NavigationInterceptionEvent afterCallback_; 239 std::function<void(NavigationMode)> modeCallback_; 240 MockReplace *mockReplace_ = new MockReplace(); 241 std::vector<RefPtr<MockNavPathInfo>> mockPathArray_; 242 std::vector<RefPtr<MockNavPathInfo>> mockPopArray_; 243 std::map<int32_t, bool> mockIsEntryMap_; 244 int32_t size_ = 0; 245 }; 246 } // namespace NG 247 #endif