[dali_1.2.61] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-internal / utc-Dali-Text-Markup.cpp
1 /*
2  * Copyright (c) 2016 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   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
146   struct Vector2ToStringData
147   {
148     std::string description;
149     Vector2 vector2;
150     std::string expectedVector2Str;
151   };
152
153   bool Vector2ToStringTest( const Vector2ToStringData& data )
154   {
155     std::cout << "  testing " << data.description << std::endl;
156
157     std::string vector2Str;
158     Vector2ToString( data.vector2, vector2Str );
159
160     if( vector2Str != data.expectedVector2Str )
161     {
162       std::cout << "  different vector2 : [" << vector2Str << "], expected : [" << data.expectedVector2Str << "]" << std::endl;
163       return false;
164     }
165
166     return true;
167   }
168
169   ///////////////////////////////////////////////////////////
170
171
172   struct XHTMLEntityToUTF8Data
173   {
174     std::string description;
175     std::string xHTMLEntityString;
176     std::string expectedString;
177   };
178
179   bool XHTMLEntityToUTF8Test( const XHTMLEntityToUTF8Data& data )
180   {
181     std::cout << "  testing " << data.description << std::endl;
182
183     Vector<ColorRun> colorRuns;
184     Vector<FontDescriptionRun> fontRuns;
185     MarkupProcessData markupProcessData( colorRuns, fontRuns );
186     ProcessMarkupString( data.xHTMLEntityString, markupProcessData );
187
188     if( markupProcessData.markupProcessedText != data.expectedString )
189     {
190       std::cout << "  different output string : " << markupProcessData.markupProcessedText << ", expected : " << data.expectedString << " " << std::endl;
191       return false;
192     }
193
194     return true;
195   }
196
197 } // namespace
198
199 int UtcDaliTextTokenComparison(void)
200 {
201   tet_infoline(" UtcDaliTextTokenComparison");
202
203   const TokenComparisonData data[] =
204   {
205     {
206       "void texts",
207       "",
208       "",
209       true
210     },
211     {
212       "different size text",
213       "hello",
214       "world!",
215       false
216     },
217     {
218       "different texts",
219       "hello",
220       "world",
221       false
222     },
223     {
224       "same texts",
225       "world",
226       "wOrLD",
227       true
228     },
229     {
230       "some punctuation characters, numbers, ...",
231       "hello0123456789.![?]",
232       "Hello0123456789.![?]",
233       true
234     }
235
236   };
237   const unsigned int numberOfTests = 5u;
238
239   for( unsigned int index = 0u; index < numberOfTests; ++index )
240   {
241     ToolkitTestApplication application;
242     if( !TokenComparisonTest( data[index] ) )
243     {
244       tet_result(TET_FAIL);
245     }
246   }
247
248   tet_result(TET_PASS);
249   END_TEST;
250 }
251
252 int UtcDaliTextColorStringToVector4(void)
253 {
254   tet_infoline(" UtcDaliTextColorStringToVector4");
255
256   const ColorStringToVector4Data data[] =
257   {
258     {
259       "black string",
260       "bLack",
261       Color::BLACK
262     },
263     {
264       "white string",
265       "White",
266       Color::WHITE
267     },
268     {
269       "red string",
270       "reD",
271       Color::RED
272     },
273     {
274       "green string",
275       "green",
276       Color::GREEN
277     },
278     {
279       "blue string",
280       "blue",
281       Color::BLUE
282     },
283     {
284       "yellow string",
285       "yeLloW",
286       Color::YELLOW
287     },
288     {
289       "magenta string",
290       "MagEnta",
291       Color::MAGENTA
292     },
293     {
294       "cyan string",
295       "CyaN",
296       Color::CYAN
297     },
298     {
299       "transparent string",
300       "transparent",
301       Color::TRANSPARENT
302     },
303     {
304       "3 component web color",
305       "#F00",
306       Color::RED
307     },
308     {
309       "6 component web color",
310       "#fF0000",
311       Color::RED
312     },
313     {
314       "hex color red (ARGB)",
315       "0xffff0000",
316       Color::RED
317     },
318     {
319       "hex color green (ARGB)",
320       "0xFf00FF00",
321       Color::GREEN
322     },
323     {
324       "undefined color",
325       "undefined",
326       Vector4::ZERO
327     },
328   };
329   const unsigned int numberOfTests = 14u;
330
331   for( unsigned int index = 0u; index < numberOfTests; ++index )
332   {
333     ToolkitTestApplication application;
334     if( !ColorStringToVector4Test( data[index] ) )
335     {
336       tet_result(TET_FAIL);
337     }
338   }
339
340   tet_result(TET_PASS);
341   END_TEST;
342 }
343
344 int UtcDaliTextVector4ToColorString(void)
345 {
346   tet_infoline(" UtcDaliTextVector4ToColorString");
347
348   const Vector4ToColorStringData data[] =
349   {
350     {
351       "black color",
352       Color::BLACK,
353       "black"
354     },
355     {
356       "white string",
357       Color::WHITE,
358       "white"
359     },
360     {
361       "red string",
362       Color::RED,
363       "red"
364     },
365     {
366       "green string",
367       Color::GREEN,
368       "green"
369     },
370     {
371       "blue string",
372       Color::BLUE,
373       "blue"
374     },
375     {
376       "yellow string",
377       Color::YELLOW,
378       "yellow"
379     },
380     {
381       "magenta string",
382       Color::MAGENTA,
383       "magenta",
384     },
385     {
386       "cyan string",
387       Color::CYAN,
388       "cyan"
389     },
390     {
391       "transparent string",
392       Color::TRANSPARENT,
393       "transparent"
394     },
395     {
396       "hex color",
397       Vector4( 0.4f, 0.5f, 0.6f, 1.f ),
398       "0xff667f99"
399     },
400   };
401   const unsigned int numberOfTests = 10u;
402
403   for( unsigned int index = 0u; index < numberOfTests; ++index )
404   {
405     ToolkitTestApplication application;
406     if( !Vector4ToColorStringTest( data[index] ) )
407     {
408       tet_result(TET_FAIL);
409     }
410   }
411
412   tet_result(TET_PASS);
413   END_TEST;
414 }
415
416 int UtcDaliTextStringToVector2(void)
417 {
418   tet_infoline(" UtcDaliTextStringToVector2");
419   const StringToVector2Data data[] =
420   {
421     {
422       "void text",
423       "",
424       Vector2::ZERO
425     },
426     {
427       "zero zero",
428       "0 0",
429       Vector2::ZERO
430     },
431     {
432       "five four",
433       "5 4",
434       Vector2(5.f, 4.f)
435     }
436   };
437   const unsigned int numberOfTests = 3u;
438
439   for( unsigned int index = 0u; index < numberOfTests; ++index )
440   {
441     ToolkitTestApplication application;
442     if( !StringToVector2Test( data[index] ) )
443     {
444       tet_result(TET_FAIL);
445     }
446   }
447
448   tet_result(TET_PASS);
449   END_TEST;
450 }
451
452 int UtcDaliTextVector2ToString(void)
453 {
454   tet_infoline(" UtcDaliTextVector2ToString");
455   const Vector2ToStringData data[] =
456   {
457     {
458       "zero zero",
459       Vector2::ZERO,
460       "0 0",
461     },
462     {
463       "five four",
464       Vector2(5.f, 4.f),
465       "5 4",
466     }
467   };
468   const unsigned int numberOfTests = 2u;
469
470   for( unsigned int index = 0u; index < numberOfTests; ++index )
471   {
472     ToolkitTestApplication application;
473     if( !Vector2ToStringTest( data[index] ) )
474     {
475       tet_result(TET_FAIL);
476     }
477   }
478
479   tet_result(TET_PASS);
480   END_TEST;
481 }
482
483 int UtcDaliTextXHTMLEntityToUTF8(void)
484 {
485   tet_infoline(" UtcDaliTextXHTMLEntityToUTF8");
486   const XHTMLEntityToUTF8Data data[] =
487   {
488     {
489       "Text Without XHTML Entity",
490       "Checking XHTML Entitities",
491       "Checking XHTML Entitities"
492     },
493     {
494       "Text With XHTML Entity in Numeric form",
495       "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; ",
496       "Checking Numeric Entitities & ' < > ¡ ¢ £ ¤ ¥ ¦ § ¨ © ª « ¬ ­ ® ¯ ° ± ² ³ ´ µ ¶ · ¸ ¹ º » ¼ ½ ¾ ¿ À Á Â Ã Ä Å æ Ç È É Ê Ë Ì Í Î Ï ð Ñ Ò Ó Ô Õ Ö × Ø Ù Ú Û Ý þ ß à á â ã ä å æ ç è é ê ë ì í î ï ð ñ ò ó ô õ ö ÷ ø ù ú û ü ý þ ÿ α β γ δ ε ζ η θ ι κ λ μ ν ξ ο π ρ σ τ υ φ χ ψ ω … € ← ↑ → ↓ ↔ ← → ∀ ∃ ∇ ∏ ∑ ∧ ∨ ∫ ≠ ≡ ⊕ ⊥ † ‡ • "
497     },
498     {
499       "Text With XHTML Named Entities",
500       "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; ",
501       "Checking Named Entitities & ' < > ¡ ¢ £ ¤ ¥ ¦ § ¨ © ª « ¬ ­ ® ¯ ° ± ² ³ ´ µ ¶ · ¸ ¹ º » ¼ ½ ¾ ¿ À Á Â Ã Ä Å æ Ç È É Ê Ë Ì Í Î Ï ð Ñ Ò Ó Ô Õ Ö × Ø Ù Ú Û Ý þ ß à á â ã ä å æ ç è é ê ë ì í î ï ð ñ ò ó ô õ ö ÷ ø ù ú û ü ý þ ÿ α β γ δ ε ζ η θ ι κ λ μ ν ξ ο π ρ σ τ υ φ χ ψ ω … € ← ↑ → ↓ ↔ ← → ∀ ∃ ∇ ∏ ∑ ∧ ∨ ∫ ≠ ≡ ⊕ ⊥ † ‡ • "
502     },
503     {
504       "Testing of < special character",
505       "Testing of < special character",
506       "Testing of "
507     },
508     {
509       "Testing of & special character",
510       "Testing of & special character",
511       "Testing of "
512     },
513     {
514       "Testing of & < > special character",
515       "Testing of \\& \\< \\> special character",
516       "Testing of & < > special character"
517     }
518   };
519   const unsigned int numberOfTests = 6u;
520
521   for( unsigned int index = 0u; index < numberOfTests; ++index )
522   {
523     ToolkitTestApplication application;
524     if( !XHTMLEntityToUTF8Test( data[index] ) )
525     {
526       tet_result(TET_FAIL);
527     }
528   }
529
530   tet_result(TET_PASS);
531   END_TEST;
532 }