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