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