f5e39e6ee5017036b55dd8ab8017550d66a385f6
[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 paragraphIndex;
59   std::size_t wordIndex;
60   std::size_t characterIndex;
61 };
62
63 /**
64  * Gets the paragraph, word, and character indices for a given text and a given position and checks the results with the given indices.
65  *
66  * If the test fails it prints a short description and the line where this function was called.
67  *
68  * @param description Short description of the experiment.
69  * @param input The input text.
70  * @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.
71  * @param resultParagraphIndex Index to the paragraph where the character is located.
72  * @param resultWordIndex Index to the word within the paragraph where the character is located.
73  * @param resultCharacterIndex Index to the character within the word where the character is located.
74  * @param location Where this function has been called.
75  *
76  * @return \e true if the experiment is successful. Otherwise returns \e false.
77  */
78 bool TestGetIndicesFromGlobalCharacterIndex( const std::string& description,
79                                              const std::string& input,
80                                              const std::size_t position,
81                                              const std::size_t resultParagraphIndex,
82                                              const std::size_t resultWordIndex,
83                                              const std::size_t resultCharacterIndex,
84                                              const char* location )
85 {
86   tet_printf( "%s", description.c_str() );
87
88   // Create natural size, layout and text-actor info for the input word.
89   Toolkit::Internal::TextView::RelayoutData relayoutData;
90   TextViewProcessor::TextLayoutInfo& inputLayout( relayoutData.mTextLayoutInfo );
91
92   MarkupProcessor::StyledTextArray inputStyledText;
93   MarkupProcessor::GetStyledTextArray( input, inputStyledText, true );
94
95   TextViewProcessor::CreateTextInfo( inputStyledText,
96                                      DEFAULT_LAYOUT_PARAMETERS,
97                                      relayoutData );
98
99   TextViewProcessor::TextInfoIndices indices;
100   TextViewProcessor::GetIndicesFromGlobalCharacterIndex( position,
101                                                          inputLayout,
102                                                          indices );
103
104   if( indices.mParagraphIndex != resultParagraphIndex )
105   {
106     tet_printf( "Fail. different paragraph index. %s", location );
107     return false;
108   }
109   if( indices.mWordIndex != resultWordIndex )
110   {
111     tet_printf( "Fail. different word index. %s", location );
112     return false;
113   }
114   if( indices.mCharacterIndex != resultCharacterIndex )
115   {
116     tet_printf( "Fail. different character index. %s", location );
117     return false;
118   }
119
120   return true;
121 }
122
123 //////////////////////////////////////////////////////////////////
124 } // namespace
125
126
127 int UtcDaliTextViewGetIndicesFromGlobalCharacterIndex(void)
128 {
129   ToolkitTestApplication application;
130
131   tet_infoline("UtcDaliTextViewGetIndicesFromGlobalCharacterIndex : ");
132   struct GetIndicesFromGlobalCharacterIndexTest getIndicesFromGlobalCharacterIndexTests[] =
133   {
134     {
135       std::string( "Test position 0" ),
136       std::string( "text te<font size='30'>xt text te</font>xt text\n"
137                    "text t<font size='30'>ext טקסט טקסט te</font>xt\n"
138                    "text text text text text\n"
139                    "\n" ),
140       0,
141       0,
142       0,
143       0
144     },
145     {
146       std::string( "Test position 76. (just after the last \\n)" ),
147       std::string( "t<font size='30'>ext text te</font>xt text text\n"
148                    "text text טקסט טקסט text\n"
149                    "text text te<font size='30'>xt text</font> text\n"
150                    "\n" ),
151       76,
152       4,
153       0,
154       0
155     },
156     {
157       std::string( "Test position 73. (the last \\n)" ),
158       std::string( "text te<font size='30'>xt text text </font>text\n"
159                    "text text טק<font size='30'>סט טקס</font>ט text\n"
160                    "text text text text text\n"
161                    "\n" ),
162       75,
163       3,
164       0,
165       0
166     },
167     {
168       std::string( "Test position 35. (first hebrew character)" ),
169       std::string( "text text text text text\n"
170                    "text text טקסט טקסט text\n"
171                    "text text text text text\n"
172                    "\n" ),
173       35,
174       1,
175       4,
176       0
177     },
178     {
179       std::string( "Test position 3. (end of the first word)" ),
180       std::string( "text te<font size='30'>xt text text text\n</font>"
181                    "text text טק<font size='30'>סט טקסט </font>text\n"
182                    "text te<font size='30'>xt text text</font> text\n"
183                    "\n" ),
184       3,
185       0,
186       0,
187       3
188     },
189     {
190       std::string( "Test position 33. (end of the second word of the second paragraph)" ),
191       std::string( "text te<font size='30'>xt text text text\n</font>"
192                    "text text טק<font size='30'>סט טקסט </font>text\n"
193                    "text te<font size='30'>xt text text</font> text\n"
194                    "\n" ),
195       33,
196       1,
197       2,
198       3
199     },
200     {
201       std::string( "Test position 43. (last hebrew character)" ),
202       std::string( "text te<font size='30'>xt text text text\n</font>"
203                    "text text טק<font size='30'>סט טקסט </font>text\n"
204                    "text te<font size='30'>xt text text</font> text\n"
205                    "\n" ),
206       43,
207       1,
208       6,
209       3
210     },
211   };
212   const std::size_t numberOfTests( 7 );
213
214   for( std::size_t index = 0; index < numberOfTests; ++index )
215   {
216     const GetIndicesFromGlobalCharacterIndexTest& test = getIndicesFromGlobalCharacterIndexTests[index];
217
218     if( !TestGetIndicesFromGlobalCharacterIndex( test.description, test.input, test.position, test.paragraphIndex, test.wordIndex, test.characterIndex, TEST_LOCATION ) )
219     {
220       tet_result( TET_FAIL );
221     }
222   }
223
224   tet_result( TET_PASS );
225   END_TEST;
226 }
227
228 int UtcDaliTextViewDebugCouts(void)
229 {
230   /////////////////////////////////////////////////////
231   // Text debug functions to not to penalize coverage
232   /////////////////////////////////////////////////////
233
234   ToolkitTestApplication application;
235
236   tet_infoline("UtcDaliTextViewDebugCouts : ");
237
238   Toolkit::Internal::TextView::RelayoutData relayoutData;
239
240   MarkupProcessor::StyledTextArray inputStyledText;
241   MarkupProcessor::GetStyledTextArray( std::string( "Hello world\nhello world" ), inputStyledText, true );
242
243   TextViewProcessor::CreateTextInfo( inputStyledText,
244                                      DEFAULT_LAYOUT_PARAMETERS,
245                                      relayoutData );
246
247   Actor dummy = Actor::New();
248   Toolkit::Internal::SplitByNewLineChar::Relayout( dummy,
249                                                    Toolkit::Internal::TextView::RELAYOUT_ALL,
250                                                    DEFAULT_LAYOUT_PARAMETERS,
251                                                    DEFAULT_VISUAL_PARAMETERS,
252                                                    relayoutData );
253
254   TextViewProcessor::dbgPrint( relayoutData.mTextLayoutInfo );
255
256   TextStyle textStyle;
257   TextViewProcessor::dbgPrint( textStyle );
258
259   TextViewProcessor::TextInfoIndices indices;
260   TextViewProcessor::dbgPrint( indices );
261
262   TextViewProcessor::dbgPrint( inputStyledText );
263
264   tet_result( TET_PASS );
265   END_TEST;
266 }