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