2 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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 #include <dali-toolkit/devel-api/text/spans/character-spacing-span.h>
41 #include <dali-toolkit/devel-api/text/spans/bold-span.h>
42 #include <dali-toolkit/devel-api/text/spans/italic-span.h>
43 #include <dali-toolkit/devel-api/text/spans/background-color-span.h>
46 using namespace Toolkit;
50 const std::string DEFAULT_FONT_DIR("/resources/fonts");
51 const float PIXEL_FORMAT_64_FACTOR = 64.f; ///< 64.f is used to convert from point size to 26.6 pixel format.
54 Text::SpannableString CreateSpannableStringForForegroundColorSpan()
56 Text::SpannableString spannableString = Text::SpannableString::New("Hello مرحبا");
57 DALI_TEST_CHECK(spannableString);
59 auto isAddedGreen = spannableString.AttachSpan(
60 Text::ForegroundColorSpan::New(Color::GREEN),
61 Text::Range::New(5u, 7u));
62 DALI_TEST_CHECK(isAddedGreen);
64 return spannableString;
67 Text::SpannableString CreateSpannableStringForFontSpan()
69 Text::SpannableString spannableString = Text::SpannableString::New("Hello World");
70 DALI_TEST_CHECK(spannableString);
72 auto isAddedFontSpan = spannableString.AttachSpan(
73 Text::FontSpan::New("TizenSans",
75 Dali::TextAbstraction::FontWeight::BOLD,
76 Dali::TextAbstraction::FontWidth::SEMI_CONDENSED,
77 Dali::TextAbstraction::FontSlant::OBLIQUE),
78 Text::Range::New(5u, 7u));
79 DALI_TEST_CHECK(isAddedFontSpan);
81 return spannableString;
84 Text::SpannableString CreateSpannableStringForUnderlineSpan()
86 Text::SpannableString spannableString = Text::SpannableString::New("Hello World");
87 DALI_TEST_CHECK(spannableString);
89 auto isAddedUnderlineSpan = spannableString.AttachSpan(
90 Text::UnderlineSpan::NewDashed(Color::GREEN, 5.0f, 2.0f, 3.0f),
91 Text::Range::New(5u, 7u));
92 DALI_TEST_CHECK(isAddedUnderlineSpan);
94 return spannableString;
97 Text::SpannableString CreateSpannableStringForCharacterSpacing()
99 Text::SpannableString spannableString = Text::SpannableString::New("Hello World");
100 DALI_TEST_CHECK(spannableString);
102 auto isCharacterSpacingSpan = spannableString.AttachSpan(
103 Text::CharacterSpacingSpan::New(5.2f),
104 Text::Range::New(5u, 7u));
105 DALI_TEST_CHECK(isCharacterSpacingSpan);
107 return spannableString;
110 Text::SpannableString CreateSpannableStringForBoldSpan()
112 Text::SpannableString spannableString = Text::SpannableString::New("Hello");
113 DALI_TEST_CHECK(spannableString);
115 auto boldspan = Dali::Toolkit::Text::BoldSpan::New();
116 auto isBoldSpan = spannableString.AttachSpan(boldspan,Dali::Toolkit::Text::Range::New(0u, 3u));
117 DALI_TEST_CHECK(isBoldSpan);
119 return spannableString;
123 Text::SpannableString CreateSpannableStringForItalicSpan()
125 Text::SpannableString spannableString = Text::SpannableString::New("Hello");
126 DALI_TEST_CHECK(spannableString);
128 auto isAddedItalic = spannableString.AttachSpan(
129 Text::ItalicSpan::New(),
130 Text::Range::New(0u, 3u));
131 DALI_TEST_CHECK(isAddedItalic);
133 return spannableString;
136 Text::SpannableString CreateSpannableStringForBackgroundColorSpan()
139 Text::SpannableString spannableString = Text::SpannableString::New("Hello مرحبا");
141 DALI_TEST_CHECK(spannableString);
143 auto isAddedGreen = spannableString.AttachSpan(
144 Text::BackgroundColorSpan::New(Color::GREEN),
145 Text::Range::New(5u, 7u));
146 DALI_TEST_CHECK(isAddedGreen);
148 return spannableString;
151 void CheckColorIndices(const Text::ColorIndex* const colorIndicesBuffer,
152 uint32_t numberOfIndices,
153 std::vector<uint32_t> indicesToCheck,
154 std::vector<uint32_t> expectedValues)
156 DALI_TEST_CHECK(colorIndicesBuffer);
158 for(uint32_t i = 0u; i < numberOfIndices; i++)
160 DALI_TEST_EQUALS(colorIndicesBuffer[indicesToCheck[i]], expectedValues[i], TEST_LOCATION);
164 int UtcDaliToolkitTextLabelSetSpannedText(void)
166 ToolkitTestApplication application;
167 tet_infoline(" UtcDaliToolkitTextLabelSetSpannedText");
169 TextLabel textLabel = TextLabel::New();
170 DALI_TEST_CHECK(textLabel);
171 application.GetScene().Add(textLabel);
173 Text::SpannableString spannableString = CreateSpannableStringForForegroundColorSpan();
175 Text::SetSpannedText(textLabel, spannableString);
177 application.SendNotification();
178 application.Render();
180 Toolkit::Internal::TextLabel& labelImpl = GetImpl(textLabel);
181 const Text::ColorIndex* const colorIndicesBuffer = labelImpl.GetTextController()->GetTextModel()->GetColorIndices();
183 CheckColorIndices(colorIndicesBuffer, 4u, {0u, 5u, 7u, 10u}, {0u, 1u, 1u, 0u});
188 int UtcDaliToolkitTextEditorSetSpannedText(void)
190 ToolkitTestApplication application;
191 tet_infoline(" UtcDaliToolkitTextEditorSetSpannedText");
193 TextEditor textEditor = TextEditor::New();
194 DALI_TEST_CHECK(textEditor);
195 application.GetScene().Add(textEditor);
197 Text::SpannableString spannableString = CreateSpannableStringForForegroundColorSpan();
199 Text::SetSpannedText(textEditor, spannableString);
201 application.SendNotification();
202 application.Render();
204 Toolkit::Internal::TextEditor& labelImpl = GetImpl(textEditor);
205 const Text::ColorIndex* const colorIndicesBuffer = labelImpl.GetTextController()->GetTextModel()->GetColorIndices();
207 CheckColorIndices(colorIndicesBuffer, 4u, {0u, 5u, 7u, 10u}, {0u, 1u, 1u, 0u});
212 int UtcDaliToolkitTextFieldSetSpannedText(void)
214 ToolkitTestApplication application;
215 tet_infoline(" UtcDaliToolkitTextFieldSetSpannedText");
217 TextField textField = TextField::New();
218 DALI_TEST_CHECK(textField);
219 application.GetScene().Add(textField);
221 Text::SpannableString spannableString = CreateSpannableStringForForegroundColorSpan();
223 Text::SetSpannedText(textField, spannableString);
225 application.SendNotification();
226 application.Render();
228 Toolkit::Internal::TextField& labelImpl = GetImpl(textField);
229 const Text::ColorIndex* const colorIndicesBuffer = labelImpl.GetTextController()->GetTextModel()->GetColorIndices();
231 CheckColorIndices(colorIndicesBuffer, 4u, {0u, 5u, 7u, 10u}, {0u, 1u, 1u, 0u});
236 int UtcDaliToolkitTextLabelSetSpannedText_FontSpan(void)
238 ToolkitTestApplication application;
239 tet_infoline(" UtcDaliToolkitTextLabelSetSpannedText_FontSpan ");
241 // Load some fonts to get the same metrics on different platforms.
242 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
243 fontClient.SetDpi(96u, 96u);
245 char* pathNamePtr = get_current_dir_name();
246 const std::string pathName(pathNamePtr);
249 fontClient.GetFontId(pathName + DEFAULT_FONT_DIR + "/tizen/TizenSansRegular.ttf");
251 TextLabel textLabel = TextLabel::New();
252 DALI_TEST_CHECK(textLabel);
253 application.GetScene().Add(textLabel);
255 Text::SpannableString spannableString = CreateSpannableStringForFontSpan();
257 Text::SetSpannedText(textLabel, spannableString);
259 application.SendNotification();
260 application.Render();
262 Toolkit::Internal::TextLabel& labelImpl = GetImpl(textLabel);
263 const Vector<Dali::Toolkit::Text::FontRun>& validFontRuns = labelImpl.GetTextController()->GetTextModel()->GetFontRuns();
265 DALI_TEST_EQUALS(validFontRuns.Count(), 3u, TEST_LOCATION);
267 DALI_TEST_EQUALS(validFontRuns[0].fontId, validFontRuns[2].fontId, TEST_LOCATION);
268 DALI_TEST_NOT_EQUALS(validFontRuns[0].fontId, validFontRuns[1].fontId, Math::MACHINE_EPSILON_100, TEST_LOCATION);
270 DALI_TEST_EQUALS(validFontRuns[1].characterRun.characterIndex, 5u, TEST_LOCATION);
271 DALI_TEST_EQUALS(validFontRuns[1].characterRun.GetEndCharacterIndex(), 7u, TEST_LOCATION);
272 DALI_TEST_EQUALS(validFontRuns[1].isItalicRequired, true, TEST_LOCATION);
273 DALI_TEST_EQUALS(validFontRuns[1].isBoldRequired, true, TEST_LOCATION);
275 TextAbstraction::FontDescription spanFontDescription;
277 float expectedPointSize = 45.0f * PIXEL_FORMAT_64_FACTOR;
278 float fontPointSize = fontClient.GetPointSize(validFontRuns[1].fontId);
279 DALI_TEST_EQUALS(fontPointSize, expectedPointSize, TEST_LOCATION);
281 const Vector<Dali::Toolkit::Text::FontDescriptionRun>& validFontDescriptionRuns = labelImpl.GetTextController()->GetTextModel()->GetFontDescriptionRuns();
282 DALI_TEST_EQUALS(validFontDescriptionRuns.Count(), 1u, TEST_LOCATION);
283 std::string familyName = validFontDescriptionRuns[0].familyName;
285 DALI_TEST_EQUALS(familyName, "TizenSans", TEST_LOCATION);
286 DALI_TEST_EQUALS(validFontDescriptionRuns[0].size, expectedPointSize, TEST_LOCATION);
287 DALI_TEST_EQUALS(validFontDescriptionRuns[0].weight, Dali::TextAbstraction::FontWeight::BOLD, TEST_LOCATION);
288 DALI_TEST_EQUALS(validFontDescriptionRuns[0].width, Dali::TextAbstraction::FontWidth::SEMI_CONDENSED, TEST_LOCATION);
289 DALI_TEST_EQUALS(validFontDescriptionRuns[0].slant, Dali::TextAbstraction::FontSlant::OBLIQUE, TEST_LOCATION);
294 int UtcDaliTextModelIsSpannedTextPlaced(void)
297 tet_infoline(" UtcDaliTextModelIsSpannedTextPlaced");
299 ToolkitTestApplication application;
301 // Create spanned-text and set it
302 Text::SpannableString spannedText = Text::SpannableString::New("Hello مرحبا");
303 DALI_TEST_CHECK(spannedText);
305 // Creates a text controller.
306 Dali::Toolkit::Text::ControllerPtr controllerTextEditor = Dali::Toolkit::Text::Controller::New();
307 const Dali::Toolkit::Text::ModelInterface* modelEditor = controllerTextEditor->GetTextModel();
309 // Tests the rendering controller has been created.
310 Dali::Toolkit::Text::TypesetterPtr typesetterEditor = Dali::Toolkit::Text::Typesetter::New(controllerTextEditor->GetTextModel());
311 DALI_TEST_CHECK(typesetterEditor);
313 // Tests the view model has been created.
314 Dali::Toolkit::Text::ViewModel* viewModelEditor = typesetterEditor->GetViewModel();
315 DALI_TEST_CHECK(viewModelEditor);
317 // Configures the text controller similarly to the text-editor.
318 Dali::Toolkit::Text::ConfigureTextEditor(controllerTextEditor);
320 DALI_TEST_EQUALS(false, modelEditor->IsSpannedTextPlaced(), TEST_LOCATION);
321 DALI_TEST_EQUALS(false, viewModelEditor->IsSpannedTextPlaced(), TEST_LOCATION);
323 controllerTextEditor->SetSpannedText(spannedText);
325 DALI_TEST_EQUALS(true, modelEditor->IsSpannedTextPlaced(), TEST_LOCATION);
326 DALI_TEST_EQUALS(true, viewModelEditor->IsSpannedTextPlaced(), TEST_LOCATION);
328 // Creates a text controller.
329 Dali::Toolkit::Text::ControllerPtr controllerTextLabel = Dali::Toolkit::Text::Controller::New();
330 const Dali::Toolkit::Text::ModelInterface* modelLabel = controllerTextLabel->GetTextModel();
332 // Tests the rendering controller has been created.
333 Dali::Toolkit::Text::TypesetterPtr typesetterLabel = Dali::Toolkit::Text::Typesetter::New(controllerTextLabel->GetTextModel());
334 DALI_TEST_CHECK(typesetterLabel);
336 // Tests the view model has been created.
337 Dali::Toolkit::Text::ViewModel* viewModelLabel = typesetterLabel->GetViewModel();
338 DALI_TEST_CHECK(viewModelLabel);
340 // Configures the text controller similarly to the text-label.
341 Dali::Toolkit::Text::ConfigureTextLabel(controllerTextLabel);
343 DALI_TEST_EQUALS(false, modelLabel->IsSpannedTextPlaced(), TEST_LOCATION);
344 DALI_TEST_EQUALS(false, viewModelLabel->IsSpannedTextPlaced(), TEST_LOCATION);
346 controllerTextLabel->SetSpannedText(spannedText);
348 DALI_TEST_EQUALS(true, modelLabel->IsSpannedTextPlaced(), TEST_LOCATION);
349 DALI_TEST_EQUALS(true, viewModelLabel->IsSpannedTextPlaced(), TEST_LOCATION);
351 // Creates a text controller.
352 Dali::Toolkit::Text::ControllerPtr controllerTextField = Dali::Toolkit::Text::Controller::New();
353 const Dali::Toolkit::Text::ModelInterface* modelField = controllerTextField->GetTextModel();
355 // Tests the rendering controller has been created.
356 Dali::Toolkit::Text::TypesetterPtr typesetterField = Dali::Toolkit::Text::Typesetter::New(controllerTextField->GetTextModel());
357 DALI_TEST_CHECK(typesetterField);
359 // Tests the view model has been created.
360 Dali::Toolkit::Text::ViewModel* viewModelField = typesetterField->GetViewModel();
361 DALI_TEST_CHECK(viewModelField);
363 // Configures the text controller similarly to the text-field.
364 Dali::Toolkit::Text::ConfigureTextField(controllerTextField);
366 DALI_TEST_EQUALS(false, modelField->IsSpannedTextPlaced(), TEST_LOCATION);
367 DALI_TEST_EQUALS(false, viewModelField->IsSpannedTextPlaced(), TEST_LOCATION);
369 controllerTextField->SetSpannedText(spannedText);
371 DALI_TEST_EQUALS(true, modelField->IsSpannedTextPlaced(), TEST_LOCATION);
372 DALI_TEST_EQUALS(true, viewModelField->IsSpannedTextPlaced(), TEST_LOCATION);
374 tet_result(TET_PASS);
379 int UtcDaliToolkitTextLabelSetSpannedText_UnderlineSpan(void)
381 ToolkitTestApplication application;
382 tet_infoline(" UtcDaliToolkitTextLabelSetSpannedText_UnderlineSpan ");
384 Dali::Toolkit::Text::UnderlineStyleProperties expectedProperties = {
385 Text::Underline::DASHED,
396 TextLabel textLabel = TextLabel::New();
397 DALI_TEST_CHECK(textLabel);
398 application.GetScene().Add(textLabel);
400 Text::SpannableString spannableString = CreateSpannableStringForUnderlineSpan();
402 Text::SetSpannedText(textLabel, spannableString);
404 application.SendNotification();
405 application.Render();
407 Toolkit::Internal::TextLabel& labelImpl = GetImpl(textLabel);
408 const Text::Length numberOfUnderlineRuns = labelImpl.GetTextController()->GetTextModel()->GetNumberOfUnderlineRuns();
410 DALI_TEST_EQUALS(numberOfUnderlineRuns, 1u, TEST_LOCATION);
412 Vector<Dali::Toolkit::Text::UnderlinedGlyphRun> underlineRuns;
414 underlineRuns.Resize(numberOfUnderlineRuns);
416 labelImpl.GetTextController()->GetTextModel()->GetUnderlineRuns(underlineRuns.Begin(), 0u, numberOfUnderlineRuns);
418 DALI_TEST_EQUALS(underlineRuns[0].glyphRun.glyphIndex, 5u, TEST_LOCATION);
419 DALI_TEST_EQUALS(underlineRuns[0].glyphRun.numberOfGlyphs, 3u, TEST_LOCATION);
420 DALI_TEST_CHECK(underlineRuns[0].properties == expectedProperties);
425 int UtcDaliToolkitTextLabelSetSpannedText_CharacterSpacingSpan(void)
427 ToolkitTestApplication application;
428 tet_infoline("UtcDaliToolkitTextLabelSetSpannedText_CharacterSpacingSpan");
430 TextLabel textLabel = TextLabel::New();
431 DALI_TEST_CHECK(textLabel);
432 application.GetScene().Add(textLabel);
434 Text::SpannableString spannableString = CreateSpannableStringForCharacterSpacing();
435 Text::SetSpannedText(textLabel, spannableString);
437 application.SendNotification();
438 application.Render();
440 Toolkit::Internal::TextLabel& labelImpl = GetImpl(textLabel);
441 const Vector<Dali::Toolkit::Text::CharacterSpacingGlyphRun> characterSpacing = labelImpl.GetTextController()->GetTextModel()->GetCharacterSpacingGlyphRuns();
442 DALI_TEST_EQUALS(1, characterSpacing.Count(), TEST_LOCATION);
446 int UtcDaliToolkitTextLabelSetSpannedText_BoldSpan(void)
448 ToolkitTestApplication application;
450 tet_infoline("UtcDaliToolkitTextLabelSetSpannedText_BoldSpan");
452 TextLabel textLabel = TextLabel::New();
454 DALI_TEST_CHECK(textLabel);
456 application.GetScene().Add(textLabel);
458 Text::SpannableString spannableString = CreateSpannableStringForBoldSpan();
459 Text::SetSpannedText(textLabel, spannableString);
461 application.SendNotification();
462 application.Render();
464 Toolkit::Internal::TextLabel& labelImpl = GetImpl(textLabel);
466 const Vector<Text::FontRun>& validFonts = labelImpl.GetTextController()->GetTextModel()->GetFontRuns();
468 DALI_TEST_EQUALS(validFonts.Count(), 2, TEST_LOCATION);
469 DALI_TEST_EQUALS(validFonts[0].characterRun.characterIndex,0, TEST_LOCATION);
470 DALI_TEST_EQUALS(validFonts[0].characterRun.GetEndCharacterIndex(),3, TEST_LOCATION);
471 DALI_TEST_EQUALS(validFonts[0].isBoldRequired, true, TEST_LOCATION);
475 int UtcDaliToolkitTextLabelSetSpannedText_ItalicSpan(void)
477 ToolkitTestApplication application;
478 tet_infoline("UtcDaliToolkitTextLabelSetSpannedText_ItalicSpan");
480 TextLabel textLabel = TextLabel::New();
481 DALI_TEST_CHECK(textLabel);
483 application.GetScene().Add(textLabel);
485 Text::SpannableString spannableString = CreateSpannableStringForItalicSpan();
487 Text::SetSpannedText(textLabel, spannableString);
488 application.SendNotification();
489 application.Render();
491 Toolkit::Internal::TextLabel& labelImpl = GetImpl(textLabel);
492 const Vector<Text::FontRun>& validFontsItalic = labelImpl.GetTextController()->GetTextModel()->GetFontRuns();
494 DALI_TEST_EQUALS(validFontsItalic.Count(), 2, TEST_LOCATION);
495 DALI_TEST_EQUALS(validFontsItalic[0].characterRun.characterIndex, 0, TEST_LOCATION);
496 DALI_TEST_EQUALS(validFontsItalic[0].characterRun.GetEndCharacterIndex(), 3, TEST_LOCATION);
497 DALI_TEST_EQUALS(validFontsItalic[0].isItalicRequired, true, TEST_LOCATION);
501 int UtcDaliToolkitTextLabelSetSpannedText_BackgroundColorSpan(void)
504 ToolkitTestApplication application;
505 tet_infoline("UtcDaliToolkitTextLabelSetSpannedText_BackgroundColorSpan");
507 TextLabel textLabel = TextLabel::New();
508 DALI_TEST_CHECK(textLabel);
510 application.GetScene().Add(textLabel);
512 Text::SpannableString spannableString = CreateSpannableStringForBackgroundColorSpan();
513 Text::SetSpannedText(textLabel, spannableString);
515 application.SendNotification();
516 application.Render();
518 Toolkit::Internal::TextLabel& labelImpl = GetImpl(textLabel);
519 const Text::ColorIndex* const backgroundColorIndicesBuffer = labelImpl.GetTextController()->GetTextModel()->GetBackgroundColorIndices();
521 CheckColorIndices(backgroundColorIndicesBuffer, 4u, {0u, 5u, 7u, 10u}, {0u, 1u, 1u, 0u});