TextModel - Layout a given range of characters inside a text.
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-internal / utc-Dali-LogicalModel.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 SetVisualToLogicalMap( const BidirectionalLineInfoRun* const bidirectionalInfo,
34 //                             Length numberOfRuns,
35 //                             CharacterIndex startIndex  )
36
37
38 //////////////////////////////////////////////////////////
39
40 namespace
41 {
42
43 struct SetVisualToLogicalMapData
44 {
45   std::string   description;                ///< Description of the test.
46   std::string   text;                       ///< Input text.
47   unsigned int  startIndex;                 ///< The start index from where the visual to logical conversion table is set.
48   unsigned int  numberOfCharacters;         ///< The number of characters to set.
49   Size          textArea;                   ///< The size of the area where the text is laid-out.
50   unsigned int  expectedNumberOfCharacters; ///< The expected number of characters.
51   unsigned int* visualToLogical;            ///< The expected visual to logical conversion table.
52 };
53
54 bool SetVisualToLogicalMapTest( const SetVisualToLogicalMapData& data )
55 {
56   std::cout << "  testing : " << data.description << std::endl;
57   // Create the model.
58   LogicalModelPtr logicalModel = LogicalModel::New();
59   VisualModelPtr visualModel = VisualModel::New();
60   Size layoutSize;
61
62   // Create the model with the whole text.
63   const Vector<FontDescriptionRun> fontDescriptions;
64   const LayoutOptions options;
65   CreateTextModel( data.text,
66                    data.textArea,
67                    fontDescriptions,
68                    options,
69                    layoutSize,
70                    logicalModel,
71                    visualModel );
72
73   // Get the visual to logical map.
74   Vector<CharacterIndex>& visualToLogicalMap = logicalModel->mVisualToLogicalMap;
75
76   // Compare the results.
77   if( data.expectedNumberOfCharacters != visualToLogicalMap.Count() )
78   {
79     std::cout << "  differetn number of characters : " << visualToLogicalMap.Count() << ", expected : " << data.expectedNumberOfCharacters << std::endl;
80     return false;
81   }
82
83   for( unsigned int index = 0u; index < data.expectedNumberOfCharacters; ++index )
84   {
85     if( data.visualToLogical[index] != visualToLogicalMap[index] )
86     {
87       std::cout << "  different visualToLogical table : " << std::endl;
88       for( unsigned int i = 0; i < data.expectedNumberOfCharacters; ++i )
89       {
90         std::cout << visualToLogicalMap[i] << " ";
91       }
92       std::cout << std::endl;
93       std::cout << "                         expected : " << std::endl;
94       for( unsigned int i = 0; i < data.expectedNumberOfCharacters; ++i )
95       {
96         std::cout << data.visualToLogical[i] << " ";
97       }
98       std::cout << std::endl;
99       return false;
100     }
101   }
102
103   return true;
104 }
105
106 } // namespace
107
108 //////////////////////////////////////////////////////////
109
110 int UtcDaliSetVisualToLogicalMap(void)
111 {
112   ToolkitTestApplication application;
113   tet_infoline(" UtcDaliSetVisualToLogicalMap");
114
115   unsigned int* visualToLogical01 = NULL;
116   unsigned int* visualToLogical02 = NULL;
117   unsigned int  visualToLogical03[] = { 12u, 11u, 10u, 9u, 8u, 7u, 6u, 5u, 4u, 3u, 2u, 1u, 0u };
118   unsigned int  visualToLogical04[] = { 0u, 1u, 2u, 3u, 4u, 5u, 6u, 7u, 8u, 9u, 10u, 11u, 12u, 25u, 24u, 23u, 22u, 21u, 20u, 19u, 18u, 17u, 16u, 15u, 14u, 13u, };
119   unsigned int  visualToLogical05[] = { 0u, 1u, 2u, 3u, 4u, 5u, 6u, 7u, 8u, 9u, 10u, 11u, 12u, 13u, 26u, 25u, 24u, 23u, 22u, 21u, 20u, 19u, 18u, 17u, 16u, 15u, 14u };
120   unsigned int  visualToLogical06[] = { 0u, 1u, 2u, 3u, 4u, 5u, 6u, 7u, 8u, 9u, 10u, 11u, 12u, 25u, 24u, 23u, 22u, 21u, 20u, 19u, 18u, 17u, 16u, 15u, 14u, 13u, 26u, 27u, 28u, 29u, 30u, 31u, 32u, 33u, 34u, 35u, 36u, 37u, 38u, 39u, 54u, 53u, 52u, 51u, 50u, 49u, 48u, 47u, 46u, 45u, 44u, 43u, 42u, 41u, 40u, 67u, 66u, 55u, 56u, 57u, 58u, 59u, 60u, 61u, 62u, 63u, 64u, 65u, 81u, 80u, 79u, 78u, 77u, 76u, 75u, 74u, 73u, 72u, 71u, 70u, 69u, 68u, 95u, 94u, 93u, 92u, 91u, 90u, 89u, 88u, 87u, 86u, 85u, 84u, 83u, 82u, 96u, 97u, 98u, 99u, 100u, 101u, 102u, 103u, 104u, 105u, 106u };
121   unsigned int  visualToLogical07[] = { 0u, 1u, 2u, 3u, 4u, 5u, 6u, 7u, 8u, 9u, 10u, 11u, 12u, 25u, 24u, 23u, 22u, 21u, 20u, 19u, 18u, 17u, 16u, 15u, 14u, 13u, 26u, 27u, 28u, 29u, 30u, 31u, 32u, 33u, 34u, 35u, 36u, 37u, 38u, 39u, 54u, 53u, 52u, 51u, 50u, 49u, 48u, 47u, 46u, 45u, 44u, 43u, 42u, 41u, 40u, 67u, 66u, 55u, 56u, 57u, 58u, 59u, 60u, 61u, 62u, 63u, 64u, 65u, 81u, 80u, 79u, 78u, 77u, 76u, 75u, 74u, 73u, 72u, 71u, 70u, 69u, 68u, 95u, 94u, 93u, 92u, 91u, 90u, 89u, 88u, 87u, 86u, 85u, 84u, 83u, 82u, 96u, 97u, 98u, 99u, 100u, 101u, 102u, 103u, 104u, 105u, 106u };
122   unsigned int  visualToLogical08[] = { 0u, 1u, 2u, 3u, 4u, 5u, 6u, 7u, 8u, 9u, 10u, 11u, 12u, 25u, 24u, 23u, 22u, 21u, 20u, 19u, 18u, 17u, 16u, 15u, 14u, 13u, 26u, 27u, 28u, 29u, 30u, 31u, 32u, 33u, 34u, 35u, 36u, 37u, 38u, 39u, 54u, 53u, 52u, 51u, 50u, 49u, 48u, 47u, 46u, 45u, 44u, 43u, 42u, 41u, 40u, 67u, 66u, 55u, 56u, 57u, 58u, 59u, 60u, 61u, 62u, 63u, 64u, 65u, 81u, 80u, 79u, 78u, 77u, 76u, 75u, 74u, 73u, 72u, 71u, 70u, 69u, 68u, 95u, 94u, 93u, 92u, 91u, 90u, 89u, 88u, 87u, 86u, 85u, 84u, 83u, 82u, 96u, 97u, 98u, 99u, 100u, 101u, 102u, 103u, 104u, 105u, 106u };
123   unsigned int  visualToLogical09[] = { 0u, 1u, 2u, 3u, 4u, 5u, 6u, 7u, 8u, 9u, 10u, 11u, 12u, 25u, 24u, 23u, 22u, 21u, 20u, 19u, 18u, 17u, 16u, 15u, 14u, 13u, 26u, 27u, 28u, 29u, 30u, 31u, 32u, 33u, 34u, 35u, 36u, 37u, 38u, 39u, 54u, 53u, 52u, 51u, 50u, 49u, 48u, 47u, 46u, 45u, 44u, 43u, 42u, 41u, 40u, 67u, 66u, 55u, 56u, 57u, 58u, 59u, 60u, 61u, 62u, 63u, 64u, 65u, 81u, 80u, 79u, 78u, 77u, 76u, 75u, 74u, 73u, 72u, 71u, 70u, 69u, 68u, 95u, 94u, 93u, 92u, 91u, 90u, 89u, 88u, 87u, 86u, 85u, 84u, 83u, 82u, 96u, 97u, 98u, 99u, 100u, 101u, 102u, 103u, 104u, 105u, 106u };
124
125   struct SetVisualToLogicalMapData data[] =
126   {
127     {
128       "Zero characters text",
129       "",
130       0u,
131       0u,
132       Size( 100.f, 300.f ),
133       0u,
134       visualToLogical01
135     },
136     {
137       "Left to right text only",
138       "Hello world",
139       0u,
140       11u,
141       Size( 100.f, 300.f ),
142       0u,
143       visualToLogical02
144     },
145     {
146       "Right to left text only",
147       "مرحبا بالعالم",
148       0u,
149       13u,
150       Size( 100.f, 300.f ),
151       13u,
152       visualToLogical03
153     },
154     {
155       "Mix of left to right and right to left text.",
156       "Hello world, مرحبا بالعالم",
157       0u,
158       26u,
159       Size( 100.f, 300.f ),
160       26u,
161       visualToLogical04
162     },
163     {
164       "Mix of left to right and right to left text.",
165       "Hello world, \nمرحبا بالعالم",
166       0u,
167       27u,
168       Size( 100.f, 300.f ),
169       27u,
170       visualToLogical05
171     },
172     {
173       "Mix of left to right and right to left text.",
174       "Hello world, مرحبا بالعالم, hello world\nمرحبا بالعالم, hello world, مرحبا بالعالم\nمرحبا بالعالم\nhello world",
175       0u,
176       107u,
177       Size( 100.f, 300.f ),
178       107u,
179       visualToLogical06
180     },
181     {
182       "Mix of left to right and right to left text. Updates from character index 5",
183       "Hello world, مرحبا بالعالم, hello world\nمرحبا بالعالم, hello world, مرحبا بالعالم\nمرحبا بالعالم\nhello world",
184       5u,
185       107u,
186       Size( 100.f, 300.f ),
187       107u,
188       visualToLogical07
189     },
190     {
191       "Mix of left to right and right to left text. Updates from character index 39",
192       "Hello world, مرحبا بالعالم, hello world\nمرحبا بالعالم, hello world, مرحبا بالعالم\nمرحبا بالعالم\nhello world",
193       39u,
194       107u,
195       Size( 100.f, 300.f ),
196       107u,
197       visualToLogical08
198     },
199     {
200       "Mix of left to right and right to left text. Updates from character index 70",
201       "Hello world, مرحبا بالعالم, hello world\nمرحبا بالعالم, hello world, مرحبا بالعالم\nمرحبا بالعالم\nhello world",
202       70u,
203       107u,
204       Size( 100.f, 300.f ),
205       107u,
206       visualToLogical09
207     }
208   };
209   const unsigned int numberOfTests = 9u;
210
211   for( unsigned int index = 0u; index < numberOfTests; ++index )
212   {
213     if( !SetVisualToLogicalMapTest( data[index] ) )
214     {
215       tet_result(TET_FAIL);
216     }
217   }
218
219   tet_result(TET_PASS);
220   END_TEST;
221 }