eb1647f582d67b37cdd71f075634e54b7ef0e948
[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 int UtcDaliTextViewTestLayoutOptions01(void)
387 {
388   tet_infoline("UtcDaliTextViewTestLayoutOptions01: ");
389
390   ToolkitTestApplication application;
391
392   const std::string text( "앞서 농식품부 주이석 검역검사본부\n"
393                           "동물방역부장을 단장으로 하는\n"
394                           "민관합동조사단은 지난달 30일부터\n"
395                           "12일간의 현지 조사활동을 마치고\n"
396                           "11일 새벽 귀국했습니다." );
397
398   const TextView::MultilinePolicy MULTILINE_POLICIES[] = { TextView::SplitByNewLineChar, TextView::SplitByWord, TextView::SplitByChar };
399   const TextView::ExceedPolicy EXCEED_WIDTH_POLICIES[] = { TextView::Original, TextView::Fade, TextView::Split, TextView::ShrinkToFit, TextView::EllipsizeEnd };
400   const TextView::ExceedPolicy EXCEED_HEIGHT_POLICIES[] = { TextView::Original, TextView::Fade, TextView::ShrinkToFit };
401   const Alignment::Type TEXT_ALIGNMENT[] = { static_cast<Alignment::Type>( Alignment::HorizontalLeft | Alignment::VerticalTop ),
402                                              static_cast<Alignment::Type>( Alignment::HorizontalLeft | Alignment::VerticalCenter ),
403                                              static_cast<Alignment::Type>( Alignment::HorizontalLeft | Alignment::VerticalBottom ),
404                                              static_cast<Alignment::Type>( Alignment::HorizontalCenter | Alignment::VerticalTop ),
405                                              static_cast<Alignment::Type>( Alignment::HorizontalCenter | Alignment::VerticalCenter ),
406                                              static_cast<Alignment::Type>( Alignment::HorizontalCenter | Alignment::VerticalBottom ),
407                                              static_cast<Alignment::Type>( Alignment::HorizontalRight | Alignment::VerticalTop ),
408                                              static_cast<Alignment::Type>( Alignment::HorizontalRight | Alignment::VerticalCenter ),
409                                              static_cast<Alignment::Type>( Alignment::HorizontalRight | Alignment::VerticalBottom ) };
410   const TextView::LineJustification LINE_JUSTIFICATION[] = { TextView::Left, TextView::Center, TextView::Right, TextView::Justified };
411
412   const unsigned int NUM_MULTILINE_POLICIES = sizeof( MULTILINE_POLICIES ) / sizeof( unsigned int );
413   const unsigned int NUM_WIDTH_EXCEED_POLICIES = sizeof( EXCEED_WIDTH_POLICIES ) / sizeof( unsigned int );
414   const unsigned int NUM_HEIGHT_EXCEED_POLICIES = sizeof( EXCEED_HEIGHT_POLICIES ) / sizeof( unsigned int );
415   const unsigned int NUM_TEXT_ALIGNMENT = sizeof( TEXT_ALIGNMENT ) / sizeof( unsigned int );
416   const unsigned int NUM_LINE_JUSTIFICATION = sizeof( LINE_JUSTIFICATION ) / sizeof( unsigned int );
417
418   TextView textView = TextView::New( text );
419   textView.SetSnapshotModeEnabled( false ); // Disables offscreen rendering.
420
421   Stage::GetCurrent().Add( textView );
422
423   TextView::TextLayoutInfo textLayoutInfo;
424
425   for( unsigned int mlpIndex = 0; mlpIndex < NUM_MULTILINE_POLICIES; ++mlpIndex )
426   {
427     textView.SetMultilinePolicy( MULTILINE_POLICIES[mlpIndex] );
428     for( unsigned int ewpIndex = 0; ewpIndex < NUM_WIDTH_EXCEED_POLICIES; ++ewpIndex )
429     {
430       textView.SetWidthExceedPolicy( EXCEED_WIDTH_POLICIES[ewpIndex] );
431       for( unsigned int ehpIndex = 0; ehpIndex < NUM_HEIGHT_EXCEED_POLICIES; ++ehpIndex )
432       {
433         textView.SetHeightExceedPolicy( EXCEED_HEIGHT_POLICIES[ehpIndex] );
434         for( unsigned int taIndex = 0; taIndex < NUM_TEXT_ALIGNMENT; ++taIndex )
435         {
436           textView.SetTextAlignment( TEXT_ALIGNMENT[taIndex] );
437           for( unsigned int ljIndex = 0; ljIndex < NUM_LINE_JUSTIFICATION; ++ljIndex )
438           {
439             textView.SetLineJustification( LINE_JUSTIFICATION[ljIndex] );
440
441             try
442             {
443               textView.GetTextLayoutInfo( textLayoutInfo );
444
445               application.SendNotification();
446               application.Render();
447             }
448             catch( Dali::DaliException& e )
449             {
450               DALI_TEST_EQUALS( e.mCondition, "!\"TextView::CombineExceedPolicies() Invalid width and height exceed policies combination\"", TEST_LOCATION );
451             }
452             catch( ... )
453             {
454               tet_printf( "Tet case fails\n" );
455               tet_printf( "      MultilinePolicy : %d\n", MULTILINE_POLICIES[mlpIndex] );
456               tet_printf( "   Width ExceedPolicy : %d\n", EXCEED_WIDTH_POLICIES[ewpIndex] );
457               tet_printf( "  Height ExceedPolicy : %d\n", EXCEED_HEIGHT_POLICIES[ehpIndex] );
458               tet_printf( "        TextAlignment : %d\n", TEXT_ALIGNMENT[taIndex] );
459               tet_printf( "    LineJustification : %d\n", LINE_JUSTIFICATION[ljIndex] );
460               tet_result(TET_FAIL);
461             }
462
463             DALI_TEST_CHECK( LINE_JUSTIFICATION[ljIndex] == textView.GetLineJustification() );
464           }
465           DALI_TEST_CHECK( TEXT_ALIGNMENT[taIndex] == textView.GetTextAlignment() );
466         }
467         DALI_TEST_CHECK( EXCEED_HEIGHT_POLICIES[ehpIndex] == textView.GetHeightExceedPolicy() );
468       }
469       DALI_TEST_CHECK( EXCEED_WIDTH_POLICIES[ewpIndex] == textView.GetWidthExceedPolicy() );
470     }
471     DALI_TEST_CHECK( MULTILINE_POLICIES[mlpIndex] == textView.GetMultilinePolicy() );
472   }
473   END_TEST;
474 }
475
476 int UtcDaliTextViewTestLayoutOptions02(void)
477 {
478   tet_infoline("UtcDaliTextViewTestLayoutOptions02: ");
479   ToolkitTestApplication application;
480
481   // Check some configurations.
482
483   TextView textView = TextView::New();
484   textView.SetSnapshotModeEnabled( false ); // Disables offscreen rendering.
485
486   Stage::GetCurrent().Add( textView );
487
488   // SplitByWord and ShrinkToFit.
489   // Centered alignment.
490   // Centered justification.
491   // Don't create a text actor per character.
492
493   textView.SetMultilinePolicy( TextView::SplitByWord );
494   textView.SetWidthExceedPolicy( TextView::ShrinkToFit );
495   textView.SetHeightExceedPolicy( TextView::ShrinkToFit );
496   textView.SetTextAlignment( static_cast<Alignment::Type>( Alignment::HorizontalCenter | Alignment::VerticalTop ) );
497   textView.SetLineJustification( TextView::Center );
498   textView.SetSize( 136.56252f, 100.f );
499
500   textView.SetText( "Hel<font color='green'>lo wo</font>rld!" );
501
502   application.SendNotification();
503   application.Render();
504
505   std::vector<Size> sizes;
506   sizes.push_back( Size( 34.14063f, 11.380210f ) );              //
507   sizes.push_back( Size( 56.90105f, 11.380210f ) );              //
508   sizes.push_back( Size( 45.52084f, 11.380210f ) );              // By default characters have width and height values of 11.380210.
509                                                                  // The result should be a line with the text 'Hello world' as shown below.
510   std::vector<Vector3> positions;                                //  ____________
511   positions.push_back( Vector3( 0.000008f, 11.380209f, 0.f ) );  // |Hello world!|
512   positions.push_back( Vector3( 34.14063f, 11.380209f, 0.f ) );  //  ------------
513   positions.push_back( Vector3( 91.04168f, 11.380209f, 0.f ) );  //
514
515   DALI_TEST_CHECK( positions.size() == textView.GetChildCount() ); // Check text has two text-actors.
516
517   for( std::size_t index = 0, num = textView.GetChildCount(); index < num; ++index )
518   {
519     const Vector3& size = textView.GetChildAt(index).GetCurrentSize();
520     const Vector3& position = textView.GetChildAt(index).GetCurrentPosition();
521
522     DALI_TEST_EQUALS( size.width, sizes[index].width, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
523     DALI_TEST_EQUALS( size.height, sizes[index].height, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
524     DALI_TEST_EQUALS( position.width, positions[index].width, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
525     DALI_TEST_EQUALS( position.height, positions[index].height, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
526   }
527
528   textView.SetSize( 50.f, 50.f );
529   textView.SetTextAlignment( static_cast<Alignment::Type>( Alignment::HorizontalCenter | Alignment::VerticalCenter ) );
530   textView.SetSizePolicy( Toolkit::Control::Fixed, Toolkit::Control::Fixed );
531   textView.SetLineJustification( Toolkit::TextView::Left );
532
533   application.SendNotification();
534   application.Render();
535
536   sizes.clear();
537   sizes.push_back( Size( 24.999999f, 8.333333f ) );              //
538   sizes.push_back( Size( 24.999999f, 8.333333f ) );              //
539   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.
540   sizes.push_back( Size( 33.333332f, 8.333333f ) );              // The scale factor is 0.732265339, so the character size is 8.333333.
541                                                                  // Text should be split in two lines, centered in the vertical dimension and fitted in the horizontal one.
542   positions.clear();                                             // As shown below, the text is two lines and centered in the vertical dimension and
543   positions.push_back( Vector3(  0.000008f, 25.223114f, 0.f ) ); // it should start in middle height (~25).
544   positions.push_back( Vector3( 24.999999f, 25.223114f, 0.f ) ); //   ______
545   positions.push_back( Vector3(  0.000006f, 33.556446f, 0.f ) ); //  |      |
546   positions.push_back( Vector3( 16.666666f, 33.556446f, 0.f ) ); //  |Hello |
547                                                                  //  |world!|
548                                                                  //  |______|
549                                                                  //
550
551   DALI_TEST_CHECK( positions.size() == textView.GetChildCount() ); // Check text has two text-actors.
552
553   for( std::size_t index = 0, num = textView.GetChildCount(); index < num; ++index )
554   {
555     const Vector3& size = textView.GetChildAt(index).GetCurrentSize();
556     const Vector3& position = textView.GetChildAt(index).GetCurrentPosition();
557
558     DALI_TEST_EQUALS( size.width, sizes[index].width, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
559     DALI_TEST_EQUALS( size.height, sizes[index].height, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
560     DALI_TEST_EQUALS( position.width, positions[index].width, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
561     DALI_TEST_EQUALS( position.height, positions[index].height, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
562   }
563
564   // TODO: Add more tests when TextView implementation is finished.
565   END_TEST;
566 }
567
568 int UtcDaliTextViewInsertRemoveText(void)
569 {
570   tet_infoline("UtcDaliTextViewInsertRemoveText: ");
571   ToolkitTestApplication application;
572
573   std::string text("Hello ");
574
575   MarkupProcessor::StyledTextArray styledText;
576   MarkupProcessor::GetStyledTextArray( text, styledText, true );
577
578   TextView view = TextView::New( "world!" );
579
580   view.InsertTextAt( 0, styledText );
581
582   DALI_TEST_EQUALS( view.GetText(), std::string("Hello world!"), TEST_LOCATION );
583
584   view.RemoveTextFrom( 4, 5 );
585
586   DALI_TEST_EQUALS( view.GetText(), std::string("Hellld!"), TEST_LOCATION );
587
588   view.InsertTextAt( 0, "Hello " );
589
590   DALI_TEST_EQUALS( view.GetText(), std::string("Hello Hellld!"), TEST_LOCATION );
591
592
593   view.InsertTextAt( 0, "Hello " );
594   view.InsertTextAt( 0, "Hello " );
595   view.InsertTextAt( 0, "Hello " );
596   view.InsertTextAt( 0, "Hello " );
597   view.RemoveTextFrom( 4, 2 );
598   view.RemoveTextFrom( 4, 2 );
599   view.RemoveTextFrom( 4, 2 );
600   view.RemoveTextFrom( 4, 2 );
601   view.RemoveTextFrom( 4, 2 );
602   view.SetText( "Hello world!" );
603
604   DALI_TEST_EQUALS( view.GetText(), std::string("Hello world!"), TEST_LOCATION );
605
606   view.ReplaceTextFromTo( 5, 1, "" );
607
608   DALI_TEST_EQUALS( view.GetText(), std::string("Helloworld!"), TEST_LOCATION );
609
610   view.ReplaceTextFromTo( 0, 11, styledText );
611
612   DALI_TEST_EQUALS( view.GetText(), std::string("Hello "), TEST_LOCATION );
613   END_TEST;
614 }
615
616 int UtcDaliTextViewSnapshotEnable(void)
617 {
618   tet_infoline("UtcDaliTextViewSnapshotEnable: ");
619   ToolkitTestApplication application;
620
621   // Avoids the frame buffer texture to throw an exception.
622   application.GetGlAbstraction().SetCheckFramebufferStatusResult( GL_FRAMEBUFFER_COMPLETE );
623
624   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." );
625
626   Stage::GetCurrent().Add( view );
627
628   view.SetSnapshotModeEnabled( true );  // VCC. By default the snapshot mode should be enabled but it has been temporary disabled.
629                                         // This line should be removed when text-view is set to use the snapshot mode by default.
630
631   // Snapshot is enabled by default.
632   DALI_TEST_CHECK( view.IsSnapshotModeEnabled() );
633
634   application.SendNotification();
635   application.Render();
636
637   // TextView should have only two actors:
638   // the root (Actor) and the image (ImageActor).
639
640   DALI_TEST_EQUALS( view.GetChildCount(), 2u, TEST_LOCATION );
641
642   view.SetSnapshotModeEnabled( false );
643   DALI_TEST_CHECK( !view.IsSnapshotModeEnabled() );
644
645   application.SendNotification();
646   application.Render();
647
648   // TextView should have one text-actor per word.
649
650   DALI_TEST_EQUALS( view.GetChildCount(), 7u, TEST_LOCATION );
651   END_TEST;
652 }
653
654 int UtcDaliTextViewScroll(void)
655 {
656   tet_infoline("UtcDaliTextViewScroll: ");
657   ToolkitTestApplication application;
658
659   // Avoids the frame buffer texture to throw an exception.
660   application.GetGlAbstraction().SetCheckFramebufferStatusResult( GL_FRAMEBUFFER_COMPLETE );
661
662   TextView view = TextView::New( "Hello world! This is a scroll test." );
663   view.SetSize( 100.f, 100.f );
664   view.SetSnapshotModeEnabled( false );
665
666   Stage::GetCurrent().Add( view );
667
668   application.SendNotification();
669   application.Render();
670
671   DALI_TEST_CHECK( !view.IsScrollEnabled() ); // Scroll should be disabled by default.
672
673   view.SetScrollEnabled( true );
674   view.ScrolledSignal().Connect( &TestTextScrolled );
675
676   DALI_TEST_CHECK( view.IsScrollEnabled() );
677   DALI_TEST_CHECK( view.IsSnapshotModeEnabled() ); // Scroll should enable snapshot mode.
678
679   gTextScrolled = false;
680   gScrollDelta = Vector2::ZERO;
681   view.SetScrollPosition( Vector2( 400.f, 400.f ) );
682
683   application.SendNotification();
684   application.Render();
685
686   const Vector2& scrollPosition = view.GetScrollPosition();
687   DALI_TEST_EQUALS( scrollPosition, Vector2( 149.153656f, 0.f ), Math::MACHINE_EPSILON_1000, TEST_LOCATION );
688
689   DALI_TEST_CHECK( gTextScrolled );
690   DALI_TEST_EQUALS( gScrollDelta, Vector2( 149.153656f, 0.f ), Math::MACHINE_EPSILON_1000, TEST_LOCATION );
691
692   DALI_TEST_CHECK( view.IsScrollPositionTrimmed() );
693   END_TEST;
694 }
695
696 int UtcDaliTextViewSetProperty(void)
697 {
698   tet_infoline("UtcDaliTextViewSetAndGetText: ");
699   ToolkitTestApplication application;
700
701   TextView view = TextView::New( "Hello world!" );
702   Stage::GetCurrent().Add( view );
703
704   //Test multiline policy property
705   view.SetProperty(view.GetPropertyIndex(PROPERTY_MULTILINE_POLICY), "SplitByNewLineChar");
706   DALI_TEST_CHECK( Toolkit::TextView::SplitByNewLineChar == view.GetMultilinePolicy() );
707
708   view.SetProperty(view.GetPropertyIndex(PROPERTY_MULTILINE_POLICY), "SplitByWord");
709   DALI_TEST_CHECK( Toolkit::TextView::SplitByWord == view.GetMultilinePolicy() );
710
711   view.SetProperty(view.GetPropertyIndex(PROPERTY_MULTILINE_POLICY), "SplitByChar");
712   DALI_TEST_CHECK( Toolkit::TextView::SplitByChar == view.GetMultilinePolicy() );
713
714   //Test width exceed policy property
715   view.SetProperty(view.GetPropertyIndex(PROPERTY_WIDTH_EXCEED_POLICY), "Original");
716   view.SetProperty(view.GetPropertyIndex(PROPERTY_HEIGHT_EXCEED_POLICY), "Original");
717   DALI_TEST_CHECK( Toolkit::TextView::Original == view.GetWidthExceedPolicy() );
718   DALI_TEST_CHECK( Toolkit::TextView::Original == view.GetHeightExceedPolicy() );
719
720   view.SetProperty(view.GetPropertyIndex(PROPERTY_WIDTH_EXCEED_POLICY), "Fade");
721   view.SetProperty(view.GetPropertyIndex(PROPERTY_HEIGHT_EXCEED_POLICY), "Fade");
722   DALI_TEST_CHECK( Toolkit::TextView::Fade == view.GetWidthExceedPolicy() );
723   DALI_TEST_CHECK( Toolkit::TextView::Fade == view.GetHeightExceedPolicy() );
724
725   view.SetProperty(view.GetPropertyIndex(PROPERTY_WIDTH_EXCEED_POLICY), "ShrinkToFit");
726   view.SetProperty(view.GetPropertyIndex(PROPERTY_HEIGHT_EXCEED_POLICY), "ShrinkToFit");
727   DALI_TEST_CHECK( Toolkit::TextView::ShrinkToFit == view.GetWidthExceedPolicy() );
728   DALI_TEST_CHECK( Toolkit::TextView::ShrinkToFit == view.GetHeightExceedPolicy() );
729
730   //Test line justification property
731   view.SetProperty(view.GetPropertyIndex(PROPERTY_LINE_JUSTIFICATION), "Left");
732   DALI_TEST_CHECK( Toolkit::TextView::Left == view.GetLineJustification() );
733
734   view.SetProperty(view.GetPropertyIndex(PROPERTY_LINE_JUSTIFICATION), "Center");
735   DALI_TEST_CHECK( Toolkit::TextView::Center == view.GetLineJustification() );
736
737   view.SetProperty(view.GetPropertyIndex(PROPERTY_LINE_JUSTIFICATION), "Right");
738   DALI_TEST_CHECK( Toolkit::TextView::Right == view.GetLineJustification() );
739
740   view.SetProperty(view.GetPropertyIndex(PROPERTY_LINE_JUSTIFICATION), "Justified");
741   DALI_TEST_CHECK( Toolkit::TextView::Justified == view.GetLineJustification() );
742
743   //Test fade boundary property
744   unsigned int testValue = 23;
745   PixelSize leftFadeBoundary(testValue);
746   view.SetProperty(view.GetPropertyIndex(PROPERTY_FADE_BOUNDARY_LEFT), testValue);
747   DALI_TEST_CHECK( leftFadeBoundary == view.GetFadeBoundary().mLeft );
748
749   testValue = 26;
750   PixelSize rightFadeBoundary(testValue);
751   view.SetProperty(view.GetPropertyIndex(PROPERTY_FADE_BOUNDARY_RIGHT), testValue);
752   DALI_TEST_CHECK( rightFadeBoundary == view.GetFadeBoundary().mRight );
753
754   testValue = 2;
755   PixelSize topFadeBoundary(testValue);
756   view.SetProperty(view.GetPropertyIndex(PROPERTY_FADE_BOUNDARY_TOP), testValue);
757   DALI_TEST_CHECK( topFadeBoundary == view.GetFadeBoundary().mTop );
758
759   testValue = 11;
760   PixelSize bottomFadeBoundary(testValue);
761   view.SetProperty(view.GetPropertyIndex(PROPERTY_FADE_BOUNDARY_BOTTOM), testValue);
762   DALI_TEST_CHECK( bottomFadeBoundary == view.GetFadeBoundary().mBottom );
763
764   //Test Line height offset property
765   float testOffsetValue = 14.04f;
766   view.SetProperty(view.GetPropertyIndex(PROPERTY_LINE_HEIGHT_OFFSET), testOffsetValue);
767   DALI_TEST_CHECK( PointSize(testOffsetValue) == view.GetLineHeightOffset() );
768
769   //Test alignment property
770   view.SetProperty(view.GetPropertyIndex(PROPERTY_HORIZONTAL_ALIGNMENT), "HorizontalLeft");
771   view.SetProperty(view.GetPropertyIndex(PROPERTY_VERTICAL_ALIGNMENT), "VerticalTop");
772   DALI_TEST_CHECK( (Toolkit::Alignment::HorizontalLeft | Toolkit::Alignment::VerticalTop) == view.GetTextAlignment() );
773   END_TEST;
774 }
775
776 int UtcDaliTextViewSetSortModifier(void)
777 {
778   tet_infoline("UtcDaliTextViewSetAndGetText: ");
779   ToolkitTestApplication application;
780
781   TextView view = TextView::New( "Hello world!" );
782   Stage::GetCurrent().Add( view );
783
784   view.SetSortModifier( 10.f );
785   view.SetSnapshotModeEnabled( false );
786
787   application.SendNotification();
788   application.Render();
789
790   DALI_TEST_EQUALS( RenderableActor::DownCast(view.GetChildAt(0)).GetSortModifier(), 10.f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
791   END_TEST;
792 }
793
794 int UtcDaliTextViewUnderlineText(void)
795 {
796   tet_infoline("UtcDaliTextViewUnderlineText: ");
797   ToolkitTestApplication application;
798
799   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>" );
800   textView.SetSnapshotModeEnabled( false );
801
802   textView.SetSize( 150.f, 100.f );
803
804   Stage::GetCurrent().Add( textView );
805
806   application.SendNotification();
807   application.Render();
808
809   std::vector<float> positions;
810   positions.push_back( 6.448784f );
811   positions.push_back( 9.862847f );
812   positions.push_back( 13.276909f );
813   positions.push_back( 16.690973f );
814   positions.push_back( 13.276909f );
815   positions.push_back( 9.862847f );
816   positions.push_back( 6.448784f );
817
818   for( std::size_t index = 0, num = textView.GetChildCount(); index < num; ++index )
819   {
820     TextStyle style = TextActor::DownCast( textView.GetChildAt(index) ).GetTextStyle();
821
822     DALI_TEST_EQUALS( 4.17274f, style.GetUnderlineThickness(), Math::MACHINE_EPSILON_1000, TEST_LOCATION );
823     DALI_TEST_EQUALS( positions[index], style.GetUnderlinePosition(), Math::MACHINE_EPSILON_1000, TEST_LOCATION );
824   }
825   END_TEST;
826 }