Merge "Support Ellipsis Position Property" into devel/master
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-internal / utc-Dali-TextLabel-internal.cpp
1 /*
2  * Copyright (c) 2021 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 #include <stdlib.h>
20
21 #include <dali-toolkit-test-suite-utils.h>
22 #include <dali-toolkit/dali-toolkit.h>
23
24 #include <dali-toolkit/internal/controls/text-controls/text-label-impl.h>
25 #include <dali-toolkit/internal/text/text-controller.h>
26 #include <dali-toolkit/internal/text/text-controller-impl.h>
27 #include <dali-toolkit/internal/text/rendering/text-typesetter.h>
28 #include <dali-toolkit/internal/text/rendering/view-model.h>
29
30 using namespace Dali;
31 using namespace Toolkit;
32 using namespace Text;
33
34 int UtcDaliTextLabelMarkupUnderline(void)
35 {
36   ToolkitTestApplication application;
37   tet_infoline(" UtcDaliTextLabelMarkupUnderline ");
38
39   TextLabel textLabel = TextLabel::New();
40
41   application.GetScene().Add( textLabel );
42
43   textLabel.SetProperty( TextLabel::Property::TEXT, "<u>ABC</u>EF<u>GH</u>" );
44   textLabel.SetProperty( TextLabel ::Property::ENABLE_MARKUP,  true );
45
46   application.SendNotification();
47   application.Render();
48
49   uint32_t expectedNumberOfUnderlinedGlyphs = 5u;
50
51   Toolkit::Internal::TextLabel& textLabelImpl = GetImpl( textLabel );
52   const Text::Length numberOfUnderlineRuns = textLabelImpl.GetTextController()->GetTextModel()->GetNumberOfUnderlineRuns();
53
54   DALI_TEST_EQUALS( numberOfUnderlineRuns, expectedNumberOfUnderlinedGlyphs, TEST_LOCATION );
55
56   Vector<GlyphRun> underlineRuns;
57   underlineRuns.Resize(numberOfUnderlineRuns);
58   textLabelImpl.GetTextController()->GetTextModel()->GetUnderlineRuns(underlineRuns.Begin(), 0u, numberOfUnderlineRuns);
59
60   //ABC are underlined
61   DALI_TEST_EQUALS( underlineRuns[0u].glyphIndex, 0u, TEST_LOCATION);
62   DALI_TEST_EQUALS( underlineRuns[1u].glyphIndex, 1u, TEST_LOCATION);
63   DALI_TEST_EQUALS( underlineRuns[2u].glyphIndex, 2u, TEST_LOCATION);
64
65   //GH are underlined
66   DALI_TEST_EQUALS( underlineRuns[3u].glyphIndex, 5u, TEST_LOCATION);
67   DALI_TEST_EQUALS( underlineRuns[4u].glyphIndex, 6u, TEST_LOCATION);
68
69   END_TEST;
70
71 }
72
73 int UtcDaliTextLabelBackgroundTag(void)
74 {
75   ToolkitTestApplication application;
76   tet_infoline("UtcDaliTextLabelBackgroundTag\n");
77
78   TextLabel label = TextLabel::New();
79   DALI_TEST_CHECK( label );
80
81   label.SetProperty( TextLabel ::Property::ENABLE_MARKUP,  true );
82   label.SetProperty( TextLabel::Property::TEXT, "H<background color='red'>e</background> Worl<background color='yellow'>d</background>" );
83   application.GetScene().Add( label );
84   application.SendNotification();
85   application.Render();
86
87   Toolkit::Internal::TextLabel& labelImpl = GetImpl( label );
88   const ColorIndex* const backgroundColorIndicesBuffer = labelImpl.GetTextController()->GetTextModel()->GetBackgroundColorIndices();
89
90   DALI_TEST_CHECK( backgroundColorIndicesBuffer );
91
92   //default color
93   DALI_TEST_EQUALS( backgroundColorIndicesBuffer[0], 0u, TEST_LOCATION);
94
95   //red color
96   DALI_TEST_EQUALS( backgroundColorIndicesBuffer[1], 1u, TEST_LOCATION);
97
98   //yellow color
99   DALI_TEST_EQUALS( backgroundColorIndicesBuffer[7], 2u, TEST_LOCATION);
100
101   END_TEST;
102 }
103
104 int UtcDaliToolkitTextlabelEllipsisInternalAPIs(void)
105 {
106   ToolkitTestApplication application;
107   tet_infoline(" UtcDaliToolkitTextlabelEllipsisInternalAPIs ");
108   TextLabel textLabel = TextLabel::New();
109
110   Toolkit::Internal::TextLabel& textLabelImpl = GetImpl( textLabel );
111   const ModelInterface* const textModel = textLabelImpl.GetTextController()->GetTextModel();
112
113
114   tet_infoline(" UtcDaliToolkitTextlabelEllipsisInternalAPIs - ELLIPSIS Disabled");
115   textLabel.SetProperty(DevelTextLabel::Property::ELLIPSIS, false);
116   DALI_TEST_EQUALS( textLabel.GetProperty< bool >( DevelTextLabel::Property::ELLIPSIS ), false, TEST_LOCATION );
117   DALI_TEST_CHECK(!(textModel->IsTextElideEnabled()));
118
119   tet_infoline(" UtcDaliToolkitTextlabelEllipsisInternalAPIs - ELLIPSIS Enabled");
120   textLabel.SetProperty(DevelTextLabel::Property::ELLIPSIS, true);
121   DALI_TEST_EQUALS( textLabel.GetProperty< bool >( DevelTextLabel::Property::ELLIPSIS ), true, TEST_LOCATION );
122   DALI_TEST_CHECK(textModel->IsTextElideEnabled());
123
124   tet_infoline(" UtcDaliToolkitTextlabelEllipsisInternalAPIs - GetStartIndexOfElidedGlyphs Default");
125   DALI_TEST_EQUALS( textModel->GetStartIndexOfElidedGlyphs(), 0u, TEST_LOCATION );
126
127   tet_infoline(" UtcDaliToolkitTextlabelEllipsisInternalAPIs - GetEndIndexOfElidedGlyphs Default");
128   DALI_TEST_EQUALS( textModel->GetEndIndexOfElidedGlyphs(), 0u, TEST_LOCATION );
129
130   tet_infoline(" UtcDaliToolkitTextlabelEllipsisInternalAPIs - GetFirstMiddleIndexOfElidedGlyphs Default");
131   DALI_TEST_EQUALS( textModel->GetFirstMiddleIndexOfElidedGlyphs(), 0u, TEST_LOCATION );
132
133   tet_infoline(" UtcDaliToolkitTextlabelEllipsisInternalAPIs - GetSecondMiddleIndexOfElidedGlyphs Default");
134   DALI_TEST_EQUALS( textModel->GetSecondMiddleIndexOfElidedGlyphs(), 0u, TEST_LOCATION );
135
136   // Tests the rendering controller has been created.
137   TypesetterPtr typesetter = Typesetter::New( textModel );
138   DALI_TEST_CHECK(typesetter);
139
140   // Tests the view model has been created.
141   ViewModel* model = typesetter->GetViewModel();
142
143   tet_infoline(" UtcDaliToolkitTextlabelEllipsisInternalAPIs - IsTextElideEnabled ViewModel");
144   DALI_TEST_CHECK(model->IsTextElideEnabled());
145
146   tet_infoline(" UtcDaliToolkitTextlabelEllipsisInternalAPIs - GetStartIndexOfElidedGlyphs ViewModel");
147   DALI_TEST_EQUALS( model->GetStartIndexOfElidedGlyphs(), 0u, TEST_LOCATION );
148
149   tet_infoline(" UtcDaliToolkitTextlabelEllipsisInternalAPIs - GetEndIndexOfElidedGlyphs ViewModel");
150   DALI_TEST_EQUALS( model->GetEndIndexOfElidedGlyphs(), 0u, TEST_LOCATION );
151
152   tet_infoline(" UtcDaliToolkitTextlabelEllipsisInternalAPIs - GetFirstMiddleIndexOfElidedGlyphs ViewModel");
153   DALI_TEST_EQUALS( model->GetFirstMiddleIndexOfElidedGlyphs(), 0u, TEST_LOCATION );
154
155   tet_infoline(" UtcDaliToolkitTextlabelEllipsisInternalAPIs - GetSecondMiddleIndexOfElidedGlyphs ViewModel");
156   DALI_TEST_EQUALS( model->GetSecondMiddleIndexOfElidedGlyphs(), 0u, TEST_LOCATION );
157
158   END_TEST;
159 }
160 int UtcDaliTextLabelTextWithSpan(void)
161 {
162   ToolkitTestApplication application;
163   tet_infoline("UtcDaliTextLabelTextWithSpan\n");
164
165   TextLabel label = TextLabel::New();
166   DALI_TEST_CHECK( label );
167
168   label.SetProperty( TextLabel ::Property::ENABLE_MARKUP,  true );
169   label.SetProperty( TextLabel::Property::TEXT, "Hello Span" );
170   application.GetScene().Add( label );
171
172   application.SendNotification();
173   application.Render();
174
175   Vector3 originalSize = label.GetNaturalSize();
176   label.SetProperty( TextLabel::Property::TEXT, "H<span font-size='45' font-family='DejaVu Sans' font-width='condensed' font-slant='italic' text-color='red'>ello</span> Span" );
177
178   application.SendNotification();
179   application.Render();
180
181   Vector3 spanSize = label.GetNaturalSize();
182
183   DALI_TEST_GREATER(spanSize.width, originalSize.width, TEST_LOCATION);
184
185   Toolkit::Internal::TextLabel& labelImpl = GetImpl( label );
186   const ColorIndex* const colorIndicesBuffer1 = labelImpl.GetTextController()->GetTextModel()->GetColorIndices();
187
188   DALI_TEST_CHECK( colorIndicesBuffer1 );
189
190   //default color
191   DALI_TEST_EQUALS( colorIndicesBuffer1[0], 0u, TEST_LOCATION);
192
193   //span color
194   DALI_TEST_EQUALS( colorIndicesBuffer1[1], 1u, TEST_LOCATION);
195
196   //default color
197   DALI_TEST_EQUALS( colorIndicesBuffer1[6], 0u, TEST_LOCATION);
198
199
200   label.SetProperty( TextLabel::Property::TEXT, "<span font-size='45'>H</span>ello <span text-color='red'>S</span>pan" );
201
202   application.SendNotification();
203   application.Render();
204
205   const ColorIndex* const colorIndicesBuffer2 = labelImpl.GetTextController()->GetTextModel()->GetColorIndices();
206
207   DALI_TEST_CHECK( colorIndicesBuffer2 );
208
209   //default color
210   DALI_TEST_EQUALS( colorIndicesBuffer2[0], 0u, TEST_LOCATION);
211
212   //default color
213   DALI_TEST_EQUALS( colorIndicesBuffer2[1], 0u, TEST_LOCATION);
214
215   //span color
216   DALI_TEST_EQUALS( colorIndicesBuffer2[6], 1u, TEST_LOCATION);
217
218   //default color
219   DALI_TEST_EQUALS( colorIndicesBuffer2[7], 0u, TEST_LOCATION);
220
221   END_TEST;
222 }