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/text-enumerations-devel.h>
29 #include <dali-toolkit/internal/controls/text-controls/text-editor-impl.h>
30 #include <dali-toolkit/internal/controls/text-controls/text-field-impl.h>
31 #include <dali-toolkit/internal/controls/text-controls/text-label-impl.h>
32 #include <dali-toolkit/internal/text/controller/text-controller.h>
33 #include <dali-toolkit/internal/text/font-description-run.h>
34 #include <dali-toolkit/internal/text/rendering/text-typesetter.h>
35 #include <dali-toolkit/internal/text/rendering/view-model.h>
36 #include <dali-toolkit/internal/text/text-view.h>
37 #include <dali-toolkit/public-api/text/text-enumerations.h>
38 #include <toolkit-text-utils.h>
41 using namespace Toolkit;
45 const std::string DEFAULT_FONT_DIR("/resources/fonts");
46 const float PIXEL_FORMAT_64_FACTOR = 64.f; ///< 64.f is used to convert from point size to 26.6 pixel format.
49 Text::SpannableString CreateSpannableStringForForegroundColorSpan()
51 Text::SpannableString spannableString = Text::SpannableString::New("Hello مرحبا");
52 DALI_TEST_CHECK(spannableString);
54 auto isAddedGreen = spannableString.AttachSpan(
55 Text::ForegroundColorSpan::New(Color::GREEN),
56 Text::Range::New(5u, 7u));
57 DALI_TEST_CHECK(isAddedGreen);
59 return spannableString;
62 Text::SpannableString CreateSpannableStringForFontSpan()
64 Text::SpannableString spannableString = Text::SpannableString::New("Hello World");
65 DALI_TEST_CHECK(spannableString);
67 auto isAddedFontSpan = spannableString.AttachSpan(
68 Text::FontSpan::New("TizenSans",
70 Dali::TextAbstraction::FontWeight::BOLD,
71 Dali::TextAbstraction::FontWidth::SEMI_CONDENSED,
72 Dali::TextAbstraction::FontSlant::OBLIQUE),
73 Text::Range::New(5u, 7u));
74 DALI_TEST_CHECK(isAddedFontSpan);
76 return spannableString;
79 void CheckColorIndices(const Text::ColorIndex* const colorIndicesBuffer,
80 uint32_t numberOfIndices,
81 std::vector<uint32_t> indicesToCheck,
82 std::vector<uint32_t> expectedValues)
84 DALI_TEST_CHECK(colorIndicesBuffer);
86 for(uint32_t i = 0u; i < numberOfIndices; i++)
88 DALI_TEST_EQUALS(colorIndicesBuffer[indicesToCheck[i]], expectedValues[i], TEST_LOCATION);
92 int UtcDaliToolkitTextLabelSetSpannedText(void)
94 ToolkitTestApplication application;
95 tet_infoline(" UtcDaliToolkitTextLabelSetSpannedText");
97 TextLabel textLabel = TextLabel::New();
98 DALI_TEST_CHECK(textLabel);
99 application.GetScene().Add(textLabel);
101 Text::SpannableString spannableString = CreateSpannableStringForForegroundColorSpan();
103 Text::SetSpannedText(textLabel, spannableString);
105 application.SendNotification();
106 application.Render();
108 Toolkit::Internal::TextLabel& labelImpl = GetImpl(textLabel);
109 const Text::ColorIndex* const colorIndicesBuffer = labelImpl.GetTextController()->GetTextModel()->GetColorIndices();
111 CheckColorIndices(colorIndicesBuffer, 4u, {0u, 5u, 7u, 10u}, {0u, 1u, 1u, 0u});
116 int UtcDaliToolkitTextEditorSetSpannedText(void)
118 ToolkitTestApplication application;
119 tet_infoline(" UtcDaliToolkitTextEditorSetSpannedText");
121 TextEditor textEditor = TextEditor::New();
122 DALI_TEST_CHECK(textEditor);
123 application.GetScene().Add(textEditor);
125 Text::SpannableString spannableString = CreateSpannableStringForForegroundColorSpan();
127 Text::SetSpannedText(textEditor, spannableString);
129 application.SendNotification();
130 application.Render();
132 Toolkit::Internal::TextEditor& labelImpl = GetImpl(textEditor);
133 const Text::ColorIndex* const colorIndicesBuffer = labelImpl.GetTextController()->GetTextModel()->GetColorIndices();
135 CheckColorIndices(colorIndicesBuffer, 4u, {0u, 5u, 7u, 10u}, {0u, 1u, 1u, 0u});
140 int UtcDaliToolkitTextFieldSetSpannedText(void)
142 ToolkitTestApplication application;
143 tet_infoline(" UtcDaliToolkitTextFieldSetSpannedText");
145 TextField textField = TextField::New();
146 DALI_TEST_CHECK(textField);
147 application.GetScene().Add(textField);
149 Text::SpannableString spannableString = CreateSpannableStringForForegroundColorSpan();
151 Text::SetSpannedText(textField, spannableString);
153 application.SendNotification();
154 application.Render();
156 Toolkit::Internal::TextField& labelImpl = GetImpl(textField);
157 const Text::ColorIndex* const colorIndicesBuffer = labelImpl.GetTextController()->GetTextModel()->GetColorIndices();
159 CheckColorIndices(colorIndicesBuffer, 4u, {0u, 5u, 7u, 10u}, {0u, 1u, 1u, 0u});
164 int UtcDaliToolkitTextLabelSetSpannedText_FontSpan(void)
166 ToolkitTestApplication application;
167 tet_infoline(" UtcDaliToolkitTextLabelSetSpannedText_FontSpan ");
169 // Load some fonts to get the same metrics on different platforms.
170 TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
171 fontClient.SetDpi(96u, 96u);
173 char* pathNamePtr = get_current_dir_name();
174 const std::string pathName(pathNamePtr);
177 fontClient.GetFontId(pathName + DEFAULT_FONT_DIR + "/tizen/TizenSansRegular.ttf");
179 TextLabel textLabel = TextLabel::New();
180 DALI_TEST_CHECK(textLabel);
181 application.GetScene().Add(textLabel);
183 Text::SpannableString spannableString = CreateSpannableStringForFontSpan();
185 Text::SetSpannedText(textLabel, spannableString);
187 application.SendNotification();
188 application.Render();
190 Toolkit::Internal::TextLabel& labelImpl = GetImpl(textLabel);
191 const Vector<Dali::Toolkit::Text::FontRun>& validFontRuns = labelImpl.GetTextController()->GetTextModel()->GetFontRuns();
193 DALI_TEST_EQUALS(validFontRuns.Count(), 3u, TEST_LOCATION);
195 DALI_TEST_EQUALS(validFontRuns[0].fontId, validFontRuns[2].fontId, TEST_LOCATION);
196 DALI_TEST_NOT_EQUALS(validFontRuns[0].fontId, validFontRuns[1].fontId, Math::MACHINE_EPSILON_100, TEST_LOCATION);
198 DALI_TEST_EQUALS(validFontRuns[1].characterRun.characterIndex, 5u, TEST_LOCATION);
199 DALI_TEST_EQUALS(validFontRuns[1].characterRun.GetEndCharacterIndex(), 7u, TEST_LOCATION);
200 DALI_TEST_EQUALS(validFontRuns[1].isItalicRequired, true, TEST_LOCATION);
201 DALI_TEST_EQUALS(validFontRuns[1].isBoldRequired, true, TEST_LOCATION);
203 TextAbstraction::FontDescription spanFontDescription;
205 float expectedPointSize = 45.0f * PIXEL_FORMAT_64_FACTOR;
206 float fontPointSize = fontClient.GetPointSize(validFontRuns[1].fontId);
207 DALI_TEST_EQUALS(fontPointSize, expectedPointSize, TEST_LOCATION);
209 const Vector<Dali::Toolkit::Text::FontDescriptionRun>& validFontDescriptionRuns = labelImpl.GetTextController()->GetTextModel()->GetFontDescriptionRuns();
210 DALI_TEST_EQUALS(validFontDescriptionRuns.Count(), 1u, TEST_LOCATION);
211 std::string familyName = validFontDescriptionRuns[0].familyName;
213 DALI_TEST_EQUALS(familyName, "TizenSans", TEST_LOCATION);
214 DALI_TEST_EQUALS(validFontDescriptionRuns[0].size, expectedPointSize, TEST_LOCATION);
215 DALI_TEST_EQUALS(validFontDescriptionRuns[0].weight, Dali::TextAbstraction::FontWeight::BOLD, TEST_LOCATION);
216 DALI_TEST_EQUALS(validFontDescriptionRuns[0].width, Dali::TextAbstraction::FontWidth::SEMI_CONDENSED, TEST_LOCATION);
217 DALI_TEST_EQUALS(validFontDescriptionRuns[0].slant, Dali::TextAbstraction::FontSlant::OBLIQUE, TEST_LOCATION);
222 int UtcDaliTextModelIsSpannedTextPlaced(void)
225 tet_infoline(" UtcDaliTextModelIsSpannedTextPlaced");
227 ToolkitTestApplication application;
229 // Create spanned-text and set it
230 Text::SpannableString spannedText = Text::SpannableString::New("Hello مرحبا");
231 DALI_TEST_CHECK(spannedText);
233 // Creates a text controller.
234 Dali::Toolkit::Text::ControllerPtr controllerTextEditor = Dali::Toolkit::Text::Controller::New();
235 const Dali::Toolkit::Text::ModelInterface* modelEditor = controllerTextEditor->GetTextModel();
237 // Tests the rendering controller has been created.
238 Dali::Toolkit::Text::TypesetterPtr typesetterEditor = Dali::Toolkit::Text::Typesetter::New(controllerTextEditor->GetTextModel());
239 DALI_TEST_CHECK(typesetterEditor);
241 // Tests the view model has been created.
242 Dali::Toolkit::Text::ViewModel* viewModelEditor = typesetterEditor->GetViewModel();
243 DALI_TEST_CHECK(viewModelEditor);
245 // Configures the text controller similarly to the text-editor.
246 Dali::Toolkit::Text::ConfigureTextEditor(controllerTextEditor);
248 DALI_TEST_EQUALS(false, modelEditor->IsSpannedTextPlaced(), TEST_LOCATION);
249 DALI_TEST_EQUALS(false, viewModelEditor->IsSpannedTextPlaced(), TEST_LOCATION);
251 controllerTextEditor->SetSpannedText(spannedText);
253 DALI_TEST_EQUALS(true, modelEditor->IsSpannedTextPlaced(), TEST_LOCATION);
254 DALI_TEST_EQUALS(true, viewModelEditor->IsSpannedTextPlaced(), TEST_LOCATION);
256 // Creates a text controller.
257 Dali::Toolkit::Text::ControllerPtr controllerTextLabel = Dali::Toolkit::Text::Controller::New();
258 const Dali::Toolkit::Text::ModelInterface* modelLabel = controllerTextLabel->GetTextModel();
260 // Tests the rendering controller has been created.
261 Dali::Toolkit::Text::TypesetterPtr typesetterLabel = Dali::Toolkit::Text::Typesetter::New(controllerTextLabel->GetTextModel());
262 DALI_TEST_CHECK(typesetterLabel);
264 // Tests the view model has been created.
265 Dali::Toolkit::Text::ViewModel* viewModelLabel = typesetterLabel->GetViewModel();
266 DALI_TEST_CHECK(viewModelLabel);
268 // Configures the text controller similarly to the text-label.
269 Dali::Toolkit::Text::ConfigureTextLabel(controllerTextLabel);
271 DALI_TEST_EQUALS(false, modelLabel->IsSpannedTextPlaced(), TEST_LOCATION);
272 DALI_TEST_EQUALS(false, viewModelLabel->IsSpannedTextPlaced(), TEST_LOCATION);
274 controllerTextLabel->SetSpannedText(spannedText);
276 DALI_TEST_EQUALS(true, modelLabel->IsSpannedTextPlaced(), TEST_LOCATION);
277 DALI_TEST_EQUALS(true, viewModelLabel->IsSpannedTextPlaced(), TEST_LOCATION);
279 // Creates a text controller.
280 Dali::Toolkit::Text::ControllerPtr controllerTextField = Dali::Toolkit::Text::Controller::New();
281 const Dali::Toolkit::Text::ModelInterface* modelField = controllerTextField->GetTextModel();
283 // Tests the rendering controller has been created.
284 Dali::Toolkit::Text::TypesetterPtr typesetterField = Dali::Toolkit::Text::Typesetter::New(controllerTextField->GetTextModel());
285 DALI_TEST_CHECK(typesetterField);
287 // Tests the view model has been created.
288 Dali::Toolkit::Text::ViewModel* viewModelField = typesetterField->GetViewModel();
289 DALI_TEST_CHECK(viewModelField);
291 // Configures the text controller similarly to the text-field.
292 Dali::Toolkit::Text::ConfigureTextField(controllerTextField);
294 DALI_TEST_EQUALS(false, modelField->IsSpannedTextPlaced(), TEST_LOCATION);
295 DALI_TEST_EQUALS(false, viewModelField->IsSpannedTextPlaced(), TEST_LOCATION);
297 controllerTextField->SetSpannedText(spannedText);
299 DALI_TEST_EQUALS(true, modelField->IsSpannedTextPlaced(), TEST_LOCATION);
300 DALI_TEST_EQUALS(true, viewModelField->IsSpannedTextPlaced(), TEST_LOCATION);
302 tet_result(TET_PASS);