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>
44 using namespace Toolkit;
48 const std::string DEFAULT_FONT_DIR("/resources/fonts");
49 const float PIXEL_FORMAT_64_FACTOR = 64.f; ///< 64.f is used to convert from point size to 26.6 pixel format.
52 Text::SpannableString CreateSpannableStringForForegroundColorSpan()
54 Text::SpannableString spannableString = Text::SpannableString::New("Hello مرحبا");
55 DALI_TEST_CHECK(spannableString);
57 auto isAddedGreen = spannableString.AttachSpan(
58 Text::ForegroundColorSpan::New(Color::GREEN),
59 Text::Range::New(5u, 7u));
60 DALI_TEST_CHECK(isAddedGreen);
62 return spannableString;
65 Text::SpannableString CreateSpannableStringForFontSpan()
67 Text::SpannableString spannableString = Text::SpannableString::New("Hello World");
68 DALI_TEST_CHECK(spannableString);
70 auto isAddedFontSpan = spannableString.AttachSpan(
71 Text::FontSpan::New("TizenSans",
73 Dali::TextAbstraction::FontWeight::BOLD,
74 Dali::TextAbstraction::FontWidth::SEMI_CONDENSED,
75 Dali::TextAbstraction::FontSlant::OBLIQUE),
76 Text::Range::New(5u, 7u));
77 DALI_TEST_CHECK(isAddedFontSpan);
79 return spannableString;
82 Text::SpannableString CreateSpannableStringForUnderlineSpan()
84 Text::SpannableString spannableString = Text::SpannableString::New("Hello World");
85 DALI_TEST_CHECK(spannableString);
87 auto isAddedUnderlineSpan = spannableString.AttachSpan(
88 Text::UnderlineSpan::NewDashed(Color::GREEN, 5.0f, 2.0f, 3.0f),
89 Text::Range::New(5u, 7u));
90 DALI_TEST_CHECK(isAddedUnderlineSpan);
92 return spannableString;
95 Text::SpannableString CreateSpannableStringForCharacterSpacing()
97 Text::SpannableString spannableString = Text::SpannableString::New("Hello World");
98 DALI_TEST_CHECK(spannableString);
100 auto isCharacterSpacingSpan = spannableString.AttachSpan(
101 Text::CharacterSpacingSpan::New(5.2f),
102 Text::Range::New(5u, 7u));
103 DALI_TEST_CHECK(isCharacterSpacingSpan);
105 return spannableString;
108 void CheckColorIndices(const Text::ColorIndex* const colorIndicesBuffer,
109 uint32_t numberOfIndices,
110 std::vector<uint32_t> indicesToCheck,
111 std::vector<uint32_t> expectedValues)
113 DALI_TEST_CHECK(colorIndicesBuffer);
115 for(uint32_t i = 0u; i < numberOfIndices; i++)
117 DALI_TEST_EQUALS(colorIndicesBuffer[indicesToCheck[i]], expectedValues[i], TEST_LOCATION);
121 int UtcDaliToolkitTextLabelSetSpannedText(void)
123 ToolkitTestApplication application;
124 tet_infoline(" UtcDaliToolkitTextLabelSetSpannedText");
126 TextLabel textLabel = TextLabel::New();
127 DALI_TEST_CHECK(textLabel);
128 application.GetScene().Add(textLabel);
130 Text::SpannableString spannableString = CreateSpannableStringForForegroundColorSpan();
132 Text::SetSpannedText(textLabel, spannableString);
134 application.SendNotification();
135 application.Render();
137 Toolkit::Internal::TextLabel& labelImpl = GetImpl(textLabel);
138 const Text::ColorIndex* const colorIndicesBuffer = labelImpl.GetTextController()->GetTextModel()->GetColorIndices();
140 CheckColorIndices(colorIndicesBuffer, 4u, {0u, 5u, 7u, 10u}, {0u, 1u, 1u, 0u});
145 int UtcDaliToolkitTextEditorSetSpannedText(void)
147 ToolkitTestApplication application;
148 tet_infoline(" UtcDaliToolkitTextEditorSetSpannedText");
150 TextEditor textEditor = TextEditor::New();
151 DALI_TEST_CHECK(textEditor);
152 application.GetScene().Add(textEditor);
154 Text::SpannableString spannableString = CreateSpannableStringForForegroundColorSpan();
156 Text::SetSpannedText(textEditor, spannableString);
158 application.SendNotification();
159 application.Render();
161 Toolkit::Internal::TextEditor& labelImpl = GetImpl(textEditor);
162 const Text::ColorIndex* const colorIndicesBuffer = labelImpl.GetTextController()->GetTextModel()->GetColorIndices();
164 CheckColorIndices(colorIndicesBuffer, 4u, {0u, 5u, 7u, 10u}, {0u, 1u, 1u, 0u});
169 int UtcDaliToolkitTextFieldSetSpannedText(void)
171 ToolkitTestApplication application;
172 tet_infoline(" UtcDaliToolkitTextFieldSetSpannedText");
174 TextField textField = TextField::New();
175 DALI_TEST_CHECK(textField);
176 application.GetScene().Add(textField);
178 Text::SpannableString spannableString = CreateSpannableStringForForegroundColorSpan();
180 Text::SetSpannedText(textField, spannableString);
182 application.SendNotification();
183 application.Render();
185 Toolkit::Internal::TextField& labelImpl = GetImpl(textField);
186 const Text::ColorIndex* const colorIndicesBuffer = labelImpl.GetTextController()->GetTextModel()->GetColorIndices();
188 CheckColorIndices(colorIndicesBuffer, 4u, {0u, 5u, 7u, 10u}, {0u, 1u, 1u, 0u});
193 int UtcDaliToolkitTextLabelSetSpannedText_FontSpan(void)
195 ToolkitTestApplication application;
196 tet_infoline(" UtcDaliToolkitTextLabelSetSpannedText_FontSpan ");
198 // Load some fonts to get the same metrics on different platforms.
199 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
200 fontClient.SetDpi(96u, 96u);
202 char* pathNamePtr = get_current_dir_name();
203 const std::string pathName(pathNamePtr);
206 fontClient.GetFontId(pathName + DEFAULT_FONT_DIR + "/tizen/TizenSansRegular.ttf");
208 TextLabel textLabel = TextLabel::New();
209 DALI_TEST_CHECK(textLabel);
210 application.GetScene().Add(textLabel);
212 Text::SpannableString spannableString = CreateSpannableStringForFontSpan();
214 Text::SetSpannedText(textLabel, spannableString);
216 application.SendNotification();
217 application.Render();
219 Toolkit::Internal::TextLabel& labelImpl = GetImpl(textLabel);
220 const Vector<Dali::Toolkit::Text::FontRun>& validFontRuns = labelImpl.GetTextController()->GetTextModel()->GetFontRuns();
222 DALI_TEST_EQUALS(validFontRuns.Count(), 3u, TEST_LOCATION);
224 DALI_TEST_EQUALS(validFontRuns[0].fontId, validFontRuns[2].fontId, TEST_LOCATION);
225 DALI_TEST_NOT_EQUALS(validFontRuns[0].fontId, validFontRuns[1].fontId, Math::MACHINE_EPSILON_100, TEST_LOCATION);
227 DALI_TEST_EQUALS(validFontRuns[1].characterRun.characterIndex, 5u, TEST_LOCATION);
228 DALI_TEST_EQUALS(validFontRuns[1].characterRun.GetEndCharacterIndex(), 7u, TEST_LOCATION);
229 DALI_TEST_EQUALS(validFontRuns[1].isItalicRequired, true, TEST_LOCATION);
230 DALI_TEST_EQUALS(validFontRuns[1].isBoldRequired, true, TEST_LOCATION);
232 TextAbstraction::FontDescription spanFontDescription;
234 float expectedPointSize = 45.0f * PIXEL_FORMAT_64_FACTOR;
235 float fontPointSize = fontClient.GetPointSize(validFontRuns[1].fontId);
236 DALI_TEST_EQUALS(fontPointSize, expectedPointSize, TEST_LOCATION);
238 const Vector<Dali::Toolkit::Text::FontDescriptionRun>& validFontDescriptionRuns = labelImpl.GetTextController()->GetTextModel()->GetFontDescriptionRuns();
239 DALI_TEST_EQUALS(validFontDescriptionRuns.Count(), 1u, TEST_LOCATION);
240 std::string familyName = validFontDescriptionRuns[0].familyName;
242 DALI_TEST_EQUALS(familyName, "TizenSans", TEST_LOCATION);
243 DALI_TEST_EQUALS(validFontDescriptionRuns[0].size, expectedPointSize, TEST_LOCATION);
244 DALI_TEST_EQUALS(validFontDescriptionRuns[0].weight, Dali::TextAbstraction::FontWeight::BOLD, TEST_LOCATION);
245 DALI_TEST_EQUALS(validFontDescriptionRuns[0].width, Dali::TextAbstraction::FontWidth::SEMI_CONDENSED, TEST_LOCATION);
246 DALI_TEST_EQUALS(validFontDescriptionRuns[0].slant, Dali::TextAbstraction::FontSlant::OBLIQUE, TEST_LOCATION);
251 int UtcDaliTextModelIsSpannedTextPlaced(void)
254 tet_infoline(" UtcDaliTextModelIsSpannedTextPlaced");
256 ToolkitTestApplication application;
258 // Create spanned-text and set it
259 Text::SpannableString spannedText = Text::SpannableString::New("Hello مرحبا");
260 DALI_TEST_CHECK(spannedText);
262 // Creates a text controller.
263 Dali::Toolkit::Text::ControllerPtr controllerTextEditor = Dali::Toolkit::Text::Controller::New();
264 const Dali::Toolkit::Text::ModelInterface* modelEditor = controllerTextEditor->GetTextModel();
266 // Tests the rendering controller has been created.
267 Dali::Toolkit::Text::TypesetterPtr typesetterEditor = Dali::Toolkit::Text::Typesetter::New(controllerTextEditor->GetTextModel());
268 DALI_TEST_CHECK(typesetterEditor);
270 // Tests the view model has been created.
271 Dali::Toolkit::Text::ViewModel* viewModelEditor = typesetterEditor->GetViewModel();
272 DALI_TEST_CHECK(viewModelEditor);
274 // Configures the text controller similarly to the text-editor.
275 Dali::Toolkit::Text::ConfigureTextEditor(controllerTextEditor);
277 DALI_TEST_EQUALS(false, modelEditor->IsSpannedTextPlaced(), TEST_LOCATION);
278 DALI_TEST_EQUALS(false, viewModelEditor->IsSpannedTextPlaced(), TEST_LOCATION);
280 controllerTextEditor->SetSpannedText(spannedText);
282 DALI_TEST_EQUALS(true, modelEditor->IsSpannedTextPlaced(), TEST_LOCATION);
283 DALI_TEST_EQUALS(true, viewModelEditor->IsSpannedTextPlaced(), TEST_LOCATION);
285 // Creates a text controller.
286 Dali::Toolkit::Text::ControllerPtr controllerTextLabel = Dali::Toolkit::Text::Controller::New();
287 const Dali::Toolkit::Text::ModelInterface* modelLabel = controllerTextLabel->GetTextModel();
289 // Tests the rendering controller has been created.
290 Dali::Toolkit::Text::TypesetterPtr typesetterLabel = Dali::Toolkit::Text::Typesetter::New(controllerTextLabel->GetTextModel());
291 DALI_TEST_CHECK(typesetterLabel);
293 // Tests the view model has been created.
294 Dali::Toolkit::Text::ViewModel* viewModelLabel = typesetterLabel->GetViewModel();
295 DALI_TEST_CHECK(viewModelLabel);
297 // Configures the text controller similarly to the text-label.
298 Dali::Toolkit::Text::ConfigureTextLabel(controllerTextLabel);
300 DALI_TEST_EQUALS(false, modelLabel->IsSpannedTextPlaced(), TEST_LOCATION);
301 DALI_TEST_EQUALS(false, viewModelLabel->IsSpannedTextPlaced(), TEST_LOCATION);
303 controllerTextLabel->SetSpannedText(spannedText);
305 DALI_TEST_EQUALS(true, modelLabel->IsSpannedTextPlaced(), TEST_LOCATION);
306 DALI_TEST_EQUALS(true, viewModelLabel->IsSpannedTextPlaced(), TEST_LOCATION);
308 // Creates a text controller.
309 Dali::Toolkit::Text::ControllerPtr controllerTextField = Dali::Toolkit::Text::Controller::New();
310 const Dali::Toolkit::Text::ModelInterface* modelField = controllerTextField->GetTextModel();
312 // Tests the rendering controller has been created.
313 Dali::Toolkit::Text::TypesetterPtr typesetterField = Dali::Toolkit::Text::Typesetter::New(controllerTextField->GetTextModel());
314 DALI_TEST_CHECK(typesetterField);
316 // Tests the view model has been created.
317 Dali::Toolkit::Text::ViewModel* viewModelField = typesetterField->GetViewModel();
318 DALI_TEST_CHECK(viewModelField);
320 // Configures the text controller similarly to the text-field.
321 Dali::Toolkit::Text::ConfigureTextField(controllerTextField);
323 DALI_TEST_EQUALS(false, modelField->IsSpannedTextPlaced(), TEST_LOCATION);
324 DALI_TEST_EQUALS(false, viewModelField->IsSpannedTextPlaced(), TEST_LOCATION);
326 controllerTextField->SetSpannedText(spannedText);
328 DALI_TEST_EQUALS(true, modelField->IsSpannedTextPlaced(), TEST_LOCATION);
329 DALI_TEST_EQUALS(true, viewModelField->IsSpannedTextPlaced(), TEST_LOCATION);
331 tet_result(TET_PASS);
336 int UtcDaliToolkitTextLabelSetSpannedText_UnderlineSpan(void)
338 ToolkitTestApplication application;
339 tet_infoline(" UtcDaliToolkitTextLabelSetSpannedText_UnderlineSpan ");
341 Dali::Toolkit::Text::UnderlineStyleProperties expectedProperties = {
342 Text::Underline::DASHED,
353 TextLabel textLabel = TextLabel::New();
354 DALI_TEST_CHECK(textLabel);
355 application.GetScene().Add(textLabel);
357 Text::SpannableString spannableString = CreateSpannableStringForUnderlineSpan();
359 Text::SetSpannedText(textLabel, spannableString);
361 application.SendNotification();
362 application.Render();
364 Toolkit::Internal::TextLabel& labelImpl = GetImpl(textLabel);
365 const Text::Length numberOfUnderlineRuns = labelImpl.GetTextController()->GetTextModel()->GetNumberOfUnderlineRuns();
367 DALI_TEST_EQUALS(numberOfUnderlineRuns, 1u, TEST_LOCATION);
369 Vector<Dali::Toolkit::Text::UnderlinedGlyphRun> underlineRuns;
371 underlineRuns.Resize(numberOfUnderlineRuns);
373 labelImpl.GetTextController()->GetTextModel()->GetUnderlineRuns(underlineRuns.Begin(), 0u, numberOfUnderlineRuns);
375 DALI_TEST_EQUALS(underlineRuns[0].glyphRun.glyphIndex, 5u, TEST_LOCATION);
376 DALI_TEST_EQUALS(underlineRuns[0].glyphRun.numberOfGlyphs, 3u, TEST_LOCATION);
377 DALI_TEST_CHECK(underlineRuns[0].properties == expectedProperties);
382 int UtcDaliToolkitTextLabelSetSpannedText_CharacterSpacingSpan(void)
384 ToolkitTestApplication application;
385 tet_infoline("UtcDaliToolkitTextLabelSetSpannedText_CharacterSpacingSpan");
387 TextLabel textLabel = TextLabel::New();
388 DALI_TEST_CHECK(textLabel);
389 application.GetScene().Add(textLabel);
391 Text::SpannableString spannableString = CreateSpannableStringForCharacterSpacing();
392 Text::SetSpannedText(textLabel, spannableString);
394 application.SendNotification();
395 application.Render();
397 Toolkit::Internal::TextLabel& labelImpl = GetImpl(textLabel);
398 const Vector<Dali::Toolkit::Text::CharacterSpacingGlyphRun> characterSpacing = labelImpl.GetTextController()->GetTextModel()->GetCharacterSpacingGlyphRuns();
399 DALI_TEST_EQUALS(1, characterSpacing.Count(), TEST_LOCATION);