[dali_1.2.16] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-internal / utc-Dali-VisualModel.cpp
1 /*
2  * Copyright (c) 2016 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
22 #include <dali-toolkit-test-suite-utils.h>
23 #include <dali-toolkit/dali-toolkit.h>
24 #include <toolkit-text-utils.h>
25
26
27 using namespace Dali;
28 using namespace Toolkit;
29 using namespace Text;
30
31 // Tests the following functions.
32 //
33 // void CreateCharacterToGlyphTable( CharacterIndex startIndex,
34 //                                   Length numberOfCharacters )
35 //
36 // void CreateGlyphsPerCharacterTable( CharacterIndex startIndex,
37 //                                     Length numberOfCharacters )
38
39
40 //////////////////////////////////////////////////////////
41
42 namespace
43 {
44
45 struct SetGlyphsPerCharacterData
46 {
47   std::string   description;             ///< Description of the test.
48   std::string   text;                    ///< Input text.
49   unsigned int  startIndex;              ///< The start index from where the glyphs per character table is set.
50   unsigned int  numberOfCharacters;      ///< The number of characters to set.
51   unsigned int  totalNumberOfCharacters; ///< The total number of characters.
52   unsigned int* glyphsPerCharacter;      ///< The number of glyphs per character.
53 };
54
55 struct SetCharacterToGlyphData
56 {
57   std::string   description;             ///< Description of the test.
58   std::string   text;                    ///< Input text.
59   unsigned int  startIndex;              ///< The start index from where the character to glyph table is set.
60   unsigned int  numberOfCharacters;      ///< The number of characters to set.
61   unsigned int  totalNumberOfCharacters; ///< The total number of characters.
62   unsigned int* glyphsIndices;           ///< The glyph indices.
63 };
64
65 bool SetGlyphsPerCharacterTest( const SetGlyphsPerCharacterData& data )
66 {
67   // 1) Create the model.
68   LogicalModelPtr logicalModel;
69   VisualModelPtr visualModel;
70   MetricsPtr metrics;
71   Size textArea(100.f, 60.f);
72   Size layoutSize;
73
74   const Vector<FontDescriptionRun> fontDescriptions;
75   const LayoutOptions options;
76   CreateTextModel( data.text,
77                    textArea,
78                    fontDescriptions,
79                    options,
80                    layoutSize,
81                    logicalModel,
82                    visualModel,
83                    metrics );
84
85   Vector<GlyphIndex>& charactersToGlyph = visualModel->mCharactersToGlyph;
86   Vector<Length>& glyphsPerCharacter = visualModel->mGlyphsPerCharacter;
87
88   // 2) Clear the model.
89
90   GlyphIndex startGlyphIndex = 0u;
91   if( 0u != charactersToGlyph.Count() )
92   {
93     // The number of glyphs to be removed.
94     const Length numberOfGlyphs = charactersToGlyph[data.startIndex + data.numberOfCharacters - 1u] + glyphsPerCharacter[data.startIndex + data.numberOfCharacters - 1u] - charactersToGlyph[data.startIndex];
95     startGlyphIndex = charactersToGlyph[data.startIndex];
96
97     charactersToGlyph.Erase( charactersToGlyph.Begin() + data.startIndex,
98                              charactersToGlyph.Begin() + data.startIndex + data.numberOfCharacters );
99     glyphsPerCharacter.Erase( glyphsPerCharacter.Begin() + data.startIndex,
100                               glyphsPerCharacter.Begin() + data.startIndex + data.numberOfCharacters );
101
102     // Update the character to glyph indices.
103     for( Vector<GlyphIndex>::Iterator it = charactersToGlyph.Begin() + data.startIndex,
104            endIt = charactersToGlyph.End();
105          it != endIt;
106          ++it )
107     {
108       *it -= numberOfGlyphs;
109     }
110   }
111
112   // 3) Call the CreateGlyphsPerCharacterTable() function
113   visualModel->CreateGlyphsPerCharacterTable( data.startIndex,
114                                               startGlyphIndex,
115                                               data.numberOfCharacters );
116
117   // 4) Compare the results.
118   if( data.totalNumberOfCharacters != glyphsPerCharacter.Count() )
119   {
120     std::cout << "  Different number of characters : " << glyphsPerCharacter.Count() << ", expected : " << data.totalNumberOfCharacters << std::endl;
121     return false;
122   }
123
124   for( unsigned int i = 0u; i < data.totalNumberOfCharacters; ++i )
125   {
126     if( data.glyphsPerCharacter[i] != glyphsPerCharacter[i] )
127     {
128       std::cout << "  Different number of glyphs for index " << i << std::endl;
129       for( unsigned int j = 0; j < data.totalNumberOfCharacters; ++j )
130       {
131         std::cout << glyphsPerCharacter[j] << " ";
132       }
133       std::cout << std::endl;
134       std::cout << "  expected" << std::endl;
135       for( unsigned int j = 0; j < data.totalNumberOfCharacters; ++j )
136       {
137         std::cout << data.glyphsPerCharacter[j] << " ";
138       }
139       std::cout << std::endl;
140       return false;
141     }
142   }
143
144   return true;
145 }
146
147 bool SetCharacterToGlyphTest( const SetCharacterToGlyphData& data )
148 {
149   // 1) Create the model.
150   LogicalModelPtr logicalModel;
151   VisualModelPtr visualModel;
152   MetricsPtr metrics;
153   Size textArea(100.f, 60.f);
154   Size layoutSize;
155
156   const Vector<FontDescriptionRun> fontDescriptions;
157   const LayoutOptions options;
158   CreateTextModel( data.text,
159                    textArea,
160                    fontDescriptions,
161                    options,
162                    layoutSize,
163                    logicalModel,
164                    visualModel,
165                    metrics );
166
167   Vector<GlyphIndex>& charactersToGlyph = visualModel->mCharactersToGlyph;
168   Vector<Length>& glyphsPerCharacter = visualModel->mGlyphsPerCharacter;
169
170   // 2) Clear the model.
171
172   GlyphIndex startGlyphIndex = 0u;
173   if( 0u != charactersToGlyph.Count() )
174   {
175     // The number of glyphs to be removed.
176     const Length numberOfGlyphs = charactersToGlyph[data.startIndex + data.numberOfCharacters - 1u] + glyphsPerCharacter[data.startIndex + data.numberOfCharacters - 1u] - charactersToGlyph[data.startIndex];
177     startGlyphIndex = charactersToGlyph[data.startIndex];
178
179     charactersToGlyph.Erase( charactersToGlyph.Begin() + data.startIndex,
180                              charactersToGlyph.Begin() + data.startIndex + data.numberOfCharacters );
181
182     // Update the character to glyph indices.
183     for( Vector<GlyphIndex>::Iterator it = charactersToGlyph.Begin() + data.startIndex,
184            endIt = charactersToGlyph.End();
185          it != endIt;
186          ++it )
187     {
188       *it -= numberOfGlyphs;
189     }
190   }
191
192   // 3) Call the CreateCharacterToGlyphTable() function
193   visualModel->CreateCharacterToGlyphTable( data.startIndex,
194                                             startGlyphIndex,
195                                             data.numberOfCharacters );
196
197   // 4) Compare the results.
198   if( data.totalNumberOfCharacters != charactersToGlyph.Count() )
199   {
200     std::cout << "  Different number of character : " << charactersToGlyph.Count() << ", expected : " << data.totalNumberOfCharacters << std::endl;
201     return false;
202   }
203
204   for( unsigned int i = 0u; i < data.totalNumberOfCharacters; ++i )
205   {
206     if( data.glyphsIndices[i] != charactersToGlyph[i] )
207     {
208       std::cout << "  Different number of character to glyph index " << i << std::endl;
209       for( unsigned int j = 0; j < data.totalNumberOfCharacters; ++j )
210       {
211         std::cout << charactersToGlyph[j] << " ";
212       }
213       std::cout << std::endl;
214       std::cout << "  expected" << std::endl;
215       for( unsigned int j = 0; j < data.totalNumberOfCharacters; ++j )
216       {
217         std::cout << data.glyphsIndices[j] << " ";
218       }
219       std::cout << std::endl;
220       return false;
221     }
222   }
223
224   return true;
225 }
226
227 } // namespace
228
229 //////////////////////////////////////////////////////////
230
231 int UtcDaliSetGlyphsPerCharacter(void)
232 {
233   tet_infoline(" UtcDaliSetGlyphsPerCharacter");
234
235   Length glyphsPerCharacter02[] = { 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u };
236   Length glyphsPerCharacter03[] = { 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 0u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u };
237   Length glyphsPerCharacter04[] = { 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 0u, 1u,
238                                     1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
239                                     1u, 1u, 1u, 1u, 0u, 1u, 0u, 2u, 1u, 0u,
240                                     2u, 0u, 2u, 0u, 2u, 1u, 1u, 0u, 0u, 0u,
241                                     2u, 1u, 1u, 1u, 1u, 1u, 0u, 0u, 2u, 1u,
242                                     0u, 2u, 1u, 1u };
243
244   struct SetGlyphsPerCharacterData data[] =
245   {
246     {
247       "Zero characters text",
248       "",
249       0u,
250       0u,
251       0u,
252       NULL
253     },
254     {
255       "Simple 1 to 1 text",
256       "Hello world",
257       0u,
258       11u,
259       11u,
260       glyphsPerCharacter02,
261     },
262     {
263       "Text with different number of glyphs and characters.",
264       "Hello different world",
265       0u,
266       21u,
267       21u,
268       glyphsPerCharacter03,
269     },
270     {
271       "Text paragraphs with different number of glyphs and characters. Update initial paragraphs.",
272       "Hello different world\nनमस्ते दुनिया\nမင်္ဂလာပါကမ္ဘာလောက",
273       0u,
274       22u,
275       54u,
276       glyphsPerCharacter04,
277     },
278     {
279       "Text paragraphs with different number of glyphs and characters. Update mid paragraphs.",
280       "Hello different world\nनमस्ते दुनिया\nမင်္ဂလာပါကမ္ဘာလောက",
281       22u,
282       14u,
283       54u,
284       glyphsPerCharacter04,
285     },
286     {
287       "Text paragraphs with different number of glyphs and characters. Update final paragraphs.",
288       "Hello different world\nनमस्ते दुनिया\nမင်္ဂလာပါကမ္ဘာလောက",
289       36u,
290       18u,
291       54u,
292       glyphsPerCharacter04,
293     },
294   };
295   const unsigned int numberOfTests = 6u;
296
297   for( unsigned int index = 0u; index < numberOfTests; ++index )
298   {
299     ToolkitTestApplication application;
300     if( !SetGlyphsPerCharacterTest( data[index] ) )
301     {
302       tet_result(TET_FAIL);
303     }
304   }
305
306   tet_result(TET_PASS);
307   END_TEST;
308 }
309
310 int UtcDaliSetCharacterToGlyph(void)
311 {
312   tet_infoline(" UtcDaliSetGlyphsPerCharacter");
313
314   GlyphIndex glyphIndices02[] = { 0u, 1u, 2u, 3u, 4u, 5u, 6u, 7u, 8u, 9u, 10u };
315   GlyphIndex glyphIndices03[] = { 0u, 1u, 2u, 3u, 4u, 5u, 6u, 7u, 8u, 8u, 9u, 10u, 11u, 12u, 13u, 14u, 15u, 16u, 17u, 18u, 19u };
316   GlyphIndex glyphIndices04[] = { 0u, 1u, 2u, 3u, 4u, 5u, 6u, 7u, 8u, 8u, 9u, 10u, 11u, 12u, 13u, 14u, 15u, 16u, 17u, 18u, 19u, 20u,
317                                   21u, 22u, 23u, 23u, 24u, 24u, 26u, 27u, 27u, 29u, 29u, 31u, 31u, 33u,
318                                   34u, 35u, 35u, 35u, 35u, 37u, 38u, 39u, 40u, 41u, 42u, 42u, 42u, 44u, 45u, 45u, 47u, 48u };
319
320   struct SetCharacterToGlyphData data[] =
321   {
322     {
323       "Zero characters text",
324       "",
325       0u,
326       0u,
327       0u,
328       NULL
329     },
330     {
331       "Simple 1 to 1 text",
332       "Hello world",
333       0u,
334       11u,
335       11u,
336       glyphIndices02,
337     },
338     {
339       "Text with different number of glyphs and characters.",
340       "Hello different world",
341       0u,
342       21u,
343       21u,
344       glyphIndices03,
345     },
346     {
347       "Text paragraphs with different number of glyphs and characters. Update initial paragraphs.",
348       "Hello different world\nनमस्ते दुनिया\nမင်္ဂလာပါကမ္ဘာလောက",
349       0u,
350       22u,
351       54u,
352       glyphIndices04,
353     },
354     {
355       "Text paragraphs with different number of glyphs and characters. Update mid paragraphs.",
356       "Hello different world\nनमस्ते दुनिया\nမင်္ဂလာပါကမ္ဘာလောက",
357       22u,
358       14u,
359       54u,
360       glyphIndices04,
361     },
362     {
363       "Text paragraphs with different number of glyphs and characters. Update final paragraphs.",
364       "Hello different world\nनमस्ते दुनिया\nမင်္ဂလာပါကမ္ဘာလောက",
365       36u,
366       18u,
367       54u,
368       glyphIndices04,
369     },
370   };
371
372   const unsigned int numberOfTests = 6u;
373
374   for( unsigned int index = 0u; index < numberOfTests; ++index )
375   {
376     ToolkitTestApplication application;
377     if( !SetCharacterToGlyphTest( data[index] ) )
378     {
379       tet_result(TET_FAIL);
380     }
381   }
382
383   tet_result(TET_PASS);
384   END_TEST;
385 }