fix issue in negative line spacing with key arrow down
[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   MarkupProcessData                 markupProcessData(colorRuns, fontRuns, items, anchors, underlinedCharacterRuns, backgroundColorRuns, strikethroughCharacterRuns, boundedParagraphRuns);
190   ProcessMarkupString(data.xHTMLEntityString, markupProcessData);
191
192   for(Vector<EmbeddedItem>::Iterator it    = items.Begin(),
193                                      endIt = items.End();
194       it != endIt;
195       ++it)
196   {
197     EmbeddedItem& item = *it;
198     delete[] item.url;
199   }
200   items.Clear();
201
202   if(markupProcessData.markupProcessedText != data.expectedString)
203   {
204     std::cout << "  different output string : " << markupProcessData.markupProcessedText << ", expected : " << data.expectedString << " " << std::endl;
205     return false;
206   }
207
208   return true;
209 }
210
211 } // namespace
212
213 int UtcDaliTextTokenComparison(void)
214 {
215   tet_infoline(" UtcDaliTextTokenComparison");
216
217   const TokenComparisonData data[] =
218     {
219       {"void texts",
220        "",
221        "",
222        true},
223       {"different size text",
224        "hello",
225        "world!",
226        false},
227       {"different texts",
228        "hello",
229        "world",
230        false},
231       {"same texts",
232        "world",
233        "wOrLD",
234        true},
235       {"some punctuation characters, numbers, ...",
236        "hello0123456789.![?]",
237        "Hello0123456789.![?]",
238        true}
239
240     };
241   const unsigned int numberOfTests = 5u;
242
243   for(unsigned int index = 0u; index < numberOfTests; ++index)
244   {
245     ToolkitTestApplication application;
246     if(!TokenComparisonTest(data[index]))
247     {
248       tet_result(TET_FAIL);
249     }
250   }
251
252   tet_result(TET_PASS);
253   END_TEST;
254 }
255
256 int UtcDaliTextColorStringToVector4(void)
257 {
258   tet_infoline(" UtcDaliTextColorStringToVector4");
259
260   const ColorStringToVector4Data data[] =
261     {
262       {"black string",
263        "bLack",
264        Color::BLACK},
265       {"white string",
266        "White",
267        Color::WHITE},
268       {"red string",
269        "reD",
270        Color::RED},
271       {"green string",
272        "green",
273        Color::GREEN},
274       {"blue string",
275        "blue",
276        Color::BLUE},
277       {"yellow string",
278        "yeLloW",
279        Color::YELLOW},
280       {"magenta string",
281        "MagEnta",
282        Color::MAGENTA},
283       {"cyan string",
284        "CyaN",
285        Color::CYAN},
286       {"transparent string",
287        "transparent",
288        Color::TRANSPARENT},
289       {"3 component web color",
290        "#F00",
291        Color::RED},
292       {"6 component web color",
293        "#fF0000",
294        Color::RED},
295       {"hex color red (ARGB)",
296        "0xffff0000",
297        Color::RED},
298       {"hex color green (ARGB)",
299        "0xFf00FF00",
300        Color::GREEN},
301       {"undefined color",
302        "undefined",
303        Vector4::ZERO},
304     };
305   const unsigned int numberOfTests = 14u;
306
307   for(unsigned int index = 0u; index < numberOfTests; ++index)
308   {
309     ToolkitTestApplication application;
310     if(!ColorStringToVector4Test(data[index]))
311     {
312       tet_result(TET_FAIL);
313     }
314   }
315
316   tet_result(TET_PASS);
317   END_TEST;
318 }
319
320 int UtcDaliTextVector4ToColorString(void)
321 {
322   tet_infoline(" UtcDaliTextVector4ToColorString");
323
324   const Vector4ToColorStringData data[] =
325     {
326       {"black color",
327        Color::BLACK,
328        "black"},
329       {"white string",
330        Color::WHITE,
331        "white"},
332       {"red string",
333        Color::RED,
334        "red"},
335       {"green string",
336        Color::GREEN,
337        "green"},
338       {"blue string",
339        Color::BLUE,
340        "blue"},
341       {"yellow string",
342        Color::YELLOW,
343        "yellow"},
344       {
345         "magenta string",
346         Color::MAGENTA,
347         "magenta",
348       },
349       {"cyan string",
350        Color::CYAN,
351        "cyan"},
352       {"transparent string",
353        Color::TRANSPARENT,
354        "transparent"},
355       {"hex color",
356        Vector4(0.4f, 0.5f, 0.6f, 1.f),
357        "0xff667f99"},
358     };
359   const unsigned int numberOfTests = 10u;
360
361   for(unsigned int index = 0u; index < numberOfTests; ++index)
362   {
363     ToolkitTestApplication application;
364     if(!Vector4ToColorStringTest(data[index]))
365     {
366       tet_result(TET_FAIL);
367     }
368   }
369
370   tet_result(TET_PASS);
371   END_TEST;
372 }
373
374 int UtcDaliTextStringToVector2(void)
375 {
376   tet_infoline(" UtcDaliTextStringToVector2");
377   const StringToVector2Data data[] =
378     {
379       {"void text",
380        "",
381        Vector2::ZERO},
382       {"zero zero",
383        "0 0",
384        Vector2::ZERO},
385       {"five four",
386        "5 4",
387        Vector2(5.f, 4.f)}};
388   const unsigned int numberOfTests = 3u;
389
390   for(unsigned int index = 0u; index < numberOfTests; ++index)
391   {
392     ToolkitTestApplication application;
393     if(!StringToVector2Test(data[index]))
394     {
395       tet_result(TET_FAIL);
396     }
397   }
398
399   tet_result(TET_PASS);
400   END_TEST;
401 }
402
403 int UtcDaliTextVector2ToString(void)
404 {
405   tet_infoline(" UtcDaliTextVector2ToString");
406   const Vector2ToStringData data[] =
407     {
408       {
409         "zero zero",
410         Vector2::ZERO,
411         "0 0",
412       },
413       {
414         "five four",
415         Vector2(5.f, 4.f),
416         "5 4",
417       }};
418   const unsigned int numberOfTests = 2u;
419
420   for(unsigned int index = 0u; index < numberOfTests; ++index)
421   {
422     ToolkitTestApplication application;
423     if(!Vector2ToStringTest(data[index]))
424     {
425       tet_result(TET_FAIL);
426     }
427   }
428
429   tet_result(TET_PASS);
430   END_TEST;
431 }
432
433 int UtcDaliTextXHTMLEntityToUTF8(void)
434 {
435   tet_infoline(" UtcDaliTextXHTMLEntityToUTF8");
436   const XHTMLEntityToUTF8Data data[] =
437     {
438       {"Text Without XHTML Entity",
439        "Checking XHTML Entitities",
440        "Checking XHTML Entitities"},
441       {"Text With XHTML Entity in Numeric form",
442        "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; ",
443        "Checking Numeric Entitities & ' < > ¡ ¢ £ ¤ ¥ ¦ § ¨ © ª « ¬ ­ ® ¯ ° ± ² ³ ´ µ ¶ · ¸ ¹ º » ¼ ½ ¾ ¿ À Á Â Ã Ä Å æ Ç È É Ê Ë Ì Í Î Ï ð Ñ Ò Ó Ô Õ Ö × Ø Ù Ú Û Ý þ ß à á â ã ä å æ ç è é ê ë ì í î ï ð ñ ò ó ô õ ö ÷ ø ù ú û ü ý þ ÿ α β γ δ ε ζ η θ ι κ λ μ ν ξ ο π ρ σ τ υ φ χ ψ ω … € ← ↑ → ↓ ↔ ← → ∀ ∃ ∇ ∏ ∑ ∧ ∨ ∫ ≠ ≡ ⊕ ⊥ † ‡ • "},
444       {"Text With XHTML Named Entities",
445        "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; ",
446        "Checking Named Entitities & ' < > ¡ ¢ £ ¤ ¥ ¦ § ¨ © ª « ¬ ­ ® ¯ ° ± ² ³ ´ µ ¶ · ¸ ¹ º » ¼ ½ ¾ ¿ À Á Â Ã Ä Å æ Ç È É Ê Ë Ì Í Î Ï ð Ñ Ò Ó Ô Õ Ö × Ø Ù Ú Û Ý þ ß à á â ã ä å æ ç è é ê ë ì í î ï ð ñ ò ó ô õ ö ÷ ø ù ú û ü ý þ ÿ α β γ δ ε ζ η θ ι κ λ μ ν ξ ο π ρ σ τ υ φ χ ψ ω … € ← ↑ → ↓ ↔ ← → ∀ ∃ ∇ ∏ ∑ ∧ ∨ ∫ ≠ ≡ ⊕ ⊥ † ‡ • "},
447       {"Testing of < special character",
448        "Testing of < special character",
449        "Testing of "},
450       {"Testing of & special character",
451        "Testing of & special character",
452        "Testing of "},
453       {"Testing of & < > special character",
454        "Testing of \\& \\< \\> special character",
455        "Testing of & < > special character"}};
456   const unsigned int numberOfTests = 6u;
457
458   for(unsigned int index = 0u; index < numberOfTests; ++index)
459   {
460     ToolkitTestApplication application;
461     if(!XHTMLEntityToUTF8Test(data[index]))
462     {
463       tet_result(TET_FAIL);
464     }
465   }
466
467   tet_result(TET_PASS);
468   END_TEST;
469 }