d2317a7024b64db17e76d2e50a957ec05a0d1346
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-MarkupProcessor.cpp
1 /*
2  * Copyright (c) 2014 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 #include <stdlib.h>
20
21 // Need to override adaptor classes for toolkit test harness, so include
22 // test harness headers before dali headers.
23 #include <dali-toolkit-test-suite-utils.h>
24
25 #include <dali.h>
26 #include <dali-toolkit/dali-toolkit.h>
27
28 using namespace Dali;
29
30 void utc_dali_toolkit_markup_processor_startup(void)
31 {
32   test_return_value = TET_UNDEF;
33 }
34
35 void utc_dali_toolkit_markup_processor_cleanup(void)
36 {
37   test_return_value = TET_PASS;
38 }
39
40 namespace
41 {
42
43 struct MarkupStringTest
44 {
45   std::string input;
46   std::string expectedResult;
47 };
48
49 bool TestMarkupString( const std::string& input, const std::string& expectedResult, std::string& result )
50 {
51   Toolkit::MarkupProcessor::StyledTextArray styledTextArray;
52
53   GetStyledTextArray( input, styledTextArray, true );
54   GetMarkupString( styledTextArray, result );
55
56   return expectedResult == result;
57 }
58
59 } // namespace
60
61
62 // Positive test case for a method
63 int UtcDaliMarkupProcessor(void)
64 {
65   ToolkitTestApplication application;
66
67   tet_infoline(" UtcDaliMarkupProcessor ");
68
69   const std::string text1( "Text" );
70   const std::string text2( "< font  face ='FreeSerif'  color= 'green' >t< / font >" );
71   const std::string text3( "<  font face =  'FreeSerif' size=  '16' style = 'Bold' color='red'>< i><u >Styled< / u> Text< /i >< / font >< br / >" );
72   const std::string text4( "<font face='FreeSerif' size='14' color='0xaadd8744'><b><u>Styled</u> Te<font size='20'>x</font>t</b></font>< br/>" );
73   const std::string text5( "< shadow color   =   'blue' paramx =   '1' paramy = '0.75'  >Shadow< / shadow><br />" );
74   const std::string text6( "<smooth     param=  '0.75'  >< glow   color =   'red' param  = '0.1'  >Glow</glow></smooth>< br />" );
75   const std::string text7( "<font color='green''><   outline color = 'red'   paramx  =  '0.7' paramy  =  '0.7' >Outline< / outline  >< /font  ><  br  /  >" );
76   const std::string text8( "<smooth param='0.75'>Smooth</smooth><  br /   >" );
77   const std::string text9( "\\<" );
78   const std::string text10( "\\>" );
79
80   char crlf[2];
81   crlf[0] = 0x0D;
82   crlf[1] = 0x0A;
83   const std::string text11( crlf, 2 );
84
85   const std::string result1( text1 );
86   const std::string result2( "<font face='FreeSerif' color='green'>t</font>" );
87   const std::string result3( "<font face='FreeSerif' style='Bold' size='16' color='red'><i><u>Styled</u></i></font><font face='FreeSerif' style='Bold' size='16' color='red'><i> Text</i></font><br />" );
88   const std::string result4( "<font face='FreeSerif' size='14' color='0xaadd8744'><b><u>Styled</u></b></font><font face='FreeSerif' size='14' color='0xaadd8744'><b> Te</b></font><font face='FreeSerif' size='20' color='0xaadd8744'><b>x</b></font><font face='FreeSerif' size='14' color='0xaadd8744'><b>t</b></font><br />" );
89   const std::string result5( "<shadow color='blue' paramx='1' paramy='0.75'>Shadow</shadow><br />" );
90   const std::string result6( "<smooth param='0.75'><glow color='red' param='0.1'>Glow</glow></smooth><br />" );
91   const std::string result7( "<font color='green'><outline color='red' paramx='0.7' paramy='0.7'>Outline</outline></font><br />" );
92   const std::string result8( "<smooth param='0.75'>Smooth</smooth><br />" );
93   const std::string result9( text9 );
94   const std::string result10( text10 );
95   const std::string result11( "<br />" );
96
97   std::string markupString;
98   Toolkit::MarkupProcessor::StyledTextArray styledTextArray;
99
100   GetStyledTextArray( text1, styledTextArray, true );
101   GetMarkupString( styledTextArray, markupString );
102   DALI_TEST_EQUALS( result1, markupString, TEST_LOCATION );
103
104   GetStyledTextArray( text2, styledTextArray, true );
105   GetMarkupString( styledTextArray, markupString );
106   DALI_TEST_EQUALS( result2, markupString, TEST_LOCATION );
107
108   GetStyledTextArray( text3, styledTextArray, true );
109   GetMarkupString( styledTextArray, markupString );
110   DALI_TEST_EQUALS( result3, markupString, TEST_LOCATION );
111
112   GetStyledTextArray( text4, styledTextArray, true );
113   GetMarkupString( styledTextArray, markupString );
114   DALI_TEST_EQUALS( result4, markupString, TEST_LOCATION );
115
116   GetStyledTextArray( text5, styledTextArray, true );
117   GetMarkupString( styledTextArray, markupString );
118   DALI_TEST_EQUALS( result5, markupString, TEST_LOCATION );
119
120   GetStyledTextArray( text6, styledTextArray, true );
121   GetMarkupString( styledTextArray, markupString );
122   DALI_TEST_EQUALS( result6, markupString, TEST_LOCATION );
123
124   GetStyledTextArray( text7, styledTextArray, true );
125   GetMarkupString( styledTextArray, markupString );
126   DALI_TEST_EQUALS( result7, markupString, TEST_LOCATION );
127
128   GetStyledTextArray( text8, styledTextArray, true );
129   GetMarkupString( styledTextArray, markupString );
130   DALI_TEST_EQUALS( result8, markupString, TEST_LOCATION );
131
132   GetStyledTextArray( text9, styledTextArray, true );
133   GetMarkupString( styledTextArray, markupString );
134   DALI_TEST_EQUALS( result9, markupString, TEST_LOCATION );
135
136   GetStyledTextArray( text10, styledTextArray, true );
137   GetMarkupString( styledTextArray, markupString );
138
139   DALI_TEST_EQUALS( result10, markupString, TEST_LOCATION );
140
141   GetStyledTextArray( text11, styledTextArray, true );
142   GetMarkupString( styledTextArray, markupString );
143
144   DALI_TEST_EQUALS( result11, markupString, TEST_LOCATION );
145   END_TEST;
146 }
147
148 int UtcDaliMarkupProcessorSetTextStyle01(void)
149 {
150   ToolkitTestApplication application;
151
152   tet_infoline(" UtcDaliMarkupProcessorSetTextStyle01 ");
153
154   const std::string text1( "Text with no defined style" );
155   const std::string result1( "<font color='green'><i>Text with no defined style</i></font>" );
156   const std::string result2( "Text with <font color='green'><i>no defined</i></font> style" );
157
158   std::string markupString;
159   Toolkit::MarkupProcessor::StyledTextArray styledTextArray;
160
161   GetStyledTextArray( text1, styledTextArray, true );
162
163   TextStyle style;
164   style.SetItalics( true );
165   style.SetTextColor( Color::GREEN );
166
167   SetTextStyle( styledTextArray, style );
168   GetMarkupString( styledTextArray, markupString );
169
170   DALI_TEST_EQUALS( result1, markupString, TEST_LOCATION );
171
172   styledTextArray.clear();
173   SetTextStyle( text1, styledTextArray, style );
174   GetMarkupString( styledTextArray, markupString );
175
176   DALI_TEST_EQUALS( result1, markupString, TEST_LOCATION );
177
178   GetStyledTextArray( text1, styledTextArray, true );
179   SetTextStyleToRange( styledTextArray, style, TextStyle::ALL, 0, text1.size() - 1 );
180   GetMarkupString( styledTextArray, markupString );
181
182   DALI_TEST_EQUALS( result1, markupString, TEST_LOCATION );
183
184   GetStyledTextArray( text1, styledTextArray, true );
185   SetTextStyleToRange( styledTextArray, style, TextStyle::ALL, 10, 19 );
186   GetMarkupString( styledTextArray, markupString );
187
188   DALI_TEST_EQUALS( result2, markupString, TEST_LOCATION );
189
190   std::string plainString;
191   GetPlainString( styledTextArray, plainString );
192
193   DALI_TEST_EQUALS( text1, plainString, TEST_LOCATION );
194   END_TEST;
195 }
196
197 int UtcDaliMarkupProcessorSetTextStyle02(void)
198 {
199   ToolkitTestApplication application;
200
201   tet_infoline(" UtcDaliMarkupProcessorSetTextStyle02 ");
202
203   Toolkit::MarkupProcessor::StyledTextArray styledTextArray;
204
205   // Test style applied to and empty string doesn't crash
206
207   TextStyle style;
208   style.SetItalics( true );
209   style.SetTextColor( Color::GREEN );
210
211   bool fails = false;
212   try
213   {
214     SetTextStyle( styledTextArray, style );
215   }
216   catch( ... )
217   {
218     fails = true;
219   }
220
221   DALI_TEST_CHECK( !fails );
222   END_TEST;
223 }
224
225 int UtcDaliMarkupProcessorTestColors(void)
226 {
227   ToolkitTestApplication application;
228
229   tet_infoline("UtcDaliMarkupProcessorTestColors  ");
230
231   struct MarkupStringTest colorTests[] =
232   {
233     {
234       std::string( "<font color='0xFF000000'>black</font>" ),
235       std::string( "<font color='black'>black</font>" )
236     },
237     {
238       std::string( "<font color='0xFFFFFFFF'>white</font>" ),
239       std::string( "white" )
240     },
241     {
242       std::string( "<font color='0xFFFF0000'>red</font>" ),
243       std::string( "<font color='red'>red</font>" )
244     },
245     {
246       std::string( "<font color='0xFF00FF00'>green</font>" ),
247       std::string( "<font color='green'>green</font>" )
248     },
249     {
250       std::string( "<font color='0xFF0000FF'>blue</font>" ),
251       std::string( "<font color='blue'>blue</font>" )
252     },
253     {
254       std::string( "<font color='0xFFFFFF00'>yellow</font>" ),
255       std::string( "<font color='yellow'>yellow</font>" )
256     },
257     {
258       std::string( "<font color='0xFFFF00FF'>magenta</font>" ),
259       std::string( "<font color='magenta'>magenta</font>" )
260     },
261     {
262       std::string( "<font color='0xFF00FFFF'>cyan</font>" ),
263       std::string( "<font color='cyan'>cyan</font>" )
264     },
265     {
266       std::string( "<font color='0x00000000'>transparent</font>" ),
267       std::string( "<font color='transparent'>transparent</font>" )
268     },
269     {
270       std::string( "<font color='#000000'>black</font>" ),
271       std::string( "<font color='black'>black</font>" )
272     },
273     {
274       std::string( "<font color='#FFFFFF'>white</font>" ),
275       std::string( "white" )
276     },
277     {
278       std::string( "<font color='#FF0000'>red</font>" ),
279       std::string( "<font color='red'>red</font>" )
280     },
281     {
282       std::string( "<font color='#00FF00'>green</font>" ),
283       std::string( "<font color='green'>green</font>" )
284     },
285     {
286       std::string( "<font color='#0000FF'>blue</font>" ),
287       std::string( "<font color='blue'>blue</font>" )
288     },
289     {
290       std::string( "<font color='#FFFF00'>yellow</font>" ),
291       std::string( "<font color='yellow'>yellow</font>" )
292     },
293     {
294       std::string( "<font color='#FF00FF'>magenta</font>" ),
295       std::string( "<font color='magenta'>magenta</font>" )
296     },
297     {
298       std::string( "<font color='#00FFFF'>cyan</font>" ),
299       std::string( "<font color='cyan'>cyan</font>" )
300     },
301     {
302       std::string( "<font color='#000'>black</font>" ),
303       std::string( "<font color='black'>black</font>" )
304     },
305     {
306       std::string( "<font color='#FFF'>white</font>" ),
307       std::string( "white" )
308     },
309     {
310       std::string( "<font color='#F00'>red</font>" ),
311       std::string( "<font color='red'>red</font>" )
312     },
313     {
314       std::string( "<font color='#0F0'>green</font>" ),
315       std::string( "<font color='green'>green</font>" )
316     },
317     {
318       std::string( "<font color='#00F'>blue</font>" ),
319       std::string( "<font color='blue'>blue</font>" )
320     },
321     {
322       std::string( "<font color='#FF0'>yellow</font>" ),
323       std::string( "<font color='yellow'>yellow</font>" )
324     },
325     {
326       std::string( "<font color='#F0F'>magenta</font>" ),
327       std::string( "<font color='magenta'>magenta</font>" )
328     },
329     {
330       std::string( "<font color='#0FF'>cyan</font>" ),
331       std::string( "<font color='cyan'>cyan</font>" )
332     },
333     {
334       std::string( "<font color='0x000000'>black</font>" ),
335       std::string( "<font color='black'>black</font>" )
336     },
337     {
338       std::string( "<font color='black'>black</font>" ),
339       std::string( "<font color='black'>black</font>" )
340     },
341     {
342       std::string( "<font color='white'>white</font>" ),
343       std::string( "white" )
344     },
345     {
346       std::string( "<font color='red'>red</font>" ),
347       std::string( "<font color='red'>red</font>" )
348     },
349     {
350       std::string( "<font color='0xFF00FF00'>green</font>" ),
351       std::string( "<font color='green'>green</font>" )
352     },
353     {
354       std::string( "<font color='blue'>blue</font>" ),
355       std::string( "<font color='blue'>blue</font>" )
356     },
357     {
358       std::string( "<font color='yellow'>yellow</font>" ),
359       std::string( "<font color='yellow'>yellow</font>" )
360     },
361     {
362       std::string( "<font color='magenta'>magenta</font>" ),
363       std::string( "<font color='magenta'>magenta</font>" )
364     },
365     {
366       std::string( "<font color='cyan'>cyan</font>" ),
367       std::string( "<font color='cyan'>cyan</font>" )
368     },
369     {
370       std::string( "<font color='transparent'>transparent</font>" ),
371       std::string( "<font color='transparent'>transparent</font>" )
372     },
373     {
374       std::string( "<outline color='white'>outline</outline>" ),
375       std::string( "<outline color='white'>outline</outline>" )
376     },
377   };
378
379   const std::size_t numberOfTests( 36 );
380
381   bool fails = false;
382   for( std::size_t index = 0; index < numberOfTests; ++index )
383   {
384     const MarkupStringTest& test = colorTests[index];
385
386     std::string result;
387     if( !TestMarkupString( test.input, test.expectedResult, result ) )
388     {
389       TestMarkupString( test.input, test.expectedResult, result );
390       tet_printf( "%s\n          input : %s\nexpected result : %s\n         result : %s\n", TEST_LOCATION, test.input.c_str(), test.expectedResult.c_str(), result.c_str() );
391
392       fails = true;
393     }
394   }
395
396   DALI_TEST_CHECK( !fails );
397   END_TEST;
398 }