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