d17835655c196f919c507ee876626741940a89e4
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-internal / utc-Dali-TextView-HelperAndDebug.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <iostream>
19
20 #include <stdlib.h>
21 #include <dali-toolkit-test-suite-utils.h>
22 #include <dali-toolkit/dali-toolkit.h>
23
24 // Internal headers are allowed here
25 #include <dali-toolkit/internal/controls/text-view/split-by-new-line-char-policies.h>
26 #include <dali-toolkit/internal/controls/text-view/text-view-impl.h>
27 #include <dali-toolkit/internal/controls/text-view/text-view-processor.h>
28 #include <dali-toolkit/internal/controls/text-view/text-view-processor-helper-functions.h>
29 #include <dali-toolkit/internal/controls/text-view/text-view-processor-dbg.h>
30
31 using namespace Dali;
32 using namespace Dali::Toolkit;
33 using namespace Dali::Toolkit::Internal;
34
35 void dali_text_view_helper_and_debug_startup(void)
36 {
37   test_return_value = TET_UNDEF;
38 }
39
40 void dali_text_view_helper_and_debug_cleanup(void)
41 {
42   test_return_value = TET_PASS;
43 }
44
45
46 namespace
47 {
48 // Data structures used to create an 'experiment' in TET cases
49
50 const Toolkit::Internal::TextView::LayoutParameters DEFAULT_LAYOUT_PARAMETERS;
51 const Toolkit::Internal::TextView::VisualParameters DEFAULT_VISUAL_PARAMETERS;
52
53 struct GetIndicesFromGlobalCharacterIndexTest
54 {
55   std::string description;
56   std::string input;
57   std::size_t position;
58   std::size_t lineIndex;
59   std::size_t groupIndex;
60   std::size_t wordIndex;
61   std::size_t characterIndex;
62 };
63
64 /**
65  * Gets the line, group, word, and character indices for a given text and a given position and checks the results with the given indices.
66  *
67  * If the test fails it prints a short description and the line where this function was called.
68  *
69  * @param description Short description of the experiment.
70  * @param input The input text.
71  * @param position Global position of the character. i.e in a text with with 1000 characters, position could be any value from 0 to 1000.
72  * @param resultLineIndex Index to the line where the character is located.
73  * @param resultGroupIndex Index to the group within the line where the character is located.
74  * @param resultWordIndex Index to the word within the group where the character is located.
75  * @param resultCharacterIndex Index to the character within the word where the character is located.
76  * @param location Where this function has been called.
77  *
78  * @return \e true if the experiment is successful. Otherwise returns \e false.
79  */
80 bool TestGetIndicesFromGlobalCharacterIndex( const std::string& description,
81                                              const std::string& input,
82                                              const std::size_t position,
83                                              const std::size_t resultLineIndex,
84                                              const std::size_t resultGroupIndex,
85                                              const std::size_t resultWordIndex,
86                                              const std::size_t resultCharacterIndex,
87                                              const char* location )
88 {
89   tet_printf( "%s", description.c_str() );
90
91   // Create natural size, layout and text-actor info for the input word.
92   Toolkit::Internal::TextView::RelayoutData relayoutData;
93   TextViewProcessor::TextLayoutInfo& inputLayout( relayoutData.mTextLayoutInfo );
94
95   MarkupProcessor::StyledTextArray inputStyledText;
96   MarkupProcessor::GetStyledTextArray( input, inputStyledText, true );
97
98   TextViewProcessor::CreateTextInfo( inputStyledText,
99                                      DEFAULT_LAYOUT_PARAMETERS,
100                                      relayoutData );
101
102   TextViewProcessor::TextInfoIndices indices;
103   TextViewProcessor::GetIndicesFromGlobalCharacterIndex( position,
104                                                          inputLayout,
105                                                          indices );
106
107   if( indices.mLineIndex != resultLineIndex )
108   {
109     tet_printf( "Fail. different line index. %s", location );
110     return false;
111   }
112   if( indices.mGroupIndex != resultGroupIndex )
113   {
114     tet_printf( "Fail. different group index. %s", location );
115     return false;
116   }
117   if( indices.mWordIndex != resultWordIndex )
118   {
119     tet_printf( "Fail. different word index. %s", location );
120     return false;
121   }
122   if( indices.mCharacterIndex != resultCharacterIndex )
123   {
124     tet_printf( "Fail. different character index. %s", location );
125     return false;
126   }
127
128   return true;
129 }
130
131 //////////////////////////////////////////////////////////////////
132 } // namespace
133
134
135 int UtcDaliTextViewGetIndicesFromGlobalCharacterIndex(void)
136 {
137   ToolkitTestApplication application;
138
139   tet_infoline("UtcDaliTextViewGetIndicesFromGlobalCharacterIndex : ");
140   struct GetIndicesFromGlobalCharacterIndexTest getIndicesFromGlobalCharacterIndexTests[] =
141   {
142     {
143       std::string( "Test position 0" ),
144       std::string( "text te<font size='30'>xt text te</font>xt text\n"
145                    "text t<font size='30'>ext טקסט טקסט te</font>xt\n"
146                    "text text text text text\n"
147                    "\n" ),
148       0,
149       0,
150       0,
151       0,
152       0
153     },
154     {
155       std::string( "Test position 76. (just after the last \\n)" ),
156       std::string( "t<font size='30'>ext text te</font>xt text text\n"
157                    "text text טקסט טקסט text\n"
158                    "text text te<font size='30'>xt text</font> text\n"
159                    "\n" ),
160       76,
161       4,
162       0,
163       0,
164       0
165     },
166     {
167       std::string( "Test position 73. (the last \\n)" ),
168       std::string( "text te<font size='30'>xt text text </font>text\n"
169                    "text text טק<font size='30'>סט טקס</font>ט text\n"
170                    "text text text text text\n"
171                    "\n" ),
172       75,
173       3,
174       0,
175       0,
176       0
177     },
178     {
179       std::string( "Test position 35. (first hebrew character)" ),
180       std::string( "text text text text text\n"
181                    "text text טקסט טקסט text\n"
182                    "text text text text text\n"
183                    "\n" ),
184       35,
185       1,
186       1,
187       0,
188       0
189     },
190     {
191       std::string( "Test position 3. (end of the first word)" ),
192       std::string( "text te<font size='30'>xt text text text\n</font>"
193                    "text text טק<font size='30'>סט טקסט </font>text\n"
194                    "text te<font size='30'>xt text text</font> text\n"
195                    "\n" ),
196       3,
197       0,
198       0,
199       0,
200       3
201     },
202     /* TODO Check for mixed RTL and LTR text.
203     {
204       std::string( "Test position 33. (end of the second word of the second line)" ),
205       std::string( "text te<font size='30'>xt text text text\n</font>"
206                    "text text טק<font size='30'>סט טקסט </font>text\n"
207                    "text te<font size='30'>xt text text</font> text\n"
208                    "\n" ),
209       33,
210       1,
211       0,
212       2,
213       3
214     },
215     {
216       std::string( "Test position 43. (last hebrew character)" ),
217       std::string( "text te<font size='30'>xt text text text\n</font>"
218                    "text text טק<font size='30'>סט טקסט </font>text\n"
219                    "text te<font size='30'>xt text text</font> text\n"
220                    "\n" ),
221       43,
222       1,
223       1,
224       3,
225       3
226     },
227     */
228   };
229   const std::size_t numberOfTests( 5 );
230
231   for( std::size_t index = 0; index < numberOfTests; ++index )
232   {
233     const GetIndicesFromGlobalCharacterIndexTest& test = getIndicesFromGlobalCharacterIndexTests[index];
234
235     if( !TestGetIndicesFromGlobalCharacterIndex( test.description, test.input, test.position, test.lineIndex, test.groupIndex, test.wordIndex, test.characterIndex, TEST_LOCATION ) )
236     {
237       tet_result( TET_FAIL );
238     }
239   }
240
241   tet_result( TET_PASS );
242   END_TEST;
243 }
244
245 int UtcDaliTextViewDebugCouts(void)
246 {
247   /////////////////////////////////////////////////////
248   // Text debug functions to not to penalize coverage
249   /////////////////////////////////////////////////////
250
251   ToolkitTestApplication application;
252
253   tet_infoline("UtcDaliTextViewDebugCouts : ");
254
255   Toolkit::Internal::TextView::RelayoutData relayoutData;
256
257   MarkupProcessor::StyledTextArray inputStyledText;
258   MarkupProcessor::GetStyledTextArray( std::string( "Hello world\nhello world" ), inputStyledText, true );
259
260   TextViewProcessor::CreateTextInfo( inputStyledText,
261                                      DEFAULT_LAYOUT_PARAMETERS,
262                                      relayoutData );
263
264   Actor dummy = Actor::New();
265   Toolkit::Internal::SplitByNewLineChar::Relayout( dummy,
266                                                    Toolkit::Internal::TextView::RELAYOUT_ALL,
267                                                    DEFAULT_LAYOUT_PARAMETERS,
268                                                    DEFAULT_VISUAL_PARAMETERS,
269                                                    relayoutData );
270
271   TextViewProcessor::dbgPrint( relayoutData.mTextLayoutInfo );
272
273   TextStyle textStyle;
274   TextViewProcessor::dbgPrint( textStyle );
275
276   TextViewProcessor::TextInfoIndices indices;
277   TextViewProcessor::dbgPrint( indices );
278
279   TextViewProcessor::dbgPrint( inputStyledText );
280
281   tet_result( TET_PASS );
282   END_TEST;
283 }