2 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <dali-toolkit-test-suite-utils.h>
22 #include <dali-toolkit/dali-toolkit.h>
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>
30 using namespace Toolkit;
33 struct BoundedParagraphData
35 CharacterIndex characterIndex;
36 Length numberOfCharacters;
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.
51 void CreateBoundedParagraphRunsFromBoundedParagraphData(
52 Vector<BoundedParagraphRun>& boundedParagraphRuns,
53 const BoundedParagraphData* boundedParagraphs,
54 const Length& numberOfBoundedParagraphs)
56 boundedParagraphRuns.Clear();
58 if(boundedParagraphs != nullptr)
60 for(Length index = 0u; index < numberOfBoundedParagraphs; index++)
62 BoundedParagraphRun boundedParagraphRun;
63 boundedParagraphRun.characterRun.characterIndex = boundedParagraphs[index].characterIndex;
64 boundedParagraphRun.characterRun.numberOfCharacters = boundedParagraphs[index].numberOfCharacters;
66 boundedParagraphRuns.PushBack(boundedParagraphRun);
71 bool MergeBoundedParagraphRunsTest(TestCaseData testCase)
73 // 1) Convert boundedParagraphs to vector of BoundedParagraphRun
74 Vector<BoundedParagraphRun> boundedParagraphRuns;
75 CreateBoundedParagraphRunsFromBoundedParagraphData(boundedParagraphRuns, testCase.boundedParagraphs, testCase.numberOfBoundedParagraphs);
77 // 2) Convert expectedBoundedParagraphs to vector of BoundedParagraphRun
78 Vector<BoundedParagraphRun> expectedBoundedParagraphRuns;
79 CreateBoundedParagraphRunsFromBoundedParagraphData(expectedBoundedParagraphRuns, testCase.expectedBoundedParagraphs, testCase.numberOfExpectedBoundedParagraphs);
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);
87 // 4) Call MergeBoundedParagraphRunsWhenRemoveCharacters
88 MergeBoundedParagraphRunsWhenRemoveCharacters(utf32Text, testCase.firstIndexOfRemovedCharacters, testCase.numberOfRemovedCharacters, boundedParagraphRuns);
90 // 5) Verify actual with expected
91 if(testCase.numberOfExpectedBoundedParagraphs != boundedParagraphRuns.Count())
93 std::cout << " Different number of bounded paragraph runs after merging: " << boundedParagraphRuns.Count() << ", expected : " << testCase.numberOfExpectedBoundedParagraphs << std::endl;
97 for(unsigned int index = 0u; index < testCase.numberOfExpectedBoundedParagraphs; ++index)
99 if(expectedBoundedParagraphRuns[index].characterRun.characterIndex != boundedParagraphRuns[index].characterRun.characterIndex)
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;
107 if(expectedBoundedParagraphRuns[index].characterRun.numberOfCharacters != boundedParagraphRuns[index].characterRun.numberOfCharacters)
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;
119 int UtcDaliMergeBoundedParagraphRunsWhenRemoveCharacters(void)
121 tet_infoline(" UtcDaliMergeBoundedParagraphRunsWhenRemoveCharacters ");
123 BoundedParagraphData boundedParagraphs01[] = {{10u, 14u}, {37u, 15u}};
124 BoundedParagraphData expectedBoundedParagraphs01[] = {{10u, 42u}};
126 BoundedParagraphData boundedParagraphs02[] = {{10u, 14u}, {37u, 15u}};
127 BoundedParagraphData expectedBoundedParagraphs02[] = {{37u, 15u}};
129 BoundedParagraphData boundedParagraphs03[] = {{10u, 14u}, {37u, 15u}};
130 BoundedParagraphData expectedBoundedParagraphs03[] = {{10u, 14u}, {37u, 20u}};
132 BoundedParagraphData boundedParagraphs04[] = {{10u, 14u}, {37u, 15u}};
133 BoundedParagraphData expectedBoundedParagraphs04[] = {{10u, 14u}, {37u, 15u}};
135 BoundedParagraphData boundedParagraphs05[] = {{10u, 14u}, {37u, 15u}};
136 BoundedParagraphData expectedBoundedParagraphs05[] = {{10u, 14u}, {37u, 15u}};
138 BoundedParagraphData boundedParagraphs06[] = {{10u, 14u}, {37u, 15u}, {64u, 14u}};
139 BoundedParagraphData expectedBoundedParagraphs06[] = {{10u, 68u}};
141 TestCaseData testCases[] =
146 "text one \nParagraph two\n text three \nParagraph four\n text five",
152 expectedBoundedParagraphs01
159 "text one \nParagraph two\n text three \nParagraph four\n text five",
165 expectedBoundedParagraphs02
172 "text one \nParagraph two\n text three \nParagraph four\n text five",
178 expectedBoundedParagraphs03
185 "text one \nParagraph two\n text three \nParagraph four\n text five",
191 expectedBoundedParagraphs04
198 "text one \nParagraph two\n text three \nParagraph four\n text five",
204 expectedBoundedParagraphs05
211 "text one \nParagraph two\n text three \nParagraph four\n text five \nParagraph six\n text seven",
217 expectedBoundedParagraphs06
223 const unsigned int numberOfTests = 6u;
225 for(unsigned int index = 0u; index < numberOfTests; ++index)
227 ToolkitTestApplication application;
228 tet_infoline(testCases[index].description.c_str());
230 if(!MergeBoundedParagraphRunsTest(testCases[index]))
232 tet_result(TET_FAIL);
236 tet_result(TET_PASS);