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