705b2f47ae2135e9bbe15ed490f265d4e51dc9cc
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-TextView.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 #include <stdlib.h>
19
20 // Need to override adaptor classes for toolkit test harness, so include
21 // test harness headers before dali headers.
22 #include <dali-toolkit-test-suite-utils.h>
23
24 #include <dali.h>
25 #include <dali-toolkit/dali-toolkit.h>
26
27 using namespace Dali;
28 using namespace Toolkit;
29
30 void utc_dali_toolkit_text_view_startup(void)
31 {
32   test_return_value = TET_UNDEF;
33 }
34
35 void utc_dali_toolkit_text_view_cleanup(void)
36 {
37   test_return_value = TET_PASS;
38 }
39
40
41 namespace
42 {
43
44 const char* const PROPERTY_TEXT = "text";
45 const char* const PROPERTY_MULTILINE_POLICY = "multiline-policy";
46 const char* const PROPERTY_WIDTH_EXCEED_POLICY = "width-exceed-policy";
47 const char* const PROPERTY_HEIGHT_EXCEED_POLICY = "height-exceed-policy";
48 const char* const PROPERTY_LINE_JUSTIFICATION = "line-justification";
49 const char* const PROPERTY_FADE_BOUNDARY_LEFT = "fade-boundary-left";
50 const char* const PROPERTY_FADE_BOUNDARY_RIGHT = "fade-boundary-right";
51 const char* const PROPERTY_FADE_BOUNDARY_TOP = "fade-boundary-top";
52 const char* const PROPERTY_FADE_BOUNDARY_BOTTOM = "fade-boundary-bottom";
53 const char* const PROPERTY_LINE_HEIGHT_OFFSET = "line-height-offset";
54 const char* const PROPERTY_HORIZONTAL_ALIGNMENT = "horizontal-alignment";
55 const char* const PROPERTY_VERTICAL_ALIGNMENT = "vertical-alignment";
56
57 bool TestEqual( float x, float y )
58 {
59   return !( fabsf( x - y ) > Math::MACHINE_EPSILON_1000 );
60 }
61
62 static bool gObjectCreatedCallBackCalled;
63 static unsigned int gNumberObjectCreated;
64
65 static void TestCallback(BaseHandle handle)
66 {
67   gObjectCreatedCallBackCalled = true;
68   ++gNumberObjectCreated;
69 }
70
71 static bool gTextScrolled;
72 static Vector2 gScrollDelta;
73 static void TestTextScrolled( TextView textView, Vector2 scrollDelta )
74 {
75   gTextScrolled = true;
76   gScrollDelta = scrollDelta;
77 }
78
79 } // namespace
80
81
82 int UtcDaliTextViewNew(void)
83 {
84   tet_infoline("UtcDaliTextViewNew: ");
85   ToolkitTestApplication application;
86
87   // Test default constructor.
88   TextView view;
89
90   DALI_TEST_CHECK( !view );
91
92   // Test default initialization.
93   view = TextView::New();
94
95   DALI_TEST_CHECK( view );
96
97   // Test copy constructor and asignment operator.
98   TextView viewCopy1;
99
100   viewCopy1 = view;
101
102   DALI_TEST_CHECK( viewCopy1 );
103
104   TextView viewCopy2( view );
105
106   DALI_TEST_CHECK( viewCopy2 );
107
108   // Test down cast.
109   Actor actorView;
110
111   actorView = view;
112
113   TextView downCastView = TextView::DownCast( actorView );
114
115   DALI_TEST_CHECK( downCastView );
116
117   // Test constructor with a given text.
118
119   const std::string text( "Hello world!" );
120
121   const float DESCENDER = 8.0f;
122
123   TextView view1 = TextView::New( text );
124
125   DALI_TEST_EQUALS( view1.GetText(), text, TEST_LOCATION );
126
127   MarkupProcessor::StyledTextArray styledText;
128   MarkupProcessor::GetStyledTextArray( text, styledText, true );
129
130   TextView view2 = TextView::New( styledText );
131
132   DALI_TEST_EQUALS( view2.GetText(), text, TEST_LOCATION );
133
134   // Check the default Toolkit::TextView::CharacterLayoutInfo::CharacterLayoutInfo() to increase coverage.
135   TextView::CharacterLayoutInfo characterLayoutInfo;
136
137   DALI_TEST_EQUALS( characterLayoutInfo.mSize, Vector2::ZERO, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
138   DALI_TEST_EQUALS( characterLayoutInfo.mPosition, Vector3::ZERO, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
139   DALI_TEST_EQUALS( characterLayoutInfo.mIsNewLineChar, false, TEST_LOCATION );
140   DALI_TEST_EQUALS( characterLayoutInfo.mIsRightToLeftCharacter, false, TEST_LOCATION );
141   DALI_TEST_EQUALS( characterLayoutInfo.mIsVisible, true, TEST_LOCATION );
142
143   TextView::CharacterLayoutInfo characterLayoutInfo2( Size( 2.f, 2.f ),
144                                                       Vector3( 3.f, 4.f, 5.f ),
145                                                       true,
146                                                       true,
147                                                       false,
148                                                       DESCENDER );
149
150   characterLayoutInfo = characterLayoutInfo2;
151
152   DALI_TEST_EQUALS( characterLayoutInfo.mSize, Size( 2.f, 2.f ), Math::MACHINE_EPSILON_1000, TEST_LOCATION );
153   DALI_TEST_EQUALS( characterLayoutInfo.mPosition, Vector3( 3.f, 4.f, 5.f ), Math::MACHINE_EPSILON_1000, TEST_LOCATION );
154   DALI_TEST_EQUALS( characterLayoutInfo.mIsNewLineChar, true, TEST_LOCATION );
155   DALI_TEST_EQUALS( characterLayoutInfo.mIsRightToLeftCharacter, true, TEST_LOCATION );
156   DALI_TEST_EQUALS( characterLayoutInfo.mIsVisible, false, TEST_LOCATION );
157   DALI_TEST_EQUALS( characterLayoutInfo.mDescender, DESCENDER , TEST_LOCATION );
158
159
160   TextView::CharacterLayoutInfo characterLayoutInfo3( characterLayoutInfo );
161
162   DALI_TEST_EQUALS( characterLayoutInfo3.mSize, Size( 2.f, 2.f ), Math::MACHINE_EPSILON_1000, TEST_LOCATION );
163   DALI_TEST_EQUALS( characterLayoutInfo3.mPosition, Vector3( 3.f, 4.f, 5.f ), Math::MACHINE_EPSILON_1000, TEST_LOCATION );
164   DALI_TEST_EQUALS( characterLayoutInfo3.mIsNewLineChar, true, TEST_LOCATION );
165   DALI_TEST_EQUALS( characterLayoutInfo3.mIsRightToLeftCharacter, true, TEST_LOCATION );
166   DALI_TEST_EQUALS( characterLayoutInfo3.mIsVisible, false, TEST_LOCATION );
167
168   // Check the default Toolkit::TextView::TextLayoutInfo::TextLayoutInfo() to increase coverage.
169
170   TextView::TextLayoutInfo textLayoutInfo;
171   DALI_TEST_EQUALS( textLayoutInfo.mCharacterLayoutInfoTable.size(), 0u, TEST_LOCATION );
172   DALI_TEST_EQUALS( textLayoutInfo.mCharacterLogicalToVisualMap.size(), 0u, TEST_LOCATION );
173   DALI_TEST_EQUALS( textLayoutInfo.mCharacterVisualToLogicalMap.size(), 0u, TEST_LOCATION );
174   DALI_TEST_EQUALS( textLayoutInfo.mTextSize, Size::ZERO, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
175   DALI_TEST_EQUALS( textLayoutInfo.mScrollOffset, Vector2::ZERO, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
176
177   textLayoutInfo.mCharacterLayoutInfoTable.push_back( characterLayoutInfo );
178   textLayoutInfo.mCharacterLogicalToVisualMap.push_back( 1 );
179   textLayoutInfo.mCharacterVisualToLogicalMap.push_back( 1 );
180   textLayoutInfo.mTextSize = Size( 10.f, 10.f );
181   textLayoutInfo.mScrollOffset = Vector2( 5.f, 5.f );
182
183   TextView::TextLayoutInfo textLayoutInfo2( textLayoutInfo );
184
185   DALI_TEST_EQUALS( textLayoutInfo2.mCharacterLayoutInfoTable.size(), 1u, TEST_LOCATION );
186   DALI_TEST_EQUALS( textLayoutInfo2.mCharacterLogicalToVisualMap.size(), 1u, TEST_LOCATION );
187   DALI_TEST_EQUALS( textLayoutInfo2.mCharacterVisualToLogicalMap.size(), 1u, TEST_LOCATION );
188   DALI_TEST_EQUALS( textLayoutInfo2.mTextSize, Size( 10.f, 10.f ), Math::MACHINE_EPSILON_1000, TEST_LOCATION );
189   DALI_TEST_EQUALS( textLayoutInfo2.mScrollOffset, Vector2( 5.f, 5.f ), Math::MACHINE_EPSILON_1000, TEST_LOCATION );
190
191   TextView::TextLayoutInfo textLayoutInfo3;
192
193   textLayoutInfo3 = textLayoutInfo2;
194
195   DALI_TEST_EQUALS( textLayoutInfo3.mCharacterLayoutInfoTable.size(), 1u, TEST_LOCATION );
196   DALI_TEST_EQUALS( textLayoutInfo3.mCharacterLogicalToVisualMap.size(), 1u, TEST_LOCATION );
197   DALI_TEST_EQUALS( textLayoutInfo3.mCharacterVisualToLogicalMap.size(), 1u, TEST_LOCATION );
198   DALI_TEST_EQUALS( textLayoutInfo3.mTextSize, Size( 10.f, 10.f ), Math::MACHINE_EPSILON_1000, TEST_LOCATION );
199   DALI_TEST_EQUALS( textLayoutInfo3.mScrollOffset, Vector2( 5.f, 5.f ), Math::MACHINE_EPSILON_1000, TEST_LOCATION );
200
201   //Additional check to ensure object is created by checking if it's registered
202   ObjectRegistry registry = Stage::GetCurrent().GetObjectRegistry();
203   DALI_TEST_CHECK( registry );
204
205   gObjectCreatedCallBackCalled = false;
206   registry.ObjectCreatedSignal().Connect(&TestCallback);
207   {
208     TextView view = TextView::New();
209   }
210   DALI_TEST_CHECK( gObjectCreatedCallBackCalled );
211   END_TEST;
212 }
213
214 int UtcDaliTextViewSetAndGetText(void)
215 {
216   tet_infoline("UtcDaliTextViewSetAndGetText: ");
217   ToolkitTestApplication application;
218
219   TextView view = TextView::New();
220   view.SetSnapshotModeEnabled( false ); // Disables offscreen rendering.
221
222   std::string str( "Text with differing aCeNdEr and dEcEnDeR" );
223
224   view.SetText( str );
225   DALI_TEST_EQUALS( view.GetText(), str, TEST_LOCATION );
226
227   MarkupProcessor::StyledTextArray styledText;
228   MarkupProcessor::GetStyledTextArray( str, styledText, true );
229
230   view.SetText( styledText );
231   DALI_TEST_EQUALS( view.GetText(), str, TEST_LOCATION );
232
233   // Test the number of text actor created.
234
235   ObjectRegistry registry = Stage::GetCurrent().GetObjectRegistry();
236   DALI_TEST_CHECK( registry );
237
238   gNumberObjectCreated = 0;
239   registry.ObjectCreatedSignal().Connect(&TestCallback);
240
241   // Following string should create three text-actors ([Hel], [lo wo] and [rld]).
242   std::string text( "Hel<font size='10'>lo wo</font>rld!\n"
243                     "\n" );
244
245   Stage::GetCurrent().Add( view );
246   view.SetText( text );
247
248   application.SendNotification();
249   application.Render();
250
251   DALI_TEST_EQUALS( 3u, gNumberObjectCreated, TEST_LOCATION );
252   END_TEST;
253 }
254
255 int UtcDaliTextViewSetStyleToCurrentText(void)
256 {
257   tet_infoline("UtcDaliTextViewSetStyleToCurrentText: ");
258   ToolkitTestApplication application;
259
260   TextStyle style;
261   style.SetItalics( true );
262
263   const std::string text( "앞서 농식품부 주이석 검역검사본부\n"
264                           "동물방역부장을 단장으로 하는\n"
265                           "민관합동조사단은 지난달 30일부터\n"
266                           "12일간의 현지 조사활동을 마치고\n"
267                           "11일 새벽 귀국했습니다." );
268   TextView view = TextView::New( text );
269
270   bool fail = false;
271   try
272   {
273     view.SetStyleToCurrentText( style );
274   }
275   catch( ... )
276   {
277     tet_printf( "Tet case fails\n" );
278     fail = true;
279     tet_result(TET_FAIL);
280   }
281
282   DALI_TEST_CHECK( !fail );
283   END_TEST;
284 }
285
286 int UtcDaliTextViewSetAndGetLineHeight(void)
287 {
288   tet_infoline("UtcDaliTextViewSetAndGetLineHeight: ");
289
290   ToolkitTestApplication application;
291
292   const float lineHeightOffset( 9.f );
293
294   TextView textView = TextView::New();
295
296   textView.SetLineHeightOffset( PointSize( lineHeightOffset ) );
297
298   DALI_TEST_EQUALS( float(textView.GetLineHeightOffset()), lineHeightOffset, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
299   END_TEST;
300 }
301
302 int UtcDaliTextViewSetAndGetFadeBoundary(void)
303 {
304   tet_infoline("UtcDaliTextViewSetAndGetFadeBoundary: ");
305
306   ToolkitTestApplication application;
307
308   TextView::FadeBoundary fadeBoundary( PixelSize( 0 ), PixelSize( 20 ), PixelSize( 0 ), PixelSize( 10 ) );
309
310   TextView textView = TextView::New( "Hello world!" );
311
312   textView.SetFadeBoundary( fadeBoundary );
313
314   TextView::FadeBoundary fadeBoundary2 = textView.GetFadeBoundary();
315
316   DALI_TEST_EQUALS( fadeBoundary.mLeft, fadeBoundary2.mLeft, TEST_LOCATION );
317   DALI_TEST_EQUALS( fadeBoundary.mRight, fadeBoundary2.mRight, TEST_LOCATION );
318   DALI_TEST_EQUALS( fadeBoundary.mTop, fadeBoundary2.mTop, TEST_LOCATION );
319   DALI_TEST_EQUALS( fadeBoundary.mBottom, fadeBoundary2.mBottom, TEST_LOCATION );
320   END_TEST;
321 }
322
323 int UtcDaliTextViewSetAndGetEllipsizeText(void)
324 {
325   tet_infoline("UtcDaliTextViewSetAndGetEllipsizeText: ");
326
327   ToolkitTestApplication application;
328
329   TextView textView = TextView::New( "Hello world!" );
330
331   textView.SetEllipsizeText( std::string( "..." ) );
332
333   DALI_TEST_EQUALS( std::string( "..." ), textView.GetEllipsizeText(), TEST_LOCATION );
334
335   Toolkit::MarkupProcessor::StyledTextArray styledTextArray;
336
337   GetStyledTextArray( std::string( "..." ), styledTextArray, true );
338
339   textView.SetEllipsizeText( styledTextArray );
340
341   DALI_TEST_EQUALS( std::string( "..." ), textView.GetEllipsizeText(), TEST_LOCATION );
342
343   END_TEST;
344 }
345
346 int UtcDaliTextViewSetAndGetWidthExceedPolicy(void)
347 {
348   tet_infoline("UtcDaliTextViewSetAndGetWidthExceedPolicy: ");
349
350   ToolkitTestApplication application;
351
352   const TextView::ExceedPolicy EXCEED_POLICIES[] = { TextView::Original, TextView::Fade, TextView::Split, TextView::ShrinkToFit };
353   const unsigned int NUM_EXCEED_POLICIES = sizeof( EXCEED_POLICIES ) / sizeof( unsigned int );
354
355   TextView textView = TextView::New( "Hello world!" );
356
357   for( unsigned int epIndex = 0; epIndex < NUM_EXCEED_POLICIES; ++epIndex )
358   {
359     textView.SetWidthExceedPolicy( EXCEED_POLICIES[epIndex] );
360
361     DALI_TEST_EQUALS( textView.GetWidthExceedPolicy(), EXCEED_POLICIES[epIndex], TEST_LOCATION );
362   }
363   END_TEST;
364 }
365
366 int UtcDaliTextViewSetAndGetHeightExceedPolicy(void)
367 {
368   tet_infoline("UtcDaliTextViewSetAndGetHeightExceedPolicy: ");
369
370   ToolkitTestApplication application;
371
372   const TextView::ExceedPolicy EXCEED_POLICIES[] = { TextView::Original, TextView::Fade, TextView::ShrinkToFit };
373   const unsigned int NUM_EXCEED_POLICIES = sizeof( EXCEED_POLICIES ) / sizeof( unsigned int );
374
375   TextView textView = TextView::New( "Hello world!" );
376
377   for( unsigned int epIndex = 0; epIndex < NUM_EXCEED_POLICIES; ++epIndex )
378   {
379     textView.SetHeightExceedPolicy( EXCEED_POLICIES[epIndex] );
380
381     DALI_TEST_EQUALS( textView.GetHeightExceedPolicy(), EXCEED_POLICIES[epIndex], TEST_LOCATION );
382   }
383   END_TEST;
384 }
385
386 /*
387 // Re-enable this test case when ALL layout options work in TextView, currently this breaks TCT
388 // output because too many warnings/errors are printed out
389 //int UtcDaliTextViewTestLayoutOptions01(void)
390 {
391   tet_infoline("UtcDaliTextViewTestLayoutOptions01: ");
392
393   ToolkitTestApplication application;
394
395   const std::string text( "앞서 농식품부 주이석 검역검사본부\n"
396                           "동물방역부장을 단장으로 하는\n"
397                           "민관합동조사단은 지난달 30일부터\n"
398                           "12일간의 현지 조사활동을 마치고\n"
399                           "11일 새벽 귀국했습니다." );
400
401   const TextView::MultilinePolicy MULTILINE_POLICIES[] = { TextView::SplitByNewLineChar, TextView::SplitByWord, TextView::SplitByChar };
402   const TextView::ExceedPolicy EXCEED_WIDTH_POLICIES[] = { TextView::Original, TextView::Fade, TextView::Split, TextView::ShrinkToFit, TextView::EllipsizeEnd };
403   const TextView::ExceedPolicy EXCEED_HEIGHT_POLICIES[] = { TextView::Original, TextView::Fade, TextView::ShrinkToFit };
404   const Alignment::Type TEXT_ALIGNMENT[] = { static_cast<Alignment::Type>( Alignment::HorizontalLeft | Alignment::VerticalTop ),
405                                              static_cast<Alignment::Type>( Alignment::HorizontalLeft | Alignment::VerticalCenter ),
406                                              static_cast<Alignment::Type>( Alignment::HorizontalLeft | Alignment::VerticalBottom ),
407                                              static_cast<Alignment::Type>( Alignment::HorizontalCenter | Alignment::VerticalTop ),
408                                              static_cast<Alignment::Type>( Alignment::HorizontalCenter | Alignment::VerticalCenter ),
409                                              static_cast<Alignment::Type>( Alignment::HorizontalCenter | Alignment::VerticalBottom ),
410                                              static_cast<Alignment::Type>( Alignment::HorizontalRight | Alignment::VerticalTop ),
411                                              static_cast<Alignment::Type>( Alignment::HorizontalRight | Alignment::VerticalCenter ),
412                                              static_cast<Alignment::Type>( Alignment::HorizontalRight | Alignment::VerticalBottom ) };
413   const TextView::LineJustification LINE_JUSTIFICATION[] = { TextView::Left, TextView::Center, TextView::Right, TextView::Justified };
414
415   const unsigned int NUM_MULTILINE_POLICIES = sizeof( MULTILINE_POLICIES ) / sizeof( unsigned int );
416   const unsigned int NUM_WIDTH_EXCEED_POLICIES = sizeof( EXCEED_WIDTH_POLICIES ) / sizeof( unsigned int );
417   const unsigned int NUM_HEIGHT_EXCEED_POLICIES = sizeof( EXCEED_HEIGHT_POLICIES ) / sizeof( unsigned int );
418   const unsigned int NUM_TEXT_ALIGNMENT = sizeof( TEXT_ALIGNMENT ) / sizeof( unsigned int );
419   const unsigned int NUM_LINE_JUSTIFICATION = sizeof( LINE_JUSTIFICATION ) / sizeof( unsigned int );
420
421   TextView textView = TextView::New( text );
422   textView.SetSnapshotModeEnabled( false ); // Disables offscreen rendering.
423
424   Stage::GetCurrent().Add( textView );
425
426   TextView::TextLayoutInfo textLayoutInfo;
427
428   for( unsigned int mlpIndex = 0; mlpIndex < NUM_MULTILINE_POLICIES; ++mlpIndex )
429   {
430     textView.SetMultilinePolicy( MULTILINE_POLICIES[mlpIndex] );
431     for( unsigned int ewpIndex = 0; ewpIndex < NUM_WIDTH_EXCEED_POLICIES; ++ewpIndex )
432     {
433       textView.SetWidthExceedPolicy( EXCEED_WIDTH_POLICIES[ewpIndex] );
434       for( unsigned int ehpIndex = 0; ehpIndex < NUM_HEIGHT_EXCEED_POLICIES; ++ehpIndex )
435       {
436         textView.SetHeightExceedPolicy( EXCEED_HEIGHT_POLICIES[ehpIndex] );
437         for( unsigned int taIndex = 0; taIndex < NUM_TEXT_ALIGNMENT; ++taIndex )
438         {
439           textView.SetTextAlignment( TEXT_ALIGNMENT[taIndex] );
440           for( unsigned int ljIndex = 0; ljIndex < NUM_LINE_JUSTIFICATION; ++ljIndex )
441           {
442             textView.SetLineJustification( LINE_JUSTIFICATION[ljIndex] );
443
444             try
445             {
446               textView.GetTextLayoutInfo( textLayoutInfo );
447
448               application.SendNotification();
449               application.Render();
450             }
451             catch( Dali::DaliException& e )
452             {
453               DALI_TEST_EQUALS( e.mCondition, "!\"TextView::CombineExceedPolicies() Invalid width and height exceed policies combination\"", TEST_LOCATION );
454             }
455             catch( ... )
456             {
457               tet_printf( "Tet case fails\n" );
458               tet_printf( "      MultilinePolicy : %d\n", MULTILINE_POLICIES[mlpIndex] );
459               tet_printf( "   Width ExceedPolicy : %d\n", EXCEED_WIDTH_POLICIES[ewpIndex] );
460               tet_printf( "  Height ExceedPolicy : %d\n", EXCEED_HEIGHT_POLICIES[ehpIndex] );
461               tet_printf( "        TextAlignment : %d\n", TEXT_ALIGNMENT[taIndex] );
462               tet_printf( "    LineJustification : %d\n", LINE_JUSTIFICATION[ljIndex] );
463               tet_result(TET_FAIL);
464             }
465
466             DALI_TEST_CHECK( LINE_JUSTIFICATION[ljIndex] == textView.GetLineJustification() );
467           }
468           DALI_TEST_CHECK( TEXT_ALIGNMENT[taIndex] == textView.GetTextAlignment() );
469         }
470         DALI_TEST_CHECK( EXCEED_HEIGHT_POLICIES[ehpIndex] == textView.GetHeightExceedPolicy() );
471       }
472       DALI_TEST_CHECK( EXCEED_WIDTH_POLICIES[ewpIndex] == textView.GetWidthExceedPolicy() );
473     }
474     DALI_TEST_CHECK( MULTILINE_POLICIES[mlpIndex] == textView.GetMultilinePolicy() );
475   }
476   END_TEST;
477 }
478 */
479
480 int UtcDaliTextViewTestLayoutOptions02(void)
481 {
482   tet_infoline("UtcDaliTextViewTestLayoutOptions02: ");
483   ToolkitTestApplication application;
484
485   // Check some configurations.
486
487   TextView textView = TextView::New();
488   textView.SetSnapshotModeEnabled( false ); // Disables offscreen rendering.
489
490   Stage::GetCurrent().Add( textView );
491
492   // SplitByWord and ShrinkToFit.
493   // Centered alignment.
494   // Centered justification.
495   // Don't create a text actor per character.
496
497   textView.SetMultilinePolicy( TextView::SplitByWord );
498   textView.SetWidthExceedPolicy( TextView::ShrinkToFit );
499   textView.SetHeightExceedPolicy( TextView::ShrinkToFit );
500   textView.SetTextAlignment( static_cast<Alignment::Type>( Alignment::HorizontalCenter | Alignment::VerticalTop ) );
501   textView.SetLineJustification( TextView::Center );
502   textView.SetSize( 136.56252f, 100.f );
503
504   textView.SetText( "Hel<font color='green'>lo wo</font>rld!" );
505
506   application.SendNotification();
507   application.Render();
508
509   std::vector<Size> sizes;
510   sizes.push_back( Size( 34.14063f, 11.380210f ) );              //
511   sizes.push_back( Size( 56.90105f, 11.380210f ) );              //
512   sizes.push_back( Size( 45.52084f, 11.380210f ) );              // By default characters have width and height values of 11.380210.
513                                                                  // The result should be a line with the text 'Hello world' as shown below.
514   std::vector<Vector3> positions;                                //  ____________
515   positions.push_back( Vector3( 0.000008f, 11.380209f, 0.f ) );  // |Hello world!|
516   positions.push_back( Vector3( 34.14063f, 11.380209f, 0.f ) );  //  ------------
517   positions.push_back( Vector3( 91.04168f, 11.380209f, 0.f ) );  //
518
519   DALI_TEST_CHECK( positions.size() == textView.GetChildCount() ); // Check text has two text-actors.
520
521   for( std::size_t index = 0, num = textView.GetChildCount(); index < num; ++index )
522   {
523     const Vector3& size = textView.GetChildAt(index).GetCurrentSize();
524     const Vector3& position = textView.GetChildAt(index).GetCurrentPosition();
525
526     DALI_TEST_EQUALS( size.width, sizes[index].width, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
527     DALI_TEST_EQUALS( size.height, sizes[index].height, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
528     DALI_TEST_EQUALS( position.width, positions[index].width, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
529     DALI_TEST_EQUALS( position.height, positions[index].height, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
530   }
531
532   textView.SetSize( 50.f, 50.f );
533   textView.SetTextAlignment( static_cast<Alignment::Type>( Alignment::HorizontalCenter | Alignment::VerticalCenter ) );
534   textView.SetSizePolicy( Toolkit::Control::Fixed, Toolkit::Control::Fixed );
535   textView.SetLineJustification( Toolkit::TextView::Left );
536
537   application.SendNotification();
538   application.Render();
539
540   sizes.clear();
541   sizes.push_back( Size( 24.999999f, 8.333333f ) );              //
542   sizes.push_back( Size( 24.999999f, 8.333333f ) );              //
543   sizes.push_back( Size( 16.666666f, 8.333333f ) );              // Longest word is 'world!' (6 characters x 11.380210) which doesn't fit in the 50x50 box.
544   sizes.push_back( Size( 33.333332f, 8.333333f ) );              // The scale factor is 0.732265339, so the character size is 8.333333.
545                                                                  // Text should be split in two lines, centered in the vertical dimension and fitted in the horizontal one.
546   positions.clear();                                             // As shown below, the text is two lines and centered in the vertical dimension and
547   positions.push_back( Vector3(  0.000008f, 25.223114f, 0.f ) ); // it should start in middle height (~25).
548   positions.push_back( Vector3( 24.999999f, 25.223114f, 0.f ) ); //   ______
549   positions.push_back( Vector3(  0.000006f, 33.556446f, 0.f ) ); //  |      |
550   positions.push_back( Vector3( 16.666666f, 33.556446f, 0.f ) ); //  |Hello |
551                                                                  //  |world!|
552                                                                  //  |______|
553                                                                  //
554
555   DALI_TEST_CHECK( positions.size() == textView.GetChildCount() ); // Check text has two text-actors.
556
557   for( std::size_t index = 0, num = textView.GetChildCount(); index < num; ++index )
558   {
559     const Vector3& size = textView.GetChildAt(index).GetCurrentSize();
560     const Vector3& position = textView.GetChildAt(index).GetCurrentPosition();
561
562     DALI_TEST_EQUALS( size.width, sizes[index].width, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
563     DALI_TEST_EQUALS( size.height, sizes[index].height, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
564     DALI_TEST_EQUALS( position.width, positions[index].width, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
565     DALI_TEST_EQUALS( position.height, positions[index].height, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
566   }
567
568   // TODO: Add more tests when TextView implementation is finished.
569   END_TEST;
570 }
571
572 int UtcDaliTextViewInsertRemoveText(void)
573 {
574   tet_infoline("UtcDaliTextViewInsertRemoveText: ");
575   ToolkitTestApplication application;
576
577   std::string text("Hello ");
578
579   MarkupProcessor::StyledTextArray styledText;
580   MarkupProcessor::GetStyledTextArray( text, styledText, true );
581
582   TextView view = TextView::New( "world!" );
583
584   view.InsertTextAt( 0, styledText );
585
586   DALI_TEST_EQUALS( view.GetText(), std::string("Hello world!"), TEST_LOCATION );
587
588   view.RemoveTextFrom( 4, 5 );
589
590   DALI_TEST_EQUALS( view.GetText(), std::string("Hellld!"), TEST_LOCATION );
591
592   view.InsertTextAt( 0, "Hello " );
593
594   DALI_TEST_EQUALS( view.GetText(), std::string("Hello Hellld!"), TEST_LOCATION );
595
596
597   view.InsertTextAt( 0, "Hello " );
598   view.InsertTextAt( 0, "Hello " );
599   view.InsertTextAt( 0, "Hello " );
600   view.InsertTextAt( 0, "Hello " );
601   view.RemoveTextFrom( 4, 2 );
602   view.RemoveTextFrom( 4, 2 );
603   view.RemoveTextFrom( 4, 2 );
604   view.RemoveTextFrom( 4, 2 );
605   view.RemoveTextFrom( 4, 2 );
606   view.SetText( "Hello world!" );
607
608   DALI_TEST_EQUALS( view.GetText(), std::string("Hello world!"), TEST_LOCATION );
609
610   view.ReplaceTextFromTo( 5, 1, "" );
611
612   DALI_TEST_EQUALS( view.GetText(), std::string("Helloworld!"), TEST_LOCATION );
613
614   view.ReplaceTextFromTo( 0, 11, styledText );
615
616   DALI_TEST_EQUALS( view.GetText(), std::string("Hello "), TEST_LOCATION );
617   END_TEST;
618 }
619
620 int UtcDaliTextViewSnapshotEnable(void)
621 {
622   tet_infoline("UtcDaliTextViewSnapshotEnable: ");
623   ToolkitTestApplication application;
624
625   // Avoids the frame buffer texture to throw an exception.
626   application.GetGlAbstraction().SetCheckFramebufferStatusResult( GL_FRAMEBUFFER_COMPLETE );
627
628   TextView view = TextView::New( "Hel<font color='green'>lo world!</font> This <font color='green'>is</font> a sna<font color='green'>psho</font>t test." );
629
630   Stage::GetCurrent().Add( view );
631
632   view.SetSnapshotModeEnabled( true );  // VCC. By default the snapshot mode should be enabled but it has been temporary disabled.
633                                         // This line should be removed when text-view is set to use the snapshot mode by default.
634
635   // Snapshot is enabled by default.
636   DALI_TEST_CHECK( view.IsSnapshotModeEnabled() );
637
638   application.SendNotification();
639   application.Render();
640
641   // TextView should have only two actors:
642   // the root (Actor) and the image (ImageActor).
643
644   DALI_TEST_EQUALS( view.GetChildCount(), 2u, TEST_LOCATION );
645
646   view.SetSnapshotModeEnabled( false );
647   DALI_TEST_CHECK( !view.IsSnapshotModeEnabled() );
648
649   application.SendNotification();
650   application.Render();
651
652   // TextView should have one text-actor per word.
653
654   DALI_TEST_EQUALS( view.GetChildCount(), 7u, TEST_LOCATION );
655   END_TEST;
656 }
657
658 int UtcDaliTextViewScroll(void)
659 {
660   tet_infoline("UtcDaliTextViewScroll: ");
661   ToolkitTestApplication application;
662
663   // Avoids the frame buffer texture to throw an exception.
664   application.GetGlAbstraction().SetCheckFramebufferStatusResult( GL_FRAMEBUFFER_COMPLETE );
665
666   TextView view = TextView::New( "Hello world! This is a scroll test." );
667   view.SetSize( 100.f, 100.f );
668   view.SetSnapshotModeEnabled( false );
669
670   Stage::GetCurrent().Add( view );
671
672   application.SendNotification();
673   application.Render();
674
675   DALI_TEST_CHECK( !view.IsScrollEnabled() ); // Scroll should be disabled by default.
676
677   view.SetScrollEnabled( true );
678   view.ScrolledSignal().Connect( &TestTextScrolled );
679
680   DALI_TEST_CHECK( view.IsScrollEnabled() );
681   DALI_TEST_CHECK( view.IsSnapshotModeEnabled() ); // Scroll should enable snapshot mode.
682
683   gTextScrolled = false;
684   gScrollDelta = Vector2::ZERO;
685   view.SetScrollPosition( Vector2( 400.f, 400.f ) );
686
687   application.SendNotification();
688   application.Render();
689
690   const Vector2& scrollPosition = view.GetScrollPosition();
691   DALI_TEST_EQUALS( scrollPosition, Vector2( 149.153656f, 0.f ), Math::MACHINE_EPSILON_1000, TEST_LOCATION );
692
693   DALI_TEST_CHECK( gTextScrolled );
694   DALI_TEST_EQUALS( gScrollDelta, Vector2( 149.153656f, 0.f ), Math::MACHINE_EPSILON_1000, TEST_LOCATION );
695
696   DALI_TEST_CHECK( view.IsScrollPositionTrimmed() );
697   END_TEST;
698 }
699
700 int UtcDaliTextViewSetProperty(void)
701 {
702   tet_infoline("UtcDaliTextViewSetAndGetText: ");
703   ToolkitTestApplication application;
704
705   TextView view = TextView::New( "Hello world!" );
706   Stage::GetCurrent().Add( view );
707
708   //Test multiline policy property
709   view.SetProperty(view.GetPropertyIndex(PROPERTY_MULTILINE_POLICY), "SplitByNewLineChar");
710   DALI_TEST_CHECK( Toolkit::TextView::SplitByNewLineChar == view.GetMultilinePolicy() );
711
712   view.SetProperty(view.GetPropertyIndex(PROPERTY_MULTILINE_POLICY), "SplitByWord");
713   DALI_TEST_CHECK( Toolkit::TextView::SplitByWord == view.GetMultilinePolicy() );
714
715   view.SetProperty(view.GetPropertyIndex(PROPERTY_MULTILINE_POLICY), "SplitByChar");
716   DALI_TEST_CHECK( Toolkit::TextView::SplitByChar == view.GetMultilinePolicy() );
717
718   //Test width exceed policy property
719   view.SetProperty(view.GetPropertyIndex(PROPERTY_WIDTH_EXCEED_POLICY), "Original");
720   view.SetProperty(view.GetPropertyIndex(PROPERTY_HEIGHT_EXCEED_POLICY), "Original");
721   DALI_TEST_CHECK( Toolkit::TextView::Original == view.GetWidthExceedPolicy() );
722   DALI_TEST_CHECK( Toolkit::TextView::Original == view.GetHeightExceedPolicy() );
723
724   view.SetProperty(view.GetPropertyIndex(PROPERTY_WIDTH_EXCEED_POLICY), "Fade");
725   view.SetProperty(view.GetPropertyIndex(PROPERTY_HEIGHT_EXCEED_POLICY), "Fade");
726   DALI_TEST_CHECK( Toolkit::TextView::Fade == view.GetWidthExceedPolicy() );
727   DALI_TEST_CHECK( Toolkit::TextView::Fade == view.GetHeightExceedPolicy() );
728
729   view.SetProperty(view.GetPropertyIndex(PROPERTY_WIDTH_EXCEED_POLICY), "ShrinkToFit");
730   view.SetProperty(view.GetPropertyIndex(PROPERTY_HEIGHT_EXCEED_POLICY), "ShrinkToFit");
731   DALI_TEST_CHECK( Toolkit::TextView::ShrinkToFit == view.GetWidthExceedPolicy() );
732   DALI_TEST_CHECK( Toolkit::TextView::ShrinkToFit == view.GetHeightExceedPolicy() );
733
734   //Test line justification property
735   view.SetProperty(view.GetPropertyIndex(PROPERTY_LINE_JUSTIFICATION), "Left");
736   DALI_TEST_CHECK( Toolkit::TextView::Left == view.GetLineJustification() );
737
738   view.SetProperty(view.GetPropertyIndex(PROPERTY_LINE_JUSTIFICATION), "Center");
739   DALI_TEST_CHECK( Toolkit::TextView::Center == view.GetLineJustification() );
740
741   view.SetProperty(view.GetPropertyIndex(PROPERTY_LINE_JUSTIFICATION), "Right");
742   DALI_TEST_CHECK( Toolkit::TextView::Right == view.GetLineJustification() );
743
744   view.SetProperty(view.GetPropertyIndex(PROPERTY_LINE_JUSTIFICATION), "Justified");
745   DALI_TEST_CHECK( Toolkit::TextView::Justified == view.GetLineJustification() );
746
747   //Test fade boundary property
748   unsigned int testValue = 23;
749   PixelSize leftFadeBoundary(testValue);
750   view.SetProperty(view.GetPropertyIndex(PROPERTY_FADE_BOUNDARY_LEFT), testValue);
751   DALI_TEST_CHECK( leftFadeBoundary == view.GetFadeBoundary().mLeft );
752
753   testValue = 26;
754   PixelSize rightFadeBoundary(testValue);
755   view.SetProperty(view.GetPropertyIndex(PROPERTY_FADE_BOUNDARY_RIGHT), testValue);
756   DALI_TEST_CHECK( rightFadeBoundary == view.GetFadeBoundary().mRight );
757
758   testValue = 2;
759   PixelSize topFadeBoundary(testValue);
760   view.SetProperty(view.GetPropertyIndex(PROPERTY_FADE_BOUNDARY_TOP), testValue);
761   DALI_TEST_CHECK( topFadeBoundary == view.GetFadeBoundary().mTop );
762
763   testValue = 11;
764   PixelSize bottomFadeBoundary(testValue);
765   view.SetProperty(view.GetPropertyIndex(PROPERTY_FADE_BOUNDARY_BOTTOM), testValue);
766   DALI_TEST_CHECK( bottomFadeBoundary == view.GetFadeBoundary().mBottom );
767
768   //Test Line height offset property
769   float testOffsetValue = 14.04f;
770   view.SetProperty(view.GetPropertyIndex(PROPERTY_LINE_HEIGHT_OFFSET), testOffsetValue);
771   DALI_TEST_CHECK( PointSize(testOffsetValue) == view.GetLineHeightOffset() );
772
773   //Test alignment property
774   view.SetProperty(view.GetPropertyIndex(PROPERTY_HORIZONTAL_ALIGNMENT), "HorizontalLeft");
775   view.SetProperty(view.GetPropertyIndex(PROPERTY_VERTICAL_ALIGNMENT), "VerticalTop");
776   DALI_TEST_CHECK( (Toolkit::Alignment::HorizontalLeft | Toolkit::Alignment::VerticalTop) == view.GetTextAlignment() );
777   END_TEST;
778 }
779
780 int UtcDaliTextViewSetSortModifier(void)
781 {
782   tet_infoline("UtcDaliTextViewSetAndGetText: ");
783   ToolkitTestApplication application;
784
785   TextView view = TextView::New( "Hello world!" );
786   Stage::GetCurrent().Add( view );
787
788   view.SetSortModifier( 10.f );
789   view.SetSnapshotModeEnabled( false );
790
791   application.SendNotification();
792   application.Render();
793
794   DALI_TEST_EQUALS( RenderableActor::DownCast(view.GetChildAt(0)).GetSortModifier(), 10.f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
795   END_TEST;
796 }
797
798 int UtcDaliTextViewUnderlineText(void)
799 {
800   tet_infoline("UtcDaliTextViewUnderlineText: ");
801   ToolkitTestApplication application;
802
803   TextView textView = TextView::New( "<u><font size='10'>gg<font size='14'>gg<font size='18'>gg<font size='22'>gg</font>gg</font>gg</font>gg</font></u>" );
804   textView.SetSnapshotModeEnabled( false );
805
806   textView.SetSize( 150.f, 100.f );
807
808   Stage::GetCurrent().Add( textView );
809
810   application.SendNotification();
811   application.Render();
812
813   std::vector<float> positions;
814   positions.push_back( 6.448784f );
815   positions.push_back( 9.862847f );
816   positions.push_back( 13.276909f );
817   positions.push_back( 16.690973f );
818   positions.push_back( 13.276909f );
819   positions.push_back( 9.862847f );
820   positions.push_back( 6.448784f );
821
822   for( std::size_t index = 0, num = textView.GetChildCount(); index < num; ++index )
823   {
824     TextStyle style = TextActor::DownCast( textView.GetChildAt(index) ).GetTextStyle();
825
826     DALI_TEST_EQUALS( 4.17274f, style.GetUnderlineThickness(), Math::MACHINE_EPSILON_1000, TEST_LOCATION );
827     DALI_TEST_EQUALS( positions[index], style.GetUnderlinePosition(), Math::MACHINE_EPSILON_1000, TEST_LOCATION );
828   }
829   END_TEST;
830 }