Merge "Refactoring related-runs for the mutable-markup (Spannable)" into devel/master
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-internal / utc-Dali-LineHelperFunctions.cpp
1 /*
2  * Copyright (c) 2020 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/line-helper-functions.h>
25 #include <dali-toolkit/internal/text/rendering/text-typesetter.h>
26 #include <dali-toolkit/internal/text/rendering/view-model.h>
27 #include <dali-toolkit/internal/text/text-controller.h>
28 #include <toolkit-text-utils.h>
29
30 using namespace Dali;
31 using namespace Toolkit;
32 using namespace Text;
33
34 int UtcDaliGetPreOffsetVerticalLineAlignmentWithNegativeLineSpacing(void)
35 {
36   tet_infoline(" UtcDaliGetPreOffsetVerticalLineAlignmentWithNegativeLineSpacing ");
37   ToolkitTestApplication application;
38
39   uint32_t expectedNumberOfLines = 2u;
40
41   // Creates a text controller.
42   ControllerPtr controller = Controller::New();
43
44   // Configures the text controller similarly to the text-label.
45   ConfigureTextLabel(controller);
46
47   // Sets the text.
48   controller->SetMarkupProcessorEnabled(true);
49   controller->SetTextElideEnabled(false);
50   controller->SetText("<p rel-line-height=0.5>Line one Line two</p>");
51
52   // Creates the text's model and relais-out the text.
53   const Size relayoutSize(120.f, 100.f);
54   controller->Relayout(relayoutSize);
55
56   // Tests the rendering controller has been created.
57   TypesetterPtr typesetter = Typesetter::New(controller->GetTextModel());
58   DALI_TEST_CHECK(typesetter);
59
60   // Tests the view model has been created.
61   ViewModel* model = typesetter->GetViewModel();
62   DALI_TEST_CHECK(model);
63
64   application.SendNotification();
65   application.Render();
66
67   DALI_TEST_EQUALS(model->GetNumberOfLines(), expectedNumberOfLines, TEST_LOCATION);
68   DALI_TEST_CHECK(model->GetLines());
69
70   const LineRun& lineOne = *(model->GetLines() + 0u);
71   const LineRun& lineTwo = *(model->GetLines() + 1u);
72
73   DALI_TEST_EQUALS(GetPreOffsetVerticalLineAlignment(lineOne, Dali::Toolkit::DevelText::VerticalLineAlignment::TOP), 0.0f, TEST_LOCATION);
74   DALI_TEST_EQUALS(GetPreOffsetVerticalLineAlignment(lineOne, Dali::Toolkit::DevelText::VerticalLineAlignment::MIDDLE), 0.0f, TEST_LOCATION);
75   DALI_TEST_EQUALS(GetPreOffsetVerticalLineAlignment(lineOne, Dali::Toolkit::DevelText::VerticalLineAlignment::BOTTOM), 0.0f, TEST_LOCATION);
76
77   DALI_TEST_EQUALS(GetPreOffsetVerticalLineAlignment(lineTwo, Dali::Toolkit::DevelText::VerticalLineAlignment::TOP), 0.0f, TEST_LOCATION);
78   DALI_TEST_EQUALS(GetPreOffsetVerticalLineAlignment(lineTwo, Dali::Toolkit::DevelText::VerticalLineAlignment::MIDDLE), 0.0f, TEST_LOCATION);
79   DALI_TEST_EQUALS(GetPreOffsetVerticalLineAlignment(lineTwo, Dali::Toolkit::DevelText::VerticalLineAlignment::BOTTOM), 0.0f, TEST_LOCATION);
80   END_TEST;
81 }
82
83 int UtcDaliGetPreOffsetVerticalLineAlignmentWithPositiveLineSpacing(void)
84 {
85   tet_infoline(" UtcDaliGetPreOffsetVerticalLineAlignmentWithPositiveLineSpacing ");
86   ToolkitTestApplication application;
87
88   uint32_t expectedNumberOfLines = 2u;
89
90   // Creates a text controller.
91   ControllerPtr controller = Controller::New();
92
93   // Configures the text controller similarly to the text-label.
94   ConfigureTextLabel(controller);
95
96   // Sets the text.
97   controller->SetMarkupProcessorEnabled(true);
98   controller->SetTextElideEnabled(false);
99   controller->SetText("<p rel-line-height=2.0>Line one Line two</p>");
100
101   // Creates the text's model and relais-out the text.
102   const Size relayoutSize(120.f, 100.f);
103   controller->Relayout(relayoutSize);
104
105   // Tests the rendering controller has been created.
106   TypesetterPtr typesetter = Typesetter::New(controller->GetTextModel());
107   DALI_TEST_CHECK(typesetter);
108
109   // Tests the view model has been created.
110   ViewModel* model = typesetter->GetViewModel();
111   DALI_TEST_CHECK(model);
112
113   application.SendNotification();
114   application.Render();
115
116   DALI_TEST_EQUALS(model->GetNumberOfLines(), expectedNumberOfLines, TEST_LOCATION);
117   DALI_TEST_CHECK(model->GetLines());
118
119   const LineRun& lineOne = *(model->GetLines() + 0u);
120   const LineRun& lineTwo = *(model->GetLines() + 1u);
121
122   DALI_TEST_EQUALS(GetPreOffsetVerticalLineAlignment(lineOne, Dali::Toolkit::DevelText::VerticalLineAlignment::TOP), 0.0f, TEST_LOCATION);
123   DALI_TEST_EQUALS(GetPreOffsetVerticalLineAlignment(lineOne, Dali::Toolkit::DevelText::VerticalLineAlignment::MIDDLE), 9.5f, TEST_LOCATION);
124   DALI_TEST_EQUALS(GetPreOffsetVerticalLineAlignment(lineOne, Dali::Toolkit::DevelText::VerticalLineAlignment::BOTTOM), 19.0f, TEST_LOCATION);
125
126   DALI_TEST_EQUALS(GetPreOffsetVerticalLineAlignment(lineTwo, Dali::Toolkit::DevelText::VerticalLineAlignment::TOP), 0.0f, TEST_LOCATION);
127   DALI_TEST_EQUALS(GetPreOffsetVerticalLineAlignment(lineTwo, Dali::Toolkit::DevelText::VerticalLineAlignment::MIDDLE), 0.0f, TEST_LOCATION);
128   DALI_TEST_EQUALS(GetPreOffsetVerticalLineAlignment(lineTwo, Dali::Toolkit::DevelText::VerticalLineAlignment::BOTTOM), 0.0f, TEST_LOCATION);
129   END_TEST;
130 }
131
132 int UtcDaliGetPostOffsetVerticalLineAlignmentWithNegativeLineSpacing(void)
133 {
134   tet_infoline(" UtcDaliGetPostOffsetVerticalLineAlignmentWithNegativeLineSpacing ");
135   ToolkitTestApplication application;
136
137   uint32_t expectedNumberOfLines = 2u;
138
139   // Creates a text controller.
140   ControllerPtr controller = Controller::New();
141
142   // Configures the text controller similarly to the text-label.
143   ConfigureTextLabel(controller);
144
145   // Sets the text.
146   controller->SetMarkupProcessorEnabled(true);
147   controller->SetTextElideEnabled(false);
148   controller->SetText("<p rel-line-height=0.5>Line one Line two</p>");
149
150   // Creates the text's model and relais-out the text.
151   const Size relayoutSize(120.f, 100.f);
152   controller->Relayout(relayoutSize);
153
154   // Tests the rendering controller has been created.
155   TypesetterPtr typesetter = Typesetter::New(controller->GetTextModel());
156   DALI_TEST_CHECK(typesetter);
157
158   // Tests the view model has been created.
159   ViewModel* model = typesetter->GetViewModel();
160   DALI_TEST_CHECK(model);
161
162   application.SendNotification();
163   application.Render();
164
165   DALI_TEST_EQUALS(model->GetNumberOfLines(), expectedNumberOfLines, TEST_LOCATION);
166   DALI_TEST_CHECK(model->GetLines());
167
168   const LineRun& lineOne = *(model->GetLines() + 0u);
169   const LineRun& lineTwo = *(model->GetLines() + 1u);
170
171   DALI_TEST_EQUALS(GetPostOffsetVerticalLineAlignment(lineOne, Dali::Toolkit::DevelText::VerticalLineAlignment::TOP), -9.5f, TEST_LOCATION);
172   DALI_TEST_EQUALS(GetPostOffsetVerticalLineAlignment(lineOne, Dali::Toolkit::DevelText::VerticalLineAlignment::MIDDLE), -9.5f, TEST_LOCATION);
173   DALI_TEST_EQUALS(GetPostOffsetVerticalLineAlignment(lineOne, Dali::Toolkit::DevelText::VerticalLineAlignment::BOTTOM), -9.5f, TEST_LOCATION);
174
175   DALI_TEST_EQUALS(GetPostOffsetVerticalLineAlignment(lineTwo, Dali::Toolkit::DevelText::VerticalLineAlignment::TOP), 0.0f, TEST_LOCATION);
176   DALI_TEST_EQUALS(GetPostOffsetVerticalLineAlignment(lineTwo, Dali::Toolkit::DevelText::VerticalLineAlignment::MIDDLE), 0.0f, TEST_LOCATION);
177   DALI_TEST_EQUALS(GetPostOffsetVerticalLineAlignment(lineTwo, Dali::Toolkit::DevelText::VerticalLineAlignment::BOTTOM), 0.0f, TEST_LOCATION);
178   END_TEST;
179 }
180
181 int UtcDaliGetPostOffsetVerticalLineAlignmentWithPositiveLineSpacing(void)
182 {
183   tet_infoline(" UtcDaliGetPostOffsetVerticalLineAlignmentWithPositiveLineSpacing ");
184   ToolkitTestApplication application;
185
186   uint32_t expectedNumberOfLines = 2u;
187
188   // Creates a text controller.
189   ControllerPtr controller = Controller::New();
190
191   // Configures the text controller similarly to the text-label.
192   ConfigureTextLabel(controller);
193
194   // Sets the text.
195   controller->SetMarkupProcessorEnabled(true);
196   controller->SetTextElideEnabled(false);
197   controller->SetText("<p rel-line-height=2.0>Line one Line two</p>");
198
199   // Creates the text's model and relais-out the text.
200   const Size relayoutSize(120.f, 100.f);
201   controller->Relayout(relayoutSize);
202
203   // Tests the rendering controller has been created.
204   TypesetterPtr typesetter = Typesetter::New(controller->GetTextModel());
205   DALI_TEST_CHECK(typesetter);
206
207   // Tests the view model has been created.
208   ViewModel* model = typesetter->GetViewModel();
209   DALI_TEST_CHECK(model);
210
211   application.SendNotification();
212   application.Render();
213
214   DALI_TEST_EQUALS(model->GetNumberOfLines(), expectedNumberOfLines, TEST_LOCATION);
215   DALI_TEST_CHECK(model->GetLines());
216
217   const LineRun& lineOne = *(model->GetLines() + 0u);
218   const LineRun& lineTwo = *(model->GetLines() + 1u);
219
220   DALI_TEST_EQUALS(GetPostOffsetVerticalLineAlignment(lineOne, Dali::Toolkit::DevelText::VerticalLineAlignment::TOP), 19.0f, TEST_LOCATION);
221   DALI_TEST_EQUALS(GetPostOffsetVerticalLineAlignment(lineOne, Dali::Toolkit::DevelText::VerticalLineAlignment::MIDDLE), 9.5f, TEST_LOCATION);
222   DALI_TEST_EQUALS(GetPostOffsetVerticalLineAlignment(lineOne, Dali::Toolkit::DevelText::VerticalLineAlignment::BOTTOM), 0.0f, TEST_LOCATION);
223
224   DALI_TEST_EQUALS(GetPostOffsetVerticalLineAlignment(lineTwo, Dali::Toolkit::DevelText::VerticalLineAlignment::TOP), 0.0f, TEST_LOCATION);
225   DALI_TEST_EQUALS(GetPostOffsetVerticalLineAlignment(lineTwo, Dali::Toolkit::DevelText::VerticalLineAlignment::MIDDLE), 0.0f, TEST_LOCATION);
226   DALI_TEST_EQUALS(GetPostOffsetVerticalLineAlignment(lineTwo, Dali::Toolkit::DevelText::VerticalLineAlignment::BOTTOM), 0.0f, TEST_LOCATION);
227   END_TEST;
228 }