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