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