Merge "fix issue in negative line spacing with key arrow down" into devel/master
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-internal / utc-Dali-Text-Markup.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 <iostream>
19
20 #include <stdlib.h>
21 #include <limits>
22
23 #include <dali-toolkit-test-suite-utils.h>
24 #include <dali-toolkit/dali-toolkit.h>
25 #include <dali-toolkit/internal/text/color-run.h>
26 #include <dali-toolkit/internal/text/font-description-run.h>
27 #include <dali-toolkit/internal/text/markup-processor-helper-functions.h>
28 #include <dali-toolkit/internal/text/markup-processor.h>
29 #include <dali-toolkit/internal/text/text-definitions.h>
30 #include <dali-toolkit/internal/text/text-io.h>
31 #include <toolkit-text-utils.h>
32
33 using namespace Dali;
34 using namespace Toolkit;
35 using namespace Text;
36
37 namespace
38 {
39 ///////////////////////////////////////////////////////////
40
41 struct TokenComparisonData
42 {
43   std::string description;
44   std::string string1; ///< must be in lower case!!!!
45   std::string string2;
46   bool        expectedResult;
47 };
48
49 bool TokenComparisonTest(const TokenComparisonData& data)
50 {
51   std::cout << "  testing " << data.description << std::endl;
52
53   const bool result = TokenComparison(data.string1,
54                                       data.string2.c_str(),
55                                       data.string2.size());
56
57   if(result != data.expectedResult)
58   {
59     std::cout << "  different conparison result : " << result << ", expected : " << data.expectedResult << std::endl;
60     std::cout << "  comparing : [" << data.string1 << "] and [" << data.string2 << "]" << std::endl;
61
62     return false;
63   }
64
65   return true;
66 }
67
68 ///////////////////////////////////////////////////////////
69
70 struct ColorStringToVector4Data
71 {
72   std::string description;
73   std::string colorStr;
74   Vector4     expectedColor;
75 };
76
77 bool ColorStringToVector4Test(const ColorStringToVector4Data& data)
78 {
79   std::cout << "  testing " << data.description << std::endl;
80
81   Vector4 color;
82   ColorStringToVector4(data.colorStr.c_str(), data.colorStr.size(), color);
83
84   if(color != data.expectedColor)
85   {
86     std::cout << "  different color : " << color << ", expected : " << data.expectedColor << std::endl;
87     return false;
88   }
89
90   return true;
91 }
92
93 ///////////////////////////////////////////////////////////
94
95 struct Vector4ToColorStringData
96 {
97   std::string description;
98   Vector4     color;
99   std::string expectedColorStr;
100 };
101
102 bool Vector4ToColorStringTest(const Vector4ToColorStringData& data)
103 {
104   std::cout << "  testing " << data.description << std::endl;
105
106   std::string colorStr;
107   Vector4ToColorString(data.color, colorStr);
108
109   if(colorStr != data.expectedColorStr)
110   {
111     std::cout << "  different color : [" << colorStr << "], expected : [" << data.expectedColorStr << "]" << std::endl;
112     return false;
113   }
114
115   return true;
116 }
117
118 ///////////////////////////////////////////////////////////
119
120 struct StringToVector2Data
121 {
122   std::string description;
123   std::string vector2Str;
124   Vector2     expectedVector2;
125 };
126
127 bool StringToVector2Test(const StringToVector2Data& data)
128 {
129   std::cout << "  testing " << data.description << std::endl;
130
131   Vector2 vector2;
132   StringToVector2(data.vector2Str.c_str(), data.vector2Str.size(), vector2);
133
134   if(vector2 != data.expectedVector2)
135   {
136     std::cout << "  different vector2 : " << vector2 << ", expected : " << data.expectedVector2 << std::endl;
137     return false;
138   }
139
140   return true;
141 }
142
143 ///////////////////////////////////////////////////////////
144
145 struct Vector2ToStringData
146 {
147   std::string description;
148   Vector2     vector2;
149   std::string expectedVector2Str;
150 };
151
152 bool Vector2ToStringTest(const Vector2ToStringData& data)
153 {
154   std::cout << "  testing " << data.description << std::endl;
155
156   std::string vector2Str;
157   Vector2ToString(data.vector2, vector2Str);
158
159   if(vector2Str != data.expectedVector2Str)
160   {
161     std::cout << "  different vector2 : [" << vector2Str << "], expected : [" << data.expectedVector2Str << "]" << std::endl;
162     return false;
163   }
164
165   return true;
166 }
167
168 ///////////////////////////////////////////////////////////
169
170 struct XHTMLEntityToUTF8Data
171 {
172   std::string description;
173   std::string xHTMLEntityString;
174   std::string expectedString;
175 };
176
177 bool XHTMLEntityToUTF8Test(const XHTMLEntityToUTF8Data& data)
178 {
179   std::cout << "  testing " << data.description << std::endl;
180
181   Vector<ColorRun>                     colorRuns;
182   Vector<FontDescriptionRun>           fontRuns;
183   Vector<EmbeddedItem>                 items;
184   Vector<Anchor>                       anchors;
185   Vector<UnderlinedCharacterRun>       underlinedCharacterRuns;
186   Vector<ColorRun>                     backgroundColorRuns;
187   Vector<StrikethroughCharacterRun>    strikethroughCharacterRuns;
188   Vector<BoundedParagraphRun>          boundedParagraphRuns;
189   Vector<CharacterSpacingCharacterRun> characterSpacingCharacterRuns;
190   MarkupProcessData                    markupProcessData(colorRuns, fontRuns, items, anchors, underlinedCharacterRuns, backgroundColorRuns, strikethroughCharacterRuns, boundedParagraphRuns, characterSpacingCharacterRuns);
191   ProcessMarkupString(data.xHTMLEntityString, markupProcessData);
192
193   for(Vector<EmbeddedItem>::Iterator it    = items.Begin(),
194                                      endIt = items.End();
195       it != endIt;
196       ++it)
197   {
198     EmbeddedItem& item = *it;
199     delete[] item.url;
200   }
201   items.Clear();
202
203   if(markupProcessData.markupProcessedText != data.expectedString)
204   {
205     std::cout << "  different output string : " << markupProcessData.markupProcessedText << ", expected : " << data.expectedString << " " << std::endl;
206     return false;
207   }
208
209   return true;
210 }
211
212 } // namespace
213
214 int UtcDaliTextTokenComparison(void)
215 {
216   tet_infoline(" UtcDaliTextTokenComparison");
217
218   const TokenComparisonData data[] =
219     {
220       {"void texts",
221        "",
222        "",
223        true},
224       {"different size text",
225        "hello",
226        "world!",
227        false},
228       {"different texts",
229        "hello",
230        "world",
231        false},
232       {"same texts",
233        "world",
234        "wOrLD",
235        true},
236       {"some punctuation characters, numbers, ...",
237        "hello0123456789.![?]",
238        "Hello0123456789.![?]",
239        true}
240
241     };
242   const unsigned int numberOfTests = 5u;
243
244   for(unsigned int index = 0u; index < numberOfTests; ++index)
245   {
246     ToolkitTestApplication application;
247     if(!TokenComparisonTest(data[index]))
248     {
249       tet_result(TET_FAIL);
250     }
251   }
252
253   tet_result(TET_PASS);
254   END_TEST;
255 }
256
257 int UtcDaliTextColorStringToVector4(void)
258 {
259   tet_infoline(" UtcDaliTextColorStringToVector4");
260
261   const ColorStringToVector4Data data[] =
262     {
263       {"black string",
264        "bLack",
265        Color::BLACK},
266       {"white string",
267        "White",
268        Color::WHITE},
269       {"red string",
270        "reD",
271        Color::RED},
272       {"green string",
273        "green",
274        Color::GREEN},
275       {"blue string",
276        "blue",
277        Color::BLUE},
278       {"yellow string",
279        "yeLloW",
280        Color::YELLOW},
281       {"magenta string",
282        "MagEnta",
283        Color::MAGENTA},
284       {"cyan string",
285        "CyaN",
286        Color::CYAN},
287       {"transparent string",
288        "transparent",
289        Color::TRANSPARENT},
290       {"3 component web color",
291        "#F00",
292        Color::RED},
293       {"6 component web color",
294        "#fF0000",
295        Color::RED},
296       {"hex color red (ARGB)",
297        "0xffff0000",
298        Color::RED},
299       {"hex color green (ARGB)",
300        "0xFf00FF00",
301        Color::GREEN},
302       {"undefined color",
303        "undefined",
304        Vector4::ZERO},
305     };
306   const unsigned int numberOfTests = 14u;
307
308   for(unsigned int index = 0u; index < numberOfTests; ++index)
309   {
310     ToolkitTestApplication application;
311     if(!ColorStringToVector4Test(data[index]))
312     {
313       tet_result(TET_FAIL);
314     }
315   }
316
317   tet_result(TET_PASS);
318   END_TEST;
319 }
320
321 int UtcDaliTextVector4ToColorString(void)
322 {
323   tet_infoline(" UtcDaliTextVector4ToColorString");
324
325   const Vector4ToColorStringData data[] =
326     {
327       {"black color",
328        Color::BLACK,
329        "black"},
330       {"white string",
331        Color::WHITE,
332        "white"},
333       {"red string",
334        Color::RED,
335        "red"},
336       {"green string",
337        Color::GREEN,
338        "green"},
339       {"blue string",
340        Color::BLUE,
341        "blue"},
342       {"yellow string",
343        Color::YELLOW,
344        "yellow"},
345       {
346         "magenta string",
347         Color::MAGENTA,
348         "magenta",
349       },
350       {"cyan string",
351        Color::CYAN,
352        "cyan"},
353       {"transparent string",
354        Color::TRANSPARENT,
355        "transparent"},
356       {"hex color",
357        Vector4(0.4f, 0.5f, 0.6f, 1.f),
358        "0xff667f99"},
359     };
360   const unsigned int numberOfTests = 10u;
361
362   for(unsigned int index = 0u; index < numberOfTests; ++index)
363   {
364     ToolkitTestApplication application;
365     if(!Vector4ToColorStringTest(data[index]))
366     {
367       tet_result(TET_FAIL);
368     }
369   }
370
371   tet_result(TET_PASS);
372   END_TEST;
373 }
374
375 int UtcDaliTextStringToVector2(void)
376 {
377   tet_infoline(" UtcDaliTextStringToVector2");
378   const StringToVector2Data data[] =
379     {
380       {"void text",
381        "",
382        Vector2::ZERO},
383       {"zero zero",
384        "0 0",
385        Vector2::ZERO},
386       {"five four",
387        "5 4",
388        Vector2(5.f, 4.f)}};
389   const unsigned int numberOfTests = 3u;
390
391   for(unsigned int index = 0u; index < numberOfTests; ++index)
392   {
393     ToolkitTestApplication application;
394     if(!StringToVector2Test(data[index]))
395     {
396       tet_result(TET_FAIL);
397     }
398   }
399
400   tet_result(TET_PASS);
401   END_TEST;
402 }
403
404 int UtcDaliTextVector2ToString(void)
405 {
406   tet_infoline(" UtcDaliTextVector2ToString");
407   const Vector2ToStringData data[] =
408     {
409       {
410         "zero zero",
411         Vector2::ZERO,
412         "0 0",
413       },
414       {
415         "five four",
416         Vector2(5.f, 4.f),
417         "5 4",
418       }};
419   const unsigned int numberOfTests = 2u;
420
421   for(unsigned int index = 0u; index < numberOfTests; ++index)
422   {
423     ToolkitTestApplication application;
424     if(!Vector2ToStringTest(data[index]))
425     {
426       tet_result(TET_FAIL);
427     }
428   }
429
430   tet_result(TET_PASS);
431   END_TEST;
432 }
433
434 int UtcDaliTextXHTMLEntityToUTF8(void)
435 {
436   tet_infoline(" UtcDaliTextXHTMLEntityToUTF8");
437   const XHTMLEntityToUTF8Data data[] =
438     {
439       {"Text Without XHTML Entity",
440        "Checking XHTML Entitities",
441        "Checking XHTML Entitities"},
442       {"Text With XHTML Entity in Numeric form",
443        "Checking Numeric Entitities &#x26; &#x27; &#x3C; &#x3E; &#xA1; &#xA2; &#xA3; &#xA4; &#xA5; &#xA6; &#xA7; &#xA8; &#xA9; &#xAA; &#xAB; &#xAC; &#xAD; &#xAE; &#xAF; &#xB0; &#xB1; &#xB2; &#xB3; &#xB4; &#xB5; &#xB6; &#xB7; &#xB8; &#xB9; &#xBA; &#xBB; &#xBC; &#xBD; &#xBE; &#xBF; &#xC0; &#xC1; &#xC2; &#xC3; &#xC4; &#xC5; &#xE6; &#xC7; &#xC8; &#xC9; &#xCA; &#xCB; &#xCC; &#xCD; &#xCE; &#xCF; &#xF0; &#xD1; &#xD2; &#xD3; &#xD4; &#xD5; &#xD6; &#xD7; &#xD8; &#xD9; &#xDA; &#xDB; &#xDD; &#xFE; &#xDF; &#xE0; &#xE1; &#xE2; &#xE3; &#xE4; &#xE5; &#xE6; &#xE7; &#xE8; &#xE9; &#xEA; &#xEB; &#xEC; &#xED; &#xEE; &#xEF; &#xF0; &#xF1; &#xF2; &#xF3; &#xF4; &#xF5; &#xF6; &#xF7; &#xF8; &#xF9; &#xFA; &#xFB; &#xFC; &#xFD; &#xFE; &#xFF; &#x3B1; &#x3B2; &#x3B3; &#x3B4; &#x3B5; &#x3B6; &#x3B7; &#x3B8; &#x3B9; &#x3BA; &#x3BB; &#x3BC; &#x3BD; &#x3BE; &#x3BF; &#x3C0; &#x3C1; &#x3C3; &#x3C4; &#x3C5; &#x3C6; &#x3C7; &#x3C8; &#x3C9; &#x2026; &#x20AC; &#x2190; &#x2191; &#x2192; &#x2193; &#x2194; &#x2190; &#x2192; &#x2200; &#x2203; &#x2207; &#x220F; &#x2211; &#x2227; &#x2228; &#x222B; &#x2260; &#x2261; &#x2295; &#x22A5; &#x2020; &#x2021; &#x2022; ",
444        "Checking Numeric Entitities & ' < > ¡ ¢ £ ¤ ¥ ¦ § ¨ © ª « ¬ ­ ® ¯ ° ± ² ³ ´ µ ¶ · ¸ ¹ º » ¼ ½ ¾ ¿ À Á Â Ã Ä Å æ Ç È É Ê Ë Ì Í Î Ï ð Ñ Ò Ó Ô Õ Ö × Ø Ù Ú Û Ý þ ß à á â ã ä å æ ç è é ê ë ì í î ï ð ñ ò ó ô õ ö ÷ ø ù ú û ü ý þ ÿ α β γ δ ε ζ η θ ι κ λ μ ν ξ ο π ρ σ τ υ φ χ ψ ω … € ← ↑ → ↓ ↔ ← → ∀ ∃ ∇ ∏ ∑ ∧ ∨ ∫ ≠ ≡ ⊕ ⊥ † ‡ • "},
445       {"Text With XHTML Named Entities",
446        "Checking Named Entitities &amp; &apos; &lt; &gt; &iexcl; &cent; &pound; &curren; &yen; &brvbar; &sect; &uml; &copy; &ordf; &laquo; &not; &shy; &reg; &macr; &deg; &plusmn; &sup2; &sup3; &acute; &micro; &para; &middot; &cedil; &sup1; &ordm; &raquo; &frac14; &frac12; &frac34; &iquest; &Agrave; &Aacute; &Acirc; &Atilde; &Auml; &Aring; &aelig; &Ccedil; &Egrave; &Eacute; &Ecirc; &Euml; &Igrave; &Iacute; &Icirc; &Iuml; &eth; &Ntilde; &Ograve; &Oacute; &Ocirc; &Otilde; &Ouml; &times; &Oslash; &Ugrave; &Uacute; &Ucirc; &Yacute; &thorn; &szlig; &agrave; &aacute; &acirc; &atilde; &auml; &aring; &aelig; &ccedil; &egrave; &eacute; &ecirc; &euml; &igrave; &iacute; &icirc; &iuml; &eth; &ntilde; &ograve; &oacute; &ocirc; &otilde; &ouml; &divide; &oslash; &ugrave; &uacute; &ucirc; &uuml; &yacute; &thorn; &yuml; &alpha; &beta; &gamma; &delta; &epsilon; &zeta; &eta; &theta; &iota; &kappa; &lambda; &mu; &nu; &xi; &omicron; &pi; &rho; &sigma; &tau; &upsilon; &phi; &chi; &psi; &omega; &hellip; &euro; &larr; &uarr; &rarr; &darr; &harr; &larr; &rarr; &forall; &exist; &nabla; &prod; &sum; &and; &or; &int; &ne; &equiv; &oplus; &perp; &dagger; &Dagger; &bull; ",
447        "Checking Named Entitities & ' < > ¡ ¢ £ ¤ ¥ ¦ § ¨ © ª « ¬ ­ ® ¯ ° ± ² ³ ´ µ ¶ · ¸ ¹ º » ¼ ½ ¾ ¿ À Á Â Ã Ä Å æ Ç È É Ê Ë Ì Í Î Ï ð Ñ Ò Ó Ô Õ Ö × Ø Ù Ú Û Ý þ ß à á â ã ä å æ ç è é ê ë ì í î ï ð ñ ò ó ô õ ö ÷ ø ù ú û ü ý þ ÿ α β γ δ ε ζ η θ ι κ λ μ ν ξ ο π ρ σ τ υ φ χ ψ ω … € ← ↑ → ↓ ↔ ← → ∀ ∃ ∇ ∏ ∑ ∧ ∨ ∫ ≠ ≡ ⊕ ⊥ † ‡ • "},
448       {"Testing of < special character",
449        "Testing of < special character",
450        "Testing of "},
451       {"Testing of & special character",
452        "Testing of & special character",
453        "Testing of "},
454       {"Testing of & < > special character",
455        "Testing of \\& \\< \\> special character",
456        "Testing of & < > special character"}};
457   const unsigned int numberOfTests = 6u;
458
459   for(unsigned int index = 0u; index < numberOfTests; ++index)
460   {
461     ToolkitTestApplication application;
462     if(!XHTMLEntityToUTF8Test(data[index]))
463     {
464       tet_result(TET_FAIL);
465     }
466   }
467
468   tet_result(TET_PASS);
469   END_TEST;
470 }