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