Add IsSpannedTextPlaced
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-internal / utc-Dali-Text-TextSpannable.cpp
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <iostream>
21
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>
39
40 using namespace Dali;
41 using namespace Toolkit;
42
43 namespace
44 {
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.
47 } // namespace
48
49 Text::SpannableString CreateSpannableStringForForegroundColorSpan()
50 {
51   Text::SpannableString spannableString = Text::SpannableString::New("Hello مرحبا");
52   DALI_TEST_CHECK(spannableString);
53
54   auto isAddedGreen = spannableString.AttachSpan(
55     Text::ForegroundColorSpan::New(Color::GREEN),
56     Text::Range::New(5u, 7u));
57   DALI_TEST_CHECK(isAddedGreen);
58
59   return spannableString;
60 }
61
62 Text::SpannableString CreateSpannableStringForFontSpan()
63 {
64   Text::SpannableString spannableString = Text::SpannableString::New("Hello World");
65   DALI_TEST_CHECK(spannableString);
66
67   auto isAddedFontSpan = spannableString.AttachSpan(
68     Text::FontSpan::New("TizenSans",
69                         45.0f,
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);
75
76   return spannableString;
77 }
78
79 void CheckColorIndices(const Text::ColorIndex* const colorIndicesBuffer,
80                        uint32_t                      numberOfIndices,
81                        std::vector<uint32_t>         indicesToCheck,
82                        std::vector<uint32_t>         expectedValues)
83 {
84   DALI_TEST_CHECK(colorIndicesBuffer);
85
86   for(uint32_t i = 0u; i < numberOfIndices; i++)
87   {
88     DALI_TEST_EQUALS(colorIndicesBuffer[indicesToCheck[i]], expectedValues[i], TEST_LOCATION);
89   }
90 }
91
92 int UtcDaliToolkitTextLabelSetSpannedText(void)
93 {
94   ToolkitTestApplication application;
95   tet_infoline(" UtcDaliToolkitTextLabelSetSpannedText");
96
97   TextLabel textLabel = TextLabel::New();
98   DALI_TEST_CHECK(textLabel);
99   application.GetScene().Add(textLabel);
100
101   Text::SpannableString spannableString = CreateSpannableStringForForegroundColorSpan();
102
103   Text::SetSpannedText(textLabel, spannableString);
104
105   application.SendNotification();
106   application.Render();
107
108   Toolkit::Internal::TextLabel& labelImpl          = GetImpl(textLabel);
109   const Text::ColorIndex* const colorIndicesBuffer = labelImpl.GetTextController()->GetTextModel()->GetColorIndices();
110
111   CheckColorIndices(colorIndicesBuffer, 4u, {0u, 5u, 7u, 10u}, {0u, 1u, 1u, 0u});
112
113   END_TEST;
114 }
115
116 int UtcDaliToolkitTextEditorSetSpannedText(void)
117 {
118   ToolkitTestApplication application;
119   tet_infoline(" UtcDaliToolkitTextEditorSetSpannedText");
120
121   TextEditor textEditor = TextEditor::New();
122   DALI_TEST_CHECK(textEditor);
123   application.GetScene().Add(textEditor);
124
125   Text::SpannableString spannableString = CreateSpannableStringForForegroundColorSpan();
126
127   Text::SetSpannedText(textEditor, spannableString);
128
129   application.SendNotification();
130   application.Render();
131
132   Toolkit::Internal::TextEditor& labelImpl          = GetImpl(textEditor);
133   const Text::ColorIndex* const  colorIndicesBuffer = labelImpl.GetTextController()->GetTextModel()->GetColorIndices();
134
135   CheckColorIndices(colorIndicesBuffer, 4u, {0u, 5u, 7u, 10u}, {0u, 1u, 1u, 0u});
136
137   END_TEST;
138 }
139
140 int UtcDaliToolkitTextFieldSetSpannedText(void)
141 {
142   ToolkitTestApplication application;
143   tet_infoline(" UtcDaliToolkitTextFieldSetSpannedText");
144
145   TextField textField = TextField::New();
146   DALI_TEST_CHECK(textField);
147   application.GetScene().Add(textField);
148
149   Text::SpannableString spannableString = CreateSpannableStringForForegroundColorSpan();
150
151   Text::SetSpannedText(textField, spannableString);
152
153   application.SendNotification();
154   application.Render();
155
156   Toolkit::Internal::TextField& labelImpl          = GetImpl(textField);
157   const Text::ColorIndex* const colorIndicesBuffer = labelImpl.GetTextController()->GetTextModel()->GetColorIndices();
158
159   CheckColorIndices(colorIndicesBuffer, 4u, {0u, 5u, 7u, 10u}, {0u, 1u, 1u, 0u});
160
161   END_TEST;
162 }
163
164 int UtcDaliToolkitTextLabelSetSpannedText_FontSpan(void)
165 {
166   ToolkitTestApplication application;
167   tet_infoline(" UtcDaliToolkitTextLabelSetSpannedText_FontSpan ");
168
169   // Load some fonts to get the same metrics on different platforms.
170   TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
171   fontClient.SetDpi(96u, 96u);
172
173   char*             pathNamePtr = get_current_dir_name();
174   const std::string pathName(pathNamePtr);
175   free(pathNamePtr);
176
177   fontClient.GetFontId(pathName + DEFAULT_FONT_DIR + "/tizen/TizenSansRegular.ttf");
178
179   TextLabel textLabel = TextLabel::New();
180   DALI_TEST_CHECK(textLabel);
181   application.GetScene().Add(textLabel);
182
183   Text::SpannableString spannableString = CreateSpannableStringForFontSpan();
184
185   Text::SetSpannedText(textLabel, spannableString);
186
187   application.SendNotification();
188   application.Render();
189
190   Toolkit::Internal::TextLabel&               labelImpl     = GetImpl(textLabel);
191   const Vector<Dali::Toolkit::Text::FontRun>& validFontRuns = labelImpl.GetTextController()->GetTextModel()->GetFontRuns();
192
193   DALI_TEST_EQUALS(validFontRuns.Count(), 3u, TEST_LOCATION);
194
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);
197
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);
202
203   TextAbstraction::FontDescription spanFontDescription;
204
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);
208
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;
212
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);
218
219   END_TEST;
220 }
221
222 int UtcDaliTextModelIsSpannedTextPlaced(void)
223
224 {
225   tet_infoline(" UtcDaliTextModelIsSpannedTextPlaced");
226
227   ToolkitTestApplication application;
228
229   // Create spanned-text and set it
230   Text::SpannableString spannedText = Text::SpannableString::New("Hello مرحبا");
231   DALI_TEST_CHECK(spannedText);
232
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();
236
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);
240
241   // Tests the view model has been created.
242   Dali::Toolkit::Text::ViewModel* viewModelEditor = typesetterEditor->GetViewModel();
243   DALI_TEST_CHECK(viewModelEditor);
244
245   // Configures the text controller similarly to the text-editor.
246   Dali::Toolkit::Text::ConfigureTextEditor(controllerTextEditor);
247
248   DALI_TEST_EQUALS(false, modelEditor->IsSpannedTextPlaced(), TEST_LOCATION);
249   DALI_TEST_EQUALS(false, viewModelEditor->IsSpannedTextPlaced(), TEST_LOCATION);
250
251   controllerTextEditor->SetSpannedText(spannedText);
252
253   DALI_TEST_EQUALS(true, modelEditor->IsSpannedTextPlaced(), TEST_LOCATION);
254   DALI_TEST_EQUALS(true, viewModelEditor->IsSpannedTextPlaced(), TEST_LOCATION);
255
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();
259
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);
263
264   // Tests the view model has been created.
265   Dali::Toolkit::Text::ViewModel* viewModelLabel = typesetterLabel->GetViewModel();
266   DALI_TEST_CHECK(viewModelLabel);
267
268   // Configures the text controller similarly to the text-label.
269   Dali::Toolkit::Text::ConfigureTextLabel(controllerTextLabel);
270
271   DALI_TEST_EQUALS(false, modelLabel->IsSpannedTextPlaced(), TEST_LOCATION);
272   DALI_TEST_EQUALS(false, viewModelLabel->IsSpannedTextPlaced(), TEST_LOCATION);
273
274   controllerTextLabel->SetSpannedText(spannedText);
275
276   DALI_TEST_EQUALS(true, modelLabel->IsSpannedTextPlaced(), TEST_LOCATION);
277   DALI_TEST_EQUALS(true, viewModelLabel->IsSpannedTextPlaced(), TEST_LOCATION);
278
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();
282
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);
286
287   // Tests the view model has been created.
288   Dali::Toolkit::Text::ViewModel* viewModelField = typesetterField->GetViewModel();
289   DALI_TEST_CHECK(viewModelField);
290
291   // Configures the text controller similarly to the text-field.
292   Dali::Toolkit::Text::ConfigureTextField(controllerTextField);
293
294   DALI_TEST_EQUALS(false, modelField->IsSpannedTextPlaced(), TEST_LOCATION);
295   DALI_TEST_EQUALS(false, viewModelField->IsSpannedTextPlaced(), TEST_LOCATION);
296
297   controllerTextField->SetSpannedText(spannedText);
298
299   DALI_TEST_EQUALS(true, modelField->IsSpannedTextPlaced(), TEST_LOCATION);
300   DALI_TEST_EQUALS(true, viewModelField->IsSpannedTextPlaced(), TEST_LOCATION);
301
302   tet_result(TET_PASS);
303
304   END_TEST;
305 }