TextModel - Shape the given number 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   CreateTextModel( data.text,
64                    data.textArea,
65                    layoutSize,
66                    logicalModel,
67                    visualModel );
68
69   // Get the visual to logical map.
70   Vector<CharacterIndex>& visualToLogicalMap = logicalModel->mVisualToLogicalMap;
71
72   // Compare the results.
73   if( data.expectedNumberOfCharacters != visualToLogicalMap.Count() )
74   {
75     std::cout << "  differetn number of characters : " << visualToLogicalMap.Count() << ", expected : " << data.expectedNumberOfCharacters << std::endl;
76     return false;
77   }
78
79   for( unsigned int index = 0u; index < data.expectedNumberOfCharacters; ++index )
80   {
81     if( data.visualToLogical[index] != visualToLogicalMap[index] )
82     {
83       std::cout << "  different visualToLogical table : " << std::endl;
84       for( unsigned int i = 0; i < data.expectedNumberOfCharacters; ++i )
85       {
86         std::cout << visualToLogicalMap[i] << " ";
87       }
88       std::cout << std::endl;
89       std::cout << "                         expected : " << std::endl;
90       for( unsigned int i = 0; i < data.expectedNumberOfCharacters; ++i )
91       {
92         std::cout << data.visualToLogical[i] << " ";
93       }
94       std::cout << std::endl;
95       return false;
96     }
97   }
98
99   return true;
100 }
101
102 } // namespace
103
104 //////////////////////////////////////////////////////////
105
106 int UtcDaliSetVisualToLogicalMap(void)
107 {
108   ToolkitTestApplication application;
109   tet_infoline(" UtcDaliSetVisualToLogicalMap");
110
111   unsigned int* visualToLogical01 = NULL;
112   unsigned int* visualToLogical02 = NULL;
113   unsigned int  visualToLogical03[] = { 12u, 11u, 10u, 9u, 8u, 7u, 6u, 5u, 4u, 3u, 2u, 1u, 0u };
114   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, };
115   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 };
116   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 };
117   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 };
118   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 };
119   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 };
120
121   struct SetVisualToLogicalMapData data[] =
122   {
123     {
124       "Zero characters text",
125       "",
126       0u,
127       0u,
128       Size( 100.f, 300.f ),
129       0u,
130       visualToLogical01
131     },
132     {
133       "Left to right text only",
134       "Hello world",
135       0u,
136       11u,
137       Size( 100.f, 300.f ),
138       0u,
139       visualToLogical02
140     },
141     {
142       "Right to left text only",
143       "مرحبا بالعالم",
144       0u,
145       13u,
146       Size( 100.f, 300.f ),
147       13u,
148       visualToLogical03
149     },
150     {
151       "Mix of left to right and right to left text.",
152       "Hello world, مرحبا بالعالم",
153       0u,
154       26u,
155       Size( 100.f, 300.f ),
156       26u,
157       visualToLogical04
158     },
159     {
160       "Mix of left to right and right to left text.",
161       "Hello world, \nمرحبا بالعالم",
162       0u,
163       27u,
164       Size( 100.f, 300.f ),
165       27u,
166       visualToLogical05
167     },
168     {
169       "Mix of left to right and right to left text.",
170       "Hello world, مرحبا بالعالم, hello world\nمرحبا بالعالم, hello world, مرحبا بالعالم\nمرحبا بالعالم\nhello world",
171       0u,
172       107u,
173       Size( 100.f, 300.f ),
174       107u,
175       visualToLogical06
176     },
177     {
178       "Mix of left to right and right to left text. Updates from character index 5",
179       "Hello world, مرحبا بالعالم, hello world\nمرحبا بالعالم, hello world, مرحبا بالعالم\nمرحبا بالعالم\nhello world",
180       5u,
181       107u,
182       Size( 100.f, 300.f ),
183       107u,
184       visualToLogical07
185     },
186     {
187       "Mix of left to right and right to left text. Updates from character index 39",
188       "Hello world, مرحبا بالعالم, hello world\nمرحبا بالعالم, hello world, مرحبا بالعالم\nمرحبا بالعالم\nhello world",
189       39u,
190       107u,
191       Size( 100.f, 300.f ),
192       107u,
193       visualToLogical08
194     },
195     {
196       "Mix of left to right and right to left text. Updates from character index 70",
197       "Hello world, مرحبا بالعالم, hello world\nمرحبا بالعالم, hello world, مرحبا بالعالم\nمرحبا بالعالم\nhello world",
198       70u,
199       107u,
200       Size( 100.f, 300.f ),
201       107u,
202       visualToLogical09
203     }
204   };
205   const unsigned int numberOfTests = 9u;
206
207   for( unsigned int index = 0u; index < numberOfTests; ++index )
208   {
209     if( !SetVisualToLogicalMapTest( data[index] ) )
210     {
211       tet_result(TET_FAIL);
212     }
213   }
214
215   tet_result(TET_PASS);
216   END_TEST;
217 }