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