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>
42 using namespace Toolkit;
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.
50 Text::SpannableString CreateSpannableStringForForegroundColorSpan()
52 Text::SpannableString spannableString = Text::SpannableString::New("Hello مرحبا");
53 DALI_TEST_CHECK(spannableString);
55 auto isAddedGreen = spannableString.AttachSpan(
56 Text::ForegroundColorSpan::New(Color::GREEN),
57 Text::Range::New(5u, 7u));
58 DALI_TEST_CHECK(isAddedGreen);
60 return spannableString;
63 Text::SpannableString CreateSpannableStringForFontSpan()
65 Text::SpannableString spannableString = Text::SpannableString::New("Hello World");
66 DALI_TEST_CHECK(spannableString);
68 auto isAddedFontSpan = spannableString.AttachSpan(
69 Text::FontSpan::New("TizenSans",
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);
77 return spannableString;
80 Text::SpannableString CreateSpannableStringForUnderlineSpan()
82 Text::SpannableString spannableString = Text::SpannableString::New("Hello World");
83 DALI_TEST_CHECK(spannableString);
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);
90 return spannableString;
93 void CheckColorIndices(const Text::ColorIndex* const colorIndicesBuffer,
94 uint32_t numberOfIndices,
95 std::vector<uint32_t> indicesToCheck,
96 std::vector<uint32_t> expectedValues)
98 DALI_TEST_CHECK(colorIndicesBuffer);
100 for(uint32_t i = 0u; i < numberOfIndices; i++)
102 DALI_TEST_EQUALS(colorIndicesBuffer[indicesToCheck[i]], expectedValues[i], TEST_LOCATION);
106 int UtcDaliToolkitTextLabelSetSpannedText(void)
108 ToolkitTestApplication application;
109 tet_infoline(" UtcDaliToolkitTextLabelSetSpannedText");
111 TextLabel textLabel = TextLabel::New();
112 DALI_TEST_CHECK(textLabel);
113 application.GetScene().Add(textLabel);
115 Text::SpannableString spannableString = CreateSpannableStringForForegroundColorSpan();
117 Text::SetSpannedText(textLabel, spannableString);
119 application.SendNotification();
120 application.Render();
122 Toolkit::Internal::TextLabel& labelImpl = GetImpl(textLabel);
123 const Text::ColorIndex* const colorIndicesBuffer = labelImpl.GetTextController()->GetTextModel()->GetColorIndices();
125 CheckColorIndices(colorIndicesBuffer, 4u, {0u, 5u, 7u, 10u}, {0u, 1u, 1u, 0u});
130 int UtcDaliToolkitTextEditorSetSpannedText(void)
132 ToolkitTestApplication application;
133 tet_infoline(" UtcDaliToolkitTextEditorSetSpannedText");
135 TextEditor textEditor = TextEditor::New();
136 DALI_TEST_CHECK(textEditor);
137 application.GetScene().Add(textEditor);
139 Text::SpannableString spannableString = CreateSpannableStringForForegroundColorSpan();
141 Text::SetSpannedText(textEditor, spannableString);
143 application.SendNotification();
144 application.Render();
146 Toolkit::Internal::TextEditor& labelImpl = GetImpl(textEditor);
147 const Text::ColorIndex* const colorIndicesBuffer = labelImpl.GetTextController()->GetTextModel()->GetColorIndices();
149 CheckColorIndices(colorIndicesBuffer, 4u, {0u, 5u, 7u, 10u}, {0u, 1u, 1u, 0u});
154 int UtcDaliToolkitTextFieldSetSpannedText(void)
156 ToolkitTestApplication application;
157 tet_infoline(" UtcDaliToolkitTextFieldSetSpannedText");
159 TextField textField = TextField::New();
160 DALI_TEST_CHECK(textField);
161 application.GetScene().Add(textField);
163 Text::SpannableString spannableString = CreateSpannableStringForForegroundColorSpan();
165 Text::SetSpannedText(textField, spannableString);
167 application.SendNotification();
168 application.Render();
170 Toolkit::Internal::TextField& labelImpl = GetImpl(textField);
171 const Text::ColorIndex* const colorIndicesBuffer = labelImpl.GetTextController()->GetTextModel()->GetColorIndices();
173 CheckColorIndices(colorIndicesBuffer, 4u, {0u, 5u, 7u, 10u}, {0u, 1u, 1u, 0u});
178 int UtcDaliToolkitTextLabelSetSpannedText_FontSpan(void)
180 ToolkitTestApplication application;
181 tet_infoline(" UtcDaliToolkitTextLabelSetSpannedText_FontSpan ");
183 // Load some fonts to get the same metrics on different platforms.
184 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
185 fontClient.SetDpi(96u, 96u);
187 char* pathNamePtr = get_current_dir_name();
188 const std::string pathName(pathNamePtr);
191 fontClient.GetFontId(pathName + DEFAULT_FONT_DIR + "/tizen/TizenSansRegular.ttf");
193 TextLabel textLabel = TextLabel::New();
194 DALI_TEST_CHECK(textLabel);
195 application.GetScene().Add(textLabel);
197 Text::SpannableString spannableString = CreateSpannableStringForFontSpan();
199 Text::SetSpannedText(textLabel, spannableString);
201 application.SendNotification();
202 application.Render();
204 Toolkit::Internal::TextLabel& labelImpl = GetImpl(textLabel);
205 const Vector<Dali::Toolkit::Text::FontRun>& validFontRuns = labelImpl.GetTextController()->GetTextModel()->GetFontRuns();
207 DALI_TEST_EQUALS(validFontRuns.Count(), 3u, TEST_LOCATION);
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);
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);
217 TextAbstraction::FontDescription spanFontDescription;
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);
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;
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);
236 int UtcDaliTextModelIsSpannedTextPlaced(void)
239 tet_infoline(" UtcDaliTextModelIsSpannedTextPlaced");
241 ToolkitTestApplication application;
243 // Create spanned-text and set it
244 Text::SpannableString spannedText = Text::SpannableString::New("Hello مرحبا");
245 DALI_TEST_CHECK(spannedText);
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();
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);
255 // Tests the view model has been created.
256 Dali::Toolkit::Text::ViewModel* viewModelEditor = typesetterEditor->GetViewModel();
257 DALI_TEST_CHECK(viewModelEditor);
259 // Configures the text controller similarly to the text-editor.
260 Dali::Toolkit::Text::ConfigureTextEditor(controllerTextEditor);
262 DALI_TEST_EQUALS(false, modelEditor->IsSpannedTextPlaced(), TEST_LOCATION);
263 DALI_TEST_EQUALS(false, viewModelEditor->IsSpannedTextPlaced(), TEST_LOCATION);
265 controllerTextEditor->SetSpannedText(spannedText);
267 DALI_TEST_EQUALS(true, modelEditor->IsSpannedTextPlaced(), TEST_LOCATION);
268 DALI_TEST_EQUALS(true, viewModelEditor->IsSpannedTextPlaced(), TEST_LOCATION);
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();
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);
278 // Tests the view model has been created.
279 Dali::Toolkit::Text::ViewModel* viewModelLabel = typesetterLabel->GetViewModel();
280 DALI_TEST_CHECK(viewModelLabel);
282 // Configures the text controller similarly to the text-label.
283 Dali::Toolkit::Text::ConfigureTextLabel(controllerTextLabel);
285 DALI_TEST_EQUALS(false, modelLabel->IsSpannedTextPlaced(), TEST_LOCATION);
286 DALI_TEST_EQUALS(false, viewModelLabel->IsSpannedTextPlaced(), TEST_LOCATION);
288 controllerTextLabel->SetSpannedText(spannedText);
290 DALI_TEST_EQUALS(true, modelLabel->IsSpannedTextPlaced(), TEST_LOCATION);
291 DALI_TEST_EQUALS(true, viewModelLabel->IsSpannedTextPlaced(), TEST_LOCATION);
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();
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);
301 // Tests the view model has been created.
302 Dali::Toolkit::Text::ViewModel* viewModelField = typesetterField->GetViewModel();
303 DALI_TEST_CHECK(viewModelField);
305 // Configures the text controller similarly to the text-field.
306 Dali::Toolkit::Text::ConfigureTextField(controllerTextField);
308 DALI_TEST_EQUALS(false, modelField->IsSpannedTextPlaced(), TEST_LOCATION);
309 DALI_TEST_EQUALS(false, viewModelField->IsSpannedTextPlaced(), TEST_LOCATION);
311 controllerTextField->SetSpannedText(spannedText);
313 DALI_TEST_EQUALS(true, modelField->IsSpannedTextPlaced(), TEST_LOCATION);
314 DALI_TEST_EQUALS(true, viewModelField->IsSpannedTextPlaced(), TEST_LOCATION);
316 tet_result(TET_PASS);
321 int UtcDaliToolkitTextLabelSetSpannedText_UnderlineSpan(void)
323 ToolkitTestApplication application;
324 tet_infoline(" UtcDaliToolkitTextLabelSetSpannedText_UnderlineSpan ");
326 Dali::Toolkit::Text::UnderlineStyleProperties expectedProperties = {
327 Text::Underline::DASHED,
338 TextLabel textLabel = TextLabel::New();
339 DALI_TEST_CHECK(textLabel);
340 application.GetScene().Add(textLabel);
342 Text::SpannableString spannableString = CreateSpannableStringForUnderlineSpan();
344 Text::SetSpannedText(textLabel, spannableString);
346 application.SendNotification();
347 application.Render();
349 Toolkit::Internal::TextLabel& labelImpl = GetImpl(textLabel);
350 const Text::Length numberOfUnderlineRuns = labelImpl.GetTextController()->GetTextModel()->GetNumberOfUnderlineRuns();
352 DALI_TEST_EQUALS(numberOfUnderlineRuns, 1u, TEST_LOCATION);
354 Vector<Dali::Toolkit::Text::UnderlinedGlyphRun> underlineRuns;
356 underlineRuns.Resize(numberOfUnderlineRuns);
358 labelImpl.GetTextController()->GetTextModel()->GetUnderlineRuns(underlineRuns.Begin(), 0u, numberOfUnderlineRuns);
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);