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