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 <array>
20 #include <cstddef>
21 #include <memory>
22 #include <optional>
23 #include <string>
24 #include <unordered_map>
25 #include <utility>
26 #include <vector>
27 #ifdef WINDOWS_PLATFORM
28 #include <Windows.h>
29 #else
30 #include <unistd.h>
31 #endif
32 
33 #include "gtest/gtest.h"
34 #include <unicode/uversion.h>
35 #include <unicode/putil.h>
36 #include <unicode/uclean.h>
37 
38 #define private public
39 #define protected public
40 
41 #include "test/mock/base/mock_task_executor.h"
42 #include "test/mock/core/common/mock_container.h"
43 #include "test/mock/core/common/mock_data_detector_mgr.h"
44 #include "test/mock/core/common/mock_theme_manager.h"
45 #include "test/mock/core/pipeline/mock_pipeline_context.h"
46 #include "test/mock/core/render/mock_paragraph.h"
47 #include "test/mock/core/render/mock_render_context.h"
48 #include "test/mock/core/rosen/mock_canvas.h"
49 #include "test/unittest/core/pattern/test_ng.h"
50 
51 #include "base/geometry/dimension.h"
52 #include "base/geometry/ng/offset_t.h"
53 #include "base/geometry/offset.h"
54 #include "base/memory/ace_type.h"
55 #include "base/memory/referenced.h"
56 #include "base/utils/string_utils.h"
57 #include "base/utils/type_definition.h"
58 #include "core/common/ace_application_info.h"
59 #include "core/common/ai/data_detector_mgr.h"
60 #include "core/common/ime/constant.h"
61 #include "core/common/ime/text_editing_value.h"
62 #include "core/common/ime/text_input_action.h"
63 #include "core/common/ime/text_input_type.h"
64 #include "core/common/ime/text_selection.h"
65 #include "core/components/common/layout/constants.h"
66 #include "core/components/common/properties/color.h"
67 #include "core/components/common/properties/text_style.h"
68 #include "core/components/scroll/scroll_bar_theme.h"
69 #include "core/components/text_field/textfield_theme.h"
70 #include "core/components/theme/theme_manager.h"
71 #include "core/components_ng/base/view_stack_processor.h"
72 #include "core/components_ng/pattern/image/image_layout_property.h"
73 #include "core/components_ng/pattern/text_field/text_field_manager.h"
74 #include "core/components_ng/pattern/text_field/text_field_model.h"
75 #include "core/components_ng/pattern/text_field/text_field_model_ng.h"
76 #include "core/components_ng/pattern/text_field/text_field_pattern.h"
77 #include "core/components_ng/pattern/text_field/text_field_event_hub.h"
78 #include "core/components_ng/pattern/text_field/text_input_response_area.h"
79 #include "core/event/key_event.h"
80 #include "core/event/touch_event.h"
81 #include "core/gestures/gesture_info.h"
82 #include "core/components/common/properties/text_style_parser.h"
83 
84 #undef private
85 #undef protected
86 
87 namespace OHOS::Ace::NG {
88 using namespace testing;
89 using namespace testing::ext;
90 const InspectorFilter filter;
91 constexpr double ICON_SIZE = 24;
92 constexpr double ICON_HOT_ZONE_SIZE = 40;
93 constexpr double FONT_SIZE = 16;
94 constexpr int32_t DEFAULT_NODE_ID = 1;
95 constexpr int32_t MAX_BACKWARD_NUMBER = 30;
96 constexpr int32_t MAX_FORWARD_NUMBER = 30;
97 constexpr uint32_t DEFAULT_MAX_LINES = 1;
98 constexpr uint32_t DEFAULT_MAX_LENGTH = 30;
99 constexpr int32_t MIN_PLATFORM_VERSION = 10;
100 constexpr int32_t WORD_LIMIT_LEN = 5;
101 constexpr int32_t WORD_LIMIT_RETURN = 2;
102 constexpr int32_t BEYOND_LIMIT_RETURN = 4;
103 constexpr int32_t DEFAULT_RETURN_VALUE = -1;
104 const std::string DEFAULT_TEXT = "abcdefghijklmnopqrstuvwxyz";
105 const std::string HELLO_TEXT = "hello";
106 const std::string DEFAULT_PLACE_HOLDER = "please input text here";
107 const std::string LOWERCASE_FILTER = "[a-z]";
108 const std::string NUMBER_FILTER = "^[0-9]*$";
109 const Color DEFAULT_PLACE_HODER_COLOR = Color::RED;
110 const Color DEFAULT_SELECTED_BACKFROUND_COLOR = Color::BLUE;
111 const Color DEFAULT_CARET_COLOR = Color::BLACK;
112 const Color DEFAULT_TEXT_COLOR = Color::BLACK;
113 const Dimension DEFAULT_FONT_SIZE = Dimension(16, DimensionUnit::VP);
114 const Dimension DEFAULT_INDENT_SIZE = Dimension(5, DimensionUnit::VP);
115 const FontWeight DEFAULT_FONT_WEIGHT = FontWeight::W500;
116 const std::string DEFAULT_INPUT_FILTER = "[a-z]";
117 const InputStyle DEFAULT_INPUT_STYLE = InputStyle::INLINE;
118 const CopyOptions DEFAULT_COPY_OPTIONS = CopyOptions::InApp;
119 const TextAlign DEFAULT_TEXT_ALIGN = TextAlign::LEFT;
120 const CaretStyle DEFAULT_CARET_STYLE = { Dimension(3, DimensionUnit::VP) };
121 const OHOS::Ace::DisplayMode DEFAULT_DISPLAY_MODE = OHOS::Ace::DisplayMode::AUTO;
122 const TextInputAction DEFAULT_ENTER_KEY_TYPE = TextInputAction::BEGIN;
123 const std::list<std::pair<std::string, int32_t>> FONT_FEATURE_VALUE_1 = ParseFontFeatureSettings("\"ss01\" 1");
124 const std::list<std::pair<std::string, int32_t>> FONT_FEATURE_VALUE_0 = ParseFontFeatureSettings("\"ss01\" 0");
125 const PreviewTextInfo PREVIEW_ONE = {"ni", {-1, -1}};
126 const PreviewTextInfo PREVIEW_TWO = {"你", {-1, -1}};
127 const PreviewTextInfo PREVIEW_THR = {"hello", {0, 5}};
128 const PreviewTextInfo PREVIEW_FOR = {"ab", {0, 2}};
129 const PreviewTextInfo PREVIEW_BAD_DATA = {"bad", {0, -1}};
130 const TextOverflow OVERFLOW_ELLIPSIS = TextOverflow::ELLIPSIS;
131 const TextOverflow OVERFLOW_MARQUEE = TextOverflow::MARQUEE;
132 const TextOverflow OVERFLOW_DEFAULT = TextOverflow::DEFAULT;
133 const WordBreak WORDBREAK_ALL = WordBreak::BREAK_ALL;
134 template<typename CheckItem, typename Expected>
135 struct TestItem {
136     CheckItem item;
137     Expected expected;
138     std::string error;
139     TestItem(CheckItem checkItem, Expected expectedValue, std::string message = "")
140         : item(checkItem), expected(expectedValue), error(std::move(message))
141     {}
142     TestItem() = default;
143 };
144 struct ExpectParagraphParams {
145     float height = 50.f;
146     float longestLine = 460.f;
147     float maxWidth = 460.f;
148     size_t lineCount = 1;
149     bool firstCalc = true;
150     bool secondCalc = true;
151 };
152 constexpr float CONTEXT_WIDTH_VALUE = 300.0f;
153 constexpr float CONTEXT_HEIGHT_VALUE = 150.0f;
154 
155 class TextInputBases : public TestNG {
156 protected:
157     static void SetUpTestSuite();
158     static void TearDownTestSuite();
159     void TearDown() override;
160 
161     void CreateTextField(const std::string& text = "", const std::string& placeHolder = "",
162         const std::function<void(TextFieldModelNG&)>& callback = nullptr);
163     static void ExpectCallParagraphMethods(ExpectParagraphParams params);
164     void GetFocus();
165     RefPtr<TextFieldTheme> GetTheme();
166 
167     RefPtr<FrameNode> frameNode_;
168     RefPtr<TextFieldPattern> pattern_;
169     RefPtr<TextFieldEventHub> eventHub_;
170     RefPtr<TextFieldLayoutProperty> layoutProperty_;
171     RefPtr<TextFieldAccessibilityProperty> accessibilityProperty_;
172 };
173 } // namespace OHOS::Ace::NG
174 
175 #endif // FOUNDATION_ACE_TEST_UNITTEST_CORE_PATTERN_TEXTFIELD_TEXTINPUT_TEST_NG_H