Completely remove use of Adaptor class
[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 #include <dali-toolkit/internal/controls/text-view/text-processor-bidirectional-info.h>
26
27 using namespace Dali;
28 using namespace Dali::Toolkit;
29 using namespace Dali::Toolkit::Internal;
30
31 void dali_text_view_processor_startup(void)
32 {
33   test_return_value = TET_UNDEF;
34 }
35
36 void dali_text_view_processor_cleanup(void)
37 {
38   test_return_value = TET_PASS;
39 }
40
41
42 namespace
43 {
44 // Data structures used to create an 'experiment' in TET cases
45
46 //////////////////////////////////////////////////////////////////
47
48 struct BeginsRightToLeftCharacterTest
49 {
50   std::string description;
51   std::string input;
52   bool result;
53 };
54
55 bool TestBeginsRightToLeftCharacter( const std::string& description, const std::string& input, const bool result, const char* location )
56 {
57   // Creates a text with the string.
58   Text text( input );
59
60   const bool ret = ( result == TextProcessor::BeginsRightToLeftCharacter( text ) );
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 text with the string.
83   Text text( input );
84
85   const bool ret = ( result == TextProcessor::ContainsRightToLeftCharacter( text ) );
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 SplitInParagraphsTest
131 {
132   std::string inputText;
133
134   std::size_t resultNumberOfParagraphs;
135 };
136
137 bool TestSplitInParagraphs( const SplitInParagraphsTest& 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<Text> paragraphs;
144   std::vector< Vector<TextStyle*> > styles;
145
146   TextProcessor::SplitInParagraphs( styledText,
147                                     paragraphs,
148                                     styles );
149
150   if( paragraphs.size() != test.resultNumberOfParagraphs )
151   {
152     tet_printf( "Fail. %s", location );
153     tet_printf( "Different number of paragraphs, result %d, expected result %d", paragraphs.size(), test.resultNumberOfParagraphs );
154
155     return false;
156   }
157
158   return true;
159 }
160
161 //////////////////////////////////////////////////////////////////
162
163 struct SplitInWordsTest
164 {
165   std::string inputText;
166
167   std::size_t resultNumberOfSeparators;
168 };
169
170 bool TestSplitInWords( const SplitInWordsTest& test, const char* location )
171 {
172   // Creates a text with the string.
173   Text text( test.inputText );
174
175   Vector<std::size_t> positions;
176
177   TextProcessor::SplitInWords( text,
178                                positions );
179
180   if( positions.Count() != test.resultNumberOfSeparators )
181   {
182     tet_printf( "Fail. %s", location );
183     tet_printf( "Different number of separators, result %d, expected result %d", positions.Count(), test.resultNumberOfSeparators );
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       3u
240     },
241     {
242       std::string( "Hello world\n" ),
243       2u
244     }
245   };
246   const std::size_t numberOfTests( 2u );
247
248   for( std::size_t index = 0; index < numberOfTests; ++index )
249   {
250     const SplitInWordsTest& test = splitInWordsTest[index];
251
252     if( !TestSplitInWords( test, TEST_LOCATION ) )
253     {
254       tet_result( TET_FAIL );
255     }
256   }
257
258   tet_result( TET_PASS );
259   END_TEST;
260 }
261
262 int UtcDaliTextViewBeginsRightToLeftCharacter(void)
263 {
264   ToolkitTestApplication application;
265
266   tet_infoline("UtcDaliTextViewBeginsRightToLeftCharacter : ");
267
268   struct BeginsRightToLeftCharacterTest beginsRightToLeftCharacterTest[] =
269   {
270     {
271       std::string( "Test if it begins with a right to left character. Should return false." ),
272       std::string( "Hello world مرحبا العالم." ),
273       false
274     },
275     {
276       std::string( "Test if it begins with a right to left character. Should return true." ),
277       std::string( "مرحبا العالم Hola mundo." ),
278       true
279     }
280   };
281   const std::size_t numberOfTests( 2 );
282
283   for( std::size_t index = 0; index < numberOfTests; ++index )
284   {
285     const BeginsRightToLeftCharacterTest& test = beginsRightToLeftCharacterTest[index];
286
287     if( !TestBeginsRightToLeftCharacter( test.description, test.input, test.result, TEST_LOCATION ) )
288     {
289       tet_result( TET_FAIL );
290     }
291   }
292
293   tet_result( TET_PASS );
294   END_TEST;
295 }
296
297 int UtcDaliTextViewContainsRightToLeftCharacter(void)
298 {
299   ToolkitTestApplication application;
300
301   tet_infoline("UtcDaliTextViewContainsRightToLeftCharacter : ");
302
303   struct ContainsRightToLeftCharacterTest containsRightToLeftCharacterTest[] =
304   {
305     {
306       std::string( "Test if it contains a right to left character. Should return true." ),
307       std::string( "Hello world مرحبا العالم." ),
308       true
309     },
310     {
311       std::string( "Test if it contains a right to left character. Should return true." ),
312       std::string( "مرحبا العالم Hola mundo." ),
313       true
314     },
315     {
316       std::string( "Test if it contains a right to left character. Should return false." ),
317       std::string( "Hello world." ),
318       false
319     },
320     {
321       std::string( "Test if it contains a right to left character. Should return true." ),
322       std::string( "مرحبا العالم." ),
323       true
324     }
325   };
326   const std::size_t numberOfTests( 4 );
327
328   for( std::size_t index = 0; index < numberOfTests; ++index )
329   {
330     const ContainsRightToLeftCharacterTest& test = containsRightToLeftCharacterTest[index];
331
332     if( !TestContainsRightToLeftCharacter( test.description, test.input, test.result, TEST_LOCATION ) )
333     {
334       tet_result( TET_FAIL );
335     }
336   }
337
338   tet_result( TET_PASS );
339   END_TEST;
340 }
341
342 int UtcDaliTextViewFindNearestWord(void)
343 {
344   ToolkitTestApplication application;
345
346   tet_infoline("UtcDaliTextViewFindNearestWord : ");
347
348   struct FindNearestWordTest findNearestWordTest[] =
349   {
350     {
351       std::string( "" ),
352       std::string( "Hello world, hola mundo" ),
353       0u,
354       0u,
355       5u
356     },
357     {
358       std::string( "" ),
359       std::string( "Hello world, hola mundo" ),
360       7u,
361       6u,
362       12u
363     },
364     {
365       std::string( "" ),
366       std::string( "Hello world, hola mundo" ),
367       11u,
368       6u,
369       12u
370     },
371     {
372       std::string( "" ),
373       std::string( "Hello world, hola mundo" ),
374       23u,
375       18u,
376       23u
377     },
378     {
379       std::string( "" ),
380       std::string( "Hello world, hola mundo" ),
381       5u,
382       0u,
383       5u
384     },
385     {
386       std::string( "" ),
387       std::string( "Hello world, hola mundo  مرحبا العالم" ),
388       24u,
389       25u,
390       30u
391     }
392   };
393
394   const std::size_t numberOfTests( 6 );
395
396   for( std::size_t index = 0; index < numberOfTests; ++index )
397   {
398     const FindNearestWordTest& test = findNearestWordTest[index];
399
400     if( !TestFindNearestWord( test.description, test.input, test.offset, test.start, test.end, TEST_LOCATION ) )
401     {
402       tet_result( TET_FAIL );
403     }
404   }
405
406   tet_result( TET_PASS );
407   END_TEST;
408 }