Merge "Fix issue using broken image" into devel/master
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-internal / utc-Dali-Text-TextSpannable.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 <unistd.h>
20 #include <iostream>
21
22 #include <dali-toolkit-test-suite-utils.h>
23 #include <dali-toolkit/dali-toolkit.h>
24 #include <dali-toolkit/devel-api/controls/text-controls/text-spannable.h>
25 #include <dali-toolkit/devel-api/text/spannable-string.h>
26 #include <dali-toolkit/devel-api/text/spans/font-span.h>
27 #include <dali-toolkit/devel-api/text/spans/foreground-color-span.h>
28 #include <dali-toolkit/devel-api/text/spans/underline-span.h>
29 #include <dali-toolkit/devel-api/text/text-enumerations-devel.h>
30 #include <dali-toolkit/internal/controls/text-controls/text-editor-impl.h>
31 #include <dali-toolkit/internal/controls/text-controls/text-field-impl.h>
32 #include <dali-toolkit/internal/controls/text-controls/text-label-impl.h>
33 #include <dali-toolkit/internal/text/controller/text-controller.h>
34 #include <dali-toolkit/internal/text/font-description-run.h>
35 #include <dali-toolkit/internal/text/rendering/text-typesetter.h>
36 #include <dali-toolkit/internal/text/rendering/view-model.h>
37 #include <dali-toolkit/internal/text/text-view.h>
38 #include <dali-toolkit/public-api/text/text-enumerations.h>
39 #include <toolkit-text-utils.h>
40
41 using namespace Dali;
42 using namespace Toolkit;
43
44 namespace
45 {
46 const std::string DEFAULT_FONT_DIR("/resources/fonts");
47 const float       PIXEL_FORMAT_64_FACTOR = 64.f; ///< 64.f is used to convert from point size to 26.6 pixel format.
48 } // namespace
49
50 Text::SpannableString CreateSpannableStringForForegroundColorSpan()
51 {
52   Text::SpannableString spannableString = Text::SpannableString::New("Hello مرحبا");
53   DALI_TEST_CHECK(spannableString);
54
55   auto isAddedGreen = spannableString.AttachSpan(
56     Text::ForegroundColorSpan::New(Color::GREEN),
57     Text::Range::New(5u, 7u));
58   DALI_TEST_CHECK(isAddedGreen);
59
60   return spannableString;
61 }
62
63 Text::SpannableString CreateSpannableStringForFontSpan()
64 {
65   Text::SpannableString spannableString = Text::SpannableString::New("Hello World");
66   DALI_TEST_CHECK(spannableString);
67
68   auto isAddedFontSpan = spannableString.AttachSpan(
69     Text::FontSpan::New("TizenSans",
70                         45.0f,
71                         Dali::TextAbstraction::FontWeight::BOLD,
72                         Dali::TextAbstraction::FontWidth::SEMI_CONDENSED,
73                         Dali::TextAbstraction::FontSlant::OBLIQUE),
74     Text::Range::New(5u, 7u));
75   DALI_TEST_CHECK(isAddedFontSpan);
76
77   return spannableString;
78 }
79
80 Text::SpannableString CreateSpannableStringForUnderlineSpan()
81 {
82   Text::SpannableString spannableString = Text::SpannableString::New("Hello World");
83   DALI_TEST_CHECK(spannableString);
84
85   auto isAddedUnderlineSpan = spannableString.AttachSpan(
86     Text::UnderlineSpan::NewDashed(Color::GREEN, 5.0f, 2.0f, 3.0f),
87     Text::Range::New(5u, 7u));
88   DALI_TEST_CHECK(isAddedUnderlineSpan);
89
90   return spannableString;
91 }
92
93 void CheckColorIndices(const Text::ColorIndex* const colorIndicesBuffer,
94                        uint32_t                      numberOfIndices,
95                        std::vector<uint32_t>         indicesToCheck,
96                        std::vector<uint32_t>         expectedValues)
97 {
98   DALI_TEST_CHECK(colorIndicesBuffer);
99
100   for(uint32_t i = 0u; i < numberOfIndices; i++)
101   {
102     DALI_TEST_EQUALS(colorIndicesBuffer[indicesToCheck[i]], expectedValues[i], TEST_LOCATION);
103   }
104 }
105
106 int UtcDaliToolkitTextLabelSetSpannedText(void)
107 {
108   ToolkitTestApplication application;
109   tet_infoline(" UtcDaliToolkitTextLabelSetSpannedText");
110
111   TextLabel textLabel = TextLabel::New();
112   DALI_TEST_CHECK(textLabel);
113   application.GetScene().Add(textLabel);
114
115   Text::SpannableString spannableString = CreateSpannableStringForForegroundColorSpan();
116
117   Text::SetSpannedText(textLabel, spannableString);
118
119   application.SendNotification();
120   application.Render();
121
122   Toolkit::Internal::TextLabel& labelImpl          = GetImpl(textLabel);
123   const Text::ColorIndex* const colorIndicesBuffer = labelImpl.GetTextController()->GetTextModel()->GetColorIndices();
124
125   CheckColorIndices(colorIndicesBuffer, 4u, {0u, 5u, 7u, 10u}, {0u, 1u, 1u, 0u});
126
127   END_TEST;
128 }
129
130 int UtcDaliToolkitTextEditorSetSpannedText(void)
131 {
132   ToolkitTestApplication application;
133   tet_infoline(" UtcDaliToolkitTextEditorSetSpannedText");
134
135   TextEditor textEditor = TextEditor::New();
136   DALI_TEST_CHECK(textEditor);
137   application.GetScene().Add(textEditor);
138
139   Text::SpannableString spannableString = CreateSpannableStringForForegroundColorSpan();
140
141   Text::SetSpannedText(textEditor, spannableString);
142
143   application.SendNotification();
144   application.Render();
145
146   Toolkit::Internal::TextEditor& labelImpl          = GetImpl(textEditor);
147   const Text::ColorIndex* const  colorIndicesBuffer = labelImpl.GetTextController()->GetTextModel()->GetColorIndices();
148
149   CheckColorIndices(colorIndicesBuffer, 4u, {0u, 5u, 7u, 10u}, {0u, 1u, 1u, 0u});
150
151   END_TEST;
152 }
153
154 int UtcDaliToolkitTextFieldSetSpannedText(void)
155 {
156   ToolkitTestApplication application;
157   tet_infoline(" UtcDaliToolkitTextFieldSetSpannedText");
158
159   TextField textField = TextField::New();
160   DALI_TEST_CHECK(textField);
161   application.GetScene().Add(textField);
162
163   Text::SpannableString spannableString = CreateSpannableStringForForegroundColorSpan();
164
165   Text::SetSpannedText(textField, spannableString);
166
167   application.SendNotification();
168   application.Render();
169
170   Toolkit::Internal::TextField& labelImpl          = GetImpl(textField);
171   const Text::ColorIndex* const colorIndicesBuffer = labelImpl.GetTextController()->GetTextModel()->GetColorIndices();
172
173   CheckColorIndices(colorIndicesBuffer, 4u, {0u, 5u, 7u, 10u}, {0u, 1u, 1u, 0u});
174
175   END_TEST;
176 }
177
178 int UtcDaliToolkitTextLabelSetSpannedText_FontSpan(void)
179 {
180   ToolkitTestApplication application;
181   tet_infoline(" UtcDaliToolkitTextLabelSetSpannedText_FontSpan ");
182
183   // Load some fonts to get the same metrics on different platforms.
184   TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
185   fontClient.SetDpi(96u, 96u);
186
187   char*             pathNamePtr = get_current_dir_name();
188   const std::string pathName(pathNamePtr);
189   free(pathNamePtr);
190
191   fontClient.GetFontId(pathName + DEFAULT_FONT_DIR + "/tizen/TizenSansRegular.ttf");
192
193   TextLabel textLabel = TextLabel::New();
194   DALI_TEST_CHECK(textLabel);
195   application.GetScene().Add(textLabel);
196
197   Text::SpannableString spannableString = CreateSpannableStringForFontSpan();
198
199   Text::SetSpannedText(textLabel, spannableString);
200
201   application.SendNotification();
202   application.Render();
203
204   Toolkit::Internal::TextLabel&               labelImpl     = GetImpl(textLabel);
205   const Vector<Dali::Toolkit::Text::FontRun>& validFontRuns = labelImpl.GetTextController()->GetTextModel()->GetFontRuns();
206
207   DALI_TEST_EQUALS(validFontRuns.Count(), 3u, TEST_LOCATION);
208
209   DALI_TEST_EQUALS(validFontRuns[0].fontId, validFontRuns[2].fontId, TEST_LOCATION);
210   DALI_TEST_NOT_EQUALS(validFontRuns[0].fontId, validFontRuns[1].fontId, Math::MACHINE_EPSILON_100, TEST_LOCATION);
211
212   DALI_TEST_EQUALS(validFontRuns[1].characterRun.characterIndex, 5u, TEST_LOCATION);
213   DALI_TEST_EQUALS(validFontRuns[1].characterRun.GetEndCharacterIndex(), 7u, TEST_LOCATION);
214   DALI_TEST_EQUALS(validFontRuns[1].isItalicRequired, true, TEST_LOCATION);
215   DALI_TEST_EQUALS(validFontRuns[1].isBoldRequired, true, TEST_LOCATION);
216
217   TextAbstraction::FontDescription spanFontDescription;
218
219   float expectedPointSize = 45.0f * PIXEL_FORMAT_64_FACTOR;
220   float fontPointSize     = fontClient.GetPointSize(validFontRuns[1].fontId);
221   DALI_TEST_EQUALS(fontPointSize, expectedPointSize, TEST_LOCATION);
222
223   const Vector<Dali::Toolkit::Text::FontDescriptionRun>& validFontDescriptionRuns = labelImpl.GetTextController()->GetTextModel()->GetFontDescriptionRuns();
224   DALI_TEST_EQUALS(validFontDescriptionRuns.Count(), 1u, TEST_LOCATION);
225   std::string familyName = validFontDescriptionRuns[0].familyName;
226
227   DALI_TEST_EQUALS(familyName, "TizenSans", TEST_LOCATION);
228   DALI_TEST_EQUALS(validFontDescriptionRuns[0].size, expectedPointSize, TEST_LOCATION);
229   DALI_TEST_EQUALS(validFontDescriptionRuns[0].weight, Dali::TextAbstraction::FontWeight::BOLD, TEST_LOCATION);
230   DALI_TEST_EQUALS(validFontDescriptionRuns[0].width, Dali::TextAbstraction::FontWidth::SEMI_CONDENSED, TEST_LOCATION);
231   DALI_TEST_EQUALS(validFontDescriptionRuns[0].slant, Dali::TextAbstraction::FontSlant::OBLIQUE, TEST_LOCATION);
232
233   END_TEST;
234 }
235
236 int UtcDaliTextModelIsSpannedTextPlaced(void)
237
238 {
239   tet_infoline(" UtcDaliTextModelIsSpannedTextPlaced");
240
241   ToolkitTestApplication application;
242
243   // Create spanned-text and set it
244   Text::SpannableString spannedText = Text::SpannableString::New("Hello مرحبا");
245   DALI_TEST_CHECK(spannedText);
246
247   // Creates a text controller.
248   Dali::Toolkit::Text::ControllerPtr         controllerTextEditor = Dali::Toolkit::Text::Controller::New();
249   const Dali::Toolkit::Text::ModelInterface* modelEditor          = controllerTextEditor->GetTextModel();
250
251   // Tests the rendering controller has been created.
252   Dali::Toolkit::Text::TypesetterPtr typesetterEditor = Dali::Toolkit::Text::Typesetter::New(controllerTextEditor->GetTextModel());
253   DALI_TEST_CHECK(typesetterEditor);
254
255   // Tests the view model has been created.
256   Dali::Toolkit::Text::ViewModel* viewModelEditor = typesetterEditor->GetViewModel();
257   DALI_TEST_CHECK(viewModelEditor);
258
259   // Configures the text controller similarly to the text-editor.
260   Dali::Toolkit::Text::ConfigureTextEditor(controllerTextEditor);
261
262   DALI_TEST_EQUALS(false, modelEditor->IsSpannedTextPlaced(), TEST_LOCATION);
263   DALI_TEST_EQUALS(false, viewModelEditor->IsSpannedTextPlaced(), TEST_LOCATION);
264
265   controllerTextEditor->SetSpannedText(spannedText);
266
267   DALI_TEST_EQUALS(true, modelEditor->IsSpannedTextPlaced(), TEST_LOCATION);
268   DALI_TEST_EQUALS(true, viewModelEditor->IsSpannedTextPlaced(), TEST_LOCATION);
269
270   // Creates a text controller.
271   Dali::Toolkit::Text::ControllerPtr         controllerTextLabel = Dali::Toolkit::Text::Controller::New();
272   const Dali::Toolkit::Text::ModelInterface* modelLabel          = controllerTextLabel->GetTextModel();
273
274   // Tests the rendering controller has been created.
275   Dali::Toolkit::Text::TypesetterPtr typesetterLabel = Dali::Toolkit::Text::Typesetter::New(controllerTextLabel->GetTextModel());
276   DALI_TEST_CHECK(typesetterLabel);
277
278   // Tests the view model has been created.
279   Dali::Toolkit::Text::ViewModel* viewModelLabel = typesetterLabel->GetViewModel();
280   DALI_TEST_CHECK(viewModelLabel);
281
282   // Configures the text controller similarly to the text-label.
283   Dali::Toolkit::Text::ConfigureTextLabel(controllerTextLabel);
284
285   DALI_TEST_EQUALS(false, modelLabel->IsSpannedTextPlaced(), TEST_LOCATION);
286   DALI_TEST_EQUALS(false, viewModelLabel->IsSpannedTextPlaced(), TEST_LOCATION);
287
288   controllerTextLabel->SetSpannedText(spannedText);
289
290   DALI_TEST_EQUALS(true, modelLabel->IsSpannedTextPlaced(), TEST_LOCATION);
291   DALI_TEST_EQUALS(true, viewModelLabel->IsSpannedTextPlaced(), TEST_LOCATION);
292
293   // Creates a text controller.
294   Dali::Toolkit::Text::ControllerPtr         controllerTextField = Dali::Toolkit::Text::Controller::New();
295   const Dali::Toolkit::Text::ModelInterface* modelField          = controllerTextField->GetTextModel();
296
297   // Tests the rendering controller has been created.
298   Dali::Toolkit::Text::TypesetterPtr typesetterField = Dali::Toolkit::Text::Typesetter::New(controllerTextField->GetTextModel());
299   DALI_TEST_CHECK(typesetterField);
300
301   // Tests the view model has been created.
302   Dali::Toolkit::Text::ViewModel* viewModelField = typesetterField->GetViewModel();
303   DALI_TEST_CHECK(viewModelField);
304
305   // Configures the text controller similarly to the text-field.
306   Dali::Toolkit::Text::ConfigureTextField(controllerTextField);
307
308   DALI_TEST_EQUALS(false, modelField->IsSpannedTextPlaced(), TEST_LOCATION);
309   DALI_TEST_EQUALS(false, viewModelField->IsSpannedTextPlaced(), TEST_LOCATION);
310
311   controllerTextField->SetSpannedText(spannedText);
312
313   DALI_TEST_EQUALS(true, modelField->IsSpannedTextPlaced(), TEST_LOCATION);
314   DALI_TEST_EQUALS(true, viewModelField->IsSpannedTextPlaced(), TEST_LOCATION);
315
316   tet_result(TET_PASS);
317
318   END_TEST;
319 }
320
321 int UtcDaliToolkitTextLabelSetSpannedText_UnderlineSpan(void)
322 {
323   ToolkitTestApplication application;
324   tet_infoline(" UtcDaliToolkitTextLabelSetSpannedText_UnderlineSpan ");
325
326   Dali::Toolkit::Text::UnderlineStyleProperties expectedProperties = {
327     Text::Underline::DASHED,
328     Color::GREEN,
329     5u,
330     2u,
331     3u,
332     true,
333     true,
334     true,
335     true,
336     true};
337
338   TextLabel textLabel = TextLabel::New();
339   DALI_TEST_CHECK(textLabel);
340   application.GetScene().Add(textLabel);
341
342   Text::SpannableString spannableString = CreateSpannableStringForUnderlineSpan();
343
344   Text::SetSpannedText(textLabel, spannableString);
345
346   application.SendNotification();
347   application.Render();
348
349   Toolkit::Internal::TextLabel& labelImpl             = GetImpl(textLabel);
350   const Text::Length            numberOfUnderlineRuns = labelImpl.GetTextController()->GetTextModel()->GetNumberOfUnderlineRuns();
351
352   DALI_TEST_EQUALS(numberOfUnderlineRuns, 1u, TEST_LOCATION);
353
354   Vector<Dali::Toolkit::Text::UnderlinedGlyphRun> underlineRuns;
355
356   underlineRuns.Resize(numberOfUnderlineRuns);
357
358   labelImpl.GetTextController()->GetTextModel()->GetUnderlineRuns(underlineRuns.Begin(), 0u, numberOfUnderlineRuns);
359
360   DALI_TEST_EQUALS(underlineRuns[0].glyphRun.glyphIndex, 5u, TEST_LOCATION);
361   DALI_TEST_EQUALS(underlineRuns[0].glyphRun.numberOfGlyphs, 3u, TEST_LOCATION);
362   DALI_TEST_CHECK(underlineRuns[0].properties == expectedProperties);
363
364   END_TEST;
365 }