1 /* 2 * Copyright (c) 2024 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 TEST_UNITTEST_CORE_EVENT_FOCUS_TEST_BASE_H 17 #define TEST_UNITTEST_CORE_EVENT_FOCUS_TEST_BASE_H 18 #include <cstddef> 19 #include <cstdint> 20 21 #include "gtest/gtest.h" 22 #include "base/geometry/ng/offset_t.h" 23 #include "base/geometry/ng/size_t.h" 24 #include "base/memory/ace_type.h" 25 #include "base/memory/referenced.h" 26 #include "core/components/theme/app_theme.h" 27 #include "core/components_ng/base/frame_node.h" 28 #include "core/components_ng/event/focus_hub.h" 29 #include "core/components_ng/event/touch_event.h" 30 #include "core/components_ng/manager/focus/focus_view.h" 31 #include "core/components_ng/pattern/pattern.h" 32 #include "test/mock/core/common/mock_theme_manager.h" 33 #include "core/components_v2/inspector/inspector_constants.h" 34 #include "core/event/key_event.h" 35 #include "core/pipeline_ng/pipeline_context.h" 36 #include "test/mock/core/pipeline/mock_pipeline_context.h" 37 #include "core/components_ng/pattern/linear_layout/linear_layout_pattern.h" 38 39 namespace OHOS::Ace::NG { 40 41 class FocusTestPattern : public Pattern { 42 public: 43 44 FocusTestPattern() = default; 45 ~FocusTestPattern() override = default; 46 GetFocusPattern()47 FocusPattern GetFocusPattern() const override 48 { 49 return *AceType::RawPtr(focusPattern_); 50 } 51 SetFocusPattern(FocusPattern focusPattern)52 void SetFocusPattern(FocusPattern focusPattern) 53 { 54 focusPattern_ = new FocusPattern(focusPattern); 55 } 56 GetScopeFocusAlgorithm()57 ScopeFocusAlgorithm GetScopeFocusAlgorithm() override 58 { 59 return scopeFocusAlgorithm_; 60 } 61 SetScopeFocusAlgorithm(ScopeFocusAlgorithm scopeFocusAlgorithm)62 void SetScopeFocusAlgorithm(ScopeFocusAlgorithm scopeFocusAlgorithm) 63 { 64 scopeFocusAlgorithm_ = scopeFocusAlgorithm; 65 } 66 67 RefPtr<FocusPattern> focusPattern_; 68 ScopeFocusAlgorithm scopeFocusAlgorithm_; 69 }; 70 71 class FocusViewTestPattern : public Pattern, public FocusView { 72 DECLARE_ACE_TYPE(FocusViewTestPattern, Pattern, FocusView); 73 public: 74 75 FocusViewTestPattern() = default; 76 ~FocusViewTestPattern() override = default; 77 GetFocusPattern()78 FocusPattern GetFocusPattern() const override 79 { 80 return *AceType::RawPtr(focusPattern_); 81 } 82 SetFocusPattern(FocusPattern focusPattern)83 void SetFocusPattern(FocusPattern focusPattern) 84 { 85 focusPattern_ = new FocusPattern(focusPattern); 86 } 87 GetScopeFocusAlgorithm()88 ScopeFocusAlgorithm GetScopeFocusAlgorithm() override 89 { 90 return scopeFocusAlgorithm_; 91 } 92 SetScopeFocusAlgorithm(ScopeFocusAlgorithm scopeFocusAlgorithm)93 void SetScopeFocusAlgorithm(ScopeFocusAlgorithm scopeFocusAlgorithm) 94 { 95 scopeFocusAlgorithm_ = scopeFocusAlgorithm; 96 } 97 GetRouteOfFirstScope()98 std::list<int32_t> GetRouteOfFirstScope() override 99 { 100 return routeOfFirstScope; 101 } 102 SetRouteOfFirstScope(std::list<int32_t> & list)103 void SetRouteOfFirstScope(std::list<int32_t>& list) 104 { 105 routeOfFirstScope = list; 106 } 107 108 RefPtr<FocusPattern> focusPattern_; 109 ScopeFocusAlgorithm scopeFocusAlgorithm_; 110 std::list<int32_t> routeOfFirstScope = { 0 }; 111 }; 112 113 class FocusTestBaseNg { 114 public: 115 bool InitFocusTestBaseNG(); 116 static RefPtr<FrameNode> CreateRootNode(); 117 static RefPtr<FrameNode> CreateNode(const std::string& tag); 118 static RefPtr<FrameNode> CreateNodeWithFocusPattern(const std::string& tag, FocusType focusType, bool focusable); 119 static RefPtr<FrameNode> CreateNodeWithFocusPattern(const std::string& tag, FocusType focusType, 120 bool focusable, FocusStyleType styleType); 121 static RefPtr<FrameNode> CreateNodeWithFocusPattern(const std::string& tag, FocusType focusType, bool focusable, 122 FocusStyleType styleType, const FocusPaintParam& paintParams); 123 static RefPtr<FrameNode> CreateNodeWithFocusPatternAndAlgorithm(const std::string& tag, FocusType focusType, 124 bool focusable, ScopeFocusAlgorithm algorithm); 125 static RefPtr<FrameNode> CreateNodeWithFocusPatternAndAlgorithm(const std::string& tag, FocusType focusType, 126 bool focusable, FocusStyleType styleType, ScopeFocusAlgorithm algorithm); 127 static RefPtr<FrameNode> CreateNodeWithFocusPatternAndAlgorithm(const std::string& tag, FocusType focusType, 128 bool focusable, FocusStyleType styleType, const FocusPaintParam& paintParams, ScopeFocusAlgorithm algorithm); 129 130 static RefPtr<FrameNode> CreateFocusViewWithFocusPattern(const std::string& tag, std::list<int32_t>& list, 131 FocusType focusType, bool focusable); 132 static RefPtr<FrameNode> CreateFocusViewWithFocusPattern(const std::string& tag, std::list<int32_t>& list, 133 FocusType focusType, bool focusable, FocusStyleType styleType); 134 static RefPtr<FrameNode> CreateFocusViewWithFocusPattern(const std::string& tag, std::list<int32_t>& list, 135 FocusType focusType, bool focusable, FocusStyleType styleType, const FocusPaintParam& paintParams); 136 static RefPtr<FrameNode> CreateFocusViewWithFocusPatternAndAlgorithm(const std::string& tag, 137 std::list<int32_t>& list, 138 FocusType focusType, bool focusable, ScopeFocusAlgorithm algorithm); 139 static RefPtr<FrameNode> CreateFocusViewWithFocusPatternAndAlgorithm(const std::string& tag, 140 std::list<int32_t>& list, 141 FocusType focusType, bool focusable, FocusStyleType styleType, ScopeFocusAlgorithm algorithm); 142 static RefPtr<FrameNode> CreateFocusViewWithFocusPatternAndAlgorithm(const std::string& tag, 143 std::list<int32_t>& list, 144 FocusType focusType, bool focusable, FocusStyleType styleType, 145 const FocusPaintParam& paintParams, ScopeFocusAlgorithm algorithm); 146 147 RefPtr<FocusManager> focusManager_; 148 }; 149 } // namespace OHOS::Ace::NG 150 151 #endif //TEST_UNITTEST_CORE_EVENT_FOCUS_TEST_BASE_H