Merge "Makes the LTR/RTL alignment of text follow the system language by default...
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-internal / utc-Dali-TextEditor-internal.cpp
1 /*
2  * Copyright (c) 2021 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 <iostream>
19 #include <stdlib.h>
20
21 #include <dali-toolkit-test-suite-utils.h>
22 #include <dali-toolkit/dali-toolkit.h>
23
24 #include <dali-toolkit/internal/text/rendering/atlas/atlas-glyph-manager.h>
25 #include <dali-toolkit/internal/controls/text-controls/text-editor-impl.h>
26 #include <dali-toolkit/internal/text/text-controller.h>
27 #include <dali-toolkit/internal/text/text-controller-impl.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
142 int UtcDaliTextEditorFontPointSizeLargerThanAtlasPlaceholderCase(void)
143 {
144   ToolkitTestApplication application;
145   tet_infoline(" UtcDaliTextEditorFontPointSizeLargerThanAtlasPlaceholderCase ");
146
147   //Set Map of placeholder: text, font-family and point-size
148   Property::Map placeholderMapSet;
149   placeholderMapSet["text"] = "A";
150   placeholderMapSet["fontFamily"] = "DejaVu Sans";
151   placeholderMapSet["pixelSize"] = 1000.0f;
152
153   // Create a text editor
154   TextEditor textEditor = TextEditor::New();
155   //Set size to avoid automatic eliding
156   textEditor.SetProperty( Actor::Property::SIZE, Vector2(1025, 1025));
157   //Set placeholder
158   textEditor.SetProperty( TextEditor::Property::PLACEHOLDER, placeholderMapSet) ;
159
160   application.GetScene().Add( textEditor );
161
162   application.SendNotification();
163   application.Render();
164
165   //Check if Glyph is added to AtlasGlyphManger or not
166   int countAtlas = AtlasGlyphManager::Get().GetMetrics().mAtlasMetrics.mAtlasCount;
167   DALI_TEST_EQUALS( countAtlas, 1, TEST_LOCATION );
168
169   END_TEST;
170 }
171
172 int UtcDaliTextEditorBackgroundTag(void)
173 {
174   ToolkitTestApplication application;
175   tet_infoline("UtcDaliTextEditorBackgroundTag\n");
176
177   TextEditor editor = TextEditor::New();
178   DALI_TEST_CHECK( editor );
179
180   editor.SetProperty( TextEditor ::Property::ENABLE_MARKUP,  true );
181   editor.SetProperty( TextEditor::Property::TEXT, "H<background color='red'>e</background> Worl<background color='yellow'>d</background>" );
182   application.GetScene().Add( editor );
183   application.SendNotification();
184   application.Render();
185
186   Toolkit::Internal::TextEditor& editorImpl = GetImpl( editor );
187   const ColorIndex* const backgroundColorIndicesBuffer = editorImpl.GetTextController()->GetTextModel()->GetBackgroundColorIndices();
188
189   DALI_TEST_CHECK( backgroundColorIndicesBuffer );
190
191   //default color
192   DALI_TEST_EQUALS( backgroundColorIndicesBuffer[0], 0u, TEST_LOCATION);
193
194   //red color
195   DALI_TEST_EQUALS( backgroundColorIndicesBuffer[1], 1u, TEST_LOCATION);
196
197   //yellow color
198   DALI_TEST_EQUALS( backgroundColorIndicesBuffer[7], 2u, TEST_LOCATION);
199
200   END_TEST;
201 }
202
203 int UtcDaliTextEditorTextWithSpan(void)
204 {
205   ToolkitTestApplication application;
206   tet_infoline("UtcDaliTextEditorTextWithSpan\n");
207
208   TextEditor editor = TextEditor::New();
209   DALI_TEST_CHECK( editor );
210
211   editor.SetProperty( TextEditor ::Property::ENABLE_MARKUP,  true );
212   editor.SetProperty( TextEditor::Property::TEXT, "Hello Span" );
213   application.GetScene().Add( editor );
214
215   application.SendNotification();
216   application.Render();
217
218   Vector3 originalSize = editor.GetNaturalSize();
219   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" );
220
221   application.SendNotification();
222   application.Render();
223
224   Vector3 spanSize = editor.GetNaturalSize();
225
226   DALI_TEST_GREATER(spanSize.width, originalSize.width, TEST_LOCATION);
227
228   Toolkit::Internal::TextEditor& editorImpl = GetImpl( editor );
229   const ColorIndex* const colorIndicesBuffer1 = editorImpl.GetTextController()->GetTextModel()->GetColorIndices();
230
231   DALI_TEST_CHECK( colorIndicesBuffer1 );
232
233   //default color
234   DALI_TEST_EQUALS( colorIndicesBuffer1[0], 0u, TEST_LOCATION);
235
236   //span color
237   DALI_TEST_EQUALS( colorIndicesBuffer1[1], 1u, TEST_LOCATION);
238
239   //default color
240   DALI_TEST_EQUALS( colorIndicesBuffer1[6], 0u, TEST_LOCATION);
241
242
243   editor.SetProperty( TextEditor::Property::TEXT, "<span font-size='45'>H</span>ello <span text-color='red'>S</span>pan" );
244
245   application.SendNotification();
246   application.Render();
247
248   const ColorIndex* const colorIndicesBuffer2 = editorImpl.GetTextController()->GetTextModel()->GetColorIndices();
249
250   DALI_TEST_CHECK( colorIndicesBuffer2 );
251
252   //default color
253   DALI_TEST_EQUALS( colorIndicesBuffer2[0], 0u, TEST_LOCATION);
254
255   //default color
256   DALI_TEST_EQUALS( colorIndicesBuffer2[1], 0u, TEST_LOCATION);
257
258   //span color
259   DALI_TEST_EQUALS( colorIndicesBuffer2[6], 1u, TEST_LOCATION);
260
261   //default color
262   DALI_TEST_EQUALS( colorIndicesBuffer2[7], 0u, TEST_LOCATION);
263
264   END_TEST;
265 }
266
267 int UtcDaliTextEditorControlBackgroundColor(void)
268 {
269   ToolkitTestApplication application;
270   tet_infoline(" UtcDaliTextEditorControlBackgroundColor\n");
271
272   TextEditor editor = TextEditor::New();
273   DALI_TEST_CHECK(editor);
274
275   Vector4 backgroundColor;
276
277   editor.SetProperty(TextEditor::Property::TEXT, "Background Color");
278   application.GetScene().Add(editor);
279   application.SendNotification();
280   application.Render();
281
282   Toolkit::Internal::TextEditor& editorImpl = GetImpl(editor);
283   ControllerPtr controller = editorImpl.GetTextController();
284   Controller::Impl& controllerImpl = Controller::Impl::GetImplementation(*controller.Get());
285
286   // Default color is transparent
287   controllerImpl.mEditableControlInterface->GetControlBackgroundColor(backgroundColor);
288   DALI_TEST_EQUALS(backgroundColor, Color::TRANSPARENT, TEST_LOCATION);
289
290   // Set background color to red
291   editor.SetBackgroundColor(Color::RED);
292   application.SendNotification();
293   application.Render();
294
295   // Should be red
296   controllerImpl.mEditableControlInterface->GetControlBackgroundColor(backgroundColor);
297   DALI_TEST_EQUALS(backgroundColor, Color::RED, TEST_LOCATION);
298
299   END_TEST;
300 }