Conversion to Apache 2.0 license
[platform/core/uifw/dali-toolkit.git] / automated-tests / TET / dali-internal-test-suite / text-view / 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 <tet_api.h>
22
23 #include <dali/public-api/dali-core.h>
24 #include <dali-toolkit/dali-toolkit.h>
25
26 #include <dali-toolkit-test-suite-utils.h>
27
28 // Internal headers are allowed here
29 #include <dali-toolkit/internal/controls/text-view/split-by-new-line-char-policies.h>
30 #include <dali-toolkit/internal/controls/text-view/text-view-impl.h>
31 #include <dali-toolkit/internal/controls/text-view/text-view-processor.h>
32 #include <dali-toolkit/internal/controls/text-view/text-view-processor-helper-functions.h>
33 #include <dali-toolkit/internal/controls/text-view/text-view-processor-dbg.h>
34
35 using namespace Dali;
36 using namespace Dali::Toolkit;
37 using namespace Dali::Toolkit::Internal;
38
39 namespace
40 {
41 // Data structures used to create an 'experiment' in TET cases
42
43 const Toolkit::Internal::TextView::LayoutParameters DEFAULT_LAYOUT_PARAMETERS;
44 const Toolkit::Internal::TextView::VisualParameters DEFAULT_VISUAL_PARAMETERS;
45
46 struct GetIndicesFromGlobalCharacterIndexTest
47 {
48   std::string description;
49   std::string input;
50   std::size_t position;
51   std::size_t lineIndex;
52   std::size_t groupIndex;
53   std::size_t wordIndex;
54   std::size_t characterIndex;
55 };
56
57 /**
58  * Gets the line, group, word, and character indices for a given text and a given position and checks the results with the given indices.
59  *
60  * If the test fails it prints a short description and the line where this function was called.
61  *
62  * @param description Short description of the experiment.
63  * @param input The input text.
64  * @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.
65  * @param resultLineIndex Index to the line where the character is located.
66  * @param resultGroupIndex Index to the group within the line where the character is located.
67  * @param resultWordIndex Index to the word within the group where the character is located.
68  * @param resultCharacterIndex Index to the character within the word where the character is located.
69  * @param location Where this function has been called.
70  *
71  * @return \e true if the experiment is successful. Otherwise returns \e false.
72  */
73 bool TestGetIndicesFromGlobalCharacterIndex( const std::string& description,
74                                              const std::string& input,
75                                              const std::size_t position,
76                                              const std::size_t resultLineIndex,
77                                              const std::size_t resultGroupIndex,
78                                              const std::size_t resultWordIndex,
79                                              const std::size_t resultCharacterIndex,
80                                              const char* location )
81 {
82   tet_printf( "%s", description.c_str() );
83
84   // Create natural size, layout and text-actor info for the input word.
85   Toolkit::Internal::TextView::RelayoutData relayoutData;
86   TextViewProcessor::TextLayoutInfo& inputLayout( relayoutData.mTextLayoutInfo );
87
88   MarkupProcessor::StyledTextArray inputStyledText;
89   MarkupProcessor::GetStyledTextArray( input, inputStyledText, true );
90
91   TextViewProcessor::CreateTextInfo( inputStyledText,
92                                      DEFAULT_LAYOUT_PARAMETERS,
93                                      relayoutData );
94
95   TextViewProcessor::TextInfoIndices indices;
96   TextViewProcessor::GetIndicesFromGlobalCharacterIndex( position,
97                                                          inputLayout,
98                                                          indices );
99
100   if( indices.mLineIndex != resultLineIndex )
101   {
102     tet_printf( "Fail. different line index. %s", location );
103     return false;
104   }
105   if( indices.mGroupIndex != resultGroupIndex )
106   {
107     tet_printf( "Fail. different group index. %s", location );
108     return false;
109   }
110   if( indices.mWordIndex != resultWordIndex )
111   {
112     tet_printf( "Fail. different word index. %s", location );
113     return false;
114   }
115   if( indices.mCharacterIndex != resultCharacterIndex )
116   {
117     tet_printf( "Fail. different character index. %s", location );
118     return false;
119   }
120
121   return true;
122 }
123
124 //////////////////////////////////////////////////////////////////
125 } // namespace
126
127 static void Startup();
128 static void Cleanup();
129
130 extern "C" {
131   void (*tet_startup)() = Startup;
132   void (*tet_cleanup)() = Cleanup;
133 }
134
135 enum {
136   POSITIVE_TC_IDX = 0x01,
137   NEGATIVE_TC_IDX,
138 };
139
140 #define MAX_NUMBER_OF_TESTS 10000
141 extern "C" {
142   struct tet_testlist tet_testlist[MAX_NUMBER_OF_TESTS];
143 }
144
145 // Add test functionality for all APIs in the class (Positive and Negative)
146 TEST_FUNCTION( UtcDaliTextViewGetIndicesFromGlobalCharacterIndex, POSITIVE_TC_IDX ); // Tests correctness when indices to lines, groups, words and characters are worked out from a given global position.
147 TEST_FUNCTION( UtcDaliTextViewDebugCouts, POSITIVE_TC_IDX );                         // Tests debug functions just to not to penalize the coverage.
148
149 // Called only once before first test is run.
150 static void Startup()
151 {
152 }
153
154 // Called only once after last test is run
155 static void Cleanup()
156 {
157 }
158
159 static void UtcDaliTextViewGetIndicesFromGlobalCharacterIndex()
160 {
161   ToolkitTestApplication application;
162
163   tet_infoline("UtcDaliTextViewGetIndicesFromGlobalCharacterIndex : ");
164   struct GetIndicesFromGlobalCharacterIndexTest getIndicesFromGlobalCharacterIndexTests[] =
165   {
166     {
167       std::string( "Test position 0" ),
168       std::string( "text te<font size='30'>xt text te</font>xt text\n"
169                    "text t<font size='30'>ext טקסט טקסט te</font>xt\n"
170                    "text text text text text\n"
171                    "\n" ),
172       0,
173       0,
174       0,
175       0,
176       0
177     },
178     {
179       std::string( "Test position 76. (just after the last \\n)" ),
180       std::string( "t<font size='30'>ext text te</font>xt text text\n"
181                    "text text טקסט טקסט text\n"
182                    "text text te<font size='30'>xt text</font> text\n"
183                    "\n" ),
184       76,
185       4,
186       0,
187       0,
188       0
189     },
190     {
191       std::string( "Test position 73. (the last \\n)" ),
192       std::string( "text te<font size='30'>xt text text </font>text\n"
193                    "text text טק<font size='30'>סט טקס</font>ט text\n"
194                    "text text text text text\n"
195                    "\n" ),
196       75,
197       3,
198       0,
199       0,
200       0
201     },
202     {
203       std::string( "Test position 35. (first hebrew character)" ),
204       std::string( "text text text text text\n"
205                    "text text טקסט טקסט text\n"
206                    "text text text text text\n"
207                    "\n" ),
208       35,
209       1,
210       1,
211       0,
212       0
213     },
214     {
215       std::string( "Test position 3. (end of the first word)" ),
216       std::string( "text te<font size='30'>xt text text text\n</font>"
217                    "text text טק<font size='30'>סט טקסט </font>text\n"
218                    "text te<font size='30'>xt text text</font> text\n"
219                    "\n" ),
220       3,
221       0,
222       0,
223       0,
224       3
225     },
226     /* TODO Check for mixed RTL and LTR text.
227     {
228       std::string( "Test position 33. (end of the second word of the second line)" ),
229       std::string( "text te<font size='30'>xt text text text\n</font>"
230                    "text text טק<font size='30'>סט טקסט </font>text\n"
231                    "text te<font size='30'>xt text text</font> text\n"
232                    "\n" ),
233       33,
234       1,
235       0,
236       2,
237       3
238     },
239     {
240       std::string( "Test position 43. (last hebrew character)" ),
241       std::string( "text te<font size='30'>xt text text text\n</font>"
242                    "text text טק<font size='30'>סט טקסט </font>text\n"
243                    "text te<font size='30'>xt text text</font> text\n"
244                    "\n" ),
245       43,
246       1,
247       1,
248       3,
249       3
250     },
251     */
252   };
253   const std::size_t numberOfTests( 5 );
254
255   for( std::size_t index = 0; index < numberOfTests; ++index )
256   {
257     const GetIndicesFromGlobalCharacterIndexTest& test = getIndicesFromGlobalCharacterIndexTests[index];
258
259     if( !TestGetIndicesFromGlobalCharacterIndex( test.description, test.input, test.position, test.lineIndex, test.groupIndex, test.wordIndex, test.characterIndex, TEST_LOCATION ) )
260     {
261       tet_result( TET_FAIL );
262     }
263   }
264
265   tet_result( TET_PASS );
266 }
267
268 static void UtcDaliTextViewDebugCouts()
269 {
270   /////////////////////////////////////////////////////
271   // Text debug functions to not to penalize coverage
272   /////////////////////////////////////////////////////
273
274   ToolkitTestApplication application;
275
276   tet_infoline("UtcDaliTextViewDebugCouts : ");
277
278   Toolkit::Internal::TextView::RelayoutData relayoutData;
279
280   MarkupProcessor::StyledTextArray inputStyledText;
281   MarkupProcessor::GetStyledTextArray( std::string( "Hello world\nhello world" ), inputStyledText, true );
282
283   TextViewProcessor::CreateTextInfo( inputStyledText,
284                                      DEFAULT_LAYOUT_PARAMETERS,
285                                      relayoutData );
286
287   Actor dummy = Actor::New();
288   Toolkit::Internal::SplitByNewLineChar::Relayout( dummy,
289                                                    Toolkit::Internal::TextView::RELAYOUT_ALL,
290                                                    DEFAULT_LAYOUT_PARAMETERS,
291                                                    DEFAULT_VISUAL_PARAMETERS,
292                                                    relayoutData );
293
294   TextViewProcessor::dbgPrint( relayoutData.mTextLayoutInfo );
295
296   TextStyle textStyle;
297   TextViewProcessor::dbgPrint( textStyle );
298
299   TextViewProcessor::TextInfoIndices indices;
300   TextViewProcessor::dbgPrint( indices );
301
302   TextViewProcessor::dbgPrint( inputStyledText );
303
304   tet_result( TET_PASS );
305 }