Merge "fix linespacing calculation in TextLabel" into devel/master
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-internal / utc-Dali-Text-Characters.cpp
1 /*
2  * Copyright (c) 2022 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 <stdlib.h>
19 #include <unistd.h>
20 #include <iostream>
21
22 #include <dali-toolkit-test-suite-utils.h>
23 #include <dali-toolkit/dali-toolkit.h>
24 #include <dali-toolkit/internal/text/characters-helper-functions.h>
25 #include <toolkit-text-utils.h>
26
27 using namespace Dali;
28 using namespace Toolkit;
29 using namespace Text;
30
31 // Tests the following function.
32 //
33 //  CharacterRun RetrieveClusteredCharactersOfCharacterIndex(const VisualModelPtr&  visualModel,
34 //                                                          const LogicalModelPtr& logicalModel,
35 //                                                          const CharacterIndex&  characterIndex)
36
37 //////////////////////////////////////////////////////////
38
39 namespace
40 {
41 const std::string  DEFAULT_FONT_DIR("/resources/fonts");
42 const unsigned int DEFAULT_FONT_SIZE = 1152u;
43
44 struct RetrieveClusteredCharactersOfCharacterIndexData
45 {
46   std::string     description;            ///< Description of the test.
47   std::string     text;                   ///< Input text.
48   bool            markupProcessorEnabled; //< Enable markup processor to use markup text.
49   unsigned int    numberOfTests;          ///< The number of tests.
50   CharacterIndex* characterIndex;         ///< The character index for each test.
51   CharacterRun*   clusteredCharacters;    ///< The expected clustered characters run for each test.
52 };
53
54 bool GetRetrieveClusteredCharactersOfCharacterIndexTest(const RetrieveClusteredCharactersOfCharacterIndexData& data)
55 {
56   std::cout << "  testing : " << data.description << std::endl;
57
58   // 1) Create the model.
59   ModelPtr   textModel;
60   MetricsPtr metrics;
61   Size       textArea(400.f, 600.f);
62   Size       layoutSize;
63
64   Vector<FontDescriptionRun> fontDescriptionRuns;
65   LayoutOptions              options;
66   CreateTextModel(data.text,
67                   textArea,
68                   fontDescriptionRuns,
69                   options,
70                   layoutSize,
71                   textModel,
72                   metrics,
73                   data.markupProcessorEnabled,
74                   LineWrap::WORD,
75                   false,
76                   Toolkit::DevelText::EllipsisPosition::END,
77                   0.0f, // lineSpacing
78                   0.0f  // characterSpacing
79   );
80
81   LogicalModelPtr logicalModel = textModel->mLogicalModel;
82   VisualModelPtr  visualModel  = textModel->mVisualModel;
83
84   for(unsigned int index = 0; index < data.numberOfTests; ++index)
85   {
86     CharacterRun clusteredCharacters = RetrieveClusteredCharactersOfCharacterIndex(visualModel, logicalModel, data.characterIndex[index]);
87
88     if(clusteredCharacters.characterIndex != data.clusteredCharacters[index].characterIndex)
89     {
90       std::cout << "  test " << index << " failed. Different clusteredCharacters.characterIndex : " << clusteredCharacters.characterIndex << ", expected : " << data.clusteredCharacters[index].characterIndex << std::endl;
91       return false;
92     }
93
94     if(clusteredCharacters.numberOfCharacters != data.clusteredCharacters[index].numberOfCharacters)
95     {
96       std::cout << "  test " << index << " failed. Different clusteredCharacters.numberOfCharacters : " << clusteredCharacters.numberOfCharacters << ", expected : " << data.clusteredCharacters[index].numberOfCharacters << std::endl;
97       return false;
98     }
99   }
100
101   return true;
102 }
103
104 } // namespace
105
106 //////////////////////////////////////////////////////////
107 //
108 // UtcDaliRetrieveClusteredCharactersOfCharacterIndex
109 //
110 //////////////////////////////////////////////////////////
111
112 int UtcDaliRetrieveClusteredCharactersOfCharacterIndex(void)
113 {
114   tet_infoline(" UtcDaliRetrieveClusteredCharactersOfCharacterIndex");
115
116   CharacterIndex characterIndex01[] = {0u, 1u, 2u, 10u};
117
118   CharacterRun clusteredCharacters01[] = {
119     {0u, 1u},
120     {1u, 1u},
121     {2u, 1u},
122     {10u, 1u}};
123
124   CharacterIndex characterIndex02[] = {0u, 4u, 6u};
125
126   CharacterRun clusteredCharacters02[] = {
127     {0u, 7u},
128     {0u, 7u},
129     {0u, 7u}};
130
131   CharacterIndex characterIndex03[] = {3u, 9u, 14u};
132
133   CharacterRun clusteredCharacters03[] = {
134     {2u, 7u},
135     {9u, 1u},
136     {11u, 4u}};
137
138   CharacterIndex characterIndex04[] = {0u, 1u, 2u, 10u};
139
140   CharacterRun clusteredCharacters04[] = {
141     {0u, 1u},
142     {1u, 1u},
143     {2u, 1u},
144     {10u, 1u}};
145
146   struct RetrieveClusteredCharactersOfCharacterIndexData data[] =
147     {
148       {"Easy latin script",
149        "Hello world",
150        true,
151        4u,
152        characterIndex01,
153        clusteredCharacters01},
154
155       {"FamilyManWomanGirlBoy Single Complex Emoji script",
156        "&#x1F468;&#x200D;&#x1F469;&#x200D;&#x1F467;&#x200D;&#x1F466;",
157        true,
158        3u,
159        characterIndex02,
160        clusteredCharacters02},
161
162       {"Long text many Emojis with letters",
163        "AB&#x1F468;&#x200D;&#x1F469;&#x200D;&#x1F467;&#x200D;&#x1F466;AB&#x1F469;&#x1F3FB;&#x200D;&#x1F52C;B&#x1F468;&#x200D;&#x1F469;&#x200D;&#x1F467;&#x200D;&#x1F466;AA&#x262a;&#xfe0e;B&#x262a;&#xfe0f;AB",
164        true,
165        3u,
166        characterIndex03,
167        clusteredCharacters03},
168
169       {"Arabic script",
170        "اهلا و سهلا",
171        true,
172        4u,
173        characterIndex04,
174        clusteredCharacters04},
175     };
176   const unsigned int numberOfTests = 4u;
177
178   for(unsigned int index = 0; index < numberOfTests; ++index)
179   {
180     ToolkitTestApplication application;
181     if(!GetRetrieveClusteredCharactersOfCharacterIndexTest(data[index]))
182     {
183       tet_result(TET_FAIL);
184     }
185   }
186
187   tet_result(TET_PASS);
188   END_TEST;
189 }