Merge "fix issue when strikethrough used without ending tag" into devel/master
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-internal / utc-Dali-TextLabel-internal.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 <iostream>
20
21 #include <dali-toolkit-test-suite-utils.h>
22 #include <dali-toolkit/dali-toolkit.h>
23
24 #include <dali-toolkit/internal/controls/text-controls/text-label-impl.h>
25 #include <dali-toolkit/internal/text/rendering/text-typesetter.h>
26 #include <dali-toolkit/internal/text/rendering/view-model.h>
27 #include <dali-toolkit/internal/text/text-controller-impl.h>
28 #include <dali-toolkit/internal/text/text-controller.h>
29
30 using namespace Dali;
31 using namespace Toolkit;
32 using namespace Text;
33
34 int UtcDaliTextLabelMarkupUnderline(void)
35 {
36   ToolkitTestApplication application;
37   tet_infoline(" UtcDaliTextLabelMarkupUnderline ");
38
39   TextLabel textLabel = TextLabel::New();
40
41   application.GetScene().Add(textLabel);
42
43   textLabel.SetProperty(TextLabel::Property::TEXT, "<u>ABC</u>EF<u>GH</u>");
44   textLabel.SetProperty(TextLabel ::Property::ENABLE_MARKUP, true);
45
46   application.SendNotification();
47   application.Render();
48
49   uint32_t expectedNumberOfUnderlinedGlyphs = 5u;
50
51   Toolkit::Internal::TextLabel& textLabelImpl         = GetImpl(textLabel);
52   const Text::Length            numberOfUnderlineRuns = textLabelImpl.GetTextController()->GetTextModel()->GetNumberOfUnderlineRuns();
53
54   DALI_TEST_EQUALS(numberOfUnderlineRuns, expectedNumberOfUnderlinedGlyphs, TEST_LOCATION);
55
56   Vector<GlyphRun> underlineRuns;
57   underlineRuns.Resize(numberOfUnderlineRuns);
58   textLabelImpl.GetTextController()->GetTextModel()->GetUnderlineRuns(underlineRuns.Begin(), 0u, numberOfUnderlineRuns);
59
60   //ABC are underlined
61   DALI_TEST_EQUALS(underlineRuns[0u].glyphIndex, 0u, TEST_LOCATION);
62   DALI_TEST_EQUALS(underlineRuns[1u].glyphIndex, 1u, TEST_LOCATION);
63   DALI_TEST_EQUALS(underlineRuns[2u].glyphIndex, 2u, TEST_LOCATION);
64
65   //GH are underlined
66   DALI_TEST_EQUALS(underlineRuns[3u].glyphIndex, 5u, TEST_LOCATION);
67   DALI_TEST_EQUALS(underlineRuns[4u].glyphIndex, 6u, TEST_LOCATION);
68
69   END_TEST;
70 }
71
72 int UtcDaliTextLabelBackgroundTag(void)
73 {
74   ToolkitTestApplication application;
75   tet_infoline("UtcDaliTextLabelBackgroundTag\n");
76
77   TextLabel label = TextLabel::New();
78   DALI_TEST_CHECK(label);
79
80   label.SetProperty(TextLabel ::Property::ENABLE_MARKUP, true);
81   label.SetProperty(TextLabel::Property::TEXT, "H<background color='red'>e</background> Worl<background color='yellow'>d</background>");
82   application.GetScene().Add(label);
83   application.SendNotification();
84   application.Render();
85
86   Toolkit::Internal::TextLabel& labelImpl                    = GetImpl(label);
87   const ColorIndex* const       backgroundColorIndicesBuffer = labelImpl.GetTextController()->GetTextModel()->GetBackgroundColorIndices();
88
89   DALI_TEST_CHECK(backgroundColorIndicesBuffer);
90
91   //default color
92   DALI_TEST_EQUALS(backgroundColorIndicesBuffer[0], 0u, TEST_LOCATION);
93
94   //red color
95   DALI_TEST_EQUALS(backgroundColorIndicesBuffer[1], 1u, TEST_LOCATION);
96
97   //yellow color
98   DALI_TEST_EQUALS(backgroundColorIndicesBuffer[7], 2u, TEST_LOCATION);
99
100   END_TEST;
101 }
102
103 int UtcDaliToolkitTextlabelEllipsisInternalAPIs(void)
104 {
105   ToolkitTestApplication application;
106   tet_infoline(" UtcDaliToolkitTextlabelEllipsisInternalAPIs ");
107   TextLabel textLabel = TextLabel::New();
108
109   Toolkit::Internal::TextLabel& textLabelImpl = GetImpl(textLabel);
110   const ModelInterface* const   textModel     = textLabelImpl.GetTextController()->GetTextModel();
111
112   tet_infoline(" UtcDaliToolkitTextlabelEllipsisInternalAPIs - ELLIPSIS Disabled");
113   textLabel.SetProperty(DevelTextLabel::Property::ELLIPSIS, false);
114   DALI_TEST_EQUALS(textLabel.GetProperty<bool>(DevelTextLabel::Property::ELLIPSIS), false, TEST_LOCATION);
115   DALI_TEST_CHECK(!(textModel->IsTextElideEnabled()));
116
117   tet_infoline(" UtcDaliToolkitTextlabelEllipsisInternalAPIs - ELLIPSIS Enabled");
118   textLabel.SetProperty(DevelTextLabel::Property::ELLIPSIS, true);
119   DALI_TEST_EQUALS(textLabel.GetProperty<bool>(DevelTextLabel::Property::ELLIPSIS), true, TEST_LOCATION);
120   DALI_TEST_CHECK(textModel->IsTextElideEnabled());
121
122   tet_infoline(" UtcDaliToolkitTextlabelEllipsisInternalAPIs - GetStartIndexOfElidedGlyphs Default");
123   DALI_TEST_EQUALS(textModel->GetStartIndexOfElidedGlyphs(), 0u, TEST_LOCATION);
124
125   tet_infoline(" UtcDaliToolkitTextlabelEllipsisInternalAPIs - GetEndIndexOfElidedGlyphs Default");
126   DALI_TEST_EQUALS(textModel->GetEndIndexOfElidedGlyphs(), 0u, TEST_LOCATION);
127
128   tet_infoline(" UtcDaliToolkitTextlabelEllipsisInternalAPIs - GetFirstMiddleIndexOfElidedGlyphs Default");
129   DALI_TEST_EQUALS(textModel->GetFirstMiddleIndexOfElidedGlyphs(), 0u, TEST_LOCATION);
130
131   tet_infoline(" UtcDaliToolkitTextlabelEllipsisInternalAPIs - GetSecondMiddleIndexOfElidedGlyphs Default");
132   DALI_TEST_EQUALS(textModel->GetSecondMiddleIndexOfElidedGlyphs(), 0u, TEST_LOCATION);
133
134   // Tests the rendering controller has been created.
135   TypesetterPtr typesetter = Typesetter::New(textModel);
136   DALI_TEST_CHECK(typesetter);
137
138   // Tests the view model has been created.
139   ViewModel* model = typesetter->GetViewModel();
140
141   tet_infoline(" UtcDaliToolkitTextlabelEllipsisInternalAPIs - IsTextElideEnabled ViewModel");
142   DALI_TEST_CHECK(model->IsTextElideEnabled());
143
144   tet_infoline(" UtcDaliToolkitTextlabelEllipsisInternalAPIs - GetStartIndexOfElidedGlyphs ViewModel");
145   DALI_TEST_EQUALS(model->GetStartIndexOfElidedGlyphs(), 0u, TEST_LOCATION);
146
147   tet_infoline(" UtcDaliToolkitTextlabelEllipsisInternalAPIs - GetEndIndexOfElidedGlyphs ViewModel");
148   DALI_TEST_EQUALS(model->GetEndIndexOfElidedGlyphs(), 0u, TEST_LOCATION);
149
150   tet_infoline(" UtcDaliToolkitTextlabelEllipsisInternalAPIs - GetFirstMiddleIndexOfElidedGlyphs ViewModel");
151   DALI_TEST_EQUALS(model->GetFirstMiddleIndexOfElidedGlyphs(), 0u, TEST_LOCATION);
152
153   tet_infoline(" UtcDaliToolkitTextlabelEllipsisInternalAPIs - GetSecondMiddleIndexOfElidedGlyphs ViewModel");
154   DALI_TEST_EQUALS(model->GetSecondMiddleIndexOfElidedGlyphs(), 0u, TEST_LOCATION);
155
156   END_TEST;
157 }
158 int UtcDaliTextLabelTextWithSpan(void)
159 {
160   ToolkitTestApplication application;
161   tet_infoline("UtcDaliTextLabelTextWithSpan\n");
162
163   TextLabel label = TextLabel::New();
164   DALI_TEST_CHECK(label);
165
166   label.SetProperty(TextLabel ::Property::ENABLE_MARKUP, true);
167   label.SetProperty(TextLabel::Property::TEXT, "Hello Span");
168   application.GetScene().Add(label);
169
170   application.SendNotification();
171   application.Render();
172
173   Vector3 originalSize = label.GetNaturalSize();
174   label.SetProperty(TextLabel::Property::TEXT, "H<span font-size='45' font-family='DejaVu Sans' font-width='condensed' font-slant='italic' text-color='red'>ello</span> Span");
175
176   application.SendNotification();
177   application.Render();
178
179   Vector3 spanSize = label.GetNaturalSize();
180
181   DALI_TEST_GREATER(spanSize.width, originalSize.width, TEST_LOCATION);
182
183   Toolkit::Internal::TextLabel& labelImpl           = GetImpl(label);
184   const ColorIndex* const       colorIndicesBuffer1 = labelImpl.GetTextController()->GetTextModel()->GetColorIndices();
185
186   DALI_TEST_CHECK(colorIndicesBuffer1);
187
188   //default color
189   DALI_TEST_EQUALS(colorIndicesBuffer1[0], 0u, TEST_LOCATION);
190
191   //span color
192   DALI_TEST_EQUALS(colorIndicesBuffer1[1], 1u, TEST_LOCATION);
193
194   //default color
195   DALI_TEST_EQUALS(colorIndicesBuffer1[6], 0u, TEST_LOCATION);
196
197   label.SetProperty(TextLabel::Property::TEXT, "<span font-size='45'>H</span>ello <span text-color='red'>S</span>pan");
198
199   application.SendNotification();
200   application.Render();
201
202   const ColorIndex* const colorIndicesBuffer2 = labelImpl.GetTextController()->GetTextModel()->GetColorIndices();
203
204   DALI_TEST_CHECK(colorIndicesBuffer2);
205
206   //default color
207   DALI_TEST_EQUALS(colorIndicesBuffer2[0], 0u, TEST_LOCATION);
208
209   //default color
210   DALI_TEST_EQUALS(colorIndicesBuffer2[1], 0u, TEST_LOCATION);
211
212   //span color
213   DALI_TEST_EQUALS(colorIndicesBuffer2[6], 1u, TEST_LOCATION);
214
215   //default color
216   DALI_TEST_EQUALS(colorIndicesBuffer2[7], 0u, TEST_LOCATION);
217
218   END_TEST;
219 }
220
221 int UtcDaliTextLabelMarkupStrikethrough(void)
222 {
223   ToolkitTestApplication application;
224   tet_infoline(" UtcDaliTextLabelMarkupStrikethrough ");
225
226   TextLabel textLabel = TextLabel::New();
227
228   application.GetScene().Add(textLabel);
229
230   textLabel.SetProperty(TextLabel::Property::TEXT, "<s color='red'>ABC</s>EF<s color='green'>GH</s>");
231   textLabel.SetProperty(TextLabel ::Property::ENABLE_MARKUP, true);
232
233   application.SendNotification();
234   application.Render();
235
236   uint32_t expectedNumberOfStrikethroughGlyphs = 2u;
237
238   Toolkit::Internal::TextLabel& textLabelImpl             = GetImpl(textLabel);
239   const Text::Length            numberOfStrikethroughRuns = textLabelImpl.GetTextController()->GetTextModel()->GetNumberOfStrikethroughRuns();
240
241   DALI_TEST_EQUALS(numberOfStrikethroughRuns, expectedNumberOfStrikethroughGlyphs, TEST_LOCATION);
242
243   Vector<StrikethroughGlyphRun> strikethroughRuns;
244   strikethroughRuns.Resize(numberOfStrikethroughRuns);
245   textLabelImpl.GetTextController()->GetTextModel()->GetStrikethroughRuns(strikethroughRuns.Begin(), 0u, numberOfStrikethroughRuns);
246
247   //ABC have strikethrough
248   DALI_TEST_EQUALS(strikethroughRuns[0u].glyphRun.glyphIndex, 0u, TEST_LOCATION);
249   DALI_TEST_EQUALS(strikethroughRuns[0u].glyphRun.numberOfGlyphs, 3u, TEST_LOCATION);
250   DALI_TEST_CHECK(strikethroughRuns[0u].isColorSet);
251   DALI_TEST_EQUALS(strikethroughRuns[0u].color.r, 1u, TEST_LOCATION);
252   DALI_TEST_EQUALS(strikethroughRuns[0u].color.g, 0u, TEST_LOCATION);
253   DALI_TEST_EQUALS(strikethroughRuns[0u].color.b, 0u, TEST_LOCATION);
254
255   //GH have strikethrough
256   DALI_TEST_EQUALS(strikethroughRuns[1u].glyphRun.glyphIndex, 5u, TEST_LOCATION);
257   DALI_TEST_EQUALS(strikethroughRuns[1u].glyphRun.numberOfGlyphs, 2u, TEST_LOCATION);
258   DALI_TEST_CHECK(strikethroughRuns[1u].isColorSet);
259   DALI_TEST_EQUALS(strikethroughRuns[1u].color.r, 0u, TEST_LOCATION);
260   DALI_TEST_EQUALS(strikethroughRuns[1u].color.g, 1u, TEST_LOCATION);
261   DALI_TEST_EQUALS(strikethroughRuns[1u].color.b, 0u, TEST_LOCATION);
262
263   END_TEST;
264 }
265
266 int UtcDaliTextLabelMarkupStrikethroughNoEndTag(void)
267 {
268   ToolkitTestApplication application;
269   tet_infoline(" UtcDaliTextLabelMarkupStrikethroughNoEndTag ");
270
271   TextLabel textLabel = TextLabel::New();
272
273   application.GetScene().Add(textLabel);
274
275   textLabel.SetProperty(TextLabel::Property::TEXT, "<s>ABC");
276   textLabel.SetProperty(TextLabel ::Property::ENABLE_MARKUP, true);
277
278   application.SendNotification();
279   application.Render();
280
281   uint32_t expectedNumberOfStrikethroughGlyphs = 0u;
282
283   Toolkit::Internal::TextLabel& textLabelImpl             = GetImpl(textLabel);
284   Text::Length                  numberOfStrikethroughRuns = textLabelImpl.GetTextController()->GetTextModel()->GetNumberOfStrikethroughRuns();
285
286   DALI_TEST_EQUALS(numberOfStrikethroughRuns, expectedNumberOfStrikethroughGlyphs, TEST_LOCATION);
287
288   END_TEST;
289 }