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 FOUNDATION_ACE_TEST_UNITTEST_CORE_PATTERN_TEXTFIELD_TEXTINPUT_TEST_NG_H
17 #define FOUNDATION_ACE_TEST_UNITTEST_CORE_PATTERN_TEXTFIELD_TEXTINPUT_TEST_NG_H
18 
19 #include <functional>
20 #include <optional>
21 
22 #include "gmock/gmock-actions.h"
23 #include "gtest/gtest.h"
24 
25 #define private public
26 #define protected public
27 
28 #include "test/mock/base/mock_task_executor.h"
29 #include "test/mock/core/common/mock_container.h"
30 #include "test/mock/core/common/mock_theme_manager.h"
31 #include "test/mock/core/pipeline/mock_pipeline_context.h"
32 #include "test/mock/core/render/mock_paragraph.h"
33 #include "test/mock/core/render/mock_render_context.h"
34 #include "test/mock/core/rosen/mock_canvas.h"
35 #include "test/mock/core/rosen/testing_typography_properties.h"
36 
37 #include "base/geometry/dimension.h"
38 #include "base/geometry/ng/offset_t.h"
39 #include "base/geometry/offset.h"
40 #include "base/memory/ace_type.h"
41 #include "base/memory/referenced.h"
42 #include "core/components/common/layout/constants.h"
43 #include "core/components/common/properties/text_style_parser.h"
44 #include "core/components/text_overlay/text_overlay_theme.h"
45 #include "core/components_ng/base/frame_node.h"
46 #include "core/components_ng/base/geometry_node.h"
47 #include "core/components_ng/base/view_stack_processor.h"
48 #include "core/components_ng/layout/layout_property.h"
49 #include "core/components_ng/pattern/image/image_pattern.h"
50 #include "core/components_ng/pattern/pattern.h"
51 #include "core/components_ng/pattern/picker/picker_type_define.h"
52 #include "core/components_ng/pattern/root/root_pattern.h"
53 #include "core/components_ng/pattern/text/span_model_ng.h"
54 #include "core/components_ng/pattern/text/text_accessibility_property.h"
55 #include "core/components_ng/pattern/text/text_content_modifier.h"
56 #include "core/components_ng/pattern/text/text_event_hub.h"
57 #include "core/components_ng/pattern/text/text_layout_property.h"
58 #include "core/components_ng/pattern/text/text_model_ng.h"
59 #include "core/components_ng/pattern/text/text_paint_method.h"
60 #include "core/components_ng/pattern/text/text_pattern.h"
61 #include "core/components_ng/pattern/text/typed_text.h"
62 #include "core/components_ng/pattern/text_field/text_selector.h"
63 #include "core/components_ng/render/paragraph.h"
64 #include "core/components_v2/inspector/inspector_constants.h"
65 #include "core/event/key_event.h"
66 #include "core/event/mouse_event.h"
67 #include "frameworks/base/window/drag_window.h"
68 #include "frameworks/core/components_ng/pattern/root/root_pattern.h"
69 
70 #undef private
71 #undef protected
72 
73 namespace OHOS::Ace::NG {
74 using namespace testing;
75 using namespace testing::ext;
76 
77 const InspectorFilter filter;
78 constexpr float RK356_WIDTH = 720.0f;
79 constexpr float RK356_HEIGHT = 1136.0f;
80 constexpr float RK356_LOW_WIDTH = 50.0f;
81 constexpr float RK356_LOW_HEIGHT = 20.0f;
82 constexpr float TEXT_WIDTH = 100.0f;
83 constexpr float TEXT_HEIGHT = 75.0f;
84 constexpr float LARGE_WIDTH = 1000000.0f;
85 constexpr float DIMENSION = 10.0f;
86 constexpr float CONTEXT_WIDTH_VALUE = 10.0f;
87 constexpr float CONTEXT_LARGE_WIDTH_VALUE = 10000.0f;
88 constexpr float CONTEXT_HEIGHT_VALUE = 10.0f;
89 constexpr float BASE_LINE_OFFSET_VALUE = 1.0;
90 constexpr float BASE_LINE_OFFSET = 20.f;
91 constexpr double RECT_X_VALUE = 1.0;
92 constexpr double RECT_SECOND_X_VALUE = 2.0;
93 constexpr double RECT_Y_VALUE = 1.0;
94 constexpr double RECT_WIDTH_VALUE = 5.0;
95 constexpr double RECT_HEIGHT_VALUE = 10.0;
96 constexpr double BLURRADIUS_VALUE = 0.0;
97 constexpr double SPREADRADIUS_VALUE = 0.0;
98 constexpr double ADAPT_OFFSETY_VALUE = 5.0;
99 constexpr double ADAPT_OFFSETX_VALUE = 5.0;
100 const std::string TEXT_CONTENT = "text";
101 constexpr int32_t TEXT_ERROR = -1;
102 constexpr int32_t TEXT_SIZE_INT = 10;
103 constexpr int32_t MAX_LINES = 500;
104 constexpr int32_t CURSOR_COLOR = 0;
105 constexpr int32_t SELECTED_COLOR = 0;
106 constexpr Dimension BASELINE_OFFSET_VALUE = Dimension(20, DimensionUnit::PX);
107 constexpr Dimension ADAPT_MIN_FONT_SIZE_VALUE = Dimension(50, DimensionUnit::PX);
108 constexpr Dimension ADAPT_OVER_MIN_FONT_SIZE_VALUE = Dimension(100, DimensionUnit::PX);
109 constexpr Dimension ADAPT_ZERO_FONT_SIZE_VALUE = Dimension(0, DimensionUnit::PX);
110 constexpr Dimension ADAPT_MAX_FONT_SIZE_VALUE_S = Dimension(80, DimensionUnit::PX);
111 constexpr Dimension ADAPT_FONT_SIZE_STEP_VALUE = Dimension(10, DimensionUnit::PX);
112 const std::string CREATE_VALUE = "Hello World";
113 const std::u16string CREATE_VALUE_W = u"Hello World";
114 const SizeF CONTAINER_SIZE(RK356_WIDTH, RK356_HEIGHT);
115 const SizeF CONTAINER_LOW_SIZE(RK356_LOW_WIDTH, RK356_LOW_HEIGHT);
116 const SizeF TEXT_SIZE(TEXT_WIDTH, TEXT_HEIGHT);
117 const SizeF LARGE_CONTAINER_SIZE(LARGE_WIDTH, TEXT_HEIGHT);
118 constexpr Dimension ADAPT_LINE_HEIGHT_VALUE = Dimension(10, DimensionUnit::PX);
119 constexpr Dimension ADAPT_FONT_SIZE_VALUE = Dimension(30, DimensionUnit::PX);
120 constexpr Dimension ADAPT_BASE_LINE_OFFSET_VALUE = Dimension(10, DimensionUnit::PX);
121 constexpr Dimension ADAPT_LINE_SPACING_VALUE = Dimension(10, DimensionUnit::PX);
122 const Dimension FONT_SIZE_VALUE = Dimension(20.1, DimensionUnit::PX);
123 const Color TEXT_COLOR_VALUE = Color::FromRGB(255, 100, 100);
124 const SizeT<float> LARGE_SIZE = SizeT<float>(10000.0f, 1000.0f);
125 const Ace::FontStyle ITALIC_FONT_STYLE_VALUE = Ace::FontStyle::ITALIC;
126 const Ace::FontWeight FONT_WEIGHT_VALUE = Ace::FontWeight::W100;
127 const std::vector<std::string> FONT_FAMILY_VALUE = { "cursive" };
128 const Ace::TextAlign TEXT_ALIGN_VALUE = Ace::TextAlign::CENTER;
129 const Ace::TextOverflow TEXT_OVERFLOW_VALUE = Ace::TextOverflow::CLIP;
130 const uint32_t MAX_LINES_VALUE = 10;
131 const Dimension LINE_HEIGHT_VALUE = Dimension(20.1, DimensionUnit::PX);
132 const Ace::TextDecoration TEXT_DECORATION_VALUE = Ace::TextDecoration::INHERIT;
133 const Color TEXT_DECORATION_COLOR_VALUE = Color::FromRGB(255, 100, 100);
134 const Ace::TextCase TEXT_CASE_VALUE = Ace::TextCase::LOWERCASE;
135 const Dimension ADAPT_MAX_FONT_SIZE_VALUE = Dimension(200, DimensionUnit::PX);
136 const Dimension LETTER_SPACING = Dimension(10, DimensionUnit::PX);
137 const Dimension TEXT_INDENT = Dimension(5, DimensionUnit::PX);
138 const Dimension ADAPT_UPDATE_FONTSIZE_VALUE = Dimension(50, DimensionUnit::PX);
139 const Dimension LINE_SPACING_VALUE = Dimension(20, DimensionUnit::PX);
140 const Dimension LINE_SPACING_VALUE_1 = Dimension(30, DimensionUnit::PX);
141 const std::string ROOT_TAG("root");
142 constexpr int32_t NODE_ID = 143;
143 const Color FOREGROUND_COLOR_VALUE = Color::FOREGROUND;
144 const RectF CONTENT_RECT(3.0, 3.0, TEXT_WIDTH, TEXT_HEIGHT);
145 constexpr int32_t ROOT_NODE_ID = 113;
146 const std::string EMPTY_TEXT = "";
147 const CopyOptions copyOption = CopyOptions::None;
148 constexpr int32_t UNKNOWN_REASON = 1;
149 constexpr float TEXT_RECT_SIZE_ZEOR = 0.0f;
150 constexpr float TEXT_RECT_WIDTH = 10.0f;
151 constexpr float TEXT_RECT_TOP_ONE = 10.0f;
152 constexpr float TEXT_RECT_TOP_TWO = 20.0f;
153 constexpr float TEXT_CONTENT_SIZE = 50.0f;
154 const std::string TEXT_DEFAULT_VALUE = "{\"style\":\"FontStyle.Normal\",\"size\":\"16.00fp\",\"weight\":"
155     "\"FontWeight.Normal\",\"variableFontWeight\":\"0\",\"enableVariableFontWeight\":\"false\",\"family\":"
156     "\"HarmonyOS Sans\"}";
157 const std::string TEXT_EQUALS_VALUE =
158     R"({"style":"FontStyle.Italic","size":"20.10px","weight":"FontWeight.Bold","family":"cursive"})";
159 const Ace::WordBreak TEXT_WORD_BREAK = Ace::WordBreak::BREAK_ALL;
160 const Ace::LineBreakStrategy TEXT_LINE_BREAK_STRATEGY = Ace::LineBreakStrategy::GREEDY;
161 const std::string TEXT_FOR_AI_SINGLE = "phone: 18888888888";
162 const std::string TEXT_FOR_AI = "phone: 12345678900,url: www.baidu.com";
163 const std::string SPAN_PHONE = "12345678900";
164 const std::string SPAN_URL = "www.baidu.com";
165 constexpr int32_t AI_SPAN_START = 7;
166 constexpr int32_t AI_SPAN_END = 18;
167 constexpr int32_t AI_SPAN_START_II = 24;
168 constexpr int32_t AI_SPAN_END_II = 37;
169 const std::list<std::pair<std::string, int32_t>> FONT_FEATURE_VALUE_1 = ParseFontFeatureSettings("\"ss01\" 1");
170 const std::list<std::pair<std::string, int32_t>> FONT_FEATURE_VALUE_0 = ParseFontFeatureSettings("\"ss01\" 0");
171 const std::string MULTIPLE_SPAN1 = "Span1";
172 const std::string MULTIPLE_SPAN2 = "Span2\n";
173 const std::string MULTIPLE_SPAN3 = "Span3\n";
174 const std::string MULTIPLE_SPAN4 = "Span4";
175 using OnClickCallback = std::function<void(const BaseEventInfo* info)>;
176 using DragDropBaseCallback = std::function<DragDropBaseInfo(const RefPtr<OHOS::Ace::DragEvent>&, const std::string&)>;
177 constexpr uint32_t CODE1 = 6;
178 constexpr uint32_t CODE2 = 10000;
179 constexpr uint32_t CODE3 = 111888;
180 const char TEXT_CONTENT1[] = "hi";
181 const char TEXT_CONTENT2[] = "hello1";
182 
183 struct TestProperty {
184     std::optional<Dimension> fontSizeValue = std::nullopt;
185     std::optional<Color> textColorValue = std::nullopt;
186     std::optional<Ace::FontStyle> italicFontStyleValue = std::nullopt;
187     std::optional<Ace::FontWeight> fontWeightValue = std::nullopt;
188     std::optional<std::vector<std::string>> fontFamilyValue = std::nullopt;
189     std::optional<Ace::TextAlign> textAlignValue = std::nullopt;
190     std::optional<Ace::TextOverflow> textOverflowValue = std::nullopt;
191     std::optional<uint32_t> maxLinesValue = std::nullopt;
192     std::optional<Dimension> lineHeightValue = std::nullopt;
193     std::optional<Dimension> lineSpacingValue = std::nullopt;
194     std::optional<Ace::TextDecoration> textDecorationValue = std::nullopt;
195     std::optional<Color> textDecorationColorValue = std::nullopt;
196     std::optional<Dimension> baselineOffsetValue = std::nullopt;
197     std::optional<Ace::TextCase> textCaseValue = std::nullopt;
198     std::optional<Dimension> adaptMinFontSize = std::nullopt;
199     std::optional<Dimension> adaptMaxFontSize = std::nullopt;
200     std::optional<Dimension> letterSpacing = std::nullopt;
201     std::optional<Dimension> textIndent = std::nullopt;
202     std::optional<Ace::WordBreak> wordBreak = std::nullopt;
203     std::optional<Ace::LineBreakStrategy> lineBreakStrategy = std::nullopt;
204 };
205 
206 struct ImageSpanNodeProperty {
207     std::optional<std::string> imageSrc = std::nullopt;
208     std::optional<RefPtr<PixelMap>> pixelMap = std::nullopt;
209     std::optional<MarginPropertyF> margin = std::nullopt;
210     std::optional<ImageFit> imageFit = std::nullopt;
211     std::optional<VerticalAlign> verticalAlign = std::nullopt;
212 };
213 
214 class TextBases : public testing::Test {
215 public:
216     static void SetUpTestSuite();
217     static void TearDownTestSuite();
218     void SetUp() override;
219     void TearDown() override;
220     RefPtr<SpanNode> CreateSpanNodeWithSetDefaultProperty(const std::string& content);
221     RefPtr<ImageSpanNode> CreateImageSpanNode(const ImageSpanNodeProperty& property);
222 
223 protected:
224     static void onClickFunc(const BaseEventInfo* info);
225     static void onRemoteMessage();
226     static void OnDragDropFunction(const RefPtr<OHOS::Ace::DragEvent>&, const std::string&);
227     static DragDropBaseInfo OnDragStartFunction(const RefPtr<OHOS::Ace::DragEvent>&, const std::string&);
228     static void SuppressMockParagraph();
229     static std::pair<RefPtr<FrameNode>, RefPtr<TextPattern>> Init();
230     static void TestUpdateScenario(const RefPtr<TextPattern>& pattern);
231     static void ConstructSpanItemList1(std::list<RefPtr<SpanItem>>& spans);
232     static RefPtr<FrameNode> CreateTextParagraph(const std::string& createValue, const TestProperty& testProperty);
233     static void SetContentModifier(TextContentModifier& textContentModifier);
234     static void SetPaintMethodModifier(TextPaintMethod& textPaintMethod);
235     static void UpdateTextLayoutProperty(RefPtr<TextLayoutProperty> textLayoutProperty);
236 };
237 } // namespace OHOS::Ace::NG
238 
239 #endif // FOUNDATION_ACE_TEST_UNITTEST_CORE_PATTERN_TEXTFIELD_TEXTINPUT_TEST_NG_H