Merge branch 'tizen' into new_text
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-Text.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 <dali/public-api/dali-core.h>
22 #include <dali-test-suite-utils.h>
23
24 using namespace Dali;
25
26 void utc_dali_text_startup(void)
27 {
28   test_return_value = TET_UNDEF;
29 }
30
31 void utc_dali_text_cleanup(void)
32 {
33   test_return_value = TET_PASS;
34 }
35
36 int UtcDaliTextConstructor(void)
37 {
38   TestApplication application;
39
40   Text text;
41
42   DALI_TEST_CHECK( text.IsEmpty() );
43
44   Text someEmptyText1( "" );
45
46   DALI_TEST_CHECK( someEmptyText1.IsEmpty() );
47
48   Text someEmptyText2( std::string( "" ) );
49
50   DALI_TEST_CHECK( someEmptyText2.IsEmpty() );
51
52   Text someEmptyText3( text );
53
54   DALI_TEST_CHECK( someEmptyText3.IsEmpty() );
55
56   Text someText1( "Some text" );
57
58   DALI_TEST_CHECK( !someText1.IsEmpty() );
59
60   Text someText2( std::string( "Some text" ) );
61
62   DALI_TEST_CHECK( !someText2.IsEmpty() );
63
64   Text someText3( Text( std::string( "Some text" ) ) );
65
66   DALI_TEST_CHECK( !someText3.IsEmpty() );
67
68   Character c = someText1[0];
69
70   Text cText( c );
71
72   DALI_TEST_CHECK( !cText.IsEmpty() );
73   END_TEST;
74 }
75
76 int UtcDaliTextCopyConstructor(void)
77 {
78   TestApplication application;
79
80   Text someText1( std::string( "Some text1" ) );
81   Text someText2( std::string( "Some text2" ) );
82
83   Text someText3( someText1 );
84   Text someText4 = someText2;
85
86   DALI_TEST_CHECK( (someText1.GetText()==someText3.GetText()) && (someText2.GetText()==someText4.GetText()) );
87   END_TEST;
88 }
89
90 int UtcDaliTextAssignmentOperator(void)
91 {
92   TestApplication application;
93
94   // check for assignment when current is NULL
95
96   Text someText1( std::string( "Some text1" ) );
97   Text someText2;
98   someText2 = someText1;
99
100   DALI_TEST_CHECK( (someText1.GetText()==someText2.GetText()) );
101
102   // check for assignment when current object already has text
103   Text someText3( std::string( "Some text3" ) );
104   someText2 = someText3;
105   DALI_TEST_CHECK( (someText3.GetText()==someText2.GetText()) );
106
107   Text someText4;
108
109   // check for assignment of empty text
110   someText2 = someText4;
111   DALI_TEST_CHECK( someText2.IsEmpty() );
112
113   // check for self assignment
114   someText3 = someText3;
115   DALI_TEST_CHECK( ! someText3.IsEmpty() );
116
117
118   END_TEST;
119 }
120
121 int UtcDaliTextSetGetText(void)
122 {
123   TestApplication application;
124
125   const char* someText = "Some text";
126   const std::string someText2( "Some text2" );
127
128   Text text;
129   text.SetText( someText );
130
131   DALI_TEST_EQUALS( someText, text.GetText(), TEST_LOCATION );
132
133   text.SetText( someText2 );
134
135   DALI_TEST_EQUALS( someText2, text.GetText(), TEST_LOCATION );
136
137   Character c = text[0];
138   text.SetText( c );
139
140   DALI_TEST_EQUALS( std::string( "S" ), text.GetText(), TEST_LOCATION );
141
142   Text text2;
143   text2.SetText( text );
144
145   DALI_TEST_EQUALS( text2.GetText(), text.GetText(), TEST_LOCATION );
146
147   END_TEST;
148 }
149
150 int UtcDaliTextAccessOperator01(void)
151 {
152   TestApplication application;
153
154   std::string someText;
155
156   Text text;
157   text.SetText( someText );
158
159   bool assert1 = false;
160   bool assert2 = false;
161   try
162   {
163     Character c = text[0];
164   }
165   catch( DaliException& e )
166   {
167     tet_printf( "Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str() );
168     DALI_TEST_EQUALS( e.mCondition, "NULL != mImpl && \"Text::operator[]: Text is uninitialized\"", TEST_LOCATION );
169
170     assert1 = true;
171   }
172
173   someText = std::string( "some text" );
174   text.SetText( someText );
175
176   try
177   {
178     Character c = text[100];
179   }
180   catch( DaliException& e )
181   {
182     tet_printf( "Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str() );
183     DALI_TEST_EQUALS( e.mCondition, "position < mString.Count() && \"Text::operator[]: Character position is out of bounds\"", TEST_LOCATION );
184
185     assert2 = true;
186   }
187
188   if( assert1 && assert2 )
189   {
190     tet_result( TET_PASS );
191   }
192   else
193   {
194     tet_result( TET_FAIL );
195   }
196   END_TEST;
197 }
198
199 int UtcDaliTextAccessOperator02(void)
200 {
201   TestApplication application;
202
203   const std::string someText( "Some text");
204
205   Text text;
206   text.SetText( someText );
207
208   DALI_TEST_CHECK( someText == text.GetText() );
209
210   Character c = text[0];
211   text.SetText( c );
212
213   DALI_TEST_CHECK( std::string("S") == text.GetText() );
214   END_TEST;
215 }
216
217 int UtcDaliTextIsEmpty(void)
218 {
219   TestApplication application;
220
221   Text text;
222
223   DALI_TEST_CHECK( text.IsEmpty() );
224
225   text.SetText( std::string( "Some text") );
226
227   DALI_TEST_CHECK( !text.IsEmpty() );
228   END_TEST;
229 }
230
231 int UtcDaliTextGetLength(void)
232 {
233   TestApplication application;
234
235   const std::string someText( "Some text");
236
237   Text text( someText );
238
239   DALI_TEST_CHECK( someText.size() == text.GetLength() );
240   END_TEST;
241 }
242
243 int UtcDaliTextAppend(void)
244 {
245   TestApplication application;
246
247   Text text( std::string( "Some text") );
248
249   text.Append( "A" );
250
251   DALI_TEST_CHECK( std::string( "Some textA" ) == text.GetText() );
252
253   text.Append( std::string( "B" ) );
254
255   DALI_TEST_CHECK( std::string( "Some textAB" ) == text.GetText() );
256
257   Character c = text[0];
258   text.Append( c );
259
260   DALI_TEST_CHECK( std::string( "Some textABS" ) == text.GetText() );
261
262   Text text2( std::string("C") );
263   text.Append( text2 );
264
265   DALI_TEST_CHECK( std::string( "Some textABSC" ) == text.GetText() );
266
267   // append to a null text
268
269   Text emptyText;
270   emptyText.Append( text2 );
271   DALI_TEST_CHECK( text2.GetText() == emptyText.GetText() );
272
273   // append a null text
274
275   Text emptyText2;
276   emptyText.Append( emptyText2 );
277   DALI_TEST_CHECK( text2.GetText() == emptyText.GetText() );
278
279   END_TEST;
280 }
281
282 int UtcDaliTextRemove01(void)
283 {
284   TestApplication application;
285
286   Text text( std::string( "Some text") );
287
288   bool assert1 = false;
289   bool assert2 = false;
290   bool assert3 = false;
291
292   try
293   {
294     text.Remove( 100, 3 );
295   }
296   catch( DaliException& e )
297   {
298     tet_printf( "Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str() );
299     DALI_TEST_EQUALS( e.mCondition, "position < mString.Count() && \"Text::Remove: Character position is out of bounds\"", TEST_LOCATION );
300     assert1 = true;
301   }
302
303   try
304   {
305     text.Remove( 1, 300 );
306   }
307   catch( DaliException& e )
308   {
309     tet_printf( "Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str() );
310     DALI_TEST_EQUALS( e.mCondition, "position + numberOfCharacters <= mString.Count() && \"Text::Remove: Character position + numberOfCharacters is out of bounds\"", TEST_LOCATION );
311     assert2 = true;
312   }
313
314   try
315   {
316     text.SetText( std::string( "" ) );
317     text.Remove( 1, 300 );
318   }
319   catch( DaliException& e )
320   {
321     tet_printf( "Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str() );
322     DALI_TEST_EQUALS( e.mCondition, "NULL != mImpl && \"Text::Remove: Text is uninitialized\"", TEST_LOCATION );
323     assert3 = true;
324   }
325
326   if( assert1 && assert2 && assert3 )
327   {
328     tet_result( TET_PASS );
329   }
330   else
331   {
332     tet_result( TET_FAIL );
333   }
334   END_TEST;
335 }
336
337 int UtcDaliTextRemove02(void)
338 {
339   TestApplication application;
340
341   Text text01( std::string( "Some text") );
342   Text text02( std::string( "Somext") );
343   Text text03( std::string( "" ) );
344   Text text04( std::string( "Hello world" ) );
345   Text text05( std::string( "world" ) );
346
347   text01.Remove( 3u, 3u );
348
349   DALI_TEST_EQUALS( text01.GetLength(), text02.GetLength(), TEST_LOCATION );
350   DALI_TEST_EQUALS( text01.GetText(), text02.GetText(), TEST_LOCATION );
351
352   text01.Remove( 0u, 0u );
353
354   DALI_TEST_EQUALS( text01.GetLength(), text02.GetLength(), TEST_LOCATION );
355   DALI_TEST_EQUALS( text01.GetText(), text02.GetText(), TEST_LOCATION );
356
357   text01.Remove( 0u, 6u );
358
359   DALI_TEST_EQUALS( text01.GetLength(), text03.GetLength(), TEST_LOCATION );
360   DALI_TEST_EQUALS( text01.GetText(), text03.GetText(), TEST_LOCATION );
361
362   text04.Remove( 0u, 6u );
363
364   DALI_TEST_EQUALS( text04.GetLength(), text05.GetLength(), TEST_LOCATION );
365   DALI_TEST_EQUALS( text04.GetText(), text05.GetText(), TEST_LOCATION );
366
367   END_TEST;
368 }
369
370 int UtcDaliTextFind(void)
371 {
372   TestApplication application;
373
374   Text text;
375   Character c1 = Text( std::string( "c" ) )[0u];
376   Character c2 = Text( std::string( "o" ) )[0u];
377   Vector<std::size_t> positions;
378
379   // Find in void text.
380
381   positions.Clear();
382   text.Find( c1, 0u, 0u, positions );
383   DALI_TEST_EQUALS( positions.Count(), 0u, TEST_LOCATION );
384
385   positions.Clear();
386   text.Find( Text::WHITE_SPACE, 0u, 0u, positions );
387   DALI_TEST_EQUALS( positions.Count(), 0u, TEST_LOCATION );
388
389   positions.Clear();
390   text.Find( Text::NEW_LINE, 0u, 0u, positions );
391   DALI_TEST_EQUALS( positions.Count(), 0u, TEST_LOCATION );
392
393   // Find in text.
394
395   // Find 'c' and 'o'
396   text.SetText( std::string( "Hello world" ) );
397
398   positions.Clear();
399   text.Find( c1, 0u, 11u, positions );
400   DALI_TEST_EQUALS( positions.Count(), 0u, TEST_LOCATION );
401
402   positions.Clear();
403   text.Find( c2, 0u, 11u, positions );
404   DALI_TEST_EQUALS( positions.Count(), 2u, TEST_LOCATION );
405   DALI_TEST_EQUALS( positions[0u], 4u, TEST_LOCATION );
406   DALI_TEST_EQUALS( positions[1u], 7u, TEST_LOCATION );
407
408   // Find white space
409   text.SetText( std::string( "   Hello  world  \n" ) );
410
411   positions.Clear();
412   text.Find( Text::WHITE_SPACE, 0u, 17u, positions );
413   DALI_TEST_EQUALS( positions.Count(), 8u, TEST_LOCATION );
414   DALI_TEST_EQUALS( positions[0u], 0u, TEST_LOCATION );
415   DALI_TEST_EQUALS( positions[1u], 1u, TEST_LOCATION );
416   DALI_TEST_EQUALS( positions[2u], 2u, TEST_LOCATION );
417   DALI_TEST_EQUALS( positions[3u], 8u, TEST_LOCATION );
418   DALI_TEST_EQUALS( positions[4u], 9u, TEST_LOCATION );
419   DALI_TEST_EQUALS( positions[5u], 15u, TEST_LOCATION );
420   DALI_TEST_EQUALS( positions[6u], 16u, TEST_LOCATION );
421   DALI_TEST_EQUALS( positions[7u], 17u, TEST_LOCATION );
422
423   // Find new line character
424   text.SetText( std::string( "\n\nHello\nworld\n\n" ) );
425
426   positions.Clear();
427   text.Find( Text::NEW_LINE, 0u, 14u, positions );
428   DALI_TEST_EQUALS( positions.Count(), 5u, TEST_LOCATION );
429   DALI_TEST_EQUALS( positions[0u], 0u, TEST_LOCATION );
430   DALI_TEST_EQUALS( positions[1u], 1u, TEST_LOCATION );
431   DALI_TEST_EQUALS( positions[2u], 7u, TEST_LOCATION );
432   DALI_TEST_EQUALS( positions[3u], 13u, TEST_LOCATION );
433   DALI_TEST_EQUALS( positions[4u], 14u, TEST_LOCATION );
434
435   END_TEST;
436 }
437
438 int UtcDaliTextGetSubText(void)
439 {
440   TestApplication application;
441
442   Text text;
443   Text subText;
444
445   // Get sub-text from a void text.
446
447   subText.SetText( "Hello" );
448   text.GetSubText( 0u, 1u, subText );
449   DALI_TEST_EQUALS( subText.GetText(), std::string( "Hello" ), TEST_LOCATION );
450
451   // Get sub-text.
452
453   text.SetText( std::string( "Hello world" ) );
454
455   // Access out of bounds
456   subText.SetText( "Hello" );
457   text.GetSubText( 30u, 31u, subText );
458   DALI_TEST_EQUALS( subText.GetText(), std::string( "Hello" ), TEST_LOCATION );
459   text.GetSubText( 0u, 31u, subText );
460   DALI_TEST_EQUALS( subText.GetText(), std::string( "Hello" ), TEST_LOCATION );
461   text.GetSubText( 30u, 1u, subText );
462   DALI_TEST_EQUALS( subText.GetText(), std::string( "Hello" ), TEST_LOCATION );
463
464   // Check it swaps the indices.
465   text.GetSubText( 8u, 2u, subText );
466   DALI_TEST_EQUALS( subText.GetText(), std::string( "row oll" ), TEST_LOCATION );
467
468   // Normal access.
469   subText.SetText( std::string( "" ) );
470   text.GetSubText( 4u, 6u, subText );
471   DALI_TEST_EQUALS( subText.GetText(), std::string( "o w" ), TEST_LOCATION );
472
473   END_TEST;
474 }
475
476 int UtcDaliTextIsWhiteSpaceNewLine(void)
477 {
478   TestApplication application;
479
480   Text text;
481
482   // Query a void text.
483
484   DALI_TEST_CHECK( !text.IsWhiteSpace( 0u ) );
485   DALI_TEST_CHECK( !text.IsNewLine( 0u ) );
486
487   // Set a text
488   text.SetText( "Hello world\n" );
489
490   // Query out of bounds
491
492   DALI_TEST_CHECK( !text.IsWhiteSpace( 30u ) );
493   DALI_TEST_CHECK( !text.IsNewLine( 30u ) );
494
495   // Normal query.
496
497   DALI_TEST_CHECK( !text.IsWhiteSpace( 1u ) );
498   DALI_TEST_CHECK( !text.IsNewLine( 1u ) );
499   DALI_TEST_CHECK( text.IsWhiteSpace( 5u ) );
500   DALI_TEST_CHECK( text.IsNewLine( 11u ) );
501
502   END_TEST;
503 }