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