a6b9c59d2d54f7581c1a093ae9e6272ad0188f3d
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-internal / utc-Dali-TextView-Processor.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 #include <dali-toolkit-test-suite-utils.h>
20 #include <dali-toolkit/dali-toolkit.h>
21
22 // Internal headers are allowed here
23 #include <dali-toolkit/internal/controls/text-view/text-processor.h>
24
25 using namespace Dali;
26 using namespace Dali::Toolkit;
27 using namespace Dali::Toolkit::Internal;
28
29 void dali_text_view_processor_startup(void)
30 {
31   test_return_value = TET_UNDEF;
32 }
33
34 void dali_text_view_processor_cleanup(void)
35 {
36   test_return_value = TET_PASS;
37 }
38
39
40 namespace
41 {
42 // Data structures used to create an 'experiment' in TET cases
43
44 //////////////////////////////////////////////////////////////////
45
46 struct BeginsRightToLeftCharacterTest
47 {
48   std::string description;
49   std::string input;
50   bool result;
51 };
52
53 bool TestBeginsRightToLeftCharacter( const std::string& description, const std::string& input, const bool result, const char* location )
54 {
55   // Creates a styled text with the markup or plain string.
56   MarkupProcessor::StyledTextArray styledText;
57   MarkupProcessor::GetStyledTextArray( input, styledText, true );
58
59   const bool ret = ( result == TextProcessor::BeginsRightToLeftCharacter( styledText ) );
60
61   if( !ret )
62   {
63     tet_printf( "Fail. %s", location );
64     tet_printf( "Input : %s", input.c_str() );
65   }
66
67   return ret;
68 }
69
70 //////////////////////////////////////////////////////////////////
71
72 struct ContainsRightToLeftCharacterTest
73 {
74   std::string description;
75   std::string input;
76   bool result;
77 };
78
79 bool TestContainsRightToLeftCharacter( const std::string& description, const std::string& input, const bool result, const char* location )
80 {
81   // Creates a styled text with the markup or plain string.
82   MarkupProcessor::StyledTextArray styledText;
83   MarkupProcessor::GetStyledTextArray( input, styledText, true );
84
85   const bool ret = ( result == TextProcessor::ContainsRightToLeftCharacter( styledText ) );
86
87   if( !ret )
88   {
89     tet_printf( "Fail. %s", location );
90     tet_printf( "Input : %s", input.c_str() );
91   }
92
93   return ret;
94 }
95
96 //////////////////////////////////////////////////////////////////
97
98 struct FindNearestWordTest
99 {
100   std::string description;
101   std::string input;
102   std::size_t offset;
103   std::size_t start;
104   std::size_t end;
105 };
106
107 bool TestFindNearestWord( const std::string& description, const std::string& input, const std::size_t offset, const std::size_t startResult, const std::size_t endResult, const char* location )
108 {
109   // Creates a styled text with the markup or plain string.
110   MarkupProcessor::StyledTextArray styledText;
111   MarkupProcessor::GetStyledTextArray( input, styledText, true );
112
113   std::size_t start;
114   std::size_t end;
115   TextProcessor::FindNearestWord( styledText, offset, start, end );
116
117   const bool ret = ( start == startResult ) && ( end == endResult );
118
119   if( !ret )
120   {
121     tet_printf( "Fail. %s", location );
122     tet_printf( "Input : %s, offset %d, start %d, end %d", input.c_str(), offset, start, end );
123   }
124
125   return ret;
126 }
127
128 //////////////////////////////////////////////////////////////////
129
130 struct SplitInLinesTest
131 {
132   std::string inputText;
133
134   std::size_t resultNumberOfLines;
135 };
136
137 bool TestSplitInLines( const SplitInLinesTest& test, const char* location )
138 {
139   // Creates a styled text with the markup or plain string.
140   MarkupProcessor::StyledTextArray styledText;
141   MarkupProcessor::GetStyledTextArray( test.inputText, styledText, true );
142
143   std::vector<MarkupProcessor::StyledTextArray> lines;
144
145   TextProcessor::SplitInLines( styledText,
146                                lines );
147
148   if( lines.size() != test.resultNumberOfLines )
149   {
150     tet_printf( "Fail. %s", location );
151     tet_printf( "Different number of lines, result %d, expected result %d", lines.size(), test.resultNumberOfLines );
152
153     return false;
154   }
155
156   return true;
157 }
158
159 //////////////////////////////////////////////////////////////////
160
161 struct SplitInWordsTest
162 {
163   std::string inputText;
164
165   std::size_t resultNumberOfWords;
166 };
167
168 bool TestSplitInWords( const SplitInWordsTest& test, const char* location )
169 {
170   // Creates a styled text with the markup or plain string.
171   MarkupProcessor::StyledTextArray styledText;
172   MarkupProcessor::GetStyledTextArray( test.inputText, styledText, true );
173
174   std::vector<MarkupProcessor::StyledTextArray> words;
175
176   TextProcessor::SplitInWords( styledText,
177                                words );
178
179   if( words.size() != test.resultNumberOfWords )
180   {
181     tet_printf( "Fail. %s", location );
182     tet_printf( "Different number of words, result %d, expected result %d", words.size(), test.resultNumberOfWords );
183
184     return false;
185   }
186
187   return true;
188 }
189
190 //////////////////////////////////////////////////////////////////
191
192 } // namespace
193
194
195 int UtcDaliTextViewSplitInLines(void)
196 {
197   ToolkitTestApplication application;
198
199   tet_infoline("UtcDaliTextViewSplitInLines : ");
200
201   struct SplitInLinesTest splitInLinesTest[] =
202   {
203     {
204       std::string( "Hello world\nhello world." ),
205       2
206     },
207     {
208       std::string( "Hello world\nhello world.\n\n" ),
209       4
210     }
211   };
212   const std::size_t numberOfTests( 2 );
213
214   for( std::size_t index = 0; index < numberOfTests; ++index )
215   {
216     const SplitInLinesTest& test = splitInLinesTest[index];
217
218     if( !TestSplitInLines( test, TEST_LOCATION ) )
219     {
220       tet_result( TET_FAIL );
221     }
222   }
223
224   tet_result( TET_PASS );
225   END_TEST;
226 }
227
228 int UtcDaliTextViewSplitInWords(void)
229 {
230   ToolkitTestApplication application;
231
232   tet_infoline("UtcDaliTextViewSplitInWords : ");
233
234   struct SplitInWordsTest splitInWordsTest[] =
235   {
236     {
237       std::string( "Hello world, hello word!" ),
238       7
239     },
240   };
241   const std::size_t numberOfTests( 1 );
242
243   for( std::size_t index = 0; index < numberOfTests; ++index )
244   {
245     const SplitInWordsTest& test = splitInWordsTest[index];
246
247     if( !TestSplitInWords( test, TEST_LOCATION ) )
248     {
249       tet_result( TET_FAIL );
250     }
251   }
252
253   tet_result( TET_PASS );
254   END_TEST;
255 }
256
257 int UtcDaliTextViewBeginsRightToLeftCharacter(void)
258 {
259   ToolkitTestApplication application;
260
261   tet_infoline("UtcDaliTextViewBeginsRightToLeftCharacter : ");
262
263   struct BeginsRightToLeftCharacterTest beginsRightToLeftCharacterTest[] =
264   {
265     {
266       std::string( "Test if it begins with a right to left character. Should return false." ),
267       std::string( "Hello world مرحبا العالم." ),
268       false
269     },
270     {
271       std::string( "Test if it begins with a right to left character. Should return true." ),
272       std::string( "مرحبا العالم Hola mundo." ),
273       true
274     }
275   };
276   const std::size_t numberOfTests( 2 );
277
278   for( std::size_t index = 0; index < numberOfTests; ++index )
279   {
280     const BeginsRightToLeftCharacterTest& test = beginsRightToLeftCharacterTest[index];
281
282     if( !TestBeginsRightToLeftCharacter( test.description, test.input, test.result, TEST_LOCATION ) )
283     {
284       tet_result( TET_FAIL );
285     }
286   }
287
288   tet_result( TET_PASS );
289   END_TEST;
290 }
291
292 int UtcDaliTextViewContainsRightToLeftCharacter(void)
293 {
294   ToolkitTestApplication application;
295
296   tet_infoline("UtcDaliTextViewContainsRightToLeftCharacter : ");
297
298   struct ContainsRightToLeftCharacterTest containsRightToLeftCharacterTest[] =
299   {
300     {
301       std::string( "Test if it contains a right to left character. Should return true." ),
302       std::string( "Hello world مرحبا العالم." ),
303       true
304     },
305     {
306       std::string( "Test if it contains a right to left character. Should return true." ),
307       std::string( "مرحبا العالم Hola mundo." ),
308       true
309     },
310     {
311       std::string( "Test if it contains a right to left character. Should return false." ),
312       std::string( "Hello world." ),
313       false
314     },
315     {
316       std::string( "Test if it contains a right to left character. Should return true." ),
317       std::string( "مرحبا العالم." ),
318       true
319     }
320   };
321   const std::size_t numberOfTests( 4 );
322
323   for( std::size_t index = 0; index < numberOfTests; ++index )
324   {
325     const ContainsRightToLeftCharacterTest& test = containsRightToLeftCharacterTest[index];
326
327     if( !TestContainsRightToLeftCharacter( test.description, test.input, test.result, TEST_LOCATION ) )
328     {
329       tet_result( TET_FAIL );
330     }
331   }
332
333   tet_result( TET_PASS );
334   END_TEST;
335 }
336
337 int UtcDaliTextViewFindNearestWord(void)
338 {
339   ToolkitTestApplication application;
340
341   tet_infoline("UtcDaliTextViewFindNearestWord : ");
342
343   struct FindNearestWordTest findNearestWordTest[] =
344   {
345     {
346       std::string( "" ),
347       std::string( "Hello world, hola mundo" ),
348       0u,
349       0u,
350       5u
351     },
352     {
353       std::string( "" ),
354       std::string( "Hello world, hola mundo" ),
355       7u,
356       6u,
357       12u
358     },
359     {
360       std::string( "" ),
361       std::string( "Hello world, hola mundo" ),
362       11u,
363       6u,
364       12u
365     },
366     {
367       std::string( "" ),
368       std::string( "Hello world, hola mundo" ),
369       23u,
370       18u,
371       23u
372     },
373     {
374       std::string( "" ),
375       std::string( "Hello world, hola mundo" ),
376       5u,
377       0u,
378       5u
379     },
380     {
381       std::string( "" ),
382       std::string( "Hello world, hola mundo  مرحبا العالم" ),
383       24u,
384       25u,
385       30u
386     }
387   };
388
389   const std::size_t numberOfTests( 6 );
390
391   for( std::size_t index = 0; index < numberOfTests; ++index )
392   {
393     const FindNearestWordTest& test = findNearestWordTest[index];
394
395     if( !TestFindNearestWord( test.description, test.input, test.offset, test.start, test.end, TEST_LOCATION ) )
396     {
397       tet_result( TET_FAIL );
398     }
399   }
400
401   tet_result( TET_PASS );
402   END_TEST;
403 }