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>
24 #include <dali-toolkit/internal/controls/text-controls/text-field-impl.h>
25 #include <dali-toolkit/internal/text/rendering/atlas/atlas-glyph-manager.h>
26 #include <dali-toolkit/internal/text/text-controller-impl.h>
27 #include <dali-toolkit/internal/text/text-controller.h>
30 using namespace Toolkit;
33 int UtcDaliTextFieldMultipleBackgroundText(void)
35 ToolkitTestApplication application;
36 tet_infoline("UtcDaliTextFieldMultipleBackgroundText");
38 // Create a text field
39 TextField textField = TextField::New();
40 textField.SetProperty(Actor::Property::SIZE, Vector2(400.f, 60.f));
41 textField.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
42 textField.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
44 // Add the text field to the stage
45 application.GetScene().Add(textField);
47 application.SendNotification();
50 Toolkit::Internal::TextField& textFieldImpl = GetImpl(textField);
51 ControllerPtr controller = textFieldImpl.GetTextController();
52 Controller::Impl& controllerImpl = Controller::Impl::GetImplementation(*controller.Get());
54 // Add multiple background colors for the text.
55 ColorRun backgroundColorRun1;
56 backgroundColorRun1.characterRun.characterIndex = 0u;
57 backgroundColorRun1.characterRun.numberOfCharacters = 1u;
58 backgroundColorRun1.color = Color::RED;
59 controllerImpl.mModel->mLogicalModel->mBackgroundColorRuns.PushBack(backgroundColorRun1);
61 ColorRun backgroundColorRun2;
62 backgroundColorRun2.characterRun.characterIndex = 5u;
63 backgroundColorRun2.characterRun.numberOfCharacters = 8u;
64 backgroundColorRun2.color = Color::CYAN;
65 controllerImpl.mModel->mLogicalModel->mBackgroundColorRuns.PushBack(backgroundColorRun2);
67 ColorRun backgroundColorRun3;
68 backgroundColorRun3.characterRun.characterIndex = 23u;
69 backgroundColorRun3.characterRun.numberOfCharacters = 6u;
70 backgroundColorRun3.color = Color::GREEN;
71 controllerImpl.mModel->mLogicalModel->mBackgroundColorRuns.PushBack(backgroundColorRun3);
73 // Check the case where there is only one character in the text
74 controller->SetText("S");
76 application.SendNotification();
79 // The offscreen root actor should have one child: the renderable.
80 Actor stencil = textField.GetChildAt(0u);
81 DALI_TEST_CHECK(stencil.GetChildCount() == 1u);
83 // The renderable actor should have two children: the text and the background.
84 Actor renderableActor = stencil.GetChildAt(0u);
85 DALI_TEST_CHECK(renderableActor.GetChildCount() == 2u);
87 // Check that the background is created
88 Actor backgroundActor = renderableActor.GetChildAt(0u);
89 DALI_TEST_CHECK(backgroundActor);
90 DALI_TEST_CHECK(backgroundActor.GetProperty<std::string>(Dali::Actor::Property::NAME) == "TextBackgroundColorActor");
92 // Change the text to contain more characters
93 controller->SetText("Text Multiple Background Test");
95 application.SendNotification();
98 // Highlight the whole text
99 textFieldImpl.SelectWholeText();
101 application.SendNotification();
102 application.Render();
104 // Now the offscreen root actor should have three children: the renderable, the highlight, and the background.
105 DALI_TEST_CHECK(stencil.GetChildCount() == 3u);
106 // The renderable actor should have one child only: the text
107 DALI_TEST_CHECK(renderableActor.GetChildCount() == 1u);
109 // The background should now be lowered below the highlight
110 backgroundActor = stencil.GetChildAt(0u);
111 DALI_TEST_CHECK(backgroundActor);
112 DALI_TEST_CHECK(backgroundActor.GetProperty<std::string>(Dali::Actor::Property::NAME) == "TextBackgroundColorActor");
117 int UtcDaliTextFieldSelectText(void)
119 ToolkitTestApplication application;
120 tet_infoline("UtcDaliTextFieldSelectText");
122 // Create a text field
123 TextField textField = TextField::New();
124 textField.SetProperty(Actor::Property::SIZE, Vector2(400.f, 60.f));
125 textField.SetProperty(TextField::Property::TEXT, "Hello World");
127 // Add the text field to the stage
128 application.GetScene().Add(textField);
130 application.SendNotification();
131 application.Render();
133 Toolkit::Internal::TextField& textFieldImpl = GetImpl(textField);
135 application.SendNotification();
136 application.Render();
138 // Highlight the whole text
139 textFieldImpl.SelectWholeText();
141 application.SendNotification();
142 application.Render();
144 DALI_TEST_CHECK(textFieldImpl.GetSelectedText() == "Hello World");
147 textFieldImpl.SelectNone();
149 application.SendNotification();
150 application.Render();
152 DALI_TEST_CHECK(textFieldImpl.GetSelectedText() == "");
157 int UtcDaliTextFieldMarkupUnderline(void)
159 ToolkitTestApplication application;
160 tet_infoline(" UtcDaliTextFieldMarkupUnderline ");
162 TextField textField = TextField::New();
164 application.GetScene().Add(textField);
166 textField.SetProperty(TextField::Property::TEXT, "<u>ABC</u>EF<u>GH</u>");
167 textField.SetProperty(TextField ::Property::ENABLE_MARKUP, true);
169 application.SendNotification();
170 application.Render();
172 uint32_t expectedNumberOfUnderlinedGlyphs = 5u;
174 Toolkit::Internal::TextField& textFieldImpl = GetImpl(textField);
175 const Text::Length numberOfUnderlineRuns = textFieldImpl.GetTextController()->GetTextModel()->GetNumberOfUnderlineRuns();
177 DALI_TEST_EQUALS(numberOfUnderlineRuns, expectedNumberOfUnderlinedGlyphs, TEST_LOCATION);
179 Vector<UnderlinedGlyphRun> underlineRuns;
180 underlineRuns.Resize(numberOfUnderlineRuns);
181 textFieldImpl.GetTextController()->GetTextModel()->GetUnderlineRuns(underlineRuns.Begin(), 0u, numberOfUnderlineRuns);
184 DALI_TEST_EQUALS(underlineRuns[0u].glyphRun.glyphIndex, 0u, TEST_LOCATION);
185 DALI_TEST_EQUALS(underlineRuns[1u].glyphRun.glyphIndex, 1u, TEST_LOCATION);
186 DALI_TEST_EQUALS(underlineRuns[2u].glyphRun.glyphIndex, 2u, TEST_LOCATION);
189 DALI_TEST_EQUALS(underlineRuns[3u].glyphRun.glyphIndex, 5u, TEST_LOCATION);
190 DALI_TEST_EQUALS(underlineRuns[4u].glyphRun.glyphIndex, 6u, TEST_LOCATION);
195 int UtcDaliTextFieldMarkupUnderlineAttributes(void)
197 ToolkitTestApplication application;
198 tet_infoline(" UtcDaliTextFieldMarkupUnderlineAttributes ");
200 TextField textField = TextField::New();
202 application.GetScene().Add(textField);
204 std::string testText =
205 "start<u>ABC1</u>then"
206 "<u type='solid'>ABC2</u>then"
207 "<u type='dashed'>ABC3</u>then"
208 "<u type='double'>ABC4</u>then"
209 "<u color='green'>ABC5</u>then"
210 "<u height='5.0f'>ABC6</u>then"
211 "<u type='dashed' dash-gap='3.0f'>ABC7</u>then"
212 "<u type='dashed' dash-width='4.0f'>ABC8</u>then"
213 "<u color='blue' type='dashed' height='4.0f' dash-gap='2.0f' dash-width='3.0f'>ABC9</u>end"
217 textField.SetProperty(TextField::Property::TEXT, testText);
218 textField.SetProperty(TextField ::Property::ENABLE_MARKUP, true);
220 application.SendNotification();
221 application.Render();
223 const uint32_t NUMBER_OF_CASES = 9u;
224 uint32_t expectedNumberOfUnderlinedGlyphs = 36u;
226 Toolkit::Internal::TextField& textFieldImpl = GetImpl(textField);
227 const Text::Length numberOfUnderlineRuns = textFieldImpl.GetTextController()->GetTextModel()->GetNumberOfUnderlineRuns();
229 DALI_TEST_EQUALS(numberOfUnderlineRuns, expectedNumberOfUnderlinedGlyphs, TEST_LOCATION);
231 Vector<UnderlinedGlyphRun> underlineRuns;
232 underlineRuns.Resize(numberOfUnderlineRuns);
233 textFieldImpl.GetTextController()->GetTextModel()->GetUnderlineRuns(underlineRuns.Begin(), 0u, numberOfUnderlineRuns);
240 GlyphIndex startGlyphIndex;
241 GlyphIndex endGlyphIndex;
242 UnderlineStyleProperties properties;
253 Text::Underline::SOLID,
265 //<u type='solid'>ABC2</u>
266 {"<u type='solid'>ABC2</u>",
272 Text::Underline::SOLID,
284 //<u type='dashed'>ABC3</u>
285 {"<u type='dashed'>ABC3</u>",
291 Text::Underline::DASHED,
303 //<u type='double'>ABC4</u>
304 {"<u type='double'>ABC4</u>",
310 Text::Underline::DOUBLE,
322 //<u color='green'>ABC5</u>
323 {"<u color='green'>ABC5</u>",
329 Text::Underline::SOLID,
341 //<u height='5.0f'>ABC6</u>
342 {"<u height='5.0f'>ABC6</u>",
348 Text::Underline::SOLID,
360 //<u type='dashed' dash-gap='3.0f'>ABC7</u>
361 {"<u type='dashed' dash-gap='3.0f'>ABC7</u>",
367 Text::Underline::DASHED,
379 //<u type='dashed' dash-width='4.0f'>ABC8</u>
380 {"<u type='dashed' dash-width='4.0f'>ABC8</u>",
386 Text::Underline::DASHED,
398 //<u color='blue' type='dashed' height='4.0f' dash-gap='2.0f' dash-width='3.0f'>
399 {"<u color='blue' type='dashed' height='4.0f' dash-gap='2.0f' dash-width='3.0f'>",
405 Text::Underline::DASHED,
419 for(uint32_t i = 0; i < NUMBER_OF_CASES; i++)
421 tet_infoline(data[i].title.c_str());
422 DALI_TEST_EQUALS(underlineRuns[data[i].startIndex].glyphRun.glyphIndex, data[i].startGlyphIndex, TEST_LOCATION);
423 DALI_TEST_EQUALS(underlineRuns[data[i].endIndex].glyphRun.glyphIndex, data[i].endGlyphIndex, TEST_LOCATION);
425 DALI_TEST_CHECK(data[i].properties == underlineRuns[data[i].startIndex].properties);
426 DALI_TEST_CHECK(data[i].properties == underlineRuns[data[i].endIndex].properties);
432 int UtcDaliTextFieldFontPointSizeLargerThanAtlas(void)
434 ToolkitTestApplication application;
435 tet_infoline(" UtcDaliTextFieldFontPointSizeLargerThanAtlas ");
437 // Create a Text field
438 TextField textField = TextField::New();
439 //Set size to avoid automatic eliding
440 textField.SetProperty(Actor::Property::SIZE, Vector2(1025, 1025));
441 //Set very large font-size using point-size
442 textField.SetProperty(TextField::Property::POINT_SIZE, 1000);
443 //Specify font-family
444 textField.SetProperty(TextField::Property::FONT_FAMILY, "DejaVu Sans");
445 //Set text to check if appear or not
446 textField.SetProperty(TextField::Property::TEXT, "A");
448 application.GetScene().Add(textField);
450 application.SendNotification();
451 application.Render();
453 //Check if Glyph is added to AtlasGlyphManger or not
454 int countAtlas = AtlasGlyphManager::Get().GetMetrics().mAtlasMetrics.mAtlasCount;
455 DALI_TEST_EQUALS(countAtlas, 1, TEST_LOCATION);
460 int UtcDaliTextFieldFontPointSizeLargerThanAtlasPlaceholderCase(void)
462 ToolkitTestApplication application;
463 tet_infoline(" UtcDaliTextFieldFontPointSizeLargerThanAtlasPlaceholderCase ");
465 //Set Map of placeholder: text, font-family and point-size
466 Property::Map placeholderMapSet;
467 placeholderMapSet["text"] = "A";
468 placeholderMapSet["fontFamily"] = "DejaVu Sans";
469 placeholderMapSet["pixelSize"] = 1000.0f;
471 // Create a text editor
472 TextField textField = TextField::New();
473 //Set size to avoid automatic eliding
474 textField.SetProperty(Actor::Property::SIZE, Vector2(1025, 1025));
476 textField.SetProperty(TextField::Property::PLACEHOLDER, placeholderMapSet);
478 application.GetScene().Add(textField);
480 application.SendNotification();
481 application.Render();
483 //Check if Glyph is added to AtlasGlyphManger or not
484 int countAtlas = AtlasGlyphManager::Get().GetMetrics().mAtlasMetrics.mAtlasCount;
485 DALI_TEST_EQUALS(countAtlas, 1, TEST_LOCATION);
490 int UtcDaliTextFieldBackgroundTag(void)
492 ToolkitTestApplication application;
493 tet_infoline("UtcDaliTextFieldBackgroundTag\n");
495 TextField field = TextField::New();
496 DALI_TEST_CHECK(field);
498 field.SetProperty(TextField ::Property::ENABLE_MARKUP, true);
499 field.SetProperty(TextField::Property::TEXT, "H<background color='red'>e</background> Worl<background color='yellow'>d</background>");
500 application.GetScene().Add(field);
501 application.SendNotification();
502 application.Render();
504 Toolkit::Internal::TextField& fieldImpl = GetImpl(field);
505 const ColorIndex* const backgroundColorIndicesBuffer = fieldImpl.GetTextController()->GetTextModel()->GetBackgroundColorIndices();
507 DALI_TEST_CHECK(backgroundColorIndicesBuffer);
510 DALI_TEST_EQUALS(backgroundColorIndicesBuffer[0], 0u, TEST_LOCATION);
513 DALI_TEST_EQUALS(backgroundColorIndicesBuffer[1], 1u, TEST_LOCATION);
516 DALI_TEST_EQUALS(backgroundColorIndicesBuffer[7], 2u, TEST_LOCATION);
521 int UtcDaliToolkitTextFieldEllipsisInternalAPIs(void)
523 ToolkitTestApplication application;
524 tet_infoline(" UtcDaliToolkitTextFieldEllipsisInternalAPIs ");
525 TextField textField = TextField::New();
527 Toolkit::Internal::TextField& textFieldImpl = GetImpl(textField);
528 Text::ViewInterface& view = textFieldImpl.GetTextController()->GetView();
530 tet_infoline(" UtcDaliToolkitTextFieldEllipsisInternalAPIs - ELLIPSIS Disabled");
531 textField.SetProperty(DevelTextField::Property::ELLIPSIS, false);
532 DALI_TEST_EQUALS(textField.GetProperty<bool>(DevelTextField::Property::ELLIPSIS), false, TEST_LOCATION);
533 DALI_TEST_CHECK(!(view.IsTextElideEnabled()));
535 tet_infoline(" UtcDaliToolkitTextFieldEllipsisInternalAPIs - ELLIPSIS Enabled");
536 textField.SetProperty(DevelTextField::Property::ELLIPSIS, true);
537 DALI_TEST_EQUALS(textField.GetProperty<bool>(DevelTextField::Property::ELLIPSIS), true, TEST_LOCATION);
538 DALI_TEST_CHECK(view.IsTextElideEnabled());
540 tet_infoline(" UtcDaliToolkitTextFieldEllipsisInternalAPIs - GetStartIndexOfElidedGlyphs Default");
541 DALI_TEST_EQUALS(view.GetStartIndexOfElidedGlyphs(), 0u, TEST_LOCATION);
543 tet_infoline(" UtcDaliToolkitTextFieldEllipsisInternalAPIs - GetEndIndexOfElidedGlyphs Default");
544 DALI_TEST_EQUALS(view.GetEndIndexOfElidedGlyphs(), 0u, TEST_LOCATION);
546 tet_infoline(" UtcDaliToolkitTextFieldEllipsisInternalAPIs - GetFirstMiddleIndexOfElidedGlyphs Default");
547 DALI_TEST_EQUALS(view.GetFirstMiddleIndexOfElidedGlyphs(), 0u, TEST_LOCATION);
549 tet_infoline(" UtcDaliToolkitTextFieldEllipsisInternalAPIs - GetSecondMiddleIndexOfElidedGlyphs Default");
550 DALI_TEST_EQUALS(view.GetSecondMiddleIndexOfElidedGlyphs(), 0u, TEST_LOCATION);
554 int UtcDaliTextFieldTextWithSpan(void)
556 ToolkitTestApplication application;
557 tet_infoline("UtcDaliTextFieldTextWithSpan\n");
559 TextField field = TextField::New();
560 DALI_TEST_CHECK(field);
562 field.SetProperty(TextField ::Property::ENABLE_MARKUP, true);
563 field.SetProperty(TextField::Property::TEXT, "Hello Span");
564 application.GetScene().Add(field);
566 application.SendNotification();
567 application.Render();
569 Vector3 originalSize = field.GetNaturalSize();
570 field.SetProperty(TextField::Property::TEXT, "H<span font-size='45' font-family='DejaVu Sans' font-width='condensed' font-slant='italic' text-color='red'>ello</span> Span");
572 application.SendNotification();
573 application.Render();
575 Vector3 spanSize = field.GetNaturalSize();
577 DALI_TEST_GREATER(spanSize.width, originalSize.width, TEST_LOCATION);
579 Toolkit::Internal::TextField& fieldImpl = GetImpl(field);
580 const ColorIndex* const colorIndicesBuffer1 = fieldImpl.GetTextController()->GetTextModel()->GetColorIndices();
582 DALI_TEST_CHECK(colorIndicesBuffer1);
585 DALI_TEST_EQUALS(colorIndicesBuffer1[0], 0u, TEST_LOCATION);
588 DALI_TEST_EQUALS(colorIndicesBuffer1[1], 1u, TEST_LOCATION);
591 DALI_TEST_EQUALS(colorIndicesBuffer1[6], 0u, TEST_LOCATION);
593 field.SetProperty(TextField::Property::TEXT, "<span font-size='45'>H</span>ello <span text-color='red'>S</span>pan");
595 application.SendNotification();
596 application.Render();
598 const ColorIndex* const colorIndicesBuffer2 = fieldImpl.GetTextController()->GetTextModel()->GetColorIndices();
600 DALI_TEST_CHECK(colorIndicesBuffer2);
603 DALI_TEST_EQUALS(colorIndicesBuffer2[0], 0u, TEST_LOCATION);
606 DALI_TEST_EQUALS(colorIndicesBuffer2[1], 0u, TEST_LOCATION);
609 DALI_TEST_EQUALS(colorIndicesBuffer2[6], 1u, TEST_LOCATION);
612 DALI_TEST_EQUALS(colorIndicesBuffer2[7], 0u, TEST_LOCATION);
617 int UtcDaliTextFieldControlBackgroundColor(void)
619 ToolkitTestApplication application;
620 tet_infoline(" UtcDaliTextFieldControlBackgroundColor\n");
622 TextField field = TextField::New();
623 DALI_TEST_CHECK(field);
625 Vector4 backgroundColor;
627 field.SetProperty(TextField::Property::TEXT, "Background Color");
628 application.GetScene().Add(field);
629 application.SendNotification();
630 application.Render();
632 Toolkit::Internal::TextField& fieldImpl = GetImpl(field);
633 ControllerPtr controller = fieldImpl.GetTextController();
634 Controller::Impl& controllerImpl = Controller::Impl::GetImplementation(*controller.Get());
636 // Default color is transparent
637 controllerImpl.mEditableControlInterface->GetControlBackgroundColor(backgroundColor);
638 DALI_TEST_EQUALS(backgroundColor, Color::TRANSPARENT, TEST_LOCATION);
640 // Set background color to red
641 field.SetBackgroundColor(Color::RED);
642 application.SendNotification();
643 application.Render();
646 controllerImpl.mEditableControlInterface->GetControlBackgroundColor(backgroundColor);
647 DALI_TEST_EQUALS(backgroundColor, Color::RED, TEST_LOCATION);
652 int UtcDaliTextFieldMarkupStrikethrough(void)
654 ToolkitTestApplication application;
655 tet_infoline(" UtcDaliTextFieldMarkupStrikethrough ");
657 TextField textField = TextField::New();
659 application.GetScene().Add(textField);
661 textField.SetProperty(TextField::Property::TEXT, "<s>ABC</s>EF<s color='red'>GH</s>");
662 textField.SetProperty(TextField ::Property::ENABLE_MARKUP, true);
664 application.SendNotification();
665 application.Render();
667 uint32_t expectedNumberOfStrikethroughGlyphs = 2u;
669 Toolkit::Internal::TextField& textFieldImpl = GetImpl(textField);
670 const Text::Length numberOfStrikethroughRuns = textFieldImpl.GetTextController()->GetTextModel()->GetNumberOfStrikethroughRuns();
672 DALI_TEST_EQUALS(numberOfStrikethroughRuns, expectedNumberOfStrikethroughGlyphs, TEST_LOCATION);
674 Vector<StrikethroughGlyphRun> strikethroughRuns;
675 strikethroughRuns.Resize(numberOfStrikethroughRuns);
676 textFieldImpl.GetTextController()->GetTextModel()->GetStrikethroughRuns(strikethroughRuns.Begin(), 0u, numberOfStrikethroughRuns);
678 //ABC have strikethrough
679 DALI_TEST_EQUALS(strikethroughRuns[0u].glyphRun.glyphIndex, 0u, TEST_LOCATION);
680 DALI_TEST_EQUALS(strikethroughRuns[0u].glyphRun.numberOfGlyphs, 3u, TEST_LOCATION);
681 DALI_TEST_CHECK(!strikethroughRuns[0u].isColorSet);
683 //GH have strikethrough
684 DALI_TEST_EQUALS(strikethroughRuns[1u].glyphRun.glyphIndex, 5u, TEST_LOCATION);
685 DALI_TEST_EQUALS(strikethroughRuns[1u].glyphRun.numberOfGlyphs, 2u, TEST_LOCATION);
686 DALI_TEST_CHECK(strikethroughRuns[1u].isColorSet);
691 int UtcDaliTextFieldMarkupStrikethroughNoEndTag(void)
693 ToolkitTestApplication application;
694 tet_infoline(" UtcDaliTextFieldMarkupStrikethroughNoEndTag ");
696 TextField textField = TextField::New();
698 application.GetScene().Add(textField);
700 textField.SetProperty(TextField::Property::TEXT, "<s>ABC");
701 textField.SetProperty(TextField ::Property::ENABLE_MARKUP, true);
703 application.SendNotification();
704 application.Render();
706 uint32_t expectedNumberOfStrikethroughGlyphs = 0u;
708 Toolkit::Internal::TextField& textFieldImpl = GetImpl(textField);
709 Text::Length numberOfStrikethroughRuns = textFieldImpl.GetTextController()->GetTextModel()->GetNumberOfStrikethroughRuns();
711 DALI_TEST_EQUALS(numberOfStrikethroughRuns, expectedNumberOfStrikethroughGlyphs, TEST_LOCATION);