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_TEST_UNITTEST_CORE_PATTERN_RICH_EDITOR_RICH_EDITOR_COMMON_TEST_NG_H
17 #define FOUNDATION_ACE_TEST_UNITTEST_CORE_PATTERN_RICH_EDITOR_RICH_EDITOR_COMMON_TEST_NG_H
18 
19 #include <optional>
20 #include <vector>
21 
22 #include "gtest/gtest.h"
23 
24 #define private public
25 #define protected public
26 
27 #include "test/mock/base/mock_task_executor.h"
28 #include "test/mock/core/common/mock_container.h"
29 #include "test/mock/core/common/mock_data_detector_mgr.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/common/mock_udmf.h"
36 
37 #include "base/geometry/dimension.h"
38 #include "base/geometry/ng/offset_t.h"
39 #include "base/image/pixel_map.h"
40 #include "base/memory/ace_type.h"
41 #include "base/memory/referenced.h"
42 #include "base/utils/string_utils.h"
43 #include "base/window/drag_window.h"
44 #include "core/components/common/layout/constants.h"
45 #include "core/components/common/properties/text_style.h"
46 #include "core/components/text_overlay/text_overlay_theme.h"
47 #include "core/components_ng/base/frame_node.h"
48 #include "core/components_ng/base/geometry_node.h"
49 #include "core/components_ng/base/view_abstract_model.h"
50 #include "core/components_ng/base/view_stack_processor.h"
51 #include "core/components_ng/layout/layout_property.h"
52 #include "core/components_ng/pattern/image/image_pattern.h"
53 #include "core/components_ng/pattern/overlay/keyboard_view.h"
54 #include "core/components_ng/pattern/pattern.h"
55 #include "core/components_ng/pattern/rich_editor/rich_editor_layout_algorithm.h"
56 #include "core/components_ng/pattern/rich_editor/rich_editor_model.h"
57 #include "core/components_ng/pattern/rich_editor/rich_editor_model_ng.h"
58 #include "core/components_ng/pattern/rich_editor/rich_editor_overlay_modifier.h"
59 #include "core/components_ng/pattern/rich_editor/rich_editor_pattern.h"
60 #include "core/components_ng/pattern/rich_editor/rich_editor_theme.h"
61 #include "core/components_ng/pattern/rich_editor/selection_info.h"
62 #include "core/components_ng/pattern/root/root_pattern.h"
63 #include "core/components_ng/pattern/select_overlay/select_overlay_property.h"
64 #include "core/components_ng/pattern/text/span_model_ng.h"
65 #include "core/components_ng/pattern/text/span_node.h"
66 #include "core/components_ng/pattern/text_field/text_field_manager.h"
67 #include "core/components_ng/pattern/text_field/text_selector.h"
68 #include "core/components_ng/render/paragraph.h"
69 #include "core/components_v2/inspector/inspector_constants.h"
70 #include "core/event/key_event.h"
71 #include "core/event/mouse_event.h"
72 #include "core/event/touch_event.h"
73 #include "core/pipeline/base/constants.h"
74 #include "test/unittest/core/pattern/test_ng.h"
75 
76 namespace OHOS::Ace::NG {
77 namespace {
78 SelectionRangeInfo testSelectionRange(0, 0);
79 int32_t callBack1 = 0;
80 int32_t callBack2 = 0;
81 int32_t callBack3 = 0;
82 const std::string INIT_VALUE_1 = "hello1";
83 const std::string INIT_VALUE_2 = "hello2";
84 const std::string INIT_VALUE_3 = "hello world! hello world! hello world!";
85 const std::string TEST_INSERT_VALUE = "s";
86 const std::string TEST_INSERT_LINE_SEP = "\n";
87 const std::string EXCEPT_VALUE = "h\n";
88 const Dimension FONT_SIZE_VALUE = Dimension(20.1, DimensionUnit::PX);
89 const Dimension FONT_SIZE_VALUE_2 = Dimension(40, DimensionUnit::PX);
90 const Color TEXT_COLOR_VALUE = Color::FromRGB(255, 100, 100);
91 const Ace::FontStyle ITALIC_FONT_STYLE_VALUE = Ace::FontStyle::ITALIC;
92 const Ace::FontWeight FONT_WEIGHT_VALUE = Ace::FontWeight::W100;
93 const Ace::FontWeight FONT_WEIGHT_BOLD = Ace::FontWeight::BOLD;
94 const std::vector<std::string> FONT_FAMILY_VALUE = { "cursive" };
95 const Ace::TextDecoration TEXT_DECORATION_VALUE = Ace::TextDecoration::INHERIT;
96 const Color TEXT_DECORATION_COLOR_VALUE = Color::FromRGB(255, 100, 100);
97 const Ace::TextCase TEXT_CASE_VALUE = Ace::TextCase::LOWERCASE;
98 const Dimension LETTER_SPACING = Dimension(10, DimensionUnit::PX);
99 const Dimension LINE_HEIGHT_VALUE = Dimension(20.1, DimensionUnit::PX);
100 const Shadow TEXT_SHADOW1 = Shadow(0, 0, Offset(), Color::RED);
101 const Shadow TEXT_SHADOW2 = Shadow(0, 0, Offset(), Color::WHITE);
102 const std::vector<Shadow> SHADOWS { TEXT_SHADOW1, TEXT_SHADOW2 };
103 const std::string IMAGE_VALUE = "image1";
104 const std::string BUNDLE_NAME = "bundleName";
105 const std::string MODULE_NAME = "moduleName";
106 const std::string PREVIEW_TEXT_VALUE1 = "nihao";
107 const std::string PREVIEW_TEXT_VALUE2 = "nihaodajia";
108 const std::string PREVIEW_TEXT_VALUE3 = "dajia";
109 const std::string ROOT_TAG = "root";
110 const CalcLength CALC_LENGTH_CALC { 10.0, DimensionUnit::CALC };
111 const CalcLength ERROR_CALC_LENGTH_CALC { -10.0, DimensionUnit::CALC };
112 const Dimension CALC_TEST { 10.0, DimensionUnit::CALC };
113 const Dimension ERROR_CALC_TEST { -10.0, DimensionUnit::CALC };
114 const Offset MOUSE_GLOBAL_LOCATION = { 100, 200 };
115 constexpr int32_t WORD_LIMIT_LEN = 6;
116 constexpr int32_t WORD_LIMIT_RETURN = 2;
117 constexpr int32_t BEYOND_LIMIT_RETURN = 4;
118 constexpr int32_t DEFAULT_RETURN_VALUE = -1;
119 constexpr int32_t THIRD_PARAM = 2;
120 const float CONTAINER_WIDTH = 300.0f;
121 const float CONTAINER_HEIGHT = 300.0f;
122 const float BUILDER_WIDTH = 150.0f;
123 const float BUILDER_HEIGHT = 75.0f;
124 const SizeF BUILDER_SIZE(BUILDER_WIDTH, BUILDER_HEIGHT);
125 const uint32_t SYMBOL_ID = 1;
126 std::list<std::pair<std::string, int32_t>> TEXT_FONTFEATURE = {{ "subs", 1 }};
127 std::list<std::pair<std::string, int32_t>> TEXT_FONTFEATURE_2 = {{ "subs", 0 }};
128 std::vector<Color> SYMBOL_COLOR_LIST_1 = { Color::FromRGB(255, 100, 100) };
129 std::vector<Color> SYMBOL_COLOR_LIST_2 = { Color::FromRGB(255, 100, 100), Color::FromRGB(255, 255, 100) };
130 const uint32_t RENDER_STRATEGY_SINGLE = 0;
131 const uint32_t RENDER_STRATEGY_MULTI_COLOR = 1;
132 const uint32_t EFFECT_STRATEGY_NONE = 0;
133 const uint32_t EFFECT_STRATEGY_SCALE = 1;
134 const SizeF CONTAINER_SIZE(720.0f, 1136.0f);
135 constexpr float DEFAILT_OPACITY = 0.2f;
136 constexpr Color SYSTEM_CARET_COLOR = Color(0xff007dff);
137 constexpr Color SYSTEM_SELECT_BACKGROUND_COLOR = Color(0x33007dff);
138 constexpr float CONTEXT_WIDTH_VALUE = 300.0f;
139 constexpr float CONTEXT_HEIGHT_VALUE = 150.0f;
140 const Color DEFAULT_TEXT_COLOR_VALUE = Color::FromARGB(229, 0, 0, 0);
141 bool g_isOnWillChangeCalled = false;
142 bool g_isOnDidChangeCalled = false;
143 bool g_isOnEditChangeCalled = false;
144 RichEditorChangeValue onWillChangeValue;
145 RichEditorChangeValue onDidChangeValue;
146 auto& onWillRangeBefore = onWillChangeValue.rangeBefore_;
147 auto& onWillReplacedSpans = onWillChangeValue.replacedSpans_;
148 auto& onWillReplacedImageSpans = onWillChangeValue.replacedImageSpans_;
149 auto& onWillReplacedSymbolSpans = onWillChangeValue.replacedSymbolSpans_;
150 auto& onDidRangeBefore = onDidChangeValue.rangeBefore_;
151 auto& onDidRangeAfter = onDidChangeValue.rangeAfter_;
152 const TextStyle TEXT_STYLE_1(10.0);
153 const TextStyle TEXT_STYLE_2(20.0);
154 const TextStyle TEXT_STYLE_3(30.0);
155 TextSpanOptions TEXT_SPAN_OPTIONS_1 = { .value = INIT_VALUE_1, .style = TEXT_STYLE_1 };
156 const ImageSpanAttribute IMAGE_SPAN_ATTRIBUTE_1 = {
157     .size = ImageSpanSize{ .width = 200.0_px, .height = 100.0_px },
158     .verticalAlign = VerticalAlign::CENTER,
159     .objectFit = ImageFit::COVER,
160     .marginProp = std::nullopt,
161     .borderRadius = std::nullopt,
162     .paddingProp = std::nullopt
163 };
164 const ImageSpanOptions IMAGE_SPAN_OPTIONS_1 = {
165     .offset = std::nullopt,
166     .image = "app.media.icon",
167     .bundleName = std::nullopt,
168     .moduleName = std::nullopt,
169     .imagePixelMap = std::nullopt,
170     .imageAttribute = IMAGE_SPAN_ATTRIBUTE_1
171 };
172 const SymbolSpanOptions SYMBOL_SPAN_OPTIONS_1 = {
173     .offset = std::nullopt,
174     .symbolId = 0,
175     .style = TEXT_STYLE_1,
176     .resourceObject = nullptr
177 };
178 auto BUILDER_NODE_1 = FrameNode::GetOrCreateFrameNode(V2::ROW_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(),
179     []() { return AceType::MakeRefPtr<LinearLayoutPattern>(false); });
180 } // namespace
181 
182 struct TestCursorItem {
183     int32_t index;
184     CaretMetricsF caretMetricsFDown;
185     CaretMetricsF caretMetricsFUp;
186 };
187 
188 struct TestParagraphRect {
189     int32_t start;
190     int32_t end;
191     std::vector<RectF> rects;
192 };
193 
194 struct TestParagraphItem {
195     int32_t start;
196     int32_t end;
197     int32_t height;
198     ParagraphStyle paragraphStyle;
199     std::map<int32_t, Offset> indexOffsetMap;
200     std::vector<TestCursorItem> testCursorItems;
201     std::vector<TestParagraphRect> testParagraphRects;
202 };
203 
204 class RichEditorCommonTestNg : public TestNG {
205 public:
206     void AddSpan(const std::string& content);
207     void AddImageSpan();
208     void AddParagraph(TestParagraphItem testParagraphItem);
209     void ClearParagraph();
210     void ClearSpan();
211     void InitAdjustObject(MockDataDetectorMgr& mockDataDetectorMgr);
212     void RequestFocus();
213     void GetFocus(const RefPtr<RichEditorPattern>& pattern);
214     void OnDrawVerify(const SelectSpanType& type, const std::string& text, SymbolSpanOptions options, Offset offset,
215         bool selected = false);
216 
MockKeyboardBuilder()217     static void MockKeyboardBuilder() {}
218     RefPtr<FrameNode> richEditorNode_;
219 };
220 } // namespace OHOS::Ace::NG
221 
222 #endif // FOUNDATION_ACE_TEST_UNITTEST_CORE_PATTERN_RICH_EDITOR_RICH_EDITOR_COMMON_TEST_NG_H