Merge "support strikethrough markup tag" into devel/master
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-internal / utc-Dali-TextEditor-internal.cpp
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <stdlib.h>
19 #include <iostream>
20
21 #include <dali-toolkit-test-suite-utils.h>
22 #include <dali-toolkit/dali-toolkit.h>
23
24 #include <dali-toolkit/internal/controls/text-controls/text-editor-impl.h>
25 #include <dali-toolkit/internal/text/rendering/atlas/atlas-glyph-manager.h>
26 #include <dali-toolkit/internal/text/text-controller-impl.h>
27 #include <dali-toolkit/internal/text/text-controller.h>
28
29 using namespace Dali;
30 using namespace Toolkit;
31 using namespace Text;
32
33 int UtcDaliTextEditorSelectText(void)
34 {
35   ToolkitTestApplication application;
36   tet_infoline("UtcDaliTextEditorSelectText");
37
38   // Create a text editor
39   TextEditor textEditor = TextEditor::New();
40   textEditor.SetProperty(Actor::Property::SIZE, Vector2(400.f, 60.f));
41   textEditor.SetProperty(TextEditor::Property::TEXT, "Hello World");
42
43   // Add the text editor to the stage
44   application.GetScene().Add(textEditor);
45
46   application.SendNotification();
47   application.Render();
48
49   Toolkit::Internal::TextEditor& textEditorImpl = GetImpl(textEditor);
50
51   application.SendNotification();
52   application.Render();
53
54   // Highlight the whole text
55   textEditorImpl.SelectWholeText();
56
57   application.SendNotification();
58   application.Render();
59
60   std::string selectedText = textEditorImpl.GetSelectedText();
61   DALI_TEST_CHECK(selectedText == "Hello World");
62
63   // Select None
64   textEditorImpl.SelectNone();
65
66   application.SendNotification();
67   application.Render();
68
69   selectedText = textEditorImpl.GetSelectedText();
70   DALI_TEST_CHECK(selectedText == "");
71
72   END_TEST;
73 }
74
75 int UtcDaliTextEditorMarkupUnderline(void)
76 {
77   ToolkitTestApplication application;
78   tet_infoline(" UtcDaliTextEditorMarkupUnderline ");
79
80   TextEditor textEditor = TextEditor::New();
81
82   application.GetScene().Add(textEditor);
83
84   textEditor.SetProperty(TextEditor::Property::TEXT, "<u>ABC</u>EF<u>GH</u>");
85   textEditor.SetProperty(TextEditor ::Property::ENABLE_MARKUP, true);
86
87   application.SendNotification();
88   application.Render();
89
90   uint32_t expectedNumberOfUnderlinedGlyphs = 5u;
91
92   Toolkit::Internal::TextEditor& textEditorImpl        = GetImpl(textEditor);
93   const Text::Length             numberOfUnderlineRuns = textEditorImpl.GetTextController()->GetTextModel()->GetNumberOfUnderlineRuns();
94
95   DALI_TEST_EQUALS(numberOfUnderlineRuns, expectedNumberOfUnderlinedGlyphs, TEST_LOCATION);
96
97   Vector<GlyphRun> underlineRuns;
98   underlineRuns.Resize(numberOfUnderlineRuns);
99   textEditorImpl.GetTextController()->GetTextModel()->GetUnderlineRuns(underlineRuns.Begin(), 0u, numberOfUnderlineRuns);
100
101   //ABC are underlined
102   DALI_TEST_EQUALS(underlineRuns[0u].glyphIndex, 0u, TEST_LOCATION);
103   DALI_TEST_EQUALS(underlineRuns[1u].glyphIndex, 1u, TEST_LOCATION);
104   DALI_TEST_EQUALS(underlineRuns[2u].glyphIndex, 2u, TEST_LOCATION);
105
106   //GH are underlined
107   DALI_TEST_EQUALS(underlineRuns[3u].glyphIndex, 5u, TEST_LOCATION);
108   DALI_TEST_EQUALS(underlineRuns[4u].glyphIndex, 6u, TEST_LOCATION);
109
110   END_TEST;
111 }
112
113 int UtcDaliTextEditorFontPointSizeLargerThanAtlas(void)
114 {
115   ToolkitTestApplication application;
116   tet_infoline(" UtcDaliTextEditorFontPointSizeLargerThanAtlas ");
117
118   // Create a text editor
119   TextEditor textEditor = TextEditor::New();
120   //Set size to avoid automatic eliding
121   textEditor.SetProperty(Actor::Property::SIZE, Vector2(1025, 1025));
122   //Set very large font-size using point-size
123   textEditor.SetProperty(TextEditor::Property::POINT_SIZE, 1000);
124   //Specify font-family
125   textEditor.SetProperty(TextEditor::Property::FONT_FAMILY, "DejaVu Sans");
126   //Set text to check if appear or not
127   textEditor.SetProperty(TextEditor::Property::TEXT, "A");
128
129   application.GetScene().Add(textEditor);
130
131   application.SendNotification();
132   application.Render();
133
134   //Check if Glyph is added to AtlasGlyphManger or not
135   int countAtlas = AtlasGlyphManager::Get().GetMetrics().mAtlasMetrics.mAtlasCount;
136   DALI_TEST_EQUALS(countAtlas, 1, TEST_LOCATION);
137
138   END_TEST;
139 }
140
141 int UtcDaliTextEditorFontPointSizeLargerThanAtlasPlaceholderCase(void)
142 {
143   ToolkitTestApplication application;
144   tet_infoline(" UtcDaliTextEditorFontPointSizeLargerThanAtlasPlaceholderCase ");
145
146   //Set Map of placeholder: text, font-family and point-size
147   Property::Map placeholderMapSet;
148   placeholderMapSet["text"]       = "A";
149   placeholderMapSet["fontFamily"] = "DejaVu Sans";
150   placeholderMapSet["pixelSize"]  = 1000.0f;
151
152   // Create a text editor
153   TextEditor textEditor = TextEditor::New();
154   //Set size to avoid automatic eliding
155   textEditor.SetProperty(Actor::Property::SIZE, Vector2(1025, 1025));
156   //Set placeholder
157   textEditor.SetProperty(TextEditor::Property::PLACEHOLDER, placeholderMapSet);
158
159   application.GetScene().Add(textEditor);
160
161   application.SendNotification();
162   application.Render();
163
164   //Check if Glyph is added to AtlasGlyphManger or not
165   int countAtlas = AtlasGlyphManager::Get().GetMetrics().mAtlasMetrics.mAtlasCount;
166   DALI_TEST_EQUALS(countAtlas, 1, TEST_LOCATION);
167
168   END_TEST;
169 }
170
171 int UtcDaliTextEditorBackgroundTag(void)
172 {
173   ToolkitTestApplication application;
174   tet_infoline("UtcDaliTextEditorBackgroundTag\n");
175
176   TextEditor editor = TextEditor::New();
177   DALI_TEST_CHECK(editor);
178
179   editor.SetProperty(TextEditor ::Property::ENABLE_MARKUP, true);
180   editor.SetProperty(TextEditor::Property::TEXT, "H<background color='red'>e</background> Worl<background color='yellow'>d</background>");
181   application.GetScene().Add(editor);
182   application.SendNotification();
183   application.Render();
184
185   Toolkit::Internal::TextEditor& editorImpl                   = GetImpl(editor);
186   const ColorIndex* const        backgroundColorIndicesBuffer = editorImpl.GetTextController()->GetTextModel()->GetBackgroundColorIndices();
187
188   DALI_TEST_CHECK(backgroundColorIndicesBuffer);
189
190   //default color
191   DALI_TEST_EQUALS(backgroundColorIndicesBuffer[0], 0u, TEST_LOCATION);
192
193   //red color
194   DALI_TEST_EQUALS(backgroundColorIndicesBuffer[1], 1u, TEST_LOCATION);
195
196   //yellow color
197   DALI_TEST_EQUALS(backgroundColorIndicesBuffer[7], 2u, TEST_LOCATION);
198
199   END_TEST;
200 }
201
202 int UtcDaliTextEditorTextWithSpan(void)
203 {
204   ToolkitTestApplication application;
205   tet_infoline("UtcDaliTextEditorTextWithSpan\n");
206
207   TextEditor editor = TextEditor::New();
208   DALI_TEST_CHECK(editor);
209
210   editor.SetProperty(TextEditor ::Property::ENABLE_MARKUP, true);
211   editor.SetProperty(TextEditor::Property::TEXT, "Hello Span");
212   application.GetScene().Add(editor);
213
214   application.SendNotification();
215   application.Render();
216
217   Vector3 originalSize = editor.GetNaturalSize();
218   editor.SetProperty(TextEditor::Property::TEXT, "H<span font-size='45' font-family='DejaVu Sans' font-width='condensed' font-slant='italic' text-color='red'>ello</span> Span");
219
220   application.SendNotification();
221   application.Render();
222
223   Vector3 spanSize = editor.GetNaturalSize();
224
225   DALI_TEST_GREATER(spanSize.width, originalSize.width, TEST_LOCATION);
226
227   Toolkit::Internal::TextEditor& editorImpl          = GetImpl(editor);
228   const ColorIndex* const        colorIndicesBuffer1 = editorImpl.GetTextController()->GetTextModel()->GetColorIndices();
229
230   DALI_TEST_CHECK(colorIndicesBuffer1);
231
232   //default color
233   DALI_TEST_EQUALS(colorIndicesBuffer1[0], 0u, TEST_LOCATION);
234
235   //span color
236   DALI_TEST_EQUALS(colorIndicesBuffer1[1], 1u, TEST_LOCATION);
237
238   //default color
239   DALI_TEST_EQUALS(colorIndicesBuffer1[6], 0u, TEST_LOCATION);
240
241   editor.SetProperty(TextEditor::Property::TEXT, "<span font-size='45'>H</span>ello <span text-color='red'>S</span>pan");
242
243   application.SendNotification();
244   application.Render();
245
246   const ColorIndex* const colorIndicesBuffer2 = editorImpl.GetTextController()->GetTextModel()->GetColorIndices();
247
248   DALI_TEST_CHECK(colorIndicesBuffer2);
249
250   //default color
251   DALI_TEST_EQUALS(colorIndicesBuffer2[0], 0u, TEST_LOCATION);
252
253   //default color
254   DALI_TEST_EQUALS(colorIndicesBuffer2[1], 0u, TEST_LOCATION);
255
256   //span color
257   DALI_TEST_EQUALS(colorIndicesBuffer2[6], 1u, TEST_LOCATION);
258
259   //default color
260   DALI_TEST_EQUALS(colorIndicesBuffer2[7], 0u, TEST_LOCATION);
261
262   END_TEST;
263 }
264
265 int UtcDaliTextEditorControlBackgroundColor(void)
266 {
267   ToolkitTestApplication application;
268   tet_infoline(" UtcDaliTextEditorControlBackgroundColor\n");
269
270   TextEditor editor = TextEditor::New();
271   DALI_TEST_CHECK(editor);
272
273   Vector4 backgroundColor;
274
275   editor.SetProperty(TextEditor::Property::TEXT, "Background Color");
276   application.GetScene().Add(editor);
277   application.SendNotification();
278   application.Render();
279
280   Toolkit::Internal::TextEditor& editorImpl     = GetImpl(editor);
281   ControllerPtr                  controller     = editorImpl.GetTextController();
282   Controller::Impl&              controllerImpl = Controller::Impl::GetImplementation(*controller.Get());
283
284   // Default color is transparent
285   controllerImpl.mEditableControlInterface->GetControlBackgroundColor(backgroundColor);
286   DALI_TEST_EQUALS(backgroundColor, Color::TRANSPARENT, TEST_LOCATION);
287
288   // Set background color to red
289   editor.SetBackgroundColor(Color::RED);
290   application.SendNotification();
291   application.Render();
292
293   // Should be red
294   controllerImpl.mEditableControlInterface->GetControlBackgroundColor(backgroundColor);
295   DALI_TEST_EQUALS(backgroundColor, Color::RED, TEST_LOCATION);
296
297   END_TEST;
298 }
299
300 int UtcDaliTextEditorTextPositionWithMinLineAndBigFont(void)
301 {
302   ToolkitTestApplication application;
303   tet_infoline(" UtcDaliTextEditorTextPositionWithMinLine ");
304
305   TextEditor textEditor = TextEditor::New();
306
307   textEditor.SetProperty(TextEditor::Property::TEXT, "<span font-size='45'>H</span>\ni");
308   textEditor.SetProperty(DevelTextEditor::Property::MIN_LINE_SIZE, 50);
309   textEditor.SetProperty(TextEditor ::Property::ENABLE_MARKUP, true);
310
311   application.GetScene().Add(textEditor);
312
313   application.SendNotification();
314   application.Render();
315
316   Toolkit::Internal::TextEditor& textEditorImpl = GetImpl(textEditor);
317   Text::ViewInterface&           view           = textEditorImpl.GetTextController()->GetView();
318
319   Length numberOfGlyphs = view.GetNumberOfGlyphs();
320
321   DALI_TEST_EQUALS(numberOfGlyphs, 3u, Math::MACHINE_EPSILON_1000, TEST_LOCATION);
322
323   Vector<GlyphInfo> glyphs;
324   glyphs.Resize(numberOfGlyphs);
325
326   Vector<Vector2> positions;
327   positions.Resize(numberOfGlyphs);
328
329   float alignmentOffset = 0u;
330   numberOfGlyphs        = view.GetGlyphs(glyphs.Begin(),
331                                   positions.Begin(),
332                                   alignmentOffset,
333                                   0u,
334                                   numberOfGlyphs);
335
336   DALI_TEST_EQUALS(positions[2].y, 165.f, Math::MACHINE_EPSILON_1000, TEST_LOCATION);
337
338   END_TEST;
339 }
340
341 int UtcDaliTextEditorMarkupStrikethrough(void)
342 {
343   ToolkitTestApplication application;
344   tet_infoline(" UtcDaliTextEditorMarkupStrikethrough ");
345
346   TextEditor textEditor = TextEditor::New();
347
348   application.GetScene().Add(textEditor);
349
350   textEditor.SetProperty(TextEditor::Property::TEXT, "<s>ABC</s>EF<s color='red'>GH</s>");
351   textEditor.SetProperty(TextEditor ::Property::ENABLE_MARKUP, true);
352
353   application.SendNotification();
354   application.Render();
355
356   uint32_t expectedNumberOfStrikethroughGlyphs = 2u;
357
358   Toolkit::Internal::TextEditor& textEditorImpl            = GetImpl(textEditor);
359   const Text::Length             numberOfStrikethroughRuns = textEditorImpl.GetTextController()->GetTextModel()->GetNumberOfStrikethroughRuns();
360
361   DALI_TEST_EQUALS(numberOfStrikethroughRuns, expectedNumberOfStrikethroughGlyphs, TEST_LOCATION);
362
363   Vector<StrikethroughGlyphRun> strikethroughRuns;
364   strikethroughRuns.Resize(numberOfStrikethroughRuns);
365   textEditorImpl.GetTextController()->GetTextModel()->GetStrikethroughRuns(strikethroughRuns.Begin(), 0u, numberOfStrikethroughRuns);
366
367   //ABC have strikethrough
368   DALI_TEST_EQUALS(strikethroughRuns[0u].glyphRun.glyphIndex, 0u, TEST_LOCATION);
369   DALI_TEST_EQUALS(strikethroughRuns[0u].glyphRun.numberOfGlyphs, 3u, TEST_LOCATION);
370   DALI_TEST_CHECK(!strikethroughRuns[0u].isColorSet);
371
372   //GH have strikethrough
373   DALI_TEST_EQUALS(strikethroughRuns[1u].glyphRun.glyphIndex, 5u, TEST_LOCATION);
374   DALI_TEST_EQUALS(strikethroughRuns[1u].glyphRun.numberOfGlyphs, 2u, TEST_LOCATION);
375   DALI_TEST_CHECK(strikethroughRuns[1u].isColorSet);
376
377   END_TEST;
378 }