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>
45 using namespace Toolkit;
49 const std::string DEFAULT_FONT_DIR("/resources/fonts");
50 const float PIXEL_FORMAT_64_FACTOR = 64.f; ///< 64.f is used to convert from point size to 26.6 pixel format.
53 Text::SpannableString CreateSpannableStringForForegroundColorSpan()
55 Text::SpannableString spannableString = Text::SpannableString::New("Hello مرحبا");
56 DALI_TEST_CHECK(spannableString);
58 auto isAddedGreen = spannableString.AttachSpan(
59 Text::ForegroundColorSpan::New(Color::GREEN),
60 Text::Range::New(5u, 7u));
61 DALI_TEST_CHECK(isAddedGreen);
63 return spannableString;
66 Text::SpannableString CreateSpannableStringForFontSpan()
68 Text::SpannableString spannableString = Text::SpannableString::New("Hello World");
69 DALI_TEST_CHECK(spannableString);
71 auto isAddedFontSpan = spannableString.AttachSpan(
72 Text::FontSpan::New("TizenSans",
74 Dali::TextAbstraction::FontWeight::BOLD,
75 Dali::TextAbstraction::FontWidth::SEMI_CONDENSED,
76 Dali::TextAbstraction::FontSlant::OBLIQUE),
77 Text::Range::New(5u, 7u));
78 DALI_TEST_CHECK(isAddedFontSpan);
80 return spannableString;
83 Text::SpannableString CreateSpannableStringForUnderlineSpan()
85 Text::SpannableString spannableString = Text::SpannableString::New("Hello World");
86 DALI_TEST_CHECK(spannableString);
88 auto isAddedUnderlineSpan = spannableString.AttachSpan(
89 Text::UnderlineSpan::NewDashed(Color::GREEN, 5.0f, 2.0f, 3.0f),
90 Text::Range::New(5u, 7u));
91 DALI_TEST_CHECK(isAddedUnderlineSpan);
93 return spannableString;
96 Text::SpannableString CreateSpannableStringForCharacterSpacing()
98 Text::SpannableString spannableString = Text::SpannableString::New("Hello World");
99 DALI_TEST_CHECK(spannableString);
101 auto isCharacterSpacingSpan = spannableString.AttachSpan(
102 Text::CharacterSpacingSpan::New(5.2f),
103 Text::Range::New(5u, 7u));
104 DALI_TEST_CHECK(isCharacterSpacingSpan);
106 return spannableString;
109 Text::SpannableString CreateSpannableStringForBoldSpan()
111 Text::SpannableString spannableString = Text::SpannableString::New("Hello");
112 DALI_TEST_CHECK(spannableString);
114 auto boldspan = Dali::Toolkit::Text::BoldSpan::New();
115 auto isBoldSpan = spannableString.AttachSpan(boldspan,Dali::Toolkit::Text::Range::New(0u, 3u));
116 DALI_TEST_CHECK(isBoldSpan);
118 return spannableString;
122 Text::SpannableString CreateSpannableStringForItalicSpan()
124 Text::SpannableString spannableString = Text::SpannableString::New("Hello");
125 DALI_TEST_CHECK(spannableString);
127 auto isAddedItalic = spannableString.AttachSpan(
128 Text::ItalicSpan::New(),
129 Text::Range::New(0u, 3u));
130 DALI_TEST_CHECK(isAddedItalic);
132 return spannableString;
135 void CheckColorIndices(const Text::ColorIndex* const colorIndicesBuffer,
136 uint32_t numberOfIndices,
137 std::vector<uint32_t> indicesToCheck,
138 std::vector<uint32_t> expectedValues)
140 DALI_TEST_CHECK(colorIndicesBuffer);
142 for(uint32_t i = 0u; i < numberOfIndices; i++)
144 DALI_TEST_EQUALS(colorIndicesBuffer[indicesToCheck[i]], expectedValues[i], TEST_LOCATION);
148 int UtcDaliToolkitTextLabelSetSpannedText(void)
150 ToolkitTestApplication application;
151 tet_infoline(" UtcDaliToolkitTextLabelSetSpannedText");
153 TextLabel textLabel = TextLabel::New();
154 DALI_TEST_CHECK(textLabel);
155 application.GetScene().Add(textLabel);
157 Text::SpannableString spannableString = CreateSpannableStringForForegroundColorSpan();
159 Text::SetSpannedText(textLabel, spannableString);
161 application.SendNotification();
162 application.Render();
164 Toolkit::Internal::TextLabel& labelImpl = GetImpl(textLabel);
165 const Text::ColorIndex* const colorIndicesBuffer = labelImpl.GetTextController()->GetTextModel()->GetColorIndices();
167 CheckColorIndices(colorIndicesBuffer, 4u, {0u, 5u, 7u, 10u}, {0u, 1u, 1u, 0u});
172 int UtcDaliToolkitTextEditorSetSpannedText(void)
174 ToolkitTestApplication application;
175 tet_infoline(" UtcDaliToolkitTextEditorSetSpannedText");
177 TextEditor textEditor = TextEditor::New();
178 DALI_TEST_CHECK(textEditor);
179 application.GetScene().Add(textEditor);
181 Text::SpannableString spannableString = CreateSpannableStringForForegroundColorSpan();
183 Text::SetSpannedText(textEditor, spannableString);
185 application.SendNotification();
186 application.Render();
188 Toolkit::Internal::TextEditor& labelImpl = GetImpl(textEditor);
189 const Text::ColorIndex* const colorIndicesBuffer = labelImpl.GetTextController()->GetTextModel()->GetColorIndices();
191 CheckColorIndices(colorIndicesBuffer, 4u, {0u, 5u, 7u, 10u}, {0u, 1u, 1u, 0u});
196 int UtcDaliToolkitTextFieldSetSpannedText(void)
198 ToolkitTestApplication application;
199 tet_infoline(" UtcDaliToolkitTextFieldSetSpannedText");
201 TextField textField = TextField::New();
202 DALI_TEST_CHECK(textField);
203 application.GetScene().Add(textField);
205 Text::SpannableString spannableString = CreateSpannableStringForForegroundColorSpan();
207 Text::SetSpannedText(textField, spannableString);
209 application.SendNotification();
210 application.Render();
212 Toolkit::Internal::TextField& labelImpl = GetImpl(textField);
213 const Text::ColorIndex* const colorIndicesBuffer = labelImpl.GetTextController()->GetTextModel()->GetColorIndices();
215 CheckColorIndices(colorIndicesBuffer, 4u, {0u, 5u, 7u, 10u}, {0u, 1u, 1u, 0u});
220 int UtcDaliToolkitTextLabelSetSpannedText_FontSpan(void)
222 ToolkitTestApplication application;
223 tet_infoline(" UtcDaliToolkitTextLabelSetSpannedText_FontSpan ");
225 // Load some fonts to get the same metrics on different platforms.
226 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
227 fontClient.SetDpi(96u, 96u);
229 char* pathNamePtr = get_current_dir_name();
230 const std::string pathName(pathNamePtr);
233 fontClient.GetFontId(pathName + DEFAULT_FONT_DIR + "/tizen/TizenSansRegular.ttf");
235 TextLabel textLabel = TextLabel::New();
236 DALI_TEST_CHECK(textLabel);
237 application.GetScene().Add(textLabel);
239 Text::SpannableString spannableString = CreateSpannableStringForFontSpan();
241 Text::SetSpannedText(textLabel, spannableString);
243 application.SendNotification();
244 application.Render();
246 Toolkit::Internal::TextLabel& labelImpl = GetImpl(textLabel);
247 const Vector<Dali::Toolkit::Text::FontRun>& validFontRuns = labelImpl.GetTextController()->GetTextModel()->GetFontRuns();
249 DALI_TEST_EQUALS(validFontRuns.Count(), 3u, TEST_LOCATION);
251 DALI_TEST_EQUALS(validFontRuns[0].fontId, validFontRuns[2].fontId, TEST_LOCATION);
252 DALI_TEST_NOT_EQUALS(validFontRuns[0].fontId, validFontRuns[1].fontId, Math::MACHINE_EPSILON_100, TEST_LOCATION);
254 DALI_TEST_EQUALS(validFontRuns[1].characterRun.characterIndex, 5u, TEST_LOCATION);
255 DALI_TEST_EQUALS(validFontRuns[1].characterRun.GetEndCharacterIndex(), 7u, TEST_LOCATION);
256 DALI_TEST_EQUALS(validFontRuns[1].isItalicRequired, true, TEST_LOCATION);
257 DALI_TEST_EQUALS(validFontRuns[1].isBoldRequired, true, TEST_LOCATION);
259 TextAbstraction::FontDescription spanFontDescription;
261 float expectedPointSize = 45.0f * PIXEL_FORMAT_64_FACTOR;
262 float fontPointSize = fontClient.GetPointSize(validFontRuns[1].fontId);
263 DALI_TEST_EQUALS(fontPointSize, expectedPointSize, TEST_LOCATION);
265 const Vector<Dali::Toolkit::Text::FontDescriptionRun>& validFontDescriptionRuns = labelImpl.GetTextController()->GetTextModel()->GetFontDescriptionRuns();
266 DALI_TEST_EQUALS(validFontDescriptionRuns.Count(), 1u, TEST_LOCATION);
267 std::string familyName = validFontDescriptionRuns[0].familyName;
269 DALI_TEST_EQUALS(familyName, "TizenSans", TEST_LOCATION);
270 DALI_TEST_EQUALS(validFontDescriptionRuns[0].size, expectedPointSize, TEST_LOCATION);
271 DALI_TEST_EQUALS(validFontDescriptionRuns[0].weight, Dali::TextAbstraction::FontWeight::BOLD, TEST_LOCATION);
272 DALI_TEST_EQUALS(validFontDescriptionRuns[0].width, Dali::TextAbstraction::FontWidth::SEMI_CONDENSED, TEST_LOCATION);
273 DALI_TEST_EQUALS(validFontDescriptionRuns[0].slant, Dali::TextAbstraction::FontSlant::OBLIQUE, TEST_LOCATION);
278 int UtcDaliTextModelIsSpannedTextPlaced(void)
281 tet_infoline(" UtcDaliTextModelIsSpannedTextPlaced");
283 ToolkitTestApplication application;
285 // Create spanned-text and set it
286 Text::SpannableString spannedText = Text::SpannableString::New("Hello مرحبا");
287 DALI_TEST_CHECK(spannedText);
289 // Creates a text controller.
290 Dali::Toolkit::Text::ControllerPtr controllerTextEditor = Dali::Toolkit::Text::Controller::New();
291 const Dali::Toolkit::Text::ModelInterface* modelEditor = controllerTextEditor->GetTextModel();
293 // Tests the rendering controller has been created.
294 Dali::Toolkit::Text::TypesetterPtr typesetterEditor = Dali::Toolkit::Text::Typesetter::New(controllerTextEditor->GetTextModel());
295 DALI_TEST_CHECK(typesetterEditor);
297 // Tests the view model has been created.
298 Dali::Toolkit::Text::ViewModel* viewModelEditor = typesetterEditor->GetViewModel();
299 DALI_TEST_CHECK(viewModelEditor);
301 // Configures the text controller similarly to the text-editor.
302 Dali::Toolkit::Text::ConfigureTextEditor(controllerTextEditor);
304 DALI_TEST_EQUALS(false, modelEditor->IsSpannedTextPlaced(), TEST_LOCATION);
305 DALI_TEST_EQUALS(false, viewModelEditor->IsSpannedTextPlaced(), TEST_LOCATION);
307 controllerTextEditor->SetSpannedText(spannedText);
309 DALI_TEST_EQUALS(true, modelEditor->IsSpannedTextPlaced(), TEST_LOCATION);
310 DALI_TEST_EQUALS(true, viewModelEditor->IsSpannedTextPlaced(), TEST_LOCATION);
312 // Creates a text controller.
313 Dali::Toolkit::Text::ControllerPtr controllerTextLabel = Dali::Toolkit::Text::Controller::New();
314 const Dali::Toolkit::Text::ModelInterface* modelLabel = controllerTextLabel->GetTextModel();
316 // Tests the rendering controller has been created.
317 Dali::Toolkit::Text::TypesetterPtr typesetterLabel = Dali::Toolkit::Text::Typesetter::New(controllerTextLabel->GetTextModel());
318 DALI_TEST_CHECK(typesetterLabel);
320 // Tests the view model has been created.
321 Dali::Toolkit::Text::ViewModel* viewModelLabel = typesetterLabel->GetViewModel();
322 DALI_TEST_CHECK(viewModelLabel);
324 // Configures the text controller similarly to the text-label.
325 Dali::Toolkit::Text::ConfigureTextLabel(controllerTextLabel);
327 DALI_TEST_EQUALS(false, modelLabel->IsSpannedTextPlaced(), TEST_LOCATION);
328 DALI_TEST_EQUALS(false, viewModelLabel->IsSpannedTextPlaced(), TEST_LOCATION);
330 controllerTextLabel->SetSpannedText(spannedText);
332 DALI_TEST_EQUALS(true, modelLabel->IsSpannedTextPlaced(), TEST_LOCATION);
333 DALI_TEST_EQUALS(true, viewModelLabel->IsSpannedTextPlaced(), TEST_LOCATION);
335 // Creates a text controller.
336 Dali::Toolkit::Text::ControllerPtr controllerTextField = Dali::Toolkit::Text::Controller::New();
337 const Dali::Toolkit::Text::ModelInterface* modelField = controllerTextField->GetTextModel();
339 // Tests the rendering controller has been created.
340 Dali::Toolkit::Text::TypesetterPtr typesetterField = Dali::Toolkit::Text::Typesetter::New(controllerTextField->GetTextModel());
341 DALI_TEST_CHECK(typesetterField);
343 // Tests the view model has been created.
344 Dali::Toolkit::Text::ViewModel* viewModelField = typesetterField->GetViewModel();
345 DALI_TEST_CHECK(viewModelField);
347 // Configures the text controller similarly to the text-field.
348 Dali::Toolkit::Text::ConfigureTextField(controllerTextField);
350 DALI_TEST_EQUALS(false, modelField->IsSpannedTextPlaced(), TEST_LOCATION);
351 DALI_TEST_EQUALS(false, viewModelField->IsSpannedTextPlaced(), TEST_LOCATION);
353 controllerTextField->SetSpannedText(spannedText);
355 DALI_TEST_EQUALS(true, modelField->IsSpannedTextPlaced(), TEST_LOCATION);
356 DALI_TEST_EQUALS(true, viewModelField->IsSpannedTextPlaced(), TEST_LOCATION);
358 tet_result(TET_PASS);
363 int UtcDaliToolkitTextLabelSetSpannedText_UnderlineSpan(void)
365 ToolkitTestApplication application;
366 tet_infoline(" UtcDaliToolkitTextLabelSetSpannedText_UnderlineSpan ");
368 Dali::Toolkit::Text::UnderlineStyleProperties expectedProperties = {
369 Text::Underline::DASHED,
380 TextLabel textLabel = TextLabel::New();
381 DALI_TEST_CHECK(textLabel);
382 application.GetScene().Add(textLabel);
384 Text::SpannableString spannableString = CreateSpannableStringForUnderlineSpan();
386 Text::SetSpannedText(textLabel, spannableString);
388 application.SendNotification();
389 application.Render();
391 Toolkit::Internal::TextLabel& labelImpl = GetImpl(textLabel);
392 const Text::Length numberOfUnderlineRuns = labelImpl.GetTextController()->GetTextModel()->GetNumberOfUnderlineRuns();
394 DALI_TEST_EQUALS(numberOfUnderlineRuns, 1u, TEST_LOCATION);
396 Vector<Dali::Toolkit::Text::UnderlinedGlyphRun> underlineRuns;
398 underlineRuns.Resize(numberOfUnderlineRuns);
400 labelImpl.GetTextController()->GetTextModel()->GetUnderlineRuns(underlineRuns.Begin(), 0u, numberOfUnderlineRuns);
402 DALI_TEST_EQUALS(underlineRuns[0].glyphRun.glyphIndex, 5u, TEST_LOCATION);
403 DALI_TEST_EQUALS(underlineRuns[0].glyphRun.numberOfGlyphs, 3u, TEST_LOCATION);
404 DALI_TEST_CHECK(underlineRuns[0].properties == expectedProperties);
409 int UtcDaliToolkitTextLabelSetSpannedText_CharacterSpacingSpan(void)
411 ToolkitTestApplication application;
412 tet_infoline("UtcDaliToolkitTextLabelSetSpannedText_CharacterSpacingSpan");
414 TextLabel textLabel = TextLabel::New();
415 DALI_TEST_CHECK(textLabel);
416 application.GetScene().Add(textLabel);
418 Text::SpannableString spannableString = CreateSpannableStringForCharacterSpacing();
419 Text::SetSpannedText(textLabel, spannableString);
421 application.SendNotification();
422 application.Render();
424 Toolkit::Internal::TextLabel& labelImpl = GetImpl(textLabel);
425 const Vector<Dali::Toolkit::Text::CharacterSpacingGlyphRun> characterSpacing = labelImpl.GetTextController()->GetTextModel()->GetCharacterSpacingGlyphRuns();
426 DALI_TEST_EQUALS(1, characterSpacing.Count(), TEST_LOCATION);
430 int UtcDaliToolkitTextLabelSetSpannedText_BoldSpan(void)
432 ToolkitTestApplication application;
434 tet_infoline("UtcDaliToolkitTextLabelSetSpannedText_BoldSpan");
436 TextLabel textLabel = TextLabel::New();
438 DALI_TEST_CHECK(textLabel);
440 application.GetScene().Add(textLabel);
442 Text::SpannableString spannableString = CreateSpannableStringForBoldSpan();
443 Text::SetSpannedText(textLabel, spannableString);
445 application.SendNotification();
446 application.Render();
448 Toolkit::Internal::TextLabel& labelImpl = GetImpl(textLabel);
450 const Vector<Text::FontRun>& validFonts = labelImpl.GetTextController()->GetTextModel()->GetFontRuns();
452 DALI_TEST_EQUALS(validFonts.Count(), 2, TEST_LOCATION);
453 DALI_TEST_EQUALS(validFonts[0].characterRun.characterIndex,0, TEST_LOCATION);
454 DALI_TEST_EQUALS(validFonts[0].characterRun.GetEndCharacterIndex(),3, TEST_LOCATION);
455 DALI_TEST_EQUALS(validFonts[0].isBoldRequired, true, TEST_LOCATION);
460 int UtcDaliToolkitTextLabelSetSpannedText_ItalicSpan(void)
462 ToolkitTestApplication application;
463 tet_infoline("UtcDaliToolkitTextLabelSetSpannedText_ItalicSpan");
465 TextLabel textLabel = TextLabel::New();
466 DALI_TEST_CHECK(textLabel);
468 application.GetScene().Add(textLabel);
470 Text::SpannableString spannableString = CreateSpannableStringForItalicSpan();
472 Text::SetSpannedText(textLabel, spannableString);
473 application.SendNotification();
474 application.Render();
476 Toolkit::Internal::TextLabel& labelImpl = GetImpl(textLabel);
477 const Vector<Text::FontRun>& validFontsItalic = labelImpl.GetTextController()->GetTextModel()->GetFontRuns();
479 DALI_TEST_EQUALS(validFontsItalic.Count(), 2, TEST_LOCATION);
480 DALI_TEST_EQUALS(validFontsItalic[0].characterRun.characterIndex, 0, TEST_LOCATION);
481 DALI_TEST_EQUALS(validFontsItalic[0].characterRun.GetEndCharacterIndex(), 3, TEST_LOCATION);
482 DALI_TEST_EQUALS(validFontsItalic[0].isItalicRequired, true, TEST_LOCATION);