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