Merge "(ItemLayout) Remove redundant GetResizeAnimation" into devel/master
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-internal / utc-Dali-Text-Segmentation.cpp
1 /*
2  * Copyright (c) 2015 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/internal/text/character-set-conversion.h>
22 #include <dali-toolkit/internal/text/segmentation.h>
23 #include <dali-toolkit-test-suite-utils.h>
24 #include <dali-toolkit/dali-toolkit.h>
25
26
27 using namespace Dali;
28 using namespace Toolkit;
29 using namespace Text;
30
31 // Tests the following functions with different scripts.
32 // void SetLineBreakInfo( const Vector<Character>& text, Vector<LineBreakInfo>& lineBreakInfo );
33 // void SetWordBreakInfo( const Vector<Character>& text, Vector<WordBreakInfo>& wordBreakInfo );
34
35 //////////////////////////////////////////////////////////
36
37 namespace
38 {
39
40 struct BreakInfoData
41 {
42   std::string description; ///< Description of the test.
43   std::string text;        ///< input text.
44   std::string breakInfo;   ///< The expected break info.
45 };
46
47 bool LineBreakInfoTest( const BreakInfoData& data )
48 {
49   // 1) Convert to utf32
50   Vector<Character> utf32;
51   utf32.Resize( data.text.size() );
52
53   const uint32_t numberOfCharacters = Utf8ToUtf32( reinterpret_cast<const uint8_t* const>( data.text.c_str() ),
54                                                    data.text.size(),
55                                                    &utf32[0u] );
56   utf32.Resize( numberOfCharacters );
57
58   // 2) Set the line break info.
59   Vector<LineBreakInfo> lineBreakInfo;
60   lineBreakInfo.Resize( numberOfCharacters );
61
62   SetLineBreakInfo( utf32, lineBreakInfo );
63
64   // 3) compare the results
65   std::ostringstream breakInfo;
66
67   for( unsigned int index = 0u; index < numberOfCharacters; ++index )
68   {
69     breakInfo << static_cast<unsigned int>( lineBreakInfo[index] );
70   }
71
72   return data.breakInfo == breakInfo.str();
73 }
74
75 bool WordBreakInfoTest( const BreakInfoData& data )
76 {
77   // 1) Convert to utf32
78   Vector<Character> utf32;
79   utf32.Resize( data.text.size() );
80
81   const uint32_t numberOfCharacters = Utf8ToUtf32( reinterpret_cast<const uint8_t* const>( data.text.c_str() ),
82                                                    data.text.size(),
83                                                    &utf32[0u] );
84   utf32.Resize( numberOfCharacters );
85
86   // 2) Set the word break info.
87   Vector<WordBreakInfo> wordBreakInfo;
88   wordBreakInfo.Resize( numberOfCharacters );
89
90   SetWordBreakInfo( utf32, wordBreakInfo );
91
92   // 3) compare the results
93   std::ostringstream breakInfo;
94
95   for( unsigned int index = 0u; index < numberOfCharacters; ++index )
96   {
97     breakInfo << static_cast<unsigned int>( wordBreakInfo[index] );
98   }
99
100   return data.breakInfo == breakInfo.str();
101 }
102
103 } // namespace
104
105 //////////////////////////////////////////////////////////
106
107 int UtcDaliTextSegnemtationSetLineBreakInfo(void)
108 {
109   ToolkitTestApplication application;
110   tet_infoline(" UtcDaliTextSegnemtationSetLineBreakInfo");
111
112   struct BreakInfoData data[] =
113   {
114     {
115       "Zero characters",
116       "",
117       "",
118     },
119     {
120       "Latin script",
121       "Hello world",
122       "22222122220",
123     },
124     {
125       "Latin script with \n",
126       "Hello\nworld",
127       "22222022220",
128     },
129     {
130       "Japanese script",
131       "こんにちは世界",
132       "1111110",
133     },
134     {
135       "Japanese script with \n",
136       "こんにちは\n世界",
137       "11112010",
138     },
139     {
140       "Chinese script",
141       "你好世界",
142       "1110",
143     },
144     {
145       "Chinese script with \n",
146       "你好\n世界",
147       "12010",
148     }
149   };
150   const unsigned int numberOfTests = 7u;
151
152   for( unsigned int index = 0u; index < numberOfTests; ++index )
153   {
154     if( !LineBreakInfoTest( data[index] ) )
155     {
156       tet_result(TET_FAIL);
157     }
158   }
159
160   tet_result(TET_PASS);
161   END_TEST;
162 }
163
164 int UtcDaliTextSegnemtationSetWordBreakInfo(void)
165 {
166   ToolkitTestApplication application;
167   tet_infoline(" UtcDaliTextSegnemtationSetWordBreakInfo");
168
169   struct BreakInfoData data[] =
170   {
171     {
172       "Zero characters",
173       "",
174       "",
175     },
176     {
177       "Latin script",
178       "Hello world",
179       "11110011110",
180     },
181     {
182       "Latin script with \n",
183       "Hello\nworld",
184       "11110011110",
185     },
186     {
187       "Japanese script",
188       "こんにちは世界",
189       "0000000",
190     },
191     {
192       "Japanese script with \n",
193       "こんにちは\n世界",
194       "00000000",
195     },
196     {
197       "Chinese script",
198       "你好世界",
199       "0000",
200     },
201     {
202       "Chinese script with \n",
203       "你好\n世界",
204       "00000",
205     }
206   };
207   const unsigned int numberOfTests = 7u;
208
209   for( unsigned int index = 0u; index < numberOfTests; ++index )
210   {
211     if( !WordBreakInfoTest( data[index] ) )
212     {
213       tet_result(TET_FAIL);
214     }
215   }
216
217   tet_result(TET_PASS);
218   END_TEST;
219 }