[dali_2.3.20] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-internal / utc-Dali-BoundedParagraph-Functions.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 <iostream>
20
21 #include <dali-toolkit-test-suite-utils.h>
22 #include <dali-toolkit/dali-toolkit.h>
23
24 // INTERNAL INCLUDES
25 #include <dali-toolkit/internal/text/bounded-paragraph-helper-functions.h>
26 #include <dali-toolkit/internal/text/bounded-paragraph-run.h>
27 #include <dali-toolkit/internal/text/character-set-conversion.h>
28
29 using namespace Dali;
30 using namespace Toolkit;
31 using namespace Text;
32
33 struct BoundedParagraphData
34 {
35   CharacterIndex characterIndex;
36   Length         numberOfCharacters;
37 };
38
39 struct TestCaseData
40 {
41   std::string           description;                       ///< Description of the test.
42   std::string           text;                              ///< The text.
43   CharacterIndex        firstIndexOfRemovedCharacters;     ///< The first index of removed characters.
44   int                   numberOfRemovedCharacters;         ///< The number of removed characters.
45   Length                numberOfBoundedParagraphs;         ///< The number of bounded paragraphs before merging.
46   BoundedParagraphData* boundedParagraphs;                 ///< The bounded paragraphs info before merging.
47   Length                numberOfExpectedBoundedParagraphs; ///< The number of expected bounded paragraphs after merging.
48   BoundedParagraphData* expectedBoundedParagraphs;         ///< The expected bounded paragraphs info after merging.
49 };
50
51 void CreateBoundedParagraphRunsFromBoundedParagraphData(
52   Vector<BoundedParagraphRun>& boundedParagraphRuns,
53   const BoundedParagraphData*  boundedParagraphs,
54   const Length&                numberOfBoundedParagraphs)
55 {
56   boundedParagraphRuns.Clear();
57
58   if(boundedParagraphs != nullptr)
59   {
60     for(Length index = 0u; index < numberOfBoundedParagraphs; index++)
61     {
62       BoundedParagraphRun boundedParagraphRun;
63       boundedParagraphRun.characterRun.characterIndex     = boundedParagraphs[index].characterIndex;
64       boundedParagraphRun.characterRun.numberOfCharacters = boundedParagraphs[index].numberOfCharacters;
65
66       boundedParagraphRuns.PushBack(boundedParagraphRun);
67     }
68   }
69 }
70
71 bool MergeBoundedParagraphRunsTest(TestCaseData testCase)
72 {
73   // 1) Convert boundedParagraphs to vector of BoundedParagraphRun
74   Vector<BoundedParagraphRun> boundedParagraphRuns;
75   CreateBoundedParagraphRunsFromBoundedParagraphData(boundedParagraphRuns, testCase.boundedParagraphs, testCase.numberOfBoundedParagraphs);
76
77   // 2) Convert expectedBoundedParagraphs to vector of BoundedParagraphRun
78   Vector<BoundedParagraphRun> expectedBoundedParagraphRuns;
79   CreateBoundedParagraphRunsFromBoundedParagraphData(expectedBoundedParagraphRuns, testCase.expectedBoundedParagraphs, testCase.numberOfExpectedBoundedParagraphs);
80
81   // 3) Convert string text to vector of character utf32
82   Vector<Character> utf32Text;
83   utf32Text.Resize(testCase.text.size());
84   const uint32_t numberOfCharacters = (testCase.text.size() == 0) ? 0 : Utf8ToUtf32(reinterpret_cast<const uint8_t* const>(testCase.text.c_str()), testCase.text.size(), &utf32Text[0u]);
85   utf32Text.Resize(numberOfCharacters);
86
87   // 4) Call MergeBoundedParagraphRunsWhenRemoveCharacters
88   MergeBoundedParagraphRunsWhenRemoveCharacters(utf32Text, testCase.firstIndexOfRemovedCharacters, testCase.numberOfRemovedCharacters, boundedParagraphRuns);
89
90   // 5) Verify actual with expected
91   if(testCase.numberOfExpectedBoundedParagraphs != boundedParagraphRuns.Count())
92   {
93     std::cout << "  Different number of bounded paragraph runs after merging: " << boundedParagraphRuns.Count() << ", expected : " << testCase.numberOfExpectedBoundedParagraphs << std::endl;
94     return false;
95   }
96
97   for(unsigned int index = 0u; index < testCase.numberOfExpectedBoundedParagraphs; ++index)
98   {
99     if(expectedBoundedParagraphRuns[index].characterRun.characterIndex != boundedParagraphRuns[index].characterRun.characterIndex)
100     {
101       std::cout << "  Different bounded paragraph runs after merging, index : " << index << std::endl;
102       std::cout << "  Different characterIndex, actual: " << boundedParagraphRuns[index].characterRun.characterIndex
103                 << ", expected : " << expectedBoundedParagraphRuns[index].characterRun.characterIndex << std::endl;
104       return false;
105     }
106
107     if(expectedBoundedParagraphRuns[index].characterRun.numberOfCharacters != boundedParagraphRuns[index].characterRun.numberOfCharacters)
108     {
109       std::cout << "  Different bounded paragraph runs after merging, index : " << index << std::endl;
110       std::cout << "  Different numberOfCharacters, actual: " << boundedParagraphRuns[index].characterRun.numberOfCharacters
111                 << ", expected : " << expectedBoundedParagraphRuns[index].characterRun.numberOfCharacters << std::endl;
112       return false;
113     }
114   }
115
116   return true;
117 }
118
119 int UtcDaliMergeBoundedParagraphRunsWhenRemoveCharacters(void)
120 {
121   tet_infoline(" UtcDaliMergeBoundedParagraphRunsWhenRemoveCharacters ");
122
123   BoundedParagraphData boundedParagraphs01[]         = {{10u, 14u}, {37u, 15u}};
124   BoundedParagraphData expectedBoundedParagraphs01[] = {{10u, 42u}};
125
126   BoundedParagraphData boundedParagraphs02[]         = {{10u, 14u}, {37u, 15u}};
127   BoundedParagraphData expectedBoundedParagraphs02[] = {{37u, 15u}};
128
129   BoundedParagraphData boundedParagraphs03[]         = {{10u, 14u}, {37u, 15u}};
130   BoundedParagraphData expectedBoundedParagraphs03[] = {{10u, 14u}, {37u, 20u}};
131
132   BoundedParagraphData boundedParagraphs04[]         = {{10u, 14u}, {37u, 15u}};
133   BoundedParagraphData expectedBoundedParagraphs04[] = {{10u, 14u}, {37u, 15u}};
134
135   BoundedParagraphData boundedParagraphs05[]         = {{10u, 14u}, {37u, 15u}};
136   BoundedParagraphData expectedBoundedParagraphs05[] = {{10u, 14u}, {37u, 15u}};
137
138   BoundedParagraphData boundedParagraphs06[]         = {{10u, 14u}, {37u, 15u}, {64u, 14u}};
139   BoundedParagraphData expectedBoundedParagraphs06[] = {{10u, 68u}};
140
141   TestCaseData testCases[] =
142     {
143       {
144
145         "test-case 01",
146         "text one \nParagraph two\n text three \nParagraph four\n text five",
147         20u,
148         -26,
149         2u,
150         boundedParagraphs01,
151         1u,
152         expectedBoundedParagraphs01
153
154       },
155
156       {
157
158         "test-case 02",
159         "text one \nParagraph two\n text three \nParagraph four\n text five",
160         5u,
161         -5,
162         2u,
163         boundedParagraphs02,
164         1u,
165         expectedBoundedParagraphs02
166
167       },
168
169       {
170
171         "test-case 03",
172         "text one \nParagraph two\n text three \nParagraph four\n text five",
173         47u,
174         -10,
175         2u,
176         boundedParagraphs03,
177         2u,
178         expectedBoundedParagraphs03
179
180       },
181
182       {
183
184         "test-case 04",
185         "text one \nParagraph two\n text three \nParagraph four\n text five",
186         10u,
187         -9,
188         2u,
189         boundedParagraphs04,
190         2u,
191         expectedBoundedParagraphs04
192
193       },
194
195       {
196
197         "test-case 05",
198         "text one \nParagraph two\n text three \nParagraph four\n text five",
199         25u,
200         -4,
201         2u,
202         boundedParagraphs05,
203         2u,
204         expectedBoundedParagraphs05
205
206       },
207
208       {
209
210         "test-case 06",
211         "text one \nParagraph two\n text three \nParagraph four\n text five \nParagraph six\n text seven",
212         10u,
213         -63,
214         3u,
215         boundedParagraphs06,
216         1u,
217         expectedBoundedParagraphs06
218
219       },
220
221     };
222
223   const unsigned int numberOfTests = 6u;
224
225   for(unsigned int index = 0u; index < numberOfTests; ++index)
226   {
227     ToolkitTestApplication application;
228     tet_infoline(testCases[index].description.c_str());
229
230     if(!MergeBoundedParagraphRunsTest(testCases[index]))
231     {
232       tet_result(TET_FAIL);
233     }
234   }
235
236   tet_result(TET_PASS);
237   END_TEST;
238 }