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