Refactoring ImageVisualShaderFactory::GetShader
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-internal / utc-Dali-Text-Markup.cpp
1 /*
2  * Copyright (c) 2021 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 <toolkit-text-utils.h>
26 #include <dali-toolkit/internal/text/markup-processor.h>
27 #include <dali-toolkit/internal/text/markup-processor-helper-functions.h>
28 #include <dali-toolkit/internal/text/color-run.h>
29 #include <dali-toolkit/internal/text/font-description-run.h>
30 #include <dali-toolkit/internal/text/text-definitions.h>
31 #include <dali-toolkit/internal/text/text-io.h>
32
33 using namespace Dali;
34 using namespace Toolkit;
35 using namespace Text;
36
37 namespace
38 {
39
40   ///////////////////////////////////////////////////////////
41
42   struct TokenComparisonData
43   {
44     std::string description;
45     std::string string1; ///< must be in lower case!!!!
46     std::string string2;
47     bool expectedResult;
48   };
49
50   bool TokenComparisonTest( const TokenComparisonData& data )
51   {
52     std::cout << "  testing " << data.description << std::endl;
53
54     const bool result = TokenComparison( data.string1,
55                                          data.string2.c_str(),
56                                          data.string2.size() );
57
58     if( result != data.expectedResult )
59     {
60       std::cout << "  different conparison result : " << result << ", expected : " << data.expectedResult << std::endl;
61       std::cout << "  comparing : [" << data.string1 << "] and [" << data.string2 << "]" << std::endl;
62
63       return false;
64     }
65
66     return true;
67   }
68
69   ///////////////////////////////////////////////////////////
70
71   struct ColorStringToVector4Data
72   {
73     std::string description;
74     std::string colorStr;
75     Vector4 expectedColor;
76   };
77
78   bool ColorStringToVector4Test( const ColorStringToVector4Data& data )
79   {
80     std::cout << "  testing " << data.description << std::endl;
81
82     Vector4 color;
83     ColorStringToVector4( data.colorStr.c_str(), data.colorStr.size(), color );
84
85     if( color != data.expectedColor )
86     {
87       std::cout << "  different color : " << color << ", expected : " << data.expectedColor << std::endl;
88       return false;
89     }
90
91     return true;
92   }
93
94   ///////////////////////////////////////////////////////////
95
96   struct Vector4ToColorStringData
97   {
98     std::string description;
99     Vector4 color;
100     std::string expectedColorStr;
101   };
102
103   bool Vector4ToColorStringTest( const Vector4ToColorStringData& data )
104   {
105     std::cout << "  testing " << data.description << std::endl;
106
107     std::string colorStr;
108     Vector4ToColorString( data.color, colorStr );
109
110     if( colorStr != data.expectedColorStr )
111     {
112       std::cout << "  different color : [" << colorStr << "], expected : [" << data.expectedColorStr << "]" << std::endl;
113       return false;
114     }
115
116     return true;
117   }
118
119   ///////////////////////////////////////////////////////////
120
121   struct StringToVector2Data
122   {
123     std::string description;
124     std::string vector2Str;
125     Vector2 expectedVector2;
126   };
127
128   bool StringToVector2Test( const StringToVector2Data& data )
129   {
130     std::cout << "  testing " << data.description << std::endl;
131
132     Vector2 vector2;
133     StringToVector2( data.vector2Str.c_str(), data.vector2Str.size(), vector2 );
134
135     if( vector2 != data.expectedVector2 )
136     {
137       std::cout << "  different vector2 : " << vector2 << ", expected : " << data.expectedVector2 << std::endl;
138       return false;
139     }
140
141     return true;
142   }
143
144   ///////////////////////////////////////////////////////////
145
146
147   struct Vector2ToStringData
148   {
149     std::string description;
150     Vector2 vector2;
151     std::string expectedVector2Str;
152   };
153
154   bool Vector2ToStringTest( const Vector2ToStringData& data )
155   {
156     std::cout << "  testing " << data.description << std::endl;
157
158     std::string vector2Str;
159     Vector2ToString( data.vector2, vector2Str );
160
161     if( vector2Str != data.expectedVector2Str )
162     {
163       std::cout << "  different vector2 : [" << vector2Str << "], expected : [" << data.expectedVector2Str << "]" << std::endl;
164       return false;
165     }
166
167     return true;
168   }
169
170   ///////////////////////////////////////////////////////////
171
172
173   struct XHTMLEntityToUTF8Data
174   {
175     std::string description;
176     std::string xHTMLEntityString;
177     std::string expectedString;
178   };
179
180   bool XHTMLEntityToUTF8Test( const XHTMLEntityToUTF8Data& data )
181   {
182     std::cout << "  testing " << data.description << std::endl;
183
184     Vector<ColorRun> colorRuns;
185     Vector<FontDescriptionRun> fontRuns;
186     Vector<EmbeddedItem> items;
187     Vector<Anchor> anchors;
188     Vector<UnderlinedCharacterRun> underlinedCharacterRuns;
189     Vector<ColorRun> backgroundColorRuns;
190     MarkupProcessData markupProcessData( colorRuns, fontRuns, items, anchors, underlinedCharacterRuns, backgroundColorRuns );
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     {
221       "void texts",
222       "",
223       "",
224       true
225     },
226     {
227       "different size text",
228       "hello",
229       "world!",
230       false
231     },
232     {
233       "different texts",
234       "hello",
235       "world",
236       false
237     },
238     {
239       "same texts",
240       "world",
241       "wOrLD",
242       true
243     },
244     {
245       "some punctuation characters, numbers, ...",
246       "hello0123456789.![?]",
247       "Hello0123456789.![?]",
248       true
249     }
250
251   };
252   const unsigned int numberOfTests = 5u;
253
254   for( unsigned int index = 0u; index < numberOfTests; ++index )
255   {
256     ToolkitTestApplication application;
257     if( !TokenComparisonTest( data[index] ) )
258     {
259       tet_result(TET_FAIL);
260     }
261   }
262
263   tet_result(TET_PASS);
264   END_TEST;
265 }
266
267 int UtcDaliTextColorStringToVector4(void)
268 {
269   tet_infoline(" UtcDaliTextColorStringToVector4");
270
271   const ColorStringToVector4Data data[] =
272   {
273     {
274       "black string",
275       "bLack",
276       Color::BLACK
277     },
278     {
279       "white string",
280       "White",
281       Color::WHITE
282     },
283     {
284       "red string",
285       "reD",
286       Color::RED
287     },
288     {
289       "green string",
290       "green",
291       Color::GREEN
292     },
293     {
294       "blue string",
295       "blue",
296       Color::BLUE
297     },
298     {
299       "yellow string",
300       "yeLloW",
301       Color::YELLOW
302     },
303     {
304       "magenta string",
305       "MagEnta",
306       Color::MAGENTA
307     },
308     {
309       "cyan string",
310       "CyaN",
311       Color::CYAN
312     },
313     {
314       "transparent string",
315       "transparent",
316       Color::TRANSPARENT
317     },
318     {
319       "3 component web color",
320       "#F00",
321       Color::RED
322     },
323     {
324       "6 component web color",
325       "#fF0000",
326       Color::RED
327     },
328     {
329       "hex color red (ARGB)",
330       "0xffff0000",
331       Color::RED
332     },
333     {
334       "hex color green (ARGB)",
335       "0xFf00FF00",
336       Color::GREEN
337     },
338     {
339       "undefined color",
340       "undefined",
341       Vector4::ZERO
342     },
343   };
344   const unsigned int numberOfTests = 14u;
345
346   for( unsigned int index = 0u; index < numberOfTests; ++index )
347   {
348     ToolkitTestApplication application;
349     if( !ColorStringToVector4Test( data[index] ) )
350     {
351       tet_result(TET_FAIL);
352     }
353   }
354
355   tet_result(TET_PASS);
356   END_TEST;
357 }
358
359 int UtcDaliTextVector4ToColorString(void)
360 {
361   tet_infoline(" UtcDaliTextVector4ToColorString");
362
363   const Vector4ToColorStringData data[] =
364   {
365     {
366       "black color",
367       Color::BLACK,
368       "black"
369     },
370     {
371       "white string",
372       Color::WHITE,
373       "white"
374     },
375     {
376       "red string",
377       Color::RED,
378       "red"
379     },
380     {
381       "green string",
382       Color::GREEN,
383       "green"
384     },
385     {
386       "blue string",
387       Color::BLUE,
388       "blue"
389     },
390     {
391       "yellow string",
392       Color::YELLOW,
393       "yellow"
394     },
395     {
396       "magenta string",
397       Color::MAGENTA,
398       "magenta",
399     },
400     {
401       "cyan string",
402       Color::CYAN,
403       "cyan"
404     },
405     {
406       "transparent string",
407       Color::TRANSPARENT,
408       "transparent"
409     },
410     {
411       "hex color",
412       Vector4( 0.4f, 0.5f, 0.6f, 1.f ),
413       "0xff667f99"
414     },
415   };
416   const unsigned int numberOfTests = 10u;
417
418   for( unsigned int index = 0u; index < numberOfTests; ++index )
419   {
420     ToolkitTestApplication application;
421     if( !Vector4ToColorStringTest( data[index] ) )
422     {
423       tet_result(TET_FAIL);
424     }
425   }
426
427   tet_result(TET_PASS);
428   END_TEST;
429 }
430
431 int UtcDaliTextStringToVector2(void)
432 {
433   tet_infoline(" UtcDaliTextStringToVector2");
434   const StringToVector2Data data[] =
435   {
436     {
437       "void text",
438       "",
439       Vector2::ZERO
440     },
441     {
442       "zero zero",
443       "0 0",
444       Vector2::ZERO
445     },
446     {
447       "five four",
448       "5 4",
449       Vector2(5.f, 4.f)
450     }
451   };
452   const unsigned int numberOfTests = 3u;
453
454   for( unsigned int index = 0u; index < numberOfTests; ++index )
455   {
456     ToolkitTestApplication application;
457     if( !StringToVector2Test( data[index] ) )
458     {
459       tet_result(TET_FAIL);
460     }
461   }
462
463   tet_result(TET_PASS);
464   END_TEST;
465 }
466
467 int UtcDaliTextVector2ToString(void)
468 {
469   tet_infoline(" UtcDaliTextVector2ToString");
470   const Vector2ToStringData data[] =
471   {
472     {
473       "zero zero",
474       Vector2::ZERO,
475       "0 0",
476     },
477     {
478       "five four",
479       Vector2(5.f, 4.f),
480       "5 4",
481     }
482   };
483   const unsigned int numberOfTests = 2u;
484
485   for( unsigned int index = 0u; index < numberOfTests; ++index )
486   {
487     ToolkitTestApplication application;
488     if( !Vector2ToStringTest( data[index] ) )
489     {
490       tet_result(TET_FAIL);
491     }
492   }
493
494   tet_result(TET_PASS);
495   END_TEST;
496 }
497
498 int UtcDaliTextXHTMLEntityToUTF8(void)
499 {
500   tet_infoline(" UtcDaliTextXHTMLEntityToUTF8");
501   const XHTMLEntityToUTF8Data data[] =
502   {
503     {
504       "Text Without XHTML Entity",
505       "Checking XHTML Entitities",
506       "Checking XHTML Entitities"
507     },
508     {
509       "Text With XHTML Entity in Numeric form",
510       "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; ",
511       "Checking Numeric Entitities & ' < > ¡ ¢ £ ¤ ¥ ¦ § ¨ © ª « ¬ ­ ® ¯ ° ± ² ³ ´ µ ¶ · ¸ ¹ º » ¼ ½ ¾ ¿ À Á Â Ã Ä Å æ Ç È É Ê Ë Ì Í Î Ï ð Ñ Ò Ó Ô Õ Ö × Ø Ù Ú Û Ý þ ß à á â ã ä å æ ç è é ê ë ì í î ï ð ñ ò ó ô õ ö ÷ ø ù ú û ü ý þ ÿ α β γ δ ε ζ η θ ι κ λ μ ν ξ ο π ρ σ τ υ φ χ ψ ω … € ← ↑ → ↓ ↔ ← → ∀ ∃ ∇ ∏ ∑ ∧ ∨ ∫ ≠ ≡ ⊕ ⊥ † ‡ • "
512     },
513     {
514       "Text With XHTML Named Entities",
515       "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; ",
516       "Checking Named Entitities & ' < > ¡ ¢ £ ¤ ¥ ¦ § ¨ © ª « ¬ ­ ® ¯ ° ± ² ³ ´ µ ¶ · ¸ ¹ º » ¼ ½ ¾ ¿ À Á Â Ã Ä Å æ Ç È É Ê Ë Ì Í Î Ï ð Ñ Ò Ó Ô Õ Ö × Ø Ù Ú Û Ý þ ß à á â ã ä å æ ç è é ê ë ì í î ï ð ñ ò ó ô õ ö ÷ ø ù ú û ü ý þ ÿ α β γ δ ε ζ η θ ι κ λ μ ν ξ ο π ρ σ τ υ φ χ ψ ω … € ← ↑ → ↓ ↔ ← → ∀ ∃ ∇ ∏ ∑ ∧ ∨ ∫ ≠ ≡ ⊕ ⊥ † ‡ • "
517     },
518     {
519       "Testing of < special character",
520       "Testing of < special character",
521       "Testing of "
522     },
523     {
524       "Testing of & special character",
525       "Testing of & special character",
526       "Testing of "
527     },
528     {
529       "Testing of & < > special character",
530       "Testing of \\& \\< \\> special character",
531       "Testing of & < > special character"
532     }
533   };
534   const unsigned int numberOfTests = 6u;
535
536   for( unsigned int index = 0u; index < numberOfTests; ++index )
537   {
538     ToolkitTestApplication application;
539     if( !XHTMLEntityToUTF8Test( data[index] ) )
540     {
541       tet_result(TET_FAIL);
542     }
543   }
544
545   tet_result(TET_PASS);
546   END_TEST;
547 }