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