Merge branch 'tizen' into new_text
[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 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 #include <stdlib.h>
20
21 // Need to override adaptor classes for toolkit test harness, so include
22 // test harness headers before dali headers.
23 #include <dali-toolkit-test-suite-utils.h>
24
25 #include <dali.h>
26 #include <dali-toolkit/dali-toolkit.h>
27
28 using namespace Dali;
29 using namespace Toolkit;
30
31 void utc_dali_toolkit_text_view_startup(void)
32 {
33   test_return_value = TET_UNDEF;
34 }
35
36 void utc_dali_toolkit_text_view_cleanup(void)
37 {
38   test_return_value = TET_PASS;
39 }
40
41
42 namespace
43 {
44
45 const char* const PROPERTY_TEXT = "text";
46 const char* const PROPERTY_MULTILINE_POLICY = "multiline-policy";
47 const char* const PROPERTY_WIDTH_EXCEED_POLICY = "width-exceed-policy";
48 const char* const PROPERTY_HEIGHT_EXCEED_POLICY = "height-exceed-policy";
49 const char* const PROPERTY_LINE_JUSTIFICATION = "line-justification";
50 const char* const PROPERTY_FADE_BOUNDARY = "fade-boundary";
51 const char* const PROPERTY_LINE_HEIGHT_OFFSET = "line-height-offset";
52 const char* const PROPERTY_HORIZONTAL_ALIGNMENT = "horizontal-alignment";
53 const char* const PROPERTY_VERTICAL_ALIGNMENT = "vertical-alignment";
54
55 bool TestEqual( float x, float y )
56 {
57   return !( fabsf( x - y ) > Math::MACHINE_EPSILON_1000 );
58 }
59
60 static bool gObjectCreatedCallBackCalled;
61 static unsigned int gNumberObjectCreated;
62
63 static void TestCallback(BaseHandle handle)
64 {
65   gObjectCreatedCallBackCalled = true;
66   ++gNumberObjectCreated;
67 }
68
69 static bool gTextScrolled;
70 static Vector2 gScrollDelta;
71 static void TestTextScrolled( TextView textView, Vector2 scrollDelta )
72 {
73   gTextScrolled = true;
74   gScrollDelta = scrollDelta;
75 }
76
77 } // namespace
78
79
80 int UtcDaliTextViewNew(void)
81 {
82   tet_infoline("UtcDaliTextViewNew: ");
83   ToolkitTestApplication application;
84
85   // Test default constructor.
86   TextView view;
87
88   DALI_TEST_CHECK( !view );
89
90   // Test default initialization.
91   view = TextView::New();
92
93   DALI_TEST_CHECK( view );
94
95   // Test copy constructor and asignment operator.
96   TextView viewCopy1;
97
98   viewCopy1 = view;
99
100   DALI_TEST_CHECK( viewCopy1 );
101
102   TextView viewCopy2( view );
103
104   DALI_TEST_CHECK( viewCopy2 );
105
106   // Test down cast.
107   Actor actorView;
108
109   actorView = view;
110
111   TextView downCastView = TextView::DownCast( actorView );
112
113   DALI_TEST_CHECK( downCastView );
114
115   // Test constructor with a given text.
116
117   const std::string text( "Hello world!" );
118
119   const float DESCENDER = 8.0f;
120
121   TextView view1 = TextView::New( text );
122
123   DALI_TEST_EQUALS( view1.GetText(), text, TEST_LOCATION );
124
125   MarkupProcessor::StyledTextArray styledText;
126   MarkupProcessor::GetStyledTextArray( text, styledText, true );
127
128   TextView view2 = TextView::New( styledText );
129
130   DALI_TEST_EQUALS( view2.GetText(), text, TEST_LOCATION );
131
132   // Check the default Toolkit::TextView::CharacterLayoutInfo::CharacterLayoutInfo() to increase coverage.
133   TextView::CharacterLayoutInfo characterLayoutInfo;
134
135   DALI_TEST_EQUALS( characterLayoutInfo.mSize, Vector2::ZERO, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
136   DALI_TEST_EQUALS( characterLayoutInfo.mPosition, Vector3::ZERO, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
137   DALI_TEST_EQUALS( characterLayoutInfo.mIsNewLineChar, false, TEST_LOCATION );
138   DALI_TEST_EQUALS( characterLayoutInfo.mIsRightToLeftCharacter, false, TEST_LOCATION );
139   DALI_TEST_EQUALS( characterLayoutInfo.mIsVisible, true, TEST_LOCATION );
140
141   TextView::CharacterLayoutInfo characterLayoutInfo2( Size( 2.f, 2.f ),
142                                                       Vector3( 3.f, 4.f, 5.f ),
143                                                       true,
144                                                       true,
145                                                       false,
146                                                       DESCENDER );
147
148   characterLayoutInfo = characterLayoutInfo2;
149
150   DALI_TEST_EQUALS( characterLayoutInfo.mSize, Size( 2.f, 2.f ), Math::MACHINE_EPSILON_1000, TEST_LOCATION );
151   DALI_TEST_EQUALS( characterLayoutInfo.mPosition, Vector3( 3.f, 4.f, 5.f ), Math::MACHINE_EPSILON_1000, TEST_LOCATION );
152   DALI_TEST_EQUALS( characterLayoutInfo.mIsNewLineChar, true, TEST_LOCATION );
153   DALI_TEST_EQUALS( characterLayoutInfo.mIsRightToLeftCharacter, true, TEST_LOCATION );
154   DALI_TEST_EQUALS( characterLayoutInfo.mIsVisible, false, TEST_LOCATION );
155   DALI_TEST_EQUALS( characterLayoutInfo.mDescender, DESCENDER , TEST_LOCATION );
156
157
158   TextView::CharacterLayoutInfo characterLayoutInfo3( characterLayoutInfo );
159
160   DALI_TEST_EQUALS( characterLayoutInfo3.mSize, Size( 2.f, 2.f ), Math::MACHINE_EPSILON_1000, TEST_LOCATION );
161   DALI_TEST_EQUALS( characterLayoutInfo3.mPosition, Vector3( 3.f, 4.f, 5.f ), Math::MACHINE_EPSILON_1000, TEST_LOCATION );
162   DALI_TEST_EQUALS( characterLayoutInfo3.mIsNewLineChar, true, TEST_LOCATION );
163   DALI_TEST_EQUALS( characterLayoutInfo3.mIsRightToLeftCharacter, true, TEST_LOCATION );
164   DALI_TEST_EQUALS( characterLayoutInfo3.mIsVisible, false, TEST_LOCATION );
165
166   // Check the default Toolkit::TextView::TextLayoutInfo::TextLayoutInfo() to increase coverage.
167
168   TextView::TextLayoutInfo textLayoutInfo;
169   DALI_TEST_EQUALS( textLayoutInfo.mCharacterLayoutInfoTable.size(), 0u, TEST_LOCATION );
170   DALI_TEST_EQUALS( textLayoutInfo.mCharacterLogicalToVisualMap.size(), 0u, TEST_LOCATION );
171   DALI_TEST_EQUALS( textLayoutInfo.mCharacterVisualToLogicalMap.size(), 0u, TEST_LOCATION );
172   DALI_TEST_EQUALS( textLayoutInfo.mTextSize, Size::ZERO, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
173   DALI_TEST_EQUALS( textLayoutInfo.mScrollOffset, Vector2::ZERO, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
174
175   textLayoutInfo.mCharacterLayoutInfoTable.push_back( characterLayoutInfo );
176   textLayoutInfo.mCharacterLogicalToVisualMap.push_back( 1 );
177   textLayoutInfo.mCharacterVisualToLogicalMap.push_back( 1 );
178   textLayoutInfo.mTextSize = Size( 10.f, 10.f );
179   textLayoutInfo.mScrollOffset = Vector2( 5.f, 5.f );
180
181   TextView::TextLayoutInfo textLayoutInfo2( textLayoutInfo );
182
183   DALI_TEST_EQUALS( textLayoutInfo2.mCharacterLayoutInfoTable.size(), 1u, TEST_LOCATION );
184   DALI_TEST_EQUALS( textLayoutInfo2.mCharacterLogicalToVisualMap.size(), 1u, TEST_LOCATION );
185   DALI_TEST_EQUALS( textLayoutInfo2.mCharacterVisualToLogicalMap.size(), 1u, TEST_LOCATION );
186   DALI_TEST_EQUALS( textLayoutInfo2.mTextSize, Size( 10.f, 10.f ), Math::MACHINE_EPSILON_1000, TEST_LOCATION );
187   DALI_TEST_EQUALS( textLayoutInfo2.mScrollOffset, Vector2( 5.f, 5.f ), Math::MACHINE_EPSILON_1000, TEST_LOCATION );
188
189   TextView::TextLayoutInfo textLayoutInfo3;
190
191   textLayoutInfo3 = textLayoutInfo2;
192
193   DALI_TEST_EQUALS( textLayoutInfo3.mCharacterLayoutInfoTable.size(), 1u, TEST_LOCATION );
194   DALI_TEST_EQUALS( textLayoutInfo3.mCharacterLogicalToVisualMap.size(), 1u, TEST_LOCATION );
195   DALI_TEST_EQUALS( textLayoutInfo3.mCharacterVisualToLogicalMap.size(), 1u, TEST_LOCATION );
196   DALI_TEST_EQUALS( textLayoutInfo3.mTextSize, Size( 10.f, 10.f ), Math::MACHINE_EPSILON_1000, TEST_LOCATION );
197   DALI_TEST_EQUALS( textLayoutInfo3.mScrollOffset, Vector2( 5.f, 5.f ), Math::MACHINE_EPSILON_1000, TEST_LOCATION );
198
199   //Additional check to ensure object is created by checking if it's registered
200   ObjectRegistry registry = Stage::GetCurrent().GetObjectRegistry();
201   DALI_TEST_CHECK( registry );
202
203   gObjectCreatedCallBackCalled = false;
204   registry.ObjectCreatedSignal().Connect(&TestCallback);
205   {
206     TextView view = TextView::New();
207   }
208   DALI_TEST_CHECK( gObjectCreatedCallBackCalled );
209   END_TEST;
210 }
211
212 int UtcDaliTextViewSetAndGetText(void)
213 {
214   tet_infoline("UtcDaliTextViewSetAndGetText: ");
215   ToolkitTestApplication application;
216
217   TextView view = TextView::New();
218   view.SetSnapshotModeEnabled( false ); // Disables offscreen rendering.
219
220   std::string str( "Text with differing aCeNdEr and dEcEnDeR" );
221
222   view.SetText( str );
223   DALI_TEST_EQUALS( view.GetText(), str, TEST_LOCATION );
224
225   MarkupProcessor::StyledTextArray styledText;
226   MarkupProcessor::GetStyledTextArray( str, styledText, true );
227
228   view.SetText( styledText );
229   DALI_TEST_EQUALS( view.GetText(), str, TEST_LOCATION );
230
231   // Test the number of text actor created.
232
233   ObjectRegistry registry = Stage::GetCurrent().GetObjectRegistry();
234   DALI_TEST_CHECK( registry );
235
236   gNumberObjectCreated = 0u;
237   registry.ObjectCreatedSignal().Connect(&TestCallback);
238
239   // Following string should create three text-actors ([Hel], [lo wo] and [rld]).
240   std::string text( "Hel<font size='10'>lo wo</font>rld!\n"
241                     "\n" );
242
243   view.SetMarkupProcessingEnabled( true ); // Enables markup processing.
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   textView.SetMarkupProcessingEnabled( true ); // Enables markup processing.
490
491   Stage::GetCurrent().Add( textView );
492
493   // SplitByWord and ShrinkToFit.
494   // Centered alignment.
495   // Centered justification.
496   // Don't create a text actor per character.
497
498   textView.SetMultilinePolicy( TextView::SplitByWord );
499   textView.SetWidthExceedPolicy( TextView::ShrinkToFit );
500   textView.SetHeightExceedPolicy( TextView::ShrinkToFit );
501   textView.SetTextAlignment( static_cast<Alignment::Type>( Alignment::HorizontalCenter | Alignment::VerticalTop ) );
502   textView.SetLineJustification( TextView::Center );
503   textView.SetSize( 136.56252f, 100.f );
504
505   textView.SetText( "Hel<font color='green'>lo wo</font>rld!" );
506
507   application.SendNotification();
508   application.Render();
509
510   std::vector<Size> sizes;
511   sizes.push_back( Size( 34.14063f, 11.380210f ) );              //
512   sizes.push_back( Size( 56.90105f, 11.380210f ) );              //
513   sizes.push_back( Size( 45.52084f, 11.380210f ) );              // By default characters have width and height values of 11.380210.
514                                                                  // The result should be a line with the text 'Hello world' as shown below.
515   std::vector<Vector3> positions;                                //  ____________
516   positions.push_back( Vector3( 0.000008f, 11.380209f, 0.f ) );  // |Hello world!|
517   positions.push_back( Vector3( 34.14063f, 11.380209f, 0.f ) );  //  ------------
518   positions.push_back( Vector3( 91.04168f, 11.380209f, 0.f ) );  //
519
520   DALI_TEST_CHECK( positions.size() == textView.GetChildCount() ); // Check text has two text-actors.
521
522   for( std::size_t index = 0, num = textView.GetChildCount(); index < num; ++index )
523   {
524     const Vector3& size = textView.GetChildAt(index).GetCurrentSize();
525     const Vector3& position = textView.GetChildAt(index).GetCurrentPosition();
526
527     DALI_TEST_EQUALS( size.width, sizes[index].width, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
528     DALI_TEST_EQUALS( size.height, sizes[index].height, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
529     DALI_TEST_EQUALS( position.width, positions[index].width, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
530     DALI_TEST_EQUALS( position.height, positions[index].height, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
531   }
532
533   textView.SetSize( 50.f, 50.f );
534   textView.SetTextAlignment( static_cast<Alignment::Type>( Alignment::HorizontalCenter | Alignment::VerticalCenter ) );
535   textView.SetSizePolicy( Toolkit::Control::Fixed, Toolkit::Control::Fixed );
536   textView.SetLineJustification( Toolkit::TextView::Left );
537
538   application.SendNotification();
539   application.Render();
540
541   sizes.clear();
542   sizes.push_back( Size( 24.999999f, 8.333333f ) );              //
543   sizes.push_back( Size( 24.999999f, 8.333333f ) );              //
544   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.
545   sizes.push_back( Size( 33.333332f, 8.333333f ) );              // The scale factor is 0.732265339, so the character size is 8.333333.
546                                                                  // Text should be split in two lines, centered in the vertical dimension and fitted in the horizontal one.
547   positions.clear();                                             // As shown below, the text is two lines and centered in the vertical dimension and
548   positions.push_back( Vector3(  0.000008f, 25.223114f, 0.f ) ); // it should start in middle height (~25).
549   positions.push_back( Vector3( 24.999999f, 25.223114f, 0.f ) ); //   ______
550   positions.push_back( Vector3(  0.000006f, 33.556446f, 0.f ) ); //  |      |
551   positions.push_back( Vector3( 16.666666f, 33.556446f, 0.f ) ); //  |Hello |
552                                                                  //  |world!|
553                                                                  //  |______|
554                                                                  //
555
556   DALI_TEST_CHECK( positions.size() == textView.GetChildCount() ); // Check text has two text-actors.
557
558   for( std::size_t index = 0, num = textView.GetChildCount(); index < num; ++index )
559   {
560     const Vector3& size = textView.GetChildAt(index).GetCurrentSize();
561     const Vector3& position = textView.GetChildAt(index).GetCurrentPosition();
562
563     DALI_TEST_EQUALS( size.width, sizes[index].width, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
564     DALI_TEST_EQUALS( size.height, sizes[index].height, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
565     DALI_TEST_EQUALS( position.width, positions[index].width, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
566     DALI_TEST_EQUALS( position.height, positions[index].height, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
567   }
568
569   // TODO: Add more tests when TextView implementation is finished.
570   END_TEST;
571 }
572
573 int UtcDaliTextViewInsertRemoveText(void)
574 {
575   tet_infoline("UtcDaliTextViewInsertRemoveText: ");
576   ToolkitTestApplication application;
577
578   std::string text("Hello ");
579
580   MarkupProcessor::StyledTextArray styledText;
581   MarkupProcessor::GetStyledTextArray( text, styledText, true );
582
583   TextView view = TextView::New( "world!" );
584
585   view.InsertTextAt( 0, styledText );
586
587   DALI_TEST_EQUALS( view.GetText(), std::string("Hello world!"), TEST_LOCATION );
588
589   view.RemoveTextFrom( 4, 5 );
590
591   DALI_TEST_EQUALS( view.GetText(), std::string("Hellld!"), TEST_LOCATION );
592
593   view.InsertTextAt( 0, "Hello " );
594
595   DALI_TEST_EQUALS( view.GetText(), std::string("Hello Hellld!"), TEST_LOCATION );
596
597
598   view.InsertTextAt( 0, "Hello " );
599   view.InsertTextAt( 0, "Hello " );
600   view.InsertTextAt( 0, "Hello " );
601   view.InsertTextAt( 0, "Hello " );
602   view.RemoveTextFrom( 4, 2 );
603   view.RemoveTextFrom( 4, 2 );
604   view.RemoveTextFrom( 4, 2 );
605   view.RemoveTextFrom( 4, 2 );
606   view.RemoveTextFrom( 4, 2 );
607   view.SetText( "Hello world!" );
608
609   DALI_TEST_EQUALS( view.GetText(), std::string("Hello world!"), TEST_LOCATION );
610
611   view.ReplaceTextFromTo( 5, 1, "" );
612
613   DALI_TEST_EQUALS( view.GetText(), std::string("Helloworld!"), TEST_LOCATION );
614
615   view.ReplaceTextFromTo( 0, 11, styledText );
616
617   DALI_TEST_EQUALS( view.GetText(), std::string("Hello "), TEST_LOCATION );
618   END_TEST;
619 }
620
621 int UtcDaliTextViewSnapshotEnable(void)
622 {
623   tet_infoline("UtcDaliTextViewSnapshotEnable: ");
624   ToolkitTestApplication application;
625
626   // Avoids the frame buffer texture to throw an exception.
627   application.GetGlAbstraction().SetCheckFramebufferStatusResult( GL_FRAMEBUFFER_COMPLETE );
628
629   TextView view = TextView::New();
630   view.SetMarkupProcessingEnabled( true ); // Enables markup processing.
631   view.SetText( "Hel<font color='green'>lo world!</font> This <font color='green'>is</font> a sna<font color='green'>psho</font>t test." );
632
633   Stage::GetCurrent().Add( view );
634
635   view.SetSnapshotModeEnabled( true );  // VCC. By default the snapshot mode should be enabled but it has been temporary disabled.
636                                         // This line should be removed when text-view is set to use the snapshot mode by default.
637
638   // Snapshot is enabled by default.
639   DALI_TEST_CHECK( view.IsSnapshotModeEnabled() );
640
641   application.SendNotification();
642   application.Render();
643
644   // TextView should have only two actors:
645   // the root (Actor) and the image (ImageActor).
646
647   DALI_TEST_EQUALS( view.GetChildCount(), 2u, TEST_LOCATION );
648
649   view.SetSnapshotModeEnabled( false );
650   DALI_TEST_CHECK( !view.IsSnapshotModeEnabled() );
651
652   application.SendNotification();
653   application.Render();
654
655   // TextView should have one text-actor per word.
656
657   DALI_TEST_EQUALS( view.GetChildCount(), 7u, TEST_LOCATION );
658   END_TEST;
659 }
660
661 int UtcDaliTextViewScroll(void)
662 {
663   tet_infoline("UtcDaliTextViewScroll: ");
664   ToolkitTestApplication application;
665
666   // Avoids the frame buffer texture to throw an exception.
667   application.GetGlAbstraction().SetCheckFramebufferStatusResult( GL_FRAMEBUFFER_COMPLETE );
668
669   TextView view = TextView::New( "Hello world! This is a scroll test." );
670   view.SetSize( 100.f, 100.f );
671   view.SetSnapshotModeEnabled( false );
672
673   Stage::GetCurrent().Add( view );
674
675   application.SendNotification();
676   application.Render();
677
678   DALI_TEST_CHECK( !view.IsScrollEnabled() ); // Scroll should be disabled by default.
679
680   view.SetScrollEnabled( true );
681   view.ScrolledSignal().Connect( &TestTextScrolled );
682
683   DALI_TEST_CHECK( view.IsScrollEnabled() );
684   DALI_TEST_CHECK( view.IsSnapshotModeEnabled() ); // Scroll should enable snapshot mode.
685
686   gTextScrolled = false;
687   gScrollDelta = Vector2::ZERO;
688   view.SetScrollPosition( Vector2( 400.f, 400.f ) );
689
690   application.SendNotification();
691   application.Render();
692
693   const Vector2& scrollPosition = view.GetScrollPosition();
694   DALI_TEST_EQUALS( scrollPosition, Vector2( 149.153656f, 0.f ), Math::MACHINE_EPSILON_1000, TEST_LOCATION );
695
696   DALI_TEST_CHECK( gTextScrolled );
697   DALI_TEST_EQUALS( gScrollDelta, Vector2( 149.153656f, 0.f ), Math::MACHINE_EPSILON_1000, TEST_LOCATION );
698
699   DALI_TEST_CHECK( view.IsScrollPositionTrimmed() );
700   END_TEST;
701 }
702
703 int UtcDaliTextViewSetProperty(void)
704 {
705   tet_infoline("UtcDaliTextViewSetAndGetText: ");
706   ToolkitTestApplication application;
707
708   TextView view = TextView::New( "Hello world!" );
709   Stage::GetCurrent().Add( view );
710
711   //Test multiline policy property
712   view.SetProperty(view.GetPropertyIndex(PROPERTY_MULTILINE_POLICY), "SplitByNewLineChar");
713   DALI_TEST_CHECK( Toolkit::TextView::SplitByNewLineChar == view.GetMultilinePolicy() );
714
715   view.SetProperty(view.GetPropertyIndex(PROPERTY_MULTILINE_POLICY), "SplitByWord");
716   DALI_TEST_CHECK( Toolkit::TextView::SplitByWord == view.GetMultilinePolicy() );
717
718   view.SetProperty(view.GetPropertyIndex(PROPERTY_MULTILINE_POLICY), "SplitByChar");
719   DALI_TEST_CHECK( Toolkit::TextView::SplitByChar == view.GetMultilinePolicy() );
720
721   //Test width exceed policy property
722   view.SetProperty(view.GetPropertyIndex(PROPERTY_WIDTH_EXCEED_POLICY), "Original");
723   view.SetProperty(view.GetPropertyIndex(PROPERTY_HEIGHT_EXCEED_POLICY), "Original");
724   DALI_TEST_CHECK( Toolkit::TextView::Original == view.GetWidthExceedPolicy() );
725   DALI_TEST_CHECK( Toolkit::TextView::Original == view.GetHeightExceedPolicy() );
726
727   view.SetProperty(view.GetPropertyIndex(PROPERTY_WIDTH_EXCEED_POLICY), "Fade");
728   view.SetProperty(view.GetPropertyIndex(PROPERTY_HEIGHT_EXCEED_POLICY), "Fade");
729   DALI_TEST_CHECK( Toolkit::TextView::Fade == view.GetWidthExceedPolicy() );
730   DALI_TEST_CHECK( Toolkit::TextView::Fade == view.GetHeightExceedPolicy() );
731
732   view.SetProperty(view.GetPropertyIndex(PROPERTY_WIDTH_EXCEED_POLICY), "ShrinkToFit");
733   view.SetProperty(view.GetPropertyIndex(PROPERTY_HEIGHT_EXCEED_POLICY), "ShrinkToFit");
734   DALI_TEST_CHECK( Toolkit::TextView::ShrinkToFit == view.GetWidthExceedPolicy() );
735   DALI_TEST_CHECK( Toolkit::TextView::ShrinkToFit == view.GetHeightExceedPolicy() );
736
737   //Test line justification property
738   view.SetProperty(view.GetPropertyIndex(PROPERTY_LINE_JUSTIFICATION), "Left");
739   DALI_TEST_CHECK( Toolkit::TextView::Left == view.GetLineJustification() );
740
741   view.SetProperty(view.GetPropertyIndex(PROPERTY_LINE_JUSTIFICATION), "Center");
742   DALI_TEST_CHECK( Toolkit::TextView::Center == view.GetLineJustification() );
743
744   view.SetProperty(view.GetPropertyIndex(PROPERTY_LINE_JUSTIFICATION), "Right");
745   DALI_TEST_CHECK( Toolkit::TextView::Right == view.GetLineJustification() );
746
747   view.SetProperty(view.GetPropertyIndex(PROPERTY_LINE_JUSTIFICATION), "Justified");
748   DALI_TEST_CHECK( Toolkit::TextView::Justified == view.GetLineJustification() );
749
750   //Test fade boundary property
751   const Vector4 testValue( 23.f, 26.f, 2.f, 11.f );
752
753   view.SetProperty(view.GetPropertyIndex(PROPERTY_FADE_BOUNDARY), testValue);
754   DALI_TEST_CHECK( testValue.x == view.GetFadeBoundary().mLeft );
755   DALI_TEST_CHECK( testValue.y == view.GetFadeBoundary().mRight );
756   DALI_TEST_CHECK( testValue.z == view.GetFadeBoundary().mTop );
757   DALI_TEST_CHECK( testValue.w == view.GetFadeBoundary().mBottom );
758
759   //Test Line height offset property
760   float testOffsetValue = 14.04f;
761   view.SetProperty(view.GetPropertyIndex(PROPERTY_LINE_HEIGHT_OFFSET), testOffsetValue);
762   DALI_TEST_CHECK( PointSize(testOffsetValue) == view.GetLineHeightOffset() );
763
764   //Test alignment property
765   view.SetProperty(view.GetPropertyIndex(PROPERTY_HORIZONTAL_ALIGNMENT), "HorizontalLeft");
766   view.SetProperty(view.GetPropertyIndex(PROPERTY_VERTICAL_ALIGNMENT), "VerticalTop");
767   DALI_TEST_CHECK( (Toolkit::Alignment::HorizontalLeft | Toolkit::Alignment::VerticalTop) == view.GetTextAlignment() );
768   END_TEST;
769 }
770
771 int UtcDaliTextViewSetSortModifier(void)
772 {
773   tet_infoline("UtcDaliTextViewSetAndGetText: ");
774   ToolkitTestApplication application;
775
776   TextView view = TextView::New( "Hello world!" );
777   Stage::GetCurrent().Add( view );
778
779   view.SetSortModifier( 10.f );
780   view.SetSnapshotModeEnabled( false );
781
782   application.SendNotification();
783   application.Render();
784
785   DALI_TEST_EQUALS( RenderableActor::DownCast(view.GetChildAt(0)).GetSortModifier(), 10.f, Math::MACHINE_EPSILON_1000, TEST_LOCATION );
786   END_TEST;
787 }
788
789 int UtcDaliTextViewUnderlineText(void)
790 {
791   tet_infoline("UtcDaliTextViewUnderlineText: ");
792   ToolkitTestApplication application;
793
794   TextView textView = TextView::New();
795   textView.SetSnapshotModeEnabled( false );
796   textView.SetMarkupProcessingEnabled( true );
797   textView.SetText( "<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>" );
798
799   textView.SetSize( 150.f, 100.f );
800
801   Stage::GetCurrent().Add( textView );
802
803   application.SendNotification();
804   application.Render();
805
806   std::vector<float> positions;
807   positions.push_back( 6.448784f );
808   positions.push_back( 9.862847f );
809   positions.push_back( 13.276909f );
810   positions.push_back( 16.690973f );
811   positions.push_back( 13.276909f );
812   positions.push_back( 9.862847f );
813   positions.push_back( 6.448784f );
814
815   for( std::size_t index = 0, num = textView.GetChildCount(); index < num; ++index )
816   {
817     TextStyle style = TextActor::DownCast( textView.GetChildAt(index) ).GetTextStyle();
818
819     DALI_TEST_EQUALS( 4.17274f, style.GetUnderlineThickness(), Math::MACHINE_EPSILON_1000, TEST_LOCATION );
820     DALI_TEST_EQUALS( positions[index], style.GetUnderlinePosition(), Math::MACHINE_EPSILON_1000, TEST_LOCATION );
821   }
822   END_TEST;
823 }